* Set zapped_ok to 1 on nearly all calls to imap_grabroom(), because the IMAP command...
[citadel.git] / citadel / modules / imap / serv_imap.c
index b37324da554d1fd716e161863f625310debac764..274022090ed9dea1fcd90f11350bbb6227f92dca 100644 (file)
@@ -447,11 +447,20 @@ 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")
+        */
 }
 
 /*
@@ -498,6 +507,12 @@ void imap_greeting(void)
        IMAP->cached_rfc822_msgnum = (-1);
        IMAP->cached_rfc822_withbody = 0;
 
+       if (CC->nologin)
+       {
+               cprintf("* BYE; Server busy, try later\r\n");
+               CC->kill_me = 1;
+               return;
+       }
        cprintf("* OK [");
        imap_output_capability_string();
        cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
@@ -741,13 +756,17 @@ void imap_select(int num_parms, char *parms[])
        cprintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE);
        cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
 
-       /* Note that \Deleted is a valid flag, but not a permanent flag,
+       /* Technically, \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.
+        * 
+        * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes
+        * some clients (particularly Thunderbird) to misbehave -- they simply
+        * 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");
-       cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] "
-               "permanent flags\r\n");
+       cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n");
 
        cprintf("%s OK [%s] %s completed\r\n",
                parms[0],
@@ -877,6 +896,7 @@ void imap_create(int num_parms, char *parms[])
        int flags;
        int newroomtype = 0;
        int newroomview = 0;
+       char *notification_message = NULL;
 
        if (strchr(parms[2], '\\') != NULL) {
                cprintf("%s NO Invalid character in folder name\r\n",
@@ -920,6 +940,18 @@ void imap_create(int num_parms, char *parms[])
                cprintf("%s NO Mailbox already exists, or create failed\r\n", parms[0]);
        } else {
                cprintf("%s OK CREATE completed\r\n", parms[0]);
+               /* post a message in Aide> describing the new room */
+               notification_message = malloc(1024);
+               snprintf(notification_message, 1024,
+                       "A new room called \"%s\" has been created by %s%s%s%s\n",
+                       roomname,
+                       CC->user.fullname,
+                       ((ret & QR_MAILBOX) ? " [personal]" : ""),
+                       ((ret & QR_PRIVATE) ? " [private]" : ""),
+                       ((ret & QR_GUESSNAME) ? " [hidden]" : "")
+               );
+               aide_message(notification_message, "Room Creation Message");
+               free(notification_message);
        }
        CtdlLogPrintf(CTDL_DEBUG, "imap_create() completed\n");
 }
@@ -993,7 +1025,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",
@@ -1095,7 +1127,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",
@@ -1329,6 +1361,7 @@ void imap_command_loop(void)
        int num_parms;
        struct timeval tv1, tv2;
        suseconds_t total_time = 0;
+       int untagged_ok = 1;
 
        gettimeofday(&tv1, NULL);
        CC->lastcmd = time(NULL);
@@ -1382,15 +1415,32 @@ void imap_command_loop(void)
         * If the command just submitted does not contain a literal, we
         * might think about delivering some untagged stuff...
         */
-       if (cmdbuf[strlen(cmdbuf)-1] != '}') {
+       if (cmdbuf[strlen(cmdbuf)-1] == '}') {
+               untagged_ok = 0;
+       }
 
-               imap_print_instant_messages();
+       /* Grab the tag, command, and parameters. */
+       num_parms = imap_parameterize(parms, cmdbuf);
+
+       /* RFC3501 says that we cannot output untagged data during these commands */
+       if (num_parms >= 2) {
+               if (  (!strcasecmp(parms[1], "FETCH"))
+                  || (!strcasecmp(parms[1], "STORE"))
+                  || (!strcasecmp(parms[1], "SEARCH"))
+               ) {
+                       untagged_ok = 0;
+               }
+       }
        
+       if (untagged_ok) {
+
+               /* we can put any additional untagged stuff right here in the future */
+
                /*
                 * Before processing the command that was just entered... if we happen
                 * to have a folder selected, we'd like to rescan that folder for new
                 * messages, and for deletions/changes of existing messages.  This
-                * could probably be optimized somehow, but IMAP sucks...
+                * could probably be optimized better with some deep thought...
                 */
                if (IMAP->selected) {
                        imap_rescan_msgids();
@@ -1399,8 +1449,6 @@ void imap_command_loop(void)
 
        /* Now for the command set. */
 
-       /* Grab the tag, command, and parameters.  Check syntax. */
-       num_parms = imap_parameterize(parms, cmdbuf);
        if (num_parms < 2) {
                cprintf("BAD syntax error\r\n");
        }
@@ -1420,7 +1468,7 @@ void imap_command_loop(void)
 
        else if (!strcasecmp(parms[1], "LOGOUT")) {
                if (IMAP->selected) {
-                       imap_do_expunge();      /* yes, we auto-expunge */
+                       imap_do_expunge();      /* yes, we auto-expunge at logout */
                }
                cprintf("* BYE %s logging out\r\n", config.c_fqdn);
                cprintf("%s OK Citadel IMAP session ended.\r\n",
@@ -1593,6 +1641,7 @@ 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)