diff --git a/src/config.h b/src/config.h index 20d18802..bf37cc0b 100644 --- a/src/config.h +++ b/src/config.h @@ -11,9 +11,8 @@ #define PROGNAME "piler" -#define VERSION "1.2.0" - -#define BUILD 954 +#define VERSION "1.3.0" +#define BUILD 955 #define HOSTID "mailarchiver" diff --git a/src/defs.h b/src/defs.h index dcc8580f..c5e37732 100644 --- a/src/defs.h +++ b/src/defs.h @@ -284,6 +284,7 @@ struct memcached_server { struct import { char *extra_recipient; char *move_folder; + char *failed_folder; int status; int total_messages; int processed_messages; diff --git a/src/import.c b/src/import.c index 5b82d4fb..864b3a4d 100644 --- a/src/import.c +++ b/src/import.c @@ -20,7 +20,7 @@ int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg){ int rc=ERR; - char *rule; + char *rule, newpath[SMALLBUFSIZE]; struct stat st; struct parser_state state; struct counters counters; @@ -126,6 +126,11 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da break; } + if(rc != OK && data->import->failed_folder){ + snprintf(newpath, sizeof(newpath)-2, "%s/%s", data->import->failed_folder, filename); + rename(filename, newpath); + } + return rc; } diff --git a/src/misc.c b/src/misc.c index c006a8cc..24a1a540 100644 --- a/src/misc.c +++ b/src/misc.c @@ -609,11 +609,14 @@ void *get_in_addr(struct sockaddr *sa){ } -int can_i_write_current_directory(){ +int can_i_write_directory(char *dir){ int fd; char filename[SMALLBUFSIZE]; - snprintf(filename, sizeof(filename)-1, "__piler_%d", getpid()); + if(dir) + snprintf(filename, sizeof(filename)-1, "%s/__piler_%d", dir, getpid()); + else + snprintf(filename, sizeof(filename)-1, "__piler_%d", getpid()); fd = open(filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP); if(fd == -1){ diff --git a/src/misc.h b/src/misc.h index 89a40500..ced3ab14 100644 --- a/src/misc.h +++ b/src/misc.h @@ -44,7 +44,7 @@ void strtolower(char *s); void *get_in_addr(struct sockaddr *sa); -int can_i_write_current_directory(); +int can_i_write_directory(char *dir); #ifndef _GNU_SOURCE char *strcasestr(const char *s, const char *find); diff --git a/src/pilerexport.c b/src/pilerexport.c index 5595f4f3..3983ff1e 100644 --- a/src/pilerexport.c +++ b/src/pilerexport.c @@ -560,7 +560,7 @@ int main(int argc, char **argv){ regfree(®exp); - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); (void) openlog("pilerexport", LOG_PID, LOG_MAIL); diff --git a/src/pilerimport.c b/src/pilerimport.c index 4ee4df51..4a1333f1 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -53,6 +53,7 @@ void usage(){ printf(" -R Assign IMAP folder names as Piler folder names\n"); printf(" -b Import only this many emails\n"); printf(" -s Start importing POP3 emails from this position\n"); + printf(" -j Move failed to import emails to this folder\n"); printf(" -a Add recipient to the To:/Cc: list\n"); printf(" -D Dry-run, do not import anything\n"); printf(" -o Only download emails for POP3/IMAP import\n"); @@ -83,7 +84,7 @@ int main(int argc, char **argv){ import.import_job_id = import.total_messages = import.total_size = import.processed_messages = import.batch_processing_limit = 0; import.started = import.updated = import.finished = import.remove_after_import = 0; - import.extra_recipient = import.move_folder = NULL; + import.extra_recipient = import.move_folder = import.failed_folder = NULL; import.start_position = 1; import.download_only = 0; import.timeout = 30; @@ -120,6 +121,7 @@ int main(int argc, char **argv){ {"quiet", no_argument, 0, 'q' }, {"recursive", required_argument, 0, 'R' }, {"remove-after-import",no_argument, 0, 'r' }, + {"failed-folder", required_argument, 0, 'j' }, {"move-folder", required_argument, 0, 'g' }, {"only-download",no_argument, 0, 'o' }, {"gui-import", no_argument, 0, 'G' }, @@ -130,9 +132,9 @@ int main(int argc, char **argv){ int option_index = 0; - c = getopt_long(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:GDRroqh?", long_options, &option_index); + c = getopt_long(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:GDRroqh?", long_options, &option_index); #else - c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:GDRroqh?"); + c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:GDRroqh?"); #endif if(c == -1) break; @@ -209,6 +211,10 @@ int main(int argc, char **argv){ data.import->move_folder = optarg; break; + case 'j' : + data.import->failed_folder = optarg; + break; + case 'o' : data.import->download_only = 1; dryrun = 1; @@ -262,7 +268,12 @@ int main(int argc, char **argv){ if(!mbox[0] && !mboxdir && !emlfile && !directory && !imapserver && !pop3server && import_from_gui == 0) usage(); - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(data.import->failed_folder && !can_i_write_directory(data.import->failed_folder)){ + printf("cannot write failed directory '%s'\n", data.import->failed_folder); + return ERR; + } + + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); cfg = read_config(configfile); diff --git a/src/reindex.c b/src/reindex.c index dbf1cc69..e4d966e8 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -211,7 +211,7 @@ int main(int argc, char **argv){ if(all == 0 && (from_id <= 0 || to_id <= 0) ) usage(); - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); (void) openlog("reindex", LOG_PID, LOG_MAIL); diff --git a/src/test.c b/src/test.c index 3c2cf1aa..aafeef14 100644 --- a/src/test.c +++ b/src/test.c @@ -32,7 +32,7 @@ int main(int argc, char **argv){ exit(1); } - if(!can_i_write_current_directory()) __fatal("cannot write current directory!"); + if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!"); if(stat(argv[1], &st) != 0){ fprintf(stderr, "%s is not found\n", argv[1]);