cleaned up some compiler warnings
[citadel.git] / citadel / modules / sieve / serv_sieve.c
index b52bb3bdb51ecaf4ac8a08be9bfe5faa87b87e47..30e03100759b8272827a7792facf871325be1b96 100644 (file)
@@ -2,7 +2,7 @@
  * This module glues libSieve to the Citadel server in order to implement
  * the Sieve mailbox filtering language (RFC 3028).
  *
- * Copyright (c) 1987-2012 by the citadel.org team
+ * Copyright (c) 1987-2018 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 3.
@@ -58,7 +58,7 @@ char *msiv_extensions = NULL;
  */
 int ctdl_debug(sieve2_context_t *s, void *my)
 {
-       syslog(LOG_DEBUG, "Sieve: %s", sieve2_getvalue_string(s, "message"));
+       syslog(LOG_DEBUG, "%s", sieve2_getvalue_string(s, "message"));
        return SIEVE2_OK;
 }
 
@@ -69,8 +69,8 @@ int ctdl_debug(sieve2_context_t *s, void *my)
 int ctdl_errparse(sieve2_context_t *s, void *my)
 {
        syslog(LOG_WARNING, "Error in script, line %d: %s",
-               sieve2_getvalue_int(s, "lineno"),
-               sieve2_getvalue_string(s, "message")
+                 sieve2_getvalue_int(s, "lineno"),
+                 sieve2_getvalue_string(s, "message")
        );
        return SIEVE2_OK;
 }
@@ -82,8 +82,8 @@ int ctdl_errparse(sieve2_context_t *s, void *my)
 int ctdl_errexec(sieve2_context_t *s, void *my)
 {
        syslog(LOG_WARNING, "Error executing script: %s",
-               sieve2_getvalue_string(s, "message")
-       );
+                 sieve2_getvalue_string(s, "message")
+               );
        return SIEVE2_OK;
 }
 
@@ -95,7 +95,7 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
        struct CtdlMessage *msg = NULL;
-       struct recptypes *valid = NULL;
+       recptypes *valid = NULL;
        char recp[256];
 
        safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
@@ -113,7 +113,7 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
                return SIEVE2_ERROR_BADARGS;
        }
 
-       msg = CtdlFetchMessage(cs->msgnum, 1);
+       msg = CtdlFetchMessage(cs->msgnum, 1, 1);
        if (msg == NULL) {
                syslog(LOG_WARNING, "REDIRECT failed: unable to fetch msg %ld", cs->msgnum);
                free_recipients(valid);
@@ -123,7 +123,7 @@ int ctdl_redirect(sieve2_context_t *s, void *my)
        CtdlSubmitMsg(msg, valid, NULL, 0);
        cs->cancel_implicit_keep = 1;
        free_recipients(valid);
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        return SIEVE2_OK;
 }
 
@@ -182,13 +182,13 @@ int ctdl_fileinto(sieve2_context_t *s, void *my)
        }
 
        /* Yes, we actually have to go there */
-       CtdlUserGoto(NULL, 0, 0, NULL, NULL);
+       CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
 
        c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL, 0);
 
        /* Go back to the room we came from */
        if (strcasecmp(original_room_name, CC->room.QRname)) {
-               CtdlUserGoto(original_room_name, 0, 0, NULL, NULL);
+               CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
        }
 
        if (c == 0) {
@@ -216,7 +216,6 @@ int ctdl_discard(sieve2_context_t *s, void *my)
 }
 
 
-
 /*
  * Callback function to indicate that a message should be rejected
  */
@@ -267,7 +266,6 @@ int ctdl_reject(sieve2_context_t *s, void *my)
 }
 
 
-
 /*
  * Callback function to indicate that a vacation message should be generated
  */
@@ -356,11 +354,11 @@ int ctdl_vacation(sieve2_context_t *s, void *my)
 }
 
 
+#if 0
 /*
  * Callback function to parse addresses per local system convention
  * It is disabled because we don't support subaddresses.
  */
-#if 0
 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
 {
        struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
@@ -418,16 +416,17 @@ int ctdl_getenvelope(sieve2_context_t *s, void *my)
 }
 
 
+#if 0
 /*
  * Callback function to fetch message body
  * (Uncomment the code if we implement this extension)
  *
+ */
 int ctdl_getbody(sieve2_context_t *s, void *my)
 {
        return SIEVE2_ERROR_UNSUPPORTED;
 }
- *
- */
+#endif
 
 
 /*
@@ -483,6 +482,7 @@ int ctdl_getscript(sieve2_context_t *s, void *my) {
        return SIEVE2_ERROR_GETSCRIPT;
 }
 
+
 /*
  * Callback function to retrieve message headers
  */
@@ -496,7 +496,6 @@ int ctdl_getheaders(sieve2_context_t *s, void *my) {
 }
 
 
-
 /*
  * Add a room to the list of those rooms which potentially require sieve processing
  */
@@ -515,7 +514,6 @@ void sieve_queue_room(struct ctdlroom *which_room) {
 }
 
 
-
 /*
  * Perform sieve processing for one message (called by sieve_do_room() for each message)
  */
@@ -529,9 +527,8 @@ void sieve_do_msg(long msgnum, void *userdata) {
        size_t headers_len = 0;
        int len = 0;
 
-       if (u == NULL)
-       {
-               syslog(LOG_EMERG, "Can't process message <%ld> without userdata!", msgnum);
+       if (u == NULL) {
+               syslog(LOG_ERR, "Can't process message <%ld> without userdata!", msgnum);
                return;
        }
 
@@ -542,7 +539,7 @@ void sieve_do_msg(long msgnum, void *userdata) {
        /*
         * Make sure you include message body so you can get those second-level headers ;)
         */
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(msgnum, 1, 1);
        if (msg == NULL) return;
 
        /*
@@ -572,37 +569,34 @@ void sieve_do_msg(long msgnum, void *userdata) {
        my.u = u;                               /* Hand off a pointer to the rest of this info */
 
        /* Keep track of the recipient so we can do handling based on it later */
-       process_rfc822_addr(msg->cm_fields['R'], my.recp_user, my.recp_node, my.recp_name);
+       process_rfc822_addr(msg->cm_fields[eRecipient], my.recp_user, my.recp_node, my.recp_name);
 
        /* Keep track of the sender so we can use it for REJECT and VACATION responses */
-       if (msg->cm_fields['F'] != NULL) {
-               safestrncpy(my.sender, msg->cm_fields['F'], sizeof my.sender);
-       }
-       else if ( (msg->cm_fields['A'] != NULL) && (msg->cm_fields['N'] != NULL) ) {
-               snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields['A'], msg->cm_fields['N']);
+       if (!CM_IsEmpty(msg, erFc822Addr)) {
+               safestrncpy(my.sender, msg->cm_fields[erFc822Addr], sizeof my.sender);
        }
-       else if (msg->cm_fields['A'] != NULL) {
-               safestrncpy(my.sender, msg->cm_fields['A'], sizeof my.sender);
+       else if (!CM_IsEmpty(msg, eAuthor)) {
+               safestrncpy(my.sender, msg->cm_fields[eAuthor], sizeof my.sender);
        }
        else {
                strcpy(my.sender, "");
        }
 
        /* Keep track of the subject so we can use it for VACATION responses */
-       if (msg->cm_fields['U'] != NULL) {
-               safestrncpy(my.subject, msg->cm_fields['U'], sizeof my.subject);
+       if (!CM_IsEmpty(msg, eMsgSubject)) {
+               safestrncpy(my.subject, msg->cm_fields[eMsgSubject], sizeof my.subject);
        }
        else {
                strcpy(my.subject, "");
        }
 
        /* Keep track of the envelope-from address (use body-from if not found) */
-       if (msg->cm_fields['P'] != NULL) {
-               safestrncpy(my.envelope_from, msg->cm_fields['P'], sizeof my.envelope_from);
+       if (!CM_IsEmpty(msg, eMessagePath)) {
+               safestrncpy(my.envelope_from, msg->cm_fields[eMessagePath], sizeof my.envelope_from);
                stripallbut(my.envelope_from, '<', '>');
        }
-       else if (msg->cm_fields['F'] != NULL) {
-               safestrncpy(my.envelope_from, msg->cm_fields['F'], sizeof my.envelope_from);
+       else if (!CM_IsEmpty(msg, erFc822Addr)) {
+               safestrncpy(my.envelope_from, msg->cm_fields[erFc822Addr], sizeof my.envelope_from);
                stripallbut(my.envelope_from, '<', '>');
        }
        else {
@@ -615,19 +609,19 @@ void sieve_do_msg(long msgnum, void *userdata) {
        }
        if (haschar(my.envelope_from, '@') == 0) {
                strcat(my.envelope_from, "@");
-               strcat(my.envelope_from, config.c_fqdn);
+               strcat(my.envelope_from, CtdlGetConfigStr("c_fqdn"));
        }
 
        /* Keep track of the envelope-to address (use body-to if not found) */
-       if (msg->cm_fields['V'] != NULL) {
-               safestrncpy(my.envelope_to, msg->cm_fields['V'], sizeof my.envelope_to);
+       if (!CM_IsEmpty(msg, eenVelopeTo)) {
+               safestrncpy(my.envelope_to, msg->cm_fields[eenVelopeTo], sizeof my.envelope_to);
                stripallbut(my.envelope_to, '<', '>');
        }
-       else if (msg->cm_fields['R'] != NULL) {
-               safestrncpy(my.envelope_to, msg->cm_fields['R'], sizeof my.envelope_to);
-               if (msg->cm_fields['D'] != NULL) {
+       else if (!CM_IsEmpty(msg, eRecipient)) {
+               safestrncpy(my.envelope_to, msg->cm_fields[eRecipient], sizeof my.envelope_to);
+               if (!CM_IsEmpty(msg, eDestination)) {
                        strcat(my.envelope_to, "@");
-                       strcat(my.envelope_to, msg->cm_fields['D']);
+                       strcat(my.envelope_to, msg->cm_fields[eDestination]);
                }
                stripallbut(my.envelope_to, '<', '>');
        }
@@ -641,15 +635,15 @@ void sieve_do_msg(long msgnum, void *userdata) {
        }
        if (haschar(my.envelope_to, '@') == 0) {
                strcat(my.envelope_to, "@");
-               strcat(my.envelope_to, config.c_fqdn);
+               strcat(my.envelope_to, CtdlGetConfigStr("c_fqdn"));
        }
 
-       CtdlFreeMessage(msg);
+       CM_Free(msg);
        
        syslog(LOG_DEBUG, "Calling sieve2_execute()");
        res = sieve2_execute(sieve2_context, &my);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
        }
 
        free(my.rfc822headers);
@@ -735,17 +729,18 @@ void get_sieve_config_backend(long msgnum, void *userdata) {
        struct sdm_userdata *u = (struct sdm_userdata *) userdata;
        struct CtdlMessage *msg;
        char *conf;
+       long conflen;
 
        u->config_msgnum = msgnum;
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(msgnum, 1, 1);
        if (msg == NULL) {
                u->config_msgnum = (-1) ;
                return;
        }
 
-       conf = msg->cm_fields['M'];
-       msg->cm_fields['M'] = NULL;
-       CtdlFreeMessage(msg);
+       CM_GetAsField(msg, eMesageText, &conf, &conflen);
+
+       CM_Free(msg);
 
        if (conf != NULL) {
                parse_sieve_config(conf, u);
@@ -889,12 +884,22 @@ void sieve_do_room(char *roomname) {
                return;
        }
 
-       /* FIXME this is where we have to check for empty scripts */
+       /*
+        * Check to see whether the script is empty and should not be processed.
+        * A script is considered non-empty if it contains at least one semicolon.
+        */
+       if (
+               (get_active_script(&u) == NULL)
+               || (strchr(get_active_script(&u), ';') == NULL)
+       ) {
+               syslog(LOG_DEBUG, "Sieve script is empty.  No processing is required.");
+               return;
+       }
 
        syslog(LOG_DEBUG, "Rules found.  Performing Sieve processing for <%s>", roomname);
 
        if (CtdlGetRoom(&CC->room, roomname) != 0) {
-               syslog(LOG_CRIT, "ERROR: cannot load <%s>", roomname);
+               syslog(LOG_ERR, "ERROR: cannot load <%s>", roomname);
                return;
        }
 
@@ -902,13 +907,13 @@ void sieve_do_room(char *roomname) {
        
        res = sieve2_alloc(&sieve2_context);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
                return;
        }
 
        res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -919,7 +924,7 @@ void sieve_do_room(char *roomname) {
        my.u = &u;
        res = sieve2_validate(sieve2_context, &my);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -934,7 +939,7 @@ void sieve_do_room(char *roomname) {
 BAIL:
        res = sieve2_free(&sieve2_context);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
        }
 
        /* Rewrite the config if we have to */
@@ -1163,7 +1168,7 @@ void cmd_msiv(char *argbuf) {
                extract_token(script_name, argbuf, 1, '|', sizeof script_name);
                if (!IsEmptyStr(script_name)) {
                        cprintf("%d Transmit script now\n", SEND_LISTING);
-                       script_content = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
+                       script_content = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
                        msiv_putscript(&u, script_name, script_content);
                        changes_made = 1;
                }
@@ -1274,13 +1279,13 @@ void ctdl_sieve_init(void) {
         */
        res = sieve2_alloc(&sieve2_context);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
                return;
        }
 
        res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
                goto BAIL;
        }
 
@@ -1289,11 +1294,12 @@ void ctdl_sieve_init(void) {
 
 BAIL:  res = sieve2_free(&sieve2_context);
        if (res != SIEVE2_OK) {
-               syslog(LOG_CRIT, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
+               syslog(LOG_ERR, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
        }
 
 }
 
+
 void cleanup_sieve(void)
 {
         struct RoomProcList *ptr, *ptr2;
@@ -1313,6 +1319,7 @@ void cleanup_sieve(void)
         end_critical_section(S_SIEVELIST);
 }
 
+
 int serv_sieve_room(struct ctdlroom *room)
 {
        if (!strcasecmp(&room->QRname[11], MAILROOM)) {
@@ -1321,19 +1328,18 @@ int serv_sieve_room(struct ctdlroom *room)
        return 0;
 }
 
+
 CTDL_MODULE_INIT(sieve)
 {
        if (!threading)
        {
-
                ctdl_sieve_init();
                CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
                CtdlRegisterRoomHook(serv_sieve_room);
-               CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE);
+               CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE, PRIO_HOUSE + 10);
                CtdlRegisterCleanupHook(cleanup_sieve);
        }
        
         /* return our module name for the log */
        return "sieve";
 }
-