4 * IMAP server for the Citadel/UX system
5 * Copyright (C) 2000-2002 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * WARNING: Mark Crispin is an idiot. IMAP is the most brain-damaged protocol
9 * you will ever have the profound lack of pleasure to encounter.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/rand.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
50 #include "serv_extensions.h"
57 #include "internet_addressing.h"
58 #include "serv_imap.h"
59 #include "imap_tools.h"
60 #include "imap_fetch.h"
61 #include "imap_search.h"
62 #include "imap_store.h"
63 #include "imap_misc.h"
66 #include "serv_crypto.h"
69 /* imap_rename() uses this struct containing list of rooms to rename */
72 char irl_oldroom[ROOMNAMELEN];
73 char irl_newroom[ROOMNAMELEN];
77 /* Data which is passed between imap_rename() and imap_rename_backend() */
86 * If there is a message ID map in memory, free it
88 void imap_free_msgids(void)
90 if (IMAP->msgids != NULL) {
95 if (IMAP->flags != NULL) {
103 * If there is a transmitted message in memory, free it
105 void imap_free_transmitted_message(void)
107 if (IMAP->transmitted_message != NULL) {
108 phree(IMAP->transmitted_message);
109 IMAP->transmitted_message = NULL;
110 IMAP->transmitted_length = 0;
116 * Set the \\Seen flag for messages which aren't new
118 void imap_set_seen_flags(void)
123 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
124 if (IMAP->num_msgs > 0) {
125 for (i = 0; i < IMAP->num_msgs; ++i) {
126 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
127 IMAP->flags[i] |= IMAP_SEEN;
130 (vbuf.v_answered, IMAP->msgids[i])) {
131 IMAP->flags[i] |= IMAP_ANSWERED;
141 * Back end for imap_load_msgids()
143 * Optimization: instead of calling realloc() to add each message, we
144 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
145 * allows the mapping to proceed much faster.
147 void imap_add_single_msgid(long msgnum, void *userdata)
150 IMAP->num_msgs = IMAP->num_msgs + 1;
151 if (IMAP->msgids == NULL) {
152 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
153 * REALLOC_INCREMENT);
154 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
155 IMAP->msgids = reallok(IMAP->msgids,
157 REALLOC_INCREMENT) * sizeof(long));
159 if (IMAP->flags == NULL) {
160 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
161 * REALLOC_INCREMENT);
162 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
163 IMAP->flags = reallok(IMAP->flags,
165 REALLOC_INCREMENT) * sizeof(long));
167 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
168 IMAP->flags[IMAP->num_msgs - 1] = 0;
174 * Set up a message ID map for the current room (folder)
176 void imap_load_msgids(void)
179 if (IMAP->selected == 0) {
181 "imap_load_msgids() can't run; no room selected\n");
185 imap_free_msgids(); /* If there was already a map, free it */
187 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
188 imap_add_single_msgid, NULL);
190 imap_set_seen_flags();
191 lprintf(9, "imap_load_msgids() mapped %d messages\n",
197 * Re-scan the selected room (folder) and see if it's been changed at all
199 void imap_rescan_msgids(void)
202 int original_num_msgs = 0;
203 long original_highest = 0L;
205 int message_still_exists;
206 struct cdbdata *cdbfr;
207 long *msglist = NULL;
211 if (IMAP->selected == 0) {
213 "imap_load_msgids() can't run; no room selected\n");
217 /* Load the *current* message list from disk, so we can compare it
218 * to what we have in memory.
220 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
222 msglist = mallok(cdbfr->len);
223 memcpy(msglist, cdbfr->ptr, cdbfr->len);
224 num_msgs = cdbfr->len / sizeof(long);
231 * Check to see if any of the messages we know about have been expunged
233 if (IMAP->num_msgs > 0)
234 for (i = 0; i < IMAP->num_msgs; ++i) {
236 message_still_exists = 0;
238 for (j = 0; j < num_msgs; ++j) {
239 if (msglist[j] == IMAP->msgids[i]) {
240 message_still_exists = 1;
244 if (message_still_exists == 0) {
245 cprintf("* %d EXPUNGE\r\n", i + 1);
247 /* Here's some nice stupid nonsense. When a message
248 * is expunged, we have to slide all the existing
249 * messages up in the message array.
252 memcpy(&IMAP->msgids[i],
253 &IMAP->msgids[i + 1],
255 (IMAP->num_msgs - i)));
256 memcpy(&IMAP->flags[i],
259 (IMAP->num_msgs - i)));
267 * Remember how many messages were here before we re-scanned.
269 original_num_msgs = IMAP->num_msgs;
270 if (IMAP->num_msgs > 0) {
271 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
273 original_highest = 0L;
277 * Now peruse the room for *new* messages only.
280 for (j = 0; j < num_msgs; ++j) {
281 if (msglist[j] > original_highest) {
282 imap_add_single_msgid(msglist[j], NULL);
285 imap_set_seen_flags();
288 * If new messages have arrived, tell the client about them.
290 if (IMAP->num_msgs > original_num_msgs) {
291 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
305 * This cleanup function blows away the temporary memory and files used by
308 void imap_cleanup_function(void)
311 /* Don't do this stuff if this is not a IMAP session! */
312 if (CC->h_command_function != imap_command_loop)
315 lprintf(9, "Performing IMAP cleanup hook\n");
317 imap_free_transmitted_message();
318 lprintf(9, "Finished IMAP cleanup hook\n");
324 * Here's where our IMAP session begins its happy day.
326 void imap_greeting(void)
329 strcpy(CC->cs_clientname, "IMAP session");
330 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
331 IMAP->authstate = imap_as_normal;
333 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
339 * implements the LOGIN command (ordinary username/password login)
341 void imap_login(int num_parms, char *parms[])
343 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
344 if (CtdlTryPassword(parms[3]) == pass_ok) {
345 cprintf("%s OK login successful\r\n", parms[0]);
350 cprintf("%s BAD Login incorrect\r\n", parms[0]);
355 * Implements the AUTHENTICATE command
357 void imap_authenticate(int num_parms, char *parms[])
361 if (num_parms != 3) {
362 cprintf("%s BAD incorrect number of parameters\r\n",
368 cprintf("%s BAD Already logged in.\r\n", parms[0]);
372 if (!strcasecmp(parms[2], "LOGIN")) {
373 CtdlEncodeBase64(buf, "Username:", 9);
374 cprintf("+ %s\r\n", buf);
375 IMAP->authstate = imap_as_expecting_username;
376 strcpy(IMAP->authseq, parms[0]);
381 cprintf("%s NO AUTHENTICATE %s failed\r\n",
386 void imap_auth_login_user(char *cmd)
390 CtdlDecodeBase64(buf, cmd, SIZ);
391 CtdlLoginExistingUser(buf);
392 CtdlEncodeBase64(buf, "Password:", 9);
393 cprintf("+ %s\r\n", buf);
394 IMAP->authstate = imap_as_expecting_password;
398 void imap_auth_login_pass(char *cmd)
402 CtdlDecodeBase64(buf, cmd, SIZ);
403 if (CtdlTryPassword(buf) == pass_ok) {
404 cprintf("%s OK authentication succeeded\r\n",
407 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
409 IMAP->authstate = imap_as_normal;
415 * implements the CAPABILITY command
417 void imap_capability(int num_parms, char *parms[])
419 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
422 cprintf(" STARTTLS");
426 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
432 * implements the STARTTLS command (Citadel API version)
435 void imap_starttls(int num_parms, char *parms[])
437 char ok_response[SIZ];
438 char nosup_response[SIZ];
439 char error_response[SIZ];
442 "%s OK begin TLS negotiation now\r\n",
444 sprintf(nosup_response,
445 "%s NO TLS not supported here\r\n",
447 sprintf(error_response,
448 "%s BAD Internal error\r\n",
450 CtdlStartTLS(ok_response, nosup_response, error_response);
456 * implements the SELECT command
458 void imap_select(int num_parms, char *parms[])
461 char augmented_roomname[ROOMNAMELEN];
465 struct ctdlroom QRscratch;
471 /* Convert the supplied folder name to a roomname */
472 i = imap_roomname(towhere, sizeof towhere, parms[2]);
474 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
478 floornum = (i & 0x00ff);
479 roomflags = (i & 0xff00);
481 /* First try a regular match */
482 c = getroom(&QRscratch, towhere);
484 /* Then try a mailbox name match */
486 MailboxName(augmented_roomname, sizeof augmented_roomname,
488 c = getroom(&QRscratch, augmented_roomname);
490 strcpy(towhere, augmented_roomname);
493 /* If the room exists, check security/access */
495 /* See if there is an existing user/room relationship */
496 ra = CtdlRoomAccess(&QRscratch, &CC->user);
498 /* normal clients have to pass through security */
504 /* Fail here if no such room */
506 cprintf("%s NO ... no such room, or access denied\r\n",
508 /* IMAP->selected = 0; */
512 /* If we already had some other folder selected, auto-expunge it */
516 * usergoto() formally takes us to the desired room, happily returning
517 * the number of messages and number of new messages.
519 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
520 usergoto(NULL, 0, 0, &msgs, &new);
523 if (!strcasecmp(parms[1], "EXAMINE")) {
531 cprintf("* %d EXISTS\r\n", msgs);
532 cprintf("* %d RECENT\r\n", new);
534 /* Note that \Deleted is a valid flag, but not a permanent flag,
535 * because we don't maintain its state across sessions. Citadel
536 * automatically expunges mailboxes when they are de-selected.
538 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
539 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
540 "permanent flags\r\n");
542 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
543 cprintf("%s OK [%s] %s completed\r\n",
545 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
551 * does the real work for expunge
553 int imap_do_expunge(void)
556 int num_expunged = 0;
558 lprintf(9, "imap_do_expunge() called\n");
559 if (IMAP->selected == 0)
562 if (IMAP->num_msgs > 0)
563 for (i = 0; i < IMAP->num_msgs; ++i) {
564 if (IMAP->flags[i] & IMAP_DELETED) {
565 CtdlDeleteMessages(CC->room.QRname,
566 IMAP->msgids[i], "");
568 lprintf(9, "%ld ... deleted\n",
571 lprintf(9, "%ld ... not deleted\n",
576 if (num_expunged > 0) {
577 imap_rescan_msgids();
580 return (num_expunged);
585 * implements the EXPUNGE command syntax
587 void imap_expunge(int num_parms, char *parms[])
589 int num_expunged = 0;
591 num_expunged = imap_do_expunge();
592 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
597 * implements the CLOSE command
599 void imap_close(int num_parms, char *parms[])
602 /* Yes, we always expunge on close. */
608 cprintf("%s OK CLOSE completed\r\n", parms[0]);
615 * Used by LIST and LSUB to show the floors in the listing
617 void imap_list_floors(char *cmd, char *pattern)
622 for (i = 0; i < MAXFLOORS; ++i) {
624 if (fl->f_flags & F_INUSE) {
625 if (imap_mailbox_matches_pattern
626 (pattern, fl->f_name)) {
627 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
628 imap_strout(fl->f_name);
638 * Back end for imap_lsub()
640 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
641 * may or may not be the desired behavior in the future.
643 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
649 pattern = (char *) data;
651 /* Only list rooms to which the user has access!! */
652 ra = CtdlRoomAccess(qrbuf, &CC->user);
654 imap_mailboxname(buf, sizeof buf, qrbuf);
655 if (imap_mailbox_matches_pattern(pattern, buf)) {
656 cprintf("* LSUB () \"|\" ");
665 * Implements the LSUB command
667 void imap_lsub(int num_parms, char *parms[])
671 cprintf("%s BAD arguments invalid\r\n", parms[0]);
674 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
676 if (strlen(parms[3]) == 0) {
677 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
681 imap_list_floors("LSUB", pattern);
682 ForEachRoom(imap_lsub_listroom, pattern);
685 cprintf("%s OK LSUB completed\r\n", parms[0]);
691 * Back end for imap_list()
693 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
699 pattern = (char *) data;
701 /* Only list rooms to which the user has access!! */
702 ra = CtdlRoomAccess(qrbuf, &CC->user);
704 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
705 imap_mailboxname(buf, sizeof buf, qrbuf);
706 if (imap_mailbox_matches_pattern(pattern, buf)) {
707 cprintf("* LIST () \"|\" ");
716 * Implements the LIST command
718 void imap_list(int num_parms, char *parms[])
722 cprintf("%s BAD arguments invalid\r\n", parms[0]);
725 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
727 if (strlen(parms[3]) == 0) {
728 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
732 imap_list_floors("LIST", pattern);
733 ForEachRoom(imap_list_listroom, pattern);
736 cprintf("%s OK LIST completed\r\n", parms[0]);
742 * Implements the CREATE command
745 void imap_create(int num_parms, char *parms[])
748 char roomname[ROOMNAMELEN];
753 if (strchr(parms[2], '\\') != NULL) {
754 cprintf("%s NO Invalid character in folder name\r\n",
759 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
761 cprintf("%s NO Invalid mailbox name or location\r\n",
765 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
766 flags = (ret & 0xff00); /* upper 8 bits = flags */
768 if (flags & IR_MAILBOX) {
769 newroomtype = 4; /* private mailbox */
771 newroomtype = 0; /* public folder */
774 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
775 roomname, floornum, newroomtype);
777 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
780 ("%s NO Mailbox already exists, or create failed\r\n",
783 cprintf("%s OK CREATE completed\r\n", parms[0]);
789 * Locate a room by its IMAP folder name, and check access to it
791 int imap_grabroom(char *returned_roomname, char *foldername)
794 char augmented_roomname[ROOMNAMELEN];
795 char roomname[ROOMNAMELEN];
797 struct ctdlroom QRscratch;
801 ret = imap_roomname(roomname, sizeof roomname, foldername);
806 /* First try a regular match */
807 c = getroom(&QRscratch, roomname);
809 /* Then try a mailbox name match */
811 MailboxName(augmented_roomname, sizeof augmented_roomname,
812 &CC->user, roomname);
813 c = getroom(&QRscratch, augmented_roomname);
815 strcpy(roomname, augmented_roomname);
818 /* If the room exists, check security/access */
820 /* See if there is an existing user/room relationship */
821 ra = CtdlRoomAccess(&QRscratch, &CC->user);
823 /* normal clients have to pass through security */
829 /* Fail here if no such room */
831 strcpy(returned_roomname, "");
834 strcpy(returned_roomname, QRscratch.QRname);
841 * Implements the STATUS command (sort of)
844 void imap_status(int num_parms, char *parms[])
847 char roomname[ROOMNAMELEN];
849 char savedroom[ROOMNAMELEN];
852 ret = imap_grabroom(roomname, parms[2]);
855 ("%s NO Invalid mailbox name or location, or access denied\r\n",
861 * usergoto() formally takes us to the desired room, happily returning
862 * the number of messages and number of new messages. (If another
863 * folder is selected, save its name so we can return there!!!!!)
865 if (IMAP->selected) {
866 strcpy(savedroom, CC->room.QRname);
868 usergoto(roomname, 0, 0, &msgs, &new);
871 * Tell the client what it wants to know. In fact, tell it *more* than
872 * it wants to know. We happily IGnore the supplied status data item
873 * names and simply spew all possible data items. It's far easier to
874 * code and probably saves us some processing time too.
876 imap_mailboxname(buf, sizeof buf, &CC->room);
877 cprintf("* STATUS ");
879 cprintf(" (MESSAGES %d ", msgs);
880 cprintf("RECENT 0 "); /* FIXME we need to implement this */
881 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
882 cprintf("UNSEEN %d)\r\n", new);
885 * If another folder is selected, go back to that room so we can resume
886 * our happy day without violent explosions.
888 if (IMAP->selected) {
889 usergoto(savedroom, 0, 0, &msgs, &new);
893 * Oooh, look, we're done!
895 cprintf("%s OK STATUS completed\r\n", parms[0]);
901 * Implements the SUBSCRIBE command
904 void imap_subscribe(int num_parms, char *parms[])
907 char roomname[ROOMNAMELEN];
908 char savedroom[ROOMNAMELEN];
911 ret = imap_grabroom(roomname, parms[2]);
914 ("%s NO Invalid mailbox name or location, or access denied\r\n",
920 * usergoto() formally takes us to the desired room, which has the side
921 * effect of marking the room as not-zapped ... exactly the effect
924 if (IMAP->selected) {
925 strcpy(savedroom, CC->room.QRname);
927 usergoto(roomname, 0, 0, &msgs, &new);
930 * If another folder is selected, go back to that room so we can resume
931 * our happy day without violent explosions.
933 if (IMAP->selected) {
934 usergoto(savedroom, 0, 0, &msgs, &new);
937 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
942 * Implements the UNSUBSCRIBE command
945 void imap_unsubscribe(int num_parms, char *parms[])
948 char roomname[ROOMNAMELEN];
949 char savedroom[ROOMNAMELEN];
952 ret = imap_grabroom(roomname, parms[2]);
955 ("%s NO Invalid mailbox name or location, or access denied\r\n",
961 * usergoto() formally takes us to the desired room.
963 if (IMAP->selected) {
964 strcpy(savedroom, CC->room.QRname);
966 usergoto(roomname, 0, 0, &msgs, &new);
969 * Now make the API call to zap the room
971 if (CtdlForgetThisRoom() == 0) {
972 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
975 ("%s NO You may not unsubscribe from this folder.\r\n",
980 * If another folder is selected, go back to that room so we can resume
981 * our happy day without violent explosions.
983 if (IMAP->selected) {
984 usergoto(savedroom, 0, 0, &msgs, &new);
991 * Implements the DELETE command
994 void imap_delete(int num_parms, char *parms[])
997 char roomname[ROOMNAMELEN];
998 char savedroom[ROOMNAMELEN];
1001 ret = imap_grabroom(roomname, parms[2]);
1003 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1009 * usergoto() formally takes us to the desired room, happily returning
1010 * the number of messages and number of new messages. (If another
1011 * folder is selected, save its name so we can return there!!!!!)
1013 if (IMAP->selected) {
1014 strcpy(savedroom, CC->room.QRname);
1016 usergoto(roomname, 0, 0, &msgs, &new);
1019 * Now delete the room.
1021 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1022 cprintf("%s OK DELETE completed\r\n", parms[0]);
1023 delete_room(&CC->room);
1025 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1029 * If another folder is selected, go back to that room so we can resume
1030 * our happy day without violent explosions.
1032 if (IMAP->selected) {
1033 usergoto(savedroom, 0, 0, &msgs, &new);
1039 * Back end function for imap_rename()
1041 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1043 char foldername[SIZ];
1044 char newfoldername[SIZ];
1045 char newroomname[ROOMNAMELEN];
1047 struct irl *irlp = NULL; /* scratch pointer */
1048 struct irlparms *irlparms;
1050 irlparms = (struct irlparms *) data;
1051 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1053 /* Rename subfolders */
1054 if ((!strncasecmp(foldername, irlparms->oldname,
1055 strlen(irlparms->oldname))
1056 && (foldername[strlen(irlparms->oldname)] == '|'))) {
1058 sprintf(newfoldername, "%s|%s",
1060 &foldername[strlen(irlparms->oldname) + 1]
1063 newfloor = imap_roomname(newroomname,
1065 newfoldername) & 0xFF;
1067 irlp = (struct irl *) mallok(sizeof(struct irl));
1068 strcpy(irlp->irl_newroom, newroomname);
1069 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1070 irlp->irl_newfloor = newfloor;
1071 irlp->next = *(irlparms->irl);
1072 *(irlparms->irl) = irlp;
1078 * Implements the RENAME command
1081 void imap_rename(int num_parms, char *parms[])
1083 char old_room[ROOMNAMELEN];
1084 char new_room[ROOMNAMELEN];
1088 struct irl *irl = NULL; /* the list */
1089 struct irl *irlp = NULL; /* scratch pointer */
1090 struct irlparms irlparms;
1092 if (strchr(parms[3], '\\') != NULL) {
1093 cprintf("%s NO Invalid character in folder name\r\n",
1098 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1099 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1100 new_floor = (newr & 0xFF);
1102 r = CtdlRenameRoom(old_room, new_room, new_floor);
1104 if (r == crr_room_not_found) {
1105 cprintf("%s NO Could not locate this folder\r\n",
1109 if (r == crr_already_exists) {
1110 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1113 if (r == crr_noneditable) {
1114 cprintf("%s This folder is not editable.\r\n", parms[0]);
1117 if (r == crr_invalid_floor) {
1118 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1121 if (r == crr_access_denied) {
1122 cprintf("%s You do not have permission to edit "
1123 "this folder.\r\n", parms[0]);
1127 cprintf("%s NO Rename failed - undefined error %d\r\n",
1133 /* If this is the INBOX, then RFC2060 says we have to just move the
1134 * contents. In a Citadel environment it's easier to rename the room
1135 * (already did that) and create a new inbox.
1137 if (!strcasecmp(parms[2], "INBOX")) {
1138 create_room(MAILROOM, 4, "", 0, 1, 0);
1141 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1143 irlparms.oldname = parms[2];
1144 irlparms.newname = parms[3];
1145 irlparms.irl = &irl;
1146 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1148 /* ... and now rename them. */
1149 while (irl != NULL) {
1150 r = CtdlRenameRoom(irl->irl_oldroom,
1154 /* FIXME handle error returns better */
1155 lprintf(5, "CtdlRenameRoom() error %d\n",
1164 cprintf("%s OK RENAME completed\r\n", parms[0]);
1171 * Main command loop for IMAP sessions.
1173 void imap_command_loop(void)
1180 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1181 if (client_gets(cmdbuf) < 1) {
1182 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1187 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1188 while (strlen(cmdbuf) < 5)
1189 strcat(cmdbuf, " ");
1192 /* strip off l/t whitespace and CRLF */
1193 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1194 cmdbuf[strlen(cmdbuf) - 1] = 0;
1195 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1196 cmdbuf[strlen(cmdbuf) - 1] = 0;
1199 /* If we're in the middle of a multi-line command, handle that */
1200 if (IMAP->authstate == imap_as_expecting_username) {
1201 imap_auth_login_user(cmdbuf);
1204 if (IMAP->authstate == imap_as_expecting_password) {
1205 imap_auth_login_pass(cmdbuf);
1210 /* Ok, at this point we're in normal command mode. The first thing
1211 * we do is print any incoming pages (yeah! we really do!)
1213 imap_print_express_messages();
1216 * Before processing the command that was just entered... if we happen
1217 * to have a folder selected, we'd like to rescan that folder for new
1218 * messages, and for deletions/changes of existing messages. This
1219 * could probably be optimized somehow, but IMAP sucks...
1221 if (IMAP->selected) {
1222 imap_rescan_msgids();
1225 /* Now for the command set. */
1227 /* Grab the tag, command, and parameters. Check syntax. */
1228 num_parms = imap_parameterize(parms, cmdbuf);
1229 if (num_parms < 2) {
1230 cprintf("BAD syntax error\r\n");
1233 /* The commands below may be executed in any state */
1235 else if ((!strcasecmp(parms[1], "NOOP"))
1236 || (!strcasecmp(parms[1], "CHECK"))) {
1237 cprintf("%s OK This command successfully did nothing.\r\n",
1241 else if (!strcasecmp(parms[1], "LOGOUT")) {
1242 imap_do_expunge(); /* yes, we auto-expunge */
1243 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1244 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1250 else if (!strcasecmp(parms[1], "LOGIN")) {
1251 imap_login(num_parms, parms);
1254 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1255 imap_authenticate(num_parms, parms);
1258 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1259 imap_capability(num_parms, parms);
1262 else if (!strcasecmp(parms[1], "STARTTLS")) {
1263 imap_starttls(num_parms, parms);
1267 else if (!CC->logged_in) {
1268 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1271 /* The commans below require a logged-in state */
1273 else if (!strcasecmp(parms[1], "SELECT")) {
1274 imap_select(num_parms, parms);
1277 else if (!strcasecmp(parms[1], "EXAMINE")) {
1278 imap_select(num_parms, parms);
1281 else if (!strcasecmp(parms[1], "LSUB")) {
1282 imap_lsub(num_parms, parms);
1285 else if (!strcasecmp(parms[1], "LIST")) {
1286 imap_list(num_parms, parms);
1289 else if (!strcasecmp(parms[1], "CREATE")) {
1290 imap_create(num_parms, parms);
1293 else if (!strcasecmp(parms[1], "DELETE")) {
1294 imap_delete(num_parms, parms);
1297 else if (!strcasecmp(parms[1], "RENAME")) {
1298 imap_rename(num_parms, parms);
1301 else if (!strcasecmp(parms[1], "STATUS")) {
1302 imap_status(num_parms, parms);
1305 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1306 imap_subscribe(num_parms, parms);
1309 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1310 imap_unsubscribe(num_parms, parms);
1313 else if (!strcasecmp(parms[1], "APPEND")) {
1314 imap_append(num_parms, parms);
1317 else if (IMAP->selected == 0) {
1318 cprintf("%s BAD no folder selected\r\n", parms[0]);
1321 /* The commands below require the SELECT state on a mailbox */
1323 else if (!strcasecmp(parms[1], "FETCH")) {
1324 imap_fetch(num_parms, parms);
1327 else if ((!strcasecmp(parms[1], "UID"))
1328 && (!strcasecmp(parms[2], "FETCH"))) {
1329 imap_uidfetch(num_parms, parms);
1332 else if (!strcasecmp(parms[1], "SEARCH")) {
1333 imap_search(num_parms, parms);
1336 else if ((!strcasecmp(parms[1], "UID"))
1337 && (!strcasecmp(parms[2], "SEARCH"))) {
1338 imap_uidsearch(num_parms, parms);
1341 else if (!strcasecmp(parms[1], "STORE")) {
1342 imap_store(num_parms, parms);
1345 else if ((!strcasecmp(parms[1], "UID"))
1346 && (!strcasecmp(parms[2], "STORE"))) {
1347 imap_uidstore(num_parms, parms);
1350 else if (!strcasecmp(parms[1], "COPY")) {
1351 imap_copy(num_parms, parms);
1354 else if ((!strcasecmp(parms[1], "UID"))
1355 && (!strcasecmp(parms[2], "COPY"))) {
1356 imap_uidcopy(num_parms, parms);
1359 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1360 imap_expunge(num_parms, parms);
1363 else if (!strcasecmp(parms[1], "CLOSE")) {
1364 imap_close(num_parms, parms);
1367 /* End of commands. If we get here, the command is either invalid
1372 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1375 /* If the client transmitted a message we can free it now */
1376 imap_free_transmitted_message();
1382 * This function is called to register the IMAP extension with Citadel.
1384 char *serv_imap_init(void)
1386 CtdlRegisterServiceHook(config.c_imap_port,
1387 NULL, imap_greeting, imap_command_loop);
1388 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);