]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_imap.c
fix all the <time.h> vs. <sys/time.h> issues, hopefully
[citadel.git] / citadel / serv_imap.c
index 9edfa2aa0453ec3d06dc37197f44902716a6cb36..b51141b393959aadffa862a6ed36afa8cbb0718c 100644 (file)
@@ -5,8 +5,10 @@
  * Copyright (C) 2000-2001 by Art Cancro and others.
  * This code is released under the terms of the GNU General Public License.
  *
- * WARNING: this is an incomplete implementation, still in progress.  Parts of
- * it work, but it's not really usable yet from a user perspective.
+ * WARNING: this is an incomplete implementation.  It is now good enough to
+ * be usable with much of the popular IMAP client software available, but it
+ * is by no means perfect.  Some commands (particularly SEARCH and RENAME)
+ * are implemented either incompletely or not at all.
  *
  * WARNING: Mark Crispin is an idiot.  IMAP is the most brain-damaged protocol
  * you will ever have the profound lack of pleasure to encounter.
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include <time.h>
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
@@ -69,28 +81,63 @@ void imap_free_msgids(void) {
 }
 
 
+/*
+ * If there is a transmitted message in memory, free it
+ */
+void imap_free_transmitted_message(void) {
+       if (IMAP->transmitted_message != NULL) {
+               phree(IMAP->transmitted_message);
+               IMAP->transmitted_message = NULL;
+               IMAP->transmitted_length = 0;
+       }
+}
+
+
+/*
+ * Set the \\Seen flag for messages which aren't new
+ */
+void imap_set_seen_flags(void) {
+       struct visit vbuf;
+       int i;
+
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       if (IMAP->num_msgs > 0) {
+               for (i=0; i<IMAP->num_msgs; ++i) {
+                       if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
+                               IMAP->flags[i] |= IMAP_SEEN;
+                       }
+               }
+       }
+}
+
+
+
+
 /*
  * Back end for imap_load_msgids()
  *
- * FIXME: this should be optimized by figuring out a way to allocate memory
- * once rather than doing a reallok() for each message.
+ * Optimization: instead of calling realloc() to add each message, we
+ * allocate space in the list for REALLOC_INCREMENT messages at a time.  This
+ * allows the mapping to proceed much faster.
  */
 void imap_add_single_msgid(long msgnum, void *userdata) {
        
        IMAP->num_msgs = IMAP->num_msgs + 1;
        if (IMAP->msgids == NULL) {
-               IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
+               IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
+                                       * REALLOC_INCREMENT);
        }
-       else {
+       else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
                IMAP->msgids = reallok(IMAP->msgids,
-                       IMAP->num_msgs * sizeof(long));
+                       (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
        }
        if (IMAP->flags == NULL) {
-               IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
+               IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
+                                       * REALLOC_INCREMENT);
        }
-       else {
+       else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
                IMAP->flags = reallok(IMAP->flags,
-                       IMAP->num_msgs * sizeof(long));
+                       (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
        }
        IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
        IMAP->flags[IMAP->num_msgs - 1] = 0;
@@ -113,6 +160,7 @@ void imap_load_msgids(void) {
        CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
                imap_add_single_msgid, NULL);
 
+       imap_set_seen_flags();
        lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
 }
 
@@ -177,6 +225,8 @@ void imap_rescan_msgids(void) {
        CtdlForEachMessage(MSGS_GT, original_highest, (-63), NULL, NULL,
                imap_add_single_msgid, NULL);
 
+       imap_set_seen_flags();
+
        /*
         * If new messages have arrived, tell the client about them.
         */
@@ -203,6 +253,7 @@ void imap_cleanup_function(void) {
 
        lprintf(9, "Performing IMAP cleanup hook\n");
        imap_free_msgids();
+       imap_free_transmitted_message();
        lprintf(9, "Finished IMAP cleanup hook\n");
 }
 
@@ -375,8 +426,8 @@ void imap_select(int num_parms, char *parms[]) {
        /* FIXME ... much more info needs to be supplied here */
        cprintf("* %d EXISTS\r\n", msgs);
        cprintf("* %d RECENT\r\n", new);
-       cprintf("* FLAGS (\\Deleted)\r\n");
-       cprintf("* OK [PERMANENTFLAGS (\\Deleted)] permanent flags\r\n");
+       cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
+       cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
        cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
        cprintf("%s OK [%s] %s completed\r\n",
                parms[0],
@@ -439,16 +490,18 @@ void imap_close(int num_parms, char *parms[]) {
 /*
  * Used by LIST and LSUB to show the floors in the listing
  */
-void imap_list_floors(char *cmd) {
+void imap_list_floors(char *cmd, char *pattern) {
        int i;
        struct floor *fl;
 
        for (i=0; i<MAXFLOORS; ++i) {
                fl = cgetfloor(i);
                if (fl->f_flags & F_INUSE) {
-                       cprintf("* %s (\\NoSelect) \"|\" ", cmd);
-                       imap_strout(fl->f_name);
-                       cprintf("\r\n");
+                       if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
+                               cprintf("* %s (\\NoSelect) \"|\" ", cmd);
+                               imap_strout(fl->f_name);
+                               cprintf("\r\n");
+                       }
                }
        }
 }
@@ -467,7 +520,6 @@ void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
        char *pattern;
 
        pattern = (char *)data;
-       lprintf(9, "lsub pattern: <%s>\n", pattern);
 
        /* Only list rooms to which the user has access!! */
        ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
@@ -484,8 +536,6 @@ void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
 
 /*
  * Implements the LSUB command
- *
- * FIXME: Handle wildcards, please.
  */
 void imap_lsub(int num_parms, char *parms[]) {
        char pattern[SIZ];
@@ -500,7 +550,7 @@ void imap_lsub(int num_parms, char *parms[]) {
        }
 
        else {
-               imap_list_floors("LSUB");
+               imap_list_floors("LSUB", pattern);
                ForEachRoom(imap_lsub_listroom, pattern);
        }
 
@@ -518,7 +568,6 @@ void imap_list_listroom(struct quickroom *qrbuf, void *data) {
        char *pattern;
 
        pattern = (char *)data;
-       lprintf(9, "list pattern: <%s>\n", pattern);
 
        /* Only list rooms to which the user has access!! */
        ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
@@ -536,8 +585,6 @@ void imap_list_listroom(struct quickroom *qrbuf, void *data) {
 
 /*
  * Implements the LIST command
- *
- * FIXME: Handle wildcards, please.
  */
 void imap_list(int num_parms, char *parms[]) {
        char pattern[SIZ];
@@ -552,7 +599,7 @@ void imap_list(int num_parms, char *parms[]) {
        }
 
        else {
-               imap_list_floors("LIST");
+               imap_list_floors("LIST", pattern);
                ForEachRoom(imap_list_listroom, pattern);
        }
 
@@ -686,18 +733,14 @@ void imap_status(int num_parms, char *parms[]) {
         * it wants to know.  We happily IGnore the supplied status data item
         * names and simply spew all possible data items.  It's far easier to
         * code and probably saves us some processing time too.
-        *
-        * FIXME we need to implement RECENT and UNSEEN eventually...
         */
-
        imap_mailboxname(buf, sizeof buf, &CC->quickroom);
        cprintf("* STATUS ");
        imap_strout(buf);
-       cprintf(" (MESSAGES %d RECENT 0 UIDNEXT %ld "
-               "UIDVALIDITY 0 UNSEEN 0)\r\n",
-               msgs,
-               CitControl.MMhighest + 1
-       );
+       cprintf(" (MESSAGES %d ", msgs);
+       cprintf("RECENT 0 ");   /* FIXME we need to implement this */
+       cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
+       cprintf("UNSEEN %d)\r\n", new);
 
        /*
         * If another folder is selected, go back to that room so we can resume
@@ -993,6 +1036,10 @@ void imap_command_loop(void) {
                imap_unsubscribe(num_parms, parms);
        }
 
+       else if (!strcasecmp(parms[1], "APPEND")) {
+               imap_append(num_parms, parms);
+       }
+
        else if (IMAP->selected == 0) {
                cprintf("%s BAD no folder selected\r\n", parms[0]);
        }
@@ -1051,6 +1098,8 @@ void imap_command_loop(void) {
                cprintf("%s BAD command unrecognized\r\n", parms[0]);
        }
 
+       /* If the client transmitted a message we can free it now */
+       imap_free_transmitted_message();
 }