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 "sysdep_decls.h"
40 #include "citserver.h"
43 #include "serv_extensions.h"
50 #include "internet_addressing.h"
51 #include "serv_imap.h"
52 #include "imap_tools.h"
53 #include "imap_fetch.h"
54 #include "imap_search.h"
55 #include "imap_store.h"
56 #include "imap_misc.h"
59 #include "serv_crypto.h"
62 /* imap_rename() uses this struct containing list of rooms to rename */
65 char irl_oldroom[ROOMNAMELEN];
66 char irl_newroom[ROOMNAMELEN];
70 /* Data which is passed between imap_rename() and imap_rename_backend() */
79 * If there is a message ID map in memory, free it
81 void imap_free_msgids(void)
83 if (IMAP->msgids != NULL) {
88 if (IMAP->flags != NULL) {
96 * If there is a transmitted message in memory, free it
98 void imap_free_transmitted_message(void)
100 if (IMAP->transmitted_message != NULL) {
101 phree(IMAP->transmitted_message);
102 IMAP->transmitted_message = NULL;
103 IMAP->transmitted_length = 0;
109 * Set the \\Seen flag for messages which aren't new
111 void imap_set_seen_flags(void)
116 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
117 if (IMAP->num_msgs > 0) {
118 for (i = 0; i < IMAP->num_msgs; ++i) {
119 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
120 IMAP->flags[i] |= IMAP_SEEN;
123 (vbuf.v_answered, IMAP->msgids[i])) {
124 IMAP->flags[i] |= IMAP_ANSWERED;
134 * Back end for imap_load_msgids()
136 * Optimization: instead of calling realloc() to add each message, we
137 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
138 * allows the mapping to proceed much faster.
140 void imap_add_single_msgid(long msgnum, void *userdata)
143 IMAP->num_msgs = IMAP->num_msgs + 1;
144 if (IMAP->msgids == NULL) {
145 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
146 * REALLOC_INCREMENT);
147 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
148 IMAP->msgids = reallok(IMAP->msgids,
150 REALLOC_INCREMENT) * sizeof(long));
152 if (IMAP->flags == NULL) {
153 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
154 * REALLOC_INCREMENT);
155 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
156 IMAP->flags = reallok(IMAP->flags,
158 REALLOC_INCREMENT) * sizeof(long));
160 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
161 IMAP->flags[IMAP->num_msgs - 1] = 0;
167 * Set up a message ID map for the current room (folder)
169 void imap_load_msgids(void)
172 if (IMAP->selected == 0) {
174 "imap_load_msgids() can't run; no room selected\n");
178 imap_free_msgids(); /* If there was already a map, free it */
180 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
181 imap_add_single_msgid, NULL);
183 imap_set_seen_flags();
184 lprintf(9, "imap_load_msgids() mapped %d messages\n",
190 * Re-scan the selected room (folder) and see if it's been changed at all
192 void imap_rescan_msgids(void)
195 int original_num_msgs = 0;
196 long original_highest = 0L;
198 int message_still_exists;
199 struct cdbdata *cdbfr;
200 long *msglist = NULL;
204 if (IMAP->selected == 0) {
206 "imap_load_msgids() can't run; no room selected\n");
210 /* Load the *current* message list from disk, so we can compare it
211 * to what we have in memory.
213 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
215 msglist = mallok(cdbfr->len);
216 memcpy(msglist, cdbfr->ptr, cdbfr->len);
217 num_msgs = cdbfr->len / sizeof(long);
224 * Check to see if any of the messages we know about have been expunged
226 if (IMAP->num_msgs > 0)
227 for (i = 0; i < IMAP->num_msgs; ++i) {
229 message_still_exists = 0;
231 for (j = 0; j < num_msgs; ++j) {
232 if (msglist[j] == IMAP->msgids[i]) {
233 message_still_exists = 1;
237 if (message_still_exists == 0) {
238 cprintf("* %d EXPUNGE\r\n", i + 1);
240 /* Here's some nice stupid nonsense. When a message
241 * is expunged, we have to slide all the existing
242 * messages up in the message array.
245 memcpy(&IMAP->msgids[i],
246 &IMAP->msgids[i + 1],
248 (IMAP->num_msgs - i)));
249 memcpy(&IMAP->flags[i],
252 (IMAP->num_msgs - i)));
260 * Remember how many messages were here before we re-scanned.
262 original_num_msgs = IMAP->num_msgs;
263 if (IMAP->num_msgs > 0) {
264 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
266 original_highest = 0L;
270 * Now peruse the room for *new* messages only.
273 for (j = 0; j < num_msgs; ++j) {
274 if (msglist[j] > original_highest) {
275 imap_add_single_msgid(msglist[j], NULL);
278 imap_set_seen_flags();
281 * If new messages have arrived, tell the client about them.
283 if (IMAP->num_msgs > original_num_msgs) {
284 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
298 * This cleanup function blows away the temporary memory and files used by
301 void imap_cleanup_function(void)
304 /* Don't do this stuff if this is not a IMAP session! */
305 if (CC->h_command_function != imap_command_loop)
308 lprintf(9, "Performing IMAP cleanup hook\n");
310 imap_free_transmitted_message();
311 lprintf(9, "Finished IMAP cleanup hook\n");
317 * Here's where our IMAP session begins its happy day.
319 void imap_greeting(void)
322 strcpy(CC->cs_clientname, "IMAP session");
323 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
324 IMAP->authstate = imap_as_normal;
326 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
332 * implements the LOGIN command (ordinary username/password login)
334 void imap_login(int num_parms, char *parms[])
336 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
337 if (CtdlTryPassword(parms[3]) == pass_ok) {
338 cprintf("%s OK login successful\r\n", parms[0]);
343 cprintf("%s BAD Login incorrect\r\n", parms[0]);
348 * Implements the AUTHENTICATE command
350 void imap_authenticate(int num_parms, char *parms[])
354 if (num_parms != 3) {
355 cprintf("%s BAD incorrect number of parameters\r\n",
361 cprintf("%s BAD Already logged in.\r\n", parms[0]);
365 if (!strcasecmp(parms[2], "LOGIN")) {
366 CtdlEncodeBase64(buf, "Username:", 9);
367 cprintf("+ %s\r\n", buf);
368 IMAP->authstate = imap_as_expecting_username;
369 strcpy(IMAP->authseq, parms[0]);
374 cprintf("%s NO AUTHENTICATE %s failed\r\n",
379 void imap_auth_login_user(char *cmd)
383 CtdlDecodeBase64(buf, cmd, SIZ);
384 CtdlLoginExistingUser(buf);
385 CtdlEncodeBase64(buf, "Password:", 9);
386 cprintf("+ %s\r\n", buf);
387 IMAP->authstate = imap_as_expecting_password;
391 void imap_auth_login_pass(char *cmd)
395 CtdlDecodeBase64(buf, cmd, SIZ);
396 if (CtdlTryPassword(buf) == pass_ok) {
397 cprintf("%s OK authentication succeeded\r\n",
400 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
402 IMAP->authstate = imap_as_normal;
408 * implements the CAPABILITY command
410 void imap_capability(int num_parms, char *parms[])
412 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
415 cprintf(" STARTTLS");
419 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
425 * implements the STARTTLS command (Citadel API version)
428 void imap_starttls(int num_parms, char *parms[])
430 char ok_response[SIZ];
431 char nosup_response[SIZ];
432 char error_response[SIZ];
435 "%s OK begin TLS negotiation now\r\n",
437 sprintf(nosup_response,
438 "%s NO TLS not supported here\r\n",
440 sprintf(error_response,
441 "%s BAD Internal error\r\n",
443 CtdlStartTLS(ok_response, nosup_response, error_response);
449 * implements the SELECT command
451 void imap_select(int num_parms, char *parms[])
454 char augmented_roomname[ROOMNAMELEN];
458 struct ctdlroom QRscratch;
464 /* Convert the supplied folder name to a roomname */
465 i = imap_roomname(towhere, sizeof towhere, parms[2]);
467 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
471 floornum = (i & 0x00ff);
472 roomflags = (i & 0xff00);
474 /* First try a regular match */
475 c = getroom(&QRscratch, towhere);
477 /* Then try a mailbox name match */
479 MailboxName(augmented_roomname, sizeof augmented_roomname,
481 c = getroom(&QRscratch, augmented_roomname);
483 strcpy(towhere, augmented_roomname);
486 /* If the room exists, check security/access */
488 /* See if there is an existing user/room relationship */
489 ra = CtdlRoomAccess(&QRscratch, &CC->user);
491 /* normal clients have to pass through security */
497 /* Fail here if no such room */
499 cprintf("%s NO ... no such room, or access denied\r\n",
501 /* IMAP->selected = 0; */
505 /* If we already had some other folder selected, auto-expunge it */
509 * usergoto() formally takes us to the desired room, happily returning
510 * the number of messages and number of new messages.
512 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
513 usergoto(NULL, 0, 0, &msgs, &new);
516 if (!strcasecmp(parms[1], "EXAMINE")) {
524 cprintf("* %d EXISTS\r\n", msgs);
525 cprintf("* %d RECENT\r\n", new);
527 /* Note that \Deleted is a valid flag, but not a permanent flag,
528 * because we don't maintain its state across sessions. Citadel
529 * automatically expunges mailboxes when they are de-selected.
531 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
532 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
533 "permanent flags\r\n");
535 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
536 cprintf("%s OK [%s] %s completed\r\n",
538 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
544 * does the real work for expunge
546 int imap_do_expunge(void)
549 int num_expunged = 0;
551 lprintf(9, "imap_do_expunge() called\n");
552 if (IMAP->selected == 0)
555 if (IMAP->num_msgs > 0)
556 for (i = 0; i < IMAP->num_msgs; ++i) {
557 if (IMAP->flags[i] & IMAP_DELETED) {
558 CtdlDeleteMessages(CC->room.QRname,
559 IMAP->msgids[i], "");
561 lprintf(9, "%ld ... deleted\n",
564 lprintf(9, "%ld ... not deleted\n",
569 if (num_expunged > 0) {
570 imap_rescan_msgids();
573 return (num_expunged);
578 * implements the EXPUNGE command syntax
580 void imap_expunge(int num_parms, char *parms[])
582 int num_expunged = 0;
584 num_expunged = imap_do_expunge();
585 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
590 * implements the CLOSE command
592 void imap_close(int num_parms, char *parms[])
595 /* Yes, we always expunge on close. */
601 cprintf("%s OK CLOSE completed\r\n", parms[0]);
608 * Used by LIST and LSUB to show the floors in the listing
610 void imap_list_floors(char *cmd, char *pattern)
615 for (i = 0; i < MAXFLOORS; ++i) {
617 if (fl->f_flags & F_INUSE) {
618 if (imap_mailbox_matches_pattern
619 (pattern, fl->f_name)) {
620 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
621 imap_strout(fl->f_name);
631 * Back end for imap_lsub()
633 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
634 * may or may not be the desired behavior in the future.
636 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
642 pattern = (char *) data;
644 /* Only list rooms to which the user has access!! */
645 ra = CtdlRoomAccess(qrbuf, &CC->user);
647 imap_mailboxname(buf, sizeof buf, qrbuf);
648 if (imap_mailbox_matches_pattern(pattern, buf)) {
649 cprintf("* LSUB () \"|\" ");
658 * Implements the LSUB command
660 void imap_lsub(int num_parms, char *parms[])
664 cprintf("%s BAD arguments invalid\r\n", parms[0]);
667 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
669 if (strlen(parms[3]) == 0) {
670 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
674 imap_list_floors("LSUB", pattern);
675 ForEachRoom(imap_lsub_listroom, pattern);
678 cprintf("%s OK LSUB completed\r\n", parms[0]);
684 * Back end for imap_list()
686 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
692 pattern = (char *) data;
694 /* Only list rooms to which the user has access!! */
695 ra = CtdlRoomAccess(qrbuf, &CC->user);
697 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
698 imap_mailboxname(buf, sizeof buf, qrbuf);
699 if (imap_mailbox_matches_pattern(pattern, buf)) {
700 cprintf("* LIST () \"|\" ");
709 * Implements the LIST command
711 void imap_list(int num_parms, char *parms[])
715 cprintf("%s BAD arguments invalid\r\n", parms[0]);
718 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
720 if (strlen(parms[3]) == 0) {
721 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
725 imap_list_floors("LIST", pattern);
726 ForEachRoom(imap_list_listroom, pattern);
729 cprintf("%s OK LIST completed\r\n", parms[0]);
735 * Implements the CREATE command
738 void imap_create(int num_parms, char *parms[])
741 char roomname[ROOMNAMELEN];
746 if (strchr(parms[2], '\\') != NULL) {
747 cprintf("%s NO Invalid character in folder name\r\n",
749 lprintf(9, "invalid character in folder name\n");
753 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
755 cprintf("%s NO Invalid mailbox name or location\r\n",
757 lprintf(9, "invalid mailbox name or location\n");
760 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
761 flags = (ret & 0xff00); /* upper 8 bits = flags */
763 if (flags & IR_MAILBOX) {
764 if (strncasecmp(parms[2], "INBOX|", 6)) {
765 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
766 lprintf(9, "not subordinate to inbox\n");
771 if (flags & IR_MAILBOX) {
772 newroomtype = 4; /* private mailbox */
774 newroomtype = 0; /* public folder */
777 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
778 roomname, floornum, newroomtype);
780 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
783 ("%s NO Mailbox already exists, or create failed\r\n",
786 cprintf("%s OK CREATE completed\r\n", parms[0]);
788 lprintf(9, "imap_create() completed\n");
793 * Locate a room by its IMAP folder name, and check access to it
795 int imap_grabroom(char *returned_roomname, char *foldername)
798 char augmented_roomname[ROOMNAMELEN];
799 char roomname[ROOMNAMELEN];
801 struct ctdlroom QRscratch;
805 ret = imap_roomname(roomname, sizeof roomname, foldername);
810 /* First try a regular match */
811 c = getroom(&QRscratch, roomname);
813 /* Then try a mailbox name match */
815 MailboxName(augmented_roomname, sizeof augmented_roomname,
816 &CC->user, roomname);
817 c = getroom(&QRscratch, augmented_roomname);
819 strcpy(roomname, augmented_roomname);
822 /* If the room exists, check security/access */
824 /* See if there is an existing user/room relationship */
825 ra = CtdlRoomAccess(&QRscratch, &CC->user);
827 /* normal clients have to pass through security */
833 /* Fail here if no such room */
835 strcpy(returned_roomname, "");
838 strcpy(returned_roomname, QRscratch.QRname);
845 * Implements the STATUS command (sort of)
848 void imap_status(int num_parms, char *parms[])
851 char roomname[ROOMNAMELEN];
853 char savedroom[ROOMNAMELEN];
856 ret = imap_grabroom(roomname, parms[2]);
859 ("%s NO Invalid mailbox name or location, or access denied\r\n",
865 * usergoto() formally takes us to the desired room, happily returning
866 * the number of messages and number of new messages. (If another
867 * folder is selected, save its name so we can return there!!!!!)
869 if (IMAP->selected) {
870 strcpy(savedroom, CC->room.QRname);
872 usergoto(roomname, 0, 0, &msgs, &new);
875 * Tell the client what it wants to know. In fact, tell it *more* than
876 * it wants to know. We happily IGnore the supplied status data item
877 * names and simply spew all possible data items. It's far easier to
878 * code and probably saves us some processing time too.
880 imap_mailboxname(buf, sizeof buf, &CC->room);
881 cprintf("* STATUS ");
883 cprintf(" (MESSAGES %d ", msgs);
884 cprintf("RECENT 0 "); /* FIXME we need to implement this */
885 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
886 cprintf("UNSEEN %d)\r\n", new);
889 * If another folder is selected, go back to that room so we can resume
890 * our happy day without violent explosions.
892 if (IMAP->selected) {
893 usergoto(savedroom, 0, 0, &msgs, &new);
897 * Oooh, look, we're done!
899 cprintf("%s OK STATUS completed\r\n", parms[0]);
905 * Implements the SUBSCRIBE command
908 void imap_subscribe(int num_parms, char *parms[])
911 char roomname[ROOMNAMELEN];
912 char savedroom[ROOMNAMELEN];
915 ret = imap_grabroom(roomname, parms[2]);
918 ("%s NO Invalid mailbox name or location, or access denied\r\n",
924 * usergoto() formally takes us to the desired room, which has the side
925 * effect of marking the room as not-zapped ... exactly the effect
928 if (IMAP->selected) {
929 strcpy(savedroom, CC->room.QRname);
931 usergoto(roomname, 0, 0, &msgs, &new);
934 * If another folder is selected, go back to that room so we can resume
935 * our happy day without violent explosions.
937 if (IMAP->selected) {
938 usergoto(savedroom, 0, 0, &msgs, &new);
941 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
946 * Implements the UNSUBSCRIBE command
949 void imap_unsubscribe(int num_parms, char *parms[])
952 char roomname[ROOMNAMELEN];
953 char savedroom[ROOMNAMELEN];
956 ret = imap_grabroom(roomname, parms[2]);
959 ("%s NO Invalid mailbox name or location, or access denied\r\n",
965 * usergoto() formally takes us to the desired room.
967 if (IMAP->selected) {
968 strcpy(savedroom, CC->room.QRname);
970 usergoto(roomname, 0, 0, &msgs, &new);
973 * Now make the API call to zap the room
975 if (CtdlForgetThisRoom() == 0) {
976 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
979 ("%s NO You may not unsubscribe from this folder.\r\n",
984 * If another folder is selected, go back to that room so we can resume
985 * our happy day without violent explosions.
987 if (IMAP->selected) {
988 usergoto(savedroom, 0, 0, &msgs, &new);
995 * Implements the DELETE command
998 void imap_delete(int num_parms, char *parms[])
1001 char roomname[ROOMNAMELEN];
1002 char savedroom[ROOMNAMELEN];
1005 ret = imap_grabroom(roomname, parms[2]);
1007 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1013 * usergoto() formally takes us to the desired room, happily returning
1014 * the number of messages and number of new messages. (If another
1015 * folder is selected, save its name so we can return there!!!!!)
1017 if (IMAP->selected) {
1018 strcpy(savedroom, CC->room.QRname);
1020 usergoto(roomname, 0, 0, &msgs, &new);
1023 * Now delete the room.
1025 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1026 cprintf("%s OK DELETE completed\r\n", parms[0]);
1027 delete_room(&CC->room);
1029 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1033 * If another folder is selected, go back to that room so we can resume
1034 * our happy day without violent explosions.
1036 if (IMAP->selected) {
1037 usergoto(savedroom, 0, 0, &msgs, &new);
1043 * Back end function for imap_rename()
1045 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1047 char foldername[SIZ];
1048 char newfoldername[SIZ];
1049 char newroomname[ROOMNAMELEN];
1051 struct irl *irlp = NULL; /* scratch pointer */
1052 struct irlparms *irlparms;
1054 irlparms = (struct irlparms *) data;
1055 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1057 /* Rename subfolders */
1058 if ((!strncasecmp(foldername, irlparms->oldname,
1059 strlen(irlparms->oldname))
1060 && (foldername[strlen(irlparms->oldname)] == '|'))) {
1062 sprintf(newfoldername, "%s|%s",
1064 &foldername[strlen(irlparms->oldname) + 1]
1067 newfloor = imap_roomname(newroomname,
1069 newfoldername) & 0xFF;
1071 irlp = (struct irl *) mallok(sizeof(struct irl));
1072 strcpy(irlp->irl_newroom, newroomname);
1073 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1074 irlp->irl_newfloor = newfloor;
1075 irlp->next = *(irlparms->irl);
1076 *(irlparms->irl) = irlp;
1082 * Implements the RENAME command
1085 void imap_rename(int num_parms, char *parms[])
1087 char old_room[ROOMNAMELEN];
1088 char new_room[ROOMNAMELEN];
1092 struct irl *irl = NULL; /* the list */
1093 struct irl *irlp = NULL; /* scratch pointer */
1094 struct irlparms irlparms;
1096 if (strchr(parms[3], '\\') != NULL) {
1097 cprintf("%s NO Invalid character in folder name\r\n",
1102 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1103 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1104 new_floor = (newr & 0xFF);
1106 r = CtdlRenameRoom(old_room, new_room, new_floor);
1108 if (r == crr_room_not_found) {
1109 cprintf("%s NO Could not locate this folder\r\n",
1113 if (r == crr_already_exists) {
1114 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1117 if (r == crr_noneditable) {
1118 cprintf("%s This folder is not editable.\r\n", parms[0]);
1121 if (r == crr_invalid_floor) {
1122 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1125 if (r == crr_access_denied) {
1126 cprintf("%s You do not have permission to edit "
1127 "this folder.\r\n", parms[0]);
1131 cprintf("%s NO Rename failed - undefined error %d\r\n",
1137 /* If this is the INBOX, then RFC2060 says we have to just move the
1138 * contents. In a Citadel environment it's easier to rename the room
1139 * (already did that) and create a new inbox.
1141 if (!strcasecmp(parms[2], "INBOX")) {
1142 create_room(MAILROOM, 4, "", 0, 1, 0);
1145 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1147 irlparms.oldname = parms[2];
1148 irlparms.newname = parms[3];
1149 irlparms.irl = &irl;
1150 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1152 /* ... and now rename them. */
1153 while (irl != NULL) {
1154 r = CtdlRenameRoom(irl->irl_oldroom,
1158 /* FIXME handle error returns better */
1159 lprintf(5, "CtdlRenameRoom() error %d\n",
1168 cprintf("%s OK RENAME completed\r\n", parms[0]);
1175 * Main command loop for IMAP sessions.
1177 void imap_command_loop(void)
1184 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1185 if (client_gets(cmdbuf) < 1) {
1186 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1191 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1192 while (strlen(cmdbuf) < 5)
1193 strcat(cmdbuf, " ");
1196 /* strip off l/t whitespace and CRLF */
1197 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1198 cmdbuf[strlen(cmdbuf) - 1] = 0;
1199 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1200 cmdbuf[strlen(cmdbuf) - 1] = 0;
1203 /* If we're in the middle of a multi-line command, handle that */
1204 if (IMAP->authstate == imap_as_expecting_username) {
1205 imap_auth_login_user(cmdbuf);
1208 if (IMAP->authstate == imap_as_expecting_password) {
1209 imap_auth_login_pass(cmdbuf);
1214 /* Ok, at this point we're in normal command mode. The first thing
1215 * we do is print any incoming pages (yeah! we really do!)
1217 imap_print_express_messages();
1220 * Before processing the command that was just entered... if we happen
1221 * to have a folder selected, we'd like to rescan that folder for new
1222 * messages, and for deletions/changes of existing messages. This
1223 * could probably be optimized somehow, but IMAP sucks...
1225 if (IMAP->selected) {
1226 imap_rescan_msgids();
1229 /* Now for the command set. */
1231 /* Grab the tag, command, and parameters. Check syntax. */
1232 num_parms = imap_parameterize(parms, cmdbuf);
1233 if (num_parms < 2) {
1234 cprintf("BAD syntax error\r\n");
1237 /* The commands below may be executed in any state */
1239 else if ((!strcasecmp(parms[1], "NOOP"))
1240 || (!strcasecmp(parms[1], "CHECK"))) {
1241 cprintf("%s OK This command successfully did nothing.\r\n",
1245 else if (!strcasecmp(parms[1], "LOGOUT")) {
1246 imap_do_expunge(); /* yes, we auto-expunge */
1247 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1248 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1254 else if (!strcasecmp(parms[1], "LOGIN")) {
1255 imap_login(num_parms, parms);
1258 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1259 imap_authenticate(num_parms, parms);
1262 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1263 imap_capability(num_parms, parms);
1266 else if (!strcasecmp(parms[1], "STARTTLS")) {
1267 imap_starttls(num_parms, parms);
1270 else if (!CC->logged_in) {
1271 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1274 /* The commans below require a logged-in state */
1276 else if (!strcasecmp(parms[1], "SELECT")) {
1277 imap_select(num_parms, parms);
1280 else if (!strcasecmp(parms[1], "EXAMINE")) {
1281 imap_select(num_parms, parms);
1284 else if (!strcasecmp(parms[1], "LSUB")) {
1285 imap_lsub(num_parms, parms);
1288 else if (!strcasecmp(parms[1], "LIST")) {
1289 imap_list(num_parms, parms);
1292 else if (!strcasecmp(parms[1], "CREATE")) {
1293 imap_create(num_parms, parms);
1296 else if (!strcasecmp(parms[1], "DELETE")) {
1297 imap_delete(num_parms, parms);
1300 else if (!strcasecmp(parms[1], "RENAME")) {
1301 imap_rename(num_parms, parms);
1304 else if (!strcasecmp(parms[1], "STATUS")) {
1305 imap_status(num_parms, parms);
1308 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1309 imap_subscribe(num_parms, parms);
1312 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1313 imap_unsubscribe(num_parms, parms);
1316 else if (!strcasecmp(parms[1], "APPEND")) {
1317 imap_append(num_parms, parms);
1320 else if (IMAP->selected == 0) {
1321 cprintf("%s BAD no folder selected\r\n", parms[0]);
1324 /* The commands below require the SELECT state on a mailbox */
1326 else if (!strcasecmp(parms[1], "FETCH")) {
1327 imap_fetch(num_parms, parms);
1330 else if ((!strcasecmp(parms[1], "UID"))
1331 && (!strcasecmp(parms[2], "FETCH"))) {
1332 imap_uidfetch(num_parms, parms);
1335 else if (!strcasecmp(parms[1], "SEARCH")) {
1336 imap_search(num_parms, parms);
1339 else if ((!strcasecmp(parms[1], "UID"))
1340 && (!strcasecmp(parms[2], "SEARCH"))) {
1341 imap_uidsearch(num_parms, parms);
1344 else if (!strcasecmp(parms[1], "STORE")) {
1345 imap_store(num_parms, parms);
1348 else if ((!strcasecmp(parms[1], "UID"))
1349 && (!strcasecmp(parms[2], "STORE"))) {
1350 imap_uidstore(num_parms, parms);
1353 else if (!strcasecmp(parms[1], "COPY")) {
1354 imap_copy(num_parms, parms);
1357 else if ((!strcasecmp(parms[1], "UID"))
1358 && (!strcasecmp(parms[2], "COPY"))) {
1359 imap_uidcopy(num_parms, parms);
1362 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1363 imap_expunge(num_parms, parms);
1366 else if (!strcasecmp(parms[1], "CLOSE")) {
1367 imap_close(num_parms, parms);
1370 /* End of commands. If we get here, the command is either invalid
1375 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1378 /* If the client transmitted a message we can free it now */
1379 imap_free_transmitted_message();
1385 * This function is called to register the IMAP extension with Citadel.
1387 char *serv_imap_init(void)
1389 CtdlRegisterServiceHook(config.c_imap_port,
1390 NULL, imap_greeting, imap_command_loop);
1391 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);