]> 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 0e776b63a0ebdb0bd633f1db55c7256021f4e15a..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"
@@ -46,6 +58,8 @@
 #include "imap_tools.h"
 #include "imap_fetch.h"
 #include "imap_search.h"
+#include "imap_store.h"
+#include "imap_misc.h"
 
 
 long SYM_IMAP;
@@ -67,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;
@@ -111,10 +160,86 @@ 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);
 }
 
 
+/*
+ * Re-scan the selected room (folder) and see if it's been changed at all
+ */
+void imap_rescan_msgids(void) {
+
+       int original_num_msgs = 0;
+       long original_highest = 0L;
+       int i;
+       int count;
+
+       if (IMAP->selected == 0) {
+               lprintf(5, "imap_load_msgids() can't run; no room selected\n");
+               return;
+       }
+
+
+       /*
+        * Check to see if any of the messages we know about have been expunged
+        */
+       if (IMAP->num_msgs > 0)
+        for (i=0; i<IMAP->num_msgs; ++i) {
+
+               count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
+                       (-63), NULL, NULL, NULL, NULL);
+
+               if (count == 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.
+                        */
+                       --IMAP->num_msgs;
+                       memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
+                               (sizeof(long)*(IMAP->num_msgs-i)) );
+                       memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
+                               (sizeof(long)*(IMAP->num_msgs-i)) );
+
+                       --i;
+               }
+
+       }
+
+       /*
+        * Remember how many messages were here before we re-scanned.
+        */
+       original_num_msgs = IMAP->num_msgs;
+       if (IMAP->num_msgs > 0) {
+               original_highest = IMAP->msgids[IMAP->num_msgs - 1];
+       }
+       else {
+               original_highest = 0L;
+       }
+
+       /*
+        * Now peruse the room for *new* messages only.
+        */
+       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.
+        */
+       if (IMAP->num_msgs > original_num_msgs) {
+               cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
+       }
+
+}
+
+
+
+
+
 
 
 /*
@@ -128,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");
 }
 
@@ -231,7 +357,7 @@ void imap_capability(int num_parms, char *parms[]) {
  */
 void imap_select(int num_parms, char *parms[]) {
        char towhere[SIZ];
-       char augmented_roomname[SIZ];
+       char augmented_roomname[ROOMNAMELEN];
        int c = 0;
        int ok = 0;
        int ra = 0;
@@ -300,6 +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 \\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],
@@ -309,10 +437,47 @@ void imap_select(int num_parms, char *parms[]) {
 
 
 
+/*
+ * does the real work for expunge
+ */
+int imap_do_expunge(void) {
+       int i;
+       int num_expunged = 0;
+
+       if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
+               if (IMAP->flags[i] & IMAP_DELETED) {
+                       CtdlDeleteMessages(CC->quickroom.QRname,
+                                       IMAP->msgids[i], "");
+                       ++num_expunged;
+               }
+       }
+
+       if (num_expunged > 0) {
+               imap_rescan_msgids();
+       }
+
+       return(num_expunged);
+}
+
+
+/*
+ * implements the EXPUNGE command syntax
+ */
+void imap_expunge(int num_parms, char *parms[]) {
+       int num_expunged = 0;
+       imap_do_expunge();
+       cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
+}
+
+
 /*
  * implements the CLOSE command
  */
 void imap_close(int num_parms, char *parms[]) {
+
+       /* Yes, we always expunge on close. */
+       imap_do_expunge();
+
        IMAP->selected = 0;
        IMAP->readonly = 0;
        imap_free_msgids();
@@ -322,6 +487,26 @@ 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, char *pattern) {
+       int i;
+       struct floor *fl;
+
+       for (i=0; i<MAXFLOORS; ++i) {
+               fl = cgetfloor(i);
+               if (fl->f_flags & F_INUSE) {
+                       if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
+                               cprintf("* %s (\\NoSelect) \"|\" ", cmd);
+                               imap_strout(fl->f_name);
+                               cprintf("\r\n");
+                       }
+               }
+       }
+}
+
+
 
 /*
  * Back end for imap_lsub()
@@ -332,25 +517,43 @@ void imap_close(int num_parms, char *parms[]) {
 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
        char buf[SIZ];
        int ra;
+       char *pattern;
+
+       pattern = (char *)data;
 
        /* Only list rooms to which the user has access!! */
        ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
        if (ra & UA_KNOWN) {
                imap_mailboxname(buf, sizeof buf, qrbuf);
-               cprintf("* LSUB () \"|\" ");
-               imap_strout(buf);
-               cprintf("\r\n");
+               if (imap_mailbox_matches_pattern(pattern, buf)) {
+                       cprintf("* LSUB (\\NoInferiors) \"|\" ");
+                       imap_strout(buf);
+                       cprintf("\r\n");
+               }
        }
 }
 
 
 /*
  * Implements the LSUB command
- *
- * FIXME: Handle wildcards, please.
  */
 void imap_lsub(int num_parms, char *parms[]) {
-       ForEachRoom(imap_lsub_listroom, NULL);
+       char pattern[SIZ];
+       if (num_parms < 4) {
+               cprintf("%s BAD arguments invalid\r\n", parms[0]);
+               return;
+       }
+       sprintf(pattern, "%s%s", parms[2], parms[3]);
+
+       if (strlen(parms[3])==0) {
+               cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
+       }
+
+       else {
+               imap_list_floors("LSUB", pattern);
+               ForEachRoom(imap_lsub_listroom, pattern);
+       }
+
        cprintf("%s OK LSUB completed\r\n", parms[0]);
 }
 
@@ -362,38 +565,56 @@ void imap_lsub(int num_parms, char *parms[]) {
 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
        char buf[SIZ];
        int ra;
+       char *pattern;
+
+       pattern = (char *)data;
 
        /* Only list rooms to which the user has access!! */
        ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
        if ( (ra & UA_KNOWN) 
          || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
                imap_mailboxname(buf, sizeof buf, qrbuf);
-               cprintf("* LIST () \"|\" ");
-               imap_strout(buf);
-               cprintf("\r\n");
+               if (imap_mailbox_matches_pattern(pattern, buf)) {
+                       cprintf("* LIST (\\NoInferiors) \"|\" ");
+                       imap_strout(buf);
+                       cprintf("\r\n");
+               }
        }
 }
 
 
 /*
  * Implements the LIST command
- *
- * FIXME: Handle wildcards, please.
  */
 void imap_list(int num_parms, char *parms[]) {
-       ForEachRoom(imap_list_listroom, NULL);
+       char pattern[SIZ];
+       if (num_parms < 4) {
+               cprintf("%s BAD arguments invalid\r\n", parms[0]);
+               return;
+       }
+       sprintf(pattern, "%s%s", parms[2], parms[3]);
+
+       if (strlen(parms[3])==0) {
+               cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
+       }
+
+       else {
+               imap_list_floors("LIST", pattern);
+               ForEachRoom(imap_list_listroom, pattern);
+       }
+
        cprintf("%s OK LIST completed\r\n", parms[0]);
 }
 
 
 
 /*
- * Implements the CREATE command (FIXME not finished yet)
+ * Implements the CREATE command
  *
  */
 void imap_create(int num_parms, char *parms[]) {
        int ret;
-       char roomname[SIZ];
+       char roomname[ROOMNAMELEN];
        int floornum;
        int flags;
        int newroomtype;
@@ -414,7 +635,10 @@ void imap_create(int num_parms, char *parms[]) {
                newroomtype = 0;        /* public folder */
        }
 
-       ret = create_room(roomname, newroomtype, "", floornum);
+       lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
+               roomname, floornum, newroomtype);
+
+       ret = create_room(roomname, newroomtype, "", floornum, 1);
        if (ret == 0) {
                cprintf("%s NO Mailbox already exists, or create failed\r\n",
                        parms[0]);
@@ -425,6 +649,260 @@ void imap_create(int num_parms, char *parms[]) {
 }
 
 
+/*
+ * Locate a room by its IMAP folder name, and check access to it
+ */
+int imap_grabroom(char *returned_roomname, char *foldername) {
+       int ret;
+       char augmented_roomname[ROOMNAMELEN];
+       char roomname[ROOMNAMELEN];
+       int c;
+       struct quickroom QRscratch;
+       int ra;
+       int ok = 0;
+
+       ret = imap_roomname(roomname, sizeof roomname, foldername);
+       if (ret < 0) {
+               return(1);
+       }
+
+        /* First try a regular match */
+        c = getroom(&QRscratch, roomname);
+
+        /* Then try a mailbox name match */
+        if (c != 0) {
+                MailboxName(augmented_roomname, &CC->usersupp, roomname);
+                c = getroom(&QRscratch, augmented_roomname);
+                if (c == 0)
+                        strcpy(roomname, augmented_roomname);
+        }
+
+       /* If the room exists, check security/access */
+        if (c == 0) {
+                /* See if there is an existing user/room relationship */
+                ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
+
+                /* normal clients have to pass through security */
+                if (ra & UA_KNOWN) {
+                        ok = 1;
+               }
+       }
+
+       /* Fail here if no such room */
+       if (!ok) {
+               strcpy(returned_roomname, "");
+               return(2);
+       }
+       else {
+               strcpy(returned_roomname, QRscratch.QRname);
+               return(0);
+       }
+}
+
+
+/*
+ * Implements the STATUS command (sort of)
+ *
+ */
+void imap_status(int num_parms, char *parms[]) {
+       int ret;
+       char roomname[ROOMNAMELEN];
+       char buf[SIZ];
+       char savedroom[ROOMNAMELEN];
+       int msgs, new;
+
+       ret = imap_grabroom(roomname, parms[2]);
+       if (ret != 0) {
+               cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
+                       parms[0]);
+               return;
+       }
+
+       /*
+        * usergoto() formally takes us to the desired room, happily returning
+        * the number of messages and number of new messages.  (If another
+        * folder is selected, save its name so we can return there!!!!!)
+        */
+       if (IMAP->selected) {
+               strcpy(savedroom, CC->quickroom.QRname);
+       }
+       usergoto(roomname, 0, &msgs, &new);
+
+       /*
+        * Tell the client what it wants to know.  In fact, tell it *more* than
+        * 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.
+        */
+       imap_mailboxname(buf, sizeof buf, &CC->quickroom);
+       cprintf("* STATUS ");
+       imap_strout(buf);
+       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
+        * our happy day without violent explosions.
+        */
+       if (IMAP->selected) {
+               usergoto(savedroom, 0, &msgs, &new);
+       }
+
+       /*
+        * Oooh, look, we're done!
+        */
+       cprintf("%s OK STATUS completed\r\n", parms[0]);
+}
+
+
+
+/*
+ * Implements the SUBSCRIBE command
+ *
+ */
+void imap_subscribe(int num_parms, char *parms[]) {
+       int ret;
+       char roomname[ROOMNAMELEN];
+       char savedroom[ROOMNAMELEN];
+       int msgs, new;
+
+       ret = imap_grabroom(roomname, parms[2]);
+       if (ret != 0) {
+               cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
+                       parms[0]);
+               return;
+       }
+
+       /*
+        * usergoto() formally takes us to the desired room, which has the side
+        * effect of marking the room as not-zapped ... exactly the effect
+        * we're looking for.
+        */
+       if (IMAP->selected) {
+               strcpy(savedroom, CC->quickroom.QRname);
+       }
+       usergoto(roomname, 0, &msgs, &new);
+
+       /*
+        * If another folder is selected, go back to that room so we can resume
+        * our happy day without violent explosions.
+        */
+       if (IMAP->selected) {
+               usergoto(savedroom, 0, &msgs, &new);
+       }
+
+       cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
+}
+
+
+/*
+ * Implements the UNSUBSCRIBE command
+ *
+ */
+void imap_unsubscribe(int num_parms, char *parms[]) {
+       int ret;
+       char roomname[ROOMNAMELEN];
+       char savedroom[ROOMNAMELEN];
+       int msgs, new;
+
+       ret = imap_grabroom(roomname, parms[2]);
+       if (ret != 0) {
+               cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
+                       parms[0]);
+               return;
+       }
+
+       /*
+        * usergoto() formally takes us to the desired room.
+        */
+       if (IMAP->selected) {
+               strcpy(savedroom, CC->quickroom.QRname);
+       }
+       usergoto(roomname, 0, &msgs, &new);
+
+       /* 
+        * Now make the API call to zap the room
+        */
+       if (CtdlForgetThisRoom() == 0) {
+               cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
+       }
+       else {
+               cprintf("%s NO You may not unsubscribe from this folder.\r\n",
+                       parms[0]);
+       }
+
+       /*
+        * If another folder is selected, go back to that room so we can resume
+        * our happy day without violent explosions.
+        */
+       if (IMAP->selected) {
+               usergoto(savedroom, 0, &msgs, &new);
+       }
+}
+
+
+
+/*
+ * Implements the DELETE command
+ *
+ */
+void imap_delete(int num_parms, char *parms[]) {
+       int ret;
+       char roomname[ROOMNAMELEN];
+       char savedroom[ROOMNAMELEN];
+       int msgs, new;
+
+       ret = imap_grabroom(roomname, parms[2]);
+       if (ret != 0) {
+               cprintf("%s NO Invalid mailbox name, or access denied\r\n",
+                       parms[0]);
+               return;
+       }
+
+       /*
+        * usergoto() formally takes us to the desired room, happily returning
+        * the number of messages and number of new messages.  (If another
+        * folder is selected, save its name so we can return there!!!!!)
+        */
+       if (IMAP->selected) {
+               strcpy(savedroom, CC->quickroom.QRname);
+       }
+       usergoto(roomname, 0, &msgs, &new);
+
+       /*
+        * Now delete the room.
+        */
+       if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom)) {
+               cprintf("%s OK DELETE completed\r\n", parms[0]);
+               delete_room(&CC->quickroom);
+       }
+       else {
+               cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
+       }
+
+       /*
+        * If another folder is selected, go back to that room so we can resume
+        * our happy day without violent explosions.
+        */
+       if (IMAP->selected) {
+               usergoto(savedroom, 0, &msgs, &new);
+       }
+}
+
+
+
+/*
+ * Implements the RENAME command
+ *
+ */
+void imap_rename(int num_parms, char *parms[]) {
+       cprintf("%s NO The RENAME command is not yet implemented (FIXME)\r\n",
+               parms[0]);
+}
+
+
 
 /* 
  * Main command loop for IMAP sessions.
@@ -462,7 +940,22 @@ void imap_command_loop(void) {
        }
 
 
-       /* Ok, at this point we're in normal command mode */
+       /* Ok, at this point we're in normal command mode.  The first thing
+        * we do is print any incoming pages (yeah! we really do!)
+        */
+       imap_print_express_messages();
+
+       /*
+        * 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...
+        */
+       if (IMAP->selected) {
+               imap_rescan_msgids();
+       }
+
+       /* Now for the command set. */
 
        /* Grab the tag, command, and parameters.  Check syntax. */
        num_parms = imap_parameterize(parms, cmdbuf);
@@ -523,6 +1016,30 @@ void imap_command_loop(void) {
                imap_create(num_parms, parms);
        }
 
+       else if (!strcasecmp(parms[1], "DELETE")) {
+               imap_delete(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "RENAME")) {
+               imap_rename(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "STATUS")) {
+               imap_status(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
+               imap_subscribe(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
+               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]);
        }
@@ -547,6 +1064,28 @@ void imap_command_loop(void) {
                imap_uidsearch(num_parms, parms);
        }
 
+       else if (!strcasecmp(parms[1], "STORE")) {
+               imap_store(num_parms, parms);
+       }
+
+       else if ( (!strcasecmp(parms[1], "UID"))
+               && (!strcasecmp(parms[2], "STORE")) ) {
+               imap_uidstore(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "COPY")) {
+               imap_copy(num_parms, parms);
+       }
+
+       else if ( (!strcasecmp(parms[1], "UID"))
+               && (!strcasecmp(parms[2], "COPY")) ) {
+               imap_uidcopy(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "EXPUNGE")) {
+               imap_expunge(num_parms, parms);
+       }
+
        else if (!strcasecmp(parms[1], "CLOSE")) {
                imap_close(num_parms, parms);
        }
@@ -559,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();
 }