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];
833 if (strchr(parms[2], '\\') != NULL) {
834 cprintf("%s NO Invalid character in folder name\r\n",
836 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
840 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
842 cprintf("%s NO Invalid mailbox name or location\r\n",
844 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
847 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
848 flags = (ret & 0xff00); /* upper 8 bits = flags */
850 if (flags & IR_MAILBOX) {
851 if (strncasecmp(parms[2], "INBOX/", 6)) {
852 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
853 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
858 if (flags & IR_MAILBOX) {
859 newroomtype = 4; /* private mailbox */
861 newroomtype = 0; /* public folder */
864 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
865 roomname, floornum, newroomtype);
867 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, VIEW_BBS);
870 ("%s NO Mailbox already exists, or create failed\r\n",
873 cprintf("%s OK CREATE completed\r\n", parms[0]);
875 lprintf(CTDL_DEBUG, "imap_create() completed\n");
880 * Locate a room by its IMAP folder name, and check access to it.
881 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
883 int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
886 char augmented_roomname[ROOMNAMELEN];
887 char roomname[ROOMNAMELEN];
889 struct ctdlroom QRscratch;
893 ret = imap_roomname(roomname, sizeof roomname, foldername);
898 /* First try a regular match */
899 c = getroom(&QRscratch, roomname);
901 /* Then try a mailbox name match */
903 MailboxName(augmented_roomname, sizeof augmented_roomname,
904 &CC->user, roomname);
905 c = getroom(&QRscratch, augmented_roomname);
907 strcpy(roomname, augmented_roomname);
910 /* If the room exists, check security/access */
912 /* See if there is an existing user/room relationship */
913 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
915 /* normal clients have to pass through security */
919 if ((zapped_ok) && (ra & UA_ZAPPED)) {
924 /* Fail here if no such room */
926 strcpy(returned_roomname, "");
929 strcpy(returned_roomname, QRscratch.QRname);
936 * Implements the STATUS command (sort of)
939 void imap_status(int num_parms, char *parms[])
942 char roomname[ROOMNAMELEN];
944 char savedroom[ROOMNAMELEN];
947 ret = imap_grabroom(roomname, parms[2], 0);
950 ("%s NO Invalid mailbox name or location, or access denied\r\n",
956 * usergoto() formally takes us to the desired room, happily returning
957 * the number of messages and number of new messages. (If another
958 * folder is selected, save its name so we can return there!!!!!)
960 if (IMAP->selected) {
961 strcpy(savedroom, CC->room.QRname);
963 usergoto(roomname, 0, 0, &msgs, &new);
966 * Tell the client what it wants to know. In fact, tell it *more* than
967 * it wants to know. We happily IGnore the supplied status data item
968 * names and simply spew all possible data items. It's far easier to
969 * code and probably saves us some processing time too.
971 imap_mailboxname(buf, sizeof buf, &CC->room);
972 cprintf("* STATUS ");
974 cprintf(" (MESSAGES %d ", msgs);
975 cprintf("RECENT %d ", new); /* Initially, new==recent */
976 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
977 cprintf("UNSEEN %d)\r\n", new);
980 * If another folder is selected, go back to that room so we can resume
981 * our happy day without violent explosions.
983 if (IMAP->selected) {
984 usergoto(savedroom, 0, 0, &msgs, &new);
988 * Oooh, look, we're done!
990 cprintf("%s OK STATUS completed\r\n", parms[0]);
996 * Implements the SUBSCRIBE command
999 void imap_subscribe(int num_parms, char *parms[])
1002 char roomname[ROOMNAMELEN];
1003 char savedroom[ROOMNAMELEN];
1006 ret = imap_grabroom(roomname, parms[2], 1);
1009 "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1017 * usergoto() formally takes us to the desired room, which has the side
1018 * effect of marking the room as not-zapped ... exactly the effect
1019 * we're looking for.
1021 if (IMAP->selected) {
1022 strcpy(savedroom, CC->room.QRname);
1024 usergoto(roomname, 0, 0, &msgs, &new);
1027 * If another folder is selected, go back to that room so we can resume
1028 * our happy day without violent explosions.
1030 if (IMAP->selected) {
1031 usergoto(savedroom, 0, 0, &msgs, &new);
1034 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1039 * Implements the UNSUBSCRIBE command
1042 void imap_unsubscribe(int num_parms, char *parms[])
1045 char roomname[ROOMNAMELEN];
1046 char savedroom[ROOMNAMELEN];
1049 ret = imap_grabroom(roomname, parms[2], 0);
1052 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1058 * usergoto() formally takes us to the desired room.
1060 if (IMAP->selected) {
1061 strcpy(savedroom, CC->room.QRname);
1063 usergoto(roomname, 0, 0, &msgs, &new);
1066 * Now make the API call to zap the room
1068 if (CtdlForgetThisRoom() == 0) {
1069 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1072 ("%s NO You may not unsubscribe from this folder.\r\n",
1077 * If another folder is selected, go back to that room so we can resume
1078 * our happy day without violent explosions.
1080 if (IMAP->selected) {
1081 usergoto(savedroom, 0, 0, &msgs, &new);
1088 * Implements the DELETE command
1091 void imap_delete(int num_parms, char *parms[])
1094 char roomname[ROOMNAMELEN];
1095 char savedroom[ROOMNAMELEN];
1098 ret = imap_grabroom(roomname, parms[2], 0);
1100 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1106 * usergoto() formally takes us to the desired room, happily returning
1107 * the number of messages and number of new messages. (If another
1108 * folder is selected, save its name so we can return there!!!!!)
1110 if (IMAP->selected) {
1111 strcpy(savedroom, CC->room.QRname);
1113 usergoto(roomname, 0, 0, &msgs, &new);
1116 * Now delete the room.
1118 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1119 cprintf("%s OK DELETE completed\r\n", parms[0]);
1120 delete_room(&CC->room);
1122 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1126 * If another folder is selected, go back to that room so we can resume
1127 * our happy day without violent explosions.
1129 if (IMAP->selected) {
1130 usergoto(savedroom, 0, 0, &msgs, &new);
1136 * Back end function for imap_rename()
1138 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1140 char foldername[SIZ];
1141 char newfoldername[SIZ];
1142 char newroomname[ROOMNAMELEN];
1144 struct irl *irlp = NULL; /* scratch pointer */
1145 struct irlparms *irlparms;
1147 irlparms = (struct irlparms *) data;
1148 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1150 /* Rename subfolders */
1151 if ((!strncasecmp(foldername, irlparms->oldname,
1152 strlen(irlparms->oldname))
1153 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1155 sprintf(newfoldername, "%s/%s",
1157 &foldername[strlen(irlparms->oldname) + 1]
1160 newfloor = imap_roomname(newroomname,
1162 newfoldername) & 0xFF;
1164 irlp = (struct irl *) malloc(sizeof(struct irl));
1165 strcpy(irlp->irl_newroom, newroomname);
1166 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1167 irlp->irl_newfloor = newfloor;
1168 irlp->next = *(irlparms->irl);
1169 *(irlparms->irl) = irlp;
1175 * Implements the RENAME command
1178 void imap_rename(int num_parms, char *parms[])
1180 char old_room[ROOMNAMELEN];
1181 char new_room[ROOMNAMELEN];
1185 struct irl *irl = NULL; /* the list */
1186 struct irl *irlp = NULL; /* scratch pointer */
1187 struct irlparms irlparms;
1189 if (strchr(parms[3], '\\') != NULL) {
1190 cprintf("%s NO Invalid character in folder name\r\n",
1195 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1196 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1197 new_floor = (newr & 0xFF);
1199 r = CtdlRenameRoom(old_room, new_room, new_floor);
1201 if (r == crr_room_not_found) {
1202 cprintf("%s NO Could not locate this folder\r\n",
1206 if (r == crr_already_exists) {
1207 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1210 if (r == crr_noneditable) {
1211 cprintf("%s This folder is not editable.\r\n", parms[0]);
1214 if (r == crr_invalid_floor) {
1215 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1218 if (r == crr_access_denied) {
1219 cprintf("%s You do not have permission to edit "
1220 "this folder.\r\n", parms[0]);
1224 cprintf("%s NO Rename failed - undefined error %d\r\n",
1230 /* If this is the INBOX, then RFC2060 says we have to just move the
1231 * contents. In a Citadel environment it's easier to rename the room
1232 * (already did that) and create a new inbox.
1234 if (!strcasecmp(parms[2], "INBOX")) {
1235 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_BBS);
1238 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1240 irlparms.oldname = parms[2];
1241 irlparms.newname = parms[3];
1242 irlparms.irl = &irl;
1243 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1245 /* ... and now rename them. */
1246 while (irl != NULL) {
1247 r = CtdlRenameRoom(irl->irl_oldroom,
1251 /* FIXME handle error returns better */
1252 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1261 cprintf("%s OK RENAME completed\r\n", parms[0]);
1268 * Main command loop for IMAP sessions.
1270 void imap_command_loop(void)
1276 CC->lastcmd = time(NULL);
1277 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1279 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1280 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1285 lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
1286 while (strlen(cmdbuf) < 5)
1287 strcat(cmdbuf, " ");
1289 /* strip off l/t whitespace and CRLF */
1290 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1291 cmdbuf[strlen(cmdbuf) - 1] = 0;
1292 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1293 cmdbuf[strlen(cmdbuf) - 1] = 0;
1296 /* If we're in the middle of a multi-line command, handle that */
1297 if (IMAP->authstate == imap_as_expecting_username) {
1298 imap_auth_login_user(cmdbuf);
1301 if (IMAP->authstate == imap_as_expecting_password) {
1302 imap_auth_login_pass(cmdbuf);
1306 /* Ok, at this point we're in normal command mode. The first thing
1307 * we do is print any incoming pages (yeah! we really do!)
1309 imap_print_instant_messages();
1312 * Before processing the command that was just entered... if we happen
1313 * to have a folder selected, we'd like to rescan that folder for new
1314 * messages, and for deletions/changes of existing messages. This
1315 * could probably be optimized somehow, but IMAP sucks...
1317 if (IMAP->selected) {
1318 imap_rescan_msgids();
1321 /* Now for the command set. */
1323 /* Grab the tag, command, and parameters. Check syntax. */
1324 num_parms = imap_parameterize(parms, cmdbuf);
1325 if (num_parms < 2) {
1326 cprintf("BAD syntax error\r\n");
1329 /* The commands below may be executed in any state */
1331 else if ((!strcasecmp(parms[1], "NOOP"))
1332 || (!strcasecmp(parms[1], "CHECK"))) {
1333 cprintf("%s OK This command successfully did nothing.\r\n",
1337 else if (!strcasecmp(parms[1], "LOGOUT")) {
1338 if (IMAP->selected) {
1339 imap_do_expunge(); /* yes, we auto-expunge */
1341 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1342 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1348 else if (!strcasecmp(parms[1], "LOGIN")) {
1349 imap_login(num_parms, parms);
1352 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1353 imap_authenticate(num_parms, parms);
1356 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1357 imap_capability(num_parms, parms);
1360 else if (!strcasecmp(parms[1], "STARTTLS")) {
1361 imap_starttls(num_parms, parms);
1364 else if (!CC->logged_in) {
1365 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1368 /* The commans below require a logged-in state */
1370 else if (!strcasecmp(parms[1], "SELECT")) {
1371 imap_select(num_parms, parms);
1374 else if (!strcasecmp(parms[1], "EXAMINE")) {
1375 imap_select(num_parms, parms);
1378 else if (!strcasecmp(parms[1], "LSUB")) {
1379 imap_lsub(num_parms, parms);
1382 else if (!strcasecmp(parms[1], "LIST")) {
1383 imap_list(num_parms, parms);
1386 else if (!strcasecmp(parms[1], "CREATE")) {
1387 imap_create(num_parms, parms);
1390 else if (!strcasecmp(parms[1], "DELETE")) {
1391 imap_delete(num_parms, parms);
1394 else if (!strcasecmp(parms[1], "RENAME")) {
1395 imap_rename(num_parms, parms);
1398 else if (!strcasecmp(parms[1], "STATUS")) {
1399 imap_status(num_parms, parms);
1402 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1403 imap_subscribe(num_parms, parms);
1406 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1407 imap_unsubscribe(num_parms, parms);
1410 else if (!strcasecmp(parms[1], "APPEND")) {
1411 imap_append(num_parms, parms);
1414 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1415 imap_namespace(num_parms, parms);
1418 else if (IMAP->selected == 0) {
1419 cprintf("%s BAD no folder selected\r\n", parms[0]);
1422 /* The commands below require the SELECT state on a mailbox */
1424 else if (!strcasecmp(parms[1], "FETCH")) {
1425 imap_fetch(num_parms, parms);
1428 else if ((!strcasecmp(parms[1], "UID"))
1429 && (!strcasecmp(parms[2], "FETCH"))) {
1430 imap_uidfetch(num_parms, parms);
1433 else if (!strcasecmp(parms[1], "SEARCH")) {
1434 imap_search(num_parms, parms);
1437 else if ((!strcasecmp(parms[1], "UID"))
1438 && (!strcasecmp(parms[2], "SEARCH"))) {
1439 imap_uidsearch(num_parms, parms);
1442 else if (!strcasecmp(parms[1], "STORE")) {
1443 imap_store(num_parms, parms);
1446 else if ((!strcasecmp(parms[1], "UID"))
1447 && (!strcasecmp(parms[2], "STORE"))) {
1448 imap_uidstore(num_parms, parms);
1451 else if (!strcasecmp(parms[1], "COPY")) {
1452 imap_copy(num_parms, parms);
1455 else if ((!strcasecmp(parms[1], "UID"))
1456 && (!strcasecmp(parms[2], "COPY"))) {
1457 imap_uidcopy(num_parms, parms);
1460 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1461 imap_expunge(num_parms, parms);
1464 else if (!strcasecmp(parms[1], "CLOSE")) {
1465 imap_close(num_parms, parms);
1468 /* End of commands. If we get here, the command is either invalid
1473 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1476 /* If the client transmitted a message we can free it now */
1477 imap_free_transmitted_message();
1483 * This function is called to register the IMAP extension with Citadel.
1485 char *serv_imap_init(void)
1487 CtdlRegisterServiceHook(config.c_imap_port,
1488 NULL, imap_greeting, imap_command_loop, NULL);
1490 CtdlRegisterServiceHook(config.c_imaps_port,
1491 NULL, imaps_greeting, imap_command_loop, NULL);
1493 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);