2016-03-03 11:04:32 +01:00
|
|
|
/*
|
|
|
|
* import_maildir.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <piler.h>
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int import_from_maildir(struct session_data *sdata, struct data *data, struct config *cfg){
|
2016-03-03 11:04:32 +01:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
|
|
|
int rc=ERR, ret=OK, i=0;
|
|
|
|
int folder;
|
2017-08-08 15:34:45 +02:00
|
|
|
char *p;
|
2016-03-03 11:04:32 +01:00
|
|
|
struct stat st;
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
dir = opendir(data->import->directory);
|
2016-03-03 11:04:32 +01:00
|
|
|
if(!dir){
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("cannot open directory: %s\n", data->import->directory);
|
2016-03-03 11:04:32 +01:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while((de = readdir(dir))){
|
|
|
|
if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
snprintf(data->import->filename, SMALLBUFSIZE-1, "%s/%s", data->import->directory, de->d_name);
|
2016-03-03 11:04:32 +01:00
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(stat(data->import->filename, &st) == 0){
|
2016-03-03 11:04:32 +01:00
|
|
|
if(S_ISDIR(st.st_mode)){
|
|
|
|
folder = data->folder;
|
2017-08-08 15:34:45 +02:00
|
|
|
data->import->directory = data->import->filename;
|
|
|
|
rc = import_from_maildir(sdata, data, cfg);
|
2016-03-03 11:04:32 +01:00
|
|
|
data->folder = folder;
|
|
|
|
if(rc == ERR) ret = ERR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if(S_ISREG(st.st_mode)){
|
|
|
|
if(i == 0 && data->recursive_folder_names == 1){
|
2017-08-08 15:34:45 +02:00
|
|
|
p = strrchr(data->import->directory, '/');
|
2016-03-03 11:04:32 +01:00
|
|
|
if(p) p++;
|
|
|
|
else {
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("ERROR: invalid directory name: '%s'\n", data->import->directory);
|
2016-03-03 11:04:32 +01:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:10:09 +02:00
|
|
|
folder = get_folder_id(sdata, data, p, data->folder);
|
2016-03-03 11:04:32 +01:00
|
|
|
if(folder == ERR_FOLDER){
|
2016-04-05 21:10:09 +02:00
|
|
|
folder = add_new_folder(sdata, data, p, data->folder);
|
2016-03-03 11:04:32 +01:00
|
|
|
|
|
|
|
if(folder == ERR_FOLDER){
|
|
|
|
printf("error: cannot get/add folder '%s' to parent id: %d\n", p, data->folder);
|
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data->folder = folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
rc = import_message(sdata, data, cfg);
|
2016-03-03 11:04:32 +01:00
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(rc == OK) (data->import->tot_msgs)++;
|
2016-03-03 11:04:32 +01:00
|
|
|
else if(rc == ERR){
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("ERROR: error importing: '%s'\n", data->import->filename);
|
2016-03-03 11:04:32 +01:00
|
|
|
ret = ERR;
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(data->import->remove_after_import == 1 && rc != ERR) unlink(data->import->filename);
|
2016-03-03 11:04:32 +01:00
|
|
|
|
|
|
|
i++;
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(data->quiet == 0){ printf("processed: %7d\r", data->import->tot_msgs); fflush(stdout); }
|
2016-03-03 11:04:32 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("%s is not a file\n", data->import->filename);
|
2016-03-03 11:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("cannot stat() %s\n", data->import->filename);
|
2016-03-03 11:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|