diff options
author | ilja <ilja@7b491191-dbf0-0310-aff6-d879d4d69008> | 2004-03-19 14:03:30 +0000 |
---|---|---|
committer | ilja <ilja@7b491191-dbf0-0310-aff6-d879d4d69008> | 2004-03-19 14:03:30 +0000 |
commit | 3e6f33ac32e6f09c770003503494af56bde1df88 (patch) | |
tree | aa27d31cbdc1c0760ae35734518af2928c5cb180 | |
parent | f9ac6bba9be298ad28b2b60ae66894ed223c7c31 (diff) |
couple of files indented nicely using indent -kr -i8
git-svn-id: https://svn.ic-s.nl/svn/dbmail/trunk/dbmail@1027 7b491191-dbf0-0310-aff6-d879d4d69008
-rw-r--r-- | auth.h | 30 | ||||
-rw-r--r-- | bounce.c | 429 | ||||
-rw-r--r-- | bounce.h | 8 | ||||
-rw-r--r-- | clientinfo.h | 17 | ||||
-rw-r--r-- | config.c | 303 | ||||
-rw-r--r-- | db.c | 4804 | ||||
-rw-r--r-- | db.h | 137 | ||||
-rw-r--r-- | dbmail.h | 36 | ||||
-rw-r--r-- | dbmailtypes.h | 218 | ||||
-rw-r--r-- | dbmd5.c | 16 | ||||
-rw-r--r-- | dbmsgbuf.c | 747 | ||||
-rw-r--r-- | dbmsgbuf.h | 10 | ||||
-rw-r--r-- | dbsearch.c | 660 | ||||
-rw-r--r-- | dbsearch.h | 6 | ||||
-rw-r--r-- | debug.c | 252 | ||||
-rw-r--r-- | debug.h | 22 |
16 files changed, 3949 insertions, 3746 deletions
@@ -68,7 +68,7 @@ int auth_disconnect(void); * - 0 if user not found * - 1 otherwise */ -int auth_user_exists(const char *username, u64_t *user_idnr); +int auth_user_exists(const char *username, u64_t * user_idnr); /** * \brief get a list of all known users @@ -91,7 +91,7 @@ int auth_get_known_users(struct list *users); * - -1 on error * - 1 on success */ -int auth_getclientid(u64_t user_idnr, u64_t *client_idnr); +int auth_getclientid(u64_t user_idnr, u64_t * client_idnr); /** * \brief get the maximum mail size for a user @@ -104,7 +104,7 @@ int auth_getclientid(u64_t user_idnr, u64_t *client_idnr); * maxmail_size of 0. * - 1 otherwise */ -int auth_getmaxmailsize(u64_t user_idnr, u64_t *maxmail_size); +int auth_getmaxmailsize(u64_t user_idnr, u64_t * maxmail_size); /** * \brief returns a string describing the encryption used for the @@ -116,7 +116,7 @@ int auth_getmaxmailsize(u64_t user_idnr, u64_t *maxmail_size); * \return * - NULL if error */ -char* auth_getencryption(u64_t user_idnr); +char *auth_getencryption(u64_t user_idnr); /** * \brief find all deliver_to addresses for a username (?, code is not exactly @@ -127,7 +127,8 @@ char* auth_getencryption(u64_t user_idnr); * be set to -1 when called! * \return number of deliver_to addresses found */ -int auth_check_user (const char *username, struct list *userids, int checks); +int auth_check_user(const char *username, struct list *userids, + int checks); /** * \brief as auth_check_user() but adds the numeric ID of the user found to * userids or the forward to the fwds list @@ -137,7 +138,8 @@ int auth_check_user (const char *username, struct list *userids, int checks); * \param checks used internally, \b should be -1 on call * \return number of deliver_to addresses found */ -int auth_check_user_ext(const char *username, struct list *userids, struct list *fwds, int checks); +int auth_check_user_ext(const char *username, struct list *userids, + struct list *fwds, int checks); /** * \brief add a new user to the database (whichever type of database is * implemented) @@ -155,8 +157,8 @@ int auth_check_user_ext(const char *username, struct list *userids, struct list * \bug this function creates its own query for adding a mailbox. It would probably * be a better idea to let db_create_mailbox() handle this. */ -int auth_adduser (char *username, char *password, char *enctype, - char *clientid, char *maxmail, u64_t *user_idnr); +int auth_adduser(char *username, char *password, char *enctype, + char *clientid, char *maxmail, u64_t * user_idnr); /** * \brief delete user from the database. Does not delete the user's email! * \param username name of user to be deleted @@ -184,8 +186,8 @@ int auth_change_username(u64_t user_idnr, const char *new_name); * - -1 on failure * - 0 on success */ -int auth_change_password(u64_t user_idnr, - const char *new_pass, const char *enctype); +int auth_change_password(u64_t user_idnr, + const char *new_pass, const char *enctype); /** * \brief change a users client id * \param user_idnr @@ -215,7 +217,7 @@ int auth_change_mailboxsize(u64_t user_idnr, u64_t new_size); * - 0 if not validated * - 1 if OK */ -int auth_validate (char *username, char *password, u64_t *user_idnr); +int auth_validate(char *username, char *password, u64_t * user_idnr); /** * \brief try tp validate a user using md5 hash @@ -227,8 +229,8 @@ int auth_validate (char *username, char *password, u64_t *user_idnr); * - 0 if not validated * - user_idrn if OK */ -u64_t auth_md5_validate (char *username, unsigned char *md5_apop_he, - char *apop_stamp); +u64_t auth_md5_validate(char *username, unsigned char *md5_apop_he, + char *apop_stamp); /** * \brief get username for a user_idnr @@ -238,7 +240,7 @@ u64_t auth_md5_validate (char *username, unsigned char *md5_apop_he, * - username otherwise * \attention caller should free username string */ -char *auth_get_userid (u64_t user_idnr); +char *auth_get_userid(u64_t user_idnr); #endif @@ -37,206 +37,241 @@ #include <stdlib.h> #include <string.h> -extern struct list mimelist; -extern struct list users; -extern struct list smtpItems; +extern struct list mimelist; +extern struct list users; +extern struct list smtpItems; -int bounce(const char *header, const char *destination_address, bounce_reason_t reason) +int bounce(const char *header, const char *destination_address, + bounce_reason_t reason) { - FILE *sendmail_stream; - char *sendmail_command = NULL; - struct list from_addresses; - struct element *tmpelement; - field_t dbmail_from_address, sendmail, postmaster; - - - /* reading configuration from db */ - GetConfigValue("DBMAIL_FROM_ADDRESS", &smtpItems, dbmail_from_address); - if (dbmail_from_address[0] == '\0') - trace(TRACE_FATAL, "%s,%s: DBMAIL_FROM_ADDRESS not configured " - "(see config file). Stop.", __FILE__, __FUNCTION__); - - GetConfigValue("SENDMAIL", &smtpItems, sendmail); - if (sendmail[0] == '\0') - trace(TRACE_FATAL, "%s,%s: SENDMAIL not configured " - "(see config file). Stop.", __FILE__, __FUNCTION__); - - GetConfigValue("POSTMASTER", &smtpItems, postmaster); - if (postmaster[0] == '\0') - trace(TRACE_FATAL, "%s,%s: POSTMASTER not configured " - "(see config file). Stop.", __FILE__, __FUNCTION__); - - trace(TRACE_DEBUG,"%s,%s: creating bounce message for bounce reason [%d]", - __FILE__, __FUNCTION__, reason); - - if (!destination_address) - { - trace(TRACE_ERROR,"%s,%s: cannot deliver to NULL.", - __FILE__, __FUNCTION__); - return -1; - } - - - switch (reason) - { - case BOUNCE_NO_SUCH_USER: - /* no such user found */ - trace (TRACE_MESSAGE,"%s,%s: sending 'no such user' bounce " - "for destination [%s]", __FILE__, __FUNCTION__, - destination_address); - list_init(&from_addresses); - /* scan the from header for addresses */ - mail_adr_list ("Return-Path", &from_addresses,&mimelist); - - if (list_totalnodes(&from_addresses) == 0) - { - trace (TRACE_INFO,"%s,%s: can't find Return-Path values, " - "resorting to From values", __FILE__, __FUNCTION__); - mail_adr_list ("From", &from_addresses, &mimelist); - } /* RR logix :) */ - - /* loop target addresses */ - if (list_totalnodes(&from_addresses) > 0) - { - tmpelement=list_getstart (&from_addresses); - while (tmpelement!=NULL) - { - /* open a stream to sendmail - the sendmail macro is defined in bounce.h */ - sendmail_command = (char *)my_malloc( - strlen((char *)(tmpelement->data))+ - strlen(sendmail)+2); /* +2 for extra space and \0 */ - if (!sendmail_command) - { - trace(TRACE_ERROR,"bounce(): out of memory"); - list_freelist(&from_addresses.start); - return -1; - } - - trace (TRACE_DEBUG,"bounce(): allocated memory for" - " external command call"); - sprintf (sendmail_command, "%s %s", - sendmail, (char *)(tmpelement->data)); - - trace (TRACE_INFO,"bounce(): opening pipe to command " - "%s",sendmail_command); - sendmail_stream = popen(sendmail_command,"w"); - - if (sendmail_stream==NULL) - { - /* could not open a succesfull stream */ - trace(TRACE_MESSAGE,"%s,%s: could not open a pipe " - "to %s", __FILE__, __FUNCTION__,sendmail); - return -1; - } - fprintf (sendmail_stream,"From: %s\n",dbmail_from_address); - fprintf (sendmail_stream,"To: %s\n",(char *)tmpelement->data); - fprintf (sendmail_stream,"Subject: DBMAIL: delivery failure\n"); - fprintf (sendmail_stream,"\n"); - fprintf (sendmail_stream,"This is the DBMAIL-SMTP program.\n\n"); - fprintf (sendmail_stream,"I'm sorry to inform you that your message, addressed to %s,\n", - destination_address); - fprintf (sendmail_stream,"could not be delivered due to the following error.\n\n"); - fprintf (sendmail_stream,"*** E-mail address %s is not known here. ***\n\n",destination_address); - fprintf (sendmail_stream,"If you think this message is incorrect please contact %s.\n\n",postmaster); - fprintf (sendmail_stream,"Header of your message follows...\n\n\n"); - fprintf (sendmail_stream,"--- header of your message ---\n"); - fprintf (sendmail_stream,"%s",header); - fprintf (sendmail_stream,"--- end of header ---\n\n\n"); - fprintf (sendmail_stream,"\n.\n"); - pclose (sendmail_stream); - - /* jump forward to next recipient */ - tmpelement=tmpelement->nextnode; - } - } - else - { - trace(TRACE_MESSAGE,"%s,%s: " - "Message does not have a Return-Path nor a From headerfield, " - "bounce failed", __FILE__, __FUNCTION__); + FILE *sendmail_stream; + char *sendmail_command = NULL; + struct list from_addresses; + struct element *tmpelement; + field_t dbmail_from_address, sendmail, postmaster; + + + /* reading configuration from db */ + GetConfigValue("DBMAIL_FROM_ADDRESS", &smtpItems, + dbmail_from_address); + if (dbmail_from_address[0] == '\0') + trace(TRACE_FATAL, + "%s,%s: DBMAIL_FROM_ADDRESS not configured " + "(see config file). Stop.", __FILE__, __FUNCTION__); + + GetConfigValue("SENDMAIL", &smtpItems, sendmail); + if (sendmail[0] == '\0') + trace(TRACE_FATAL, "%s,%s: SENDMAIL not configured " + "(see config file). Stop.", __FILE__, __FUNCTION__); + + GetConfigValue("POSTMASTER", &smtpItems, postmaster); + if (postmaster[0] == '\0') + trace(TRACE_FATAL, "%s,%s: POSTMASTER not configured " + "(see config file). Stop.", __FILE__, __FUNCTION__); + + trace(TRACE_DEBUG, + "%s,%s: creating bounce message for bounce reason [%d]", + __FILE__, __FUNCTION__, reason); + + if (!destination_address) { + trace(TRACE_ERROR, "%s,%s: cannot deliver to NULL.", + __FILE__, __FUNCTION__); + return -1; } - - break; - case BOUNCE_STORAGE_LIMIT_REACHED: - /* mailbox size exceeded */ - trace (TRACE_MESSAGE,"%s,%s: sending 'mailboxsize exceeded' bounce " - "for user [%s].", __FILE__, __FUNCTION__, destination_address); - list_init(&from_addresses); - - /* scan the Return-Path header for addresses - if they don't exist, resort to From addresses */ - mail_adr_list ("Return-Path", &from_addresses,&mimelist); - - if (list_totalnodes(&from_addresses) == 0) - { - trace (TRACE_INFO,"%s,%s: can't find Return-Path values, " - "resorting to From values", __FILE__, __FUNCTION__); - mail_adr_list ("From", &from_addresses, &mimelist); - } /* RR logix :) */ - - if (list_totalnodes(&from_addresses)>0) - { - /* loop target addresses */ - tmpelement=list_getstart (&from_addresses); - while (tmpelement!=NULL) - { - /* open a stream to sendmail - the sendmail macro is defined in bounce.h */ - sendmail_command = (char *)my_malloc(strlen((char *)(tmpelement->data))+ - strlen(sendmail)+2); /* +2 for extra space and \0 */ - if (!sendmail_command) - { - trace(TRACE_ERROR,"bounce(): out of memory"); - list_freelist(&from_addresses.start); - return -1; - } - - trace (TRACE_DEBUG,"bounce(): allocated memory for" - " external command call"); - sprintf (sendmail_command, "%s %s",sendmail, (char *)(tmpelement->data)); - - trace (TRACE_INFO,"bounce(): opening pipe to command " - "%s",sendmail_command); - sendmail_stream=popen (sendmail_command,"w"); - - if (sendmail_stream==NULL) - { - /* could not open a succesfull stream */ - trace(TRACE_MESSAGE,"%s,%s: could not open a pipe to %s", - __FILE__, __FUNCTION__, sendmail); - return -1; + + + switch (reason) { + case BOUNCE_NO_SUCH_USER: + /* no such user found */ + trace(TRACE_MESSAGE, + "%s,%s: sending 'no such user' bounce " + "for destination [%s]", __FILE__, __FUNCTION__, + destination_address); + list_init(&from_addresses); + /* scan the from header for addresses */ + mail_adr_list("Return-Path", &from_addresses, &mimelist); + + if (list_totalnodes(&from_addresses) == 0) { + trace(TRACE_INFO, + "%s,%s: can't find Return-Path values, " + "resorting to From values", __FILE__, + __FUNCTION__); + mail_adr_list("From", &from_addresses, &mimelist); + } + + /* RR logix :) */ + /* loop target addresses */ + if (list_totalnodes(&from_addresses) > 0) { + tmpelement = list_getstart(&from_addresses); + while (tmpelement != NULL) { + /* open a stream to sendmail + the sendmail macro is defined in bounce.h */ + sendmail_command = (char *) my_malloc(strlen((char *) (tmpelement->data)) + strlen(sendmail) + 2); /* +2 for extra space and \0 */ + if (!sendmail_command) { + trace(TRACE_ERROR, + "bounce(): out of memory"); + list_freelist(&from_addresses. + start); + return -1; + } + + trace(TRACE_DEBUG, + "bounce(): allocated memory for" + " external command call"); + sprintf(sendmail_command, "%s %s", + sendmail, + (char *) (tmpelement->data)); + + trace(TRACE_INFO, + "bounce(): opening pipe to command " + "%s", sendmail_command); + sendmail_stream = + popen(sendmail_command, "w"); + + if (sendmail_stream == NULL) { + /* could not open a succesfull stream */ + trace(TRACE_MESSAGE, + "%s,%s: could not open a pipe " + "to %s", __FILE__, + __FUNCTION__, sendmail); + return -1; + } + fprintf(sendmail_stream, "From: %s\n", + dbmail_from_address); + fprintf(sendmail_stream, "To: %s\n", + (char *) tmpelement->data); + fprintf(sendmail_stream, + "Subject: DBMAIL: delivery failure\n"); + fprintf(sendmail_stream, "\n"); + fprintf(sendmail_stream, + "This is the DBMAIL-SMTP program.\n\n"); + fprintf(sendmail_stream, + "I'm sorry to inform you that your message, addressed to %s,\n", + destination_address); + fprintf(sendmail_stream, + "could not be delivered due to the following error.\n\n"); + fprintf(sendmail_stream, + "*** E-mail address %s is not known here. ***\n\n", + destination_address); + fprintf(sendmail_stream, + "If you think this message is incorrect please contact %s.\n\n", + postmaster); + fprintf(sendmail_stream, + "Header of your message follows...\n\n\n"); + fprintf(sendmail_stream, + "--- header of your message ---\n"); + fprintf(sendmail_stream, "%s", header); + fprintf(sendmail_stream, + "--- end of header ---\n\n\n"); + fprintf(sendmail_stream, "\n.\n"); + pclose(sendmail_stream); + + /* jump forward to next recipient */ + tmpelement = tmpelement->nextnode; + } + } else { + trace(TRACE_MESSAGE, "%s,%s: " + "Message does not have a Return-Path nor a From headerfield, " + "bounce failed", __FILE__, __FUNCTION__); + } + + break; + case BOUNCE_STORAGE_LIMIT_REACHED: + /* mailbox size exceeded */ + trace(TRACE_MESSAGE, + "%s,%s: sending 'mailboxsize exceeded' bounce " + "for user [%s].", __FILE__, __FUNCTION__, + destination_address); + list_init(&from_addresses); + + /* scan the Return-Path header for addresses + if they don't exist, resort to From addresses */ + mail_adr_list("Return-Path", &from_addresses, &mimelist); + + if (list_totalnodes(&from_addresses) == 0) { + trace(TRACE_INFO, + "%s,%s: can't find Return-Path values, " + "resorting to From values", __FILE__, + __FUNCTION__); + mail_adr_list("From", &from_addresses, &mimelist); + } + /* RR logix :) */ + if (list_totalnodes(&from_addresses) > 0) { + /* loop target addresses */ + tmpelement = list_getstart(&from_addresses); + while (tmpelement != NULL) { + /* open a stream to sendmail + the sendmail macro is defined in bounce.h */ + sendmail_command = (char *) my_malloc(strlen((char *) (tmpelement->data)) + strlen(sendmail) + 2); /* +2 for extra space and \0 */ + if (!sendmail_command) { + trace(TRACE_ERROR, + "bounce(): out of memory"); + list_freelist(&from_addresses. + start); + return -1; + } + + trace(TRACE_DEBUG, + "bounce(): allocated memory for" + " external command call"); + sprintf(sendmail_command, "%s %s", + sendmail, + (char *) (tmpelement->data)); + + trace(TRACE_INFO, + "bounce(): opening pipe to command " + "%s", sendmail_command); + sendmail_stream = + popen(sendmail_command, "w"); + + if (sendmail_stream == NULL) { + /* could not open a succesfull stream */ + trace(TRACE_MESSAGE, + "%s,%s: could not open a pipe to %s", + __FILE__, __FUNCTION__, + sendmail); + return -1; + } + fprintf(sendmail_stream, "From: %s\n", + dbmail_from_address); + fprintf(sendmail_stream, "To: %s\n", + (char *) tmpelement->data); + fprintf(sendmail_stream, + "Subject: DBMAIL: delivery failure\n"); + fprintf(sendmail_stream, "\n"); + fprintf(sendmail_stream, + "This is the DBMAIL-SMTP program.\n\n"); + fprintf(sendmail_stream, + "I'm sorry to inform you that your message, addressed to %s,\n", + destination_address); + fprintf(sendmail_stream, + "could not be delivered due to the following error.\n\n"); + fprintf(sendmail_stream, + "*** Mailbox of user %s is FULL ***\n\n", + destination_address); + fprintf(sendmail_stream, + "If you think this message is incorrect please contact %s.\n\n", + postmaster); + fprintf(sendmail_stream, + "Header of your message follows...\n\n\n"); + fprintf(sendmail_stream, + "--- header of your message ---\n"); + fprintf(sendmail_stream, "%s", header); + fprintf(sendmail_stream, + "--- end of header ---\n\n\n"); + fprintf(sendmail_stream, "\n.\n"); + pclose(sendmail_stream); + + /* jump forward to next recipient */ + tmpelement = tmpelement->nextnode; + } + } else { + trace(TRACE_MESSAGE, "%s,%s: " + "Message does not have a Return-Path nor a From headerfield, " + "bounce failed", __FILE__, __FUNCTION__); + break; } - fprintf (sendmail_stream,"From: %s\n",dbmail_from_address); - fprintf (sendmail_stream,"To: %s\n",(char *)tmpelement->data); - fprintf (sendmail_stream,"Subject: DBMAIL: delivery failure\n"); - fprintf (sendmail_stream,"\n"); - fprintf (sendmail_stream,"This is the DBMAIL-SMTP program.\n\n"); - fprintf (sendmail_stream,"I'm sorry to inform you that your message, addressed to %s,\n", - destination_address); - fprintf (sendmail_stream,"could not be delivered due to the following error.\n\n"); - fprintf (sendmail_stream,"*** Mailbox of user %s is FULL ***\n\n",destination_address); - fprintf (sendmail_stream,"If you think this message is incorrect please contact %s.\n\n",postmaster); - fprintf (sendmail_stream,"Header of your message follows...\n\n\n"); - fprintf (sendmail_stream,"--- header of your message ---\n"); - fprintf (sendmail_stream,"%s",header); - fprintf (sendmail_stream,"--- end of header ---\n\n\n"); - fprintf (sendmail_stream,"\n.\n"); - pclose (sendmail_stream); - - /* jump forward to next recipient */ - tmpelement=tmpelement->nextnode; - } } - else - { - trace(TRACE_MESSAGE,"%s,%s: " - "Message does not have a Return-Path nor a From headerfield, " - "bounce failed", __FILE__, __FUNCTION__); - break; - } - } - - return 0; -} + return 0; +} @@ -42,8 +42,9 @@ /** different reasons for bouncing a message */ typedef enum { - BOUNCE_NO_SUCH_USER, /**< no such user in database */ - BOUNCE_STORAGE_LIMIT_REACHED/**< user exceeds mail quotum */ + BOUNCE_NO_SUCH_USER, /**< no such user in database */ + BOUNCE_STORAGE_LIMIT_REACHED + /**< user exceeds mail quotum */ } bounce_reason_t; /** @@ -61,6 +62,7 @@ typedef enum { * not checked, which should be done to make sure only valid data is sent * to the shell. */ -int bounce(const char *header, const char *destination_address, bounce_reason_t reason); +int bounce(const char *header, const char *destination_address, + bounce_reason_t reason); #endif diff --git a/clientinfo.h b/clientinfo.h index 08eef11b..6cee6e49 100644 --- a/clientinfo.h +++ b/clientinfo.h @@ -35,17 +35,16 @@ #define IPNUM_LEN 32 -typedef struct -{ - FILE *tx,*rx; - char ip[IPNUM_LEN]; /* client IP-number */ - field_t clientname; /* resolved client ip */ - char *timeoutMsg; - int timeout; /* server timeout (seconds) */ - void *userData; +typedef struct { + FILE *tx, *rx; + char ip[IPNUM_LEN]; /* client IP-number */ + field_t clientname; /* resolved client ip */ + char *timeoutMsg; + int timeout; /* server timeout (seconds) */ + void *userData; } clientinfo_t; -typedef clientinfo_t ClientInfo; /* FIXME alias shouldn't be necessary */ +typedef clientinfo_t ClientInfo; /* FIXME alias shouldn't be necessary */ #endif @@ -52,177 +52,188 @@ * returns 0 on succes, -1 on error unless CONFIG_ERROR_LEVEL is set TRACE_FATAL/TRACE_STOP; * if so the function will not return upon error but call exit(). */ -int ReadConfig(const char *serviceName, const char *cfilename, struct list *cfg_items) +int ReadConfig(const char *serviceName, const char *cfilename, + struct list *cfg_items) { - item_t item; - char line[LINESIZE],*tmp,*value,service[LINESIZE]; - FILE *cfile = NULL; - int serviceFound = 0,isCommentline; + item_t item; + char line[LINESIZE], *tmp, *value, service[LINESIZE]; + FILE *cfile = NULL; + int serviceFound = 0, isCommentline; - trace(TRACE_DEBUG, "ReadConfig(): starting procedure"); + trace(TRACE_DEBUG, "ReadConfig(): starting procedure"); - snprintf(service, LINESIZE, "[%s]", serviceName); + snprintf(service, LINESIZE, "[%s]", serviceName); - list_init(cfg_items); + list_init(cfg_items); - if ( !(cfile = fopen(cfilename, "r")) ) - { - trace(CONFIG_ERROR_LEVEL, "ReadConfig(): could not open config file [%s]", cfilename); - return -1; - } - - do - { - fgets(line, LINESIZE, cfile); - - if (feof(cfile) || ferror(cfile)) - break; + if (!(cfile = fopen(cfilename, "r"))) { + trace(CONFIG_ERROR_LEVEL, + "ReadConfig(): could not open config file [%s]", + cfilename); + return -1; + } - /* chop whitespace front */ - for (tmp = line; *tmp && isspace(*tmp); tmp++) ; - memmove(line, tmp, strlen(tmp)); + do { + fgets(line, LINESIZE, cfile); + + if (feof(cfile) || ferror(cfile)) + break; + + /* chop whitespace front */ + for (tmp = line; *tmp && isspace(*tmp); tmp++); + memmove(line, tmp, strlen(tmp)); + + if (strncasecmp(line, service, strlen(service)) == 0) { + /* ok entering config block */ + serviceFound = 1; + + trace(TRACE_DEBUG, "ReadConfig(): found %s tag", + service); + memset(&item, 0, sizeof(item)); + + while (1) { + isCommentline = 0; + + fgets(line, LINESIZE, cfile); + + if (feof(cfile) || ferror(cfile) + || strlen(line) == 0) + break; + + if ((tmp = strchr(line, '#'))) { /* remove comments */ + isCommentline = 1; + *tmp = '\0'; + } + + /* chop whitespace front */ + for (tmp = line; *tmp && isspace(*tmp); + tmp++); + memmove(line, tmp, strlen(tmp)); + + /* chop whitespace at end */ + for (tmp = &line[strlen(line) - 1]; + tmp >= line && isspace(*tmp); tmp--) + *tmp = '\0'; + + if (strlen(line) == 0 && !isCommentline) + break; /* empty line specifies ending */ + + if (!(tmp = strchr(line, '='))) { + trace(TRACE_INFO, + "ReadConfig(): no value specified for service item [%s].", + line); + continue; + } + + *tmp = '\0'; + value = tmp + 1; + + strncpy(item.name, line, FIELDSIZE); + strncpy(item.value, value, FIELDSIZE); + + if (!list_nodeadd + (cfg_items, &item, sizeof(item))) { + trace(CONFIG_ERROR_LEVEL, + "ReadConfig(): could not add node"); + return -1; + } + + trace(TRACE_DEBUG, + "ReadConfig(): item [%s] value [%s] added", + item.name, item.value); + } + trace(TRACE_DEBUG, + "ReadConfig(): service %s added", service); + } - if (strncasecmp(line, service, strlen(service)) == 0) - { - /* ok entering config block */ - serviceFound = 1; + /* skip otherwise */ + } while (!serviceFound); - trace(TRACE_DEBUG, "ReadConfig(): found %s tag", service); - memset(&item, 0, sizeof(item)); + trace(TRACE_DEBUG, + "ReadConfig(): config for %s read, found [%ld] cfg_items", + service, cfg_items->total_nodes); + fclose(cfile); + return 0; +} - while (1) - { - isCommentline = 0; - fgets(line, LINESIZE, cfile); +int GetConfigValue(const field_t name, struct list *cfg_items, + field_t value) +{ + item_t *item; + struct element *el; - if (feof(cfile) || ferror(cfile) || strlen(line) == 0) - break; - - if ( (tmp = strchr(line, '#')) ) /* remove comments */ - { - isCommentline = 1; - *tmp = '\0'; - } + value[0] = '\0'; - /* chop whitespace front */ - for (tmp = line; *tmp && isspace(*tmp); tmp++) ; - memmove(line, tmp, strlen(tmp)); + trace(TRACE_DEBUG, + "GetConfigValue(): searching value for config item [%s]", + name); - /* chop whitespace at end */ - for (tmp = &line[strlen(line)-1]; tmp >= line && isspace(*tmp); tmp--) - *tmp = '\0'; + el = list_getstart(cfg_items); + while (el) { + item = (item_t *) el->data; - if (strlen(line) == 0 && !isCommentline) - break; /* empty line specifies ending */ + if (!item || !item->name || !item->value) { + trace(TRACE_INFO, + "GetConfigValue(): NULL item%s in item-list", + item ? (item-> + name ? " value" : " name") : ""); - if (! (tmp = strchr(line, '=')) ) - { - trace(TRACE_INFO, "ReadConfig(): no value specified for service item [%s].", line); - continue; + el = el->nextnode; + continue; } - *tmp = '\0'; - value = tmp+1; - - strncpy(item.name, line, FIELDSIZE); - strncpy(item.value, value, FIELDSIZE); - - if (!list_nodeadd(cfg_items, &item, sizeof(item))) - { - trace(CONFIG_ERROR_LEVEL, "ReadConfig(): could not add node"); - return -1; + if (strcasecmp(item->name, name) == 0) { + trace(TRACE_DEBUG, + "GetConfigValue(): found value [%s]", + item->value); + strncpy(value, item->value, FIELDSIZE); + return 0; } - - trace(TRACE_DEBUG, "ReadConfig(): item [%s] value [%s] added", item.name, item.value); - } - trace(TRACE_DEBUG, "ReadConfig(): service %s added", service); - } - /* skip otherwise */ - } while (!serviceFound) ; + el = el->nextnode; + } - trace(TRACE_DEBUG, "ReadConfig(): config for %s read, found [%ld] cfg_items", - service, cfg_items->total_nodes); - fclose(cfile); - return 0; + trace(TRACE_DEBUG, "GetConfigValue(): item not found"); + return 0; } - -int GetConfigValue(const field_t name, struct list *cfg_items, field_t value) +void SetTraceLevel(struct list *cfg_items) { - item_t *item; - struct element *el; - - value[0] = '\0'; + field_t val; - trace(TRACE_DEBUG, "GetConfigValue(): searching value for config item [%s]", name); - - el = list_getstart(cfg_items); - while (el) - { - item = (item_t*)el->data; - - if (!item || !item->name || !item->value) - { - trace(TRACE_INFO, "GetConfigValue(): NULL item%s in item-list", - item ? (item->name ? " value" : " name") : "" ); - - el = el->nextnode; - continue; - } - - if (strcasecmp(item->name, name) == 0) - { - trace(TRACE_DEBUG, "GetConfigValue(): found value [%s]", item->value); - strncpy(value, item->value, FIELDSIZE); - return 0; - } - - el = el->nextnode; - } - - trace(TRACE_DEBUG, "GetConfigValue(): item not found"); - return 0; + GetConfigValue("trace_level", cfg_items, val); + if (strlen(val) == 0) + configure_debug(TRACE_ERROR, 1, 0); + else + configure_debug(atoi(val), 1, 0); } -void SetTraceLevel(struct list *cfg_items) -{ - field_t val; - - GetConfigValue("trace_level", cfg_items, val); - if (strlen(val) == 0) - configure_debug(TRACE_ERROR, 1, 0); - else - configure_debug(atoi(val), 1, 0); -} - -void GetDBParams(db_param_t *db_params, struct list *cfg_items) +void GetDBParams(db_param_t * db_params, struct list *cfg_items) { - field_t port_string; - field_t sock_string; - GetConfigValue("host", cfg_items, db_params->host); - GetConfigValue("db", cfg_items, db_params->db); - GetConfigValue("user", cfg_items, db_params->user); - GetConfigValue("pass", cfg_items, db_params->pass); - GetConfigValue("sqlport", cfg_items, port_string); - GetConfigValue("sqlsocket", cfg_items, sock_string); - - /* check if port_string holds a value */ - if (strlen(port_string) != 0) { - db_params->port = (unsigned int) strtoul(port_string, NULL, 10); - if (errno == EINVAL || errno == ERANGE) - trace(TRACE_FATAL,"%s,%s: wrong value for sqlport in " - "config file", __FILE__, __FUNCTION__); - } else - db_params->port = 0; - - /* same for sock_string */ - if (strlen(sock_string) != 0) - strncpy(db_params->sock, sock_string, FIELDSIZE); - else - db_params->sock[0] = '\0'; + field_t port_string; + field_t sock_string; + GetConfigValue("host", cfg_items, db_params->host); + GetConfigValue("db", cfg_items, db_params->db); + GetConfigValue("user", cfg_items, db_params->user); + GetConfigValue("pass", cfg_items, db_params->pass); + GetConfigValue("sqlport", cfg_items, port_string); + GetConfigValue("sqlsocket", cfg_items, sock_string); + + /* check if port_string holds a value */ + if (strlen(port_string) != 0) { + db_params->port = + (unsigned int) strtoul(port_string, NULL, 10); + if (errno == EINVAL || errno == ERANGE) + trace(TRACE_FATAL, + "%s,%s: wrong value for sqlport in " + "config file", __FILE__, __FUNCTION__); + } else + db_params->port = 0; + + /* same for sock_string */ + if (strlen(sock_string) != 0) + strncpy(db_params->sock, sock_string, FIELDSIZE); + else + db_params->sock[0] = '\0'; } - - - @@ -41,12 +41,12 @@ #include <assert.h> static const char *db_flag_desc[] = { - "seen_flag", - "answered_flag", - "deleted_flag", - "flagged_flag", - "draft_flag", - "recent_flag" + "seen_flag", + "answered_flag", + "deleted_flag", + "flagged_flag", + "draft_flag", + "recent_flag" }; #define MAX_COLUMN_LEN 50 @@ -57,7 +57,7 @@ extern const char *TO_DATE; /** list of tables used in dbmail */ const char *DB_TABLENAMES[DB_NTABLES] = { - "users", "aliases", "mailboxes", "messages", "physmessage", + "users", "aliases", "mailboxes", "messages", "physmessage", "messageblks" }; @@ -75,17 +75,18 @@ static int db_subtract_quotum_used(u64_t user_idnr, u64_t sub_size); static int db_check_quotum_used(u64_t user_idnr, u64_t msg_size); /** list all mailboxes owned by user owner_idnr */ -static int db_list_mailboxes_by_regex(u64_t owner_idnr, int only_subscribed, - regex_t *preg, - u64_t **mailboxes, unsigned int *nr_mailboxes); +static int db_list_mailboxes_by_regex(u64_t owner_idnr, + int only_subscribed, regex_t * preg, + u64_t ** mailboxes, + unsigned int *nr_mailboxes); /** get size of a message */ -static int db_get_message_size(u64_t message_idnr, u64_t *message_size); +static int db_get_message_size(u64_t message_idnr, u64_t * message_size); /** find a mailbox with a specific owner */ -static int db_findmailbox_owner(const char *name, u64_t owner_idnr, - u64_t *mailbox_idnr); +static int db_findmailbox_owner(const char *name, u64_t owner_idnr, + u64_t * mailbox_idnr); /** get the total size of messages in a mailbox. Does not work recursively! */ -static int db_get_mailbox_size(u64_t mailbox_idnr, int only_deleted, - u64_t *mailbox_size); +static int db_get_mailbox_size(u64_t mailbox_idnr, int only_deleted, + u64_t * mailbox_size); /** * constructs a string for use in queries. This is used to not be dependent * on the internal representation of a date in the database. Whenever the @@ -103,65 +104,65 @@ static char *date2char_str(const char *column); */ static char *char2date_str(const char *date); -int db_get_physmessage_id(u64_t message_idnr, u64_t *physmessage_id) +int db_get_physmessage_id(u64_t message_idnr, u64_t * physmessage_id) { - assert(physmessage_id != NULL); - *physmessage_id = 0; + assert(physmessage_id != NULL); + *physmessage_id = 0; - snprintf(query, DEF_QUERYSIZE, - "SELECT physmessage_id FROM messages " - "WHERE message_idnr = '%llu'", message_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT physmessage_id FROM messages " + "WHERE message_idnr = '%llu'", message_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", + __FILE__, __FUNCTION__); + return -1; + } - if (db_num_rows() < 1) { - db_free_result(); - return 0; - } + if (db_num_rows() < 1) { + db_free_result(); + return 0; + } - *physmessage_id = db_get_result_u64(0, 0); + *physmessage_id = db_get_result_u64(0, 0); - db_free_result(); + db_free_result(); - return 1; + return 1; } -int db_get_quotum_used(u64_t user_idnr, u64_t *curmail_size) +int db_get_quotum_used(u64_t user_idnr, u64_t * curmail_size) { - assert(curmail_size != NULL); - - snprintf(query, DEF_QUERYSIZE, - "SELECT curmail_size FROM users " - "WHERE user_idnr = '%llu'", user_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error getting used quotum for " - "user [%llu]", __FILE__, __FUNCTION__, user_idnr); - return -1; - } - - *curmail_size = db_get_result_u64(0, 0); - db_free_result(); - return 1; + assert(curmail_size != NULL); + + snprintf(query, DEF_QUERYSIZE, + "SELECT curmail_size FROM users " + "WHERE user_idnr = '%llu'", user_idnr); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error getting used quotum for " + "user [%llu]", __FILE__, __FUNCTION__, user_idnr); + return -1; + } + + *curmail_size = db_get_result_u64(0, 0); + db_free_result(); + return 1; } /* this is a local (static) function */ int db_set_quotum_used(u64_t user_idnr, u64_t curmail_size) { - snprintf(query, DEF_QUERYSIZE, - "UPDATE users SET curmail_size = '%llu' " - "WHERE user_idnr = '%llu'", curmail_size, user_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error setting used quotum of " - "[%llu] for user [%llu]", - __FILE__, __FUNCTION__, curmail_size, user_idnr); - return -1; - } - return 0; + snprintf(query, DEF_QUERYSIZE, + "UPDATE users SET curmail_size = '%llu' " + "WHERE user_idnr = '%llu'", curmail_size, user_idnr); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error setting used quotum of " + "[%llu] for user [%llu]", + __FILE__, __FUNCTION__, curmail_size, user_idnr); + return -1; + } + return 0; } int db_add_quotum_used(u64_t user_idnr, u64_t add_size) @@ -188,9 +189,10 @@ int db_subtract_quotum_used(u64_t user_idnr, u64_t sub_size) "UPDATE users SET curmail_size = curmail_size - '%llu' " "WHERE user_idnr = '%llu'", sub_size, user_idnr); if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error subtracting [%llu] from quotum " - "of user [%llu]", __FILE__, __FUNCTION__, - sub_size, user_idnr); + trace(TRACE_ERROR, + "%s,%s: error subtracting [%llu] from quotum " + "of user [%llu]", __FILE__, __FUNCTION__, sub_size, + user_idnr); return -1; } return 0; @@ -199,17 +201,17 @@ int db_subtract_quotum_used(u64_t user_idnr, u64_t sub_size) int db_check_quotum_used(u64_t user_idnr, u64_t msg_size) { snprintf(query, DEF_QUERYSIZE, - "SELECT (maxmail_size > 0) " - " AND (curmail_size + '%llu' > maxmail_size)" - " FROM users " - " WHERE user_idnr = '%llu'", msg_size, user_idnr); + "SELECT (maxmail_size > 0) " + " AND (curmail_size + '%llu' > maxmail_size)" + " FROM users " + " WHERE user_idnr = '%llu'", msg_size, user_idnr); if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: error checking quotum for " - "user [%llu]", __FILE__, __FUNCTION__, user_idnr); + "user [%llu]", __FILE__, __FUNCTION__, user_idnr); return -1; } - + /* If there is a quotum defined, and the inequality is true, * then the message would therefore exceed the quotum, * and so the function returns non-zero. */ @@ -222,123 +224,132 @@ int db_check_quotum_used(u64_t user_idnr, u64_t msg_size) int db_calculate_quotum_all() { - u64_t *user_idnrs; /**< will hold all user_idnr for which the quotum + u64_t *user_idnrs; + /**< will hold all user_idnr for which the quotum has to be set again */ - u64_t *curmail_sizes; /**< will hold current mailsizes */ - int i; - int n; /**< number of records returned */ - int result; - - snprintf(query, DEF_QUERYSIZE, - "SELECT usr.user_idnr, sum(pm.messagesize), usr.curmail_size " - "FROM users usr, mailboxes mbx, messages msg, physmessage pm " - "WHERE pm.id = msg.physmessage_id " - "AND msg.mailbox_idnr = mbx.mailbox_idnr " - "AND mbx.owner_idnr = usr.user_idnr " - "AND msg.status < '2' " - "GROUP BY usr.user_idnr, usr.curmail_size " - "HAVING sum(pm.messagesize) <> usr.curmail_size"); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error findng quotum used", - __FILE__, __FUNCTION__); - return -1; - } - - n = db_num_rows(); - result = n; - if (n == 0) { - trace(TRACE_DEBUG, "%s,%s: quotum is already up to date", - __FILE__, __FUNCTION__); - return 0; - } + u64_t *curmail_sizes; + /**< will hold current mailsizes */ + int i; + int n; + /**< number of records returned */ + int result; - user_idnrs = (u64_t *) my_malloc(n * sizeof(u64_t)); - curmail_sizes = (u64_t *) my_malloc(n * sizeof(u64_t)); - if (user_idnrs == NULL || curmail_sizes == NULL) { - trace(TRACE_ERROR, - "%s,%s: malloc failed. probably out of memory..", __FILE__, - __FUNCTION__); - return -2; - } - for (i = 0; i < n; i++) { - user_idnrs[i] = db_get_result_u64(i, 0); - curmail_sizes[i] = db_get_result_u64(i, 1); - } - db_free_result(); + snprintf(query, DEF_QUERYSIZE, + "SELECT usr.user_idnr, sum(pm.messagesize), usr.curmail_size " + "FROM users usr, mailboxes mbx, messages msg, physmessage pm " + "WHERE pm.id = msg.physmessage_id " + "AND msg.mailbox_idnr = mbx.mailbox_idnr " + "AND mbx.owner_idnr = usr.user_idnr " + "AND msg.status < '2' " + "GROUP BY usr.user_idnr, usr.curmail_size " + "HAVING sum(pm.messagesize) <> usr.curmail_size"); - /* now update the used quotum for all users that need to be updated */ - for (i = 0; i < n; i++) { - if (db_set_quotum_used(user_idnrs[i], curmail_sizes[i]) == -1) { - trace(TRACE_ERROR, "%s,%s: error setting quotum used, " - "trying to continue", __FILE__, __FUNCTION__); - result = -1; + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error findng quotum used", + __FILE__, __FUNCTION__); + return -1; + } + + n = db_num_rows(); + result = n; + if (n == 0) { + trace(TRACE_DEBUG, "%s,%s: quotum is already up to date", + __FILE__, __FUNCTION__); + return 0; + } + + user_idnrs = (u64_t *) my_malloc(n * sizeof(u64_t)); + curmail_sizes = (u64_t *) my_malloc(n * sizeof(u64_t)); + if (user_idnrs == NULL || curmail_sizes == NULL) { + trace(TRACE_ERROR, + "%s,%s: malloc failed. probably out of memory..", + __FILE__, __FUNCTION__); + return -2; + } + for (i = 0; i < n; i++) { + user_idnrs[i] = db_get_result_u64(i, 0); + curmail_sizes[i] = db_get_result_u64(i, 1); + } + db_free_result(); + + /* now update the used quotum for all users that need to be updated */ + for (i = 0; i < n; i++) { + if (db_set_quotum_used(user_idnrs[i], curmail_sizes[i]) == + -1) { + trace(TRACE_ERROR, + "%s,%s: error setting quotum used, " + "trying to continue", __FILE__, + __FUNCTION__); + result = -1; + } } - } - /* free allocated memory */ - my_free(user_idnrs); - my_free(curmail_sizes); - return result; + /* free allocated memory */ + my_free(user_idnrs); + my_free(curmail_sizes); + return result; } int db_calculate_quotum_used(u64_t user_idnr) { - u64_t quotum = 0; - - snprintf(query, DEF_QUERYSIZE, "SELECT SUM(pm.messagesize) " - "FROM physmessage pm, messages m, mailboxes mb " - "WHERE m.physmessage_id = pm.id " - "AND m.mailbox_idnr = mb.mailbox_idnr " - "AND mb.owner_idnr = '%llu' " "AND m.status < 2", user_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", - __FILE__, __FUNCTION__); - return -1; - } - if (db_num_rows() < 1) - trace(TRACE_WARNING, "%s,%s: SUM did not give result, " - "assuming empty mailbox", __FILE__, __FUNCTION__); - else { - quotum = db_get_result_u64(0, 0); - } - db_free_result(); - trace(TRACE_DEBUG, "%s, found quotum usage of [%llu] bytes", - __FUNCTION__, quotum); - /* now insert the used quotum into the users table */ - if (db_set_quotum_used(user_idnr, quotum) == -1) { - if (db_query(query) == -1) { - trace(TRACE_ERROR, - "%s,%s: error setting quotum for user [%llu]", - __FILE__, __FUNCTION__, user_idnr); - return -1; - } - } - return 0; -} - -int db_get_users_from_clientid(u64_t client_id, u64_t **user_ids, + u64_t quotum = 0; + + snprintf(query, DEF_QUERYSIZE, "SELECT SUM(pm.messagesize) " + "FROM physmessage pm, messages m, mailboxes mb " + "WHERE m.physmessage_id = pm.id " + "AND m.mailbox_idnr = mb.mailbox_idnr " + "AND mb.owner_idnr = '%llu' " "AND m.status < 2", + user_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return -1; + } + if (db_num_rows() < 1) + trace(TRACE_WARNING, "%s,%s: SUM did not give result, " + "assuming empty mailbox", __FILE__, __FUNCTION__); + else { + quotum = db_get_result_u64(0, 0); + } + db_free_result(); + trace(TRACE_DEBUG, "%s, found quotum usage of [%llu] bytes", + __FUNCTION__, quotum); + /* now insert the used quotum into the users table */ + if (db_set_quotum_used(user_idnr, quotum) == -1) { + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: error setting quotum for user [%llu]", + __FILE__, __FUNCTION__, user_idnr); + return -1; + } + } + return 0; +} + +int db_get_users_from_clientid(u64_t client_id, u64_t ** user_ids, unsigned *num_users) { unsigned i; - - assert (user_ids != NULL); - assert (num_users != NULL); - + + assert(user_ids != NULL); + assert(num_users != NULL); + snprintf(query, DEF_QUERYSIZE, "SELECT user_idnr FROM users WHERE client_idnr = '%llu'", client_id); if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: error gettings users for " - "client_id [%llu]", __FILE__, __FUNCTION__, client_id); + "client_id [%llu]", __FILE__, __FUNCTION__, + client_id); return -1; } *num_users = db_num_rows(); - *user_ids = (u64_t*) my_malloc(*num_users * sizeof(u64_t)); + *user_ids = (u64_t *) my_malloc(*num_users * sizeof(u64_t)); if (*user_ids == NULL) { - trace(TRACE_ERROR, "%s,%s: error allocating memory, probably " + trace(TRACE_ERROR, + "%s,%s: error allocating memory, probably " "out of memory", __FILE__, __FUNCTION__); db_free_result(); return -2; @@ -352,291 +363,311 @@ int db_get_users_from_clientid(u64_t client_id, u64_t **user_ids, char *db_get_deliver_from_alias(const char *alias) { - char *deliver = NULL; - const char *query_result = NULL; + char *deliver = NULL; + const char *query_result = NULL; - snprintf(query, DEF_QUERYSIZE, - "SELECT deliver_to FROM aliases WHERE alias = '%s'", alias); + snprintf(query, DEF_QUERYSIZE, + "SELECT deliver_to FROM aliases WHERE alias = '%s'", + alias); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", - __FILE__, __FUNCTION__); - return NULL; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return NULL; + } - if (db_num_rows() == 0) { - /* no such user */ - db_free_result(); - return ""; - } + if (db_num_rows() == 0) { + /* no such user */ + db_free_result(); + return ""; + } - query_result = db_get_result(0, 0); - if (!query_result) { - db_free_result(); - return NULL; - } + query_result = db_get_result(0, 0); + if (!query_result) { + db_free_result(); + return NULL; + } - /* FIXME: Why not just use a strdup() here? */ - if (!(deliver = (char *) my_malloc(strlen(query_result) + 1))) { - trace(TRACE_ERROR, "%s,%s: out of mem", __FILE__, __FUNCTION__); - db_free_result(); - return NULL; - } + /* FIXME: Why not just use a strdup() here? */ + if (!(deliver = (char *) my_malloc(strlen(query_result) + 1))) { + trace(TRACE_ERROR, "%s,%s: out of mem", __FILE__, + __FUNCTION__); + db_free_result(); + return NULL; + } - strncpy(deliver, query_result, strlen(query_result) + 1); - db_free_result(); - return deliver; + strncpy(deliver, query_result, strlen(query_result) + 1); + db_free_result(); + return deliver; } int db_addalias(u64_t user_idnr, const char *alias, u64_t clientid) { - /* check if this alias already exists */ - snprintf(query, DEF_QUERYSIZE, - "SELECT alias_idnr FROM aliases " - "WHERE lower(alias) = lower('%s') AND deliver_to = '%llu' " - "AND client_idnr = '%llu'", alias, user_idnr, clientid); + /* check if this alias already exists */ + snprintf(query, DEF_QUERYSIZE, + "SELECT alias_idnr FROM aliases " + "WHERE lower(alias) = lower('%s') AND deliver_to = '%llu' " + "AND client_idnr = '%llu'", alias, user_idnr, clientid); + + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, + "%s,%s: query for searching alias failed", __FILE__, + __FUNCTION__); + return -1; + } - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query for searching alias failed", - __FILE__, __FUNCTION__); - return -1; - } + if (db_num_rows() > 0) { + trace(TRACE_INFO, + "%s,%s: alias [%s] for user [%llu] already exists", + __FILE__, __FUNCTION__, alias, user_idnr); - if (db_num_rows() > 0) { - trace(TRACE_INFO, - "%s,%s: alias [%s] for user [%llu] already exists", __FILE__, - __FUNCTION__, alias, user_idnr); + db_free_result(); + return 1; + } db_free_result(); - return 1; - } - - db_free_result(); - snprintf(query, DEF_QUERYSIZE, - "INSERT INTO aliases (alias,deliver_to,client_idnr) " - "VALUES ('%s','%llu','%llu')", alias, user_idnr, clientid); + snprintf(query, DEF_QUERYSIZE, + "INSERT INTO aliases (alias,deliver_to,client_idnr) " + "VALUES ('%s','%llu','%llu')", alias, user_idnr, + clientid); - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query for adding alias failed", - __FILE__, __FUNCTION__); - return -1; - } - return 0; + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query for adding alias failed", + __FILE__, __FUNCTION__); + return -1; + } + return 0; } int db_addalias_ext(const char *alias, const char *deliver_to, u64_t clientid) { - /* check if this alias already exists */ - if (clientid != 0) { - snprintf(query, DEF_QUERYSIZE, - "SELECT alias_idnr FROM aliases " - "WHERE lower(alias) = lower('%s') AND " - "lower(deliver_to) = lower('%s') " - "AND client_idnr = '%llu'", alias, deliver_to, clientid); - } else { - snprintf(query, DEF_QUERYSIZE, - "SELECT alias_idnr FROM aliases " - "WHERE lower(alias) = lower('%s') " - "AND lower(deliver_to) = lower('%s') ", - alias, deliver_to); - } - - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query for searching alias failed", - __FILE__, __FUNCTION__); - return -1; - } - - if (db_num_rows() > 0) { - trace(TRACE_INFO, "%s,%s: alias [%s] --> [%s] already exists", - __FILE__, __FUNCTION__, alias, deliver_to); + /* check if this alias already exists */ + if (clientid != 0) { + snprintf(query, DEF_QUERYSIZE, + "SELECT alias_idnr FROM aliases " + "WHERE lower(alias) = lower('%s') AND " + "lower(deliver_to) = lower('%s') " + "AND client_idnr = '%llu'", alias, deliver_to, + clientid); + } else { + snprintf(query, DEF_QUERYSIZE, + "SELECT alias_idnr FROM aliases " + "WHERE lower(alias) = lower('%s') " + "AND lower(deliver_to) = lower('%s') ", + alias, deliver_to); + } + + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, + "%s,%s: query for searching alias failed", __FILE__, + __FUNCTION__); + return -1; + } + + if (db_num_rows() > 0) { + trace(TRACE_INFO, + "%s,%s: alias [%s] --> [%s] already exists", + __FILE__, __FUNCTION__, alias, deliver_to); + db_free_result(); + return 1; + } db_free_result(); - return 1; - } - db_free_result(); - snprintf(query, DEF_QUERYSIZE, - "INSERT INTO aliases (alias,deliver_to,client_idnr) " - "VALUES ('%s','%s','%llu')", alias, deliver_to, clientid); - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query for adding alias failed", - __FILE__, __FUNCTION__); - return -1; - } - return 0; + snprintf(query, DEF_QUERYSIZE, + "INSERT INTO aliases (alias,deliver_to,client_idnr) " + "VALUES ('%s','%s','%llu')", alias, deliver_to, clientid); + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query for adding alias failed", + __FILE__, __FUNCTION__); + return -1; + } + return 0; } int db_removealias(u64_t user_idnr, const char *alias) { - snprintf(query, DEF_QUERYSIZE, - "DELETE FROM aliases WHERE deliver_to='%llu' " - "AND lower(alias) = lower('%s')", user_idnr, alias); + snprintf(query, DEF_QUERYSIZE, + "DELETE FROM aliases WHERE deliver_to='%llu' " + "AND lower(alias) = lower('%s')", user_idnr, alias); - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - return -1; - } - return 0; + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, + __FUNCTION__); + return -1; + } + return 0; } int db_removealias_ext(const char *alias, const char *deliver_to) { - snprintf(query, DEF_QUERYSIZE, - "DELETE FROM aliases WHERE lower(deliver_to) = lower('%s') " - "AND lower(alias) = lower('%s')", deliver_to, alias); - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - return -1; - } - return 0; + snprintf(query, DEF_QUERYSIZE, + "DELETE FROM aliases WHERE lower(deliver_to) = lower('%s') " + "AND lower(alias) = lower('%s')", deliver_to, alias); + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, + __FUNCTION__); + return -1; + } + return 0; } int db_get_notify_address(u64_t user_idnr, char **notify_address) { - char *query_result = NULL; - *notify_address = NULL; - snprintf(query, DEF_QUERYSIZE, "SELECT notify_address " - "FROM auto_notifications WHERE user_idnr = %llu", user_idnr); - - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - return -1; - } - if (db_num_rows() > 0) { - query_result = db_get_result(0, 0); - if (query_result && strlen(query_result) > 0) { - /* FIXME: Why not just use a strdup() here? */ - if (! - (*notify_address = - (char *) my_malloc(strlen(query_result) + 1))) { - trace(TRACE_ERROR, "%s,%s: could not allocate", __FILE__, + char *query_result = NULL; + *notify_address = NULL; + snprintf(query, DEF_QUERYSIZE, "SELECT notify_address " + "FROM auto_notifications WHERE user_idnr = %llu", + user_idnr); + + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - db_free_result(); - return -2; - } - snprintf(*notify_address, strlen(query_result) + 1, - "%s", query_result); - trace(TRACE_DEBUG, "%s,%s: found address [%s]", - __FILE__, __FUNCTION__, *notify_address); + return -1; + } + if (db_num_rows() > 0) { + query_result = db_get_result(0, 0); + if (query_result && strlen(query_result) > 0) { + /* FIXME: Why not just use a strdup() here? */ + if (! + (*notify_address = + (char *) my_malloc(strlen(query_result) + + 1))) { + trace(TRACE_ERROR, + "%s,%s: could not allocate", + __FILE__, __FUNCTION__); + db_free_result(); + return -2; + } + snprintf(*notify_address, strlen(query_result) + 1, + "%s", query_result); + trace(TRACE_DEBUG, "%s,%s: found address [%s]", + __FILE__, __FUNCTION__, *notify_address); + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } int db_get_reply_body(u64_t user_idnr, char **reply_body) { - char *query_result; - *reply_body = NULL; - - snprintf(query, DEF_QUERYSIZE, "SELECT reply_body FROM auto_replies " - "WHERE user_idnr = %llu", user_idnr); - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - return -1; - } - if (db_num_rows() > 0) { - query_result = db_get_result(0, 0); - if (query_result && strlen(query_result) > 0) { - /* FIXME: Why not just use a strdup() here? */ - if (! - (*reply_body = - (char *) my_malloc(strlen(query_result) + 1))) { - trace(TRACE_ERROR, "%s,%s: could not allocate", __FILE__, + char *query_result; + *reply_body = NULL; + + snprintf(query, DEF_QUERYSIZE, + "SELECT reply_body FROM auto_replies " + "WHERE user_idnr = %llu", user_idnr); + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - db_free_result(); - return -2; - } - snprintf(*reply_body, strlen(query_result) + 1, - "%s", query_result); - trace(TRACE_DEBUG, "%s,%s: found reply_body [%s]", - __FILE__, __FUNCTION__, *reply_body); + return -1; + } + if (db_num_rows() > 0) { + query_result = db_get_result(0, 0); + if (query_result && strlen(query_result) > 0) { + /* FIXME: Why not just use a strdup() here? */ + if (! + (*reply_body = + (char *) my_malloc(strlen(query_result) + + 1))) { + trace(TRACE_ERROR, + "%s,%s: could not allocate", + __FILE__, __FUNCTION__); + db_free_result(); + return -2; + } + snprintf(*reply_body, strlen(query_result) + 1, + "%s", query_result); + trace(TRACE_DEBUG, "%s,%s: found reply_body [%s]", + __FILE__, __FUNCTION__, *reply_body); + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } u64_t db_get_mailbox_from_message(u64_t message_idnr) { - u64_t mailbox_idnr; + u64_t mailbox_idnr; - snprintf(query, DEF_QUERYSIZE, - "SELECT mailbox_idnr FROM messages " - "WHERE message_idnr = '%llu'", message_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT mailbox_idnr FROM messages " + "WHERE message_idnr = '%llu'", message_idnr); + + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, + __FUNCTION__); + return -1; + } - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - return -1; - } - - if (db_num_rows() < 1) { - trace(TRACE_DEBUG, "%s,%s: No mailbox found for message", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } - mailbox_idnr = db_get_result_u64(0, 0); - db_free_result(); - return mailbox_idnr; + if (db_num_rows() < 1) { + trace(TRACE_DEBUG, "%s,%s: No mailbox found for message", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + mailbox_idnr = db_get_result_u64(0, 0); + db_free_result(); + return mailbox_idnr; } u64_t db_get_useridnr(u64_t message_idnr) { - char *query_result; - u64_t user_idnr; - - snprintf(query, DEF_QUERYSIZE, - "SELECT mailboxes.owner_idnr FROM mailboxes, messages " - "WHERE mailboxes.mailbox_idnr = messages.mailbox_idnr " - "AND messages.message_idnr = '%llu'", message_idnr); - if (db_query(query) == -1) { - /* query failed */ - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); - return -1; - } - - if (db_num_rows() < 1) { - trace(TRACE_DEBUG, "%s,%s: No owner found for message", - __FILE__, __FUNCTION__); + char *query_result; + u64_t user_idnr; + + snprintf(query, DEF_QUERYSIZE, + "SELECT mailboxes.owner_idnr FROM mailboxes, messages " + "WHERE mailboxes.mailbox_idnr = messages.mailbox_idnr " + "AND messages.message_idnr = '%llu'", message_idnr); + if (db_query(query) == -1) { + /* query failed */ + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, + __FUNCTION__); + return -1; + } + + if (db_num_rows() < 1) { + trace(TRACE_DEBUG, "%s,%s: No owner found for message", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + query_result = db_get_result(0, 0); + user_idnr = db_get_result_u64(0, 0); db_free_result(); - return 0; - } - query_result = db_get_result(0, 0); - user_idnr = db_get_result_u64(0, 0); - db_free_result(); - return user_idnr; + return user_idnr; } int db_insert_physmessage_with_internal_date(timestring_t internal_date, - u64_t *physmessage_id) + u64_t * physmessage_id) { char *to_date_str; assert(physmessage_id != NULL); *physmessage_id = 0; to_date_str = char2date_str(internal_date); - + snprintf(query, DEF_QUERYSIZE, "INSERT INTO physmessage (messagesize, internal_date) " "VALUES ('0', %s)", to_date_str); my_free(to_date_str); - + if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: insertion of physmessage failed", - __FILE__, __FUNCTION__); + trace(TRACE_ERROR, + "%s,%s: insertion of physmessage failed", __FILE__, + __FUNCTION__); return -1; } *physmessage_id = db_insert_result("physmessage_id"); @@ -644,7 +675,7 @@ int db_insert_physmessage_with_internal_date(timestring_t internal_date, return 1; } -int db_insert_physmessage(u64_t *physmessage_id) +int db_insert_physmessage(u64_t * physmessage_id) { assert(physmessage_id != NULL); @@ -655,84 +686,87 @@ int db_insert_physmessage(u64_t *physmessage_id) "VALUES ('0', CURRENT_TIMESTAMP)"); if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, __FUNCTION__); + trace(TRACE_ERROR, "%s,%s: query failed", __FILE__, + __FUNCTION__); return -1; } - + *physmessage_id = db_insert_result("physmessage_id"); - + return 1; } int db_update_physmessage(u64_t physmessage_id, u64_t message_size, - u64_t rfc_size) + u64_t rfc_size) { - snprintf(query, DEF_QUERYSIZE, - "UPDATE physmessage SET messagesize = '%llu', " - "rfcsize = '%llu' WHERE id = '%llu'", - message_size, rfc_size, physmessage_id); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error executing query", - __FILE__, __FUNCTION__); - return -1; - } - return 1; + snprintf(query, DEF_QUERYSIZE, + "UPDATE physmessage SET messagesize = '%llu', " + "rfcsize = '%llu' WHERE id = '%llu'", + message_size, rfc_size, physmessage_id); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error executing query", + __FILE__, __FUNCTION__); + return -1; + } + return 1; } int db_insert_message(u64_t user_idnr, const char *mailbox, int create_or_error_mailbox, - const char *unique_id, - u64_t *message_idnr) -{ - u64_t mailboxid; - u64_t physmessage_id; - int result; - - if (!mailbox) - mailbox = "INBOX"; - - switch (create_or_error_mailbox) { - case CREATE_IF_MBOX_NOT_FOUND: - result = db_find_create_mailbox(mailbox, user_idnr, &mailboxid); - break; - case ERROR_IF_MBOX_NOT_FOUND: - default: - result = db_findmailbox(mailbox, user_idnr, &mailboxid); - break; - } - - if (result == -1) { - trace(TRACE_ERROR, "%s,%s: error finding and/or creating mailbox [%s]", - __FILE__, __FUNCTION__, mailbox); - return -1; - } - if (mailboxid == 0) { - trace(TRACE_WARNING, "%s,%s: mailbox [%s] could not be found!", - __FILE__, __FUNCTION__, mailbox); - return -1; - } - - /* insert a new physmessage entry */ - if (db_insert_physmessage(&physmessage_id) == -1) { - trace(TRACE_ERROR, "%s,%s: error inserting physmessage", - __FILE__, __FUNCTION__); - return -1; - } - - /* now insert an entry into the messages table */ - snprintf(query, DEF_QUERYSIZE, "INSERT INTO " - "messages(mailbox_idnr, physmessage_id, unique_id," - "recent_flag, status) " - "VALUES ('%llu', '%llu', '%s', '1', '005')", - mailboxid, - physmessage_id, unique_id ? unique_id : ""); - if (db_query(query) == -1) { - trace(TRACE_STOP, "%s,%s: query failed", __FILE__, __FUNCTION__); - } - - *message_idnr = db_insert_result("message_idnr"); - return 1; + const char *unique_id, u64_t * message_idnr) +{ + u64_t mailboxid; + u64_t physmessage_id; + int result; + + if (!mailbox) + mailbox = "INBOX"; + + switch (create_or_error_mailbox) { + case CREATE_IF_MBOX_NOT_FOUND: + result = + db_find_create_mailbox(mailbox, user_idnr, &mailboxid); + break; + case ERROR_IF_MBOX_NOT_FOUND: + default: + result = db_findmailbox(mailbox, user_idnr, &mailboxid); + break; + } + + if (result == -1) { + trace(TRACE_ERROR, + "%s,%s: error finding and/or creating mailbox [%s]", + __FILE__, __FUNCTION__, mailbox); + return -1; + } + if (mailboxid == 0) { + trace(TRACE_WARNING, + "%s,%s: mailbox [%s] could not be found!", __FILE__, + __FUNCTION__, mailbox); + return -1; + } + + /* insert a new physmessage entry */ + if (db_insert_physmessage(&physmessage_id) == -1) { + trace(TRACE_ERROR, "%s,%s: error inserting physmessage", + __FILE__, __FUNCTION__); + return -1; + } + + /* now insert an entry into the messages table */ + snprintf(query, DEF_QUERYSIZE, "INSERT INTO " + "messages(mailbox_idnr, physmessage_id, unique_id," + "recent_flag, status) " + "VALUES ('%llu', '%llu', '%s', '1', '005')", + mailboxid, physmessage_id, unique_id ? unique_id : ""); + if (db_query(query) == -1) { + trace(TRACE_STOP, "%s,%s: query failed", __FILE__, + __FUNCTION__); + } + + *message_idnr = db_insert_result("message_idnr"); + return 1; } int db_message_set_unique_id(u64_t message_idnr, const char *unique_id) @@ -742,7 +776,8 @@ int db_message_set_unique_id(u64_t message_idnr, const char *unique_id) "WHERE message_idnr = '%llu'", unique_id, message_idnr); if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: setting unique id for message " - "[%llu] failed", __FILE__, __FUNCTION__, message_idnr); + "[%llu] failed", __FILE__, __FUNCTION__, + message_idnr); return -1; } return 0; @@ -751,389 +786,411 @@ int db_message_set_unique_id(u64_t message_idnr, const char *unique_id) int db_update_message(u64_t message_idnr, const char *unique_id, u64_t message_size, u64_t rfc_size) { - u64_t physmessage_id = 0; + u64_t physmessage_id = 0; - if (db_message_set_unique_id(message_idnr, unique_id) < 0) { - trace(TRACE_STOP, "%s,%s: setting unique id failed of message " - "[%llu] failed", - __FILE__, __FUNCTION__, message_idnr); - } + if (db_message_set_unique_id(message_idnr, unique_id) < 0) { + trace(TRACE_STOP, + "%s,%s: setting unique id failed of message " + "[%llu] failed", __FILE__, __FUNCTION__, + message_idnr); + } - /* update the fields in the physmessage table */ - if (db_get_physmessage_id(message_idnr, &physmessage_id) == -1) { - trace(TRACE_ERROR, - "%s,%s: could not find physmessage_id of message", __FILE__, - __FUNCTION__); - return -1; - } - - if (db_update_physmessage(physmessage_id, message_size, rfc_size) == -1) { - trace(TRACE_ERROR, "%s,%s: error updating physmessage [%llu]. " - "The database might be inconsistent. Run dbmail-maintenance", - __FILE__, __FUNCTION__, physmessage_id); - } - - if (db_add_quotum_used(db_get_useridnr(message_idnr), message_size) == -1) { - trace (TRACE_ERROR, "%s,%s: error calculating quotum " - "used for user [%llu]. Database might be " - "inconsistent. run dbmail-maintenance", - __FILE__, __FUNCTION__, db_get_useridnr(message_idnr)); - return -1; - } - return 0; -} - -int db_insert_message_block_physmessage(const char *block, u64_t block_size, - u64_t physmessage_id, - u64_t *messageblk_idnr) -{ - char *escaped_query = NULL; - unsigned maxesclen = (READ_BLOCK_SIZE + 1) * 2 + DEF_QUERYSIZE; - unsigned startlen = 0; - unsigned esclen = 0; - - assert(messageblk_idnr != NULL); - *messageblk_idnr = 0; - - if (block == NULL) { - trace(TRACE_ERROR, - "%s,%s: got NULL as block. Insertion not possible", - __FILE__, __FUNCTION__); - return -1; - } - - if (block_size > READ_BLOCK_SIZE) { - trace(TRACE_ERROR, "%s,%s: blocksize [%llu], maximum is [%ld]", - __FILE__, __FUNCTION__, block_size, READ_BLOCK_SIZE); - return -1; - } - - escaped_query = (char *) my_malloc(sizeof(char) * maxesclen); - if (!escaped_query) { - trace(TRACE_ERROR, "%s,%s: not enough memory", __FILE__, - __FUNCTION__); - return -1; - } - - startlen = snprintf(escaped_query, maxesclen, "INSERT INTO messageblks" - "(messageblk,blocksize, physmessage_id) VALUES ('"); - - /* escape & add data */ - esclen = db_escape_string(&escaped_query[startlen], block, block_size); - - snprintf(&escaped_query[esclen + startlen], - maxesclen - esclen - startlen, "', '%llu', '%llu')", - block_size, physmessage_id); - - if (db_query(escaped_query) == -1) { - my_free(escaped_query); - - trace(TRACE_ERROR, "%s,%s: dbquery failed\n", __FILE__, - __FUNCTION__); - return -1; - } - - /* all done, clean up & exit */ - my_free(escaped_query); - - *messageblk_idnr = db_insert_result("messageblk_idnr"); - return 1; -} - -int db_insert_message_block(const char *block, u64_t block_size, - u64_t message_idnr, u64_t *messageblk_idnr) -{ - u64_t physmessage_id; - - assert(messageblk_idnr != NULL); - *messageblk_idnr = 0; - if (block == NULL) { - trace(TRACE_ERROR, - "%s,%s: got NULL as block, insertion not possible\n", - __FILE__, __FUNCTION__); - return -1; - } - - if (db_get_physmessage_id(message_idnr, &physmessage_id) < 0) { - trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", - __FILE__, __FUNCTION__); - return -1; - } - - if (db_insert_message_block_physmessage(block, block_size, physmessage_id, - messageblk_idnr) < 0) { - trace(TRACE_ERROR, "%s,%s: error inserting messageblks for " - "physmessage [%llu]", __FILE__, __FUNCTION__, physmessage_id); - return -1; - } - return 1; + /* update the fields in the physmessage table */ + if (db_get_physmessage_id(message_idnr, &physmessage_id) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not find physmessage_id of message", + __FILE__, __FUNCTION__); + return -1; + } + + if (db_update_physmessage(physmessage_id, message_size, rfc_size) + == -1) { + trace(TRACE_ERROR, + "%s,%s: error updating physmessage [%llu]. " + "The database might be inconsistent. Run dbmail-maintenance", + __FILE__, __FUNCTION__, physmessage_id); + } + + if (db_add_quotum_used(db_get_useridnr(message_idnr), message_size) + == -1) { + trace(TRACE_ERROR, + "%s,%s: error calculating quotum " + "used for user [%llu]. Database might be " + "inconsistent. run dbmail-maintenance", __FILE__, + __FUNCTION__, db_get_useridnr(message_idnr)); + return -1; + } + return 0; } -int db_log_ip(const char *ip) +int db_insert_message_block_physmessage(const char *block, + u64_t block_size, + u64_t physmessage_id, + u64_t * messageblk_idnr) { - u64_t id = 0; + char *escaped_query = NULL; + unsigned maxesclen = (READ_BLOCK_SIZE + 1) * 2 + DEF_QUERYSIZE; + unsigned startlen = 0; + unsigned esclen = 0; + + assert(messageblk_idnr != NULL); + *messageblk_idnr = 0; + + if (block == NULL) { + trace(TRACE_ERROR, + "%s,%s: got NULL as block. Insertion not possible", + __FILE__, __FUNCTION__); + return -1; + } - snprintf(query, DEF_QUERYSIZE, - "SELECT idnr FROM pbsp WHERE ipnumber = '%s'", ip); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not access ip-log table " - "(pop/imap-before-smtp): %s", __FILE__, __FUNCTION__, ip); - return -1; - } + if (block_size > READ_BLOCK_SIZE) { + trace(TRACE_ERROR, + "%s,%s: blocksize [%llu], maximum is [%ld]", + __FILE__, __FUNCTION__, block_size, READ_BLOCK_SIZE); + return -1; + } - id = db_get_result_u64(0, 0); + escaped_query = (char *) my_malloc(sizeof(char) * maxesclen); + if (!escaped_query) { + trace(TRACE_ERROR, "%s,%s: not enough memory", __FILE__, + __FUNCTION__); + return -1; + } - db_free_result(); + startlen = + snprintf(escaped_query, maxesclen, + "INSERT INTO messageblks" + "(messageblk,blocksize, physmessage_id) VALUES ('"); - if (id) { - /* this IP is already in the table, update the 'since' field */ - snprintf(query, DEF_QUERYSIZE, "UPDATE pbsp " - "SET since = CURRENT_TIMESTAMP WHERE idnr='%llu'", id); + /* escape & add data */ + esclen = + db_escape_string(&escaped_query[startlen], block, block_size); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not update ip-log " - "(pop/imap-before-smtp)", __FILE__, __FUNCTION__); - return -1; + snprintf(&escaped_query[esclen + startlen], + maxesclen - esclen - startlen, "', '%llu', '%llu')", + block_size, physmessage_id); + + if (db_query(escaped_query) == -1) { + my_free(escaped_query); + + trace(TRACE_ERROR, "%s,%s: dbquery failed\n", __FILE__, + __FUNCTION__); + return -1; + } + + /* all done, clean up & exit */ + my_free(escaped_query); + + *messageblk_idnr = db_insert_result("messageblk_idnr"); + return 1; +} + +int db_insert_message_block(const char *block, u64_t block_size, + u64_t message_idnr, u64_t * messageblk_idnr) +{ + u64_t physmessage_id; + + assert(messageblk_idnr != NULL); + *messageblk_idnr = 0; + if (block == NULL) { + trace(TRACE_ERROR, + "%s,%s: got NULL as block, insertion not possible\n", + __FILE__, __FUNCTION__); + return -1; + } + + if (db_get_physmessage_id(message_idnr, &physmessage_id) < 0) { + trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", + __FILE__, __FUNCTION__); + return -1; + } + + if (db_insert_message_block_physmessage + (block, block_size, physmessage_id, messageblk_idnr) < 0) { + trace(TRACE_ERROR, + "%s,%s: error inserting messageblks for " + "physmessage [%llu]", __FILE__, __FUNCTION__, + physmessage_id); + return -1; } - } else { - /* IP not in table, insert row */ + return 1; +} + +int db_log_ip(const char *ip) +{ + u64_t id = 0; + snprintf(query, DEF_QUERYSIZE, - "INSERT INTO pbsp (since, ipnumber) " - "VALUES (CURRENT_TIMESTAMP, '%s')", ip); + "SELECT idnr FROM pbsp WHERE ipnumber = '%s'", ip); if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not log IP number to dbase " - "(pop/imap-before-smtp)", __FILE__, __FUNCTION__); - return -1; + trace(TRACE_ERROR, "%s,%s: could not access ip-log table " + "(pop/imap-before-smtp): %s", __FILE__, __FUNCTION__, + ip); + return -1; } - } - trace(TRACE_DEBUG, "%s,%s: ip [%s] logged\n", __FILE__, __FUNCTION__, - ip); + id = db_get_result_u64(0, 0); - return 0; + db_free_result(); + + if (id) { + /* this IP is already in the table, update the 'since' field */ + snprintf(query, DEF_QUERYSIZE, "UPDATE pbsp " + "SET since = CURRENT_TIMESTAMP WHERE idnr='%llu'", + id); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not update ip-log " + "(pop/imap-before-smtp)", __FILE__, + __FUNCTION__); + return -1; + } + } else { + /* IP not in table, insert row */ + snprintf(query, DEF_QUERYSIZE, + "INSERT INTO pbsp (since, ipnumber) " + "VALUES (CURRENT_TIMESTAMP, '%s')", ip); + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not log IP number to dbase " + "(pop/imap-before-smtp)", __FILE__, + __FUNCTION__); + return -1; + } + } + + trace(TRACE_DEBUG, "%s,%s: ip [%s] logged\n", __FILE__, + __FUNCTION__, ip); + + return 0; } int db_cleanup_iplog(const char *lasttokeep) { - snprintf(query, DEF_QUERYSIZE, "DELETE FROM pbsp WHERE since < '%s'", - lasttokeep); + snprintf(query, DEF_QUERYSIZE, + "DELETE FROM pbsp WHERE since < '%s'", lasttokeep); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s:%s: error executing query", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s:%s: error executing query", + __FILE__, __FUNCTION__); + return -1; + } - return 0; + return 0; } int db_cleanup() { - return db_do_cleanup(DB_TABLENAMES, DB_NTABLES); + return db_do_cleanup(DB_TABLENAMES, DB_NTABLES); } int db_empty_mailbox(u64_t user_idnr) { - u64_t *mboxids = NULL; - unsigned n, i; - int result = 0; - - snprintf(query, DEF_QUERYSIZE, - "SELECT mailbox_idnr FROM mailboxes WHERE owner_idnr='%llu'", - user_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error executing query", - __FILE__, __FUNCTION__); - return -1; - } - n = db_num_rows(); - if (n == 0) { - db_free_result(); - trace(TRACE_WARNING, - "%s,%s: user [%llu] does not have any mailboxes?", __FILE__, - __FUNCTION__, user_idnr); - return 0; - } + u64_t *mboxids = NULL; + unsigned n, i; + int result = 0; - mboxids = (u64_t *) my_malloc(n * sizeof(u64_t)); - if (!mboxids) { - trace(TRACE_ERROR, "%s,%s: not enough memory", - __FILE__, __FUNCTION__); - db_free_result(); - return -2; - } + snprintf(query, DEF_QUERYSIZE, + "SELECT mailbox_idnr FROM mailboxes WHERE owner_idnr='%llu'", + user_idnr); - for (i = 0; i < n; i++) { - mboxids[i] = db_get_result_u64(i, 0); - } - db_free_result(); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error executing query", + __FILE__, __FUNCTION__); + return -1; + } + n = db_num_rows(); + if (n == 0) { + db_free_result(); + trace(TRACE_WARNING, + "%s,%s: user [%llu] does not have any mailboxes?", + __FILE__, __FUNCTION__, user_idnr); + return 0; + } + + mboxids = (u64_t *) my_malloc(n * sizeof(u64_t)); + if (!mboxids) { + trace(TRACE_ERROR, "%s,%s: not enough memory", + __FILE__, __FUNCTION__); + db_free_result(); + return -2; + } - for (i = 0; i < n; i++) { - if (db_delete_mailbox(mboxids[i], 1, 1) == -1) { - trace(TRACE_ERROR, "%s,%s: error emptying mailbox [%llu]", - __FILE__, __FUNCTION__, mboxids[i]); - result = -1; + for (i = 0; i < n; i++) { + mboxids[i] = db_get_result_u64(i, 0); + } + db_free_result(); + + for (i = 0; i < n; i++) { + if (db_delete_mailbox(mboxids[i], 1, 1) == -1) { + trace(TRACE_ERROR, + "%s,%s: error emptying mailbox [%llu]", + __FILE__, __FUNCTION__, mboxids[i]); + result = -1; + } } - } - my_free(mboxids); - return result; + my_free(mboxids); + return result; } int db_icheck_messageblks(struct list *lost_list) { - char *query_result; - u64_t messageblk_idnr; - int i, n; - list_init(lost_list); - - /* get all lost message blocks. Instead of doing all kinds of - * nasty stuff here, we let the RDBMS handle all this. Problem - * is that MySQL cannot handle subqueries. This is handled by - * a left join select query. - * This query will select all message block idnr that have no - * associated physmessage in the physmessage table. - */ - snprintf(query, DEF_QUERYSIZE, - "SELECT mb.messageblk_idnr FROM messageblks mb " - "LEFT JOIN physmessage pm ON " - "mb.physmessage_id = pm.id " - "WHERE pm.id IS NULL"); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: Could not execute query", - __FILE__, __FUNCTION__); - return -1; - } - - n = db_num_rows(); - if (n < 1) { - trace(TRACE_DEBUG, "%s,%s: no lost messageblocks", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } + char *query_result; + u64_t messageblk_idnr; + int i, n; + list_init(lost_list); - /* FIXME: this is actually properly designed... */ - for (i = 0; i < n; i++) { - query_result = db_get_result(i, 0); - if (query_result) - messageblk_idnr = strtoull(query_result, NULL, 10); - else - continue; + /* get all lost message blocks. Instead of doing all kinds of + * nasty stuff here, we let the RDBMS handle all this. Problem + * is that MySQL cannot handle subqueries. This is handled by + * a left join select query. + * This query will select all message block idnr that have no + * associated physmessage in the physmessage table. + */ + snprintf(query, DEF_QUERYSIZE, + "SELECT mb.messageblk_idnr FROM messageblks mb " + "LEFT JOIN physmessage pm ON " + "mb.physmessage_id = pm.id " "WHERE pm.id IS NULL"); - trace(TRACE_INFO, "%s,%s: found lost block id [%llu]", - __FILE__, __FUNCTION__, messageblk_idnr); - if (!list_nodeadd(lost_list, &messageblk_idnr, sizeof(u64_t))) { - trace(TRACE_ERROR, "%s,%s: could not add block to list", - __FILE__, __FUNCTION__); - list_freelist(&lost_list->start); - db_free_result(); - return -2; + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: Could not execute query", + __FILE__, __FUNCTION__); + return -1; } - } - db_free_result(); - return 0; + + n = db_num_rows(); + if (n < 1) { + trace(TRACE_DEBUG, "%s,%s: no lost messageblocks", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + + /* FIXME: this is actually properly designed... */ + for (i = 0; i < n; i++) { + query_result = db_get_result(i, 0); + if (query_result) + messageblk_idnr = strtoull(query_result, NULL, 10); + else + continue; + + trace(TRACE_INFO, "%s,%s: found lost block id [%llu]", + __FILE__, __FUNCTION__, messageblk_idnr); + if (!list_nodeadd + (lost_list, &messageblk_idnr, sizeof(u64_t))) { + trace(TRACE_ERROR, + "%s,%s: could not add block to list", + __FILE__, __FUNCTION__); + list_freelist(&lost_list->start); + db_free_result(); + return -2; + } + } + db_free_result(); + return 0; } int db_icheck_messages(struct list *lost_list) { - u64_t message_idnr; - char *query_result; - int i, n; + u64_t message_idnr; + char *query_result; + int i, n; - list_init(lost_list); + list_init(lost_list); - snprintf(query, DEF_QUERYSIZE, - "SELECT msg.message_idnr FROM messages msg " - "LEFT JOIN mailboxes mbx ON " - "msg.mailbox_idnr=mbx.mailbox_idnr " - "WHERE mbx.mailbox_idnr IS NULL"); + snprintf(query, DEF_QUERYSIZE, + "SELECT msg.message_idnr FROM messages msg " + "LEFT JOIN mailboxes mbx ON " + "msg.mailbox_idnr=mbx.mailbox_idnr " + "WHERE mbx.mailbox_idnr IS NULL"); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", - __FILE__, __FUNCTION__); - return -2; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return -2; + } - n = db_num_rows(); - if (n < 1) { - trace(TRACE_DEBUG, "%s,%s: no lost messages", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } + n = db_num_rows(); + if (n < 1) { + trace(TRACE_DEBUG, "%s,%s: no lost messages", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } - /* FIXME: this is actually properly designed... */ - for (i = 0; i < n; i++) { - query_result = db_get_result(i, 0); - if (query_result) - message_idnr = strtoull(query_result, NULL, 10); - else - continue; + /* FIXME: this is actually properly designed... */ + for (i = 0; i < n; i++) { + query_result = db_get_result(i, 0); + if (query_result) + message_idnr = strtoull(query_result, NULL, 10); + else + continue; - trace(TRACE_INFO, "%s,%s: found lost message id [%llu]", - __FILE__, __FUNCTION__, message_idnr); - if (!list_nodeadd(lost_list, &message_idnr, sizeof(u64_t))) { - trace(TRACE_ERROR, "%s,%s: could not add message to list", - __FILE__, __FUNCTION__); - list_freelist(&lost_list->start); - db_free_result(); - return -2; + trace(TRACE_INFO, "%s,%s: found lost message id [%llu]", + __FILE__, __FUNCTION__, message_idnr); + if (!list_nodeadd(lost_list, &message_idnr, sizeof(u64_t))) { + trace(TRACE_ERROR, + "%s,%s: could not add message to list", + __FILE__, __FUNCTION__); + list_freelist(&lost_list->start); + db_free_result(); + return -2; + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } int db_icheck_mailboxes(struct list *lost_list) { - u64_t mailbox_idnr; - char *query_result; - int i, n; + u64_t mailbox_idnr; + char *query_result; + int i, n; - list_init(lost_list); + list_init(lost_list); - snprintf(query, DEF_QUERYSIZE, - "SELECT mbx.mailbox_idnr FROM mailboxes mbx " - "LEFT JOIN users usr ON " - "mbx.owner_idnr=usr.user_idnr " - "WHERE usr.user_idnr is NULL"); + snprintf(query, DEF_QUERYSIZE, + "SELECT mbx.mailbox_idnr FROM mailboxes mbx " + "LEFT JOIN users usr ON " + "mbx.owner_idnr=usr.user_idnr " + "WHERE usr.user_idnr is NULL"); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", - __FILE__, __FUNCTION__); - return -2; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return -2; + } - n = db_num_rows(); - if (n < 1) { - trace(TRACE_DEBUG, "%s,%s: no lost mailboxes", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } + n = db_num_rows(); + if (n < 1) { + trace(TRACE_DEBUG, "%s,%s: no lost mailboxes", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } - /* FIXME: this is actually properly designed... */ - for (i = 0; i < n; i++) { - query_result = db_get_result(i, 0); - if (query_result) - mailbox_idnr = strtoull(query_result, NULL, 10); - else - continue; + /* FIXME: this is actually properly designed... */ + for (i = 0; i < n; i++) { + query_result = db_get_result(i, 0); + if (query_result) + mailbox_idnr = strtoull(query_result, NULL, 10); + else + continue; - trace(TRACE_INFO, "%s,%s: found lost mailbox id [%llu]", - __FILE__, __FUNCTION__, mailbox_idnr); - if (!list_nodeadd(lost_list, &mailbox_idnr, sizeof(u64_t))) { - trace(TRACE_ERROR, "%s,%s: could not add mailbox to list", - __FILE__, __FUNCTION__); - list_freelist(&lost_list->start); - db_free_result(); - return -2; + trace(TRACE_INFO, "%s,%s: found lost mailbox id [%llu]", + __FILE__, __FUNCTION__, mailbox_idnr); + if (!list_nodeadd(lost_list, &mailbox_idnr, sizeof(u64_t))) { + trace(TRACE_ERROR, + "%s,%s: could not add mailbox to list", + __FILE__, __FUNCTION__); + list_freelist(&lost_list->start); + db_free_result(); + return -2; + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } int db_icheck_null_physmessages(struct list *lost_list) @@ -1141,21 +1198,21 @@ int db_icheck_null_physmessages(struct list *lost_list) u64_t physmessage_id; char *result_string; unsigned i, n; - + list_init(lost_list); - + snprintf(query, DEF_QUERYSIZE, "SELECT pm.id FROM physmessage pm " "LEFT JOIN messageblks mbk ON " "pm.id = mbk.physmessage_id " "WHERE mbk.physmessage_id is NULL"); - + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not execute query", __FILE__, __FUNCTION__); return -1; } - + n = db_num_rows(); if (n < 1) { trace(TRACE_DEBUG, "%s,%s: no null physmessages", @@ -1163,18 +1220,21 @@ int db_icheck_null_physmessages(struct list *lost_list) return 0; } - /* FIXME: this is actually properly designed... */ + /* FIXME: this is actually properly designed... */ for (i = 0; i < n; i++) { result_string = db_get_result(i, 0); if (result_string) physmessage_id = strtoull(result_string, NULL, 10); else continue; - - trace(TRACE_INFO, "%s,%s: found empty physmessage_id [%llu]", - __FILE__, __FUNCTION__, physmessage_id); - if (!list_nodeadd(lost_list, &physmessage_id, sizeof(u64_t))) { - trace(TRACE_ERROR, "%s,%s: could not add physmessage " + + trace(TRACE_INFO, + "%s,%s: found empty physmessage_id [%llu]", __FILE__, + __FUNCTION__, physmessage_id); + if (!list_nodeadd + (lost_list, &physmessage_id, sizeof(u64_t))) { + trace(TRACE_ERROR, + "%s,%s: could not add physmessage " "to list", __FILE__, __FUNCTION__); list_freelist(&lost_list->start); db_free_result(); @@ -1187,67 +1247,67 @@ int db_icheck_null_physmessages(struct list *lost_list) int db_icheck_null_messages(struct list *lost_list) { - u64_t message_idnr; - char *query_result; - int i, n; + u64_t message_idnr; + char *query_result; + int i, n; - list_init(lost_list); + list_init(lost_list); - snprintf(query, DEF_QUERYSIZE, - "SELECT msg.message_idnr FROM messages msg " - "LEFT JOIN physmessage pm ON " - "msg.physmessage_id = pm.id " - "WHERE pm.id is NULL"); + snprintf(query, DEF_QUERYSIZE, + "SELECT msg.message_idnr FROM messages msg " + "LEFT JOIN physmessage pm ON " + "msg.physmessage_id = pm.id " "WHERE pm.id is NULL"); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return -1; + } - n = db_num_rows(); - if (n < 1) { - trace(TRACE_DEBUG, "%s,%s: no null messages", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } + n = db_num_rows(); + if (n < 1) { + trace(TRACE_DEBUG, "%s,%s: no null messages", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } - /* FIXME: this is actually properly designed... */ - for (i = 0; i < n; i++) { - query_result = db_get_result(i, 0); - if (query_result) - message_idnr = strtoull(query_result, NULL, 10); - else - continue; + /* FIXME: this is actually properly designed... */ + for (i = 0; i < n; i++) { + query_result = db_get_result(i, 0); + if (query_result) + message_idnr = strtoull(query_result, NULL, 10); + else + continue; - trace(TRACE_INFO, "%s,%s: found empty message id [%llu]", - __FILE__, __FUNCTION__, message_idnr); - if (!list_nodeadd(lost_list, &message_idnr, sizeof(u64_t))) { - trace(TRACE_ERROR, "%s,%s: could not add message to list", - __FILE__, __FUNCTION__); - list_freelist(&lost_list->start); - db_free_result(); - return -2; + trace(TRACE_INFO, "%s,%s: found empty message id [%llu]", + __FILE__, __FUNCTION__, message_idnr); + if (!list_nodeadd(lost_list, &message_idnr, sizeof(u64_t))) { + trace(TRACE_ERROR, + "%s,%s: could not add message to list", + __FILE__, __FUNCTION__); + list_freelist(&lost_list->start); + db_free_result(); + return -2; + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } int db_set_message_status(u64_t message_idnr, int status) { - snprintf(query, DEF_QUERYSIZE, "UPDATE messages SET status = %d " - "WHERE message_idnr = '%llu'", status, message_idnr); - return db_query(query); + snprintf(query, DEF_QUERYSIZE, "UPDATE messages SET status = %d " + "WHERE message_idnr = '%llu'", status, message_idnr); + return db_query(query); } int db_delete_messageblk(u64_t messageblk_idnr) { - snprintf(query, DEF_QUERYSIZE, - "DELETE FROM messageblks " - "WHERE messageblk_idnr = '%llu'", messageblk_idnr); - return db_query(query); + snprintf(query, DEF_QUERYSIZE, + "DELETE FROM messageblks " + "WHERE messageblk_idnr = '%llu'", messageblk_idnr); + return db_query(query); } int db_delete_physmessage(u64_t physmessage_id) @@ -1260,7 +1320,7 @@ int db_delete_physmessage(u64_t physmessage_id) __FILE__, __FUNCTION__); return -1; } - + /* if foreign keys do their work (not with MySQL ISAM tables :( ) the next query would not be necessary */ snprintf(query, DEF_QUERYSIZE, @@ -1270,12 +1330,11 @@ int db_delete_physmessage(u64_t physmessage_id) trace(TRACE_ERROR, "%s,%s: could not execute query. There " "are now messageblocks in the database that have no " "physmessage attached to them. run dbmail-maintenance " - "to fix this.", - __FILE__, __FUNCTION__); + "to fix this.", __FILE__, __FUNCTION__); return -1; } - + return 1; } @@ -1288,7 +1347,7 @@ int db_delete_message(u64_t message_idnr) __FILE__, __FUNCTION__); return -1; } - + /* now delete the message from the message table */ snprintf(query, DEF_QUERYSIZE, "DELETE FROM messages WHERE message_idnr = '%llu'", @@ -1298,7 +1357,7 @@ int db_delete_message(u64_t message_idnr) __FILE__, __FUNCTION__); return -1; } - + /* find if there are other messages pointing to the same physmessage entry */ snprintf(query, DEF_QUERYSIZE, @@ -1315,611 +1374,662 @@ int db_delete_message(u64_t message_idnr) * be removed */ db_free_result(); if (db_delete_physmessage(physmessage_id) < 0) { - trace(TRACE_ERROR,"%s,%s: error deleting physmessage", + trace(TRACE_ERROR, + "%s,%s: error deleting physmessage", __FILE__, __FUNCTION__); return -1; } - } else + } else db_free_result(); return 1; } -int db_delete_mailbox(u64_t mailbox_idnr, int only_empty, int update_curmail_size) -{ - unsigned i, n; - u64_t *message_idnrs; - u64_t user_idnr = 0; - int result; - u64_t mailbox_size; - - /* get the user_idnr of the owner of the mailbox */ - result = db_get_mailbox_owner(mailbox_idnr, &user_idnr); - if (result < 0) { - trace(TRACE_ERROR, "%s,%s: cannot find owner of mailbox for " - "mailbox [%llu]", __FILE__, __FUNCTION__, mailbox_idnr); - return -1; - } - if (result == 0) { - trace(TRACE_ERROR, "%s,%s: unable to find owner of mailbox " - "[%llu]", __FILE__, __FUNCTION__, mailbox_idnr); - return 0; - } - - if (update_curmail_size) { - if (db_get_mailbox_size(mailbox_idnr, 0, &mailbox_size) < 0) { - trace(TRACE_ERROR, "%s,%s: error getting mailbox size " - "for mailbox [%llu]", __FILE__, __FUNCTION__, - mailbox_idnr); - return -1; - } - } - - /* remove the mailbox */ - if (!only_empty) { - /* delete mailbox */ - snprintf(query, DEF_QUERYSIZE, - "DELETE FROM mailboxes WHERE mailbox_idnr = '%llu'", - mailbox_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not delete mailbox [%llu]", - __FILE__, __FUNCTION__, mailbox_idnr); - return -1; - } - } - - /* we want to delete all messages from the mailbox. So we - * need to find all messages in the box */ - snprintf(query, DEF_QUERYSIZE, - "SELECT message_idnr FROM messages " - "WHERE mailbox_idnr = '%llu'", mailbox_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, - "%s,%s: could not select message ID's for mailbox [%llu]", - __FILE__, __FUNCTION__, mailbox_idnr); - return -1; - } - - n = db_num_rows(); - if (n == 0) { +int db_delete_mailbox(u64_t mailbox_idnr, int only_empty, + int update_curmail_size) +{ + unsigned i, n; + u64_t *message_idnrs; + u64_t user_idnr = 0; + int result; + u64_t mailbox_size; + + /* get the user_idnr of the owner of the mailbox */ + result = db_get_mailbox_owner(mailbox_idnr, &user_idnr); + if (result < 0) { + trace(TRACE_ERROR, + "%s,%s: cannot find owner of mailbox for " + "mailbox [%llu]", __FILE__, __FUNCTION__, + mailbox_idnr); + return -1; + } + if (result == 0) { + trace(TRACE_ERROR, + "%s,%s: unable to find owner of mailbox " "[%llu]", + __FILE__, __FUNCTION__, mailbox_idnr); + return 0; + } + + if (update_curmail_size) { + if (db_get_mailbox_size(mailbox_idnr, 0, &mailbox_size) < + 0) { + trace(TRACE_ERROR, + "%s,%s: error getting mailbox size " + "for mailbox [%llu]", __FILE__, __FUNCTION__, + mailbox_idnr); + return -1; + } + } + + /* remove the mailbox */ + if (!only_empty) { + /* delete mailbox */ + snprintf(query, DEF_QUERYSIZE, + "DELETE FROM mailboxes WHERE mailbox_idnr = '%llu'", + mailbox_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not delete mailbox [%llu]", + __FILE__, __FUNCTION__, mailbox_idnr); + return -1; + } + } + + /* we want to delete all messages from the mailbox. So we + * need to find all messages in the box */ + snprintf(query, DEF_QUERYSIZE, + "SELECT message_idnr FROM messages " + "WHERE mailbox_idnr = '%llu'", mailbox_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not select message ID's for mailbox [%llu]", + __FILE__, __FUNCTION__, mailbox_idnr); + return -1; + } + + n = db_num_rows(); + if (n == 0) { + db_free_result(); + trace(TRACE_INFO, "%s,%s: mailbox is empty", __FILE__, + __FUNCTION__); + } + + if (!(message_idnrs = (u64_t *) my_malloc(n * sizeof(u64_t)))) { + trace(TRACE_ERROR, "%s,%s: error allocating memory", + __FILE__, __FUNCTION__); + return -1; + } + for (i = 0; i < n; i++) + message_idnrs[i] = db_get_result_u64(0, 0); db_free_result(); - trace(TRACE_INFO, "%s,%s: mailbox is empty", __FILE__, __FUNCTION__); - } - - if (!(message_idnrs = (u64_t *) my_malloc(n * sizeof(u64_t)))) { - trace(TRACE_ERROR, "%s,%s: error allocating memory", - __FILE__, __FUNCTION__); - return -1; - } - for (i = 0; i < n; i++) - message_idnrs[i] = db_get_result_u64(0, 0); - db_free_result(); - /* delete every message in the mailbox */ - for (i = 0; i < n; i++) { - if (db_delete_message(message_idnrs[i]) == -1) { - trace(TRACE_ERROR, "%s,%s: error deleting message [%llu] " - "database might be inconsistent. run dbmail-maintenance", - __FILE__, __FUNCTION__, message_idnrs[i]); - return -1; - } - } - my_free(message_idnrs); - - /* calculate the new quotum */ - if (update_curmail_size) { - if (db_subtract_quotum_used(user_idnr, mailbox_size) < 0) { - trace(TRACE_ERROR, "%s,%s: error decreasing curmail_size", - __FILE__, __FUNCTION__); - return -1; - } - } - return 0; + /* delete every message in the mailbox */ + for (i = 0; i < n; i++) { + if (db_delete_message(message_idnrs[i]) == -1) { + trace(TRACE_ERROR, + "%s,%s: error deleting message [%llu] " + "database might be inconsistent. run dbmail-maintenance", + __FILE__, __FUNCTION__, message_idnrs[i]); + return -1; + } + } + my_free(message_idnrs); + + /* calculate the new quotum */ + if (update_curmail_size) { + if (db_subtract_quotum_used(user_idnr, mailbox_size) < 0) { + trace(TRACE_ERROR, + "%s,%s: error decreasing curmail_size", + __FILE__, __FUNCTION__); + return -1; + } + } + return 0; } int db_send_message_lines(void *fstream, u64_t message_idnr, long lines, int no_end_dot) { - u64_t physmessage_id = 0; - char *buffer = NULL; - int buffer_pos; - char *nextpos, *tmppos = NULL; - int block_count; - u64_t rowlength; - int n; - char *query_result; - - trace(TRACE_DEBUG, "%s,%s: request for [%ld] lines", - __FILE__, __FUNCTION__, lines); - - /* first find the physmessage_id */ - snprintf(query, DEF_QUERYSIZE, - "SELECT physmessage_id FROM messages " - "WHERE message_idnr = '%llu'", message_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error executing query", - __FILE__, __FUNCTION__); - return 0; - } - physmessage_id = db_get_result_u64(0, 0); - db_free_result(); + u64_t physmessage_id = 0; + char *buffer = NULL; + int buffer_pos; + char *nextpos, *tmppos = NULL; + int block_count; + u64_t rowlength; + int n; + char *query_result; - memtst((buffer = (char *) my_malloc(WRITE_BUFFER_SIZE * 2)) == NULL); + trace(TRACE_DEBUG, "%s,%s: request for [%ld] lines", + __FILE__, __FUNCTION__, lines); - snprintf(query, DEF_QUERYSIZE, - "SELECT messageblk FROM messageblks " - "WHERE physmessage_id='%llu' " - "ORDER BY messageblk_idnr ASC", physmessage_id); - trace(TRACE_DEBUG, "%s,%s: executing query [%s]", - __FILE__, __FUNCTION__, query); + /* first find the physmessage_id */ + snprintf(query, DEF_QUERYSIZE, + "SELECT physmessage_id FROM messages " + "WHERE message_idnr = '%llu'", message_idnr); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error executing query", + __FILE__, __FUNCTION__); + return 0; + } + physmessage_id = db_get_result_u64(0, 0); + db_free_result(); - if (db_query(query) == -1) { - my_free(buffer); - return 0; - } - - trace(TRACE_DEBUG, "%s,%s: sending [%ld] lines from message [%llu]", - __FILE__, __FUNCTION__, lines, message_idnr); - - block_count = 0; - n = db_num_rows(); - /* loop over all rows in the result set, until the right amount of - * lines has been read - */ - while ((block_count < n) - && ((lines > 0) || (lines == -2) || (block_count == 0))) { - query_result = db_get_result(block_count, 0); - nextpos = query_result; - rowlength = (u64_t) db_get_length(block_count, 0); - - /* reset our buffer */ - memset(buffer, '\0', (WRITE_BUFFER_SIZE) * 2); - buffer_pos = 0; - - while ((*nextpos != '\0') && (rowlength > 0) + memtst((buffer = + (char *) my_malloc(WRITE_BUFFER_SIZE * 2)) == NULL); + + snprintf(query, DEF_QUERYSIZE, + "SELECT messageblk FROM messageblks " + "WHERE physmessage_id='%llu' " + "ORDER BY messageblk_idnr ASC", physmessage_id); + trace(TRACE_DEBUG, "%s,%s: executing query [%s]", + __FILE__, __FUNCTION__, query); + + if (db_query(query) == -1) { + my_free(buffer); + return 0; + } + + trace(TRACE_DEBUG, + "%s,%s: sending [%ld] lines from message [%llu]", __FILE__, + __FUNCTION__, lines, message_idnr); + + block_count = 0; + n = db_num_rows(); + /* loop over all rows in the result set, until the right amount of + * lines has been read + */ + while ((block_count < n) && ((lines > 0) || (lines == -2) || (block_count == 0))) { - - if (*nextpos == '\n') { - /* first block is always the full header - so this should not be counted when parsing - if lines == -2 none of the lines should be counted - since the whole message is requested */ - if ((lines != -2) && (block_count != 0)) - lines--; - - if (tmppos != NULL) { - if (*tmppos == '\r') { - buffer[buffer_pos++] = *nextpos; - } else { - buffer[buffer_pos++] = '\r'; - buffer[buffer_pos++] = *nextpos; - } - } else { - buffer[buffer_pos++] = '\r'; - buffer[buffer_pos++] = *nextpos; - } - } else { - if (*nextpos == '.') { - if (tmppos != NULL) { - if (*tmppos == '\n') { - buffer[buffer_pos++] = '.'; - buffer[buffer_pos++] = *nextpos; - } else { - buffer[buffer_pos++] = *nextpos; - } - } else { - buffer[buffer_pos++] = *nextpos; - } - } else { - buffer[buffer_pos++] = *nextpos; + query_result = db_get_result(block_count, 0); + nextpos = query_result; + rowlength = (u64_t) db_get_length(block_count, 0); + + /* reset our buffer */ + memset(buffer, '\0', (WRITE_BUFFER_SIZE) * 2); + buffer_pos = 0; + + while ((*nextpos != '\0') && (rowlength > 0) + && ((lines > 0) || (lines == -2) + || (block_count == 0))) { + + if (*nextpos == '\n') { + /* first block is always the full header + so this should not be counted when parsing + if lines == -2 none of the lines should be counted + since the whole message is requested */ + if ((lines != -2) && (block_count != 0)) + lines--; + + if (tmppos != NULL) { + if (*tmppos == '\r') { + buffer[buffer_pos++] = + *nextpos; + } else { + buffer[buffer_pos++] = + '\r'; + buffer[buffer_pos++] = + *nextpos; + } + } else { + buffer[buffer_pos++] = '\r'; + buffer[buffer_pos++] = *nextpos; + } + } else { + if (*nextpos == '.') { + if (tmppos != NULL) { + if (*tmppos == '\n') { + buffer + [buffer_pos++] + = '.'; + buffer + [buffer_pos++] + = *nextpos; + } else { + buffer + [buffer_pos++] + = *nextpos; + } + } else { + buffer[buffer_pos++] = + *nextpos; + } + } else { + buffer[buffer_pos++] = *nextpos; + } + } + + tmppos = nextpos; + + /* get the next character */ + nextpos++; + rowlength--; + + if (rowlength % WRITE_BUFFER_SIZE == 0) { + /* purge buffer at every WRITE_BUFFER_SIZE bytes */ + fwrite(buffer, sizeof(char), + strlen(buffer), (FILE *) fstream); + /* cleanup the buffer */ + memset(buffer, '\0', + (WRITE_BUFFER_SIZE * 2)); + buffer_pos = 0; + } } - } - - tmppos = nextpos; - - /* get the next character */ - nextpos++; - rowlength--; - - if (rowlength % WRITE_BUFFER_SIZE == 0) { - /* purge buffer at every WRITE_BUFFER_SIZE bytes */ - fwrite(buffer, sizeof(char), strlen(buffer), - (FILE *) fstream); - /* cleanup the buffer */ - memset(buffer, '\0', (WRITE_BUFFER_SIZE * 2)); - buffer_pos = 0; - } - } - /* next block in while loop */ - block_count++; - trace(TRACE_DEBUG, "%s,%s: getting nextblock [%d]\n", - __FILE__, __FUNCTION__, block_count); - /* flush our buffer */ - fwrite(buffer, sizeof(char), strlen(buffer), (FILE *) fstream); - } - /* delimiter */ - if (no_end_dot == 0) - fprintf((FILE *) fstream, "\r\n.\r\n"); - - db_free_result(); - my_free(buffer); - return 1; + /* next block in while loop */ + block_count++; + trace(TRACE_DEBUG, "%s,%s: getting nextblock [%d]\n", + __FILE__, __FUNCTION__, block_count); + /* flush our buffer */ + fwrite(buffer, sizeof(char), strlen(buffer), + (FILE *) fstream); + } + /* delimiter */ + if (no_end_dot == 0) + fprintf((FILE *) fstream, "\r\n.\r\n"); + + db_free_result(); + my_free(buffer); + return 1; } int db_createsession(u64_t user_idnr, PopSession_t * session_ptr) { - struct message tmpmessage; - int message_counter = 0; - unsigned i; - char *query_result; - u64_t inbox_mailbox_idnr; - - list_init(&session_ptr->messagelst); - - if (db_findmailbox("INBOX", user_idnr, &inbox_mailbox_idnr) <= 0) { - trace(TRACE_ERROR, "%s,%s: error finding mailbox_idnr of " - "INBOX for user [%llu], exiting..", - __FILE__, __FUNCTION__, user_idnr); - return -1; - } - /* query is <2 because we don't want deleted messages - * the unique_id should not be empty, this could mean that the message is still being delivered */ - snprintf(query, DEF_QUERYSIZE, - "SELECT pm.messagesize, msg.message_idnr, msg.status, " - "msg.unique_id FROM messages msg, physmessage pm " - "WHERE msg.mailbox_idnr = '%llu' " - "AND msg.status < 002 " - "AND msg.physmessage_id = pm.id " - "AND unique_id != '' order by status ASC", - inbox_mailbox_idnr); - - if (db_query(query) == -1) { - return -1; - } - - session_ptr->totalmessages = 0; - session_ptr->totalsize = 0; - - message_counter = db_num_rows(); - - if (message_counter < 1) { - /* there are no messages for this user */ + struct message tmpmessage; + int message_counter = 0; + unsigned i; + char *query_result; + u64_t inbox_mailbox_idnr; + + list_init(&session_ptr->messagelst); + + if (db_findmailbox("INBOX", user_idnr, &inbox_mailbox_idnr) <= 0) { + trace(TRACE_ERROR, "%s,%s: error finding mailbox_idnr of " + "INBOX for user [%llu], exiting..", + __FILE__, __FUNCTION__, user_idnr); + return -1; + } + /* query is <2 because we don't want deleted messages + * the unique_id should not be empty, this could mean that the message is still being delivered */ + snprintf(query, DEF_QUERYSIZE, + "SELECT pm.messagesize, msg.message_idnr, msg.status, " + "msg.unique_id FROM messages msg, physmessage pm " + "WHERE msg.mailbox_idnr = '%llu' " + "AND msg.status < 002 " + "AND msg.physmessage_id = pm.id " + "AND unique_id != '' order by status ASC", + inbox_mailbox_idnr); + + if (db_query(query) == -1) { + return -1; + } + + session_ptr->totalmessages = 0; + session_ptr->totalsize = 0; + + message_counter = db_num_rows(); + + if (message_counter < 1) { + /* there are no messages for this user */ + db_free_result(); + return 1; + } + + /* messagecounter is total message, +1 tot end at message 1 */ + message_counter++; + + /* filling the list */ + trace(TRACE_DEBUG, "%s,%s: adding items to list", __FILE__, + __FUNCTION__); + for (i = 0; i < db_num_rows(); i++) { + /* message size */ + tmpmessage.msize = db_get_result_u64(i, 0); + /* real message id */ + tmpmessage.realmessageid = db_get_result_u64(i, 1); + /* message status */ + tmpmessage.messagestatus = db_get_result_u64(i, 2); + /* virtual message status */ + tmpmessage.virtual_messagestatus = + tmpmessage.messagestatus; + /* unique id */ + query_result = db_get_result(i, 3); + if (query_result) + strncpy(tmpmessage.uidl, query_result, UID_SIZE); + + session_ptr->totalmessages++; + session_ptr->totalsize += tmpmessage.msize; + /* descending to create inverted list */ + message_counter--; + tmpmessage.messageid = (u64_t) message_counter; + list_nodeadd(&session_ptr->messagelst, &tmpmessage, + sizeof(tmpmessage)); + } + + trace(TRACE_DEBUG, "%s,%s: adding succesfull", __FILE__, + __FUNCTION__); + + /* setting all virtual values */ + session_ptr->virtual_totalmessages = session_ptr->totalmessages; + session_ptr->virtual_totalsize = session_ptr->totalsize; + db_free_result(); + return 1; - } - - /* messagecounter is total message, +1 tot end at message 1 */ - message_counter++; - - /* filling the list */ - trace(TRACE_DEBUG, "%s,%s: adding items to list", __FILE__, - __FUNCTION__); - for (i = 0; i < db_num_rows(); i++) { - /* message size */ - tmpmessage.msize = db_get_result_u64(i, 0); - /* real message id */ - tmpmessage.realmessageid = db_get_result_u64(i, 1); - /* message status */ - tmpmessage.messagestatus = db_get_result_u64(i, 2); - /* virtual message status */ - tmpmessage.virtual_messagestatus = tmpmessage.messagestatus; - /* unique id */ - query_result = db_get_result(i, 3); - if (query_result) - strncpy(tmpmessage.uidl, query_result, UID_SIZE); - - session_ptr->totalmessages++; - session_ptr->totalsize += tmpmessage.msize; - /* descending to create inverted list */ - message_counter--; - tmpmessage.messageid = (u64_t) message_counter; - list_nodeadd(&session_ptr->messagelst, &tmpmessage, - sizeof(tmpmessage)); - } - - trace(TRACE_DEBUG, "%s,%s: adding succesfull", __FILE__, __FUNCTION__); - - /* setting all virtual values */ - session_ptr->virtual_totalmessages = session_ptr->totalmessages; - session_ptr->virtual_totalsize = session_ptr->totalsize; - - db_free_result(); - - return 1; } void db_session_cleanup(PopSession_t * session_ptr) { - /* cleanups a session - removes a list and all references */ - session_ptr->totalsize = 0; - session_ptr->virtual_totalsize = 0; - session_ptr->totalmessages = 0; - session_ptr->virtual_totalmessages = 0; - list_freelist(&(session_ptr->messagelst.start)); + /* cleanups a session + removes a list and all references */ + session_ptr->totalsize = 0; + session_ptr->virtual_totalsize = 0; + session_ptr->totalmessages = 0; + session_ptr->virtual_totalmessages = 0; + list_freelist(&(session_ptr->messagelst.start)); } int db_update_pop(PopSession_t * session_ptr) { - struct element *tmpelement; - u64_t user_idnr = 0; - - /* get first element in list */ - tmpelement = list_getstart(&session_ptr->messagelst); - - while (tmpelement != NULL) { - /* check if they need an update in the database */ - if (((struct message *) tmpelement->data)->virtual_messagestatus != - ((struct message *) tmpelement->data)->messagestatus) { - /* use one message to get the user_idnr that goes with the - messages */ - if (user_idnr == 0) - user_idnr = - db_get_useridnr(((struct message *) tmpelement->data)-> - realmessageid); - - /* yes they need an update, do the query */ - snprintf(query, DEF_QUERYSIZE, - "UPDATE messages set status='%llu' WHERE " - "message_idnr='%llu' AND status<002", - ((struct message *) - tmpelement->data)->virtual_messagestatus, - ((struct message *) tmpelement->data)->realmessageid); - - /* FIXME: a message could be deleted already if it has been accessed - * by another interface and be deleted by sysop - * we need a check if the query failes because it doesn't exists anymore - * now it will just bailout */ - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", + struct element *tmpelement; + u64_t user_idnr = 0; + + /* get first element in list */ + tmpelement = list_getstart(&session_ptr->messagelst); + + while (tmpelement != NULL) { + /* check if they need an update in the database */ + if (((struct message *) tmpelement->data)-> + virtual_messagestatus != + ((struct message *) tmpelement->data)->messagestatus) { + /* use one message to get the user_idnr that goes with the + messages */ + if (user_idnr == 0) + user_idnr = + db_get_useridnr(((struct message *) + tmpelement->data)-> + realmessageid); + + /* yes they need an update, do the query */ + snprintf(query, DEF_QUERYSIZE, + "UPDATE messages set status='%llu' WHERE " + "message_idnr='%llu' AND status<002", + ((struct message *) + tmpelement->data)->virtual_messagestatus, + ((struct message *) tmpelement->data)-> + realmessageid); + + /* FIXME: a message could be deleted already if it has been accessed + * by another interface and be deleted by sysop + * we need a check if the query failes because it doesn't exists anymore + * now it will just bailout */ + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return -1; + } + } + tmpelement = tmpelement->nextnode; + } + + /* because the status of some messages might have changed (for instance + to status >= 002, the quotum has to be recalculated */ + if (db_calculate_quotum_used(user_idnr) == -1) { + trace(TRACE_ERROR, "%s,%s: error calculating quotum used", __FILE__, __FUNCTION__); return -1; - } - } - tmpelement = tmpelement->nextnode; - } - - /* because the status of some messages might have changed (for instance - to status >= 002, the quotum has to be recalculated */ - if (db_calculate_quotum_used(user_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: error calculating quotum used", - __FILE__, __FUNCTION__); - return -1; - } - return 0; -} - -int db_set_deleted(u64_t *affected_rows) -{ - assert(affected_rows != NULL); - *affected_rows = 0; - - snprintf(query, DEF_QUERYSIZE, - "UPDATE messages SET status = '003' WHERE status = '002'"); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: Could not execute query", - __FILE__, __FUNCTION__); - return -1; - } - *affected_rows = db_get_affected_rows(); - db_free_result(); - return 1; -} - -int db_deleted_purge(u64_t *affected_rows) -{ - unsigned i; - u64_t *message_idnrs; - - assert(affected_rows != NULL); - *affected_rows = 0; - - /* first we're deleting all the messageblks */ - snprintf(query, DEF_QUERYSIZE, - "SELECT message_idnr FROM messages WHERE status='003'"); - trace(TRACE_DEBUG, "%s,%s: executing query [%s]", - __FILE__, __FUNCTION__, query); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: Cound not fetch message ID numbers", - __FILE__, __FUNCTION__); - return -1; - } - - *affected_rows = db_num_rows(); - if (*affected_rows == 0) { - trace(TRACE_DEBUG, "%s,%s: no messages to purge", - __FILE__, __FUNCTION__); - db_free_result(); + } return 0; - } - if (!(message_idnrs = - (u64_t *) my_malloc(*affected_rows * sizeof(u64_t)))) { - trace(TRACE_ERROR, "%s,%s: error allocating memory", - __FILE__, __FUNCTION__); - return -2; - } - - /* delete each message */ - for (i = 0; i < *affected_rows; i++) - message_idnrs[i] = db_get_result_u64(i, 0); - db_free_result(); - for (i = 0; i < *affected_rows; i++) { - if (db_delete_message(message_idnrs[i]) == -1) { - trace(TRACE_ERROR, "%s,%s: error deleting message", - __FILE__, __FUNCTION__); - my_free(message_idnrs); - return -1; - } - } - my_free(message_idnrs); - return 1; +} + +int db_set_deleted(u64_t * affected_rows) +{ + assert(affected_rows != NULL); + *affected_rows = 0; + + snprintf(query, DEF_QUERYSIZE, + "UPDATE messages SET status = '003' WHERE status = '002'"); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: Could not execute query", + __FILE__, __FUNCTION__); + return -1; + } + *affected_rows = db_get_affected_rows(); + db_free_result(); + return 1; +} + +int db_deleted_purge(u64_t * affected_rows) +{ + unsigned i; + u64_t *message_idnrs; + + assert(affected_rows != NULL); + *affected_rows = 0; + + /* first we're deleting all the messageblks */ + snprintf(query, DEF_QUERYSIZE, + "SELECT message_idnr FROM messages WHERE status='003'"); + trace(TRACE_DEBUG, "%s,%s: executing query [%s]", + __FILE__, __FUNCTION__, query); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: Cound not fetch message ID numbers", + __FILE__, __FUNCTION__); + return -1; + } + + *affected_rows = db_num_rows(); + if (*affected_rows == 0) { + trace(TRACE_DEBUG, "%s,%s: no messages to purge", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + if (!(message_idnrs = + (u64_t *) my_malloc(*affected_rows * sizeof(u64_t)))) { + trace(TRACE_ERROR, "%s,%s: error allocating memory", + __FILE__, __FUNCTION__); + return -2; + } + + /* delete each message */ + for (i = 0; i < *affected_rows; i++) + message_idnrs[i] = db_get_result_u64(i, 0); + db_free_result(); + for (i = 0; i < *affected_rows; i++) { + if (db_delete_message(message_idnrs[i]) == -1) { + trace(TRACE_ERROR, "%s,%s: error deleting message", + __FILE__, __FUNCTION__); + my_free(message_idnrs); + return -1; + } + } + my_free(message_idnrs); + return 1; } int db_imap_append_msg(const char *msgdata, u64_t datalen, u64_t mailbox_idnr, u64_t user_idnr, - timestring_t internal_date, u64_t *msg_idnr) + timestring_t internal_date, u64_t * msg_idnr) { - u64_t message_idnr; - u64_t messageblk_idnr; - u64_t physmessage_id = 0; - u64_t count; - char unique_id[UID_SIZE]; /* unique id */ + u64_t message_idnr; + u64_t messageblk_idnr; + u64_t physmessage_id = 0; + u64_t count; + char unique_id[UID_SIZE]; /* unique id */ - switch (db_check_quotum_used(user_idnr, datalen)) { + switch (db_check_quotum_used(user_idnr, datalen)) { case -1: trace(TRACE_ERROR, "%s,%s: error checking quotum", - __FILE__, __FUNCTION__); + __FILE__, __FUNCTION__); return -1; case 1: trace(TRACE_INFO, "%s,%s: user [%llu] would exceed quotum", - __FILE__, __FUNCTION__, user_idnr); + __FILE__, __FUNCTION__, user_idnr); return 2; - } - - if (strlen(internal_date) > 0) { - if (db_insert_physmessage_with_internal_date(internal_date, - &physmessage_id) < 0){ - trace(TRACE_ERROR, "%s,%s: could not create physmessage " - "with internal date [%s]", - __FILE__, __FUNCTION__, internal_date); - return -1; - } - } else { - if (db_insert_physmessage(&physmessage_id) < 0) { - trace(TRACE_ERROR, "%s,%s: could not create physmessage", - __FILE__, __FUNCTION__); - return -1; - } - } - - /* create a msg - * according to the rfc, the recent flag has to be set to '1'. - * this also means that the status will be set to '001' - */ - snprintf(query, DEF_QUERYSIZE, - "INSERT INTO messages " - "(mailbox_idnr, physmessage_id, unique_id, status," - "recent_flag) VALUES ('%llu', '%llu', '', '001', '1')", - mailbox_idnr, physmessage_id); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not create message", - __FILE__, __FUNCTION__); - return -1; - } - - /* fetch the id of the new message */ - message_idnr = db_insert_result("message_idnr"); - - /* ok insert blocks */ - /* first the header: scan until double newline */ - for (count = 1; count < datalen; count++) - if (msgdata[count - 1] == '\n' && msgdata[count] == '\n') - break; - - if (count == datalen) { - trace(TRACE_INFO, "%s,%s: no double newline found [invalid msg]\n", - __FILE__, __FUNCTION__); - if (db_delete_message(message_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not delete invalid message" - "%llu. Database could be invalid now..", - __FILE__, __FUNCTION__, message_idnr); - } - db_free_result(); - return 1; - } - - if (count == datalen - 1) { - /* msg consists of a single header */ - trace(TRACE_INFO, "%s,%s: msg only contains a header", - __FILE__, __FUNCTION__); - - if (db_insert_message_block(msgdata, datalen, - message_idnr, &messageblk_idnr) == -1 - || db_insert_message_block(" \n", 2, message_idnr, - &messageblk_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not insert msg block\n", - __FILE__, __FUNCTION__); - if (db_delete_message(message_idnr) == -1) { - trace(TRACE_ERROR, - "%s,%s: could not delete invalid message" - "%llu. Database could be invalid now..", __FILE__, - __FUNCTION__, message_idnr); - } - return -1; - } - } else { - /* - * output header: - * the first count bytes is the header + } + + if (strlen(internal_date) > 0) { + if (db_insert_physmessage_with_internal_date(internal_date, + &physmessage_id) + < 0) { + trace(TRACE_ERROR, + "%s,%s: could not create physmessage " + "with internal date [%s]", __FILE__, + __FUNCTION__, internal_date); + return -1; + } + } else { + if (db_insert_physmessage(&physmessage_id) < 0) { + trace(TRACE_ERROR, + "%s,%s: could not create physmessage", + __FILE__, __FUNCTION__); + return -1; + } + } + + /* create a msg + * according to the rfc, the recent flag has to be set to '1'. + * this also means that the status will be set to '001' */ - count++; + snprintf(query, DEF_QUERYSIZE, + "INSERT INTO messages " + "(mailbox_idnr, physmessage_id, unique_id, status," + "recent_flag) VALUES ('%llu', '%llu', '', '001', '1')", + mailbox_idnr, physmessage_id); - if (db_insert_message_block(msgdata, count, message_idnr, - &messageblk_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not insert msg block\n", - __FILE__, __FUNCTION__); - if (db_delete_message(message_idnr) == -1) { - trace(TRACE_ERROR, - "%s,%s: could not delete invalid message" - "%llu. Database could be invalid now..", - __FILE__, __FUNCTION__, message_idnr); - } - return -1; + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not create message", + __FILE__, __FUNCTION__); + return -1; } - /* output message */ - while ((datalen - count) > READ_BLOCK_SIZE) { - if (db_insert_message_block(&msgdata[count], - READ_BLOCK_SIZE, - message_idnr, - &messageblk_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not insert msg block", + /* fetch the id of the new message */ + message_idnr = db_insert_result("message_idnr"); + + /* ok insert blocks */ + /* first the header: scan until double newline */ + for (count = 1; count < datalen; count++) + if (msgdata[count - 1] == '\n' && msgdata[count] == '\n') + break; + + if (count == datalen) { + trace(TRACE_INFO, + "%s,%s: no double newline found [invalid msg]\n", __FILE__, __FUNCTION__); if (db_delete_message(message_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not delete invalid " - "message %llu. Database could be invalid now..", - __FILE__, __FUNCTION__, message_idnr); + trace(TRACE_ERROR, + "%s,%s: could not delete invalid message" + "%llu. Database could be invalid now..", + __FILE__, __FUNCTION__, message_idnr); } - return -1; - } - count += READ_BLOCK_SIZE; + db_free_result(); + return 1; } + if (count == datalen - 1) { + /* msg consists of a single header */ + trace(TRACE_INFO, "%s,%s: msg only contains a header", + __FILE__, __FUNCTION__); + + if (db_insert_message_block(msgdata, datalen, + message_idnr, + &messageblk_idnr) == -1 + || db_insert_message_block(" \n", 2, message_idnr, + &messageblk_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not insert msg block\n", + __FILE__, __FUNCTION__); + if (db_delete_message(message_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not delete invalid message" + "%llu. Database could be invalid now..", + __FILE__, __FUNCTION__, + message_idnr); + } + return -1; + } + } else { + /* + * output header: + * the first count bytes is the header + */ + count++; + + if (db_insert_message_block(msgdata, count, message_idnr, + &messageblk_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not insert msg block\n", + __FILE__, __FUNCTION__); + if (db_delete_message(message_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not delete invalid message" + "%llu. Database could be invalid now..", + __FILE__, __FUNCTION__, + message_idnr); + } + return -1; + } + + /* output message */ + while ((datalen - count) > READ_BLOCK_SIZE) { + if (db_insert_message_block(&msgdata[count], + READ_BLOCK_SIZE, + message_idnr, + &messageblk_idnr) == + -1) { + trace(TRACE_ERROR, + "%s,%s: could not insert msg block", + __FILE__, __FUNCTION__); + if (db_delete_message(message_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not delete invalid " + "message %llu. Database could be invalid now..", + __FILE__, __FUNCTION__, + message_idnr); + } + return -1; + } + count += READ_BLOCK_SIZE; + } + + + if (db_insert_message_block(&msgdata[count], + datalen - count, message_idnr, + &messageblk_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not insert msg block\n", + __FILE__, __FUNCTION__); + if (db_delete_message(message_idnr) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not delete invalid " + "message %llu. Database could be invalid now..", + __FILE__, __FUNCTION__, + message_idnr); + } + return -1; + } - if (db_insert_message_block(&msgdata[count], - datalen - count, message_idnr, - &messageblk_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not insert msg block\n", - __FILE__, __FUNCTION__); - if (db_delete_message(message_idnr) == -1) { - trace(TRACE_ERROR, "%s,%s: could not delete invalid " - "message %llu. Database could be invalid now..", - __FILE__, __FUNCTION__, message_idnr); - } - return -1; } - } + /* create a unique id */ + create_unique_id(unique_id, message_idnr); + //snprintf(unique_id, UID_SIZE, "%lluA%lu", message_idnr, td); - /* create a unique id */ - create_unique_id(unique_id, message_idnr); - //snprintf(unique_id, UID_SIZE, "%lluA%lu", message_idnr, td); + /* set info on message */ + db_update_message(message_idnr, unique_id, datalen, 0); - /* set info on message */ - db_update_message(message_idnr, unique_id, datalen, 0); + /* recalculate quotum used */ + db_add_quotum_used(user_idnr, datalen); - /* recalculate quotum used */ - db_add_quotum_used(user_idnr, datalen); - - *msg_idnr = message_idnr; - return 0; + *msg_idnr = message_idnr; + return 0; } -int db_findmailbox(const char *fq_name, u64_t user_idnr, u64_t *mailbox_idnr) +int db_findmailbox(const char *fq_name, u64_t user_idnr, + u64_t * mailbox_idnr) { - char *username = NULL; + char *username = NULL; char *mailbox_name; char *name_str_copy; char *tempstr; @@ -1955,7 +2065,7 @@ int db_findmailbox(const char *fq_name, u64_t user_idnr, u64_t *mailbox_idnr) } } if (username) { - trace(TRACE_DEBUG,"%s,%s: finding user with name [%s].", + trace(TRACE_DEBUG, "%s,%s: finding user with name [%s].", __FILE__, __FUNCTION__, username); result = auth_user_exists(username, &owner_idnr); if (result < 0) { @@ -1969,9 +2079,11 @@ int db_findmailbox(const char *fq_name, u64_t user_idnr, u64_t *mailbox_idnr) return 0; } } - result = db_findmailbox_owner(mailbox_name, owner_idnr, mailbox_idnr); + result = + db_findmailbox_owner(mailbox_name, owner_idnr, mailbox_idnr); if (result < 0) { - trace(TRACE_ERROR, "%s,%s: error finding mailbox [%s] with " + trace(TRACE_ERROR, + "%s,%s: error finding mailbox [%s] with " "owner [%s, %llu]", __FILE__, __FUNCTION__, mailbox_name, username, owner_idnr); return -1; @@ -1981,124 +2093,133 @@ int db_findmailbox(const char *fq_name, u64_t user_idnr, u64_t *mailbox_idnr) } -int db_findmailbox_owner(const char *name, u64_t owner_idnr, - u64_t *mailbox_idnr) +int db_findmailbox_owner(const char *name, u64_t owner_idnr, + u64_t * mailbox_idnr) { - assert(mailbox_idnr != NULL); - *mailbox_idnr = 0; + assert(mailbox_idnr != NULL); + *mailbox_idnr = 0; - /* if we check the INBOX, we need to be case insensitive */ - if (strncasecmp(name,"INBOX", 5) == 0) { - snprintf(query, DEF_QUERYSIZE, - "SELECT mailbox_idnr FROM mailboxes " - "WHERE LOWER(name) = LOWER('%s') " - "AND owner_idnr='%llu'", name, owner_idnr); - } else { - snprintf(query, DEF_QUERYSIZE, - "SELECT mailbox_idnr FROM mailboxes " - "WHERE name='%s' AND owner_idnr='%llu'", name, owner_idnr); - } - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not select mailbox '%s'\n", - __FILE__, __FUNCTION__, name); + /* if we check the INBOX, we need to be case insensitive */ + if (strncasecmp(name, "INBOX", 5) == 0) { + snprintf(query, DEF_QUERYSIZE, + "SELECT mailbox_idnr FROM mailboxes " + "WHERE LOWER(name) = LOWER('%s') " + "AND owner_idnr='%llu'", name, owner_idnr); + } else { + snprintf(query, DEF_QUERYSIZE, + "SELECT mailbox_idnr FROM mailboxes " + "WHERE name='%s' AND owner_idnr='%llu'", name, + owner_idnr); + } + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not select mailbox '%s'\n", __FILE__, + __FUNCTION__, name); + db_free_result(); + return -1; + } + + if (db_num_rows() < 1) { + db_free_result(); + return 0; + } else { + *mailbox_idnr = db_get_result_u64(0, 0); + db_free_result(); + } + + if (*mailbox_idnr == 0) + return 0; + return 1; +} + +int db_list_mailboxes_by_regex(u64_t user_idnr, int only_subscribed, + regex_t * preg, + u64_t ** mailboxes, + unsigned int *nr_mailboxes) +{ + unsigned int i; + u64_t *tmp_mailboxes; + char *result_string; + u64_t owner_idnr; + char *mailbox_name; + + assert(mailboxes != NULL); + assert(nr_mailboxes != NULL); + + *mailboxes = NULL; + *nr_mailboxes = 0; + if (only_subscribed) + snprintf(query, DEF_QUERYSIZE, + "SELECT mbx.name, mbx.mailbox_idnr, mbx.owner_idnr " + "FROM mailboxes mbx " + "LEFT JOIN acl " + "ON acl.mailbox_id = mbx.mailbox_idnr " + "JOIN subscription sub ON sub.user_id = '%llu' " + "AND sub.mailbox_id = mbx.mailbox_idnr " + "WHERE mbx.owner_idnr = '%llu' " + "OR (acl.user_id = '%llu' AND acl.lookup_flag = '1') " + "GROUP BY mbx.name, mbx.mailbox_idnr, mbx.owner_idnr", + user_idnr, user_idnr, user_idnr); + else + snprintf(query, DEF_QUERYSIZE, + "SELECT mbx.name, mbx.mailbox_idnr, mbx.owner_idnr " + "FROM mailboxes mbx " + "LEFT JOIN acl " + "ON mbx.mailbox_idnr = acl.mailbox_id " + "WHERE (acl.user_id = '%llu' AND acl.lookup_flag = '1') " + "OR mbx.owner_idnr = '%llu'", user_idnr, + user_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: error during mailbox query", + __FILE__, __FUNCTION__); + return (-1); + } + if (db_num_rows() == 0) { + /* none exist, none matched */ + db_free_result(); + return 0; + } + tmp_mailboxes = (u64_t *) my_malloc(db_num_rows() * sizeof(u64_t)); + if (!tmp_mailboxes) { + trace(TRACE_ERROR, "%s,%s: not enough memory\n", + __FILE__, __FUNCTION__); + return (-2); + } + + for (i = 0; i < (unsigned) db_num_rows(); i++) { + result_string = db_get_result(i, 0); + owner_idnr = db_get_result_u64(i, 2); + /* add possible namespace prefix to mailbox_name */ + mailbox_name = + mailbox_add_namespace(result_string, owner_idnr, + user_idnr); + if (mailbox_name) { + trace(TRACE_DEBUG, + "%s,%s: comparing mailbox [%s] to " + "regular expression", __FILE__, __FUNCTION__, + mailbox_name); + if (regexec(preg, mailbox_name, 0, NULL, 0) == 0) { + tmp_mailboxes[*nr_mailboxes] = + db_get_result_u64(i, 1); + (*nr_mailboxes)++; + trace(TRACE_DEBUG, "%s,%s: regex match %s", + __FILE__, __FUNCTION__, + mailbox_name); + } + my_free(mailbox_name); + } + } db_free_result(); - return -1; - } - - if (db_num_rows() < 1) { - db_free_result(); - return 0; - } else { - *mailbox_idnr = db_get_result_u64(0, 0); - db_free_result(); - } - - if (*mailbox_idnr == 0) - return 0; - return 1; -} - -int db_list_mailboxes_by_regex(u64_t user_idnr, int only_subscribed, - regex_t *preg, - u64_t **mailboxes, unsigned int *nr_mailboxes) -{ - unsigned int i; - u64_t *tmp_mailboxes; - char *result_string; - u64_t owner_idnr; - char *mailbox_name; - - assert(mailboxes != NULL); - assert(nr_mailboxes != NULL); - - *mailboxes = NULL; - *nr_mailboxes = 0; - if (only_subscribed) - snprintf(query, DEF_QUERYSIZE, - "SELECT mbx.name, mbx.mailbox_idnr, mbx.owner_idnr " - "FROM mailboxes mbx " - "LEFT JOIN acl " - "ON acl.mailbox_id = mbx.mailbox_idnr " - "JOIN subscription sub ON sub.user_id = '%llu' " - "AND sub.mailbox_id = mbx.mailbox_idnr " - "WHERE mbx.owner_idnr = '%llu' " - "OR (acl.user_id = '%llu' AND acl.lookup_flag = '1') " - "GROUP BY mbx.name, mbx.mailbox_idnr, mbx.owner_idnr", - user_idnr, user_idnr, user_idnr); - else - snprintf(query, DEF_QUERYSIZE, - "SELECT mbx.name, mbx.mailbox_idnr, mbx.owner_idnr " - "FROM mailboxes mbx " - "LEFT JOIN acl " - "ON mbx.mailbox_idnr = acl.mailbox_id " - "WHERE (acl.user_id = '%llu' AND acl.lookup_flag = '1') " - "OR mbx.owner_idnr = '%llu'", user_idnr, user_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: error during mailbox query", - __FILE__, __FUNCTION__); - return (-1); - } - if (db_num_rows() == 0) { - /* none exist, none matched */ - db_free_result(); - return 0; - } - tmp_mailboxes = (u64_t*)my_malloc(db_num_rows() * sizeof(u64_t)); - if (!tmp_mailboxes) { - trace(TRACE_ERROR, "%s,%s: not enough memory\n", - __FILE__, __FUNCTION__); - return (-2); - } - - for (i = 0; i < (unsigned) db_num_rows(); i++) { - result_string = db_get_result(i, 0); - owner_idnr = db_get_result_u64(i, 2); - /* add possible namespace prefix to mailbox_name */ - mailbox_name = mailbox_add_namespace(result_string, owner_idnr, - user_idnr); - if (mailbox_name) { - trace(TRACE_DEBUG, "%s,%s: comparing mailbox [%s] to " - "regular expression", __FILE__, __FUNCTION__, mailbox_name); - if (regexec(preg, mailbox_name, 0, NULL, 0) == 0) { - tmp_mailboxes[*nr_mailboxes] = db_get_result_u64(i, 1); - (*nr_mailboxes)++; - trace(TRACE_DEBUG, "%s,%s: regex match %s", - __FILE__, __FUNCTION__, mailbox_name); - } - my_free(mailbox_name); - } - } - db_free_result(); - if (*nr_mailboxes == 0) { - /* none exist, none matched */ - my_free(tmp_mailboxes); - return 0; - } - - *mailboxes = tmp_mailboxes; - - return 1; + if (*nr_mailboxes == 0) { + /* none exist, none matched */ + my_free(tmp_mailboxes); + return 0; + } + + *mailboxes = tmp_mailboxes; + + return 1; } int db_findmailbox_by_regex(u64_t owner_idnr, const char *pattern, @@ -2107,162 +2228,166 @@ int db_findmailbox_by_regex(u64_t owner_idnr, const char *pattern, { int result; regex_t preg; - + *children = NULL; if ((result = regcomp(&preg, pattern, REG_ICASE | REG_NOSUB)) != 0) { - trace(TRACE_ERROR, "%s,%s: error compiling regex pattern: %d\n", + trace(TRACE_ERROR, + "%s,%s: error compiling regex pattern: %d\n", __FILE__, __FUNCTION__, result); return 1; } - + /* list normal mailboxes */ - if (db_list_mailboxes_by_regex(owner_idnr, only_subscribed, &preg, + if (db_list_mailboxes_by_regex(owner_idnr, only_subscribed, &preg, children, nchildren) < 0) { - trace(TRACE_ERROR, "%s,%s: error listing mailboxes", + trace(TRACE_ERROR, "%s,%s: error listing mailboxes", __FILE__, __FUNCTION__); return -1; } if (nchildren == 0) { - trace(TRACE_INFO, "%s, %s: did not find any mailboxes that " - "match pattern. returning 0, nchildren = 0", + trace(TRACE_INFO, + "%s, %s: did not find any mailboxes that " + "match pattern. returning 0, nchildren = 0", __FILE__, __FUNCTION__); return 0; } - + /* store matches */ - trace(TRACE_INFO,"%s,%s: found [%d] mailboxes", __FILE__, __FUNCTION__, - *nchildren); + trace(TRACE_INFO, "%s,%s: found [%d] mailboxes", __FILE__, + __FUNCTION__, *nchildren); return 0; } int db_getmailbox(mailbox_t * mb) { - u64_t highest_id; - unsigned i; - - /* free existing MSN list */ - if (mb->seq_list) { - my_free(mb->seq_list); - mb->seq_list = NULL; - } - - mb->flags = 0; - mb->exists = 0; - mb->unseen = 0; - mb->recent = 0; - mb->msguidnext = 0; - - /* select mailbox */ - snprintf(query, DEF_QUERYSIZE, - "SELECT permission," - "seen_flag," - "answered_flag," - "deleted_flag," - "flagged_flag," - "recent_flag," - "draft_flag " - "FROM mailboxes WHERE mailbox_idnr = '%llu'", - mb->uid); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not select mailbox\n", - __FILE__, __FUNCTION__); - return -1; - } - - - if (db_num_rows() == 0) { - trace(TRACE_ERROR, "%s,%s: invalid mailbox id specified", - __FILE__, __FUNCTION__); - db_free_result(); - return -1; - } - - mb->permission = db_get_result_int(0, 0); - - if (db_get_result(0, 1)) - mb->flags |= IMAPFLAG_SEEN; - if (db_get_result(0, 2)) - mb->flags |= IMAPFLAG_ANSWERED; - if (db_get_result(0, 3)) - mb->flags |= IMAPFLAG_DELETED; - if (db_get_result(0, 4)) - mb->flags |= IMAPFLAG_FLAGGED; - if (db_get_result(0, 5)) - mb->flags |= IMAPFLAG_RECENT; - if (db_get_result(0, 6)) - mb->flags |= IMAPFLAG_DRAFT; - - db_free_result(); - - /* select messages */ - snprintf(query, DEF_QUERYSIZE, - "SELECT message_idnr, seen_flag, recent_flag " - "FROM messages WHERE mailbox_idnr = '%llu' " - "AND status < 2 AND unique_id != '' " - "ORDER BY message_idnr ASC", mb->uid); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not retrieve messages", - __FILE__, __FUNCTION__); - return -1; - } - - mb->exists = db_num_rows(); - - /* alloc mem */ - mb->seq_list = (u64_t *) my_malloc(sizeof(u64_t) * mb->exists); - if (!mb->seq_list) { - /* out of mem */ + u64_t highest_id; + unsigned i; + + /* free existing MSN list */ + if (mb->seq_list) { + my_free(mb->seq_list); + mb->seq_list = NULL; + } + + mb->flags = 0; + mb->exists = 0; + mb->unseen = 0; + mb->recent = 0; + mb->msguidnext = 0; + + /* select mailbox */ + snprintf(query, DEF_QUERYSIZE, + "SELECT permission," + "seen_flag," + "answered_flag," + "deleted_flag," + "flagged_flag," + "recent_flag," + "draft_flag " + "FROM mailboxes WHERE mailbox_idnr = '%llu'", mb->uid); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not select mailbox\n", + __FILE__, __FUNCTION__); + return -1; + } + + + if (db_num_rows() == 0) { + trace(TRACE_ERROR, "%s,%s: invalid mailbox id specified", + __FILE__, __FUNCTION__); + db_free_result(); + return -1; + } + + mb->permission = db_get_result_int(0, 0); + + if (db_get_result(0, 1)) + mb->flags |= IMAPFLAG_SEEN; + if (db_get_result(0, 2)) + mb->flags |= IMAPFLAG_ANSWERED; + if (db_get_result(0, 3)) + mb->flags |= IMAPFLAG_DELETED; + if (db_get_result(0, 4)) + mb->flags |= IMAPFLAG_FLAGGED; + if (db_get_result(0, 5)) + mb->flags |= IMAPFLAG_RECENT; + if (db_get_result(0, 6)) + mb->flags |= IMAPFLAG_DRAFT; + db_free_result(); - return -1; - } - for (i = 0; i < db_num_rows(); i++) { - if (db_get_result(i, 1)[0] == '0') - mb->unseen++; - if (db_get_result(i, 2)[0] == '1') - mb->recent++; + /* select messages */ + snprintf(query, DEF_QUERYSIZE, + "SELECT message_idnr, seen_flag, recent_flag " + "FROM messages WHERE mailbox_idnr = '%llu' " + "AND status < 2 AND unique_id != '' " + "ORDER BY message_idnr ASC", mb->uid); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not retrieve messages", + __FILE__, __FUNCTION__); + return -1; + } + + mb->exists = db_num_rows(); + + /* alloc mem */ + mb->seq_list = (u64_t *) my_malloc(sizeof(u64_t) * mb->exists); + if (!mb->seq_list) { + /* out of mem */ + db_free_result(); + return -1; + } - mb->seq_list[i] = db_get_result_u64(i, 0); - } + for (i = 0; i < db_num_rows(); i++) { + if (db_get_result(i, 1)[0] == '0') + mb->unseen++; + if (db_get_result(i, 2)[0] == '1') + mb->recent++; - db_free_result(); + mb->seq_list[i] = db_get_result_u64(i, 0); + } - /* now determine the next message UID - * NOTE expunged messages are selected as well in order to be - * able to restore them - */ - snprintf(query, DEF_QUERYSIZE, - "SELECT MAX(message_idnr) FROM messages " - "WHERE unique_id != ''"); + db_free_result(); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not determine highest message ID", - __FILE__, __FUNCTION__); - my_free(mb->seq_list); - mb->seq_list = NULL; - return -1; - } + /* now determine the next message UID + * NOTE expunged messages are selected as well in order to be + * able to restore them + */ + snprintf(query, DEF_QUERYSIZE, + "SELECT MAX(message_idnr) FROM messages " + "WHERE unique_id != ''"); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not determine highest message ID", + __FILE__, __FUNCTION__); + my_free(mb->seq_list); + mb->seq_list = NULL; + return -1; + } - highest_id = db_get_result_u64(0, 0); - mb->msguidnext = highest_id + 1; - db_free_result(); + highest_id = db_get_result_u64(0, 0); + mb->msguidnext = highest_id + 1; + db_free_result(); - return 0; + return 0; } -int db_createmailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr) +int db_createmailbox(const char *name, u64_t owner_idnr, + u64_t * mailbox_idnr) { const char *simple_name; assert(mailbox_idnr != NULL); *mailbox_idnr = 0; /* remove namespace information from mailbox name */ if (!(simple_name = mailbox_remove_namespace(name))) { - trace(TRACE_ERROR, "%s,%s: could not create simple mailbox name " + trace(TRACE_ERROR, + "%s,%s: could not create simple mailbox name " "from full name", __FILE__, __FUNCTION__); return -1; } @@ -2270,9 +2395,9 @@ int db_createmailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr) "INSERT INTO mailboxes (name, owner_idnr," "seen_flag, answered_flag, deleted_flag, flagged_flag, " "recent_flag, draft_flag, permission)" - " VALUES ('%s', '%llu', 1, 1, 1, 1, 1, 1, 2)", simple_name, - owner_idnr); - + " VALUES ('%s', '%llu', 1, 1, 1, 1, 1, 1, 2)", + simple_name, owner_idnr); + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not create mailbox", __FILE__, __FUNCTION__); @@ -2283,195 +2408,201 @@ int db_createmailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr) } -int db_find_create_mailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr) +int db_find_create_mailbox(const char *name, u64_t owner_idnr, + u64_t * mailbox_idnr) { - u64_t mboxidnr; + u64_t mboxidnr; - assert(mailbox_idnr != NULL); - *mailbox_idnr = 0; + assert(mailbox_idnr != NULL); + *mailbox_idnr = 0; - /* Did we fail to find the mailbox? */ - if (db_findmailbox_owner(name, owner_idnr, &mboxidnr) != 1) - { - /* Did we fail to create the mailbox? */ - if (db_createmailbox(name, owner_idnr, &mboxidnr) != 0) - { - /* Serious failure situation! */ - trace(TRACE_ERROR, "%s, %s: seriously could not create mailbox [%s]", - __FILE__, __FUNCTION__, name); - return -1; - } - trace(TRACE_DEBUG, "%s, %s: mailbox [%s] created on the fly", - __FILE__, __FUNCTION__, name); - } - trace(TRACE_DEBUG, "%s, %s: mailbox [%s] found", - __FILE__, __FUNCTION__, name); + /* Did we fail to find the mailbox? */ + if (db_findmailbox_owner(name, owner_idnr, &mboxidnr) != 1) { + /* Did we fail to create the mailbox? */ + if (db_createmailbox(name, owner_idnr, &mboxidnr) != 0) { + /* Serious failure situation! */ + trace(TRACE_ERROR, + "%s, %s: seriously could not create mailbox [%s]", + __FILE__, __FUNCTION__, name); + return -1; + } + trace(TRACE_DEBUG, + "%s, %s: mailbox [%s] created on the fly", __FILE__, + __FUNCTION__, name); + } + trace(TRACE_DEBUG, "%s, %s: mailbox [%s] found", + __FILE__, __FUNCTION__, name); - *mailbox_idnr = mboxidnr; - return 0; + *mailbox_idnr = mboxidnr; + return 0; } int db_listmailboxchildren(u64_t mailbox_idnr, u64_t user_idnr, u64_t ** children, int *nchildren, const char *filter) { - int i; - char *mailbox_name; - - /* retrieve the name of this mailbox */ - snprintf(query, DEF_QUERYSIZE, - "SELECT name FROM mailboxes WHERE " - "mailbox_idnr = '%llu' AND owner_idnr = '%llu'", - mailbox_idnr, user_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not retrieve mailbox name\n", - __FILE__, __FUNCTION__); - return -1; - } - - if (db_num_rows() == 0) { - trace(TRACE_WARNING, "%s,%s: No mailbox found with mailbox_idnr " - "[%llu]", __FILE__, __FUNCTION__, mailbox_idnr); - db_free_result(); - *children = NULL; - *nchildren = 0; - return 0; - } - - mailbox_name = db_get_result(0, 0); - db_free_result(); - if (mailbox_name) - snprintf(query, DEF_QUERYSIZE, - "SELECT mailbox_idnr FROM mailboxes WHERE name LIKE '%s/%s'" - " AND owner_idnr = '%llu'", - mailbox_name, filter, user_idnr); - else - snprintf(query, DEF_QUERYSIZE, - "SELECT mailbox_idnr FROM mailboxes WHERE name LIKE '%s'" - " AND owner_idnr = '%llu'", filter, user_idnr); - - /* now find the children */ - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not retrieve mailbox id", - __FILE__, __FUNCTION__); - return -1; - } - - if (db_num_rows() == 0) { - /* empty set */ - *children = NULL; - *nchildren = 0; - db_free_result(); - return 0; - } + int i; + char *mailbox_name; - *nchildren = db_num_rows(); - if (*nchildren == 0) { - *children = NULL; - db_free_result(); - return 0; - } + /* retrieve the name of this mailbox */ + snprintf(query, DEF_QUERYSIZE, + "SELECT name FROM mailboxes WHERE " + "mailbox_idnr = '%llu' AND owner_idnr = '%llu'", + mailbox_idnr, user_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not retrieve mailbox name\n", __FILE__, + __FUNCTION__); + return -1; + } - *children = (u64_t *) my_malloc(sizeof(u64_t) * (*nchildren)); + if (db_num_rows() == 0) { + trace(TRACE_WARNING, + "%s,%s: No mailbox found with mailbox_idnr " + "[%llu]", __FILE__, __FUNCTION__, mailbox_idnr); + db_free_result(); + *children = NULL; + *nchildren = 0; + return 0; + } - if (!(*children)) { - /* out of mem */ - trace(TRACE_ERROR, "%s,%s: out of memory\n", __FILE__, __FILE__); + mailbox_name = db_get_result(0, 0); db_free_result(); - return -1; - } + if (mailbox_name) + snprintf(query, DEF_QUERYSIZE, + "SELECT mailbox_idnr FROM mailboxes WHERE name LIKE '%s/%s'" + " AND owner_idnr = '%llu'", + mailbox_name, filter, user_idnr); + else + snprintf(query, DEF_QUERYSIZE, + "SELECT mailbox_idnr FROM mailboxes WHERE name LIKE '%s'" + " AND owner_idnr = '%llu'", filter, user_idnr); + + /* now find the children */ + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not retrieve mailbox id", + __FILE__, __FUNCTION__); + return -1; + } + + if (db_num_rows() == 0) { + /* empty set */ + *children = NULL; + *nchildren = 0; + db_free_result(); + return 0; + } + + *nchildren = db_num_rows(); + if (*nchildren == 0) { + *children = NULL; + db_free_result(); + return 0; + } + + *children = (u64_t *) my_malloc(sizeof(u64_t) * (*nchildren)); + + if (!(*children)) { + /* out of mem */ + trace(TRACE_ERROR, "%s,%s: out of memory\n", __FILE__, + __FILE__); + db_free_result(); + return -1; + } - for (i = 0; i < *nchildren; i++) { - (*children)[i] = db_get_result_u64(i, 0); - } + for (i = 0; i < *nchildren; i++) { + (*children)[i] = db_get_result_u64(i, 0); + } - db_free_result(); - return 0; /* success */ + db_free_result(); + return 0; /* success */ } int db_isselectable(u64_t mailbox_idnr) { - char *query_result; - long not_selectable; + char *query_result; + long not_selectable; - snprintf(query, DEF_QUERYSIZE, - "SELECT no_select FROM mailboxes WHERE mailbox_idnr = '%llu'", - mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT no_select FROM mailboxes WHERE mailbox_idnr = '%llu'", + mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not retrieve select-flag", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not retrieve select-flag", + __FILE__, __FUNCTION__); + return -1; + } - /* FIXME: figure this one out... */ - query_result = db_get_result(0, 0); + /* FIXME: figure this one out... */ + query_result = db_get_result(0, 0); - if (!query_result) { - /* empty set, mailbox does not exist */ - db_free_result(); - return 0; - } + if (!query_result) { + /* empty set, mailbox does not exist */ + db_free_result(); + return 0; + } - not_selectable = strtol(query_result, NULL, 10); - db_free_result(); - if (not_selectable == 0) - return 1; - else - return 0; + not_selectable = strtol(query_result, NULL, 10); + db_free_result(); + if (not_selectable == 0) + return 1; + else + return 0; } int db_noinferiors(u64_t mailbox_idnr) { - char *query_result; - long no_inferiors; + char *query_result; + long no_inferiors; - snprintf(query, DEF_QUERYSIZE, - "SELECT no_inferiors FROM mailboxes WHERE mailbox_idnr = '%llu'", - mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT no_inferiors FROM mailboxes WHERE mailbox_idnr = '%llu'", + mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not retrieve noinferiors-flag", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not retrieve noinferiors-flag", + __FILE__, __FUNCTION__); + return -1; + } - /* FIXME: figure this one out... */ - query_result = db_get_result(0, 0); + /* FIXME: figure this one out... */ + query_result = db_get_result(0, 0); - if (!query_result) { - /* empty set, mailbox does not exist */ + if (!query_result) { + /* empty set, mailbox does not exist */ + db_free_result(); + return 0; + } + no_inferiors = strtol(query_result, NULL, 10); db_free_result(); - return 0; - } - no_inferiors = strtol(query_result, NULL, 10); - db_free_result(); - return no_inferiors; + return no_inferiors; } int db_setselectable(u64_t mailbox_idnr, int select_value) { - snprintf(query, DEF_QUERYSIZE, - "UPDATE mailboxes SET no_select = %d WHERE mailbox_idnr = '%llu'", - (!select_value), mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "UPDATE mailboxes SET no_select = %d WHERE mailbox_idnr = '%llu'", + (!select_value), mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not set noselect-flag", - __FILE__, __FUNCTION__); - return -1; - } - return 0; + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not set noselect-flag", + __FILE__, __FUNCTION__); + return -1; + } + return 0; } -int db_get_mailbox_size(u64_t mailbox_idnr, int only_deleted, u64_t *mailbox_size) -{ +int db_get_mailbox_size(u64_t mailbox_idnr, int only_deleted, + u64_t * mailbox_size) +{ assert(mailbox_size != NULL); - + *mailbox_size = 0; - - if (only_deleted) + + if (only_deleted) snprintf(query, DEF_QUERYSIZE, "SELECT sum(pm.messagesize) FROM messages msg, " "physmessage pm " @@ -2489,7 +2620,8 @@ int db_get_mailbox_size(u64_t mailbox_idnr, int only_deleted, u64_t *mailbox_siz if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not calculate size of " - "mailbox [%llu]", __FILE__, __FUNCTION__, mailbox_idnr); + "mailbox [%llu]", __FILE__, __FUNCTION__, + mailbox_idnr); return -1; } @@ -2506,7 +2638,8 @@ int db_removemsg(u64_t user_idnr, u64_t mailbox_idnr) u64_t mailbox_size; if (db_get_mailbox_size(mailbox_idnr, 0, &mailbox_size) < 0) { - trace(TRACE_ERROR, "%s,%s: error getting size for mailbox [%llu]", + trace(TRACE_ERROR, + "%s,%s: error getting size for mailbox [%llu]", __FILE__, __FUNCTION__, mailbox_idnr); return -1; } @@ -2517,121 +2650,123 @@ int db_removemsg(u64_t user_idnr, u64_t mailbox_idnr) mailbox_idnr); if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not update messages in mailbox", + trace(TRACE_ERROR, + "%s,%s: could not update messages in mailbox", __FILE__, __FUNCTION__); return -1; } - + if (db_subtract_quotum_used(user_idnr, mailbox_size) < 0) { - trace(TRACE_ERROR, "%s,%s: error subtracting mailbox size from " + trace(TRACE_ERROR, + "%s,%s: error subtracting mailbox size from " "used quotum for mailbox [%llu], user [%llu]. Database " "might be inconsistent. Run dbmail-maintenance", __FILE__, __FUNCTION__, mailbox_idnr, user_idnr); return -1; } - return 0; /* success */ + return 0; /* success */ } int db_movemsg(u64_t mailbox_to, u64_t mailbox_from) { - snprintf(query, DEF_QUERYSIZE, - "UPDATE messages SET mailbox_idnr='%llu' WHERE" - " mailbox_idnr = '%llu'", mailbox_to, mailbox_from); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not update messages in mailbox\n", - __FILE__, __FUNCTION__); - return -1; - } - return 0; /* success */ -} - -int db_get_message_size(u64_t message_idnr, u64_t *message_size) -{ - char *result_string; - - assert (message_size != NULL); - - snprintf(query, DEF_QUERYSIZE, - "SELECT pm.messagesize FROM physmessage pm, messages msg " - "WHERE pm.id = msg.physmessage_id " - "AND message_idnr = '%llu'", message_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, - "%s,%s: could not fetch message size for message id " - "[%llu]", __FILE__, __FUNCTION__, message_idnr); - return -1; - } - - if (db_num_rows() != 1) { - trace(TRACE_ERROR, - "%s,%s: message [%llu] does not exist/has " - "multiple entries\n", - __FILE__, __FUNCTION__, message_idnr); - db_free_result(); - return -1; - } - - /* FIXME: figure this one out... */ - result_string = db_get_result(0, 0); - if (result_string) - *message_size = strtoull(result_string, NULL, 10); - else { - trace(TRACE_ERROR, - "%s,%s: no result set after requesting msgsize " - "of msg [%llu]\n", - __FILE__, __FUNCTION__, message_idnr); - db_free_result(); - return -1; - } - db_free_result(); - return 1; - + snprintf(query, DEF_QUERYSIZE, + "UPDATE messages SET mailbox_idnr='%llu' WHERE" + " mailbox_idnr = '%llu'", mailbox_to, mailbox_from); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not update messages in mailbox\n", + __FILE__, __FUNCTION__); + return -1; + } + return 0; /* success */ +} + +int db_get_message_size(u64_t message_idnr, u64_t * message_size) +{ + char *result_string; + + assert(message_size != NULL); + + snprintf(query, DEF_QUERYSIZE, + "SELECT pm.messagesize FROM physmessage pm, messages msg " + "WHERE pm.id = msg.physmessage_id " + "AND message_idnr = '%llu'", message_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not fetch message size for message id " + "[%llu]", __FILE__, __FUNCTION__, message_idnr); + return -1; + } + + if (db_num_rows() != 1) { + trace(TRACE_ERROR, + "%s,%s: message [%llu] does not exist/has " + "multiple entries\n", + __FILE__, __FUNCTION__, message_idnr); + db_free_result(); + return -1; + } + + /* FIXME: figure this one out... */ + result_string = db_get_result(0, 0); + if (result_string) + *message_size = strtoull(result_string, NULL, 10); + else { + trace(TRACE_ERROR, + "%s,%s: no result set after requesting msgsize " + "of msg [%llu]\n", + __FILE__, __FUNCTION__, message_idnr); + db_free_result(); + return -1; + } + db_free_result(); + return 1; + } int db_copymsg(u64_t msg_idnr, u64_t mailbox_to, u64_t user_idnr, - u64_t *newmsg_idnr) + u64_t * newmsg_idnr) { u64_t msgsize; char unique_id[UID_SIZE]; /* Get the size of the message to be copied. */ if (db_get_message_size(msg_idnr, &msgsize) == -1) { - trace(TRACE_ERROR,"%s,%s: error getting message size for " - "message [%llu]", __FILE__, __FUNCTION__, msg_idnr); + trace(TRACE_ERROR, "%s,%s: error getting message size for " + "message [%llu]", __FILE__, __FUNCTION__, msg_idnr); return -1; } /* Check to see if the user has room for the message. */ switch (db_check_quotum_used(user_idnr, msgsize)) { - case -1: - trace(TRACE_ERROR, "%s,%s: error checking quotum", - __FILE__, __FUNCTION__); - return -1; - case 1: - trace(TRACE_INFO, "%s,%s: user [%llu] would exceed quotum", - __FILE__, __FUNCTION__, user_idnr); - return -2; + case -1: + trace(TRACE_ERROR, "%s,%s: error checking quotum", + __FILE__, __FUNCTION__); + return -1; + case 1: + trace(TRACE_INFO, "%s,%s: user [%llu] would exceed quotum", + __FILE__, __FUNCTION__, user_idnr); + return -2; } - + create_unique_id(unique_id, msg_idnr); /* Copy the message table entry of the message. */ snprintf(query, DEF_QUERYSIZE, - "INSERT INTO messages (mailbox_idnr," - "physmessage_id, seen_flag, answered_flag, deleted_flag, " - "flagged_flag, recent_flag, draft_flag, unique_id, status) " - "SELECT '%llu', " - "physmessage_id, seen_flag, answered_flag, deleted_flag, " - "flagged_flag, recent_flag, draft_flag, '%s', status " - "FROM messages WHERE message_idnr = '%llu'", - mailbox_to, unique_id, msg_idnr - ); + "INSERT INTO messages (mailbox_idnr," + "physmessage_id, seen_flag, answered_flag, deleted_flag, " + "flagged_flag, recent_flag, draft_flag, unique_id, status) " + "SELECT '%llu', " + "physmessage_id, seen_flag, answered_flag, deleted_flag, " + "flagged_flag, recent_flag, draft_flag, '%s', status " + "FROM messages WHERE message_idnr = '%llu'", + mailbox_to, unique_id, msg_idnr); if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: error copying message", - __FILE__, __FUNCTION__); + __FILE__, __FUNCTION__); return -1; } @@ -2643,11 +2778,11 @@ int db_copymsg(u64_t msg_idnr, u64_t mailbox_to, u64_t user_idnr, /* update quotum */ if (db_add_quotum_used(user_idnr, msgsize) == -1) { trace(TRACE_ERROR, "%s,%s: error setting the new quotum " - "used value for user [%llu]", - __FILE__, __FUNCTION__, user_idnr); + "used value for user [%llu]", + __FILE__, __FUNCTION__, user_idnr); return -1; - } - + } + return 1; } @@ -2662,14 +2797,14 @@ int db_getmailboxname(u64_t mailbox_idnr, u64_t user_idnr, char *name) result = db_get_mailbox_owner(mailbox_idnr, &owner_idnr); if (result <= 0) { trace(TRACE_ERROR, "%s,%s: error checking ownership of " - "mailbox", __FILE__,__FUNCTION__); + "mailbox", __FILE__, __FUNCTION__); return -1; } - + snprintf(query, DEF_QUERYSIZE, "SELECT name FROM mailboxes WHERE mailbox_idnr = '%llu'", mailbox_idnr); - + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not retrieve name", __FILE__, __FUNCTION__); @@ -2682,7 +2817,7 @@ int db_getmailboxname(u64_t mailbox_idnr, u64_t user_idnr, char *name) return 0; } - /* FIXME: isn't this just a stdup()??? */ + /* FIXME: isn't this just a stdup()??? */ query_result = db_get_result(0, 0); if (!query_result) { /* empty set, mailbox does not exist */ @@ -2693,23 +2828,24 @@ int db_getmailboxname(u64_t mailbox_idnr, u64_t user_idnr, char *name) tmp_name_len = strlen(query_result); if (!(tmp_name = my_malloc((tmp_name_len + 1) * sizeof(char)))) { - trace(TRACE_ERROR,"%s,%s: error allocating memory", + trace(TRACE_ERROR, "%s,%s: error allocating memory", __FILE__, __FUNCTION__); return -1; } - + strncpy(tmp_name, query_result, tmp_name_len); tmp_name[tmp_name_len] = '\0'; db_free_result(); - tmp_fq_name = mailbox_add_namespace(tmp_name, owner_idnr, user_idnr); + tmp_fq_name = + mailbox_add_namespace(tmp_name, owner_idnr, user_idnr); if (!tmp_fq_name) { - trace(TRACE_ERROR,"%s,%s: error getting fully qualified " + trace(TRACE_ERROR, "%s,%s: error getting fully qualified " "mailbox name", __FILE__, __FUNCTION__); return -1; } tmp_fq_name_len = strlen(tmp_fq_name); - if (tmp_fq_name_len >= IMAP_MAX_MAILBOX_NAMELEN) - tmp_fq_name_len = IMAP_MAX_MAILBOX_NAMELEN -1; + if (tmp_fq_name_len >= IMAP_MAX_MAILBOX_NAMELEN) + tmp_fq_name_len = IMAP_MAX_MAILBOX_NAMELEN - 1; strncpy(name, tmp_fq_name, tmp_fq_name_len); name[tmp_fq_name_len] = '\0'; my_free(tmp_name); @@ -2721,115 +2857,116 @@ int db_setmailboxname(u64_t mailbox_idnr, const char *name) { snprintf(query, DEF_QUERYSIZE, "UPDATE mailboxes SET name = '%s' " - "WHERE mailbox_idnr = '%llu'", - name, mailbox_idnr); - + "WHERE mailbox_idnr = '%llu'", name, mailbox_idnr); + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not set name", __FILE__, __FUNCTION__); return -1; } - + return 0; } -int db_expunge(u64_t mailbox_idnr, u64_t user_idnr, +int db_expunge(u64_t mailbox_idnr, u64_t user_idnr, u64_t ** msg_idnrs, u64_t * nmsgs) { - u64_t i; - u64_t mailbox_size; - - if (db_get_mailbox_size(mailbox_idnr, 1, &mailbox_size) < 0) { - trace(TRACE_ERROR, "%s,%s: error getting mailbox size " - "for mailbox [%llu]", - __FILE__, __FUNCTION__, mailbox_idnr); - return -1; - } + u64_t i; + u64_t mailbox_size; + + if (db_get_mailbox_size(mailbox_idnr, 1, &mailbox_size) < 0) { + trace(TRACE_ERROR, "%s,%s: error getting mailbox size " + "for mailbox [%llu]", + __FILE__, __FUNCTION__, mailbox_idnr); + return -1; + } - if (nmsgs && msg_idnrs) { - + if (nmsgs && msg_idnrs) { - /* first select msg UIDs */ - snprintf(query, DEF_QUERYSIZE, - "SELECT message_idnr FROM messages WHERE " - "mailbox_idnr = '%llu' AND deleted_flag='1' " - "AND status<'2' " - "ORDER BY message_idnr DESC", mailbox_idnr); - if (db_query(query) == -1) { + /* first select msg UIDs */ + snprintf(query, DEF_QUERYSIZE, + "SELECT message_idnr FROM messages WHERE " + "mailbox_idnr = '%llu' AND deleted_flag='1' " + "AND status<'2' " + "ORDER BY message_idnr DESC", mailbox_idnr); - trace(TRACE_ERROR, - "%s,%s: could not select messages in mailbox", __FILE__, - __FUNCTION__); - return -1; - } - - /* now alloc mem */ - *nmsgs = db_num_rows(); - *msg_idnrs = (u64_t *) my_malloc(sizeof(u64_t) * (*nmsgs)); - if (!(*msg_idnrs)) { - /* out of mem */ - *nmsgs = 0; - db_free_result(); - return -1; - } + if (db_query(query) == -1) { + + trace(TRACE_ERROR, + "%s,%s: could not select messages in mailbox", + __FILE__, __FUNCTION__); + return -1; + } + + /* now alloc mem */ + *nmsgs = db_num_rows(); + *msg_idnrs = (u64_t *) my_malloc(sizeof(u64_t) * (*nmsgs)); + if (!(*msg_idnrs)) { + /* out of mem */ + *nmsgs = 0; + db_free_result(); + return -1; + } - /* save ID's in array */ - for (i = 0; i < *nmsgs; i++) { - (*msg_idnrs)[i] = db_get_result_u64(i, 0); + /* save ID's in array */ + for (i = 0; i < *nmsgs; i++) { + (*msg_idnrs)[i] = db_get_result_u64(i, 0); + } + db_free_result(); } - db_free_result(); - } - /* update messages belonging to this mailbox: - * mark as expunged (status 2) */ - snprintf(query, DEF_QUERYSIZE, - "UPDATE messages SET status='002' " - "WHERE mailbox_idnr = '%llu' " - "AND deleted_flag='1' AND status < '002'", mailbox_idnr); + /* update messages belonging to this mailbox: + * mark as expunged (status 2) */ + snprintf(query, DEF_QUERYSIZE, + "UPDATE messages SET status='002' " + "WHERE mailbox_idnr = '%llu' " + "AND deleted_flag='1' AND status < '002'", mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not update messages in mailbox", - __FILE__, __FUNCTION__); - if (msg_idnrs) - my_free(*msg_idnrs); + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not update messages in mailbox", + __FILE__, __FUNCTION__); + if (msg_idnrs) + my_free(*msg_idnrs); - if (nmsgs) - *nmsgs = 0; + if (nmsgs) + *nmsgs = 0; - return -1; - } + return -1; + } - if (db_subtract_quotum_used(user_idnr, mailbox_size) < 0) { - trace(TRACE_ERROR, "%s,%s: error decreasing used quotum for " - "user [%llu]. Database might be inconsistent now", - __FILE__, __FUNCTION__, user_idnr); - return -1; - } + if (db_subtract_quotum_used(user_idnr, mailbox_size) < 0) { + trace(TRACE_ERROR, + "%s,%s: error decreasing used quotum for " + "user [%llu]. Database might be inconsistent now", + __FILE__, __FUNCTION__, user_idnr); + return -1; + } - return 0; /* success */ + return 0; /* success */ } u64_t db_first_unseen(u64_t mailbox_idnr) { - u64_t id; + u64_t id; - snprintf(query, DEF_QUERYSIZE, - "SELECT MIN(message_idnr) FROM messages " - "WHERE mailbox_idnr = '%llu' " - "AND status < '2' AND seen_flag = '0' AND unique_id != ''", - mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT MIN(message_idnr) FROM messages " + "WHERE mailbox_idnr = '%llu' " + "AND status < '2' AND seen_flag = '0' AND unique_id != ''", + mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not select messages", - __FILE__, __FUNCTION__); - return (u64_t) (-1); - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not select messages", + __FILE__, __FUNCTION__); + return (u64_t) (-1); + } - id = db_get_result_u64(0, 0); + id = db_get_result_u64(0, 0); - db_free_result(); - return id; + db_free_result(); + return id; } int db_subscribe(u64_t mailbox_idnr, u64_t user_idnr) @@ -2838,32 +2975,32 @@ int db_subscribe(u64_t mailbox_idnr, u64_t user_idnr) "SELECT * FROM subscription " "WHERE mailbox_id = '%llu' " "AND user_id = '%llu'", mailbox_idnr, user_idnr); - + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not verify subscription", __FILE__, __FUNCTION__); return (-1); } - + if (db_num_rows() > 0) { trace(TRACE_DEBUG, "%s,%s: already subscribed to mailbox " "[%llu]", __FILE__, __FUNCTION__, mailbox_idnr); db_free_result(); return 0; } - + db_free_result(); - + snprintf(query, DEF_QUERYSIZE, "INSERT INTO subscription (user_id, mailbox_id) " "VALUES ('%llu', '%llu')", user_idnr, mailbox_idnr); - + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not insert subscription", __FILE__, __FUNCTION__); return -1; } - + return 0; } @@ -2873,7 +3010,7 @@ int db_unsubscribe(u64_t mailbox_idnr, u64_t user_idnr) "DELETE FROM subscription " "WHERE user_id = '%llu' AND mailbox_id = '%llu'", user_idnr, mailbox_idnr); - + if (db_query(query) == -1) { trace(TRACE_ERROR, "%s,%s: could not update mailbox", __FILE__, __FUNCTION__); @@ -2885,246 +3022,250 @@ int db_unsubscribe(u64_t mailbox_idnr, u64_t user_idnr) int db_get_msgflag(const char *flag_name, u64_t msg_idnr, u64_t mailbox_idnr) { - char the_flag_name[DEF_QUERYSIZE / 2]; /* should be sufficient ;) */ - int val; - - /* determine flag */ - if (strcasecmp(flag_name, "seen") == 0) - snprintf(the_flag_name, DEF_QUERYSIZE / 2, "seen_flag"); - else if (strcasecmp(flag_name, "deleted") == 0) - snprintf(the_flag_name, DEF_QUERYSIZE / 2, "deleted_flag"); - else if (strcasecmp(flag_name, "answered") == 0) - snprintf(the_flag_name, DEF_QUERYSIZE / 2, "answered_flag"); - else if (strcasecmp(flag_name, "flagged") == 0) - snprintf(the_flag_name, DEF_QUERYSIZE / 2, "flagged_flag"); - else if (strcasecmp(flag_name, "recent") == 0) - snprintf(the_flag_name, DEF_QUERYSIZE / 2, "recent_flag"); - else if (strcasecmp(flag_name, "draft") == 0) - snprintf(the_flag_name, DEF_QUERYSIZE / 2, "draft_flag"); - else - return 0; /* non-existent flag is not set */ - - snprintf(query, DEF_QUERYSIZE, - "SELECT %s FROM messages " - "WHERE message_idnr = '%llu' AND status< '2' " - "AND unique_id != '' " - "AND mailbox_idnr = '%llu'", - the_flag_name, msg_idnr, mailbox_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not select message", - __FILE__, __FUNCTION__); - return (-1); - } - - val = db_get_result_int(0, 0); - - db_free_result(); - return val; + char the_flag_name[DEF_QUERYSIZE / 2]; /* should be sufficient ;) */ + int val; + + /* determine flag */ + if (strcasecmp(flag_name, "seen") == 0) + snprintf(the_flag_name, DEF_QUERYSIZE / 2, "seen_flag"); + else if (strcasecmp(flag_name, "deleted") == 0) + snprintf(the_flag_name, DEF_QUERYSIZE / 2, "deleted_flag"); + else if (strcasecmp(flag_name, "answered") == 0) + snprintf(the_flag_name, DEF_QUERYSIZE / 2, + "answered_flag"); + else if (strcasecmp(flag_name, "flagged") == 0) + snprintf(the_flag_name, DEF_QUERYSIZE / 2, "flagged_flag"); + else if (strcasecmp(flag_name, "recent") == 0) + snprintf(the_flag_name, DEF_QUERYSIZE / 2, "recent_flag"); + else if (strcasecmp(flag_name, "draft") == 0) + snprintf(the_flag_name, DEF_QUERYSIZE / 2, "draft_flag"); + else + return 0; /* non-existent flag is not set */ + + snprintf(query, DEF_QUERYSIZE, + "SELECT %s FROM messages " + "WHERE message_idnr = '%llu' AND status< '2' " + "AND unique_id != '' " + "AND mailbox_idnr = '%llu'", + the_flag_name, msg_idnr, mailbox_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not select message", + __FILE__, __FUNCTION__); + return (-1); + } + + val = db_get_result_int(0, 0); + + db_free_result(); + return val; } int db_get_msgflag_all(u64_t msg_idnr, u64_t mailbox_idnr, int *flags) { - int i; + int i; - memset(flags, 0, sizeof(int) * IMAP_NFLAGS); + memset(flags, 0, sizeof(int) * IMAP_NFLAGS); - snprintf(query, DEF_QUERYSIZE, - "SELECT seen_flag, answered_flag, deleted_flag, " - "flagged_flag, draft_flag, recent_flag FROM messages " - "WHERE message_idnr = '%llu' AND status<2 AND unique_id != '' " - "AND mailbox_idnr = '%llu'", msg_idnr, mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT seen_flag, answered_flag, deleted_flag, " + "flagged_flag, draft_flag, recent_flag FROM messages " + "WHERE message_idnr = '%llu' AND status<2 AND unique_id != '' " + "AND mailbox_idnr = '%llu'", msg_idnr, mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not select message", - __FILE__, __FUNCTION__); - return (-1); - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not select message", + __FILE__, __FUNCTION__); + return (-1); + } - if (db_num_rows() > 0) { - for (i = 0; i < IMAP_NFLAGS; i++) { - flags[i] = db_get_result_bool(0, i); + if (db_num_rows() > 0) { + for (i = 0; i < IMAP_NFLAGS; i++) { + flags[i] = db_get_result_bool(0, i); + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } int db_set_msgflag(u64_t msg_idnr, u64_t mailbox_idnr, int *flags, int action_type) { - /* we're lazy.. just call db_set_msgflag_range with range - * msg_idnr to msg_idnr! */ + /* we're lazy.. just call db_set_msgflag_range with range + * msg_idnr to msg_idnr! */ - if (db_set_msgflag_range(msg_idnr, msg_idnr, mailbox_idnr, - flags, action_type) == -1) { - trace(TRACE_ERROR, "%s,%s: could not set message flags", - __FILE__, __FUNCTION__); - return -1; - } + if (db_set_msgflag_range(msg_idnr, msg_idnr, mailbox_idnr, + flags, action_type) == -1) { + trace(TRACE_ERROR, "%s,%s: could not set message flags", + __FILE__, __FUNCTION__); + return -1; + } - return 0; + return 0; } int db_set_msgflag_range(u64_t msg_idnr_low, u64_t msg_idnr_high, u64_t mailbox_idnr, int *flags, int action_type) { - int i; - int placed = 0; - int left; + int i; + int placed = 0; + int left; - snprintf(query, DEF_QUERYSIZE, "UPDATE messages SET "); + snprintf(query, DEF_QUERYSIZE, "UPDATE messages SET "); - for (i = 0; i < IMAP_NFLAGS; i++) { - left = DEF_QUERYSIZE - strlen(query); - switch (action_type) { - case IMAPFA_ADD: - if (flags[i] > 0) { - strncat(query, db_flag_desc[i], left); - left = DEF_QUERYSIZE - strlen(query); - strncat(query, "=1,", left); - placed = 1; - } - break; - case IMAPFA_REMOVE: - if (flags[i] > 0) { - strncat(query, db_flag_desc[i], left); + for (i = 0; i < IMAP_NFLAGS; i++) { left = DEF_QUERYSIZE - strlen(query); - strncat(query, "=0,", left); - placed = 1; - } - break; - - case IMAPFA_REPLACE: - strncat(query, db_flag_desc[i], left); - left = DEF_QUERYSIZE - strlen(query); - if (flags[i] == 0) - strncat(query, "=0,", left); - else - strncat(query, "=1,", left); - placed = 1; - break; - } - db_free_result(); - } - - if (!placed) - return 0; /* nothing to update */ - - /* last character in string is comma, replace it --> strlen()-1 */ - left = DEF_QUERYSIZE - strlen(query); - snprintf(&query[strlen(query) - 1], left, - " WHERE message_idnr BETWEEN '%llu' AND '%llu' AND " - "status < '2' AND mailbox_idnr = '%llu'", - msg_idnr_low, msg_idnr_high, mailbox_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not set flags", - __FILE__, __FUNCTION__); - return (-1); - } - - return 0; + switch (action_type) { + case IMAPFA_ADD: + if (flags[i] > 0) { + strncat(query, db_flag_desc[i], left); + left = DEF_QUERYSIZE - strlen(query); + strncat(query, "=1,", left); + placed = 1; + } + break; + case IMAPFA_REMOVE: + if (flags[i] > 0) { + strncat(query, db_flag_desc[i], left); + left = DEF_QUERYSIZE - strlen(query); + strncat(query, "=0,", left); + placed = 1; + } + break; + + case IMAPFA_REPLACE: + strncat(query, db_flag_desc[i], left); + left = DEF_QUERYSIZE - strlen(query); + if (flags[i] == 0) + strncat(query, "=0,", left); + else + strncat(query, "=1,", left); + placed = 1; + break; + } + db_free_result(); + } + + if (!placed) + return 0; /* nothing to update */ + + /* last character in string is comma, replace it --> strlen()-1 */ + left = DEF_QUERYSIZE - strlen(query); + snprintf(&query[strlen(query) - 1], left, + " WHERE message_idnr BETWEEN '%llu' AND '%llu' AND " + "status < '2' AND mailbox_idnr = '%llu'", + msg_idnr_low, msg_idnr_high, mailbox_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not set flags", + __FILE__, __FUNCTION__); + return (-1); + } + + return 0; } int db_get_msgdate(u64_t mailbox_idnr, u64_t msg_idnr, char *date) { - char *query_result; - char *to_char_str; - - to_char_str = date2char_str("pm.internal_date"); - snprintf(query, DEF_QUERYSIZE, - "SELECT %s FROM physmessage pm, messages msg " - "WHERE msg.mailbox_idnr = '%llu' " - "AND msg.message_idnr = '%llu' AND msg.unique_id!='' " - "AND pm.id = msg.physmessage_id", - to_char_str, mailbox_idnr, msg_idnr); - my_free(to_char_str); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get message", - __FILE__, __FUNCTION__); - return (-1); - } - - if ((db_num_rows() > 0) && (query_result = db_get_result(0, 0))) { - strncpy(date, query_result, IMAP_INTERNALDATE_LEN); - date[IMAP_INTERNALDATE_LEN - 1] = '\0'; - } else { - /* no date ? let's say 1 jan 1970 */ - strncpy(date, "1970-01-01 00:00:01", IMAP_INTERNALDATE_LEN); - date[IMAP_INTERNALDATE_LEN - 1] = '\0'; - } - - db_free_result(); - return 0; + char *query_result; + char *to_char_str; + + to_char_str = date2char_str("pm.internal_date"); + snprintf(query, DEF_QUERYSIZE, + "SELECT %s FROM physmessage pm, messages msg " + "WHERE msg.mailbox_idnr = '%llu' " + "AND msg.message_idnr = '%llu' AND msg.unique_id!='' " + "AND pm.id = msg.physmessage_id", + to_char_str, mailbox_idnr, msg_idnr); + my_free(to_char_str); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not get message", + __FILE__, __FUNCTION__); + return (-1); + } + + if ((db_num_rows() > 0) && (query_result = db_get_result(0, 0))) { + strncpy(date, query_result, IMAP_INTERNALDATE_LEN); + date[IMAP_INTERNALDATE_LEN - 1] = '\0'; + } else { + /* no date ? let's say 1 jan 1970 */ + strncpy(date, "1970-01-01 00:00:01", + IMAP_INTERNALDATE_LEN); + date[IMAP_INTERNALDATE_LEN - 1] = '\0'; + } + + db_free_result(); + return 0; } int db_set_rfcsize(u64_t rfcsize, u64_t msg_idnr, u64_t mailbox_idnr) { - u64_t physmessage_id = 0; - - snprintf(query, DEF_QUERYSIZE, - "SELECT physmessage_id FROM messages " - "WHERE message_idnr = '%llu' " - "AND mailbox_idnr = '%llu'", msg_idnr, mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get physmessage_id for " - "message [%llu]", __FILE__, __FUNCTION__, msg_idnr); - return -1; - } - - if (db_num_rows() == 0) { - trace(TRACE_DEBUG, "%s,%s: no such message [%llu]", - __FILE__, __FUNCTION__, msg_idnr); - return 0; - } + u64_t physmessage_id = 0; + + snprintf(query, DEF_QUERYSIZE, + "SELECT physmessage_id FROM messages " + "WHERE message_idnr = '%llu' " + "AND mailbox_idnr = '%llu'", msg_idnr, mailbox_idnr); + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not get physmessage_id for " + "message [%llu]", __FILE__, __FUNCTION__, msg_idnr); + return -1; + } + + if (db_num_rows() == 0) { + trace(TRACE_DEBUG, "%s,%s: no such message [%llu]", + __FILE__, __FUNCTION__, msg_idnr); + return 0; + } - physmessage_id = db_get_result_u64(0, 0); + physmessage_id = db_get_result_u64(0, 0); - snprintf(query, DEF_QUERYSIZE, - "UPDATE physmessage SET rfcsize = '%llu' " - "WHERE id = '%llu'", rfcsize, physmessage_id); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not update " - "message [%llu]", __FILE__, __FUNCTION__, msg_idnr); - return -1; - } + snprintf(query, DEF_QUERYSIZE, + "UPDATE physmessage SET rfcsize = '%llu' " + "WHERE id = '%llu'", rfcsize, physmessage_id); + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not update " + "message [%llu]", __FILE__, __FUNCTION__, msg_idnr); + return -1; + } - return 0; + return 0; } -int db_get_rfcsize(u64_t msg_idnr, u64_t mailbox_idnr, u64_t *rfc_size) +int db_get_rfcsize(u64_t msg_idnr, u64_t mailbox_idnr, u64_t * rfc_size) { - assert(rfc_size != NULL); - *rfc_size = 0; + assert(rfc_size != NULL); + *rfc_size = 0; - snprintf(query, DEF_QUERYSIZE, - "SELECT pm.rfcsize FROM physmessage pm, messages msg " - "WHERE pm.id = msg.physmessage_id " - "AND msg.message_idnr = '%llu' " - "AND msg.status< '2' " - "AND msg.unique_id != '' " - "AND msg.mailbox_idnr = '%llu'", msg_idnr, mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT pm.rfcsize FROM physmessage pm, messages msg " + "WHERE pm.id = msg.physmessage_id " + "AND msg.message_idnr = '%llu' " + "AND msg.status< '2' " + "AND msg.unique_id != '' " + "AND msg.mailbox_idnr = '%llu'", msg_idnr, mailbox_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, + "%s,%s: could not fetch RFC size from table", + __FILE__, __FUNCTION__); + return -1; + } - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not fetch RFC size from table", - __FILE__, __FUNCTION__); - return -1; - } + if (db_num_rows() < 1) { + trace(TRACE_ERROR, "%s,%s: message not found", + __FILE__, __FUNCTION__); + db_free_result(); + return -1; + } - if (db_num_rows() < 1) { - trace(TRACE_ERROR, "%s,%s: message not found", - __FILE__, __FUNCTION__); - db_free_result(); - return -1; - } + *rfc_size = db_get_result_u64(0, 0); - *rfc_size = db_get_result_u64(0, 0); - - db_free_result(); - return 1; + db_free_result(); + return 1; } int db_get_msginfo_range(u64_t msg_idnr_low, u64_t msg_idnr_high, @@ -3133,220 +3274,232 @@ int db_get_msginfo_range(u64_t msg_idnr_low, u64_t msg_idnr_high, int get_rfcsize, int get_msg_idnr, msginfo_t ** result, unsigned *resultsetlen) { - unsigned nrows, i, j; - char *query_result; - char *to_char_str; - *result = 0; - *resultsetlen = 0; - - db_free_result(); - - to_char_str = date2char_str("internal_date"); - snprintf(query, DEF_QUERYSIZE, - "SELECT seen_flag, answered_flag, deleted_flag, flagged_flag, " - "draft_flag, recent_flag, %s, rfcsize, message_idnr " - "FROM messages msg, physmessage pm " - "WHERE pm.id = msg.physmessage_id " - "AND message_idnr BETWEEN '%llu' AND '%llu' " - "AND mailbox_idnr = '%llu' AND status < '2' AND unique_id != '' " - "ORDER BY message_idnr ASC", - to_char_str, msg_idnr_low, msg_idnr_high, - mailbox_idnr); - my_free(to_char_str); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not select message", - __FILE__, __FUNCTION__); - return (-1); - } - - if ((nrows = db_num_rows()) == 0) { - db_free_result(); - return 0; - } + unsigned nrows, i, j; + char *query_result; + char *to_char_str; + *result = 0; + *resultsetlen = 0; - *result = (msginfo_t *) my_malloc(nrows * sizeof(msginfo_t)); - if (!(*result)) { - trace(TRACE_ERROR, "%s,%s: out of memory", __FILE__, __FUNCTION__); db_free_result(); - return -2; - } - memset(*result, 0, nrows * sizeof(msginfo_t)); + to_char_str = date2char_str("internal_date"); + snprintf(query, DEF_QUERYSIZE, + "SELECT seen_flag, answered_flag, deleted_flag, flagged_flag, " + "draft_flag, recent_flag, %s, rfcsize, message_idnr " + "FROM messages msg, physmessage pm " + "WHERE pm.id = msg.physmessage_id " + "AND message_idnr BETWEEN '%llu' AND '%llu' " + "AND mailbox_idnr = '%llu' AND status < '2' AND unique_id != '' " + "ORDER BY message_idnr ASC", + to_char_str, msg_idnr_low, msg_idnr_high, mailbox_idnr); + my_free(to_char_str); - for (i = 0; i < nrows; i++) { - if (get_flags) { - for (j = 0; j < IMAP_NFLAGS; j++) { - (*result)[i].flags[j] = db_get_result_bool(i, j); - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not select message", + __FILE__, __FUNCTION__); + return (-1); } - if (get_internaldate) { - query_result = db_get_result(i, IMAP_NFLAGS); - strncpy((*result)[i].internaldate, - (query_result) ? query_result : "1970-01-01 00:00:01", - IMAP_INTERNALDATE_LEN); + if ((nrows = db_num_rows()) == 0) { + db_free_result(); + return 0; } - if (get_rfcsize) { - (*result)[i].rfcsize = db_get_result_u64(i, IMAP_NFLAGS + 1); + + *result = (msginfo_t *) my_malloc(nrows * sizeof(msginfo_t)); + if (!(*result)) { + trace(TRACE_ERROR, "%s,%s: out of memory", __FILE__, + __FUNCTION__); + db_free_result(); + return -2; } - if (get_msg_idnr) { - (*result)[i].uid = db_get_result_u64(i, IMAP_NFLAGS + 2); + + memset(*result, 0, nrows * sizeof(msginfo_t)); + + for (i = 0; i < nrows; i++) { + if (get_flags) { + for (j = 0; j < IMAP_NFLAGS; j++) { + (*result)[i].flags[j] = + db_get_result_bool(i, j); + } + } + + if (get_internaldate) { + query_result = db_get_result(i, IMAP_NFLAGS); + strncpy((*result)[i].internaldate, + (query_result) ? query_result : + "1970-01-01 00:00:01", + IMAP_INTERNALDATE_LEN); + } + if (get_rfcsize) { + (*result)[i].rfcsize = + db_get_result_u64(i, IMAP_NFLAGS + 1); + } + if (get_msg_idnr) { + (*result)[i].uid = + db_get_result_u64(i, IMAP_NFLAGS + 2); + } } - } - db_free_result(); + db_free_result(); - *resultsetlen = nrows; - return 0; + *resultsetlen = nrows; + return 0; } int db_get_main_header(u64_t msg_idnr, struct list *hdrlist) { - char *query_result; - u64_t dummy = 0, sizedummy = 0; - int result; + char *query_result; + u64_t dummy = 0, sizedummy = 0; + int result; - if (!hdrlist) - return 0; + if (!hdrlist) + return 0; - if (hdrlist->start) - list_freelist(&hdrlist->start); + if (hdrlist->start) + list_freelist(&hdrlist->start); - list_init(hdrlist); + list_init(hdrlist); - snprintf(query, DEF_QUERYSIZE, - "SELECT messageblk " - "FROM messageblks blk, messages msg " - "WHERE blk.physmessage_id = msg.physmessage_id " - "AND msg.message_idnr = '%llu' " - "ORDER BY blk.messageblk_idnr ASC", msg_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT messageblk " + "FROM messageblks blk, messages msg " + "WHERE blk.physmessage_id = msg.physmessage_id " + "AND msg.message_idnr = '%llu' " + "ORDER BY blk.messageblk_idnr ASC", msg_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get message header", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not get message header", + __FILE__, __FUNCTION__); + return -1; + } - if (db_num_rows() > 0) { - query_result = db_get_result(0, 0); - if (!query_result) { - trace(TRACE_ERROR, "%s,%s: no header for message found", - __FILE__, __FUNCTION__); - db_free_result(); - return -1; - } - } else { - trace(TRACE_ERROR, "%s,%s: no message blocks found for message", - __FILE__, __FUNCTION__); - db_free_result(); - return -1; - } + if (db_num_rows() > 0) { + query_result = db_get_result(0, 0); + if (!query_result) { + trace(TRACE_ERROR, + "%s,%s: no header for message found", + __FILE__, __FUNCTION__); + db_free_result(); + return -1; + } + } else { + trace(TRACE_ERROR, + "%s,%s: no message blocks found for message", + __FILE__, __FUNCTION__); + db_free_result(); + return -1; + } - result = mime_readheader(query_result, &dummy, hdrlist, &sizedummy); + result = + mime_readheader(query_result, &dummy, hdrlist, &sizedummy); - db_free_result(); + db_free_result(); - if (result == -1) { - /* parse error */ - trace(TRACE_ERROR, "%s,%s: error parsing header of message [%llu]", - __FILE__, __FUNCTION__, msg_idnr); - if (hdrlist->start) { - list_freelist(&hdrlist->start); - list_init(hdrlist); + if (result == -1) { + /* parse error */ + trace(TRACE_ERROR, + "%s,%s: error parsing header of message [%llu]", + __FILE__, __FUNCTION__, msg_idnr); + if (hdrlist->start) { + list_freelist(&hdrlist->start); + list_init(hdrlist); + } + return -3; } - return -3; - } - if (result == -2) { - /* out of memory */ - trace(TRACE_ERROR, "%s,%s: out of memory", __FILE__, __FUNCTION__); - if (hdrlist->start) { - list_freelist(&hdrlist->start); - list_init(hdrlist); + if (result == -2) { + /* out of memory */ + trace(TRACE_ERROR, "%s,%s: out of memory", __FILE__, + __FUNCTION__); + if (hdrlist->start) { + list_freelist(&hdrlist->start); + list_init(hdrlist); + } + return -2; } - return -2; - } - /* success ! */ - return 0; + /* success ! */ + return 0; } int db_mailbox_msg_match(u64_t mailbox_idnr, u64_t msg_idnr) { - int val; + int val; - snprintf(query, DEF_QUERYSIZE, - "SELECT message_idnr FROM messages " - "WHERE message_idnr = '%llu' " - "AND mailbox_idnr = '%llu' " - "AND status< '002' AND unique_id!=''", msg_idnr, - mailbox_idnr); + snprintf(query, DEF_QUERYSIZE, + "SELECT message_idnr FROM messages " + "WHERE message_idnr = '%llu' " + "AND mailbox_idnr = '%llu' " + "AND status< '002' AND unique_id!=''", msg_idnr, + mailbox_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get message", - __FILE__, __FUNCTION__); - return (-1); - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not get message", + __FILE__, __FUNCTION__); + return (-1); + } - val = db_num_rows(); - db_free_result(); - return val; + val = db_num_rows(); + db_free_result(); + return val; } int db_get_user_aliases(u64_t user_idnr, struct list *aliases) { - int i, n; - char *query_result; - if (!aliases) { - trace(TRACE_ERROR, "%s,%s: got a NULL pointer as argument", - __FILE__, __FUNCTION__); - return -2; - } + int i, n; + char *query_result; + if (!aliases) { + trace(TRACE_ERROR, "%s,%s: got a NULL pointer as argument", + __FILE__, __FUNCTION__); + return -2; + } - list_init(aliases); + list_init(aliases); - /* do a inverted (DESC) query because adding the names to the - * final list inverts again */ - snprintf(query, DEF_QUERYSIZE, - "SELECT alias FROM aliases WHERE deliver_to = '%llu' " - "ORDER BY alias DESC", user_idnr); + /* do a inverted (DESC) query because adding the names to the + * final list inverts again */ + snprintf(query, DEF_QUERYSIZE, + "SELECT alias FROM aliases WHERE deliver_to = '%llu' " + "ORDER BY alias DESC", user_idnr); - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not retrieve list", - __FILE__, __FUNCTION__); - return -1; - } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not retrieve list", + __FILE__, __FUNCTION__); + return -1; + } - n = db_num_rows(); - for (i = 0; i < n; i++) { - query_result = db_get_result(i, 0); - if (!query_result || !list_nodeadd(aliases, - query_result, strlen(query_result) + 1) ) { - list_freelist(&aliases->start); - db_free_result(); - return -2; + n = db_num_rows(); + for (i = 0; i < n; i++) { + query_result = db_get_result(i, 0); + if (!query_result || !list_nodeadd(aliases, + query_result, + strlen(query_result) + + 1)) { + list_freelist(&aliases->start); + db_free_result(); + return -2; + } } - } - db_free_result(); - return 0; + db_free_result(); + return 0; } int db_acl_has_right(u64_t userid, u64_t mboxid, const char *right_flag) { int result; int owner_result; - + trace(TRACE_DEBUG, "%s,%s: checking ACL for user [%llu] on " "mailbox [%llu]", __FILE__, __FUNCTION__, userid, mboxid); owner_result = db_user_is_mailbox_owner(userid, mboxid); if (owner_result < 0) { - trace(TRACE_ERROR, "%s,%s: error checking mailbox ownership.", - __FILE__, __FUNCTION__); + trace(TRACE_ERROR, + "%s,%s: error checking mailbox ownership.", __FILE__, + __FUNCTION__); return -1; } - + if (owner_result == 1) return 1; @@ -3355,18 +3508,18 @@ int db_acl_has_right(u64_t userid, u64_t mboxid, const char *right_flag) "WHERE user_id = '%llu' " "AND mailbox_id = '%llu' " "AND %s = '1'", userid, mboxid, right_flag); - + if (db_query(query) < 0) { trace(TRACE_ERROR, "%s,%s: error finding acl_right", __FILE__, __FUNCTION__); return -1; } - + if (db_num_rows() == 0) result = 0; else result = 1; - + db_free_result(); return result; } @@ -3379,7 +3532,7 @@ static int db_acl_has_acl(u64_t userid, u64_t mboxid) "SELECT user_id, mailbox_id FROM acl " "WHERE user_id = '%llu' AND mailbox_id = '%llu'", userid, mboxid); - + if (db_query(query) < 0) { trace(TRACE_ERROR, "%s,%s: Error finding ACL entry", __FILE__, __FUNCTION__); @@ -3402,9 +3555,10 @@ static int db_acl_create_acl(u64_t userid, u64_t mboxid) "VALUES ('%llu', '%llu')", userid, mboxid); if (db_query(query) < 0) { - trace(TRACE_ERROR, "%s,%s: Error creating ACL entry for user " - "[%llu], mailbox [%llu].", - __FILE__, __FUNCTION__, userid, mboxid); + trace(TRACE_ERROR, + "%s,%s: Error creating ACL entry for user " + "[%llu], mailbox [%llu].", __FILE__, __FUNCTION__, + userid, mboxid); return -1; } @@ -3417,7 +3571,7 @@ int db_acl_set_right(u64_t userid, u64_t mboxid, const char *right_flag, int owner_result; int result; - assert (set == 0 || set == 1); + assert(set == 0 || set == 1); trace(TRACE_DEBUG, "%s, %s: Setting ACL for user [%llu], mailbox " "[%llu].", __FILE__, __FUNCTION__, userid, mboxid); @@ -3428,7 +3582,7 @@ int db_acl_set_right(u64_t userid, u64_t mboxid, const char *right_flag, "mailbox.", __FILE__, __FUNCTION__); return -1; } - if (owner_result == 1) + if (owner_result == 1) return 0; // if necessary, create ACL for user, mailbox @@ -3439,7 +3593,7 @@ int db_acl_set_right(u64_t userid, u64_t mboxid, const char *right_flag, __FILE__, __FUNCTION__, userid, mboxid); return -1; } - + if (result == 0) { if (db_acl_create_acl(userid, mboxid) == -1) { trace(TRACE_ERROR, "%s,%s: Error creating ACL for " @@ -3448,7 +3602,7 @@ int db_acl_set_right(u64_t userid, u64_t mboxid, const char *right_flag, return -1; } } - + snprintf(query, DEF_QUERYSIZE, "UPDATE acl SET %s = '%i' " "WHERE user_id = '%llu' AND mailbox_id = '%llu'", @@ -3464,7 +3618,7 @@ int db_acl_set_right(u64_t userid, u64_t mboxid, const char *right_flag, "mailbox [%llu].", __FILE__, __FUNCTION__, userid, mboxid); return 1; } - + int db_acl_delete_acl(u64_t userid, u64_t mboxid) { trace(TRACE_DEBUG, "%s,%s: deleting ACL for user [%llu], " @@ -3474,7 +3628,7 @@ int db_acl_delete_acl(u64_t userid, u64_t mboxid) "DELETE FROM acl " "WHERE user_id = '%llu' AND mailbox_id = '%llu'", userid, mboxid); - + if (db_query(query) < 0) { trace(TRACE_ERROR, "%s,%s: error deleting ACL", __FILE__, __FUNCTION__); @@ -3496,12 +3650,12 @@ int db_acl_get_identifier(u64_t mboxid, struct list *identifier_list) snprintf(query, DEF_QUERYSIZE, "SELECT users.userid FROM users, acl " "WHERE acl.mailbox_id = '%llu' " - "AND users.user_idnr = acl.user_id", - mboxid); - + "AND users.user_idnr = acl.user_id", mboxid); + if (db_query(query) < 0) { trace(TRACE_ERROR, "%s,%s: error getting acl identifiers " - "for mailbox [%llu].", __FILE__, __FUNCTION__, mboxid); + "for mailbox [%llu].", __FILE__, __FUNCTION__, + mboxid); return -1; } @@ -3511,7 +3665,9 @@ int db_acl_get_identifier(u64_t mboxid, struct list *identifier_list) trace(TRACE_DEBUG, "%s,%s: adding %s to identifier list", __FILE__, __FUNCTION__, result_string); if (!result_string || !list_nodeadd(identifier_list, - result_string, strlen(result_string) + 1)) { + result_string, + strlen(result_string) + + 1)) { db_free_result(); return -2; } @@ -3520,20 +3676,20 @@ int db_acl_get_identifier(u64_t mboxid, struct list *identifier_list) return 1; } -int db_get_mailbox_owner(u64_t mboxid, u64_t *owner_id) +int db_get_mailbox_owner(u64_t mboxid, u64_t * owner_id) { assert(owner_id != NULL); snprintf(query, DEF_QUERYSIZE, "SELECT owner_idnr FROM mailboxes " "WHERE mailbox_idnr = '%llu'", mboxid); - + if (db_query(query) < 0) { trace(TRACE_ERROR, "%s,%s: error finding owner of mailbox " "[%llu]", __FILE__, __FUNCTION__, mboxid); return -1; } - + *owner_id = db_get_result_u64(0, 0); db_free_result(); if (*owner_id == 0) @@ -3542,18 +3698,18 @@ int db_get_mailbox_owner(u64_t mboxid, u64_t *owner_id) return 1; } -int db_user_is_mailbox_owner(u64_t userid, u64_t mboxid) +int db_user_is_mailbox_owner(u64_t userid, u64_t mboxid) { int result; snprintf(query, DEF_QUERYSIZE, "SELECT mailbox_idnr FROM mailboxes " "WHERE mailbox_idnr = '%llu' " - "AND owner_idnr = '%llu'", - mboxid, userid); - + "AND owner_idnr = '%llu'", mboxid, userid); + if (db_query(query) < 0) { - trace(TRACE_ERROR, "%s,%s: error checking if user [%llu] is " + trace(TRACE_ERROR, + "%s,%s: error checking if user [%llu] is " "owner of mailbox [%llu]", __FILE__, __FUNCTION__, userid, mboxid); return -1; @@ -3561,9 +3717,9 @@ int db_user_is_mailbox_owner(u64_t userid, u64_t mboxid) if (db_num_rows() == 0) result = 0; - else + else result = 1; - + db_free_result(); return result; } @@ -3577,46 +3733,46 @@ int db_get_result_int(unsigned row, unsigned field) { char *tmp; tmp = db_get_result(row, field); - return ( tmp ? atoi(tmp) : 0 ); + return (tmp ? atoi(tmp) : 0); } int db_get_result_bool(unsigned row, unsigned field) { char *tmp; tmp = db_get_result(row, field); - return ( tmp ? ( atoi(tmp) ? 1 : 0 ) : 0 ); + return (tmp ? (atoi(tmp) ? 1 : 0) : 0); } u64_t db_get_result_u64(unsigned row, unsigned field) { char *tmp; tmp = db_get_result(row, field); - return ( tmp ? strtoull(tmp, NULL, 10) : 0 ); + return (tmp ? strtoull(tmp, NULL, 10) : 0); } -char *date2char_str(const char *column) +char *date2char_str(const char *column) { unsigned len; char *s; len = strlen(TO_CHAR) + MAX_COLUMN_LEN; - - s = (char*) my_malloc(len); + + s = (char *) my_malloc(len); snprintf(s, len, TO_CHAR, column); return s; } -char *char2date_str(const char *date) +char *char2date_str(const char *date) { unsigned len; char *s; - + len = strlen(TO_CHAR) + MAX_DATE_LEN; - - s = (char*) my_malloc(len); + + s = (char *) my_malloc(len); snprintf(s, len, TO_DATE, date); - + return s; } @@ -49,10 +49,10 @@ #define MAX_EMAIL_SIZE 250 /* size of the messageblk's */ -#define READ_BLOCK_SIZE (512ul*1024ul) /* be carefull, MYSQL has a limit */ +#define READ_BLOCK_SIZE (512ul*1024ul) /* be carefull, MYSQL has a limit */ /* config types */ -#define CONFIG_MANDATORY 1 +#define CONFIG_MANDATORY 1 #define CONFIG_EMPTY 0 @@ -92,7 +92,7 @@ int db_disconnect(void); * - 0 on success * - 1 on failure */ -int db_query (const char *the_query); +int db_query(const char *the_query); /** * \brief get number of rows in result set. @@ -149,7 +149,7 @@ u64_t db_get_result_u64(unsigned row, unsigned field); * value was generated). * - the id otherwise */ -u64_t db_insert_result (const char *sequence_identifier); +u64_t db_insert_result(const char *sequence_identifier); /** * \brief escape a string for use in query @@ -159,8 +159,8 @@ u64_t db_insert_result (const char *sequence_identifier); * \return length of escaped string * \attention behaviour is undefined if to and from overlap */ -unsigned long db_escape_string(char *to, - const char *from, unsigned long length); +unsigned long db_escape_string(char *to, + const char *from, unsigned long length); /** * \brief get length in bytes of a result field in a result set. @@ -211,14 +211,14 @@ void db_store_auth_result(void); * \return a pointer to a result set * \bug this is really ugly and should be dealt with differently! */ -void* db_get_result_set(void); +void *db_get_result_set(void); /** * \brief set the new result set * \param res the new result set * \bug this is really ugly and should be dealt with differently! */ -void db_set_result_set(void* res); +void db_set_result_set(void *res); /* shared implementattion from hereon */ /** @@ -233,7 +233,7 @@ void db_set_result_set(void* res); * \attention function will fail and halt program if physmessage_id is * NULL on call. */ -int db_get_physmessage_id(u64_t message_idnr, u64_t *physmessage_id); +int db_get_physmessage_id(u64_t message_idnr, u64_t * physmessage_id); /** * \brief return number of bytes used by user identified by userid @@ -244,7 +244,7 @@ int db_get_physmessage_id(u64_t message_idnr, u64_t *physmessage_id); * - -1 on failure<BR> * - 1 otherwise */ -int db_get_quotum_used(u64_t user_idnr, u64_t *curmail_size); +int db_get_quotum_used(u64_t user_idnr, u64_t * curmail_size); /** * \brief finds all users which need to have their curmail_size (amount @@ -277,7 +277,7 @@ int db_calculate_quotum_used(u64_t user_idnr); * - -1 on database error * - 1 on success */ -int db_get_users_from_clientid(u64_t client_id, u64_t **user_ids, +int db_get_users_from_clientid(u64_t client_id, u64_t ** user_ids, unsigned *num_users); /** * \brief get deliver_to from alias. Gets a list of deliver_to @@ -289,7 +289,7 @@ int db_get_users_from_clientid(u64_t client_id, u64_t **user_ids, * - deliver_to address otherwise * \attention caller needs to free the return value */ -char* db_get_deliver_from_alias(const char *alias); +char *db_get_deliver_from_alias(const char *alias); /** * \brief get a list of aliases associated with a user's user_idnr * \param user_idnr idnr of user @@ -323,7 +323,8 @@ int db_addalias(u64_t user_idnr, const char *alias, u64_t clientid); * - 0 on success * - 1 if deliver_to already exists for given alias */ -int db_addalias_ext(const char *alias, const char *deliver_to, u64_t clientid); +int db_addalias_ext(const char *alias, const char *deliver_to, + u64_t clientid); /** * \brief remove alias for user * \param user_idnr user id @@ -354,7 +355,8 @@ int db_removealias_ext(const char *alias, const char *deliver_to); * - 0 on success * \attention caller should free the returned script */ -int db_get_sievescript_byname(u64_t user_idnr, char *scriptname, char **script); +int db_get_sievescript_byname(u64_t user_idnr, char *scriptname, + char **script); /** * \brief get the name of the active sieve script for a user * \param user_idnr user id @@ -389,7 +391,8 @@ int db_get_sievescript_listall(u64_t user_idnr, struct list *scriptlist); * - -1 on database failure * - 0 on success */ -int db_replace_sievescript(u64_t user_idnr, char *scriptname, char *script); +int db_replace_sievescript(u64_t user_idnr, char *scriptname, + char *script); /** * \brief add a sieve script for a user * \param user_idnr user id @@ -461,7 +464,7 @@ int db_set_sievescript_quota(u64_t user_idnr, u64_t quotasize); * - -1 on database failure * - 0 on success */ -int db_get_sievescript_quota(u64_t user_idnr, u64_t *quotasize); +int db_get_sievescript_quota(u64_t user_idnr, u64_t * quotasize); /** * \brief get auto-notification address for a user * \param user_idnr user id @@ -484,7 +487,7 @@ int db_get_notify_address(u64_t user_idnr, char **notify_address); * \attention caller should free reply_body */ int db_get_reply_body(u64_t user_idnr, char **body); - + /** * \brief get user idnr of a message. * \param message_idnr idnr of message @@ -504,7 +507,7 @@ u64_t db_get_useridnr(u64_t message_idnr); * - -1 on failure * - 1 on success */ -int db_insert_physmessage(u64_t *physmessage_id); +int db_insert_physmessage(u64_t * physmessage_id); /** * \brief insert a physmessage with an internal date. @@ -516,7 +519,7 @@ int db_insert_physmessage(u64_t *physmessage_id); * - 1 on success */ int db_insert_physmessage_with_internal_date(timestring_t internal_date, - u64_t *physmessage_id); + u64_t * physmessage_id); /** * \Brief update a physmessage by setting messagesize and rfcsize * \param physmessage_id the id of the physmessage @@ -543,16 +546,15 @@ int db_update_physmessage(u64_t physmessage_id, u64_t message_size, * - -1 on failure * - 1 on success */ -int db_insert_message(u64_t user_idnr, - const char *deliver_to, - int create_or_error_mailbox, - const char *unique_id, - u64_t *message_idnr); +int db_insert_message(u64_t user_idnr, + const char *deliver_to, + int create_or_error_mailbox, + const char *unique_id, u64_t * message_idnr); #define CREATE_IF_MBOX_NOT_FOUND 1 #define ERROR_IF_MBOX_NOT_FOUND -1 - + /** * \brief update unique_id, message_size and rfc_size of * a message identified by message_idnr @@ -565,7 +567,7 @@ int db_insert_message(u64_t user_idnr, * - 0 on success */ int db_update_message(u64_t message_idnr, const char *unique_id, - u64_t message_size, u64_t rfc_size); + u64_t message_size, u64_t rfc_size); /** * \brief set unique id of a message * \param message_idnr @@ -587,9 +589,10 @@ int db_message_set_unique_id(u64_t message_idnr, const char *unique_id); * - -1 on failure * - 1 on success */ -int db_insert_message_block_physmessage(const char *block, u64_t block_size, +int db_insert_message_block_physmessage(const char *block, + u64_t block_size, u64_t physmessage_id, - u64_t *messageblock_idnr); + u64_t * messageblock_idnr); /** * \brief insert a message block into the message block table * \param block the message block (which is a string) @@ -602,7 +605,7 @@ int db_insert_message_block_physmessage(const char *block, u64_t block_size, * - 1 otherwise */ int db_insert_message_block(const char *block, u64_t block_size, - u64_t message_idnr, u64_t *messageblock_idnr); + u64_t message_idnr, u64_t * messageblock_idnr); /** * \brief log IP-address for POP/IMAP_BEFORE_SMTP. If the IP-address * is already logged, it's timestamp is renewed. @@ -766,7 +769,8 @@ int db_delete_message(u64_t message_idnr); * - 0 on success * \attention this function is unable to delete shared mailboxes */ -int db_delete_mailbox(u64_t mailbox_idnr, int only_empty, int update_curmail_size); +int db_delete_mailbox(u64_t mailbox_idnr, int only_empty, + int update_curmail_size); /** * \brief write lines of message to fstream. Does not write the header @@ -782,19 +786,19 @@ int db_delete_mailbox(u64_t mailbox_idnr, int only_empty, int update_curmail_siz * - 0 on failure * - 1 on success */ -int db_send_message_lines (void *fstream, u64_t message_idnr, - long lines, int no_end_dot); +int db_send_message_lines(void *fstream, u64_t message_idnr, + long lines, int no_end_dot); /** * \brief create a new POP3 session. (was createsession() in dbmysql.c) * \param user_idnr user idnr * \param session_ptr pointer to POP3 session */ -int db_createsession (u64_t user_idnr, PopSession_t *session_ptr); +int db_createsession(u64_t user_idnr, PopSession_t * session_ptr); /** * \brief Clean up a POP3 Session * \param session_ptr pointer to POP3 session */ -void db_session_cleanup (PopSession_t *session_ptr); +void db_session_cleanup(PopSession_t * session_ptr); /** * \brief update POP3 session * \param session_ptr pointer to POP3 session @@ -805,7 +809,7 @@ void db_session_cleanup (PopSession_t *session_ptr); * not be nessecary, because the shared mailboxes are not * touched by POP3 */ -int db_update_pop (PopSession_t *session_ptr); +int db_update_pop(PopSession_t * session_ptr); /** * \brief set deleted status (=3) for all messages that are marked for * delete (=2) @@ -815,7 +819,7 @@ int db_update_pop (PopSession_t *session_ptr); * - -1 on database failure; * - 1 otherwise */ -int db_set_deleted (u64_t *affected_rows); +int db_set_deleted(u64_t * affected_rows); /** * \brief purge all messages from the database with a "delete"-status * (status = 3) @@ -828,7 +832,7 @@ int db_set_deleted (u64_t *affected_rows); * - 1 if a number of messages deleted (affected_rows will hold the number * of deleted messages. */ -int db_deleted_purge(u64_t *affected_rows); +int db_deleted_purge(u64_t * affected_rows); /** * \brief check if a block of a certain size can be inserted. * \param addblocksize size of added blocks (UNUSED) @@ -845,8 +849,8 @@ int db_deleted_purge(u64_t *affected_rows); * \attention when inserting a block would cause a limit run-overrun. * the message insert is automagically rolled back */ -u64_t db_check_sizelimit (u64_t addblocksize, u64_t message_idnr, - u64_t *user_idnr); +u64_t db_check_sizelimit(u64_t addblocksize, u64_t message_idnr, + u64_t * user_idnr); /** * \brief insert a message into the database * \param msgdata the message @@ -862,7 +866,7 @@ u64_t db_check_sizelimit (u64_t addblocksize, u64_t message_idnr, */ int db_imap_append_msg(const char *msgdata, u64_t datalen, u64_t mailbox_idnr, u64_t user_idnr, - timestring_t internal_date, u64_t *msg_idnr); + timestring_t internal_date, u64_t * msg_idnr); /* mailbox functionality */ /** @@ -876,7 +880,8 @@ int db_imap_append_msg(const char *msgdata, u64_t datalen, * - 0 if mailbox not found * - 1 if found */ -int db_findmailbox(const char *name, u64_t user_idnr, u64_t *mailbox_idnr); +int db_findmailbox(const char *name, u64_t user_idnr, + u64_t * mailbox_idnr); /** * \brief finds all the mailboxes owned by owner_idnr who match * the regex pattern pattern. @@ -892,8 +897,8 @@ int db_findmailbox(const char *name, u64_t user_idnr, u64_t *mailbox_idnr); * - 0 on success * - 1 on invalid regex pattern */ -int db_findmailbox_by_regex(u64_t owner_idnr, const char *pattern, - u64_t **children, unsigned *nchildren, +int db_findmailbox_by_regex(u64_t owner_idnr, const char *pattern, + u64_t ** children, unsigned *nchildren, int only_subscribed); /** * \brief get info on a mailbox. Info is filled in in the @@ -904,7 +909,7 @@ int db_findmailbox_by_regex(u64_t owner_idnr, const char *pattern, * - -1 on failure * - 0 on success */ -int db_getmailbox(mailbox_t *mb); +int db_getmailbox(mailbox_t * mb); /** * \brief find owner of a mailbox @@ -915,7 +920,7 @@ int db_getmailbox(mailbox_t *mb); * - 0 if owner not found * - 1 if owner found */ -int db_get_mailbox_owner(u64_t mboxid, u64_t *owner_id); +int db_get_mailbox_owner(u64_t mboxid, u64_t * owner_id); /** * \brief check if a user is owner of the specified mailbox @@ -936,7 +941,8 @@ int db_user_is_mailbox_owner(u64_t userid, u64_t mboxid); * - -1 on failure * - 0 on success */ -int db_createmailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr); +int db_createmailbox(const char *name, u64_t owner_idnr, + u64_t * mailbox_idnr); /** * \brief find a mailbox, create if not found * \param name name of mailbox @@ -945,7 +951,8 @@ int db_createmailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr); * - -1 on failure * - 0 on success */ -int db_find_create_mailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_idnr); +int db_find_create_mailbox(const char *name, u64_t owner_idnr, + u64_t * mailbox_idnr); /** * \brief produce a list containing the UID's of the specified * mailbox' children matching the search criterion @@ -958,8 +965,8 @@ int db_find_create_mailbox(const char *name, u64_t owner_idnr, u64_t *mailbox_id * - -1 on failure * - 0 on success */ -int db_listmailboxchildren(u64_t mailbox_idnr, u64_t user_idnr, - u64_t **children, int *nchildren, +int db_listmailboxchildren(u64_t mailbox_idnr, u64_t user_idnr, + u64_t ** children, int *nchildren, const char *filter); /** @@ -1020,7 +1027,7 @@ int db_movemsg(u64_t mailbox_to, u64_t mailbox_from); * - 0 on success */ int db_copymsg(u64_t msg_idnr, u64_t mailbox_to, - u64_t user_idnr, u64_t *newmsg_idnr); + u64_t user_idnr, u64_t * newmsg_idnr); /** * \brief get name of mailbox * \param mailbox_idnr @@ -1057,9 +1064,7 @@ int db_setmailboxname(u64_t mailbox_idnr, const char *name); * \attention caller should free msg_idnrs and nmsg */ int db_expunge(u64_t mailbox_idnr, - u64_t user_idnr, - u64_t **msg_idnrs, - u64_t *nmsgs); + u64_t user_idnr, u64_t ** msg_idnrs, u64_t * nmsgs); /** * \brief get first unseen message in a mailbox * \param mailbox_idnr @@ -1099,8 +1104,8 @@ int db_unsubscribe(u64_t mailbox_idnr, u64_t user_idnr); * - 0 if flag is not set or a non-existent flag is asked. * - 1 if flag is set. */ -int db_get_msgflag(const char *flag_name, - u64_t msg_idnr, u64_t mailbox_idnr); +int db_get_msgflag(const char *flag_name, + u64_t msg_idnr, u64_t mailbox_idnr); /** * \brief get all flags for a message @@ -1130,7 +1135,8 @@ int db_get_msgflag_all(u64_t msg_idnr, u64_t mailbox_idnr, int *flags); * - -1 on failure * - 0 on success */ -int db_set_msgflag(u64_t msg_idnr, u64_t mailbox_idnr, int *flags, int action_type); +int db_set_msgflag(u64_t msg_idnr, u64_t mailbox_idnr, int *flags, + int action_type); /** * \brief set flags for a range of messages in a mailbox * \param msg_idnr_low beginning of range @@ -1148,10 +1154,10 @@ int db_set_msgflag(u64_t msg_idnr, u64_t mailbox_idnr, int *flags, int action_ty * \return * - -1 on failure * - 0 on success - */ -int db_set_msgflag_range(u64_t msg_idnr_low, - u64_t msg_idnr_high, u64_t mailbox_idnr, - int *flags, int action_type); + */ +int db_set_msgflag_range(u64_t msg_idnr_low, + u64_t msg_idnr_high, u64_t mailbox_idnr, + int *flags, int action_type); /** * \brief retrieve internal message date * \param mailbox_idnr @@ -1183,7 +1189,7 @@ int db_set_rfcsize(u64_t rfcsize, u64_t msg_idnr, u64_t mailbox_idnr); * - -1 on failure * - 1 on success */ -int db_get_rfcsize(u64_t msg_idnr, u64_t mailbox_idnr, u64_t *rfc_size); +int db_get_rfcsize(u64_t msg_idnr, u64_t mailbox_idnr, u64_t * rfc_size); /** * \brief get info on a range of messages. * \param msg_idnr_low beginning of range @@ -1202,9 +1208,10 @@ int db_get_rfcsize(u64_t msg_idnr, u64_t mailbox_idnr, u64_t *rfc_size); * \attention caller should free result. */ int db_get_msginfo_range(u64_t msg_idnr_low, u64_t msg_idnr_high, - u64_t mailbox_idnr, int get_flags, int get_internaldate, - int get_rfcsize, int get_msg_idnr, - msginfo_t **result, unsigned *resultsetlen); + u64_t mailbox_idnr, int get_flags, + int get_internaldate, int get_rfcsize, + int get_msg_idnr, msginfo_t ** result, + unsigned *resultsetlen); /** * \brief builds a list containing the fields of @@ -1240,7 +1247,7 @@ int db_mailbox_msg_match(u64_t mailbox_idnr, u64_t message_idnr); * - 0 if no right * - 1 if user has the right */ -int db_acl_has_right(u64_t user_idnr, u64_t mailbox_idnr, +int db_acl_has_right(u64_t user_idnr, u64_t mailbox_idnr, const char *right_flag); /** @@ -75,19 +75,25 @@ typedef char timestring_t[TIMESTRING_SIZE]; /** parameters for the database connection */ typedef struct { - field_t host; /**< hostname or ip address of database server */ - field_t user; /**< username to connect with */ - field_t pass; /**< password of user */ - field_t db; /**< name of database to connect with */ - unsigned int port; /**< port number of database server */ - field_t sock; /**< path to local unix socket (local connection) */ + field_t host; + /**< hostname or ip address of database server */ + field_t user; + /**< username to connect with */ + field_t pass; + /**< password of user */ + field_t db;/**< name of database to connect with */ + unsigned int port; + /**< port number of database server */ + field_t sock; + /**< path to local unix socket (local connection) */ } db_param_t; - + /** configuration items */ -typedef struct -{ - field_t name; /**< name of configuration item */ - field_t value; /**< value of configuration item */ +typedef struct { + field_t name; + /**< name of configuration item */ + field_t value; + /**< value of configuration item */ } item_t; /** @@ -101,7 +107,8 @@ typedef struct * - -1 on error * - 0 on success */ -int ReadConfig(const char *serviceName, const char *cfilename, struct list *cfg_items); +int ReadConfig(const char *serviceName, const char *cfilename, + struct list *cfg_items); /** * \brief get configuration value for an item * \param name name of configuration item @@ -112,7 +119,8 @@ int ReadConfig(const char *serviceName, const char *cfilename, struct list *cfg_ * \attention value is set to a string beginning with a '\\0' if no configuration item with name is found in items. */ -int GetConfigValue(const field_t name, struct list *cfg_items, field_t value); +int GetConfigValue(const field_t name, struct list *cfg_items, + field_t value); /* some common used functions reading config options */ /** @@ -120,7 +128,7 @@ int GetConfigValue(const field_t name, struct list *cfg_items, field_t value); \param db_params list of database parameters (db_param_t) \param cfg_items list of configuration items */ -void GetDBParams(db_param_t *db_params, struct list *cfg_items); +void GetDBParams(db_param_t * db_params, struct list *cfg_items); /** \brief set the overall trace level, using the value in cfg_items \param cfg_items list of configuration items diff --git a/dbmailtypes.h b/dbmailtypes.h index 14275e1b..6d341d6c 100644 --- a/dbmailtypes.h +++ b/dbmailtypes.h @@ -54,58 +54,65 @@ typedef unsigned long long u64_t; /** all virtual_ definitions are session specific * when a RSET occurs all will be set to the real values */ -struct message -{ - u64_t msize; /**< message size */ - u64_t messageid; /**< messageid (from database) */ - u64_t realmessageid; /**< ? */ - char uidl[UID_SIZE]; /**< unique id */ - /* message status : - * 000 message is new, never touched - * 001 message is read - * 002 message is deleted by user - * ---------------------------------- - * The server additionally uses: - * 003 message is deleted by sysop - * 004 message is ready for final deletion */ - - u64_t messagestatus; /**< message status */ - u64_t virtual_messagestatus; /**< virtual message status */ +struct message { + u64_t msize; /**< message size */ + u64_t messageid; /**< messageid (from database) */ + u64_t realmessageid; + /**< ? */ + char uidl[UID_SIZE]; + /**< unique id */ + /* message status : + * 000 message is new, never touched + * 001 message is read + * 002 message is deleted by user + * ---------------------------------- + * The server additionally uses: + * 003 message is deleted by sysop + * 004 message is ready for final deletion */ + + u64_t messagestatus; + /**< message status */ + u64_t virtual_messagestatus; + /**< virtual message status */ }; /** * pop3 connection states */ typedef enum { - POP3_AUTHORIZATION_STATE, - POP3_TRANSACTION_STATE, - POP3_UPDATE_STATE, + POP3_AUTHORIZATION_STATE, + POP3_TRANSACTION_STATE, + POP3_UPDATE_STATE, } Pop3State_t; /** * struct for a POP3 session. */ -typedef struct -{ - int error_count; /**< number of errors that have occured */ - Pop3State_t state; /**< current POP state */ - int was_apop; /**< 1 if session was session was apop +typedef struct { + int error_count;/**< number of errors that have occured */ + Pop3State_t state; + /**< current POP state */ + int was_apop; /**< 1 if session was session was apop (no plaintext password) */ - - int SessionResult; /**< what happened during the session */ - - char *username; - char *password; - - char *apop_stamp; /**< timestamp for APOP */ - - u64_t useridnr; /**< Used by timsieved */ - - u64_t totalsize; /**< total size of messages */ - u64_t virtual_totalsize; - u64_t totalmessages;/**< number of messages */ - u64_t virtual_totalmessages; - - struct list messagelst; /** list of messages */ + + int SessionResult; + /**< what happened during the session */ + + char *username; + char *password; + + char *apop_stamp; + /**< timestamp for APOP */ + + u64_t useridnr; /**< Used by timsieved */ + + u64_t totalsize;/**< total size of messages */ + u64_t virtual_totalsize; + u64_t totalmessages; + /**< number of messages */ + u64_t virtual_totalmessages; + + struct list messagelst; + /** list of messages */ } PopSession_t; @@ -115,20 +122,25 @@ typedef struct */ #define IMAP_NFLAGS 6 -enum IMAP4_CLIENT_STATES { IMAPCS_INITIAL_CONNECT, IMAPCS_NON_AUTHENTICATED, - IMAPCS_AUTHENTICATED, IMAPCS_SELECTED, IMAPCS_LOGOUT }; +enum IMAP4_CLIENT_STATES { IMAPCS_INITIAL_CONNECT, + IMAPCS_NON_AUTHENTICATED, + IMAPCS_AUTHENTICATED, IMAPCS_SELECTED, IMAPCS_LOGOUT +}; -enum IMAP4_FLAGS { IMAPFLAG_SEEN = 0x01, IMAPFLAG_ANSWERED = 0x02, - IMAPFLAG_DELETED = 0x04, IMAPFLAG_FLAGGED = 0x08, - IMAPFLAG_DRAFT = 0x10, IMAPFLAG_RECENT = 0x20 }; +enum IMAP4_FLAGS { IMAPFLAG_SEEN = 0x01, IMAPFLAG_ANSWERED = 0x02, + IMAPFLAG_DELETED = 0x04, IMAPFLAG_FLAGGED = 0x08, + IMAPFLAG_DRAFT = 0x10, IMAPFLAG_RECENT = 0x20 +}; enum IMAP4_PERMISSION { IMAPPERM_READ = 0x01, IMAPPERM_READWRITE = 0x02 }; -enum IMAP4_FLAG_ACTIONS { IMAPFA_NONE, IMAPFA_REPLACE, IMAPFA_ADD, IMAPFA_REMOVE }; +enum IMAP4_FLAG_ACTIONS { IMAPFA_NONE, IMAPFA_REPLACE, IMAPFA_ADD, + IMAPFA_REMOVE }; enum BODY_FETCH_ITEM_TYPES { BFIT_TEXT, BFIT_HEADER, BFIT_MIME, - BFIT_HEADER_FIELDS, - BFIT_HEADER_FIELDS_NOT, BFIT_TEXT_SILENT }; + BFIT_HEADER_FIELDS, + BFIT_HEADER_FIELDS_NOT, BFIT_TEXT_SILENT +}; /* maximum size of a mailbox name */ #define IMAP_MAX_MAILBOX_NAMELEN 100 @@ -143,13 +155,12 @@ enum BODY_FETCH_ITEM_TYPES { BFIT_TEXT, BFIT_HEADER, BFIT_MIME, /* * (imap) mailbox data type */ -typedef struct -{ - u64_t uid,msguidnext; - unsigned exists,recent,unseen; - unsigned flags; - int permission; - u64_t *seq_list; +typedef struct { + u64_t uid, msguidnext; + unsigned exists, recent, unseen; + unsigned flags; + int permission; + u64_t *seq_list; } mailbox_t; @@ -157,60 +168,67 @@ typedef struct * search data types */ -enum IMAP_SEARCH_TYPES { IST_SET, IST_SET_UID, IST_FLAG, IST_HDR, - IST_HDRDATE_BEFORE, IST_HDRDATE_ON, IST_HDRDATE_SINCE, - IST_IDATE, IST_DATA_BODY, IST_DATA_TEXT, - IST_SIZE_LARGER, IST_SIZE_SMALLER, IST_SUBSEARCH_AND, - IST_SUBSEARCH_OR, IST_SUBSEARCH_NOT }; - -typedef struct -{ - int type; - u64_t size; - char search[MAX_SEARCH_LEN]; - char hdrfld[MIME_FIELD_MAX]; - struct list sub_search; +enum IMAP_SEARCH_TYPES { IST_SET, IST_SET_UID, IST_FLAG, IST_HDR, + IST_HDRDATE_BEFORE, IST_HDRDATE_ON, IST_HDRDATE_SINCE, + IST_IDATE, IST_DATA_BODY, IST_DATA_TEXT, + IST_SIZE_LARGER, IST_SIZE_SMALLER, IST_SUBSEARCH_AND, + IST_SUBSEARCH_OR, IST_SUBSEARCH_NOT +}; + +typedef struct { + int type; + u64_t size; + char search[MAX_SEARCH_LEN]; + char hdrfld[MIME_FIELD_MAX]; + struct list sub_search; } search_key_t; /** * remembering database positions for mail */ -typedef struct -{ - u64_t block,pos; +typedef struct { + u64_t block, pos; } db_pos_t; /** * RFC822/MIME message data type */ -typedef struct -{ - struct list mimeheader; /**< the MIME header of this part (if present) */ - struct list rfcheader; /**< RFC822 header of this part (if present) */ - int message_has_errors; /**< if set the content-type is meaningless */ - db_pos_t bodystart,bodyend; /**< the body of this part */ - u64_t bodysize; /**< size of message body */ - u64_t bodylines; /**< number of lines in message body */ - u64_t rfcheadersize; /**< size of rfc header */ - struct list children; /**< the children (multipart msg) */ - u64_t rfcheaderlines; /** number of lines in rfc header */ - u64_t mimerfclines; /**< the total number of lines (only specified in +typedef struct { + struct list mimeheader; + /**< the MIME header of this part (if present) */ + struct list rfcheader; + /**< RFC822 header of this part (if present) */ + int message_has_errors; + /**< if set the content-type is meaningless */ + db_pos_t bodystart, bodyend; + /**< the body of this part */ + u64_t bodysize; + /**< size of message body */ + u64_t bodylines; + /**< number of lines in message body */ + u64_t rfcheadersize; + /**< size of rfc header */ + struct list children; + /**< the children (multipart msg) */ + u64_t rfcheaderlines; + /** number of lines in rfc header */ + u64_t mimerfclines; + /**< the total number of lines (only specified in case of a MIME msg containing an RFC822 msg) */ } mime_message_t; - + /* * simple cache mechanism */ -typedef struct -{ - mime_message_t msg; - MEM *memdump,*tmpdump; - u64_t num; - int file_dumped,msg_parsed; - u64_t dumpsize; +typedef struct { + mime_message_t msg; + MEM *memdump, *tmpdump; + u64_t num; + int file_dumped, msg_parsed; + u64_t dumpsize; } cache_t; @@ -218,20 +236,18 @@ typedef struct * structure for basic message info * so it can be retrieved at once */ -typedef struct -{ - int flags[IMAP_NFLAGS]; - char internaldate[IMAP_INTERNALDATE_LEN]; - u64_t rfcsize,uid; +typedef struct { + int flags[IMAP_NFLAGS]; + char internaldate[IMAP_INTERNALDATE_LEN]; + u64_t rfcsize, uid; } msginfo_t; /* * A struct to hold info about a Sieve script */ -typedef struct -{ - char *name; - int active; +typedef struct { + char *name; + int active; } sievescript_info_t; #endif @@ -40,15 +40,15 @@ unsigned char *makemd5(char *buf) unsigned char result[16]; unsigned char *md5hash; int i; - - md5hash=(unsigned char *)my_malloc(33); - - gdm_md5_init (&mycontext); - gdm_md5_update (&mycontext,buf,strlen(buf)); - gdm_md5_final (result,&mycontext); - + + md5hash = (unsigned char *) my_malloc(33); + + gdm_md5_init(&mycontext); + gdm_md5_update(&mycontext, buf, strlen(buf)); + gdm_md5_final(result, &mycontext); + for (i = 0; i < 16; i++) { - sprintf (&md5hash[i*2],"%02x", result[i]); + sprintf(&md5hash[i * 2], "%02x", result[i]); } return md5hash; @@ -52,408 +52,401 @@ static unsigned nblocks = 0; /**< number of block */ int db_init_msgfetch(u64_t msg_idnr) { - u64_t physmessage_id; - - if (db_get_physmessage_id(msg_idnr, &physmessage_id) == -1) { - trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", - __FILE__, __FUNCTION__); - return -1; - } - - msgbuf_buf = (char*) my_malloc (sizeof (char) * MSGBUF_WINDOWSIZE); - if (!msgbuf_buf) - return -1; - - if (_msg_fetch_inited) - return 0; - - snprintf(query, DEF_QUERYSIZE, - "SELECT messageblk FROM messageblks " - "WHERE physmessage_id = '%llu' ORDER BY messageblk_idnr", - physmessage_id); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get message", - __FILE__, __FUNCTION__); - return (-1); - } - - nblocks = db_num_rows(); - - if (nblocks == 0) { - trace(TRACE_ERROR, "%s,%s: message has no blocks", - __FILE__, __FUNCTION__); - db_free_result(); - - return -1; /* msg should have 1 block at least */ - } - - _msg_fetch_inited = 1; - msgbuf_idx = 0; - - /* start at row (tuple) 0 */ - _msgrow_idx = 0; - - /* FIXME: this will explode is db_get_result returns NULL. */ - rowlength = db_get_length(_msgrow_idx, 0); - strncpy(msgbuf_buf, db_get_result(_msgrow_idx, 0), MSGBUF_WINDOWSIZE - 1); - - zeropos.block = 0; - zeropos.pos = 0; - - if (rowlength >= MSGBUF_WINDOWSIZE-1) { - msgbuf_buflen = MSGBUF_WINDOWSIZE-1; - rowpos = MSGBUF_WINDOWSIZE; /* remember store pos */ - msgbuf_buf[msgbuf_buflen] = '\0'; /* terminate buff */ - return 1; /* msgbuf_buf full */ - } - - msgbuf_buflen = rowlength; /* NOTE \0 has been copied from the result set */ - rowpos = rowlength; /* no more to read from this row */ - - _msgrow_idx++; - if (_msgrow_idx >= db_num_rows()) { - rowlength = rowpos = 0; - return 1; - } - - /* FIXME: this will explode is db_get_result returns NULL. */ - rowlength = db_get_length(_msgrow_idx, 0); - rowpos = 0; - strncpy(&msgbuf_buf[msgbuf_buflen], db_get_result(_msgrow_idx, 0), - MSGBUF_WINDOWSIZE - msgbuf_buflen - 1); - - if (rowlength <= MSGBUF_WINDOWSIZE - msgbuf_buflen - 1) { - /* 2nd block fits entirely */ - rowpos = rowlength; - msgbuf_buflen += rowlength; - } else { - rowpos = MSGBUF_WINDOWSIZE - (msgbuf_buflen+1); - msgbuf_buflen = MSGBUF_WINDOWSIZE-1; - } - msgbuf_buf[msgbuf_buflen] = '\0'; /* add NULL */ - - /* store the current result set in db.c as msgbuf_result for - * later use */ - db_store_msgbuf_result(); - return 1; + u64_t physmessage_id; + + if (db_get_physmessage_id(msg_idnr, &physmessage_id) == -1) { + trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", + __FILE__, __FUNCTION__); + return -1; + } + + msgbuf_buf = (char *) my_malloc(sizeof(char) * MSGBUF_WINDOWSIZE); + if (!msgbuf_buf) + return -1; + + if (_msg_fetch_inited) + return 0; + + snprintf(query, DEF_QUERYSIZE, + "SELECT messageblk FROM messageblks " + "WHERE physmessage_id = '%llu' ORDER BY messageblk_idnr", + physmessage_id); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not get message", + __FILE__, __FUNCTION__); + return (-1); + } + + nblocks = db_num_rows(); + + if (nblocks == 0) { + trace(TRACE_ERROR, "%s,%s: message has no blocks", + __FILE__, __FUNCTION__); + db_free_result(); + + return -1; /* msg should have 1 block at least */ + } + + _msg_fetch_inited = 1; + msgbuf_idx = 0; + + /* start at row (tuple) 0 */ + _msgrow_idx = 0; + + /* FIXME: this will explode is db_get_result returns NULL. */ + rowlength = db_get_length(_msgrow_idx, 0); + strncpy(msgbuf_buf, db_get_result(_msgrow_idx, 0), + MSGBUF_WINDOWSIZE - 1); + + zeropos.block = 0; + zeropos.pos = 0; + + if (rowlength >= MSGBUF_WINDOWSIZE - 1) { + msgbuf_buflen = MSGBUF_WINDOWSIZE - 1; + rowpos = MSGBUF_WINDOWSIZE; /* remember store pos */ + msgbuf_buf[msgbuf_buflen] = '\0'; /* terminate buff */ + return 1; /* msgbuf_buf full */ + } + + msgbuf_buflen = rowlength; /* NOTE \0 has been copied from the result set */ + rowpos = rowlength; /* no more to read from this row */ + + _msgrow_idx++; + if (_msgrow_idx >= db_num_rows()) { + rowlength = rowpos = 0; + return 1; + } + + /* FIXME: this will explode is db_get_result returns NULL. */ + rowlength = db_get_length(_msgrow_idx, 0); + rowpos = 0; + strncpy(&msgbuf_buf[msgbuf_buflen], db_get_result(_msgrow_idx, 0), + MSGBUF_WINDOWSIZE - msgbuf_buflen - 1); + + if (rowlength <= MSGBUF_WINDOWSIZE - msgbuf_buflen - 1) { + /* 2nd block fits entirely */ + rowpos = rowlength; + msgbuf_buflen += rowlength; + } else { + rowpos = MSGBUF_WINDOWSIZE - (msgbuf_buflen + 1); + msgbuf_buflen = MSGBUF_WINDOWSIZE - 1; + } + msgbuf_buf[msgbuf_buflen] = '\0'; /* add NULL */ + + /* store the current result set in db.c as msgbuf_result for + * later use */ + db_store_msgbuf_result(); + return 1; } int db_update_msgbuf(int minlen) { - /* use the former msgbuf_result */ - db_use_msgbuf_result(); - - if (_msgrow_idx >= db_num_rows()) { - db_store_msgbuf_result(); - return 0; /* no more */ - } - - if (msgbuf_idx > msgbuf_buflen) { - db_store_msgbuf_result(); - return -1; /* error, msgbuf_idx should be within buf */ - } - - if (minlen > 0 && ((int)(msgbuf_buflen - msgbuf_idx)) > minlen) { - db_store_msgbuf_result(); - return 1; /* ok, need no update */ - } - - if (msgbuf_idx == 0) { - db_store_msgbuf_result(); - return 1; /* update no use, buffer would not change */ - } - - - trace(TRACE_DEBUG, - "%s,%s: update msgbuf_buf updating %llu, %llu, %llu, %llu", - __FILE__, __FUNCTION__, MSGBUF_WINDOWSIZE, - msgbuf_buflen, rowlength, rowpos); - - /* move buf to make msgbuf_idx 0 */ - memmove(msgbuf_buf, &msgbuf_buf[msgbuf_idx], (msgbuf_buflen-msgbuf_idx)); - if (msgbuf_idx > ((msgbuf_buflen+1) - rowpos)) { - zeropos.block++; - zeropos.pos = (msgbuf_idx - ((msgbuf_buflen) - rowpos)); - } else { - zeropos.pos += msgbuf_idx; - } - - msgbuf_buflen -= msgbuf_idx; - msgbuf_idx = 0; - - if ((rowlength-rowpos) >= (MSGBUF_WINDOWSIZE - msgbuf_buflen)) - { - trace(TRACE_DEBUG,"%s,%s update msgbuf non-entire fit", - __FILE__, __FUNCTION__); - - /* rest of row does not fit entirely in buf */ - /* FIXME: this will explode is db_get_result returns NULL. */ - strncpy(&msgbuf_buf[msgbuf_buflen], &( (db_get_result(_msgrow_idx, 0))[rowpos] ), - MSGBUF_WINDOWSIZE - msgbuf_buflen); - rowpos += (MSGBUF_WINDOWSIZE - msgbuf_buflen - 1); - - msgbuf_buflen = MSGBUF_WINDOWSIZE-1; - msgbuf_buf[msgbuf_buflen] = '\0'; - - db_store_msgbuf_result(); - return 1; - } - - trace(TRACE_DEBUG,"%s,%s: update msgbuf: entire fit", - __FILE__, __FUNCTION__); - - /* FIXME: this will explode is db_get_result returns NULL. */ - strncpy(&msgbuf_buf[msgbuf_buflen], &( (db_get_result(_msgrow_idx, 0))[rowpos] ), - (rowlength-rowpos)); - msgbuf_buflen += (rowlength - rowpos); - msgbuf_buf[msgbuf_buflen] = '\0'; - rowpos = rowlength; - - /* try to fetch a new row */ - _msgrow_idx++; - if (_msgrow_idx >= db_num_rows()) - { - trace(TRACE_DEBUG,"%s,%s update msgbuf succes NOMORE", - __FILE__, __FUNCTION__); - db_store_msgbuf_result(); - return 0; - } - - rowlength = db_get_length(_msgrow_idx, 0); - rowpos = 0; - - trace(TRACE_DEBUG,"%s,%s: update msgbuf, got new block, " - "trying to place data", __FILE__, __FUNCTION__); - - /* FIXME: this will explode is db_get_result returns NULL. */ - strncpy(&msgbuf_buf[msgbuf_buflen], db_get_result(_msgrow_idx, 0), - MSGBUF_WINDOWSIZE - msgbuf_buflen - 1); - - if (rowlength <= MSGBUF_WINDOWSIZE - msgbuf_buflen - 1) { - /* 2nd block fits entirely */ - trace(TRACE_DEBUG,"update msgbuf: new block fits entirely\n"); - - rowpos = rowlength; - msgbuf_buflen += rowlength; - } - else - { - rowpos = MSGBUF_WINDOWSIZE - (msgbuf_buflen+1); - msgbuf_buflen = MSGBUF_WINDOWSIZE-1; - } - - msgbuf_buf[msgbuf_buflen] = '\0' ; /* add NULL */ - - trace(TRACE_DEBUG,"%s,%s: update msgbuf succes", __FILE__, __FUNCTION__); - db_store_msgbuf_result(); - return 1; + /* use the former msgbuf_result */ + db_use_msgbuf_result(); + + if (_msgrow_idx >= db_num_rows()) { + db_store_msgbuf_result(); + return 0; /* no more */ + } + + if (msgbuf_idx > msgbuf_buflen) { + db_store_msgbuf_result(); + return -1; /* error, msgbuf_idx should be within buf */ + } + + if (minlen > 0 && ((int) (msgbuf_buflen - msgbuf_idx)) > minlen) { + db_store_msgbuf_result(); + return 1; /* ok, need no update */ + } + + if (msgbuf_idx == 0) { + db_store_msgbuf_result(); + return 1; /* update no use, buffer would not change */ + } + + + trace(TRACE_DEBUG, + "%s,%s: update msgbuf_buf updating %llu, %llu, %llu, %llu", + __FILE__, __FUNCTION__, MSGBUF_WINDOWSIZE, + msgbuf_buflen, rowlength, rowpos); + + /* move buf to make msgbuf_idx 0 */ + memmove(msgbuf_buf, &msgbuf_buf[msgbuf_idx], + (msgbuf_buflen - msgbuf_idx)); + if (msgbuf_idx > ((msgbuf_buflen + 1) - rowpos)) { + zeropos.block++; + zeropos.pos = (msgbuf_idx - ((msgbuf_buflen) - rowpos)); + } else { + zeropos.pos += msgbuf_idx; + } + + msgbuf_buflen -= msgbuf_idx; + msgbuf_idx = 0; + + if ((rowlength - rowpos) >= (MSGBUF_WINDOWSIZE - msgbuf_buflen)) { + trace(TRACE_DEBUG, "%s,%s update msgbuf non-entire fit", + __FILE__, __FUNCTION__); + + /* rest of row does not fit entirely in buf */ + /* FIXME: this will explode is db_get_result returns NULL. */ + strncpy(&msgbuf_buf[msgbuf_buflen], + &((db_get_result(_msgrow_idx, 0))[rowpos]), + MSGBUF_WINDOWSIZE - msgbuf_buflen); + rowpos += (MSGBUF_WINDOWSIZE - msgbuf_buflen - 1); + + msgbuf_buflen = MSGBUF_WINDOWSIZE - 1; + msgbuf_buf[msgbuf_buflen] = '\0'; + + db_store_msgbuf_result(); + return 1; + } + + trace(TRACE_DEBUG, "%s,%s: update msgbuf: entire fit", + __FILE__, __FUNCTION__); + + /* FIXME: this will explode is db_get_result returns NULL. */ + strncpy(&msgbuf_buf[msgbuf_buflen], + &((db_get_result(_msgrow_idx, 0))[rowpos]), + (rowlength - rowpos)); + msgbuf_buflen += (rowlength - rowpos); + msgbuf_buf[msgbuf_buflen] = '\0'; + rowpos = rowlength; + + /* try to fetch a new row */ + _msgrow_idx++; + if (_msgrow_idx >= db_num_rows()) { + trace(TRACE_DEBUG, "%s,%s update msgbuf succes NOMORE", + __FILE__, __FUNCTION__); + db_store_msgbuf_result(); + return 0; + } + + rowlength = db_get_length(_msgrow_idx, 0); + rowpos = 0; + + trace(TRACE_DEBUG, "%s,%s: update msgbuf, got new block, " + "trying to place data", __FILE__, __FUNCTION__); + + /* FIXME: this will explode is db_get_result returns NULL. */ + strncpy(&msgbuf_buf[msgbuf_buflen], db_get_result(_msgrow_idx, 0), + MSGBUF_WINDOWSIZE - msgbuf_buflen - 1); + + if (rowlength <= MSGBUF_WINDOWSIZE - msgbuf_buflen - 1) { + /* 2nd block fits entirely */ + trace(TRACE_DEBUG, + "update msgbuf: new block fits entirely\n"); + + rowpos = rowlength; + msgbuf_buflen += rowlength; + } else { + rowpos = MSGBUF_WINDOWSIZE - (msgbuf_buflen + 1); + msgbuf_buflen = MSGBUF_WINDOWSIZE - 1; + } + + msgbuf_buf[msgbuf_buflen] = '\0'; /* add NULL */ + + trace(TRACE_DEBUG, "%s,%s: update msgbuf succes", __FILE__, + __FUNCTION__); + db_store_msgbuf_result(); + return 1; } void db_close_msgfetch() { - if (!_msg_fetch_inited) - return; /* nothing to be done */ - - my_free(msgbuf_buf); - msgbuf_buf = NULL; - - nblocks = 0; - /* make sure the right result set is freed and restore the - * old one after that.*/ - db_use_msgbuf_result(); - db_free_result(); - db_store_msgbuf_result(); - _msg_fetch_inited = 0; + if (!_msg_fetch_inited) + return; /* nothing to be done */ + + my_free(msgbuf_buf); + msgbuf_buf = NULL; + + nblocks = 0; + /* make sure the right result set is freed and restore the + * old one after that.*/ + db_use_msgbuf_result(); + db_free_result(); + db_store_msgbuf_result(); + _msg_fetch_inited = 0; } -void db_give_msgpos(db_pos_t *pos) +void db_give_msgpos(db_pos_t * pos) { - if (msgbuf_idx >= ((msgbuf_buflen)-rowpos)) - { - pos->block = zeropos.block+1; - pos->pos = msgbuf_idx - ((msgbuf_buflen)-rowpos); - } - else - { - pos->block = zeropos.block; - pos->pos = zeropos.pos + msgbuf_idx; - } + if (msgbuf_idx >= ((msgbuf_buflen) - rowpos)) { + pos->block = zeropos.block + 1; + pos->pos = msgbuf_idx - ((msgbuf_buflen) - rowpos); + } else { + pos->block = zeropos.block; + pos->pos = zeropos.pos + msgbuf_idx; + } } -u64_t db_give_range_size(db_pos_t *start, db_pos_t *end) +u64_t db_give_range_size(db_pos_t * start, db_pos_t * end) { - unsigned i; - u64_t size; + unsigned i; + u64_t size; - if (start->block > end->block) - return 0; /* bad range */ + if (start->block > end->block) + return 0; /* bad range */ - if (start->block >= nblocks || end->block >= nblocks) - return 0; /* bad range */ + if (start->block >= nblocks || end->block >= nblocks) + return 0; /* bad range */ - if (start->block == end->block) - return (start->pos > end->pos) ? 0 : (end->pos - start->pos+1); + if (start->block == end->block) + return (start->pos > + end->pos) ? 0 : (end->pos - start->pos + 1); - /* use the former msgbuf result */ - db_use_msgbuf_result(); + /* use the former msgbuf result */ + db_use_msgbuf_result(); - if (start->pos > db_get_length(start->block, 0) || - end->pos > db_get_length(end->block, 0)) { - db_store_msgbuf_result(); - return 0; /* bad range */ - } + if (start->pos > db_get_length(start->block, 0) || + end->pos > db_get_length(end->block, 0)) { + db_store_msgbuf_result(); + return 0; /* bad range */ + } - size = db_get_length(start->block, 0) - start->pos; + size = db_get_length(start->block, 0) - start->pos; - for (i = start->block+1; i<end->block; i++) - size += db_get_length(i, 0); + for (i = start->block + 1; i < end->block; i++) + size += db_get_length(i, 0); - size += end->pos; - size++; + size += end->pos; + size++; - /* store the result again.. */ - db_store_msgbuf_result(); - return size; + /* store the result again.. */ + db_store_msgbuf_result(); + return size; } -long db_dump_range(MEM *outmem, db_pos_t start, +long db_dump_range(MEM * outmem, db_pos_t start, db_pos_t end, u64_t msg_idnr) { - u64_t physmessage_id; - u64_t i,startpos,endpos,j,bufcnt; - u64_t outcnt; - u64_t distance; - char buf[DUMP_BUF_SIZE]; - char *field; - - if (db_get_physmessage_id(msg_idnr, &physmessage_id) == -1) { - trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", - __FILE__, __FUNCTION__); - return -1; - } - - trace(TRACE_DEBUG, - "%s,%s: Dumping range: (%llu,%llu) - (%llu,%llu)", - __FILE__, __FUNCTION__, - start.block, start.pos, end.block, end.pos); - - if (start.block > end.block) { - trace(TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - return -1; - } - - if (start.block == end.block && start.pos > end.pos) { - trace(TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - return -1; - } - - snprintf(query, DEF_QUERYSIZE, - "SELECT messageblk FROM messageblks " - "WHERE physmessage_id = '%llu' " - "ORDER BY messageblk_idnr", - physmessage_id); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get message", - __FILE__, __FUNCTION__); - return (-1); - } - - if (start.block >= db_num_rows()) { - trace(TRACE_ERROR,"db_dump_range(): bad range specified\n"); - db_free_result(); - return -1; - } - - outcnt = 0; - - /* just one block? */ - if (start.block == end.block) - { - /* dump everything */ - bufcnt = 0; - field = db_get_result(start.block, 0); - - for (i=start.pos; i<=end.pos; i++) - { - if (bufcnt >= DUMP_BUF_SIZE-1) - { - outcnt += mwrite(buf, bufcnt, outmem); - bufcnt = 0; - } - - /* FIXME: field may be NULL from db_get_result! */ - if (field[i] == '\n' && - !(i > 0 && field[i-1] == '\r')) - { - trace(TRACE_DEBUG,"%s,%s: adding '\r' to buf", - __FILE__, __FUNCTION__); - buf[bufcnt++] = '\r'; - buf[bufcnt++] = '\n'; - } - else - buf[bufcnt++] = field[i]; - } - - outcnt += mwrite(buf, bufcnt, outmem); - bufcnt = 0; - - db_free_result(); - return outcnt; - } - - - /* - * multiple block range specified - */ - - for (i=start.block, outcnt=0; i<=end.block; i++) - { - if (i >= db_num_rows()) - { - trace(TRACE_ERROR,"db_dump_range(): bad range specified\n"); - db_free_result(); - return -1; - } - - startpos = (i == start.block) ? start.pos : 0; - endpos = (i == end.block) ? end.pos+1 : db_get_length(i, 0); - - distance = endpos - startpos; - - /* output */ - bufcnt = 0; - field = db_get_result(i, 0); - - for (j=0; j<distance; j++) - { - if (bufcnt >= DUMP_BUF_SIZE-1) - { - outcnt += mwrite(buf, bufcnt, outmem); - bufcnt = 0; - } - - /* FIXME: field may be NULL from db_get_result! */ - if (field[startpos+j] == '\n' && - !(j > 0 && field[startpos + j - 1] == '\r')) - { - trace(TRACE_DEBUG,"%s,%s: adding '\r' to buf", - __FILE__, __FUNCTION__); - - buf[bufcnt++] = '\r'; - buf[bufcnt++] = '\n'; - } - else if (field[startpos+j]) - buf[bufcnt++] = field[startpos+j]; - } - outcnt += mwrite(buf, bufcnt, outmem); - bufcnt = 0; - } - - db_free_result(); - - return outcnt; + u64_t physmessage_id; + u64_t i, startpos, endpos, j, bufcnt; + u64_t outcnt; + u64_t distance; + char buf[DUMP_BUF_SIZE]; + char *field; + + if (db_get_physmessage_id(msg_idnr, &physmessage_id) == -1) { + trace(TRACE_ERROR, "%s,%s: error getting physmessage_id", + __FILE__, __FUNCTION__); + return -1; + } + + trace(TRACE_DEBUG, + "%s,%s: Dumping range: (%llu,%llu) - (%llu,%llu)", + __FILE__, __FUNCTION__, + start.block, start.pos, end.block, end.pos); + + if (start.block > end.block) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + return -1; + } + + if (start.block == end.block && start.pos > end.pos) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + return -1; + } + + snprintf(query, DEF_QUERYSIZE, + "SELECT messageblk FROM messageblks " + "WHERE physmessage_id = '%llu' " + "ORDER BY messageblk_idnr", physmessage_id); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not get message", + __FILE__, __FUNCTION__); + return (-1); + } + + if (start.block >= db_num_rows()) { + trace(TRACE_ERROR, + "db_dump_range(): bad range specified\n"); + db_free_result(); + return -1; + } + + outcnt = 0; + + /* just one block? */ + if (start.block == end.block) { + /* dump everything */ + bufcnt = 0; + field = db_get_result(start.block, 0); + + for (i = start.pos; i <= end.pos; i++) { + if (bufcnt >= DUMP_BUF_SIZE - 1) { + outcnt += mwrite(buf, bufcnt, outmem); + bufcnt = 0; + } + + /* FIXME: field may be NULL from db_get_result! */ + if (field[i] == '\n' && + !(i > 0 && field[i - 1] == '\r')) { + trace(TRACE_DEBUG, + "%s,%s: adding '\r' to buf", + __FILE__, __FUNCTION__); + buf[bufcnt++] = '\r'; + buf[bufcnt++] = '\n'; + } else + buf[bufcnt++] = field[i]; + } + + outcnt += mwrite(buf, bufcnt, outmem); + bufcnt = 0; + + db_free_result(); + return outcnt; + } + + + /* + * multiple block range specified + */ + + for (i = start.block, outcnt = 0; i <= end.block; i++) { + if (i >= db_num_rows()) { + trace(TRACE_ERROR, + "db_dump_range(): bad range specified\n"); + db_free_result(); + return -1; + } + + startpos = (i == start.block) ? start.pos : 0; + endpos = + (i == end.block) ? end.pos + 1 : db_get_length(i, 0); + + distance = endpos - startpos; + + /* output */ + bufcnt = 0; + field = db_get_result(i, 0); + + for (j = 0; j < distance; j++) { + if (bufcnt >= DUMP_BUF_SIZE - 1) { + outcnt += mwrite(buf, bufcnt, outmem); + bufcnt = 0; + } + + /* FIXME: field may be NULL from db_get_result! */ + if (field[startpos + j] == '\n' && + !(j > 0 && field[startpos + j - 1] == '\r')) { + trace(TRACE_DEBUG, + "%s,%s: adding '\r' to buf", + __FILE__, __FUNCTION__); + + buf[bufcnt++] = '\r'; + buf[bufcnt++] = '\n'; + } else if (field[startpos + j]) + buf[bufcnt++] = field[startpos + j]; + } + outcnt += mwrite(buf, bufcnt, outmem); + bufcnt = 0; + } + + db_free_result(); + + return outcnt; } @@ -62,7 +62,7 @@ int db_init_msgfetch(u64_t msg_idnr); * - 0 if no more chars in rows * - 1 on success */ -int db_update_msgbuf(int minlen);\ +int db_update_msgbuf(int minlen); /** * \brief finishes a message fetch @@ -73,7 +73,7 @@ void db_close_msgfetch(void); * \brief get position in message * \param pos pointer to db_pos_t which will hold the position */ -void db_give_msgpos(db_pos_t *pos); +void db_give_msgpos(db_pos_t * pos); /** * \brief determines number of bytes between start and end position @@ -83,7 +83,7 @@ void db_give_msgpos(db_pos_t *pos); * \pre _msg_result must contain a valid result set for return value * to be valid */ -u64_t db_give_range_size(db_pos_t *start, db_pos_t *end); +u64_t db_give_range_size(db_pos_t * start, db_pos_t * end); /** * \brief dump a range specified by start,end for the message with @@ -96,7 +96,7 @@ u64_t db_give_range_size(db_pos_t *start, db_pos_t *end); * - -1 on error * - number of bytes written to outmem otherwise */ -long db_dump_range(MEM *outmem, db_pos_t start, db_pos_t end, u64_t msg_idnr); +long db_dump_range(MEM * outmem, db_pos_t start, db_pos_t end, + u64_t msg_idnr); #endif - @@ -26,7 +26,7 @@ * dbpgsql.c (PostgreSQL) and dbmysql (MySQL), but have * been made backend-independent, so they can be used * by any SQL database. - */ + */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -44,10 +44,9 @@ /** * abbreviated names of the months */ -const char *month_desc[]= -{ - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +const char *month_desc[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; @@ -61,7 +60,7 @@ const char *month_desc[]= * - -1 if not found * - index of key in array if found */ -static int db_binary_search(const u64_t *array, int arraysize, u64_t key); +static int db_binary_search(const u64_t * array, int arraysize, u64_t key); /** * \brief perform search on on the body of a message * \param msg mime_message_t struct of message @@ -71,8 +70,8 @@ static int db_binary_search(const u64_t *array, int arraysize, u64_t key); * - 0 if no match * - 1 if match */ -static int db_exec_search(mime_message_t *msg, search_key_t *sk, - u64_t msg_idnr); +static int db_exec_search(mime_message_t * msg, search_key_t * sk, + u64_t msg_idnr); /** * \brief search the specified range of a message for a key @@ -85,7 +84,7 @@ static int db_exec_search(mime_message_t *msg, search_key_t *sk, * - 1 if found */ static int db_search_range(db_pos_t start, db_pos_t end, const char *key, - u64_t msg_idnr); + u64_t msg_idnr); /** * \brief converts an IMAP date to a number (strictly ascending in date) * valid IMAP dates: @@ -96,243 +95,240 @@ static int db_search_range(db_pos_t start, db_pos_t end, const char *key, */ static int num_from_imapdate(const char *date); -int db_search(int *rset, int setlen, const char *key, mailbox_t *mb, int type) +int db_search(int *rset, int setlen, const char *key, mailbox_t * mb, + int type) { - u64_t uid; - int msn; - unsigned i; + u64_t uid; + int msn; + unsigned i; - if (!key) - return -2; + if (!key) + return -2; - memset(rset, 0, setlen * sizeof(int)); + memset(rset, 0, setlen * sizeof(int)); - if (type == IST_IDATE) { + if (type == IST_IDATE) { /** \todo this next solution (pms.%s) is really dirty. If anything, the IMAP search algorithm is dirty, and should be fixed */ - snprintf(query, DEF_QUERYSIZE, - "SELECT msg.message_idnr FROM messages msg, physmessage pms " - "WHERE msg.mailbox_idnr = '%llu' " - "AND msg.physmessage_id = pms.id " - "AND msg.status < 2 " - "AND msg.unique_id <> '' " - "AND pms.%s", mb->uid, key); - } else { - snprintf(query, DEF_QUERYSIZE, - "SELECT message_idnr FROM messages " - "WHERE mailbox_idnr = '%llu' " - "AND status<2 AND unique_id!='' AND %s", mb->uid, key); - } - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not execute query", - __FILE__, __FUNCTION__); - return (-1); - } - - for (i = 0; i < db_num_rows(); i++) { - uid = db_get_result_u64(i, 0); - msn = db_binary_search(mb->seq_list, mb->exists, uid); - - if (msn == -1 || msn >= setlen) { - db_free_result(); - return 1; - } - - rset[msn] = 1; - } - - db_free_result(); - return 0; + snprintf(query, DEF_QUERYSIZE, + "SELECT msg.message_idnr FROM messages msg, physmessage pms " + "WHERE msg.mailbox_idnr = '%llu' " + "AND msg.physmessage_id = pms.id " + "AND msg.status < 2 " + "AND msg.unique_id <> '' " + "AND pms.%s", mb->uid, key); + } else { + snprintf(query, DEF_QUERYSIZE, + "SELECT message_idnr FROM messages " + "WHERE mailbox_idnr = '%llu' " + "AND status<2 AND unique_id!='' AND %s", mb->uid, + key); + } + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not execute query", + __FILE__, __FUNCTION__); + return (-1); + } + + for (i = 0; i < db_num_rows(); i++) { + uid = db_get_result_u64(i, 0); + msn = db_binary_search(mb->seq_list, mb->exists, uid); + + if (msn == -1 || msn >= setlen) { + db_free_result(); + return 1; + } + + rset[msn] = 1; + } + + db_free_result(); + return 0; } -int db_search_parsed(int *rset, unsigned int setlen, - search_key_t *sk, mailbox_t *mb) +int db_search_parsed(int *rset, unsigned int setlen, + search_key_t * sk, mailbox_t * mb) { - unsigned i; - int result; - mime_message_t msg; - - if (mb->exists != setlen) - return 1; - - memset(rset, 0, sizeof(int)*setlen); - - for (i=0; i<setlen; i++) - { - memset(&msg, 0, sizeof(msg)); - - result = db_fetch_headers(mb->seq_list[i], &msg); - if (result != 0) - continue; /* ignore parse errors */ - - if (sk->type == IST_SIZE_LARGER) - { - rset[i] = - ((msg.rfcheadersize + msg.bodylines + msg.bodysize) > sk->size) - ? 1 : 0; - } - else if (sk->type == IST_SIZE_SMALLER) - { - rset[i] = - ((msg.rfcheadersize + msg.bodylines + msg.bodysize) < sk->size) - ? 1 : 0; - } - else - { - rset[i] = db_exec_search(&msg, sk, mb->seq_list[i]); - } - - db_free_msg(&msg); - } - return 0; + unsigned i; + int result; + mime_message_t msg; + + if (mb->exists != setlen) + return 1; + + memset(rset, 0, sizeof(int) * setlen); + + for (i = 0; i < setlen; i++) { + memset(&msg, 0, sizeof(msg)); + + result = db_fetch_headers(mb->seq_list[i], &msg); + if (result != 0) + continue; /* ignore parse errors */ + + if (sk->type == IST_SIZE_LARGER) { + rset[i] = + ((msg.rfcheadersize + msg.bodylines + + msg.bodysize) > sk->size) + ? 1 : 0; + } else if (sk->type == IST_SIZE_SMALLER) { + rset[i] = + ((msg.rfcheadersize + msg.bodylines + + msg.bodysize) < sk->size) + ? 1 : 0; + } else { + rset[i] = + db_exec_search(&msg, sk, mb->seq_list[i]); + } + + db_free_msg(&msg); + } + return 0; } -int db_binary_search(const u64_t *array, int arraysize, u64_t key) +int db_binary_search(const u64_t * array, int arraysize, u64_t key) { - int low,high,mid; - - low = 0; - high = arraysize-1; - - while (low <= high) - { - mid = (high+low)/2; - if (array[mid] < key) - low = mid+1; - else if (array[mid] > key) - high = mid-1; - else - return mid; - } - - return -1; /* not found */ + int low, high, mid; + + low = 0; + high = arraysize - 1; + + while (low <= high) { + mid = (high + low) / 2; + if (array[mid] < key) + low = mid + 1; + else if (array[mid] > key) + high = mid - 1; + else + return mid; + } + + return -1; /* not found */ } -int db_exec_search(mime_message_t *msg, search_key_t *sk, u64_t msg_idnr) +int db_exec_search(mime_message_t * msg, search_key_t * sk, u64_t msg_idnr) { struct element *el; struct mime_record *mr; - int i,givendate,sentdate; + int i, givendate, sentdate; if (!sk->search) return 0; - switch (sk->type) - { - case IST_HDR: - if (list_getstart(&msg->mimeheader)) - { - mime_findfield(sk->hdrfld, &msg->mimeheader, &mr); - if (mr) - { - for (i=0; mr->value[i]; i++) - if (strncasecmp(&mr->value[i], sk->search, - strlen(sk->search)) == 0) - return 1; - } + switch (sk->type) { + case IST_HDR: + if (list_getstart(&msg->mimeheader)) { + mime_findfield(sk->hdrfld, &msg->mimeheader, &mr); + if (mr) { + for (i = 0; mr->value[i]; i++) + if (strncasecmp + (&mr->value[i], sk->search, + strlen(sk->search)) == 0) + return 1; } - if (list_getstart(&msg->rfcheader)) - { - mime_findfield(sk->hdrfld, &msg->rfcheader, &mr); - if (mr) - { - for (i=0; mr->value[i]; i++) - if (strncasecmp(&mr->value[i], sk->search, - strlen(sk->search)) == 0) - return 1; - } + } + if (list_getstart(&msg->rfcheader)) { + mime_findfield(sk->hdrfld, &msg->rfcheader, &mr); + if (mr) { + for (i = 0; mr->value[i]; i++) + if (strncasecmp + (&mr->value[i], sk->search, + strlen(sk->search)) == 0) + return 1; } - - break; - - case IST_HDRDATE_BEFORE: - case IST_HDRDATE_ON: - case IST_HDRDATE_SINCE: - /* do not check children */ - if (list_getstart(&msg->rfcheader)) + } + + break; + + case IST_HDRDATE_BEFORE: + case IST_HDRDATE_ON: + case IST_HDRDATE_SINCE: + /* do not check children */ + if (list_getstart(&msg->rfcheader)) { + mime_findfield("date", &msg->rfcheader, &mr); + if (mr + && strlen(mr->value) >= + strlen("Day, d mon yyyy ")) + /* 01234567890123456 */ { - mime_findfield("date", &msg->rfcheader, &mr); - if (mr && strlen(mr->value) >= strlen("Day, d mon yyyy ")) - /* 01234567890123456 */ - { - givendate = num_from_imapdate(sk->search); - - if (mr->value[6] == ' ') - mr->value[15] = 0; - else - mr->value[16] = 0; - - sentdate = num_from_imapdate(&mr->value[5]); - - switch (sk->type) - { - case IST_HDRDATE_BEFORE: return sentdate < givendate; - case IST_HDRDATE_ON: return sentdate == givendate; - case IST_HDRDATE_SINCE: return sentdate > givendate; - } + givendate = num_from_imapdate(sk->search); + + if (mr->value[6] == ' ') + mr->value[15] = 0; + else + mr->value[16] = 0; + + sentdate = + num_from_imapdate(&mr->value[5]); + + switch (sk->type) { + case IST_HDRDATE_BEFORE: + return sentdate < givendate; + case IST_HDRDATE_ON: + return sentdate == givendate; + case IST_HDRDATE_SINCE: + return sentdate > givendate; } } - return 0; + } + return 0; - case IST_DATA_TEXT: - el = list_getstart(&msg->rfcheader); - while (el) - { - mr = (struct mime_record*)el->data; + case IST_DATA_TEXT: + el = list_getstart(&msg->rfcheader); + while (el) { + mr = (struct mime_record *) el->data; - for (i=0; mr->field[i]; i++) - if (strncasecmp(&mr->field[i], sk->search, - strlen(sk->search)) == 0) - return 1; + for (i = 0; mr->field[i]; i++) + if (strncasecmp(&mr->field[i], sk->search, + strlen(sk->search)) == 0) + return 1; - for (i=0; mr->value[i]; i++) - if (strncasecmp(&mr->value[i], sk->search, - strlen(sk->search)) == 0) - return 1; + for (i = 0; mr->value[i]; i++) + if (strncasecmp(&mr->value[i], sk->search, + strlen(sk->search)) == 0) + return 1; - el = el->nextnode; - } + el = el->nextnode; + } - el = list_getstart(&msg->mimeheader); - while (el) - { - mr = (struct mime_record*)el->data; + el = list_getstart(&msg->mimeheader); + while (el) { + mr = (struct mime_record *) el->data; - for (i=0; mr->field[i]; i++) - if (strncasecmp(&mr->field[i], sk->search, - strlen(sk->search)) == 0) - return 1; + for (i = 0; mr->field[i]; i++) + if (strncasecmp(&mr->field[i], sk->search, + strlen(sk->search)) == 0) + return 1; - for (i=0; mr->value[i]; i++) - if (strncasecmp(&mr->value[i], sk->search, - strlen(sk->search)) == 0) - return 1; + for (i = 0; mr->value[i]; i++) + if (strncasecmp(&mr->value[i], sk->search, + strlen(sk->search)) == 0) + return 1; - el = el->nextnode; - } + el = el->nextnode; + } - case IST_DATA_BODY: - /* only check body if there are no children */ - if (list_getstart(&msg->children)) - break; + case IST_DATA_BODY: + /* only check body if there are no children */ + if (list_getstart(&msg->children)) + break; - /* only check text bodies */ - mime_findfield("content-type", &msg->mimeheader, &mr); - if (mr && strncasecmp(mr->value, "text", 4) != 0) - break; + /* only check text bodies */ + mime_findfield("content-type", &msg->mimeheader, &mr); + if (mr && strncasecmp(mr->value, "text", 4) != 0) + break; - mime_findfield("content-type", &msg->rfcheader, &mr); - if (mr && strncasecmp(mr->value, "text", 4) != 0) - break; + mime_findfield("content-type", &msg->rfcheader, &mr); + if (mr && strncasecmp(mr->value, "text", 4) != 0) + break; - return db_search_range(msg->bodystart, msg->bodyend, sk->search, - msg_idnr); - } + return db_search_range(msg->bodystart, msg->bodyend, + sk->search, msg_idnr); + } /* no match found yet, try the children */ el = list_getstart(&msg->children); - while (el) - { - if (db_exec_search((mime_message_t*)el->data, sk, msg_idnr) == 1) + while (el) { + if (db_exec_search + ((mime_message_t *) el->data, sk, msg_idnr) == 1) return 1; el = el->nextnode; @@ -340,145 +336,135 @@ int db_exec_search(mime_message_t *msg, search_key_t *sk, u64_t msg_idnr) return 0; } -int db_search_range(db_pos_t start, db_pos_t end, - const char *key, u64_t msg_idnr) +int db_search_range(db_pos_t start, db_pos_t end, + const char *key, u64_t msg_idnr) { - unsigned i,j; - unsigned startpos, endpos; - int distance; - - char *query_result; - - if (start.block > end.block) { - trace(TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - return 0; - } - - if (start.block == end.block && start.pos > end.pos) - { - trace(TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - return 0; - } - - snprintf(query, DEF_QUERYSIZE, - "SELECT messageblk FROM messageblks " - "WHERE message_idnr = '%llu' " - "ORDER BY messageblk_idnr", - msg_idnr); - - if (db_query(query) == -1) { - trace(TRACE_ERROR, "%s,%s: could not get message", - __FILE__, __FUNCTION__); - return 0; - } - - if (db_num_rows() == 0) { - trace (TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } - - query_result = db_get_result(start.block, 0); - - if (!query_result) { - trace(TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } - - /* just one block? */ - if (start.block == end.block) - { - for (i=start.pos; i <= end.pos - strlen(key); i++) - { - if (strncasecmp(&query_result[i], key, strlen(key)) == 0) - { + unsigned i, j; + unsigned startpos, endpos; + int distance; + + char *query_result; + + if (start.block > end.block) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + return 0; + } + + if (start.block == end.block && start.pos > end.pos) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + return 0; + } + + snprintf(query, DEF_QUERYSIZE, + "SELECT messageblk FROM messageblks " + "WHERE message_idnr = '%llu' " + "ORDER BY messageblk_idnr", msg_idnr); + + if (db_query(query) == -1) { + trace(TRACE_ERROR, "%s,%s: could not get message", + __FILE__, __FUNCTION__); + return 0; + } + + if (db_num_rows() == 0) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + + query_result = db_get_result(start.block, 0); + + if (!query_result) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + + /* just one block? */ + if (start.block == end.block) { + for (i = start.pos; i <= end.pos - strlen(key); i++) { + if (strncasecmp(&query_result[i], key, strlen(key)) + == 0) { db_free_result(); - return 1; - } - } - - db_free_result(); - return 0; - } - - - /* - * multiple block range specified - */ - - for (i=start.block; i<=end.block; i++) - { - if (!query_result) - { - trace(TRACE_ERROR,"%s,%s: bad range specified", - __FILE__, __FUNCTION__); - db_free_result(); - return 0; - } - - startpos = (i == start.block) ? start.pos : 0; - endpos = (i == end.block) ? end.pos+1 : db_get_length(i, 0); - - distance = endpos - startpos; - - for (j=0; j<distance-strlen(key); j++) - { - if (strncasecmp(&query_result[i], key, strlen(key)) == 0) - { + return 1; + } + } + + db_free_result(); + return 0; + } + + + /* + * multiple block range specified + */ + + for (i = start.block; i <= end.block; i++) { + if (!query_result) { + trace(TRACE_ERROR, "%s,%s: bad range specified", + __FILE__, __FUNCTION__); + db_free_result(); + return 0; + } + + startpos = (i == start.block) ? start.pos : 0; + endpos = + (i == end.block) ? end.pos + 1 : db_get_length(i, 0); + + distance = endpos - startpos; + + for (j = 0; j < distance - strlen(key); j++) { + if (strncasecmp(&query_result[i], key, strlen(key)) + == 0) { db_free_result(); - return 1; - } - } + return 1; + } + } - query_result = db_get_result(i, 0); /* fetch next row */ - } + query_result = db_get_result(i, 0); /* fetch next row */ + } - db_free_result(); + db_free_result(); - return 0; + return 0; } int num_from_imapdate(const char *date) { - int j=0,i; - char datenum[] = "YYYYMMDD"; - char sub[4]; - - if (date[1] == ' ' || date[1] == '-') - j = 1; - - strncpy(datenum, &date[7-j], 4); - - strncpy(sub, &date[3-j], 3); - sub[3] = 0; - - for (i=0; i<12; i++) - { - if (strcasecmp(sub, month_desc[i]) == 0) - break; - } - - i++; - if (i > 12) - i = 12; - - sprintf(&datenum[4], "%02d", i); - - if (j) - { - datenum[6] = '0'; - datenum[7] = date[0]; - } - else - { - datenum[6] = date[0]; - datenum[7] = date[1]; - } - - return atoi(datenum); + int j = 0, i; + char datenum[] = "YYYYMMDD"; + char sub[4]; + + if (date[1] == ' ' || date[1] == '-') + j = 1; + + strncpy(datenum, &date[7 - j], 4); + + strncpy(sub, &date[3 - j], 3); + sub[3] = 0; + + for (i = 0; i < 12; i++) { + if (strcasecmp(sub, month_desc[i]) == 0) + break; + } + + i++; + if (i > 12) + i = 12; + + sprintf(&datenum[4], "%02d", i); + + if (j) { + datenum[6] = '0'; + datenum[7] = date[0]; + } else { + datenum[6] = date[0]; + datenum[7] = date[1]; + } + + return atoi(datenum); } @@ -53,7 +53,7 @@ * - 1 on synchronisation error. (search returned a UID which was not * in the MSN-list -> mailbox should be updated */ -int db_search(int *rset, int setlen, const char *key, mailbox_t *mb, +int db_search(int *rset, int setlen, const char *key, mailbox_t * mb, int type); /** @@ -67,7 +67,7 @@ int db_search(int *rset, int setlen, const char *key, mailbox_t *mb, * - 0 on success * - 1 if mb->exists != setlen */ -int db_search_parsed(int *rset, unsigned setlen, - search_key_t *sk, mailbox_t *mb); +int db_search_parsed(int *rset, unsigned setlen, + search_key_t * sk, mailbox_t * mb); #endif @@ -30,26 +30,25 @@ #include "debug.h" #include <string.h> -struct debug_mem -{ - long addr; - int linenr; - char fname[200]; +struct debug_mem { + long addr; + int linenr; + char fname[200]; - struct debug_mem *nextaddr; + struct debug_mem *nextaddr; }; typedef struct debug_mem debug_mem_t; -debug_mem_t *__dm_first=0,*__dm_last=0; +debug_mem_t *__dm_first = 0, *__dm_last = 0; #define err_out_stream stderr #define EXIT_CODE 75 /* the debug variables */ -int TRACE_TO_SYSLOG = 1; /* default: yes */ -int TRACE_VERBOSE = 0; /* default: no */ -int TRACE_LEVEL = 2; /* default: error operations */ +int TRACE_TO_SYSLOG = 1; /* default: yes */ +int TRACE_VERBOSE = 0; /* default: no */ +int TRACE_LEVEL = 2; /* default: error operations */ /* @@ -57,157 +56,146 @@ int TRACE_LEVEL = 2; /* default: error operations */ */ void configure_debug(trace_t level, int trace_syslog, int trace_verbose) { - TRACE_LEVEL = level; - TRACE_TO_SYSLOG = trace_syslog; - TRACE_VERBOSE = trace_verbose; + TRACE_LEVEL = level; + TRACE_TO_SYSLOG = trace_syslog; + TRACE_VERBOSE = trace_verbose; } -void func_memtst(const char *filename,int line,int tst) +void func_memtst(const char *filename, int line, int tst) { - if (tst != 0) - trace(TRACE_FATAL,"func_memtst(): fatal: %s:%d Memory error, result should not be NULL)", - filename,line); + if (tst != 0) + trace(TRACE_FATAL, + "func_memtst(): fatal: %s:%d Memory error, result should not be NULL)", + filename, line); } void trace(trace_t level, char *formatstring, ...) { - va_list argp; - - va_start(argp, formatstring); - - if (level <= TRACE_LEVEL) - { - if (TRACE_VERBOSE != 0) - { - vfprintf (err_out_stream, formatstring, argp); - if (formatstring[strlen(formatstring)]!='\n') - fprintf (err_out_stream,"\n"); - } - if (TRACE_TO_SYSLOG != 0) - { - if (formatstring[strlen(formatstring)]=='\n') - formatstring[strlen(formatstring)]='\0'; - if (level <= TRACE_WARNING) - { - /* set LOG_ALERT at warnings */ - vsyslog (LOG_ALERT, formatstring, argp); - } - else - vsyslog (LOG_NOTICE, formatstring, argp); + va_list argp; + + va_start(argp, formatstring); + + if (level <= TRACE_LEVEL) { + if (TRACE_VERBOSE != 0) { + vfprintf(err_out_stream, formatstring, argp); + if (formatstring[strlen(formatstring)] != '\n') + fprintf(err_out_stream, "\n"); + } + if (TRACE_TO_SYSLOG != 0) { + if (formatstring[strlen(formatstring)] == '\n') + formatstring[strlen(formatstring)] = '\0'; + if (level <= TRACE_WARNING) { + /* set LOG_ALERT at warnings */ + vsyslog(LOG_ALERT, formatstring, argp); + } else + vsyslog(LOG_NOTICE, formatstring, argp); + } + va_end(argp); } - va_end(argp); - } - /* very big fatal error - * bailout */ - - if (level == TRACE_FATAL) - exit(EXIT_CODE); + /* very big fatal error + * bailout */ + + if (level == TRACE_FATAL) + exit(EXIT_CODE); - if (level == TRACE_STOP) - exit(EXIT_CODE); + if (level == TRACE_STOP) + exit(EXIT_CODE); } -void* __debug_malloc(unsigned long size, const char *fname, int linenr) +void *__debug_malloc(unsigned long size, const char *fname, int linenr) { - void *ptr = malloc(size); - debug_mem_t *new; - - if (!ptr) - return NULL; - - new = (debug_mem_t*)malloc(sizeof(debug_mem_t)); - - if (!new) - { - trace(TRACE_WARNING,"__debug_malloc(): could not add malloc to list (call: %s, %d)\n", - fname,linenr); - return ptr; - } - - new->addr = (long)ptr; - new->linenr = linenr; - if (fname) - strncpy(new->fname, fname, 200); - else - new->fname[0] = 0; - - new->fname[199]= 0; - new->nextaddr = 0; - - if (!__dm_first) - { - __dm_first = new; - __dm_last = new; - } - else - { - __dm_last->nextaddr = new; - __dm_last = new; - } - - return ptr; + void *ptr = malloc(size); + debug_mem_t *new; + + if (!ptr) + return NULL; + + new = (debug_mem_t *) malloc(sizeof(debug_mem_t)); + + if (!new) { + trace(TRACE_WARNING, + "__debug_malloc(): could not add malloc to list (call: %s, %d)\n", + fname, linenr); + return ptr; + } + + new->addr = (long) ptr; + new->linenr = linenr; + if (fname) + strncpy(new->fname, fname, 200); + else + new->fname[0] = 0; + + new->fname[199] = 0; + new->nextaddr = 0; + + if (!__dm_first) { + __dm_first = new; + __dm_last = new; + } else { + __dm_last->nextaddr = new; + __dm_last = new; + } + + return ptr; } - + void __debug_free(void *ptr, const char *fname, int linenr) { - debug_mem_t *curr = __dm_first,*prev = NULL; - - if (!ptr) - return; - - while (curr && curr->addr != (long)ptr) - { - prev = curr; - curr = curr->nextaddr; - } - - if (!curr) - { - trace(TRACE_WARNING,"__debug_free(): freeing a memory block that is not in the list\n"); - trace(TRACE_WARNING,"__debug_free(): called in file %s, line %d\n",fname,linenr); - free(ptr); - return; - } - - if (prev) - { - prev->nextaddr = curr->nextaddr; - if (__dm_last == curr) - __dm_last = prev; - } - else - { - __dm_first = __dm_first->nextaddr; - if (__dm_first == 0) - __dm_last = 0; + debug_mem_t *curr = __dm_first, *prev = NULL; + + if (!ptr) + return; + + while (curr && curr->addr != (long) ptr) { + prev = curr; + curr = curr->nextaddr; + } + + if (!curr) { + trace(TRACE_WARNING, + "__debug_free(): freeing a memory block that is not in the list\n"); + trace(TRACE_WARNING, + "__debug_free(): called in file %s, line %d\n", + fname, linenr); + free(ptr); + return; + } + + if (prev) { + prev->nextaddr = curr->nextaddr; + if (__dm_last == curr) + __dm_last = prev; + } else { + __dm_first = __dm_first->nextaddr; + if (__dm_first == 0) + __dm_last = 0; /* if (__dm_last == __dm_first) __dm_last = 0; */ - } + } - free(curr); - free(ptr); + free(curr); + free(ptr); } void __debug_dumpallocs() { #ifdef __DEBUG_TRACE_MEMALLOC - debug_mem_t *curr = __dm_first; + debug_mem_t *curr = __dm_first; - trace(TRACE_WARNING,"__debug_dumpallocs(): retrieving list of currently allocated items\n"); + trace(TRACE_WARNING, + "__debug_dumpallocs(): retrieving list of currently allocated items\n"); - while (curr) - { - trace(TRACE_WARNING," From %s, line %d: %X\n",curr->fname,curr->linenr,curr); - curr=curr->nextaddr; - } + while (curr) { + trace(TRACE_WARNING, " From %s, line %d: %X\n", + curr->fname, curr->linenr, curr); + curr = curr->nextaddr; + } - trace(TRACE_WARNING,"\n__debug_dumpallocs(): end\n"); + trace(TRACE_WARNING, "\n__debug_dumpallocs(): end\n"); #endif } - - - @@ -36,17 +36,17 @@ extern int TRACE_TO_SYSLOG; extern int TRACE_VERBOSE; -extern int TRACE_LEVEL; /* 5: maximum debugging */ - /* 2: normal operations */ +extern int TRACE_LEVEL; /* 5: maximum debugging */ + /* 2: normal operations */ typedef enum { - TRACE_FATAL = -1, - TRACE_STOP, - TRACE_MESSAGE, - TRACE_ERROR, - TRACE_WARNING, - TRACE_INFO, - TRACE_DEBUG + TRACE_FATAL = -1, + TRACE_STOP, + TRACE_MESSAGE, + TRACE_ERROR, + TRACE_WARNING, + TRACE_INFO, + TRACE_DEBUG } trace_t; #define memtst(tstbool) func_memtst (__FILE__,__LINE__,tstbool) @@ -67,14 +67,14 @@ typedef enum { void func_memtst(const char *filename, int line, int tst); #ifdef __GNUC__ void trace(trace_t level, char *formatstring, ...) - __attribute__((format(printf, 2, 3))); + __attribute__ ((format(printf, 2, 3))); #else void trace(trace_t level, char *formatstring, ...); #endif void configure_debug(trace_t level, int trace_syslog, int trace_verbose); -void* __debug_malloc(unsigned long size, const char *fname, int linenr); +void *__debug_malloc(unsigned long size, const char *fname, int linenr); void __debug_free(void *ptr, const char *fname, int linenr); void __debug_dumpallocs(void); |