4 * IMAP server for the Citadel 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 free(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 IMAP->flags[i] |= IMAP_RECENT;
126 (vbuf.v_answered, IMAP->msgids[i])) {
127 IMAP->flags[i] |= IMAP_ANSWERED;
137 * Back end for imap_load_msgids()
139 * Optimization: instead of calling realloc() to add each message, we
140 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
141 * allows the mapping to proceed much faster.
143 void imap_add_single_msgid(long msgnum, void *userdata)
146 IMAP->num_msgs = IMAP->num_msgs + 1;
147 if (IMAP->msgids == NULL) {
148 IMAP->msgids = malloc(IMAP->num_msgs * sizeof(long)
149 * REALLOC_INCREMENT);
150 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
151 IMAP->msgids = realloc(IMAP->msgids,
153 REALLOC_INCREMENT) * sizeof(long));
155 if (IMAP->flags == NULL) {
156 IMAP->flags = malloc(IMAP->num_msgs * sizeof(long)
157 * REALLOC_INCREMENT);
158 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
159 IMAP->flags = realloc(IMAP->flags,
161 REALLOC_INCREMENT) * sizeof(long));
163 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
164 IMAP->flags[IMAP->num_msgs - 1] = 0;
170 * Set up a message ID map for the current room (folder)
172 void imap_load_msgids(void)
175 if (IMAP->selected == 0) {
177 "imap_load_msgids() can't run; no room selected\n");
181 imap_free_msgids(); /* If there was already a map, free it */
183 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
184 imap_add_single_msgid, NULL);
186 imap_set_seen_flags();
187 lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
193 * Re-scan the selected room (folder) and see if it's been changed at all
195 void imap_rescan_msgids(void)
198 int original_num_msgs = 0;
199 long original_highest = 0L;
201 int message_still_exists;
202 struct cdbdata *cdbfr;
203 long *msglist = NULL;
208 if (IMAP->selected == 0) {
210 "imap_load_msgids() can't run; no room selected\n");
214 /* Load the *current* message list from disk, so we can compare it
215 * to what we have in memory.
217 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
219 msglist = malloc(cdbfr->len);
220 if (msglist == NULL) {
221 lprintf(CTDL_CRIT, "malloc() failed\n");
224 memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
225 num_msgs = cdbfr->len / sizeof(long);
232 * Check to see if any of the messages we know about have been expunged
234 if (IMAP->num_msgs > 0)
235 for (i = 0; i < IMAP->num_msgs; ++i) {
237 message_still_exists = 0;
239 for (j = 0; j < num_msgs; ++j) {
240 if (msglist[j] == IMAP->msgids[i]) {
241 message_still_exists = 1;
245 if (message_still_exists == 0) {
246 cprintf("* %d EXPUNGE\r\n", i + 1);
248 /* Here's some nice stupid nonsense. When a message
249 * is expunged, we have to slide all the existing
250 * messages up in the message array.
253 memcpy(&IMAP->msgids[i],
254 &IMAP->msgids[i + 1],
256 (IMAP->num_msgs - i)));
257 memcpy(&IMAP->flags[i],
260 (IMAP->num_msgs - i)));
268 * Remember how many messages were here before we re-scanned.
270 original_num_msgs = IMAP->num_msgs;
271 if (IMAP->num_msgs > 0) {
272 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
274 original_highest = 0L;
278 * Now peruse the room for *new* messages only.
281 for (j = 0; j < num_msgs; ++j) {
282 if (msglist[j] > original_highest) {
283 imap_add_single_msgid(msglist[j], NULL);
286 imap_set_seen_flags();
289 * If new messages have arrived, tell the client about them.
291 if (IMAP->num_msgs > original_num_msgs) {
293 for (j = 0; j < num_msgs; ++j) {
294 if (IMAP->flags[j] & IMAP_RECENT) {
299 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
300 cprintf("* %d RECENT\r\n", num_recent);
314 * This cleanup function blows away the temporary memory and files used by
317 void imap_cleanup_function(void)
320 /* Don't do this stuff if this is not a IMAP session! */
321 if (CC->h_command_function != imap_command_loop)
324 /* If there is a mailbox selected, auto-expunge it. */
325 if (IMAP->selected) {
329 lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
331 imap_free_transmitted_message();
333 if (IMAP->cached_fetch != NULL) {
334 fclose(IMAP->cached_fetch);
335 IMAP->cached_fetch = NULL;
336 IMAP->cached_msgnum = (-1);
339 if (IMAP->cached_body != NULL) {
340 fclose(IMAP->cached_body);
341 IMAP->cached_body = NULL;
342 IMAP->cached_bodymsgnum = (-1);
345 lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
351 * Here's where our IMAP session begins its happy day.
353 void imap_greeting(void)
356 strcpy(CC->cs_clientname, "IMAP session");
357 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
358 IMAP->authstate = imap_as_normal;
359 IMAP->cached_fetch = NULL;
360 IMAP->cached_msgnum = (-1);
362 cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
368 * implements the LOGIN command (ordinary username/password login)
370 void imap_login(int num_parms, char *parms[])
372 if (num_parms != 4) {
373 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
377 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
378 if (CtdlTryPassword(parms[3]) == pass_ok) {
379 cprintf("%s OK login successful\r\n", parms[0]);
384 cprintf("%s BAD Login incorrect\r\n", parms[0]);
389 * Implements the AUTHENTICATE command
391 void imap_authenticate(int num_parms, char *parms[])
395 if (num_parms != 3) {
396 cprintf("%s BAD incorrect number of parameters\r\n",
402 cprintf("%s BAD Already logged in.\r\n", parms[0]);
406 if (!strcasecmp(parms[2], "LOGIN")) {
407 CtdlEncodeBase64(buf, "Username:", 9);
408 cprintf("+ %s\r\n", buf);
409 IMAP->authstate = imap_as_expecting_username;
410 strcpy(IMAP->authseq, parms[0]);
415 cprintf("%s NO AUTHENTICATE %s failed\r\n",
420 void imap_auth_login_user(char *cmd)
424 CtdlDecodeBase64(buf, cmd, SIZ);
425 CtdlLoginExistingUser(buf);
426 CtdlEncodeBase64(buf, "Password:", 9);
427 cprintf("+ %s\r\n", buf);
428 IMAP->authstate = imap_as_expecting_password;
432 void imap_auth_login_pass(char *cmd)
436 CtdlDecodeBase64(buf, cmd, SIZ);
437 if (CtdlTryPassword(buf) == pass_ok) {
438 cprintf("%s OK authentication succeeded\r\n",
441 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
443 IMAP->authstate = imap_as_normal;
449 * implements the CAPABILITY command
451 void imap_capability(int num_parms, char *parms[])
453 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
456 cprintf(" STARTTLS");
460 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
466 * implements the STARTTLS command (Citadel API version)
469 void imap_starttls(int num_parms, char *parms[])
471 char ok_response[SIZ];
472 char nosup_response[SIZ];
473 char error_response[SIZ];
476 "%s OK begin TLS negotiation now\r\n",
478 sprintf(nosup_response,
479 "%s NO TLS not supported here\r\n",
481 sprintf(error_response,
482 "%s BAD Internal error\r\n",
484 CtdlStartTLS(ok_response, nosup_response, error_response);
490 * implements the SELECT command
492 void imap_select(int num_parms, char *parms[])
495 char augmented_roomname[ROOMNAMELEN];
499 struct ctdlroom QRscratch;
505 /* Convert the supplied folder name to a roomname */
506 i = imap_roomname(towhere, sizeof towhere, parms[2]);
508 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
512 floornum = (i & 0x00ff);
513 roomflags = (i & 0xff00);
515 /* First try a regular match */
516 c = getroom(&QRscratch, towhere);
518 /* Then try a mailbox name match */
520 MailboxName(augmented_roomname, sizeof augmented_roomname,
522 c = getroom(&QRscratch, augmented_roomname);
524 strcpy(towhere, augmented_roomname);
527 /* If the room exists, check security/access */
529 /* See if there is an existing user/room relationship */
530 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
532 /* normal clients have to pass through security */
538 /* Fail here if no such room */
540 cprintf("%s NO ... no such room, or access denied\r\n",
545 /* If we already had some other folder selected, auto-expunge it */
549 * usergoto() formally takes us to the desired room, happily returning
550 * the number of messages and number of new messages.
552 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
553 usergoto(NULL, 0, 0, &msgs, &new);
556 if (!strcasecmp(parms[1], "EXAMINE")) {
564 cprintf("* %d EXISTS\r\n", msgs);
565 cprintf("* %d RECENT\r\n", new);
567 /* Note that \Deleted is a valid flag, but not a permanent flag,
568 * because we don't maintain its state across sessions. Citadel
569 * automatically expunges mailboxes when they are de-selected.
571 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
572 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
573 "permanent flags\r\n");
575 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
576 cprintf("%s OK [%s] %s completed\r\n",
578 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
584 * does the real work for expunge
586 int imap_do_expunge(void)
589 int num_expunged = 0;
591 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
592 if (IMAP->selected == 0) {
596 if (IMAP->num_msgs > 0)
597 for (i = 0; i < IMAP->num_msgs; ++i) {
598 if (IMAP->flags[i] & IMAP_DELETED) {
599 CtdlDeleteMessages(CC->room.QRname,
600 IMAP->msgids[i], "");
605 if (num_expunged > 0) {
606 imap_rescan_msgids();
609 lprintf(CTDL_DEBUG, "Expunged %d messages.\n", num_expunged);
610 return (num_expunged);
615 * implements the EXPUNGE command syntax
617 void imap_expunge(int num_parms, char *parms[])
619 int num_expunged = 0;
621 num_expunged = imap_do_expunge();
622 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
627 * implements the CLOSE command
629 void imap_close(int num_parms, char *parms[])
632 /* Yes, we always expunge on close. */
633 if (IMAP->selected) {
640 cprintf("%s OK CLOSE completed\r\n", parms[0]);
645 * Implements the NAMESPACE command.
647 void imap_namespace(int num_parms, char *parms[])
654 cprintf("* NAMESPACE ");
656 /* All personal folders are subordinate to INBOX. */
657 cprintf("((\"INBOX/\" \"/\")) ");
659 /* Other users' folders ... coming soon! FIXME */
662 /* Show all floors as shared namespaces. Neato! */
664 for (i = 0; i < MAXFLOORS; ++i) {
666 if (fl->f_flags & F_INUSE) {
667 if (floors > 0) cprintf(" ");
669 sprintf(buf, "%s/", fl->f_name);
677 /* Wind it up with a newline and a completion message. */
679 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
685 * Used by LIST and LSUB to show the floors in the listing
687 void imap_list_floors(char *cmd, char *pattern)
692 for (i = 0; i < MAXFLOORS; ++i) {
694 if (fl->f_flags & F_INUSE) {
695 if (imap_mailbox_matches_pattern
696 (pattern, fl->f_name)) {
697 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
698 imap_strout(fl->f_name);
708 * Back end for imap_lsub()
710 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
711 * may or may not be the desired behavior in the future.
713 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
719 pattern = (char *) data;
721 /* Only list rooms to which the user has access!! */
722 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
724 imap_mailboxname(buf, sizeof buf, qrbuf);
725 if (imap_mailbox_matches_pattern(pattern, buf)) {
726 cprintf("* LSUB () \"/\" ");
735 * Implements the LSUB command
737 void imap_lsub(int num_parms, char *parms[])
741 cprintf("%s BAD arguments invalid\r\n", parms[0]);
744 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
746 if (strlen(parms[3]) == 0) {
747 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
751 imap_list_floors("LSUB", pattern);
752 ForEachRoom(imap_lsub_listroom, pattern);
755 cprintf("%s OK LSUB completed\r\n", parms[0]);
761 * Back end for imap_list()
763 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
769 pattern = (char *) data;
771 /* Only list rooms to which the user has access!! */
772 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
774 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
775 imap_mailboxname(buf, sizeof buf, qrbuf);
776 if (imap_mailbox_matches_pattern(pattern, buf)) {
777 cprintf("* LIST () \"/\" ");
786 * Implements the LIST command
788 void imap_list(int num_parms, char *parms[])
792 cprintf("%s BAD arguments invalid\r\n", parms[0]);
795 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
797 if (strlen(parms[3]) == 0) {
798 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
802 imap_list_floors("LIST", pattern);
803 ForEachRoom(imap_list_listroom, pattern);
806 cprintf("%s OK LIST completed\r\n", parms[0]);
812 * Implements the CREATE command
815 void imap_create(int num_parms, char *parms[])
818 char roomname[ROOMNAMELEN];
823 if (strchr(parms[2], '\\') != NULL) {
824 cprintf("%s NO Invalid character in folder name\r\n",
826 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
830 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
832 cprintf("%s NO Invalid mailbox name or location\r\n",
834 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
837 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
838 flags = (ret & 0xff00); /* upper 8 bits = flags */
840 if (flags & IR_MAILBOX) {
841 if (strncasecmp(parms[2], "INBOX/", 6)) {
842 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
843 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
848 if (flags & IR_MAILBOX) {
849 newroomtype = 4; /* private mailbox */
851 newroomtype = 0; /* public folder */
854 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
855 roomname, floornum, newroomtype);
857 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, VIEW_BBS);
860 ("%s NO Mailbox already exists, or create failed\r\n",
863 cprintf("%s OK CREATE completed\r\n", parms[0]);
865 lprintf(CTDL_DEBUG, "imap_create() completed\n");
870 * Locate a room by its IMAP folder name, and check access to it
872 int imap_grabroom(char *returned_roomname, char *foldername)
875 char augmented_roomname[ROOMNAMELEN];
876 char roomname[ROOMNAMELEN];
878 struct ctdlroom QRscratch;
882 ret = imap_roomname(roomname, sizeof roomname, foldername);
887 /* First try a regular match */
888 c = getroom(&QRscratch, roomname);
890 /* Then try a mailbox name match */
892 MailboxName(augmented_roomname, sizeof augmented_roomname,
893 &CC->user, roomname);
894 c = getroom(&QRscratch, augmented_roomname);
896 strcpy(roomname, augmented_roomname);
899 /* If the room exists, check security/access */
901 /* See if there is an existing user/room relationship */
902 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
904 /* normal clients have to pass through security */
910 /* Fail here if no such room */
912 strcpy(returned_roomname, "");
915 strcpy(returned_roomname, QRscratch.QRname);
922 * Implements the STATUS command (sort of)
925 void imap_status(int num_parms, char *parms[])
928 char roomname[ROOMNAMELEN];
930 char savedroom[ROOMNAMELEN];
933 ret = imap_grabroom(roomname, parms[2]);
936 ("%s NO Invalid mailbox name or location, or access denied\r\n",
942 * usergoto() formally takes us to the desired room, happily returning
943 * the number of messages and number of new messages. (If another
944 * folder is selected, save its name so we can return there!!!!!)
946 if (IMAP->selected) {
947 strcpy(savedroom, CC->room.QRname);
949 usergoto(roomname, 0, 0, &msgs, &new);
952 * Tell the client what it wants to know. In fact, tell it *more* than
953 * it wants to know. We happily IGnore the supplied status data item
954 * names and simply spew all possible data items. It's far easier to
955 * code and probably saves us some processing time too.
957 imap_mailboxname(buf, sizeof buf, &CC->room);
958 cprintf("* STATUS ");
960 cprintf(" (MESSAGES %d ", msgs);
961 cprintf("RECENT %d ", new); /* Initially, new==recent */
962 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
963 cprintf("UNSEEN %d)\r\n", new);
966 * If another folder is selected, go back to that room so we can resume
967 * our happy day without violent explosions.
969 if (IMAP->selected) {
970 usergoto(savedroom, 0, 0, &msgs, &new);
974 * Oooh, look, we're done!
976 cprintf("%s OK STATUS completed\r\n", parms[0]);
982 * Implements the SUBSCRIBE command
985 void imap_subscribe(int num_parms, char *parms[])
988 char roomname[ROOMNAMELEN];
989 char savedroom[ROOMNAMELEN];
992 ret = imap_grabroom(roomname, parms[2]);
995 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1001 * usergoto() formally takes us to the desired room, which has the side
1002 * effect of marking the room as not-zapped ... exactly the effect
1003 * we're looking for.
1005 if (IMAP->selected) {
1006 strcpy(savedroom, CC->room.QRname);
1008 usergoto(roomname, 0, 0, &msgs, &new);
1011 * If another folder is selected, go back to that room so we can resume
1012 * our happy day without violent explosions.
1014 if (IMAP->selected) {
1015 usergoto(savedroom, 0, 0, &msgs, &new);
1018 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1023 * Implements the UNSUBSCRIBE command
1026 void imap_unsubscribe(int num_parms, char *parms[])
1029 char roomname[ROOMNAMELEN];
1030 char savedroom[ROOMNAMELEN];
1033 ret = imap_grabroom(roomname, parms[2]);
1036 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1042 * usergoto() formally takes us to the desired room.
1044 if (IMAP->selected) {
1045 strcpy(savedroom, CC->room.QRname);
1047 usergoto(roomname, 0, 0, &msgs, &new);
1050 * Now make the API call to zap the room
1052 if (CtdlForgetThisRoom() == 0) {
1053 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1056 ("%s NO You may not unsubscribe from this folder.\r\n",
1061 * If another folder is selected, go back to that room so we can resume
1062 * our happy day without violent explosions.
1064 if (IMAP->selected) {
1065 usergoto(savedroom, 0, 0, &msgs, &new);
1072 * Implements the DELETE command
1075 void imap_delete(int num_parms, char *parms[])
1078 char roomname[ROOMNAMELEN];
1079 char savedroom[ROOMNAMELEN];
1082 ret = imap_grabroom(roomname, parms[2]);
1084 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1090 * usergoto() formally takes us to the desired room, happily returning
1091 * the number of messages and number of new messages. (If another
1092 * folder is selected, save its name so we can return there!!!!!)
1094 if (IMAP->selected) {
1095 strcpy(savedroom, CC->room.QRname);
1097 usergoto(roomname, 0, 0, &msgs, &new);
1100 * Now delete the room.
1102 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1103 cprintf("%s OK DELETE completed\r\n", parms[0]);
1104 delete_room(&CC->room);
1106 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1110 * If another folder is selected, go back to that room so we can resume
1111 * our happy day without violent explosions.
1113 if (IMAP->selected) {
1114 usergoto(savedroom, 0, 0, &msgs, &new);
1120 * Back end function for imap_rename()
1122 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1124 char foldername[SIZ];
1125 char newfoldername[SIZ];
1126 char newroomname[ROOMNAMELEN];
1128 struct irl *irlp = NULL; /* scratch pointer */
1129 struct irlparms *irlparms;
1131 irlparms = (struct irlparms *) data;
1132 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1134 /* Rename subfolders */
1135 if ((!strncasecmp(foldername, irlparms->oldname,
1136 strlen(irlparms->oldname))
1137 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1139 sprintf(newfoldername, "%s/%s",
1141 &foldername[strlen(irlparms->oldname) + 1]
1144 newfloor = imap_roomname(newroomname,
1146 newfoldername) & 0xFF;
1148 irlp = (struct irl *) malloc(sizeof(struct irl));
1149 strcpy(irlp->irl_newroom, newroomname);
1150 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1151 irlp->irl_newfloor = newfloor;
1152 irlp->next = *(irlparms->irl);
1153 *(irlparms->irl) = irlp;
1159 * Implements the RENAME command
1162 void imap_rename(int num_parms, char *parms[])
1164 char old_room[ROOMNAMELEN];
1165 char new_room[ROOMNAMELEN];
1169 struct irl *irl = NULL; /* the list */
1170 struct irl *irlp = NULL; /* scratch pointer */
1171 struct irlparms irlparms;
1173 if (strchr(parms[3], '\\') != NULL) {
1174 cprintf("%s NO Invalid character in folder name\r\n",
1179 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1180 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1181 new_floor = (newr & 0xFF);
1183 r = CtdlRenameRoom(old_room, new_room, new_floor);
1185 if (r == crr_room_not_found) {
1186 cprintf("%s NO Could not locate this folder\r\n",
1190 if (r == crr_already_exists) {
1191 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1194 if (r == crr_noneditable) {
1195 cprintf("%s This folder is not editable.\r\n", parms[0]);
1198 if (r == crr_invalid_floor) {
1199 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1202 if (r == crr_access_denied) {
1203 cprintf("%s You do not have permission to edit "
1204 "this folder.\r\n", parms[0]);
1208 cprintf("%s NO Rename failed - undefined error %d\r\n",
1214 /* If this is the INBOX, then RFC2060 says we have to just move the
1215 * contents. In a Citadel environment it's easier to rename the room
1216 * (already did that) and create a new inbox.
1218 if (!strcasecmp(parms[2], "INBOX")) {
1219 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_BBS);
1222 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1224 irlparms.oldname = parms[2];
1225 irlparms.newname = parms[3];
1226 irlparms.irl = &irl;
1227 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1229 /* ... and now rename them. */
1230 while (irl != NULL) {
1231 r = CtdlRenameRoom(irl->irl_oldroom,
1235 /* FIXME handle error returns better */
1236 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1245 cprintf("%s OK RENAME completed\r\n", parms[0]);
1252 * Main command loop for IMAP sessions.
1254 void imap_command_loop(void)
1260 CC->lastcmd = time(NULL);
1261 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1263 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1264 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1269 lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
1270 while (strlen(cmdbuf) < 5)
1271 strcat(cmdbuf, " ");
1273 /* strip off l/t whitespace and CRLF */
1274 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1275 cmdbuf[strlen(cmdbuf) - 1] = 0;
1276 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1277 cmdbuf[strlen(cmdbuf) - 1] = 0;
1280 /* If we're in the middle of a multi-line command, handle that */
1281 if (IMAP->authstate == imap_as_expecting_username) {
1282 imap_auth_login_user(cmdbuf);
1285 if (IMAP->authstate == imap_as_expecting_password) {
1286 imap_auth_login_pass(cmdbuf);
1290 /* Ok, at this point we're in normal command mode. The first thing
1291 * we do is print any incoming pages (yeah! we really do!)
1293 imap_print_instant_messages();
1296 * Before processing the command that was just entered... if we happen
1297 * to have a folder selected, we'd like to rescan that folder for new
1298 * messages, and for deletions/changes of existing messages. This
1299 * could probably be optimized somehow, but IMAP sucks...
1301 if (IMAP->selected) {
1302 imap_rescan_msgids();
1305 /* Now for the command set. */
1307 /* Grab the tag, command, and parameters. Check syntax. */
1308 num_parms = imap_parameterize(parms, cmdbuf);
1309 if (num_parms < 2) {
1310 cprintf("BAD syntax error\r\n");
1313 /* The commands below may be executed in any state */
1315 else if ((!strcasecmp(parms[1], "NOOP"))
1316 || (!strcasecmp(parms[1], "CHECK"))) {
1317 cprintf("%s OK This command successfully did nothing.\r\n",
1321 else if (!strcasecmp(parms[1], "LOGOUT")) {
1322 if (IMAP->selected) {
1323 imap_do_expunge(); /* yes, we auto-expunge */
1325 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1326 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1332 else if (!strcasecmp(parms[1], "LOGIN")) {
1333 imap_login(num_parms, parms);
1336 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1337 imap_authenticate(num_parms, parms);
1340 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1341 imap_capability(num_parms, parms);
1344 else if (!strcasecmp(parms[1], "STARTTLS")) {
1345 imap_starttls(num_parms, parms);
1348 else if (!CC->logged_in) {
1349 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1352 /* The commans below require a logged-in state */
1354 else if (!strcasecmp(parms[1], "SELECT")) {
1355 imap_select(num_parms, parms);
1358 else if (!strcasecmp(parms[1], "EXAMINE")) {
1359 imap_select(num_parms, parms);
1362 else if (!strcasecmp(parms[1], "LSUB")) {
1363 imap_lsub(num_parms, parms);
1366 else if (!strcasecmp(parms[1], "LIST")) {
1367 imap_list(num_parms, parms);
1370 else if (!strcasecmp(parms[1], "CREATE")) {
1371 imap_create(num_parms, parms);
1374 else if (!strcasecmp(parms[1], "DELETE")) {
1375 imap_delete(num_parms, parms);
1378 else if (!strcasecmp(parms[1], "RENAME")) {
1379 imap_rename(num_parms, parms);
1382 else if (!strcasecmp(parms[1], "STATUS")) {
1383 imap_status(num_parms, parms);
1386 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1387 imap_subscribe(num_parms, parms);
1390 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1391 imap_unsubscribe(num_parms, parms);
1394 else if (!strcasecmp(parms[1], "APPEND")) {
1395 imap_append(num_parms, parms);
1398 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1399 imap_namespace(num_parms, parms);
1402 else if (IMAP->selected == 0) {
1403 cprintf("%s BAD no folder selected\r\n", parms[0]);
1406 /* The commands below require the SELECT state on a mailbox */
1408 else if (!strcasecmp(parms[1], "FETCH")) {
1409 imap_fetch(num_parms, parms);
1412 else if ((!strcasecmp(parms[1], "UID"))
1413 && (!strcasecmp(parms[2], "FETCH"))) {
1414 imap_uidfetch(num_parms, parms);
1417 else if (!strcasecmp(parms[1], "SEARCH")) {
1418 imap_search(num_parms, parms);
1421 else if ((!strcasecmp(parms[1], "UID"))
1422 && (!strcasecmp(parms[2], "SEARCH"))) {
1423 imap_uidsearch(num_parms, parms);
1426 else if (!strcasecmp(parms[1], "STORE")) {
1427 imap_store(num_parms, parms);
1430 else if ((!strcasecmp(parms[1], "UID"))
1431 && (!strcasecmp(parms[2], "STORE"))) {
1432 imap_uidstore(num_parms, parms);
1435 else if (!strcasecmp(parms[1], "COPY")) {
1436 imap_copy(num_parms, parms);
1439 else if ((!strcasecmp(parms[1], "UID"))
1440 && (!strcasecmp(parms[2], "COPY"))) {
1441 imap_uidcopy(num_parms, parms);
1444 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1445 imap_expunge(num_parms, parms);
1448 else if (!strcasecmp(parms[1], "CLOSE")) {
1449 imap_close(num_parms, parms);
1452 /* End of commands. If we get here, the command is either invalid
1457 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1460 /* If the client transmitted a message we can free it now */
1461 imap_free_transmitted_message();
1467 * This function is called to register the IMAP extension with Citadel.
1469 char *serv_imap_init(void)
1471 CtdlRegisterServiceHook(config.c_imap_port,
1472 NULL, imap_greeting, imap_command_loop, NULL);
1473 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);