]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_imap.c
* Reference count adjustments are now deferred by queuing
[citadel.git] / citadel / serv_imap.c
index b89b4f08b1f71285a64c3458e6b4cc5ebc181d76..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
@@ -231,9 +231,7 @@ void imap_load_msgids(void)
                return;
        }
 
-       TRACE;
        imap_free_msgids();     /* If there was already a map, free it */
-       TRACE;
 
        /* Load the message list */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
@@ -250,9 +248,7 @@ void imap_load_msgids(void)
                memset(IMAP->flags, 0, (IMAP->num_alloc * sizeof(long)) );
        }
 
-       TRACE;
        imap_set_seen_flags(0);
-       TRACE;
 }
 
 
@@ -277,7 +273,6 @@ void imap_rescan_msgids(void)
                return;
        }
 
-       TRACE;
        /*
         * Check to see if the room's contents have changed.
         * If not, we can avoid this rescan.
@@ -287,7 +282,6 @@ void imap_rescan_msgids(void)
                return;
        }
 
-       TRACE;
        /* Load the *current* message list from disk, so we can compare it
         * to what we have in memory.
         */
@@ -305,7 +299,6 @@ void imap_rescan_msgids(void)
                num_msgs = 0;
        }
 
-       TRACE;
        /*
         * Check to see if any of the messages we know about have been expunged
         */
@@ -348,7 +341,6 @@ void imap_rescan_msgids(void)
                }
        }
 
-       TRACE;
        /*
         * Remember how many messages were here before we re-scanned.
         */
@@ -359,7 +351,6 @@ void imap_rescan_msgids(void)
                original_highest = 0L;
        }
 
-       TRACE;
        /*
         * Now peruse the room for *new* messages only.
         */
@@ -372,7 +363,6 @@ void imap_rescan_msgids(void)
        }
        imap_set_seen_flags(original_num_msgs);
 
-       TRACE;
        /*
         * If new messages have arrived, tell the client about them.
         */
@@ -388,7 +378,6 @@ void imap_rescan_msgids(void)
                cprintf("* %d RECENT\r\n", num_recent);
        }
 
-       TRACE;
        if (num_msgs != 0) {
                free(msglist);
        }
@@ -445,7 +434,7 @@ void imap_cleanup_function(void)
  * output this stuff in other places as well)
  */
 void imap_output_capability_string(void) {
-       cprintf("CAPABILITY IMAP4REV1 NAMESPACE AUTH=LOGIN");
+       cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=LOGIN");
 #ifdef HAVE_OPENSSL
        if (!CC->redirect_ssl) cprintf(" STARTTLS");
 #endif
@@ -464,6 +453,23 @@ void imap_capability(int num_parms, char *parms[])
 
 
 
+/*
+ * 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.
  */
@@ -658,20 +664,15 @@ void imap_select(int num_parms, char *parms[])
        }
 
        /* If we already had some other folder selected, auto-expunge it */
-       TRACE;
        imap_do_expunge();
 
        /*
         * usergoto() formally takes us to the desired room, happily returning
         * the number of messages and number of new messages.
         */
-       TRACE;
        memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
-       TRACE;
        usergoto(NULL, 0, 0, &msgs, &new);
-       TRACE;
        IMAP->selected = 1;
-       TRACE;
 
        if (!strcasecmp(parms[1], "EXAMINE")) {
                IMAP->readonly = 1;
@@ -679,11 +680,8 @@ void imap_select(int num_parms, char *parms[])
                IMAP->readonly = 0;
        }
 
-       TRACE;
        imap_load_msgids();
-       TRACE;
        IMAP->last_mtime = CC->room.QRmtime;
-       TRACE;
 
        cprintf("* %d EXISTS\r\n", msgs);
        cprintf("* %d RECENT\r\n", new);
@@ -696,7 +694,7 @@ void imap_select(int num_parms, char *parms[])
         * 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("%s OK [%s] %s completed\r\n",
@@ -707,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);
 }
 
@@ -1214,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]);
@@ -1368,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;
@@ -1391,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, " ");
 
@@ -1453,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 */
@@ -1594,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))
+       );
 }