]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_imap.c
* Full text indexer is now switchable on/off
[citadel.git] / citadel / serv_imap.c
index 2eaa0b3be5c6ca169242ee707a984b0428d1acbb..c4bd19dd7ece64eb22227b4cfb3036968403dc20 100644 (file)
@@ -91,6 +91,7 @@ void imap_free_msgids(void)
                free(IMAP->flags);
                IMAP->flags = NULL;
        }
+       IMAP->last_mtime = (-1);
 }
 
 
@@ -108,30 +109,81 @@ void imap_free_transmitted_message(void)
 
 
 /*
- * Set the \\Seen flag for messages which aren't new
+ * Set the \Seen, \Recent. and \Answered flags, based on the sequence
+ * 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.
  */
 void imap_set_seen_flags(void)
 {
        struct visit vbuf;
        int i;
+       int num_sets;
+       int s;
+       char setstr[SIZ], lostr[SIZ], histr[SIZ];
+       long lo, hi;
 
+       if (IMAP->num_msgs < 1) return;
        CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
-       if (IMAP->num_msgs > 0) {
+
+       for (i = 0; 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".) */
+       num_sets = num_tokens(vbuf.v_seen, ',');
+       for (s=0; s<num_sets; ++s) {
+               extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
+
+               extract_token(lostr, setstr, 0, ':', sizeof lostr);
+               if (num_tokens(setstr, ':') >= 2) {
+                       extract_token(histr, setstr, 1, ':', sizeof histr);
+                       if (!strcmp(histr, "*")) {
+                               snprintf(histr, sizeof histr, "%ld", LONG_MAX);
+                       }
+               } 
+               else {
+                       strcpy(histr, lostr);
+               }
+               lo = atol(lostr);
+               hi = atol(histr);
+
                for (i = 0; i < IMAP->num_msgs; ++i) {
-                       if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
+                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)) {
                                IMAP->flags[i] |= IMAP_SEEN;
+                               IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
                        }
-                       else {
-                               IMAP->flags[i] |= IMAP_RECENT;
+               }
+       }
+
+       /* Do the ANSWERED flag */
+       num_sets = num_tokens(vbuf.v_answered, ',');
+       for (s=0; s<num_sets; ++s) {
+               extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
+
+               extract_token(lostr, setstr, 0, ':', sizeof lostr);
+               if (num_tokens(setstr, ':') >= 2) {
+                       extract_token(histr, setstr, 1, ':', sizeof histr);
+                       if (!strcmp(histr, "*")) {
+                               snprintf(histr, sizeof histr, "%ld", LONG_MAX);
                        }
-                       if (is_msg_in_mset
-                           (vbuf.v_answered, IMAP->msgids[i])) {
+               } 
+               else {
+                       strcpy(histr, lostr);
+               }
+               lo = atol(lostr);
+               hi = atol(histr);
+
+               for (i = 0; i < IMAP->num_msgs; ++i) {
+                       if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)) {
                                IMAP->flags[i] |= IMAP_ANSWERED;
                        }
                }
        }
-}
 
+}
 
 
 
@@ -175,8 +227,6 @@ void imap_load_msgids(void)
                           imap_add_single_msgid, NULL);
 
        imap_set_seen_flags();
-       /* lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
-               IMAP->num_msgs); */
 }
 
 
@@ -195,13 +245,21 @@ void imap_rescan_msgids(void)
        int num_msgs = 0;
        int num_recent = 0;
 
-
        if (IMAP->selected == 0) {
                lprintf(CTDL_ERR,
                        "imap_load_msgids() can't run; no room selected\n");
                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.
         */
@@ -236,9 +294,10 @@ void imap_rescan_msgids(void)
                        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],
@@ -268,12 +327,13 @@ 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();
 
        /*
@@ -291,8 +351,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;
 }
 
 
@@ -325,6 +387,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) {
@@ -339,6 +402,29 @@ 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 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]);
+}
+
+
 
 /*
  * Here's where our IMAP session begins its happy day.
@@ -352,9 +438,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);
 }
 
 /*
@@ -380,7 +468,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;
                }
        }
@@ -449,23 +539,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)
  */
@@ -564,10 +637,14 @@ 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.
@@ -576,7 +653,6 @@ void imap_select(int num_parms, char *parms[])
        cprintf("* OK [PERMANENTFLAGS (\\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]);
@@ -1113,8 +1189,8 @@ void imap_delete(int num_parms, char *parms[])
         * Now delete the room.
         */
        if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
+               schedule_room_for_deletion(&CC->room);
                cprintf("%s OK DELETE completed\r\n", parms[0]);
-               delete_room(&CC->room);
        } else {
                cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
        }