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(CTDL_DEBUG, "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(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
310 imap_free_transmitted_message();
312 if (IMAP->cached_fetch != NULL) {
313 fclose(IMAP->cached_fetch);
314 IMAP->cached_fetch = NULL;
315 IMAP->cached_msgnum = (-1);
318 lprintf(CTDL_DEBUG, "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;
332 IMAP->cached_fetch = NULL;
333 IMAP->cached_msgnum = (-1);
335 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
341 * implements the LOGIN command (ordinary username/password login)
343 void imap_login(int num_parms, char *parms[])
345 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
346 if (CtdlTryPassword(parms[3]) == pass_ok) {
347 cprintf("%s OK login successful\r\n", parms[0]);
352 cprintf("%s BAD Login incorrect\r\n", parms[0]);
357 * Implements the AUTHENTICATE command
359 void imap_authenticate(int num_parms, char *parms[])
363 if (num_parms != 3) {
364 cprintf("%s BAD incorrect number of parameters\r\n",
370 cprintf("%s BAD Already logged in.\r\n", parms[0]);
374 if (!strcasecmp(parms[2], "LOGIN")) {
375 CtdlEncodeBase64(buf, "Username:", 9);
376 cprintf("+ %s\r\n", buf);
377 IMAP->authstate = imap_as_expecting_username;
378 strcpy(IMAP->authseq, parms[0]);
383 cprintf("%s NO AUTHENTICATE %s failed\r\n",
388 void imap_auth_login_user(char *cmd)
392 CtdlDecodeBase64(buf, cmd, SIZ);
393 CtdlLoginExistingUser(buf);
394 CtdlEncodeBase64(buf, "Password:", 9);
395 cprintf("+ %s\r\n", buf);
396 IMAP->authstate = imap_as_expecting_password;
400 void imap_auth_login_pass(char *cmd)
404 CtdlDecodeBase64(buf, cmd, SIZ);
405 if (CtdlTryPassword(buf) == pass_ok) {
406 cprintf("%s OK authentication succeeded\r\n",
409 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
411 IMAP->authstate = imap_as_normal;
417 * implements the CAPABILITY command
419 void imap_capability(int num_parms, char *parms[])
421 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
424 cprintf(" STARTTLS");
428 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
434 * implements the STARTTLS command (Citadel API version)
437 void imap_starttls(int num_parms, char *parms[])
439 char ok_response[SIZ];
440 char nosup_response[SIZ];
441 char error_response[SIZ];
444 "%s OK begin TLS negotiation now\r\n",
446 sprintf(nosup_response,
447 "%s NO TLS not supported here\r\n",
449 sprintf(error_response,
450 "%s BAD Internal error\r\n",
452 CtdlStartTLS(ok_response, nosup_response, error_response);
458 * implements the SELECT command
460 void imap_select(int num_parms, char *parms[])
463 char augmented_roomname[ROOMNAMELEN];
467 struct ctdlroom QRscratch;
473 /* Convert the supplied folder name to a roomname */
474 i = imap_roomname(towhere, sizeof towhere, parms[2]);
476 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
480 floornum = (i & 0x00ff);
481 roomflags = (i & 0xff00);
483 /* First try a regular match */
484 c = getroom(&QRscratch, towhere);
486 /* Then try a mailbox name match */
488 MailboxName(augmented_roomname, sizeof augmented_roomname,
490 c = getroom(&QRscratch, augmented_roomname);
492 strcpy(towhere, augmented_roomname);
495 /* If the room exists, check security/access */
497 /* See if there is an existing user/room relationship */
498 ra = CtdlRoomAccess(&QRscratch, &CC->user);
500 /* normal clients have to pass through security */
506 /* Fail here if no such room */
508 cprintf("%s NO ... no such room, or access denied\r\n",
510 /* IMAP->selected = 0; */
514 /* If we already had some other folder selected, auto-expunge it */
518 * usergoto() formally takes us to the desired room, happily returning
519 * the number of messages and number of new messages.
521 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
522 usergoto(NULL, 0, 0, &msgs, &new);
525 if (!strcasecmp(parms[1], "EXAMINE")) {
533 cprintf("* %d EXISTS\r\n", msgs);
534 cprintf("* %d RECENT\r\n", new);
536 /* Note that \Deleted is a valid flag, but not a permanent flag,
537 * because we don't maintain its state across sessions. Citadel
538 * automatically expunges mailboxes when they are de-selected.
540 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
541 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
542 "permanent flags\r\n");
544 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
545 cprintf("%s OK [%s] %s completed\r\n",
547 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
553 * does the real work for expunge
555 int imap_do_expunge(void)
558 int num_expunged = 0;
560 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
561 if (IMAP->selected == 0)
564 if (IMAP->num_msgs > 0)
565 for (i = 0; i < IMAP->num_msgs; ++i) {
566 if (IMAP->flags[i] & IMAP_DELETED) {
567 CtdlDeleteMessages(CC->room.QRname,
568 IMAP->msgids[i], "");
570 lprintf(CTDL_DEBUG, "%ld ... deleted\n",
573 lprintf(CTDL_DEBUG, "%ld ... not deleted\n",
578 if (num_expunged > 0) {
579 imap_rescan_msgids();
582 return (num_expunged);
587 * implements the EXPUNGE command syntax
589 void imap_expunge(int num_parms, char *parms[])
591 int num_expunged = 0;
593 num_expunged = imap_do_expunge();
594 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
599 * implements the CLOSE command
601 void imap_close(int num_parms, char *parms[])
604 /* Yes, we always expunge on close. */
610 cprintf("%s OK CLOSE completed\r\n", parms[0]);
615 * Implements the NAMESPACE command.
617 void imap_namespace(int num_parms, char *parms[])
624 cprintf("* NAMESPACE ");
626 /* All personal folders are subordinate to INBOX. */
627 cprintf("((\"INBOX|\" \"|\")) ");
629 /* Other users' folders ... coming soon! FIXME */
632 /* Show all floors as shared namespaces. Neato! */
634 for (i = 0; i < MAXFLOORS; ++i) {
636 if (fl->f_flags & F_INUSE) {
637 if (floors > 0) cprintf(" ");
639 sprintf(buf, "%s|", fl->f_name);
647 /* Wind it up with a newline and a completion message. */
649 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
655 * Used by LIST and LSUB to show the floors in the listing
657 void imap_list_floors(char *cmd, char *pattern)
662 for (i = 0; i < MAXFLOORS; ++i) {
664 if (fl->f_flags & F_INUSE) {
665 if (imap_mailbox_matches_pattern
666 (pattern, fl->f_name)) {
667 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
668 imap_strout(fl->f_name);
678 * Back end for imap_lsub()
680 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
681 * may or may not be the desired behavior in the future.
683 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
689 pattern = (char *) data;
691 /* Only list rooms to which the user has access!! */
692 ra = CtdlRoomAccess(qrbuf, &CC->user);
694 imap_mailboxname(buf, sizeof buf, qrbuf);
695 if (imap_mailbox_matches_pattern(pattern, buf)) {
696 cprintf("* LSUB () \"|\" ");
705 * Implements the LSUB command
707 void imap_lsub(int num_parms, char *parms[])
711 cprintf("%s BAD arguments invalid\r\n", parms[0]);
714 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
716 if (strlen(parms[3]) == 0) {
717 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
721 imap_list_floors("LSUB", pattern);
722 ForEachRoom(imap_lsub_listroom, pattern);
725 cprintf("%s OK LSUB completed\r\n", parms[0]);
731 * Back end for imap_list()
733 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
739 pattern = (char *) data;
741 /* Only list rooms to which the user has access!! */
742 ra = CtdlRoomAccess(qrbuf, &CC->user);
744 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
745 imap_mailboxname(buf, sizeof buf, qrbuf);
746 if (imap_mailbox_matches_pattern(pattern, buf)) {
747 cprintf("* LIST () \"|\" ");
756 * Implements the LIST command
758 void imap_list(int num_parms, char *parms[])
762 cprintf("%s BAD arguments invalid\r\n", parms[0]);
765 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
767 if (strlen(parms[3]) == 0) {
768 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
772 imap_list_floors("LIST", pattern);
773 ForEachRoom(imap_list_listroom, pattern);
776 cprintf("%s OK LIST completed\r\n", parms[0]);
782 * Implements the CREATE command
785 void imap_create(int num_parms, char *parms[])
788 char roomname[ROOMNAMELEN];
793 if (strchr(parms[2], '\\') != NULL) {
794 cprintf("%s NO Invalid character in folder name\r\n",
796 lprintf(9, "invalid character in folder name\n");
800 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
802 cprintf("%s NO Invalid mailbox name or location\r\n",
804 lprintf(9, "invalid mailbox name or location\n");
807 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
808 flags = (ret & 0xff00); /* upper 8 bits = flags */
810 if (flags & IR_MAILBOX) {
811 if (strncasecmp(parms[2], "INBOX|", 6)) {
812 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
813 lprintf(9, "not subordinate to inbox\n");
818 if (flags & IR_MAILBOX) {
819 newroomtype = 4; /* private mailbox */
821 newroomtype = 0; /* public folder */
824 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
825 roomname, floornum, newroomtype);
827 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
830 ("%s NO Mailbox already exists, or create failed\r\n",
833 cprintf("%s OK CREATE completed\r\n", parms[0]);
835 lprintf(9, "imap_create() completed\n");
840 * Locate a room by its IMAP folder name, and check access to it
842 int imap_grabroom(char *returned_roomname, char *foldername)
845 char augmented_roomname[ROOMNAMELEN];
846 char roomname[ROOMNAMELEN];
848 struct ctdlroom QRscratch;
852 ret = imap_roomname(roomname, sizeof roomname, foldername);
857 /* First try a regular match */
858 c = getroom(&QRscratch, roomname);
860 /* Then try a mailbox name match */
862 MailboxName(augmented_roomname, sizeof augmented_roomname,
863 &CC->user, roomname);
864 c = getroom(&QRscratch, augmented_roomname);
866 strcpy(roomname, augmented_roomname);
869 /* If the room exists, check security/access */
871 /* See if there is an existing user/room relationship */
872 ra = CtdlRoomAccess(&QRscratch, &CC->user);
874 /* normal clients have to pass through security */
880 /* Fail here if no such room */
882 strcpy(returned_roomname, "");
885 strcpy(returned_roomname, QRscratch.QRname);
892 * Implements the STATUS command (sort of)
895 void imap_status(int num_parms, char *parms[])
898 char roomname[ROOMNAMELEN];
900 char savedroom[ROOMNAMELEN];
903 ret = imap_grabroom(roomname, parms[2]);
906 ("%s NO Invalid mailbox name or location, or access denied\r\n",
912 * usergoto() formally takes us to the desired room, happily returning
913 * the number of messages and number of new messages. (If another
914 * folder is selected, save its name so we can return there!!!!!)
916 if (IMAP->selected) {
917 strcpy(savedroom, CC->room.QRname);
919 usergoto(roomname, 0, 0, &msgs, &new);
922 * Tell the client what it wants to know. In fact, tell it *more* than
923 * it wants to know. We happily IGnore the supplied status data item
924 * names and simply spew all possible data items. It's far easier to
925 * code and probably saves us some processing time too.
927 imap_mailboxname(buf, sizeof buf, &CC->room);
928 cprintf("* STATUS ");
930 cprintf(" (MESSAGES %d ", msgs);
931 cprintf("RECENT 0 "); /* FIXME we need to implement this */
932 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
933 cprintf("UNSEEN %d)\r\n", new);
936 * If another folder is selected, go back to that room so we can resume
937 * our happy day without violent explosions.
939 if (IMAP->selected) {
940 usergoto(savedroom, 0, 0, &msgs, &new);
944 * Oooh, look, we're done!
946 cprintf("%s OK STATUS completed\r\n", parms[0]);
952 * Implements the SUBSCRIBE command
955 void imap_subscribe(int num_parms, char *parms[])
958 char roomname[ROOMNAMELEN];
959 char savedroom[ROOMNAMELEN];
962 ret = imap_grabroom(roomname, parms[2]);
965 ("%s NO Invalid mailbox name or location, or access denied\r\n",
971 * usergoto() formally takes us to the desired room, which has the side
972 * effect of marking the room as not-zapped ... exactly the effect
975 if (IMAP->selected) {
976 strcpy(savedroom, CC->room.QRname);
978 usergoto(roomname, 0, 0, &msgs, &new);
981 * If another folder is selected, go back to that room so we can resume
982 * our happy day without violent explosions.
984 if (IMAP->selected) {
985 usergoto(savedroom, 0, 0, &msgs, &new);
988 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
993 * Implements the UNSUBSCRIBE command
996 void imap_unsubscribe(int num_parms, char *parms[])
999 char roomname[ROOMNAMELEN];
1000 char savedroom[ROOMNAMELEN];
1003 ret = imap_grabroom(roomname, parms[2]);
1006 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1012 * usergoto() formally takes us to the desired room.
1014 if (IMAP->selected) {
1015 strcpy(savedroom, CC->room.QRname);
1017 usergoto(roomname, 0, 0, &msgs, &new);
1020 * Now make the API call to zap the room
1022 if (CtdlForgetThisRoom() == 0) {
1023 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1026 ("%s NO You may not unsubscribe from this folder.\r\n",
1031 * If another folder is selected, go back to that room so we can resume
1032 * our happy day without violent explosions.
1034 if (IMAP->selected) {
1035 usergoto(savedroom, 0, 0, &msgs, &new);
1042 * Implements the DELETE command
1045 void imap_delete(int num_parms, char *parms[])
1048 char roomname[ROOMNAMELEN];
1049 char savedroom[ROOMNAMELEN];
1052 ret = imap_grabroom(roomname, parms[2]);
1054 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1060 * usergoto() formally takes us to the desired room, happily returning
1061 * the number of messages and number of new messages. (If another
1062 * folder is selected, save its name so we can return there!!!!!)
1064 if (IMAP->selected) {
1065 strcpy(savedroom, CC->room.QRname);
1067 usergoto(roomname, 0, 0, &msgs, &new);
1070 * Now delete the room.
1072 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1073 cprintf("%s OK DELETE completed\r\n", parms[0]);
1074 delete_room(&CC->room);
1076 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1080 * If another folder is selected, go back to that room so we can resume
1081 * our happy day without violent explosions.
1083 if (IMAP->selected) {
1084 usergoto(savedroom, 0, 0, &msgs, &new);
1090 * Back end function for imap_rename()
1092 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1094 char foldername[SIZ];
1095 char newfoldername[SIZ];
1096 char newroomname[ROOMNAMELEN];
1098 struct irl *irlp = NULL; /* scratch pointer */
1099 struct irlparms *irlparms;
1101 irlparms = (struct irlparms *) data;
1102 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1104 /* Rename subfolders */
1105 if ((!strncasecmp(foldername, irlparms->oldname,
1106 strlen(irlparms->oldname))
1107 && (foldername[strlen(irlparms->oldname)] == '|'))) {
1109 sprintf(newfoldername, "%s|%s",
1111 &foldername[strlen(irlparms->oldname) + 1]
1114 newfloor = imap_roomname(newroomname,
1116 newfoldername) & 0xFF;
1118 irlp = (struct irl *) mallok(sizeof(struct irl));
1119 strcpy(irlp->irl_newroom, newroomname);
1120 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1121 irlp->irl_newfloor = newfloor;
1122 irlp->next = *(irlparms->irl);
1123 *(irlparms->irl) = irlp;
1129 * Implements the RENAME command
1132 void imap_rename(int num_parms, char *parms[])
1134 char old_room[ROOMNAMELEN];
1135 char new_room[ROOMNAMELEN];
1139 struct irl *irl = NULL; /* the list */
1140 struct irl *irlp = NULL; /* scratch pointer */
1141 struct irlparms irlparms;
1143 if (strchr(parms[3], '\\') != NULL) {
1144 cprintf("%s NO Invalid character in folder name\r\n",
1149 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1150 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1151 new_floor = (newr & 0xFF);
1153 r = CtdlRenameRoom(old_room, new_room, new_floor);
1155 if (r == crr_room_not_found) {
1156 cprintf("%s NO Could not locate this folder\r\n",
1160 if (r == crr_already_exists) {
1161 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1164 if (r == crr_noneditable) {
1165 cprintf("%s This folder is not editable.\r\n", parms[0]);
1168 if (r == crr_invalid_floor) {
1169 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1172 if (r == crr_access_denied) {
1173 cprintf("%s You do not have permission to edit "
1174 "this folder.\r\n", parms[0]);
1178 cprintf("%s NO Rename failed - undefined error %d\r\n",
1184 /* If this is the INBOX, then RFC2060 says we have to just move the
1185 * contents. In a Citadel environment it's easier to rename the room
1186 * (already did that) and create a new inbox.
1188 if (!strcasecmp(parms[2], "INBOX")) {
1189 create_room(MAILROOM, 4, "", 0, 1, 0);
1192 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1194 irlparms.oldname = parms[2];
1195 irlparms.newname = parms[3];
1196 irlparms.irl = &irl;
1197 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1199 /* ... and now rename them. */
1200 while (irl != NULL) {
1201 r = CtdlRenameRoom(irl->irl_oldroom,
1205 /* FIXME handle error returns better */
1206 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1215 cprintf("%s OK RENAME completed\r\n", parms[0]);
1222 * Main command loop for IMAP sessions.
1224 void imap_command_loop(void)
1231 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1232 if (client_gets(cmdbuf) < 1) {
1233 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1238 lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
1239 while (strlen(cmdbuf) < 5)
1240 strcat(cmdbuf, " ");
1243 /* strip off l/t whitespace and CRLF */
1244 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1245 cmdbuf[strlen(cmdbuf) - 1] = 0;
1246 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1247 cmdbuf[strlen(cmdbuf) - 1] = 0;
1250 /* If we're in the middle of a multi-line command, handle that */
1251 if (IMAP->authstate == imap_as_expecting_username) {
1252 imap_auth_login_user(cmdbuf);
1255 if (IMAP->authstate == imap_as_expecting_password) {
1256 imap_auth_login_pass(cmdbuf);
1261 /* Ok, at this point we're in normal command mode. The first thing
1262 * we do is print any incoming pages (yeah! we really do!)
1264 imap_print_express_messages();
1267 * Before processing the command that was just entered... if we happen
1268 * to have a folder selected, we'd like to rescan that folder for new
1269 * messages, and for deletions/changes of existing messages. This
1270 * could probably be optimized somehow, but IMAP sucks...
1272 if (IMAP->selected) {
1273 imap_rescan_msgids();
1276 /* Now for the command set. */
1278 /* Grab the tag, command, and parameters. Check syntax. */
1279 num_parms = imap_parameterize(parms, cmdbuf);
1280 if (num_parms < 2) {
1281 cprintf("BAD syntax error\r\n");
1284 /* The commands below may be executed in any state */
1286 else if ((!strcasecmp(parms[1], "NOOP"))
1287 || (!strcasecmp(parms[1], "CHECK"))) {
1288 cprintf("%s OK This command successfully did nothing.\r\n",
1292 else if (!strcasecmp(parms[1], "LOGOUT")) {
1293 imap_do_expunge(); /* yes, we auto-expunge */
1294 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1295 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1301 else if (!strcasecmp(parms[1], "LOGIN")) {
1302 imap_login(num_parms, parms);
1305 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1306 imap_authenticate(num_parms, parms);
1309 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1310 imap_capability(num_parms, parms);
1313 else if (!strcasecmp(parms[1], "STARTTLS")) {
1314 imap_starttls(num_parms, parms);
1317 else if (!CC->logged_in) {
1318 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1321 /* The commans below require a logged-in state */
1323 else if (!strcasecmp(parms[1], "SELECT")) {
1324 imap_select(num_parms, parms);
1327 else if (!strcasecmp(parms[1], "EXAMINE")) {
1328 imap_select(num_parms, parms);
1331 else if (!strcasecmp(parms[1], "LSUB")) {
1332 imap_lsub(num_parms, parms);
1335 else if (!strcasecmp(parms[1], "LIST")) {
1336 imap_list(num_parms, parms);
1339 else if (!strcasecmp(parms[1], "CREATE")) {
1340 imap_create(num_parms, parms);
1343 else if (!strcasecmp(parms[1], "DELETE")) {
1344 imap_delete(num_parms, parms);
1347 else if (!strcasecmp(parms[1], "RENAME")) {
1348 imap_rename(num_parms, parms);
1351 else if (!strcasecmp(parms[1], "STATUS")) {
1352 imap_status(num_parms, parms);
1355 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1356 imap_subscribe(num_parms, parms);
1359 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1360 imap_unsubscribe(num_parms, parms);
1363 else if (!strcasecmp(parms[1], "APPEND")) {
1364 imap_append(num_parms, parms);
1367 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1368 imap_namespace(num_parms, parms);
1371 else if (IMAP->selected == 0) {
1372 cprintf("%s BAD no folder selected\r\n", parms[0]);
1375 /* The commands below require the SELECT state on a mailbox */
1377 else if (!strcasecmp(parms[1], "FETCH")) {
1378 imap_fetch(num_parms, parms);
1381 else if ((!strcasecmp(parms[1], "UID"))
1382 && (!strcasecmp(parms[2], "FETCH"))) {
1383 imap_uidfetch(num_parms, parms);
1386 else if (!strcasecmp(parms[1], "SEARCH")) {
1387 imap_search(num_parms, parms);
1390 else if ((!strcasecmp(parms[1], "UID"))
1391 && (!strcasecmp(parms[2], "SEARCH"))) {
1392 imap_uidsearch(num_parms, parms);
1395 else if (!strcasecmp(parms[1], "STORE")) {
1396 imap_store(num_parms, parms);
1399 else if ((!strcasecmp(parms[1], "UID"))
1400 && (!strcasecmp(parms[2], "STORE"))) {
1401 imap_uidstore(num_parms, parms);
1404 else if (!strcasecmp(parms[1], "COPY")) {
1405 imap_copy(num_parms, parms);
1408 else if ((!strcasecmp(parms[1], "UID"))
1409 && (!strcasecmp(parms[2], "COPY"))) {
1410 imap_uidcopy(num_parms, parms);
1413 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1414 imap_expunge(num_parms, parms);
1417 else if (!strcasecmp(parms[1], "CLOSE")) {
1418 imap_close(num_parms, parms);
1421 /* End of commands. If we get here, the command is either invalid
1426 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1429 /* If the client transmitted a message we can free it now */
1430 imap_free_transmitted_message();
1436 * This function is called to register the IMAP extension with Citadel.
1438 char *serv_imap_init(void)
1440 CtdlRegisterServiceHook(config.c_imap_port,
1441 NULL, imap_greeting, imap_command_loop);
1442 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);