diff --git a/configure b/configure index cf4b2fc..5e3e335 100755 --- a/configure +++ b/configure @@ -4866,7 +4866,7 @@ CFLAGS="$static -O2 -Wall -g" LIBS="$antispam_libs $sunos_libs " -OBJS="dirs.o base64.o misc.o counters.o cfg.o sig.o decoder.o hash.o parser.o parser_utils.o rules.o smtp.o session.o message.o attachment.o digest.o store.o archive.o tai.o import.o import_maildir.o import_mailbox.o import_pop3.o import_imap.o import_gui.o imap.o pop3.o extract.o folder_extra.o mydomains.o $objs" +OBJS="dirs.o base64.o misc.o counters.o cfg.o sig.o decoder.o hash.o parser.o parser_utils.o rules.o smtp.o session.o message.o attachment.o digest.o store.o archive.o tai.o import.o import_maildir.o import_mailbox.o import_pop3.o import_imap.o import_gui.o imap.o pop3.o extract.o folder.o mydomains.o $objs" ac_config_files="$ac_config_files Makefile src/Makefile etc/Makefile util/Makefile init.d/Makefile unit_tests/Makefile contrib/imap/Makefile" diff --git a/configure.in b/configure.in index c645bd6..5505d6f 100644 --- a/configure.in +++ b/configure.in @@ -544,7 +544,7 @@ CFLAGS="$static -O2 -Wall -g" LIBS="$antispam_libs $sunos_libs " -OBJS="dirs.o base64.o misc.o counters.o cfg.o sig.o decoder.o hash.o parser.o parser_utils.o rules.o smtp.o session.o message.o attachment.o digest.o store.o archive.o tai.o import.o import_maildir.o import_mailbox.o import_pop3.o import_imap.o import_gui.o imap.o pop3.o extract.o folder_extra.o mydomains.o $objs" +OBJS="dirs.o base64.o misc.o counters.o cfg.o sig.o decoder.o hash.o parser.o parser_utils.o rules.o smtp.o session.o message.o attachment.o digest.o store.o archive.o tai.o import.o import_maildir.o import_mailbox.o import_pop3.o import_imap.o import_gui.o imap.o pop3.o extract.o folder.o mydomains.o $objs" AC_CONFIG_FILES([Makefile src/Makefile etc/Makefile util/Makefile init.d/Makefile unit_tests/Makefile contrib/imap/Makefile]) AC_OUTPUT diff --git a/etc/example.conf b/etc/example.conf index 3a0b0eb..e0814a2 100644 --- a/etc/example.conf +++ b/etc/example.conf @@ -71,7 +71,7 @@ workdir=/var/piler/tmp -; whether to enable writing folder_message table (1) or not (0) +; whether the daemon is enabled writing folder_message table (1) or not (0) enable_folders=0 ; discard a message if it's shorter than this value (in bytes) diff --git a/src/config.h b/src/config.h index dbfcae4..d88a31c 100644 --- a/src/config.h +++ b/src/config.h @@ -84,6 +84,7 @@ #define SQL_RETENTION_RULE_TABLE "retention_rule" #define SQL_FOLDER_RULE_TABLE "folder_rule" #define SQL_FOLDER_EXTRA_TABLE "folder_extra" +#define SQL_FOLDER_EMAIL_TABLE "folder_email" #define SQL_COUNTER_TABLE "counter" #define SQL_OPTION_TABLE "option" #define SQL_DOMAIN_TABLE "domain" @@ -106,6 +107,7 @@ #define SQL_PREPARED_STMT_QUERY_ATTACHMENT "SELECT `attachment_id`, `ptr` FROM " SQL_ATTACHMENT_TABLE " WHERE piler_id=? ORDER BY attachment_id ASC" #define SQL_PREPARED_STMT_GET_FOLDER_ID "SELECT `id` FROM " SQL_FOLDER_TABLE " WHERE `name`=? AND `parent_id`=?" #define SQL_PREPARED_STMT_GET_FOLDER_EXTRA_ID "SELECT `id` FROM " SQL_FOLDER_EXTRA_TABLE " WHERE `name`=? AND `uid`=?" +#define SQL_PREPARED_STMT_GET_FOLDER_UID_BY_EMAIL "SELECT `uid` FROM " SQL_FOLDER_EMAIL_TABLE " WHERE `email`=?" #define SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE "INSERT INTO `" SQL_FOLDER_TABLE "` (`name`, `parent_id`) VALUES(?,?)" #define SQL_PREPARED_STMT_INSERT_INTO_FOLDER_EXTRA_TABLE "INSERT INTO `" SQL_FOLDER_EXTRA_TABLE "` (`name`, `uid`) VALUES(?,?)" #define SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE "UPDATE " SQL_METADATA_TABLE " SET reference=? WHERE message_id=? AND reference=''" diff --git a/src/defs.h b/src/defs.h index 0564306..08f0698 100644 --- a/src/defs.h +++ b/src/defs.h @@ -286,6 +286,7 @@ struct import { char *extra_recipient; char *move_folder; + char *email; int status; int total_messages; int processed_messages; diff --git a/src/folder.c b/src/folder.c new file mode 100644 index 0000000..4ab2ce9 --- /dev/null +++ b/src/folder.c @@ -0,0 +1,91 @@ +/* + * folder.c, SJ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +void get_folder_uid_by_email(struct session_data *sdata, struct __data *data){ + int uid = 0; + + data->import->uid = uid; + + if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_UID_BY_EMAIL) == ERR) return; + + p_bind_init(data); + data->sql[data->pos] = data->import->email; data->type[data->pos] = TYPE_STRING; data->pos++; + + if(p_exec_query(sdata, data->stmt_get_folder_id, data) == OK){ + p_bind_init(data); + data->sql[data->pos] = (char *)&uid; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; + + p_store_results(data->stmt_get_folder_id, data); + p_fetch_results(data->stmt_get_folder_id); + p_free_results(data->stmt_get_folder_id); + } + + printf("uid=%d\n", uid); + + close_prepared_statement(data->stmt_get_folder_id); + + data->import->uid = uid; +} + + +int get_folder_extra_id(struct session_data *sdata, struct __data *data, char *foldername){ + int id=ERR_FOLDER; + + if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_EXTRA_ID) == ERR) return id; + + p_bind_init(data); + data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; + data->sql[data->pos] = (char *)&(data->import->uid); data->type[data->pos] = TYPE_LONG; data->pos++; + + if(p_exec_query(sdata, data->stmt_get_folder_id, data) == OK){ + + p_bind_init(data); + data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; + + p_store_results(data->stmt_get_folder_id, data); + p_fetch_results(data->stmt_get_folder_id); + p_free_results(data->stmt_get_folder_id); + } + + close_prepared_statement(data->stmt_get_folder_id); + + return id; +} + + +int add_new_folder_extra(struct session_data *sdata, struct __data *data, char *foldername){ + int id=ERR_FOLDER; + + if(foldername == NULL) return id; + + if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_EXTRA_TABLE) == ERR) return id; + + p_bind_init(data); + data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; + data->sql[data->pos] = (char *)&(data->import->uid); data->type[data->pos] = TYPE_LONG; data->pos++; + + if(p_exec_query(sdata, data->stmt_insert_into_folder_table, data) == OK){ + id = p_get_insert_id(data->stmt_insert_into_folder_table); + } + + close_prepared_statement(data->stmt_insert_into_folder_table); + + return id; +} diff --git a/src/folder_extra.c b/src/folder_extra.c deleted file mode 100644 index f258261..0000000 --- a/src/folder_extra.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * folder_extra.c, SJ - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -int get_folder_extra_id(struct session_data *sdata, struct __data *data, char *foldername){ - int id=ERR_FOLDER; - - if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_EXTRA_ID) == ERR) return id; - - p_bind_init(data); - data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&(data->import->uid); data->type[data->pos] = TYPE_LONG; data->pos++; - - if(p_exec_query(sdata, data->stmt_get_folder_id, data) == OK){ - - p_bind_init(data); - data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++; - - p_store_results(data->stmt_get_folder_id, data); - p_fetch_results(data->stmt_get_folder_id); - p_free_results(data->stmt_get_folder_id); - } - - close_prepared_statement(data->stmt_get_folder_id); - - return id; -} - - -int add_new_folder_extra(struct session_data *sdata, struct __data *data, char *foldername){ - int id=ERR_FOLDER; - - if(foldername == NULL) return id; - - if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_EXTRA_TABLE) == ERR) return id; - - p_bind_init(data); - data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++; - data->sql[data->pos] = (char *)&(data->import->uid); data->type[data->pos] = TYPE_LONG; data->pos++; - - if(p_exec_query(sdata, data->stmt_insert_into_folder_table, data) == OK){ - id = p_get_insert_id(data->stmt_insert_into_folder_table); - } - - close_prepared_statement(data->stmt_insert_into_folder_table); - - return id; -} diff --git a/src/import.c b/src/import.c index 56a802d..4d0044f 100644 --- a/src/import.c +++ b/src/import.c @@ -98,8 +98,8 @@ rc = process_message(sdata, &state, data, cfg); /* - * if pilerimport was invoked with --uid, and this is a duplicate, - * then add it to the folder_extra table + * if pilerimport was invoked with --email (then queried the matching uid!), + * and this is a duplicate, then add it to the folder_extra table */ if(rc == ERR_EXISTS && data->import->uid > 0){ diff --git a/src/piler.h b/src/piler.h index e387541..2ea1790 100644 --- a/src/piler.h +++ b/src/piler.h @@ -59,6 +59,7 @@ int get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id); int add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id); +void get_folder_uid_by_email(struct session_data *sdata, struct __data *data); int get_folder_extra_id(struct session_data *sdata, struct __data *data, char *foldername); int add_new_folder_extra(struct session_data *sdata, struct __data *data, char *foldername); diff --git a/src/pilerimport.c b/src/pilerimport.c index 1608d49..3ceb988 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -50,7 +50,7 @@ printf(" -f IMAP folder name to import\n"); printf(" -g Move email after import to this IMAP folder\n"); printf(" -F Piler folder name to assign to this import\n"); - printf(" --uid UID to assign to this import\n"); + printf(" --email Email address to assign to this import\n"); 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"); @@ -90,6 +90,7 @@ import.download_only = 0; import.timeout = 30; import.uid = 0; + import.email = NULL; data.import = &import; @@ -116,7 +117,7 @@ {"skiplist", required_argument, 0, 'x' }, {"folder", required_argument, 0, 'F' }, {"folder_imap", required_argument, 0, 'f' }, - {"uid", required_argument, 0, 'U' }, + {"email", required_argument, 0, 'E' }, {"add-recipient",required_argument, 0, 'a' }, {"batch-limit", required_argument, 0, 'b' }, {"timeout", required_argument, 0, 't' }, @@ -135,9 +136,9 @@ 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:U:GDRrozqh?", 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:E:GDRrozqh?", 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:U:GDRrozqh?"); + c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:E:GDRrozqh?"); #endif if(c == -1) break; @@ -182,8 +183,8 @@ username = optarg; break; - case 'U' : - data.import->uid = atoi(optarg); + case 'E' : + data.import->email = optarg; break; case 'p' : @@ -305,7 +306,12 @@ #endif if(folder){ - if(data.import->uid > 0){ + + cfg.enable_folders = 1; + + if(data.import->email){ + get_folder_uid_by_email(&sdata, &data); + data.folder = get_folder_extra_id(&sdata, &data, folder); if(data.folder == ERR_FOLDER){ diff --git a/util/db-mysql.sql b/util/db-mysql.sql index 2c20abf..339c35a 100644 --- a/util/db-mysql.sql +++ b/util/db-mysql.sql @@ -279,15 +279,15 @@ ) Engine=InnoDB; -create table if not exists `folder_user` ( - `id` bigint unsigned not null, - `uid` int unsigned not null, - key `folder_user_idx` (`id`), - key `folder_user_idx2` (`uid`) +create table if not exists `folder_email` ( + `uid` int unsigned not null auto_increment, + `email` char(128) not null unique, + key `folder_user_idx` (`uid`) + key `folder_user_idx2` (`email`) ) ENGINE=InnoDB; -create table if not exists `folder_extra` ( +create table if not exists `folder_user` ( `id` int unsigned not null auto_increment, `uid` int unsigned not null, `name` char(64) not null, @@ -301,7 +301,7 @@ `folder_id` bigint not null, `message_id` bigint not null, `uid` int not null, - unique(`folder_id`, `message_id`, `uid`), + unique(`message_id`, `uid`), key (`id`) ) ENGINE=InnoDB;