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-10-28 22:13:08 +02:00
|
|
|
int import_from_maildir(struct session_data *sdata, struct data *data, char *directory, 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-10-28 22:33:47 +02:00
|
|
|
char *p, subdir[SMALLBUFSIZE];
|
2016-03-03 11:04:32 +01:00
|
|
|
struct stat st;
|
|
|
|
|
2017-10-28 22:13:08 +02:00
|
|
|
dir = opendir(directory);
|
2016-03-03 11:04:32 +01:00
|
|
|
if(!dir){
|
2017-10-28 22:13:08 +02:00
|
|
|
printf("cannot open directory: %s\n", 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-10-28 22:13:08 +02:00
|
|
|
snprintf(data->import->filename, SMALLBUFSIZE-1, "%s/%s", 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-10-28 22:33:47 +02:00
|
|
|
snprintf(subdir, sizeof(subdir)-1, "%s", data->import->filename);
|
|
|
|
rc = import_from_maildir(sdata, data, subdir, 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-10-28 22:13:08 +02:00
|
|
|
p = strrchr(directory, '/');
|
2016-03-03 11:04:32 +01:00
|
|
|
if(p) p++;
|
|
|
|
else {
|
2017-10-28 22:13:08 +02:00
|
|
|
printf("ERROR: invalid directory name: '%s'\n", 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;
|
|
|
|
}
|