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 memcpy(msglist, cdbfr->ptr, cdbfr->len);
221 num_msgs = cdbfr->len / sizeof(long);
228 * Check to see if any of the messages we know about have been expunged
230 if (IMAP->num_msgs > 0)
231 for (i = 0; i < IMAP->num_msgs; ++i) {
233 message_still_exists = 0;
235 for (j = 0; j < num_msgs; ++j) {
236 if (msglist[j] == IMAP->msgids[i]) {
237 message_still_exists = 1;
241 if (message_still_exists == 0) {
242 cprintf("* %d EXPUNGE\r\n", i + 1);
244 /* Here's some nice stupid nonsense. When a message
245 * is expunged, we have to slide all the existing
246 * messages up in the message array.
249 memcpy(&IMAP->msgids[i],
250 &IMAP->msgids[i + 1],
252 (IMAP->num_msgs - i)));
253 memcpy(&IMAP->flags[i],
256 (IMAP->num_msgs - i)));
264 * Remember how many messages were here before we re-scanned.
266 original_num_msgs = IMAP->num_msgs;
267 if (IMAP->num_msgs > 0) {
268 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
270 original_highest = 0L;
274 * Now peruse the room for *new* messages only.
277 for (j = 0; j < num_msgs; ++j) {
278 if (msglist[j] > original_highest) {
279 imap_add_single_msgid(msglist[j], NULL);
282 imap_set_seen_flags();
285 * If new messages have arrived, tell the client about them.
287 if (IMAP->num_msgs > original_num_msgs) {
289 for (j = 0; j < num_msgs; ++j) {
290 if (IMAP->flags[j] & IMAP_RECENT) {
295 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
296 cprintf("* %d RECENT\r\n", num_recent);
310 * This cleanup function blows away the temporary memory and files used by
313 void imap_cleanup_function(void)
316 /* Don't do this stuff if this is not a IMAP session! */
317 if (CC->h_command_function != imap_command_loop)
320 /* If there is a mailbox selected, auto-expunge it. */
321 if (IMAP->selected) {
325 lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
327 imap_free_transmitted_message();
329 if (IMAP->cached_fetch != NULL) {
330 fclose(IMAP->cached_fetch);
331 IMAP->cached_fetch = NULL;
332 IMAP->cached_msgnum = (-1);
335 if (IMAP->cached_body != NULL) {
336 fclose(IMAP->cached_body);
337 IMAP->cached_body = NULL;
338 IMAP->cached_bodymsgnum = (-1);
341 lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
347 * Here's where our IMAP session begins its happy day.
349 void imap_greeting(void)
352 strcpy(CC->cs_clientname, "IMAP session");
353 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
354 IMAP->authstate = imap_as_normal;
355 IMAP->cached_fetch = NULL;
356 IMAP->cached_msgnum = (-1);
358 cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
364 * implements the LOGIN command (ordinary username/password login)
366 void imap_login(int num_parms, char *parms[])
368 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
369 if (CtdlTryPassword(parms[3]) == pass_ok) {
370 cprintf("%s OK login successful\r\n", parms[0]);
375 cprintf("%s BAD Login incorrect\r\n", parms[0]);
380 * Implements the AUTHENTICATE command
382 void imap_authenticate(int num_parms, char *parms[])
386 if (num_parms != 3) {
387 cprintf("%s BAD incorrect number of parameters\r\n",
393 cprintf("%s BAD Already logged in.\r\n", parms[0]);
397 if (!strcasecmp(parms[2], "LOGIN")) {
398 CtdlEncodeBase64(buf, "Username:", 9);
399 cprintf("+ %s\r\n", buf);
400 IMAP->authstate = imap_as_expecting_username;
401 strcpy(IMAP->authseq, parms[0]);
406 cprintf("%s NO AUTHENTICATE %s failed\r\n",
411 void imap_auth_login_user(char *cmd)
415 CtdlDecodeBase64(buf, cmd, SIZ);
416 CtdlLoginExistingUser(buf);
417 CtdlEncodeBase64(buf, "Password:", 9);
418 cprintf("+ %s\r\n", buf);
419 IMAP->authstate = imap_as_expecting_password;
423 void imap_auth_login_pass(char *cmd)
427 CtdlDecodeBase64(buf, cmd, SIZ);
428 if (CtdlTryPassword(buf) == pass_ok) {
429 cprintf("%s OK authentication succeeded\r\n",
432 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
434 IMAP->authstate = imap_as_normal;
440 * implements the CAPABILITY command
442 void imap_capability(int num_parms, char *parms[])
444 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
447 cprintf(" STARTTLS");
451 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
457 * implements the STARTTLS command (Citadel API version)
460 void imap_starttls(int num_parms, char *parms[])
462 char ok_response[SIZ];
463 char nosup_response[SIZ];
464 char error_response[SIZ];
467 "%s OK begin TLS negotiation now\r\n",
469 sprintf(nosup_response,
470 "%s NO TLS not supported here\r\n",
472 sprintf(error_response,
473 "%s BAD Internal error\r\n",
475 CtdlStartTLS(ok_response, nosup_response, error_response);
481 * implements the SELECT command
483 void imap_select(int num_parms, char *parms[])
486 char augmented_roomname[ROOMNAMELEN];
490 struct ctdlroom QRscratch;
496 /* Convert the supplied folder name to a roomname */
497 i = imap_roomname(towhere, sizeof towhere, parms[2]);
499 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
503 floornum = (i & 0x00ff);
504 roomflags = (i & 0xff00);
506 /* First try a regular match */
507 c = getroom(&QRscratch, towhere);
509 /* Then try a mailbox name match */
511 MailboxName(augmented_roomname, sizeof augmented_roomname,
513 c = getroom(&QRscratch, augmented_roomname);
515 strcpy(towhere, augmented_roomname);
518 /* If the room exists, check security/access */
520 /* See if there is an existing user/room relationship */
521 ra = CtdlRoomAccess(&QRscratch, &CC->user);
523 /* normal clients have to pass through security */
529 /* Fail here if no such room */
531 cprintf("%s NO ... no such room, or access denied\r\n",
536 /* If we already had some other folder selected, auto-expunge it */
540 * usergoto() formally takes us to the desired room, happily returning
541 * the number of messages and number of new messages.
543 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
544 usergoto(NULL, 0, 0, &msgs, &new);
547 if (!strcasecmp(parms[1], "EXAMINE")) {
555 cprintf("* %d EXISTS\r\n", msgs);
556 cprintf("* %d RECENT\r\n", new);
558 /* Note that \Deleted is a valid flag, but not a permanent flag,
559 * because we don't maintain its state across sessions. Citadel
560 * automatically expunges mailboxes when they are de-selected.
562 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
563 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
564 "permanent flags\r\n");
566 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
567 cprintf("%s OK [%s] %s completed\r\n",
569 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
575 * does the real work for expunge
577 int imap_do_expunge(void)
580 int num_expunged = 0;
582 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
583 if (IMAP->selected == 0) {
587 if (IMAP->num_msgs > 0)
588 for (i = 0; i < IMAP->num_msgs; ++i) {
589 if (IMAP->flags[i] & IMAP_DELETED) {
590 CtdlDeleteMessages(CC->room.QRname,
591 IMAP->msgids[i], "");
596 if (num_expunged > 0) {
597 imap_rescan_msgids();
600 lprintf(9, "Expunged %d messages.\n", num_expunged);
601 return (num_expunged);
606 * implements the EXPUNGE command syntax
608 void imap_expunge(int num_parms, char *parms[])
610 int num_expunged = 0;
612 num_expunged = imap_do_expunge();
613 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
618 * implements the CLOSE command
620 void imap_close(int num_parms, char *parms[])
623 /* Yes, we always expunge on close. */
624 if (IMAP->selected) {
631 cprintf("%s OK CLOSE completed\r\n", parms[0]);
636 * Implements the NAMESPACE command.
638 void imap_namespace(int num_parms, char *parms[])
645 cprintf("* NAMESPACE ");
647 /* All personal folders are subordinate to INBOX. */
648 cprintf("((\"INBOX/\" \"/\")) ");
650 /* Other users' folders ... coming soon! FIXME */
653 /* Show all floors as shared namespaces. Neato! */
655 for (i = 0; i < MAXFLOORS; ++i) {
657 if (fl->f_flags & F_INUSE) {
658 if (floors > 0) cprintf(" ");
660 sprintf(buf, "%s/", fl->f_name);
668 /* Wind it up with a newline and a completion message. */
670 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
676 * Used by LIST and LSUB to show the floors in the listing
678 void imap_list_floors(char *cmd, char *pattern)
683 for (i = 0; i < MAXFLOORS; ++i) {
685 if (fl->f_flags & F_INUSE) {
686 if (imap_mailbox_matches_pattern
687 (pattern, fl->f_name)) {
688 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
689 imap_strout(fl->f_name);
699 * Back end for imap_lsub()
701 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
702 * may or may not be the desired behavior in the future.
704 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
710 pattern = (char *) data;
712 /* Only list rooms to which the user has access!! */
713 ra = CtdlRoomAccess(qrbuf, &CC->user);
715 imap_mailboxname(buf, sizeof buf, qrbuf);
716 if (imap_mailbox_matches_pattern(pattern, buf)) {
717 cprintf("* LSUB () \"/\" ");
726 * Implements the LSUB command
728 void imap_lsub(int num_parms, char *parms[])
732 cprintf("%s BAD arguments invalid\r\n", parms[0]);
735 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
737 if (strlen(parms[3]) == 0) {
738 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
742 imap_list_floors("LSUB", pattern);
743 ForEachRoom(imap_lsub_listroom, pattern);
746 cprintf("%s OK LSUB completed\r\n", parms[0]);
752 * Back end for imap_list()
754 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
760 pattern = (char *) data;
762 /* Only list rooms to which the user has access!! */
763 ra = CtdlRoomAccess(qrbuf, &CC->user);
765 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
766 imap_mailboxname(buf, sizeof buf, qrbuf);
767 if (imap_mailbox_matches_pattern(pattern, buf)) {
768 cprintf("* LIST () \"/\" ");
777 * Implements the LIST command
779 void imap_list(int num_parms, char *parms[])
783 cprintf("%s BAD arguments invalid\r\n", parms[0]);
786 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
788 if (strlen(parms[3]) == 0) {
789 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
793 imap_list_floors("LIST", pattern);
794 ForEachRoom(imap_list_listroom, pattern);
797 cprintf("%s OK LIST completed\r\n", parms[0]);
803 * Implements the CREATE command
806 void imap_create(int num_parms, char *parms[])
809 char roomname[ROOMNAMELEN];
814 if (strchr(parms[2], '\\') != NULL) {
815 cprintf("%s NO Invalid character in folder name\r\n",
817 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
821 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
823 cprintf("%s NO Invalid mailbox name or location\r\n",
825 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
828 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
829 flags = (ret & 0xff00); /* upper 8 bits = flags */
831 if (flags & IR_MAILBOX) {
832 if (strncasecmp(parms[2], "INBOX/", 6)) {
833 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
834 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
839 if (flags & IR_MAILBOX) {
840 newroomtype = 4; /* private mailbox */
842 newroomtype = 0; /* public folder */
845 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
846 roomname, floornum, newroomtype);
848 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, VIEW_BBS);
851 ("%s NO Mailbox already exists, or create failed\r\n",
854 cprintf("%s OK CREATE completed\r\n", parms[0]);
856 lprintf(CTDL_DEBUG, "imap_create() completed\n");
861 * Locate a room by its IMAP folder name, and check access to it
863 int imap_grabroom(char *returned_roomname, char *foldername)
866 char augmented_roomname[ROOMNAMELEN];
867 char roomname[ROOMNAMELEN];
869 struct ctdlroom QRscratch;
873 ret = imap_roomname(roomname, sizeof roomname, foldername);
878 /* First try a regular match */
879 c = getroom(&QRscratch, roomname);
881 /* Then try a mailbox name match */
883 MailboxName(augmented_roomname, sizeof augmented_roomname,
884 &CC->user, roomname);
885 c = getroom(&QRscratch, augmented_roomname);
887 strcpy(roomname, augmented_roomname);
890 /* If the room exists, check security/access */
892 /* See if there is an existing user/room relationship */
893 ra = CtdlRoomAccess(&QRscratch, &CC->user);
895 /* normal clients have to pass through security */
901 /* Fail here if no such room */
903 strcpy(returned_roomname, "");
906 strcpy(returned_roomname, QRscratch.QRname);
913 * Implements the STATUS command (sort of)
916 void imap_status(int num_parms, char *parms[])
919 char roomname[ROOMNAMELEN];
921 char savedroom[ROOMNAMELEN];
924 ret = imap_grabroom(roomname, parms[2]);
927 ("%s NO Invalid mailbox name or location, or access denied\r\n",
933 * usergoto() formally takes us to the desired room, happily returning
934 * the number of messages and number of new messages. (If another
935 * folder is selected, save its name so we can return there!!!!!)
937 if (IMAP->selected) {
938 strcpy(savedroom, CC->room.QRname);
940 usergoto(roomname, 0, 0, &msgs, &new);
943 * Tell the client what it wants to know. In fact, tell it *more* than
944 * it wants to know. We happily IGnore the supplied status data item
945 * names and simply spew all possible data items. It's far easier to
946 * code and probably saves us some processing time too.
948 imap_mailboxname(buf, sizeof buf, &CC->room);
949 cprintf("* STATUS ");
951 cprintf(" (MESSAGES %d ", msgs);
952 cprintf("RECENT %d ", new); /* Initially, new==recent */
953 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
954 cprintf("UNSEEN %d)\r\n", new);
957 * If another folder is selected, go back to that room so we can resume
958 * our happy day without violent explosions.
960 if (IMAP->selected) {
961 usergoto(savedroom, 0, 0, &msgs, &new);
965 * Oooh, look, we're done!
967 cprintf("%s OK STATUS completed\r\n", parms[0]);
973 * Implements the SUBSCRIBE command
976 void imap_subscribe(int num_parms, char *parms[])
979 char roomname[ROOMNAMELEN];
980 char savedroom[ROOMNAMELEN];
983 ret = imap_grabroom(roomname, parms[2]);
986 ("%s NO Invalid mailbox name or location, or access denied\r\n",
992 * usergoto() formally takes us to the desired room, which has the side
993 * effect of marking the room as not-zapped ... exactly the effect
996 if (IMAP->selected) {
997 strcpy(savedroom, CC->room.QRname);
999 usergoto(roomname, 0, 0, &msgs, &new);
1002 * If another folder is selected, go back to that room so we can resume
1003 * our happy day without violent explosions.
1005 if (IMAP->selected) {
1006 usergoto(savedroom, 0, 0, &msgs, &new);
1009 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1014 * Implements the UNSUBSCRIBE command
1017 void imap_unsubscribe(int num_parms, char *parms[])
1020 char roomname[ROOMNAMELEN];
1021 char savedroom[ROOMNAMELEN];
1024 ret = imap_grabroom(roomname, parms[2]);
1027 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1033 * usergoto() formally takes us to the desired room.
1035 if (IMAP->selected) {
1036 strcpy(savedroom, CC->room.QRname);
1038 usergoto(roomname, 0, 0, &msgs, &new);
1041 * Now make the API call to zap the room
1043 if (CtdlForgetThisRoom() == 0) {
1044 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1047 ("%s NO You may not unsubscribe from this folder.\r\n",
1052 * If another folder is selected, go back to that room so we can resume
1053 * our happy day without violent explosions.
1055 if (IMAP->selected) {
1056 usergoto(savedroom, 0, 0, &msgs, &new);
1063 * Implements the DELETE command
1066 void imap_delete(int num_parms, char *parms[])
1069 char roomname[ROOMNAMELEN];
1070 char savedroom[ROOMNAMELEN];
1073 ret = imap_grabroom(roomname, parms[2]);
1075 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1081 * usergoto() formally takes us to the desired room, happily returning
1082 * the number of messages and number of new messages. (If another
1083 * folder is selected, save its name so we can return there!!!!!)
1085 if (IMAP->selected) {
1086 strcpy(savedroom, CC->room.QRname);
1088 usergoto(roomname, 0, 0, &msgs, &new);
1091 * Now delete the room.
1093 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1094 cprintf("%s OK DELETE completed\r\n", parms[0]);
1095 delete_room(&CC->room);
1097 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1101 * If another folder is selected, go back to that room so we can resume
1102 * our happy day without violent explosions.
1104 if (IMAP->selected) {
1105 usergoto(savedroom, 0, 0, &msgs, &new);
1111 * Back end function for imap_rename()
1113 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1115 char foldername[SIZ];
1116 char newfoldername[SIZ];
1117 char newroomname[ROOMNAMELEN];
1119 struct irl *irlp = NULL; /* scratch pointer */
1120 struct irlparms *irlparms;
1122 irlparms = (struct irlparms *) data;
1123 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1125 /* Rename subfolders */
1126 if ((!strncasecmp(foldername, irlparms->oldname,
1127 strlen(irlparms->oldname))
1128 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1130 sprintf(newfoldername, "%s/%s",
1132 &foldername[strlen(irlparms->oldname) + 1]
1135 newfloor = imap_roomname(newroomname,
1137 newfoldername) & 0xFF;
1139 irlp = (struct irl *) malloc(sizeof(struct irl));
1140 strcpy(irlp->irl_newroom, newroomname);
1141 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1142 irlp->irl_newfloor = newfloor;
1143 irlp->next = *(irlparms->irl);
1144 *(irlparms->irl) = irlp;
1150 * Implements the RENAME command
1153 void imap_rename(int num_parms, char *parms[])
1155 char old_room[ROOMNAMELEN];
1156 char new_room[ROOMNAMELEN];
1160 struct irl *irl = NULL; /* the list */
1161 struct irl *irlp = NULL; /* scratch pointer */
1162 struct irlparms irlparms;
1164 if (strchr(parms[3], '\\') != NULL) {
1165 cprintf("%s NO Invalid character in folder name\r\n",
1170 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1171 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1172 new_floor = (newr & 0xFF);
1174 r = CtdlRenameRoom(old_room, new_room, new_floor);
1176 if (r == crr_room_not_found) {
1177 cprintf("%s NO Could not locate this folder\r\n",
1181 if (r == crr_already_exists) {
1182 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1185 if (r == crr_noneditable) {
1186 cprintf("%s This folder is not editable.\r\n", parms[0]);
1189 if (r == crr_invalid_floor) {
1190 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1193 if (r == crr_access_denied) {
1194 cprintf("%s You do not have permission to edit "
1195 "this folder.\r\n", parms[0]);
1199 cprintf("%s NO Rename failed - undefined error %d\r\n",
1205 /* If this is the INBOX, then RFC2060 says we have to just move the
1206 * contents. In a Citadel environment it's easier to rename the room
1207 * (already did that) and create a new inbox.
1209 if (!strcasecmp(parms[2], "INBOX")) {
1210 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_BBS);
1213 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1215 irlparms.oldname = parms[2];
1216 irlparms.newname = parms[3];
1217 irlparms.irl = &irl;
1218 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1220 /* ... and now rename them. */
1221 while (irl != NULL) {
1222 r = CtdlRenameRoom(irl->irl_oldroom,
1226 /* FIXME handle error returns better */
1227 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1236 cprintf("%s OK RENAME completed\r\n", parms[0]);
1243 * Main command loop for IMAP sessions.
1245 void imap_command_loop(void)
1252 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1254 if (client_gets(cmdbuf) < 1) {
1255 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1260 lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
1261 while (strlen(cmdbuf) < 5)
1262 strcat(cmdbuf, " ");
1265 /* strip off l/t whitespace and CRLF */
1266 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1267 cmdbuf[strlen(cmdbuf) - 1] = 0;
1268 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1269 cmdbuf[strlen(cmdbuf) - 1] = 0;
1272 /* If we're in the middle of a multi-line command, handle that */
1273 if (IMAP->authstate == imap_as_expecting_username) {
1274 imap_auth_login_user(cmdbuf);
1277 if (IMAP->authstate == imap_as_expecting_password) {
1278 imap_auth_login_pass(cmdbuf);
1283 /* Ok, at this point we're in normal command mode. The first thing
1284 * we do is print any incoming pages (yeah! we really do!)
1286 imap_print_instant_messages();
1289 * Before processing the command that was just entered... if we happen
1290 * to have a folder selected, we'd like to rescan that folder for new
1291 * messages, and for deletions/changes of existing messages. This
1292 * could probably be optimized somehow, but IMAP sucks...
1294 if (IMAP->selected) {
1295 imap_rescan_msgids();
1298 /* Now for the command set. */
1300 /* Grab the tag, command, and parameters. Check syntax. */
1301 num_parms = imap_parameterize(parms, cmdbuf);
1302 if (num_parms < 2) {
1303 cprintf("BAD syntax error\r\n");
1306 /* The commands below may be executed in any state */
1308 else if ((!strcasecmp(parms[1], "NOOP"))
1309 || (!strcasecmp(parms[1], "CHECK"))) {
1310 cprintf("%s OK This command successfully did nothing.\r\n",
1314 else if (!strcasecmp(parms[1], "LOGOUT")) {
1315 if (IMAP->selected) {
1316 imap_do_expunge(); /* yes, we auto-expunge */
1318 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1319 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1325 else if (!strcasecmp(parms[1], "LOGIN")) {
1326 imap_login(num_parms, parms);
1329 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1330 imap_authenticate(num_parms, parms);
1333 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1334 imap_capability(num_parms, parms);
1337 else if (!strcasecmp(parms[1], "STARTTLS")) {
1338 imap_starttls(num_parms, parms);
1341 else if (!CC->logged_in) {
1342 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1345 /* The commans below require a logged-in state */
1347 else if (!strcasecmp(parms[1], "SELECT")) {
1348 imap_select(num_parms, parms);
1351 else if (!strcasecmp(parms[1], "EXAMINE")) {
1352 imap_select(num_parms, parms);
1355 else if (!strcasecmp(parms[1], "LSUB")) {
1356 imap_lsub(num_parms, parms);
1359 else if (!strcasecmp(parms[1], "LIST")) {
1360 imap_list(num_parms, parms);
1363 else if (!strcasecmp(parms[1], "CREATE")) {
1364 imap_create(num_parms, parms);
1367 else if (!strcasecmp(parms[1], "DELETE")) {
1368 imap_delete(num_parms, parms);
1371 else if (!strcasecmp(parms[1], "RENAME")) {
1372 imap_rename(num_parms, parms);
1375 else if (!strcasecmp(parms[1], "STATUS")) {
1376 imap_status(num_parms, parms);
1379 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1380 imap_subscribe(num_parms, parms);
1383 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1384 imap_unsubscribe(num_parms, parms);
1387 else if (!strcasecmp(parms[1], "APPEND")) {
1388 imap_append(num_parms, parms);
1391 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1392 imap_namespace(num_parms, parms);
1395 else if (IMAP->selected == 0) {
1396 cprintf("%s BAD no folder selected\r\n", parms[0]);
1399 /* The commands below require the SELECT state on a mailbox */
1401 else if (!strcasecmp(parms[1], "FETCH")) {
1402 imap_fetch(num_parms, parms);
1405 else if ((!strcasecmp(parms[1], "UID"))
1406 && (!strcasecmp(parms[2], "FETCH"))) {
1407 imap_uidfetch(num_parms, parms);
1410 else if (!strcasecmp(parms[1], "SEARCH")) {
1411 imap_search(num_parms, parms);
1414 else if ((!strcasecmp(parms[1], "UID"))
1415 && (!strcasecmp(parms[2], "SEARCH"))) {
1416 imap_uidsearch(num_parms, parms);
1419 else if (!strcasecmp(parms[1], "STORE")) {
1420 imap_store(num_parms, parms);
1423 else if ((!strcasecmp(parms[1], "UID"))
1424 && (!strcasecmp(parms[2], "STORE"))) {
1425 imap_uidstore(num_parms, parms);
1428 else if (!strcasecmp(parms[1], "COPY")) {
1429 imap_copy(num_parms, parms);
1432 else if ((!strcasecmp(parms[1], "UID"))
1433 && (!strcasecmp(parms[2], "COPY"))) {
1434 imap_uidcopy(num_parms, parms);
1437 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1438 imap_expunge(num_parms, parms);
1441 else if (!strcasecmp(parms[1], "CLOSE")) {
1442 imap_close(num_parms, parms);
1445 /* End of commands. If we get here, the command is either invalid
1450 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1453 /* If the client transmitted a message we can free it now */
1454 imap_free_transmitted_message();
1460 * This function is called to register the IMAP extension with Citadel.
1462 char *serv_imap_init(void)
1464 CtdlRegisterServiceHook(config.c_imap_port,
1465 NULL, imap_greeting, imap_command_loop, NULL);
1466 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);