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",
367 * IMAPS is just like IMAP, except it goes crypto right away.
370 void imaps_greeting(void) {
371 CtdlStartTLS(NULL, NULL, NULL);
378 * implements the LOGIN command (ordinary username/password login)
380 void imap_login(int num_parms, char *parms[])
382 if (num_parms != 4) {
383 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
387 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
388 if (CtdlTryPassword(parms[3]) == pass_ok) {
389 cprintf("%s OK login successful\r\n", parms[0]);
394 cprintf("%s BAD Login incorrect\r\n", parms[0]);
399 * Implements the AUTHENTICATE command
401 void imap_authenticate(int num_parms, char *parms[])
405 if (num_parms != 3) {
406 cprintf("%s BAD incorrect number of parameters\r\n",
412 cprintf("%s BAD Already logged in.\r\n", parms[0]);
416 if (!strcasecmp(parms[2], "LOGIN")) {
417 CtdlEncodeBase64(buf, "Username:", 9);
418 cprintf("+ %s\r\n", buf);
419 IMAP->authstate = imap_as_expecting_username;
420 strcpy(IMAP->authseq, parms[0]);
425 cprintf("%s NO AUTHENTICATE %s failed\r\n",
430 void imap_auth_login_user(char *cmd)
434 CtdlDecodeBase64(buf, cmd, SIZ);
435 CtdlLoginExistingUser(buf);
436 CtdlEncodeBase64(buf, "Password:", 9);
437 cprintf("+ %s\r\n", buf);
438 IMAP->authstate = imap_as_expecting_password;
442 void imap_auth_login_pass(char *cmd)
446 CtdlDecodeBase64(buf, cmd, SIZ);
447 if (CtdlTryPassword(buf) == pass_ok) {
448 cprintf("%s OK authentication succeeded\r\n",
451 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
453 IMAP->authstate = imap_as_normal;
459 * implements the CAPABILITY command
461 void imap_capability(int num_parms, char *parms[])
463 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
466 cprintf(" STARTTLS");
470 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
476 * implements the STARTTLS command (Citadel API version)
479 void imap_starttls(int num_parms, char *parms[])
481 char ok_response[SIZ];
482 char nosup_response[SIZ];
483 char error_response[SIZ];
486 "%s OK begin TLS negotiation now\r\n",
488 sprintf(nosup_response,
489 "%s NO TLS not supported here\r\n",
491 sprintf(error_response,
492 "%s BAD Internal error\r\n",
494 CtdlStartTLS(ok_response, nosup_response, error_response);
500 * implements the SELECT command
502 void imap_select(int num_parms, char *parms[])
505 char augmented_roomname[ROOMNAMELEN];
509 struct ctdlroom QRscratch;
515 /* Convert the supplied folder name to a roomname */
516 i = imap_roomname(towhere, sizeof towhere, parms[2]);
518 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
522 floornum = (i & 0x00ff);
523 roomflags = (i & 0xff00);
525 /* First try a regular match */
526 c = getroom(&QRscratch, towhere);
528 /* Then try a mailbox name match */
530 MailboxName(augmented_roomname, sizeof augmented_roomname,
532 c = getroom(&QRscratch, augmented_roomname);
534 strcpy(towhere, augmented_roomname);
537 /* If the room exists, check security/access */
539 /* See if there is an existing user/room relationship */
540 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
542 /* normal clients have to pass through security */
548 /* Fail here if no such room */
550 cprintf("%s NO ... no such room, or access denied\r\n",
555 /* If we already had some other folder selected, auto-expunge it */
559 * usergoto() formally takes us to the desired room, happily returning
560 * the number of messages and number of new messages.
562 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
563 usergoto(NULL, 0, 0, &msgs, &new);
566 if (!strcasecmp(parms[1], "EXAMINE")) {
574 cprintf("* %d EXISTS\r\n", msgs);
575 cprintf("* %d RECENT\r\n", new);
577 /* Note that \Deleted is a valid flag, but not a permanent flag,
578 * because we don't maintain its state across sessions. Citadel
579 * automatically expunges mailboxes when they are de-selected.
581 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
582 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
583 "permanent flags\r\n");
585 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
586 cprintf("%s OK [%s] %s completed\r\n",
588 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
594 * does the real work for expunge
596 int imap_do_expunge(void)
599 int num_expunged = 0;
601 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
602 if (IMAP->selected == 0) {
606 if (IMAP->num_msgs > 0)
607 for (i = 0; i < IMAP->num_msgs; ++i) {
608 if (IMAP->flags[i] & IMAP_DELETED) {
609 CtdlDeleteMessages(CC->room.QRname,
610 IMAP->msgids[i], "");
615 if (num_expunged > 0) {
616 imap_rescan_msgids();
619 lprintf(CTDL_DEBUG, "Expunged %d messages.\n", num_expunged);
620 return (num_expunged);
625 * implements the EXPUNGE command syntax
627 void imap_expunge(int num_parms, char *parms[])
629 int num_expunged = 0;
631 num_expunged = imap_do_expunge();
632 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
637 * implements the CLOSE command
639 void imap_close(int num_parms, char *parms[])
642 /* Yes, we always expunge on close. */
643 if (IMAP->selected) {
650 cprintf("%s OK CLOSE completed\r\n", parms[0]);
655 * Implements the NAMESPACE command.
657 void imap_namespace(int num_parms, char *parms[])
664 cprintf("* NAMESPACE ");
666 /* All personal folders are subordinate to INBOX. */
667 cprintf("((\"INBOX/\" \"/\")) ");
669 /* Other users' folders ... coming soon! FIXME */
672 /* Show all floors as shared namespaces. Neato! */
674 for (i = 0; i < MAXFLOORS; ++i) {
676 if (fl->f_flags & F_INUSE) {
677 if (floors > 0) cprintf(" ");
679 sprintf(buf, "%s/", fl->f_name);
687 /* Wind it up with a newline and a completion message. */
689 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
695 * Used by LIST and LSUB to show the floors in the listing
697 void imap_list_floors(char *cmd, char *pattern)
702 for (i = 0; i < MAXFLOORS; ++i) {
704 if (fl->f_flags & F_INUSE) {
705 if (imap_mailbox_matches_pattern
706 (pattern, fl->f_name)) {
707 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
708 imap_strout(fl->f_name);
718 * Back end for imap_lsub()
720 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
721 * may or may not be the desired behavior in the future.
723 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
729 pattern = (char *) data;
731 /* Only list rooms to which the user has access!! */
732 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
734 imap_mailboxname(buf, sizeof buf, qrbuf);
735 if (imap_mailbox_matches_pattern(pattern, buf)) {
736 cprintf("* LSUB () \"/\" ");
745 * Implements the LSUB command
747 void imap_lsub(int num_parms, char *parms[])
751 cprintf("%s BAD arguments invalid\r\n", parms[0]);
754 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
756 if (strlen(parms[3]) == 0) {
757 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
761 imap_list_floors("LSUB", pattern);
762 ForEachRoom(imap_lsub_listroom, pattern);
765 cprintf("%s OK LSUB completed\r\n", parms[0]);
771 * Back end for imap_list()
773 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
779 pattern = (char *) data;
781 /* Only list rooms to which the user has access!! */
782 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
784 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
785 imap_mailboxname(buf, sizeof buf, qrbuf);
786 if (imap_mailbox_matches_pattern(pattern, buf)) {
787 cprintf("* LIST () \"/\" ");
796 * Implements the LIST command
798 void imap_list(int num_parms, char *parms[])
802 cprintf("%s BAD arguments invalid\r\n", parms[0]);
805 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
807 if (strlen(parms[3]) == 0) {
808 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
812 imap_list_floors("LIST", pattern);
813 ForEachRoom(imap_list_listroom, pattern);
816 cprintf("%s OK LIST completed\r\n", parms[0]);
822 * Implements the CREATE command
825 void imap_create(int num_parms, char *parms[])
828 char roomname[ROOMNAMELEN];
834 if (strchr(parms[2], '\\') != NULL) {
835 cprintf("%s NO Invalid character in folder name\r\n",
837 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
841 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
843 cprintf("%s NO Invalid mailbox name or location\r\n",
845 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
848 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
849 flags = (ret & 0xff00); /* upper 8 bits = flags */
851 if (flags & IR_MAILBOX) {
852 if (strncasecmp(parms[2], "INBOX/", 6)) {
853 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
854 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
859 if (flags & IR_MAILBOX) {
860 newroomtype = 4; /* private mailbox */
861 newroomview = VIEW_MAILBOX;
863 newroomtype = 0; /* public folder */
864 newroomview = VIEW_BBS;
867 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
868 roomname, floornum, newroomtype);
870 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
873 ("%s NO Mailbox already exists, or create failed\r\n",
876 cprintf("%s OK CREATE completed\r\n", parms[0]);
878 lprintf(CTDL_DEBUG, "imap_create() completed\n");
883 * Locate a room by its IMAP folder name, and check access to it.
884 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
886 int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
889 char augmented_roomname[ROOMNAMELEN];
890 char roomname[ROOMNAMELEN];
892 struct ctdlroom QRscratch;
896 ret = imap_roomname(roomname, sizeof roomname, foldername);
901 /* First try a regular match */
902 c = getroom(&QRscratch, roomname);
904 /* Then try a mailbox name match */
906 MailboxName(augmented_roomname, sizeof augmented_roomname,
907 &CC->user, roomname);
908 c = getroom(&QRscratch, augmented_roomname);
910 strcpy(roomname, augmented_roomname);
913 /* If the room exists, check security/access */
915 /* See if there is an existing user/room relationship */
916 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
918 /* normal clients have to pass through security */
922 if ((zapped_ok) && (ra & UA_ZAPPED)) {
927 /* Fail here if no such room */
929 strcpy(returned_roomname, "");
932 strcpy(returned_roomname, QRscratch.QRname);
939 * Implements the STATUS command (sort of)
942 void imap_status(int num_parms, char *parms[])
945 char roomname[ROOMNAMELEN];
947 char savedroom[ROOMNAMELEN];
950 ret = imap_grabroom(roomname, parms[2], 0);
953 ("%s NO Invalid mailbox name or location, or access denied\r\n",
959 * usergoto() formally takes us to the desired room, happily returning
960 * the number of messages and number of new messages. (If another
961 * folder is selected, save its name so we can return there!!!!!)
963 if (IMAP->selected) {
964 strcpy(savedroom, CC->room.QRname);
966 usergoto(roomname, 0, 0, &msgs, &new);
969 * Tell the client what it wants to know. In fact, tell it *more* than
970 * it wants to know. We happily IGnore the supplied status data item
971 * names and simply spew all possible data items. It's far easier to
972 * code and probably saves us some processing time too.
974 imap_mailboxname(buf, sizeof buf, &CC->room);
975 cprintf("* STATUS ");
977 cprintf(" (MESSAGES %d ", msgs);
978 cprintf("RECENT %d ", new); /* Initially, new==recent */
979 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
980 cprintf("UNSEEN %d)\r\n", new);
983 * If another folder is selected, go back to that room so we can resume
984 * our happy day without violent explosions.
986 if (IMAP->selected) {
987 usergoto(savedroom, 0, 0, &msgs, &new);
991 * Oooh, look, we're done!
993 cprintf("%s OK STATUS completed\r\n", parms[0]);
999 * Implements the SUBSCRIBE command
1002 void imap_subscribe(int num_parms, char *parms[])
1005 char roomname[ROOMNAMELEN];
1006 char savedroom[ROOMNAMELEN];
1009 ret = imap_grabroom(roomname, parms[2], 1);
1012 "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1020 * usergoto() formally takes us to the desired room, which has the side
1021 * effect of marking the room as not-zapped ... exactly the effect
1022 * we're looking for.
1024 if (IMAP->selected) {
1025 strcpy(savedroom, CC->room.QRname);
1027 usergoto(roomname, 0, 0, &msgs, &new);
1030 * If another folder is selected, go back to that room so we can resume
1031 * our happy day without violent explosions.
1033 if (IMAP->selected) {
1034 usergoto(savedroom, 0, 0, &msgs, &new);
1037 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1042 * Implements the UNSUBSCRIBE command
1045 void imap_unsubscribe(int num_parms, char *parms[])
1048 char roomname[ROOMNAMELEN];
1049 char savedroom[ROOMNAMELEN];
1052 ret = imap_grabroom(roomname, parms[2], 0);
1055 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1061 * usergoto() formally takes us to the desired room.
1063 if (IMAP->selected) {
1064 strcpy(savedroom, CC->room.QRname);
1066 usergoto(roomname, 0, 0, &msgs, &new);
1069 * Now make the API call to zap the room
1071 if (CtdlForgetThisRoom() == 0) {
1072 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1075 ("%s NO You may not unsubscribe from this folder.\r\n",
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);
1091 * Implements the DELETE command
1094 void imap_delete(int num_parms, char *parms[])
1097 char roomname[ROOMNAMELEN];
1098 char savedroom[ROOMNAMELEN];
1101 ret = imap_grabroom(roomname, parms[2], 0);
1103 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1109 * usergoto() formally takes us to the desired room, happily returning
1110 * the number of messages and number of new messages. (If another
1111 * folder is selected, save its name so we can return there!!!!!)
1113 if (IMAP->selected) {
1114 strcpy(savedroom, CC->room.QRname);
1116 usergoto(roomname, 0, 0, &msgs, &new);
1119 * Now delete the room.
1121 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1122 cprintf("%s OK DELETE completed\r\n", parms[0]);
1123 delete_room(&CC->room);
1125 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1129 * If another folder is selected, go back to that room so we can resume
1130 * our happy day without violent explosions.
1132 if (IMAP->selected) {
1133 usergoto(savedroom, 0, 0, &msgs, &new);
1139 * Back end function for imap_rename()
1141 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1143 char foldername[SIZ];
1144 char newfoldername[SIZ];
1145 char newroomname[ROOMNAMELEN];
1147 struct irl *irlp = NULL; /* scratch pointer */
1148 struct irlparms *irlparms;
1150 irlparms = (struct irlparms *) data;
1151 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1153 /* Rename subfolders */
1154 if ((!strncasecmp(foldername, irlparms->oldname,
1155 strlen(irlparms->oldname))
1156 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1158 sprintf(newfoldername, "%s/%s",
1160 &foldername[strlen(irlparms->oldname) + 1]
1163 newfloor = imap_roomname(newroomname,
1165 newfoldername) & 0xFF;
1167 irlp = (struct irl *) malloc(sizeof(struct irl));
1168 strcpy(irlp->irl_newroom, newroomname);
1169 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1170 irlp->irl_newfloor = newfloor;
1171 irlp->next = *(irlparms->irl);
1172 *(irlparms->irl) = irlp;
1178 * Implements the RENAME command
1181 void imap_rename(int num_parms, char *parms[])
1183 char old_room[ROOMNAMELEN];
1184 char new_room[ROOMNAMELEN];
1188 struct irl *irl = NULL; /* the list */
1189 struct irl *irlp = NULL; /* scratch pointer */
1190 struct irlparms irlparms;
1192 if (strchr(parms[3], '\\') != NULL) {
1193 cprintf("%s NO Invalid character in folder name\r\n",
1198 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1199 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1200 new_floor = (newr & 0xFF);
1202 r = CtdlRenameRoom(old_room, new_room, new_floor);
1204 if (r == crr_room_not_found) {
1205 cprintf("%s NO Could not locate this folder\r\n",
1209 if (r == crr_already_exists) {
1210 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1213 if (r == crr_noneditable) {
1214 cprintf("%s This folder is not editable.\r\n", parms[0]);
1217 if (r == crr_invalid_floor) {
1218 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1221 if (r == crr_access_denied) {
1222 cprintf("%s You do not have permission to edit "
1223 "this folder.\r\n", parms[0]);
1227 cprintf("%s NO Rename failed - undefined error %d\r\n",
1233 /* If this is the INBOX, then RFC2060 says we have to just move the
1234 * contents. In a Citadel environment it's easier to rename the room
1235 * (already did that) and create a new inbox.
1237 if (!strcasecmp(parms[2], "INBOX")) {
1238 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_BBS);
1241 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1243 irlparms.oldname = parms[2];
1244 irlparms.newname = parms[3];
1245 irlparms.irl = &irl;
1246 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1248 /* ... and now rename them. */
1249 while (irl != NULL) {
1250 r = CtdlRenameRoom(irl->irl_oldroom,
1254 /* FIXME handle error returns better */
1255 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1264 cprintf("%s OK RENAME completed\r\n", parms[0]);
1271 * Main command loop for IMAP sessions.
1273 void imap_command_loop(void)
1279 CC->lastcmd = time(NULL);
1280 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1282 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1283 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1288 lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
1289 while (strlen(cmdbuf) < 5)
1290 strcat(cmdbuf, " ");
1292 /* strip off l/t whitespace and CRLF */
1293 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1294 cmdbuf[strlen(cmdbuf) - 1] = 0;
1295 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1296 cmdbuf[strlen(cmdbuf) - 1] = 0;
1299 /* If we're in the middle of a multi-line command, handle that */
1300 if (IMAP->authstate == imap_as_expecting_username) {
1301 imap_auth_login_user(cmdbuf);
1304 if (IMAP->authstate == imap_as_expecting_password) {
1305 imap_auth_login_pass(cmdbuf);
1309 /* Ok, at this point we're in normal command mode. The first thing
1310 * we do is print any incoming pages (yeah! we really do!)
1312 imap_print_instant_messages();
1315 * Before processing the command that was just entered... if we happen
1316 * to have a folder selected, we'd like to rescan that folder for new
1317 * messages, and for deletions/changes of existing messages. This
1318 * could probably be optimized somehow, but IMAP sucks...
1320 if (IMAP->selected) {
1321 imap_rescan_msgids();
1324 /* Now for the command set. */
1326 /* Grab the tag, command, and parameters. Check syntax. */
1327 num_parms = imap_parameterize(parms, cmdbuf);
1328 if (num_parms < 2) {
1329 cprintf("BAD syntax error\r\n");
1332 /* The commands below may be executed in any state */
1334 else if ((!strcasecmp(parms[1], "NOOP"))
1335 || (!strcasecmp(parms[1], "CHECK"))) {
1336 cprintf("%s OK This command successfully did nothing.\r\n",
1340 else if (!strcasecmp(parms[1], "LOGOUT")) {
1341 if (IMAP->selected) {
1342 imap_do_expunge(); /* yes, we auto-expunge */
1344 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1345 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1351 else if (!strcasecmp(parms[1], "LOGIN")) {
1352 imap_login(num_parms, parms);
1355 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1356 imap_authenticate(num_parms, parms);
1359 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1360 imap_capability(num_parms, parms);
1363 else if (!strcasecmp(parms[1], "STARTTLS")) {
1364 imap_starttls(num_parms, parms);
1367 else if (!CC->logged_in) {
1368 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1371 /* The commans below require a logged-in state */
1373 else if (!strcasecmp(parms[1], "SELECT")) {
1374 imap_select(num_parms, parms);
1377 else if (!strcasecmp(parms[1], "EXAMINE")) {
1378 imap_select(num_parms, parms);
1381 else if (!strcasecmp(parms[1], "LSUB")) {
1382 imap_lsub(num_parms, parms);
1385 else if (!strcasecmp(parms[1], "LIST")) {
1386 imap_list(num_parms, parms);
1389 else if (!strcasecmp(parms[1], "CREATE")) {
1390 imap_create(num_parms, parms);
1393 else if (!strcasecmp(parms[1], "DELETE")) {
1394 imap_delete(num_parms, parms);
1397 else if (!strcasecmp(parms[1], "RENAME")) {
1398 imap_rename(num_parms, parms);
1401 else if (!strcasecmp(parms[1], "STATUS")) {
1402 imap_status(num_parms, parms);
1405 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1406 imap_subscribe(num_parms, parms);
1409 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1410 imap_unsubscribe(num_parms, parms);
1413 else if (!strcasecmp(parms[1], "APPEND")) {
1414 imap_append(num_parms, parms);
1417 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1418 imap_namespace(num_parms, parms);
1421 else if (IMAP->selected == 0) {
1422 cprintf("%s BAD no folder selected\r\n", parms[0]);
1425 /* The commands below require the SELECT state on a mailbox */
1427 else if (!strcasecmp(parms[1], "FETCH")) {
1428 imap_fetch(num_parms, parms);
1431 else if ((!strcasecmp(parms[1], "UID"))
1432 && (!strcasecmp(parms[2], "FETCH"))) {
1433 imap_uidfetch(num_parms, parms);
1436 else if (!strcasecmp(parms[1], "SEARCH")) {
1437 imap_search(num_parms, parms);
1440 else if ((!strcasecmp(parms[1], "UID"))
1441 && (!strcasecmp(parms[2], "SEARCH"))) {
1442 imap_uidsearch(num_parms, parms);
1445 else if (!strcasecmp(parms[1], "STORE")) {
1446 imap_store(num_parms, parms);
1449 else if ((!strcasecmp(parms[1], "UID"))
1450 && (!strcasecmp(parms[2], "STORE"))) {
1451 imap_uidstore(num_parms, parms);
1454 else if (!strcasecmp(parms[1], "COPY")) {
1455 imap_copy(num_parms, parms);
1458 else if ((!strcasecmp(parms[1], "UID"))
1459 && (!strcasecmp(parms[2], "COPY"))) {
1460 imap_uidcopy(num_parms, parms);
1463 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1464 imap_expunge(num_parms, parms);
1467 else if (!strcasecmp(parms[1], "CLOSE")) {
1468 imap_close(num_parms, parms);
1471 /* End of commands. If we get here, the command is either invalid
1476 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1479 /* If the client transmitted a message we can free it now */
1480 imap_free_transmitted_message();
1486 * This function is called to register the IMAP extension with Citadel.
1488 char *serv_imap_init(void)
1490 CtdlRegisterServiceHook(config.c_imap_port,
1491 NULL, imap_greeting, imap_command_loop, NULL);
1493 CtdlRegisterServiceHook(config.c_imaps_port,
1494 NULL, imaps_greeting, imap_command_loop, NULL);
1496 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);