]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/imap/serv_imap.c
* Gratuitous style cleanup of some of the imap code
[citadel.git] / citadel / modules / imap / serv_imap.c
index af02dd68cb4da83c8c9cc08401c91ca0ecf760f8..9bd44105a40b9682f39c2eecedd2692b91d7c766 100644 (file)
@@ -2,12 +2,26 @@
  * $Id$ 
  *
  * IMAP server for the Citadel system
- * Copyright (C) 2000-2007 by Art Cancro and others.
+ * Copyright (C) 2000-2009 by Art Cancro and others.
  * This code is released under the terms of the GNU General Public License.
  *
  * WARNING: the IMAP protocol is badly designed.  No implementation of it
  * is perfect.  Indeed, with so much gratuitous complexity, *all* IMAP
  * implementations have bugs.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "sysdep.h"
@@ -41,7 +55,6 @@
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
 #include "database.h"
@@ -57,7 +70,6 @@
 #include "imap_metadata.h"
 #include "imap_misc.h"
 
-
 #include "ctdl_module.h"
 
 
@@ -208,10 +220,8 @@ void imap_add_single_msgid(long msgnum, void *userdata)
        ++IMAP->num_msgs;
        if (IMAP->num_msgs > IMAP->num_alloc) {
                IMAP->num_alloc += REALLOC_INCREMENT;
-               IMAP->msgids = realloc(IMAP->msgids,
-                                       (IMAP->num_alloc * sizeof(long)) );
-               IMAP->flags = realloc(IMAP->flags,
-                                       (IMAP->num_alloc * sizeof(long)) );
+               IMAP->msgids = realloc(IMAP->msgids, (IMAP->num_alloc * sizeof(long)) );
+               IMAP->flags = realloc(IMAP->flags, (IMAP->num_alloc * sizeof(long)) );
        }
        IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
        IMAP->flags[IMAP->num_msgs - 1] = 0;
@@ -269,8 +279,7 @@ void imap_rescan_msgids(void)
        int num_recent = 0;
 
        if (IMAP->selected == 0) {
-               CtdlLogPrintf(CTDL_ERR,
-                       "imap_load_msgids() can't run; no room selected\n");
+               CtdlLogPrintf(CTDL_ERR, "imap_load_msgids() can't run; no room selected\n");
                return;
        }
 
@@ -278,7 +287,7 @@ void imap_rescan_msgids(void)
         * Check to see if the room's contents have changed.
         * If not, we can avoid this rescan.
         */
-       getroom(&CC->room, CC->room.QRname);
+       CtdlGetRoom(&CC->room, CC->room.QRname);
        if (IMAP->last_mtime == CC->room.QRmtime) {     /* No changes! */
                return;
        }
@@ -354,6 +363,8 @@ void imap_rescan_msgids(void)
 
        /*
         * Now peruse the room for *new* messages only.
+        * This logic is probably the cause of Bug # 368
+        * [ http://bugzilla.citadel.org/show_bug.cgi?id=368 ]
         */
        if (num_msgs > 0) {
                for (j = 0; j < num_msgs; ++j) {
@@ -386,11 +397,6 @@ void imap_rescan_msgids(void)
 }
 
 
-
-
-
-
-
 /*
  * This cleanup function blows away the temporary memory and files used by
  * the IMAP server.
@@ -447,13 +453,23 @@ void imap_output_capability_string(void) {
 
        /* We are building a partial implementation of METADATA for the sole purpose
         * of interoperating with the ical/vcard version of the Bynari Insight Connector.
-        * If you were expecting something else, comment out one or both of these
-        * extension advertisements.
+        * It is not a full RFC5464 implementation, but it should refuse non-Bynari
+        * metadata in a compatible and graceful way.
         */
        cprintf(" METADATA");
-       /* cprintf(" LIST-EXTENDED"); */
+
+       /*
+        * LIST-EXTENDED was originally going to be required by the METADATA extension.
+        * It was mercifully removed prior to the finalization of RFC5464.  We started
+        * implementing this but stopped when we learned that it would not be needed.
+        * If you uncomment this declaration you are responsible for writing a lot of new
+        * code.
+        *
+        * cprintf(" LIST-EXTENDED")
+        */
 }
 
+
 /*
  * implements the CAPABILITY command
  */
@@ -466,7 +482,6 @@ void imap_capability(int num_parms, char *parms[])
 }
 
 
-
 /*
  * Implements the ID command (specified by RFC2971)
  *
@@ -483,7 +498,6 @@ void imap_id(int num_parms, char *parms[])
 }
 
 
-
 /*
  * Here's where our IMAP session begins its happy day.
  */
@@ -509,6 +523,7 @@ void imap_greeting(void)
        cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
 }
 
+
 /*
  * IMAPS is just like IMAP, except it goes crypto right away.
  */
@@ -585,6 +600,7 @@ void imap_authenticate(int num_parms, char *parms[])
        }
 }
 
+
 void imap_auth_plain(char *cmd)
 {
        char decoded_authstring[1024];
@@ -593,6 +609,7 @@ void imap_auth_plain(char *cmd)
        char pass[256];
        int result;
 
+       memset(pass, 0, sizeof(pass));
        CtdlDecodeBase64(decoded_authstring, cmd, strlen(cmd));
        safestrncpy(ident, decoded_authstring, sizeof ident);
        safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
@@ -616,6 +633,7 @@ void imap_auth_plain(char *cmd)
        cprintf("%s NO authentication failed\r\n", IMAP->authseq);
 }
 
+
 void imap_auth_login_user(char *cmd)
 {
        char buf[SIZ];
@@ -628,10 +646,12 @@ void imap_auth_login_user(char *cmd)
        return;
 }
 
+
 void imap_auth_login_pass(char *cmd)
 {
        char buf[SIZ];
 
+       memset(buf, 0, sizeof(buf));
        CtdlDecodeBase64(buf, cmd, SIZ);
        if (CtdlTryPassword(buf) == pass_ok) {
                cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
@@ -652,15 +672,9 @@ void imap_starttls(int num_parms, char *parms[])
        char nosup_response[SIZ];
        char error_response[SIZ];
 
-       sprintf(ok_response,
-               "%s OK begin TLS negotiation now\r\n",
-               parms[0]);
-       sprintf(nosup_response,
-               "%s NO TLS not supported here\r\n",
-               parms[0]);
-       sprintf(error_response,
-               "%s BAD Internal error\r\n",
-               parms[0]);
+       sprintf(ok_response,    "%s OK begin TLS negotiation now\r\n",  parms[0]);
+       sprintf(nosup_response, "%s NO TLS not supported here\r\n",     parms[0]);
+       sprintf(error_response, "%s BAD Internal error\r\n",            parms[0]);
        CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
 }
 
@@ -692,15 +706,15 @@ void imap_select(int num_parms, char *parms[])
        roomflags = (i & 0xff00);
 
        /* First try a regular match */
-       c = getroom(&QRscratch, towhere);
+       c = CtdlGetRoom(&QRscratch, towhere);
 
        /* Then try a mailbox name match */
        if (c != 0) {
-               MailboxName(augmented_roomname, sizeof augmented_roomname,
-                           &CC->user, towhere);
-               c = getroom(&QRscratch, augmented_roomname);
-               if (c == 0)
+               CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, towhere);
+               c = CtdlGetRoom(&QRscratch, augmented_roomname);
+               if (c == 0) {
                        strcpy(towhere, augmented_roomname);
+               }
        }
 
        /* If the room exists, check security/access */
@@ -716,8 +730,7 @@ void imap_select(int num_parms, char *parms[])
 
        /* Fail here if no such room */
        if (!ok) {
-               cprintf("%s NO ... no such room, or access denied\r\n",
-                       parms[0]);
+               cprintf("%s NO ... no such room, or access denied\r\n", parms[0]);
                return;
        }
 
@@ -725,11 +738,11 @@ void imap_select(int num_parms, char *parms[])
        imap_do_expunge();
 
        /*
-        * usergoto() formally takes us to the desired room, happily returning
+        * CtdlUserGoto() formally takes us to the desired room, happily returning
         * the number of messages and number of new messages.
         */
        memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
-       usergoto(NULL, 0, 0, &msgs, &new);
+       CtdlUserGoto(NULL, 0, 0, &msgs, &new);
        IMAP->selected = 1;
 
        if (!strcasecmp(parms[1], "EXAMINE")) {
@@ -753,7 +766,7 @@ void imap_select(int num_parms, char *parms[])
         * 
         * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes
         * some clients (particularly Thunderbird) to misbehave -- they simply
-        * electing not to transmit the flag at all.  So we have to advertise
+        * elect not to transmit the flag at all.  So we have to advertise
         * \Deleted as a PERMANENTFLAGS flag, even though it technically isn't.
         */
        cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
@@ -761,11 +774,11 @@ void imap_select(int num_parms, char *parms[])
 
        cprintf("%s OK [%s] %s completed\r\n",
                parms[0],
-               (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
+               (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]
+       );
 }
 
 
-
 /*
  * Does the real work for expunge.
  */
@@ -799,8 +812,7 @@ int imap_do_expunge(void)
                imap_rescan_msgids();
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n",
-               num_expunged, CC->room.QRname);
+       CtdlLogPrintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n", num_expunged, CC->room.QRname);
        return (num_expunged);
 }
 
@@ -856,7 +868,7 @@ void imap_namespace(int num_parms, char *parms[])
        /* Show all floors as shared namespaces.  Neato! */
        cprintf("(");
        for (i = 0; i < MAXFLOORS; ++i) {
-               fl = cgetfloor(i);
+               fl = CtdlGetCachedFloor(i);
                if (fl->f_flags & F_INUSE) {
                        if (floors > 0) cprintf(" ");
                        cprintf("(");
@@ -874,7 +886,6 @@ void imap_namespace(int num_parms, char *parms[])
 }
 
 
-
 /*
  * Implements the CREATE command
  *
@@ -889,9 +900,13 @@ void imap_create(int num_parms, char *parms[])
        int newroomview = 0;
        char *notification_message = NULL;
 
+       if (num_parms < 3) {
+               cprintf("%s NO A foder name must be specified\r\n", parms[0]);
+               return;
+       }
+
        if (strchr(parms[2], '\\') != NULL) {
-               cprintf("%s NO Invalid character in folder name\r\n",
-                       parms[0]);
+               cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
                CtdlLogPrintf(CTDL_DEBUG, "invalid character in folder name\n");
                return;
        }
@@ -925,7 +940,7 @@ void imap_create(int num_parms, char *parms[])
        CtdlLogPrintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
                roomname, floornum, newroomtype);
 
-       ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
+       ret = CtdlCreateRoom(roomname, newroomtype, "", floornum, 1, 0, newroomview);
        if (ret == 0) {
                /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY!  BYNARI CONNECTOR DEPENDS ON IT! ***/
                cprintf("%s NO Mailbox already exists, or create failed\r\n", parms[0]);
@@ -941,7 +956,7 @@ void imap_create(int num_parms, char *parms[])
                        ((ret & QR_PRIVATE) ? " [private]" : ""),
                        ((ret & QR_GUESSNAME) ? " [hidden]" : "")
                );
-               aide_message(notification_message, "Room Creation Message");
+               CtdlAideMessage(notification_message, "Room Creation Message");
                free(notification_message);
        }
        CtdlLogPrintf(CTDL_DEBUG, "imap_create() completed\n");
@@ -968,13 +983,13 @@ int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
        }
 
        /* First try a regular match */
-       c = getroom(&QRscratch, roomname);
+       c = CtdlGetRoom(&QRscratch, roomname);
 
        /* Then try a mailbox name match */
        if (c != 0) {
-               MailboxName(augmented_roomname, sizeof augmented_roomname,
+               CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
                            &CC->user, roomname);
-               c = getroom(&QRscratch, augmented_roomname);
+               c = CtdlGetRoom(&QRscratch, augmented_roomname);
                if (c == 0)
                        strcpy(roomname, augmented_roomname);
        }
@@ -1016,7 +1031,7 @@ void imap_status(int num_parms, char *parms[])
        char savedroom[ROOMNAMELEN];
        int msgs, new;
 
-       ret = imap_grabroom(roomname, parms[2], 0);
+       ret = imap_grabroom(roomname, parms[2], 1);
        if (ret != 0) {
                cprintf
                    ("%s NO Invalid mailbox name or location, or access denied\r\n",
@@ -1025,14 +1040,14 @@ void imap_status(int num_parms, char *parms[])
        }
 
        /*
-        * usergoto() formally takes us to the desired room, happily returning
+        * CtdlUserGoto() formally takes us to the desired room, happily returning
         * the number of messages and number of new messages.  (If another
         * folder is selected, save its name so we can return there!!!!!)
         */
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       usergoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
 
        /*
         * Tell the client what it wants to know.  In fact, tell it *more* than
@@ -1053,7 +1068,7 @@ void imap_status(int num_parms, char *parms[])
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               usergoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
        }
 
        /*
@@ -1063,7 +1078,6 @@ void imap_status(int num_parms, char *parms[])
 }
 
 
-
 /*
  * Implements the SUBSCRIBE command
  *
@@ -1086,21 +1100,21 @@ void imap_subscribe(int num_parms, char *parms[])
        }
 
        /*
-        * usergoto() formally takes us to the desired room, which has the side
+        * CtdlUserGoto() formally takes us to the desired room, which has the side
         * effect of marking the room as not-zapped ... exactly the effect
         * we're looking for.
         */
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       usergoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
 
        /*
         * If another folder is selected, go back to that room so we can resume
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               usergoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
        }
 
        cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
@@ -1118,7 +1132,7 @@ void imap_unsubscribe(int num_parms, char *parms[])
        char savedroom[ROOMNAMELEN];
        int msgs, new;
 
-       ret = imap_grabroom(roomname, parms[2], 0);
+       ret = imap_grabroom(roomname, parms[2], 1);
        if (ret != 0) {
                cprintf
                    ("%s NO Invalid mailbox name or location, or access denied\r\n",
@@ -1127,12 +1141,12 @@ void imap_unsubscribe(int num_parms, char *parms[])
        }
 
        /*
-        * usergoto() formally takes us to the desired room.
+        * CtdlUserGoto() formally takes us to the desired room.
         */
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       usergoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
 
        /* 
         * Now make the API call to zap the room
@@ -1150,12 +1164,11 @@ void imap_unsubscribe(int num_parms, char *parms[])
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               usergoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
        }
 }
 
 
-
 /*
  * Implements the DELETE command
  *
@@ -1175,20 +1188,20 @@ void imap_delete(int num_parms, char *parms[])
        }
 
        /*
-        * usergoto() formally takes us to the desired room, happily returning
+        * CtdlUserGoto() formally takes us to the desired room, happily returning
         * the number of messages and number of new messages.  (If another
         * folder is selected, save its name so we can return there!!!!!)
         */
        if (IMAP->selected) {
                strcpy(savedroom, CC->room.QRname);
        }
-       usergoto(roomname, 0, 0, &msgs, &new);
+       CtdlUserGoto(roomname, 0, 0, &msgs, &new);
 
        /*
         * Now delete the room.
         */
        if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
-               schedule_room_for_deletion(&CC->room);
+               CtdlScheduleRoomForDeletion(&CC->room);
                cprintf("%s OK DELETE completed\r\n", parms[0]);
        } else {
                cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
@@ -1199,7 +1212,7 @@ void imap_delete(int num_parms, char *parms[])
         * our happy day without violent explosions.
         */
        if (IMAP->selected) {
-               usergoto(savedroom, 0, 0, &msgs, &new);
+               CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
        }
 }
 
@@ -1304,7 +1317,7 @@ void imap_rename(int num_parms, char *parms[])
         * (already did that) and create a new inbox.
         */
        if (!strcasecmp(parms[2], "INBOX")) {
-               create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
+               CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
        }
 
        /* Otherwise, do the subfolders.  Build a list of rooms to rename... */
@@ -1312,7 +1325,7 @@ void imap_rename(int num_parms, char *parms[])
                irlparms.oldname = parms[2];
                irlparms.newname = parms[3];
                irlparms.irl = &irl;
-               ForEachRoom(imap_rename_backend, (void *) &irlparms);
+               CtdlForEachRoom(imap_rename_backend, (void *) &irlparms);
 
                /* ... and now rename them. */
                while (irl != NULL) {
@@ -1334,14 +1347,12 @@ void imap_rename(int num_parms, char *parms[])
                parms[3],
                CC->curr_user
        );
-       aide_message(buf, "IMAP folder rename");
+       CtdlAideMessage(buf, "IMAP folder rename");
 
        cprintf("%s OK RENAME completed\r\n", parms[0]);
 }
 
 
-
-
 /* 
  * Main command loop for IMAP sessions.
  */
@@ -1401,7 +1412,6 @@ void imap_command_loop(void)
                return;
        }
 
-
        /* Ok, at this point we're in normal command mode.
         * If the command just submitted does not contain a literal, we
         * might think about delivering some untagged stuff...
@@ -1632,7 +1642,6 @@ void imap_command_loop(void)
 
        gettimeofday(&tv2, NULL);
        total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
-       CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", cmdbuf); // FIXME FIXME FIXME REMOVE THIS NOW
        CtdlLogPrintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n",
                (total_time / 1000000),
                (total_time % 1000000)