* Reference count adjustments are now deferred by queuing
[citadel.git] / citadel / serv_imap.c
index c56ee75e5fc269d60443f44702c7f20b1b3b9a58..9c17ccae7aac8e3d98c231a46fe120d4e6454de0 100644 (file)
@@ -2,7 +2,7 @@
  * $Id$ 
  *
  * IMAP server for the Citadel system
- * Copyright (C) 2000-2005 by Art Cancro and others.
+ * Copyright (C) 2000-2006 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
@@ -91,6 +91,7 @@ void imap_free_msgids(void)
                free(IMAP->flags);
                IMAP->flags = NULL;
        }
+       IMAP->last_mtime = (-1);
 }
 
 
@@ -112,26 +113,33 @@ void imap_free_transmitted_message(void)
  * sets stored in the visit record for this user/room.  Note that we have
  * to parse each sequence set manually here, because calling the utility
  * function is_msg_in_sequence_set() over and over again is too expensive.
+ *
+ * first_msg should be set to 0 to rescan the flags for every message in the
+ * room, or some other value if we're only interested in an incremental
+ * update.
  */
-void imap_set_seen_flags(void)
+void imap_set_seen_flags(int first_msg)
 {
        struct visit vbuf;
        int i;
        int num_sets;
        int s;
-       char setstr[SIZ], lostr[SIZ], histr[SIZ];
+       char setstr[64], lostr[64], histr[64];
        long lo, hi;
 
        if (IMAP->num_msgs < 1) return;
        CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
 
-       for (i = 0; i < IMAP->num_msgs; ++i) {
+       for (i = first_msg; i < IMAP->num_msgs; ++i) {
                IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SEEN;
                IMAP->flags[i] |= IMAP_RECENT;
                IMAP->flags[i] = IMAP->flags[i] & ~IMAP_ANSWERED;
        }
 
-       /* Do the "\Seen" flag.  (Any message not "\Seen" is considered "\Recent".) */
+       /*
+        * Do the "\Seen" flag.
+        * (Any message not "\Seen" is considered "\Recent".)
+        */
        num_sets = num_tokens(vbuf.v_seen, ',');
        for (s=0; s<num_sets; ++s) {
                extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
@@ -149,8 +157,8 @@ void imap_set_seen_flags(void)
                lo = atol(lostr);
                hi = atol(histr);
 
-               for (i = 0; i < IMAP->num_msgs; ++i) {
-                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)) {
+               for (i = first_msg; i < IMAP->num_msgs; ++i) {
+                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
                                IMAP->flags[i] |= IMAP_SEEN;
                                IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
                        }
@@ -175,8 +183,8 @@ void imap_set_seen_flags(void)
                lo = atol(lostr);
                hi = atol(histr);
 
-               for (i = 0; i < IMAP->num_msgs; ++i) {
-                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)) {
+               for (i = first_msg; i < IMAP->num_msgs; ++i) {
+                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
                                IMAP->flags[i] |= IMAP_ANSWERED;
                        }
                }
@@ -199,8 +207,10 @@ 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;
@@ -213,6 +223,7 @@ void imap_add_single_msgid(long msgnum, void *userdata)
  */
 void imap_load_msgids(void)
 {
+       struct cdbdata *cdbfr;
 
        if (IMAP->selected == 0) {
                lprintf(CTDL_ERR,
@@ -222,10 +233,22 @@ void imap_load_msgids(void)
 
        imap_free_msgids();     /* If there was already a map, free it */
 
-       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
-                          imap_add_single_msgid, NULL);
+       /* Load the message list */
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
+       if (cdbfr != NULL) {
+               IMAP->msgids = malloc(cdbfr->len);
+               memcpy(IMAP->msgids, cdbfr->ptr, cdbfr->len);
+               IMAP->num_msgs = cdbfr->len / sizeof(long);
+               IMAP->num_alloc = cdbfr->len / sizeof(long);
+               cdb_free(cdbfr);
+       }
+
+       if (IMAP->num_msgs) {
+               IMAP->flags = malloc(IMAP->num_alloc * sizeof(long));
+               memset(IMAP->flags, 0, (IMAP->num_alloc * sizeof(long)) );
+       }
 
-       imap_set_seen_flags();
+       imap_set_seen_flags(0);
 }
 
 
@@ -237,7 +260,7 @@ void imap_rescan_msgids(void)
 
        int original_num_msgs = 0;
        long original_highest = 0L;
-       int i, j;
+       int i, j, jstart;
        int message_still_exists;
        struct cdbdata *cdbfr;
        long *msglist = NULL;
@@ -250,6 +273,15 @@ void imap_rescan_msgids(void)
                return;
        }
 
+       /*
+        * Check to see if the room's contents have changed.
+        * If not, we can avoid this rescan.
+        */
+       getroom(&CC->room, CC->room.QRname);
+       if (IMAP->last_mtime == CC->room.QRmtime) {     /* No changes! */
+               return;
+       }
+
        /* Load the *current* message list from disk, so we can compare it
         * to what we have in memory.
         */
@@ -270,23 +302,28 @@ void imap_rescan_msgids(void)
        /*
         * Check to see if any of the messages we know about have been expunged
         */
-       if (IMAP->num_msgs > 0)
+       if (IMAP->num_msgs > 0) {
+               jstart = 0;
                for (i = 0; i < IMAP->num_msgs; ++i) {
 
                        message_still_exists = 0;
-                       if (num_msgs > 0)
-                               for (j = 0; j < num_msgs; ++j) {
+                       if (num_msgs > 0) {
+                               for (j = jstart; j < num_msgs; ++j) {
                                        if (msglist[j] == IMAP->msgids[i]) {
                                                message_still_exists = 1;
+                                               jstart = j;
+                                               break;
                                        }
                                }
+                       }
 
                        if (message_still_exists == 0) {
                                cprintf("* %d EXPUNGE\r\n", i + 1);
 
-                               /* Here's some nice stupid nonsense.  When a message
-                                * is expunged, we have to slide all the existing
-                                * messages up in the message array.
+                               /* Here's some nice stupid nonsense.  When a
+                                * message is expunged, we have to slide all
+                                * the existing messages up in the message
+                                * array.
                                 */
                                --IMAP->num_msgs;
                                memcpy(&IMAP->msgids[i],
@@ -302,6 +339,7 @@ void imap_rescan_msgids(void)
                        }
 
                }
+       }
 
        /*
         * Remember how many messages were here before we re-scanned.
@@ -316,13 +354,14 @@ void imap_rescan_msgids(void)
        /*
         * Now peruse the room for *new* messages only.
         */
-       if (num_msgs > 0)
+       if (num_msgs > 0) {
                for (j = 0; j < num_msgs; ++j) {
                        if (msglist[j] > original_highest) {
                                imap_add_single_msgid(msglist[j], NULL);
                        }
                }
-       imap_set_seen_flags();
+       }
+       imap_set_seen_flags(original_num_msgs);
 
        /*
         * If new messages have arrived, tell the client about them.
@@ -339,8 +378,10 @@ void imap_rescan_msgids(void)
                cprintf("* %d RECENT\r\n", num_recent);
        }
 
-       if (num_msgs != 0)
+       if (num_msgs != 0) {
                free(msglist);
+       }
+       IMAP->last_mtime = CC->room.QRmtime;
 }
 
 
@@ -373,6 +414,7 @@ void imap_cleanup_function(void)
                free(IMAP->cached_rfc822_data);
                IMAP->cached_rfc822_data = NULL;
                IMAP->cached_rfc822_msgnum = (-1);
+               IMAP->cached_rfc822_withbody = 0;
        }
 
        if (IMAP->cached_body != NULL) {
@@ -387,6 +429,46 @@ void imap_cleanup_function(void)
 }
 
 
+/*
+ * Does the actual work of the CAPABILITY command (because we need to
+ * output this stuff in other places as well)
+ */
+void imap_output_capability_string(void) {
+       cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=LOGIN");
+#ifdef HAVE_OPENSSL
+       if (!CC->redirect_ssl) cprintf(" STARTTLS");
+#endif
+}
+
+/*
+ * implements the CAPABILITY command
+ */
+void imap_capability(int num_parms, char *parms[])
+{
+       cprintf("* ");
+       imap_output_capability_string();
+       cprintf("\r\n");
+       cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
+}
+
+
+
+/*
+ * Implements the ID command (specified by RFC2971)
+ *
+ * We ignore the client-supplied information, and output a NIL response.
+ * Although this is technically a valid implementation of the extension, it
+ * is quite useless.  It exists only so that we may see which clients are
+ * making use of this extension.
+ * 
+ */
+void imap_id(int num_parms, char *parms[])
+{
+       cprintf("* ID NIL\r\n");
+       cprintf("%s OK ID completed\r\n", parms[0]);
+}
+
+
 
 /*
  * Here's where our IMAP session begins its happy day.
@@ -400,9 +482,11 @@ void imap_greeting(void)
        IMAP->authstate = imap_as_normal;
        IMAP->cached_rfc822_data = NULL;
        IMAP->cached_rfc822_msgnum = (-1);
+       IMAP->cached_rfc822_withbody = 0;
 
-       cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
-               config.c_fqdn);
+       cprintf("* OK [");
+       imap_output_capability_string();
+       cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
 }
 
 /*
@@ -428,7 +512,9 @@ void imap_login(int num_parms, char *parms[])
 
        if (CtdlLoginExistingUser(parms[2]) == login_ok) {
                if (CtdlTryPassword(parms[3]) == pass_ok) {
-                       cprintf("%s OK login successful\r\n", parms[0]);
+                       cprintf("%s OK [", parms[0]);
+                       imap_output_capability_string();
+                       cprintf("] Hello, %s\r\n", CC->user.fullname);
                        return;
                }
        }
@@ -497,23 +583,6 @@ void imap_auth_login_pass(char *cmd)
 }
 
 
-/*
- * implements the CAPABILITY command
- */
-void imap_capability(int num_parms, char *parms[])
-{
-       cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
-
-#ifdef HAVE_OPENSSL
-       cprintf(" STARTTLS");
-#endif
-
-       cprintf("\r\n");
-       cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
-}
-
-
-
 /*
  * implements the STARTTLS command (Citadel API version)
  */
@@ -612,19 +681,22 @@ void imap_select(int num_parms, char *parms[])
        }
 
        imap_load_msgids();
+       IMAP->last_mtime = CC->room.QRmtime;
 
        cprintf("* %d EXISTS\r\n", msgs);
        cprintf("* %d RECENT\r\n", new);
 
+       cprintf("* OK [UIDVALIDITY 1] UID validity status\r\n");
+       cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
+
        /* Note that \Deleted is a valid flag, but not a permanent flag,
         * because we don't maintain its state across sessions.  Citadel
         * automatically expunges mailboxes when they are de-selected.
         */
        cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
-       cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
+       cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] "
                "permanent flags\r\n");
 
-       cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
        cprintf("%s OK [%s] %s completed\r\n",
                parms[0],
                (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
@@ -633,32 +705,40 @@ void imap_select(int num_parms, char *parms[])
 
 
 /*
- * does the real work for expunge
+ * Does the real work for expunge.
  */
 int imap_do_expunge(void)
 {
        int i;
        int num_expunged = 0;
+       long *delmsgs = NULL;
+       int num_delmsgs = 0;
 
        lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
        if (IMAP->selected == 0) {
                return (0);
        }
 
-       if (IMAP->num_msgs > 0)
+       if (IMAP->num_msgs > 0) {
+               delmsgs = malloc(IMAP->num_msgs * sizeof(long));
                for (i = 0; i < IMAP->num_msgs; ++i) {
                        if (IMAP->flags[i] & IMAP_DELETED) {
-                               CtdlDeleteMessages(CC->room.QRname,
-                                                  IMAP->msgids[i], "");
-                               ++num_expunged;
+                               delmsgs[num_delmsgs++] = IMAP->msgids[i];
                        }
                }
+               if (num_delmsgs > 0) {
+                       CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
+               }
+               num_expunged += num_delmsgs;
+               free(delmsgs);
+       }
 
        if (num_expunged > 0) {
                imap_rescan_msgids();
        }
 
-       lprintf(CTDL_DEBUG, "Expunged %d messages.\n", num_expunged);
+       lprintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n",
+               num_expunged, CC->room.QRname);
        return (num_expunged);
 }
 
@@ -1140,7 +1220,7 @@ void imap_delete(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 access denied\r\n",
                        parms[0]);
@@ -1161,7 +1241,7 @@ void imap_delete(int num_parms, char *parms[])
         * Now delete the room.
         */
        if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
-               delete_room(&CC->room);
+               schedule_room_for_deletion(&CC->room);
                cprintf("%s OK DELETE completed\r\n", parms[0]);
        } else {
                cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
@@ -1294,8 +1374,7 @@ void imap_rename(int num_parms, char *parms[])
                                           irl->irl_newfloor);
                        if (r != crr_ok) {
                                /* FIXME handle error returns better */
-                               lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
-                                       r);
+                               lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r);
                        }
                        irlp = irl;
                        irl = irl->next;
@@ -1317,17 +1396,28 @@ void imap_command_loop(void)
        char cmdbuf[SIZ];
        char *parms[SIZ];
        int num_parms;
+       struct timeval tv1, tv2;
 
+       gettimeofday(&tv1, NULL);
        CC->lastcmd = time(NULL);
        memset(cmdbuf, 0, sizeof cmdbuf);       /* Clear it, just in case */
        flush_output();
        if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
-               lprintf(CTDL_ERR, "IMAP socket is broken.  Ending session.\r\n");
+               lprintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
                CC->kill_me = 1;
                return;
        }
 
-       lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
+       if (IMAP->authstate == imap_as_expecting_password) {
+               lprintf(CTDL_INFO, "IMAP: <password>\n");
+       }
+       else if (bmstrcasestr(cmdbuf, " LOGIN ")) {
+               lprintf(CTDL_INFO, "IMAP: LOGIN...\n");
+       }
+       else {
+               lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
+       }
+
        while (strlen(cmdbuf) < 5)
                strcat(cmdbuf, " ");
 
@@ -1379,6 +1469,11 @@ void imap_command_loop(void)
                        parms[0]);
        }
 
+       else if (!strcasecmp(parms[1], "ID")) {
+               imap_id(num_parms, parms);
+       }
+
+
        else if (!strcasecmp(parms[1], "LOGOUT")) {
                if (IMAP->selected) {
                        imap_do_expunge();      /* yes, we auto-expunge */
@@ -1520,6 +1615,12 @@ void imap_command_loop(void)
 
        /* If the client transmitted a message we can free it now */
        imap_free_transmitted_message();
+
+       gettimeofday(&tv2, NULL);
+       lprintf(CTDL_DEBUG, "IMAP %s took %ld microseconds\n",
+               parms[1],
+               (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000))
+       );
 }