4 * IMAP server for the Citadel system
5 * Copyright (C) 2000-2005 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * WARNING: the IMAP protocol is badly designed. No implementation of it
9 * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP
10 * implementations have bugs.
21 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
40 #include "sysdep_decls.h"
41 #include "citserver.h"
44 #include "serv_extensions.h"
51 #include "internet_addressing.h"
52 #include "serv_imap.h"
53 #include "imap_tools.h"
54 #include "imap_fetch.h"
55 #include "imap_search.h"
56 #include "imap_store.h"
57 #include "imap_misc.h"
60 #include "serv_crypto.h"
63 /* imap_rename() uses this struct containing list of rooms to rename */
66 char irl_oldroom[ROOMNAMELEN];
67 char irl_newroom[ROOMNAMELEN];
71 /* Data which is passed between imap_rename() and imap_rename_backend() */
80 * If there is a message ID map in memory, free it
82 void imap_free_msgids(void)
84 if (IMAP->msgids != NULL) {
90 if (IMAP->flags != NULL) {
98 * If there is a transmitted message in memory, free it
100 void imap_free_transmitted_message(void)
102 if (IMAP->transmitted_message != NULL) {
103 free(IMAP->transmitted_message);
104 IMAP->transmitted_message = NULL;
105 IMAP->transmitted_length = 0;
111 * Set the \\Seen flag for messages which aren't new
113 void imap_set_seen_flags(void)
118 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
119 if (IMAP->num_msgs > 0) {
120 for (i = 0; i < IMAP->num_msgs; ++i) {
121 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
122 IMAP->flags[i] |= IMAP_SEEN;
125 IMAP->flags[i] |= IMAP_RECENT;
128 (vbuf.v_answered, IMAP->msgids[i])) {
129 IMAP->flags[i] |= IMAP_ANSWERED;
139 * Back end for imap_load_msgids()
141 * Optimization: instead of calling realloc() to add each message, we
142 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
143 * allows the mapping to proceed much faster.
145 void imap_add_single_msgid(long msgnum, void *userdata)
149 if (IMAP->num_msgs > IMAP->num_alloc) {
150 IMAP->num_alloc += REALLOC_INCREMENT;
151 IMAP->msgids = realloc(IMAP->msgids, (IMAP->num_alloc * sizeof(long)));
152 IMAP->flags = realloc(IMAP->flags, (IMAP->num_alloc * sizeof(long)));
154 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
155 IMAP->flags[IMAP->num_msgs - 1] = 0;
161 * Set up a message ID map for the current room (folder)
163 void imap_load_msgids(void)
166 if (IMAP->selected == 0) {
168 "imap_load_msgids() can't run; no room selected\n");
172 imap_free_msgids(); /* If there was already a map, free it */
174 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
175 imap_add_single_msgid, NULL);
177 imap_set_seen_flags();
178 /* lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
184 * Re-scan the selected room (folder) and see if it's been changed at all
186 void imap_rescan_msgids(void)
189 int original_num_msgs = 0;
190 long original_highest = 0L;
192 int message_still_exists;
193 struct cdbdata *cdbfr;
194 long *msglist = NULL;
199 if (IMAP->selected == 0) {
201 "imap_load_msgids() can't run; no room selected\n");
205 /* Load the *current* message list from disk, so we can compare it
206 * to what we have in memory.
208 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
210 msglist = malloc(cdbfr->len);
211 if (msglist == NULL) {
212 lprintf(CTDL_CRIT, "malloc() failed\n");
215 memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
216 num_msgs = cdbfr->len / sizeof(long);
223 * Check to see if any of the messages we know about have been expunged
225 if (IMAP->num_msgs > 0)
226 for (i = 0; i < IMAP->num_msgs; ++i) {
228 message_still_exists = 0;
230 for (j = 0; j < num_msgs; ++j) {
231 if (msglist[j] == IMAP->msgids[i]) {
232 message_still_exists = 1;
236 if (message_still_exists == 0) {
237 cprintf("* %d EXPUNGE\r\n", i + 1);
239 /* Here's some nice stupid nonsense. When a message
240 * is expunged, we have to slide all the existing
241 * messages up in the message array.
244 memcpy(&IMAP->msgids[i],
245 &IMAP->msgids[i + 1],
247 (IMAP->num_msgs - i)));
248 memcpy(&IMAP->flags[i],
251 (IMAP->num_msgs - i)));
259 * Remember how many messages were here before we re-scanned.
261 original_num_msgs = IMAP->num_msgs;
262 if (IMAP->num_msgs > 0) {
263 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
265 original_highest = 0L;
269 * Now peruse the room for *new* messages only.
272 for (j = 0; j < num_msgs; ++j) {
273 if (msglist[j] > original_highest) {
274 imap_add_single_msgid(msglist[j], NULL);
277 imap_set_seen_flags();
280 * If new messages have arrived, tell the client about them.
282 if (IMAP->num_msgs > original_num_msgs) {
284 for (j = 0; j < num_msgs; ++j) {
285 if (IMAP->flags[j] & IMAP_RECENT) {
290 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
291 cprintf("* %d RECENT\r\n", num_recent);
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 /* If there is a mailbox selected, auto-expunge it. */
316 if (IMAP->selected) {
320 lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
322 imap_free_transmitted_message();
324 if (IMAP->cached_rfc822_data != NULL) {
325 free(IMAP->cached_rfc822_data);
326 IMAP->cached_rfc822_data = NULL;
327 IMAP->cached_rfc822_msgnum = (-1);
330 if (IMAP->cached_body != NULL) {
331 free(IMAP->cached_body);
332 IMAP->cached_body = NULL;
333 IMAP->cached_body_len = 0;
334 IMAP->cached_bodymsgnum = (-1);
338 lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
344 * Here's where our IMAP session begins its happy day.
346 void imap_greeting(void)
349 strcpy(CC->cs_clientname, "IMAP session");
350 IMAP = malloc(sizeof (struct citimap));
351 memset(IMAP, 0, sizeof(struct citimap));
352 IMAP->authstate = imap_as_normal;
353 IMAP->cached_rfc822_data = NULL;
354 IMAP->cached_rfc822_msgnum = (-1);
356 cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
361 * IMAPS is just like IMAP, except it goes crypto right away.
364 void imaps_greeting(void) {
365 CtdlStartTLS(NULL, NULL, NULL);
372 * implements the LOGIN command (ordinary username/password login)
374 void imap_login(int num_parms, char *parms[])
376 if (num_parms != 4) {
377 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
381 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
382 if (CtdlTryPassword(parms[3]) == pass_ok) {
383 cprintf("%s OK login successful\r\n", parms[0]);
388 cprintf("%s BAD Login incorrect\r\n", parms[0]);
393 * Implements the AUTHENTICATE command
395 void imap_authenticate(int num_parms, char *parms[])
399 if (num_parms != 3) {
400 cprintf("%s BAD incorrect number of parameters\r\n",
406 cprintf("%s BAD Already logged in.\r\n", parms[0]);
410 if (!strcasecmp(parms[2], "LOGIN")) {
411 CtdlEncodeBase64(buf, "Username:", 9);
412 cprintf("+ %s\r\n", buf);
413 IMAP->authstate = imap_as_expecting_username;
414 strcpy(IMAP->authseq, parms[0]);
419 cprintf("%s NO AUTHENTICATE %s failed\r\n",
424 void imap_auth_login_user(char *cmd)
428 CtdlDecodeBase64(buf, cmd, SIZ);
429 CtdlLoginExistingUser(buf);
430 CtdlEncodeBase64(buf, "Password:", 9);
431 cprintf("+ %s\r\n", buf);
432 IMAP->authstate = imap_as_expecting_password;
436 void imap_auth_login_pass(char *cmd)
440 CtdlDecodeBase64(buf, cmd, SIZ);
441 if (CtdlTryPassword(buf) == pass_ok) {
442 cprintf("%s OK authentication succeeded\r\n",
445 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
447 IMAP->authstate = imap_as_normal;
453 * implements the CAPABILITY command
455 void imap_capability(int num_parms, char *parms[])
457 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
460 cprintf(" STARTTLS");
464 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
470 * implements the STARTTLS command (Citadel API version)
473 void imap_starttls(int num_parms, char *parms[])
475 char ok_response[SIZ];
476 char nosup_response[SIZ];
477 char error_response[SIZ];
480 "%s OK begin TLS negotiation now\r\n",
482 sprintf(nosup_response,
483 "%s NO TLS not supported here\r\n",
485 sprintf(error_response,
486 "%s BAD Internal error\r\n",
488 CtdlStartTLS(ok_response, nosup_response, error_response);
494 * implements the SELECT command
496 void imap_select(int num_parms, char *parms[])
499 char augmented_roomname[ROOMNAMELEN];
503 struct ctdlroom QRscratch;
509 /* Convert the supplied folder name to a roomname */
510 i = imap_roomname(towhere, sizeof towhere, parms[2]);
512 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
516 floornum = (i & 0x00ff);
517 roomflags = (i & 0xff00);
519 /* First try a regular match */
520 c = getroom(&QRscratch, towhere);
522 /* Then try a mailbox name match */
524 MailboxName(augmented_roomname, sizeof augmented_roomname,
526 c = getroom(&QRscratch, augmented_roomname);
528 strcpy(towhere, augmented_roomname);
531 /* If the room exists, check security/access */
533 /* See if there is an existing user/room relationship */
534 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
536 /* normal clients have to pass through security */
542 /* Fail here if no such room */
544 cprintf("%s NO ... no such room, or access denied\r\n",
549 /* If we already had some other folder selected, auto-expunge it */
553 * usergoto() formally takes us to the desired room, happily returning
554 * the number of messages and number of new messages.
556 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
557 usergoto(NULL, 0, 0, &msgs, &new);
560 if (!strcasecmp(parms[1], "EXAMINE")) {
568 cprintf("* %d EXISTS\r\n", msgs);
569 cprintf("* %d RECENT\r\n", new);
571 /* Note that \Deleted is a valid flag, but not a permanent flag,
572 * because we don't maintain its state across sessions. Citadel
573 * automatically expunges mailboxes when they are de-selected.
575 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
576 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
577 "permanent flags\r\n");
579 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
580 cprintf("%s OK [%s] %s completed\r\n",
582 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
588 * does the real work for expunge
590 int imap_do_expunge(void)
593 int num_expunged = 0;
595 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
596 if (IMAP->selected == 0) {
600 if (IMAP->num_msgs > 0)
601 for (i = 0; i < IMAP->num_msgs; ++i) {
602 if (IMAP->flags[i] & IMAP_DELETED) {
603 CtdlDeleteMessages(CC->room.QRname,
604 IMAP->msgids[i], "");
609 if (num_expunged > 0) {
610 imap_rescan_msgids();
613 lprintf(CTDL_DEBUG, "Expunged %d messages.\n", num_expunged);
614 return (num_expunged);
619 * implements the EXPUNGE command syntax
621 void imap_expunge(int num_parms, char *parms[])
623 int num_expunged = 0;
625 num_expunged = imap_do_expunge();
626 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
631 * implements the CLOSE command
633 void imap_close(int num_parms, char *parms[])
636 /* Yes, we always expunge on close. */
637 if (IMAP->selected) {
644 cprintf("%s OK CLOSE completed\r\n", parms[0]);
649 * Implements the NAMESPACE command.
651 void imap_namespace(int num_parms, char *parms[])
658 cprintf("* NAMESPACE ");
660 /* All personal folders are subordinate to INBOX. */
661 cprintf("((\"INBOX/\" \"/\")) ");
663 /* Other users' folders ... coming soon! FIXME */
666 /* Show all floors as shared namespaces. Neato! */
668 for (i = 0; i < MAXFLOORS; ++i) {
670 if (fl->f_flags & F_INUSE) {
671 if (floors > 0) cprintf(" ");
673 sprintf(buf, "%s/", fl->f_name);
681 /* Wind it up with a newline and a completion message. */
683 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
689 * Used by LIST and LSUB to show the floors in the listing
691 void imap_list_floors(char *cmd, char *pattern)
696 for (i = 0; i < MAXFLOORS; ++i) {
698 if (fl->f_flags & F_INUSE) {
699 if (imap_mailbox_matches_pattern
700 (pattern, fl->f_name)) {
701 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
702 imap_strout(fl->f_name);
712 * Back end for imap_lsub()
714 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
715 * may or may not be the desired behavior in the future.
717 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
723 pattern = (char *) data;
725 /* Only list rooms to which the user has access!! */
726 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
728 imap_mailboxname(buf, sizeof buf, qrbuf);
729 if (imap_mailbox_matches_pattern(pattern, buf)) {
730 cprintf("* LSUB () \"/\" ");
739 * Implements the LSUB command
741 void imap_lsub(int num_parms, char *parms[])
745 cprintf("%s BAD arguments invalid\r\n", parms[0]);
748 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
750 if (strlen(parms[3]) == 0) {
751 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
755 imap_list_floors("LSUB", pattern);
756 ForEachRoom(imap_lsub_listroom, pattern);
759 cprintf("%s OK LSUB completed\r\n", parms[0]);
765 * Back end for imap_list()
767 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
773 pattern = (char *) data;
775 /* Only list rooms to which the user has access!! */
776 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
778 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
779 imap_mailboxname(buf, sizeof buf, qrbuf);
780 if (imap_mailbox_matches_pattern(pattern, buf)) {
781 cprintf("* LIST () \"/\" ");
790 * Implements the LIST command
792 void imap_list(int num_parms, char *parms[])
796 cprintf("%s BAD arguments invalid\r\n", parms[0]);
799 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
801 if (strlen(parms[3]) == 0) {
802 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
806 imap_list_floors("LIST", pattern);
807 ForEachRoom(imap_list_listroom, pattern);
810 cprintf("%s OK LIST completed\r\n", parms[0]);
816 * Implements the CREATE command
819 void imap_create(int num_parms, char *parms[])
822 char roomname[ROOMNAMELEN];
828 if (strchr(parms[2], '\\') != NULL) {
829 cprintf("%s NO Invalid character in folder name\r\n",
831 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
835 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
837 cprintf("%s NO Invalid mailbox name or location\r\n",
839 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
842 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
843 flags = (ret & 0xff00); /* upper 8 bits = flags */
845 if (flags & IR_MAILBOX) {
846 if (strncasecmp(parms[2], "INBOX/", 6)) {
847 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
848 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
853 if (flags & IR_MAILBOX) {
854 newroomtype = 4; /* private mailbox */
855 newroomview = VIEW_MAILBOX;
857 newroomtype = 0; /* public folder */
858 newroomview = VIEW_BBS;
861 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
862 roomname, floornum, newroomtype);
864 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
867 ("%s NO Mailbox already exists, or create failed\r\n",
870 cprintf("%s OK CREATE completed\r\n", parms[0]);
872 lprintf(CTDL_DEBUG, "imap_create() completed\n");
877 * Locate a room by its IMAP folder name, and check access to it.
878 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
880 int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
883 char augmented_roomname[ROOMNAMELEN];
884 char roomname[ROOMNAMELEN];
886 struct ctdlroom QRscratch;
890 ret = imap_roomname(roomname, sizeof roomname, foldername);
895 /* First try a regular match */
896 c = getroom(&QRscratch, roomname);
898 /* Then try a mailbox name match */
900 MailboxName(augmented_roomname, sizeof augmented_roomname,
901 &CC->user, roomname);
902 c = getroom(&QRscratch, augmented_roomname);
904 strcpy(roomname, augmented_roomname);
907 /* If the room exists, check security/access */
909 /* See if there is an existing user/room relationship */
910 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
912 /* normal clients have to pass through security */
916 if ((zapped_ok) && (ra & UA_ZAPPED)) {
921 /* Fail here if no such room */
923 strcpy(returned_roomname, "");
926 strcpy(returned_roomname, QRscratch.QRname);
933 * Implements the STATUS command (sort of)
936 void imap_status(int num_parms, char *parms[])
939 char roomname[ROOMNAMELEN];
941 char savedroom[ROOMNAMELEN];
944 ret = imap_grabroom(roomname, parms[2], 0);
947 ("%s NO Invalid mailbox name or location, or access denied\r\n",
953 * usergoto() formally takes us to the desired room, happily returning
954 * the number of messages and number of new messages. (If another
955 * folder is selected, save its name so we can return there!!!!!)
957 if (IMAP->selected) {
958 strcpy(savedroom, CC->room.QRname);
960 usergoto(roomname, 0, 0, &msgs, &new);
963 * Tell the client what it wants to know. In fact, tell it *more* than
964 * it wants to know. We happily IGnore the supplied status data item
965 * names and simply spew all possible data items. It's far easier to
966 * code and probably saves us some processing time too.
968 imap_mailboxname(buf, sizeof buf, &CC->room);
969 cprintf("* STATUS ");
971 cprintf(" (MESSAGES %d ", msgs);
972 cprintf("RECENT %d ", new); /* Initially, new==recent */
973 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
974 cprintf("UNSEEN %d)\r\n", new);
977 * If another folder is selected, go back to that room so we can resume
978 * our happy day without violent explosions.
980 if (IMAP->selected) {
981 usergoto(savedroom, 0, 0, &msgs, &new);
985 * Oooh, look, we're done!
987 cprintf("%s OK STATUS completed\r\n", parms[0]);
993 * Implements the SUBSCRIBE command
996 void imap_subscribe(int num_parms, char *parms[])
999 char roomname[ROOMNAMELEN];
1000 char savedroom[ROOMNAMELEN];
1003 ret = imap_grabroom(roomname, parms[2], 1);
1006 "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1014 * usergoto() formally takes us to the desired room, which has the side
1015 * effect of marking the room as not-zapped ... exactly the effect
1016 * we're looking for.
1018 if (IMAP->selected) {
1019 strcpy(savedroom, CC->room.QRname);
1021 usergoto(roomname, 0, 0, &msgs, &new);
1024 * If another folder is selected, go back to that room so we can resume
1025 * our happy day without violent explosions.
1027 if (IMAP->selected) {
1028 usergoto(savedroom, 0, 0, &msgs, &new);
1031 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1036 * Implements the UNSUBSCRIBE command
1039 void imap_unsubscribe(int num_parms, char *parms[])
1042 char roomname[ROOMNAMELEN];
1043 char savedroom[ROOMNAMELEN];
1046 ret = imap_grabroom(roomname, parms[2], 0);
1049 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1055 * usergoto() formally takes us to the desired room.
1057 if (IMAP->selected) {
1058 strcpy(savedroom, CC->room.QRname);
1060 usergoto(roomname, 0, 0, &msgs, &new);
1063 * Now make the API call to zap the room
1065 if (CtdlForgetThisRoom() == 0) {
1066 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1069 ("%s NO You may not unsubscribe from this folder.\r\n",
1074 * If another folder is selected, go back to that room so we can resume
1075 * our happy day without violent explosions.
1077 if (IMAP->selected) {
1078 usergoto(savedroom, 0, 0, &msgs, &new);
1085 * Implements the DELETE command
1088 void imap_delete(int num_parms, char *parms[])
1091 char roomname[ROOMNAMELEN];
1092 char savedroom[ROOMNAMELEN];
1095 ret = imap_grabroom(roomname, parms[2], 0);
1097 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1103 * usergoto() formally takes us to the desired room, happily returning
1104 * the number of messages and number of new messages. (If another
1105 * folder is selected, save its name so we can return there!!!!!)
1107 if (IMAP->selected) {
1108 strcpy(savedroom, CC->room.QRname);
1110 usergoto(roomname, 0, 0, &msgs, &new);
1113 * Now delete the room.
1115 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1116 cprintf("%s OK DELETE completed\r\n", parms[0]);
1117 delete_room(&CC->room);
1119 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1123 * If another folder is selected, go back to that room so we can resume
1124 * our happy day without violent explosions.
1126 if (IMAP->selected) {
1127 usergoto(savedroom, 0, 0, &msgs, &new);
1133 * Back end function for imap_rename()
1135 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1137 char foldername[SIZ];
1138 char newfoldername[SIZ];
1139 char newroomname[ROOMNAMELEN];
1141 struct irl *irlp = NULL; /* scratch pointer */
1142 struct irlparms *irlparms;
1144 irlparms = (struct irlparms *) data;
1145 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1147 /* Rename subfolders */
1148 if ((!strncasecmp(foldername, irlparms->oldname,
1149 strlen(irlparms->oldname))
1150 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1152 sprintf(newfoldername, "%s/%s",
1154 &foldername[strlen(irlparms->oldname) + 1]
1157 newfloor = imap_roomname(newroomname,
1159 newfoldername) & 0xFF;
1161 irlp = (struct irl *) malloc(sizeof(struct irl));
1162 strcpy(irlp->irl_newroom, newroomname);
1163 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1164 irlp->irl_newfloor = newfloor;
1165 irlp->next = *(irlparms->irl);
1166 *(irlparms->irl) = irlp;
1172 * Implements the RENAME command
1175 void imap_rename(int num_parms, char *parms[])
1177 char old_room[ROOMNAMELEN];
1178 char new_room[ROOMNAMELEN];
1182 struct irl *irl = NULL; /* the list */
1183 struct irl *irlp = NULL; /* scratch pointer */
1184 struct irlparms irlparms;
1186 if (strchr(parms[3], '\\') != NULL) {
1187 cprintf("%s NO Invalid character in folder name\r\n",
1192 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1193 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1194 new_floor = (newr & 0xFF);
1196 r = CtdlRenameRoom(old_room, new_room, new_floor);
1198 if (r == crr_room_not_found) {
1199 cprintf("%s NO Could not locate this folder\r\n",
1203 if (r == crr_already_exists) {
1204 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1207 if (r == crr_noneditable) {
1208 cprintf("%s This folder is not editable.\r\n", parms[0]);
1211 if (r == crr_invalid_floor) {
1212 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1215 if (r == crr_access_denied) {
1216 cprintf("%s You do not have permission to edit "
1217 "this folder.\r\n", parms[0]);
1221 cprintf("%s NO Rename failed - undefined error %d\r\n",
1227 /* If this is the INBOX, then RFC2060 says we have to just move the
1228 * contents. In a Citadel environment it's easier to rename the room
1229 * (already did that) and create a new inbox.
1231 if (!strcasecmp(parms[2], "INBOX")) {
1232 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
1235 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1237 irlparms.oldname = parms[2];
1238 irlparms.newname = parms[3];
1239 irlparms.irl = &irl;
1240 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1242 /* ... and now rename them. */
1243 while (irl != NULL) {
1244 r = CtdlRenameRoom(irl->irl_oldroom,
1248 /* FIXME handle error returns better */
1249 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1258 cprintf("%s OK RENAME completed\r\n", parms[0]);
1265 * Main command loop for IMAP sessions.
1267 void imap_command_loop(void)
1273 CC->lastcmd = time(NULL);
1274 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1276 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1277 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1282 lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
1283 while (strlen(cmdbuf) < 5)
1284 strcat(cmdbuf, " ");
1286 /* strip off l/t whitespace and CRLF */
1287 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1288 cmdbuf[strlen(cmdbuf) - 1] = 0;
1289 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1290 cmdbuf[strlen(cmdbuf) - 1] = 0;
1293 /* If we're in the middle of a multi-line command, handle that */
1294 if (IMAP->authstate == imap_as_expecting_username) {
1295 imap_auth_login_user(cmdbuf);
1298 if (IMAP->authstate == imap_as_expecting_password) {
1299 imap_auth_login_pass(cmdbuf);
1303 /* Ok, at this point we're in normal command mode. The first thing
1304 * we do is print any incoming pages (yeah! we really do!)
1306 imap_print_instant_messages();
1309 * Before processing the command that was just entered... if we happen
1310 * to have a folder selected, we'd like to rescan that folder for new
1311 * messages, and for deletions/changes of existing messages. This
1312 * could probably be optimized somehow, but IMAP sucks...
1314 if (IMAP->selected) {
1315 imap_rescan_msgids();
1318 /* Now for the command set. */
1320 /* Grab the tag, command, and parameters. Check syntax. */
1321 num_parms = imap_parameterize(parms, cmdbuf);
1322 if (num_parms < 2) {
1323 cprintf("BAD syntax error\r\n");
1326 /* The commands below may be executed in any state */
1328 else if ((!strcasecmp(parms[1], "NOOP"))
1329 || (!strcasecmp(parms[1], "CHECK"))) {
1330 cprintf("%s OK This command successfully did nothing.\r\n",
1334 else if (!strcasecmp(parms[1], "LOGOUT")) {
1335 if (IMAP->selected) {
1336 imap_do_expunge(); /* yes, we auto-expunge */
1338 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1339 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1345 else if (!strcasecmp(parms[1], "LOGIN")) {
1346 imap_login(num_parms, parms);
1349 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1350 imap_authenticate(num_parms, parms);
1353 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1354 imap_capability(num_parms, parms);
1357 else if (!strcasecmp(parms[1], "STARTTLS")) {
1358 imap_starttls(num_parms, parms);
1361 else if (!CC->logged_in) {
1362 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1365 /* The commans below require a logged-in state */
1367 else if (!strcasecmp(parms[1], "SELECT")) {
1368 imap_select(num_parms, parms);
1371 else if (!strcasecmp(parms[1], "EXAMINE")) {
1372 imap_select(num_parms, parms);
1375 else if (!strcasecmp(parms[1], "LSUB")) {
1376 imap_lsub(num_parms, parms);
1379 else if (!strcasecmp(parms[1], "LIST")) {
1380 imap_list(num_parms, parms);
1383 else if (!strcasecmp(parms[1], "CREATE")) {
1384 imap_create(num_parms, parms);
1387 else if (!strcasecmp(parms[1], "DELETE")) {
1388 imap_delete(num_parms, parms);
1391 else if (!strcasecmp(parms[1], "RENAME")) {
1392 imap_rename(num_parms, parms);
1395 else if (!strcasecmp(parms[1], "STATUS")) {
1396 imap_status(num_parms, parms);
1399 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1400 imap_subscribe(num_parms, parms);
1403 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1404 imap_unsubscribe(num_parms, parms);
1407 else if (!strcasecmp(parms[1], "APPEND")) {
1408 imap_append(num_parms, parms);
1411 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1412 imap_namespace(num_parms, parms);
1415 else if (IMAP->selected == 0) {
1416 cprintf("%s BAD no folder selected\r\n", parms[0]);
1419 /* The commands below require the SELECT state on a mailbox */
1421 else if (!strcasecmp(parms[1], "FETCH")) {
1422 imap_fetch(num_parms, parms);
1425 else if ((!strcasecmp(parms[1], "UID"))
1426 && (!strcasecmp(parms[2], "FETCH"))) {
1427 imap_uidfetch(num_parms, parms);
1430 else if (!strcasecmp(parms[1], "SEARCH")) {
1431 imap_search(num_parms, parms);
1434 else if ((!strcasecmp(parms[1], "UID"))
1435 && (!strcasecmp(parms[2], "SEARCH"))) {
1436 imap_uidsearch(num_parms, parms);
1439 else if (!strcasecmp(parms[1], "STORE")) {
1440 imap_store(num_parms, parms);
1443 else if ((!strcasecmp(parms[1], "UID"))
1444 && (!strcasecmp(parms[2], "STORE"))) {
1445 imap_uidstore(num_parms, parms);
1448 else if (!strcasecmp(parms[1], "COPY")) {
1449 imap_copy(num_parms, parms);
1452 else if ((!strcasecmp(parms[1], "UID"))
1453 && (!strcasecmp(parms[2], "COPY"))) {
1454 imap_uidcopy(num_parms, parms);
1457 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1458 imap_expunge(num_parms, parms);
1461 else if (!strcasecmp(parms[1], "CLOSE")) {
1462 imap_close(num_parms, parms);
1465 /* End of commands. If we get here, the command is either invalid
1470 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1473 /* If the client transmitted a message we can free it now */
1474 imap_free_transmitted_message();
1480 * This function is called to register the IMAP extension with Citadel.
1482 char *serv_imap_init(void)
1484 CtdlRegisterServiceHook(config.c_imap_port,
1485 NULL, imap_greeting, imap_command_loop, NULL);
1487 CtdlRegisterServiceHook(config.c_imaps_port,
1488 NULL, imaps_greeting, imap_command_loop, NULL);
1490 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);