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 (vbuf.v_answered, IMAP->msgids[i])) {
124 IMAP->flags[i] |= IMAP_ANSWERED;
134 * Back end for imap_load_msgids()
136 * Optimization: instead of calling realloc() to add each message, we
137 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
138 * allows the mapping to proceed much faster.
140 void imap_add_single_msgid(long msgnum, void *userdata)
143 IMAP->num_msgs = IMAP->num_msgs + 1;
144 if (IMAP->msgids == NULL) {
145 IMAP->msgids = malloc(IMAP->num_msgs * sizeof(long)
146 * REALLOC_INCREMENT);
147 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
148 IMAP->msgids = realloc(IMAP->msgids,
150 REALLOC_INCREMENT) * sizeof(long));
152 if (IMAP->flags == NULL) {
153 IMAP->flags = malloc(IMAP->num_msgs * sizeof(long)
154 * REALLOC_INCREMENT);
155 } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
156 IMAP->flags = realloc(IMAP->flags,
158 REALLOC_INCREMENT) * sizeof(long));
160 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
161 IMAP->flags[IMAP->num_msgs - 1] = 0;
167 * Set up a message ID map for the current room (folder)
169 void imap_load_msgids(void)
172 if (IMAP->selected == 0) {
174 "imap_load_msgids() can't run; no room selected\n");
178 imap_free_msgids(); /* If there was already a map, free it */
180 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
181 imap_add_single_msgid, NULL);
183 imap_set_seen_flags();
184 lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
190 * Re-scan the selected room (folder) and see if it's been changed at all
192 void imap_rescan_msgids(void)
195 int original_num_msgs = 0;
196 long original_highest = 0L;
198 int message_still_exists;
199 struct cdbdata *cdbfr;
200 long *msglist = NULL;
204 if (IMAP->selected == 0) {
206 "imap_load_msgids() can't run; no room selected\n");
210 /* Load the *current* message list from disk, so we can compare it
211 * to what we have in memory.
213 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
215 msglist = malloc(cdbfr->len);
216 memcpy(msglist, cdbfr->ptr, cdbfr->len);
217 num_msgs = cdbfr->len / sizeof(long);
224 * Check to see if any of the messages we know about have been expunged
226 if (IMAP->num_msgs > 0)
227 for (i = 0; i < IMAP->num_msgs; ++i) {
229 message_still_exists = 0;
231 for (j = 0; j < num_msgs; ++j) {
232 if (msglist[j] == IMAP->msgids[i]) {
233 message_still_exists = 1;
237 if (message_still_exists == 0) {
238 cprintf("* %d EXPUNGE\r\n", i + 1);
240 /* Here's some nice stupid nonsense. When a message
241 * is expunged, we have to slide all the existing
242 * messages up in the message array.
245 memcpy(&IMAP->msgids[i],
246 &IMAP->msgids[i + 1],
248 (IMAP->num_msgs - i)));
249 memcpy(&IMAP->flags[i],
252 (IMAP->num_msgs - i)));
260 * Remember how many messages were here before we re-scanned.
262 original_num_msgs = IMAP->num_msgs;
263 if (IMAP->num_msgs > 0) {
264 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
266 original_highest = 0L;
270 * Now peruse the room for *new* messages only.
273 for (j = 0; j < num_msgs; ++j) {
274 if (msglist[j] > original_highest) {
275 imap_add_single_msgid(msglist[j], NULL);
278 imap_set_seen_flags();
281 * If new messages have arrived, tell the client about them.
283 if (IMAP->num_msgs > original_num_msgs) {
284 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
298 * This cleanup function blows away the temporary memory and files used by
301 void imap_cleanup_function(void)
304 /* Don't do this stuff if this is not a IMAP session! */
305 if (CC->h_command_function != imap_command_loop)
308 /* If there is a mailbox selected, auto-expunge it. */
309 if (IMAP->selected) {
313 lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
315 imap_free_transmitted_message();
317 if (IMAP->cached_fetch != NULL) {
318 fclose(IMAP->cached_fetch);
319 IMAP->cached_fetch = NULL;
320 IMAP->cached_msgnum = (-1);
323 if (IMAP->cached_body != NULL) {
324 fclose(IMAP->cached_body);
325 IMAP->cached_body = NULL;
326 IMAP->cached_bodymsgnum = (-1);
329 lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
335 * Here's where our IMAP session begins its happy day.
337 void imap_greeting(void)
340 strcpy(CC->cs_clientname, "IMAP session");
341 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
342 IMAP->authstate = imap_as_normal;
343 IMAP->cached_fetch = NULL;
344 IMAP->cached_msgnum = (-1);
346 cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
352 * implements the LOGIN command (ordinary username/password login)
354 void imap_login(int num_parms, char *parms[])
356 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
357 if (CtdlTryPassword(parms[3]) == pass_ok) {
358 cprintf("%s OK login successful\r\n", parms[0]);
363 cprintf("%s BAD Login incorrect\r\n", parms[0]);
368 * Implements the AUTHENTICATE command
370 void imap_authenticate(int num_parms, char *parms[])
374 if (num_parms != 3) {
375 cprintf("%s BAD incorrect number of parameters\r\n",
381 cprintf("%s BAD Already logged in.\r\n", parms[0]);
385 if (!strcasecmp(parms[2], "LOGIN")) {
386 CtdlEncodeBase64(buf, "Username:", 9);
387 cprintf("+ %s\r\n", buf);
388 IMAP->authstate = imap_as_expecting_username;
389 strcpy(IMAP->authseq, parms[0]);
394 cprintf("%s NO AUTHENTICATE %s failed\r\n",
399 void imap_auth_login_user(char *cmd)
403 CtdlDecodeBase64(buf, cmd, SIZ);
404 CtdlLoginExistingUser(buf);
405 CtdlEncodeBase64(buf, "Password:", 9);
406 cprintf("+ %s\r\n", buf);
407 IMAP->authstate = imap_as_expecting_password;
411 void imap_auth_login_pass(char *cmd)
415 CtdlDecodeBase64(buf, cmd, SIZ);
416 if (CtdlTryPassword(buf) == pass_ok) {
417 cprintf("%s OK authentication succeeded\r\n",
420 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
422 IMAP->authstate = imap_as_normal;
428 * implements the CAPABILITY command
430 void imap_capability(int num_parms, char *parms[])
432 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
435 cprintf(" STARTTLS");
439 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
445 * implements the STARTTLS command (Citadel API version)
448 void imap_starttls(int num_parms, char *parms[])
450 char ok_response[SIZ];
451 char nosup_response[SIZ];
452 char error_response[SIZ];
455 "%s OK begin TLS negotiation now\r\n",
457 sprintf(nosup_response,
458 "%s NO TLS not supported here\r\n",
460 sprintf(error_response,
461 "%s BAD Internal error\r\n",
463 CtdlStartTLS(ok_response, nosup_response, error_response);
469 * implements the SELECT command
471 void imap_select(int num_parms, char *parms[])
474 char augmented_roomname[ROOMNAMELEN];
478 struct ctdlroom QRscratch;
484 /* Convert the supplied folder name to a roomname */
485 i = imap_roomname(towhere, sizeof towhere, parms[2]);
487 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
491 floornum = (i & 0x00ff);
492 roomflags = (i & 0xff00);
494 /* First try a regular match */
495 c = getroom(&QRscratch, towhere);
497 /* Then try a mailbox name match */
499 MailboxName(augmented_roomname, sizeof augmented_roomname,
501 c = getroom(&QRscratch, augmented_roomname);
503 strcpy(towhere, augmented_roomname);
506 /* If the room exists, check security/access */
508 /* See if there is an existing user/room relationship */
509 ra = CtdlRoomAccess(&QRscratch, &CC->user);
511 /* normal clients have to pass through security */
517 /* Fail here if no such room */
519 cprintf("%s NO ... no such room, or access denied\r\n",
524 /* If we already had some other folder selected, auto-expunge it */
528 * usergoto() formally takes us to the desired room, happily returning
529 * the number of messages and number of new messages.
531 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
532 usergoto(NULL, 0, 0, &msgs, &new);
535 if (!strcasecmp(parms[1], "EXAMINE")) {
543 cprintf("* %d EXISTS\r\n", msgs);
544 cprintf("* %d RECENT\r\n", new);
546 /* Note that \Deleted is a valid flag, but not a permanent flag,
547 * because we don't maintain its state across sessions. Citadel
548 * automatically expunges mailboxes when they are de-selected.
550 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
551 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
552 "permanent flags\r\n");
554 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
555 cprintf("%s OK [%s] %s completed\r\n",
557 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
563 * does the real work for expunge
565 int imap_do_expunge(void)
568 int num_expunged = 0;
570 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
571 if (IMAP->selected == 0) {
575 if (IMAP->num_msgs > 0)
576 for (i = 0; i < IMAP->num_msgs; ++i) {
577 if (IMAP->flags[i] & IMAP_DELETED) {
578 CtdlDeleteMessages(CC->room.QRname,
579 IMAP->msgids[i], "");
584 if (num_expunged > 0) {
585 imap_rescan_msgids();
588 lprintf(9, "Expunged %d messages.\n", num_expunged);
589 return (num_expunged);
594 * implements the EXPUNGE command syntax
596 void imap_expunge(int num_parms, char *parms[])
598 int num_expunged = 0;
600 num_expunged = imap_do_expunge();
601 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
606 * implements the CLOSE command
608 void imap_close(int num_parms, char *parms[])
611 /* Yes, we always expunge on close. */
612 if (IMAP->selected) {
619 cprintf("%s OK CLOSE completed\r\n", parms[0]);
624 * Implements the NAMESPACE command.
626 void imap_namespace(int num_parms, char *parms[])
633 cprintf("* NAMESPACE ");
635 /* All personal folders are subordinate to INBOX. */
636 cprintf("((\"INBOX/\" \"/\")) ");
638 /* Other users' folders ... coming soon! FIXME */
641 /* Show all floors as shared namespaces. Neato! */
643 for (i = 0; i < MAXFLOORS; ++i) {
645 if (fl->f_flags & F_INUSE) {
646 if (floors > 0) cprintf(" ");
648 sprintf(buf, "%s/", fl->f_name);
656 /* Wind it up with a newline and a completion message. */
658 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
664 * Used by LIST and LSUB to show the floors in the listing
666 void imap_list_floors(char *cmd, char *pattern)
671 for (i = 0; i < MAXFLOORS; ++i) {
673 if (fl->f_flags & F_INUSE) {
674 if (imap_mailbox_matches_pattern
675 (pattern, fl->f_name)) {
676 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
677 imap_strout(fl->f_name);
687 * Back end for imap_lsub()
689 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
690 * may or may not be the desired behavior in the future.
692 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
698 pattern = (char *) data;
700 /* Only list rooms to which the user has access!! */
701 ra = CtdlRoomAccess(qrbuf, &CC->user);
703 imap_mailboxname(buf, sizeof buf, qrbuf);
704 if (imap_mailbox_matches_pattern(pattern, buf)) {
705 cprintf("* LSUB () \"/\" ");
714 * Implements the LSUB command
716 void imap_lsub(int num_parms, char *parms[])
720 cprintf("%s BAD arguments invalid\r\n", parms[0]);
723 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
725 if (strlen(parms[3]) == 0) {
726 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
730 imap_list_floors("LSUB", pattern);
731 ForEachRoom(imap_lsub_listroom, pattern);
734 cprintf("%s OK LSUB completed\r\n", parms[0]);
740 * Back end for imap_list()
742 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
748 pattern = (char *) data;
750 /* Only list rooms to which the user has access!! */
751 ra = CtdlRoomAccess(qrbuf, &CC->user);
753 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
754 imap_mailboxname(buf, sizeof buf, qrbuf);
755 if (imap_mailbox_matches_pattern(pattern, buf)) {
756 cprintf("* LIST () \"/\" ");
765 * Implements the LIST command
767 void imap_list(int num_parms, char *parms[])
771 cprintf("%s BAD arguments invalid\r\n", parms[0]);
774 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
776 if (strlen(parms[3]) == 0) {
777 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
781 imap_list_floors("LIST", pattern);
782 ForEachRoom(imap_list_listroom, pattern);
785 cprintf("%s OK LIST completed\r\n", parms[0]);
791 * Implements the CREATE command
794 void imap_create(int num_parms, char *parms[])
797 char roomname[ROOMNAMELEN];
802 if (strchr(parms[2], '\\') != NULL) {
803 cprintf("%s NO Invalid character in folder name\r\n",
805 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
809 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
811 cprintf("%s NO Invalid mailbox name or location\r\n",
813 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
816 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
817 flags = (ret & 0xff00); /* upper 8 bits = flags */
819 if (flags & IR_MAILBOX) {
820 if (strncasecmp(parms[2], "INBOX/", 6)) {
821 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
822 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
827 if (flags & IR_MAILBOX) {
828 newroomtype = 4; /* private mailbox */
830 newroomtype = 0; /* public folder */
833 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
834 roomname, floornum, newroomtype);
836 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
839 ("%s NO Mailbox already exists, or create failed\r\n",
842 cprintf("%s OK CREATE completed\r\n", parms[0]);
844 lprintf(CTDL_DEBUG, "imap_create() completed\n");
849 * Locate a room by its IMAP folder name, and check access to it
851 int imap_grabroom(char *returned_roomname, char *foldername)
854 char augmented_roomname[ROOMNAMELEN];
855 char roomname[ROOMNAMELEN];
857 struct ctdlroom QRscratch;
861 ret = imap_roomname(roomname, sizeof roomname, foldername);
866 /* First try a regular match */
867 c = getroom(&QRscratch, roomname);
869 /* Then try a mailbox name match */
871 MailboxName(augmented_roomname, sizeof augmented_roomname,
872 &CC->user, roomname);
873 c = getroom(&QRscratch, augmented_roomname);
875 strcpy(roomname, augmented_roomname);
878 /* If the room exists, check security/access */
880 /* See if there is an existing user/room relationship */
881 ra = CtdlRoomAccess(&QRscratch, &CC->user);
883 /* normal clients have to pass through security */
889 /* Fail here if no such room */
891 strcpy(returned_roomname, "");
894 strcpy(returned_roomname, QRscratch.QRname);
901 * Implements the STATUS command (sort of)
904 void imap_status(int num_parms, char *parms[])
907 char roomname[ROOMNAMELEN];
909 char savedroom[ROOMNAMELEN];
912 ret = imap_grabroom(roomname, parms[2]);
915 ("%s NO Invalid mailbox name or location, or access denied\r\n",
921 * usergoto() formally takes us to the desired room, happily returning
922 * the number of messages and number of new messages. (If another
923 * folder is selected, save its name so we can return there!!!!!)
925 if (IMAP->selected) {
926 strcpy(savedroom, CC->room.QRname);
928 usergoto(roomname, 0, 0, &msgs, &new);
931 * Tell the client what it wants to know. In fact, tell it *more* than
932 * it wants to know. We happily IGnore the supplied status data item
933 * names and simply spew all possible data items. It's far easier to
934 * code and probably saves us some processing time too.
936 imap_mailboxname(buf, sizeof buf, &CC->room);
937 cprintf("* STATUS ");
939 cprintf(" (MESSAGES %d ", msgs);
940 cprintf("RECENT 0 "); /* FIXME we need to implement this */
941 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
942 cprintf("UNSEEN %d)\r\n", new);
945 * If another folder is selected, go back to that room so we can resume
946 * our happy day without violent explosions.
948 if (IMAP->selected) {
949 usergoto(savedroom, 0, 0, &msgs, &new);
953 * Oooh, look, we're done!
955 cprintf("%s OK STATUS completed\r\n", parms[0]);
961 * Implements the SUBSCRIBE command
964 void imap_subscribe(int num_parms, char *parms[])
967 char roomname[ROOMNAMELEN];
968 char savedroom[ROOMNAMELEN];
971 ret = imap_grabroom(roomname, parms[2]);
974 ("%s NO Invalid mailbox name or location, or access denied\r\n",
980 * usergoto() formally takes us to the desired room, which has the side
981 * effect of marking the room as not-zapped ... exactly the effect
984 if (IMAP->selected) {
985 strcpy(savedroom, CC->room.QRname);
987 usergoto(roomname, 0, 0, &msgs, &new);
990 * If another folder is selected, go back to that room so we can resume
991 * our happy day without violent explosions.
993 if (IMAP->selected) {
994 usergoto(savedroom, 0, 0, &msgs, &new);
997 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1002 * Implements the UNSUBSCRIBE command
1005 void imap_unsubscribe(int num_parms, char *parms[])
1008 char roomname[ROOMNAMELEN];
1009 char savedroom[ROOMNAMELEN];
1012 ret = imap_grabroom(roomname, parms[2]);
1015 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1021 * usergoto() formally takes us to the desired room.
1023 if (IMAP->selected) {
1024 strcpy(savedroom, CC->room.QRname);
1026 usergoto(roomname, 0, 0, &msgs, &new);
1029 * Now make the API call to zap the room
1031 if (CtdlForgetThisRoom() == 0) {
1032 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1035 ("%s NO You may not unsubscribe from this folder.\r\n",
1040 * If another folder is selected, go back to that room so we can resume
1041 * our happy day without violent explosions.
1043 if (IMAP->selected) {
1044 usergoto(savedroom, 0, 0, &msgs, &new);
1051 * Implements the DELETE command
1054 void imap_delete(int num_parms, char *parms[])
1057 char roomname[ROOMNAMELEN];
1058 char savedroom[ROOMNAMELEN];
1061 ret = imap_grabroom(roomname, parms[2]);
1063 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1069 * usergoto() formally takes us to the desired room, happily returning
1070 * the number of messages and number of new messages. (If another
1071 * folder is selected, save its name so we can return there!!!!!)
1073 if (IMAP->selected) {
1074 strcpy(savedroom, CC->room.QRname);
1076 usergoto(roomname, 0, 0, &msgs, &new);
1079 * Now delete the room.
1081 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1082 cprintf("%s OK DELETE completed\r\n", parms[0]);
1083 delete_room(&CC->room);
1085 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1089 * If another folder is selected, go back to that room so we can resume
1090 * our happy day without violent explosions.
1092 if (IMAP->selected) {
1093 usergoto(savedroom, 0, 0, &msgs, &new);
1099 * Back end function for imap_rename()
1101 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1103 char foldername[SIZ];
1104 char newfoldername[SIZ];
1105 char newroomname[ROOMNAMELEN];
1107 struct irl *irlp = NULL; /* scratch pointer */
1108 struct irlparms *irlparms;
1110 irlparms = (struct irlparms *) data;
1111 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1113 /* Rename subfolders */
1114 if ((!strncasecmp(foldername, irlparms->oldname,
1115 strlen(irlparms->oldname))
1116 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1118 sprintf(newfoldername, "%s/%s",
1120 &foldername[strlen(irlparms->oldname) + 1]
1123 newfloor = imap_roomname(newroomname,
1125 newfoldername) & 0xFF;
1127 irlp = (struct irl *) malloc(sizeof(struct irl));
1128 strcpy(irlp->irl_newroom, newroomname);
1129 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1130 irlp->irl_newfloor = newfloor;
1131 irlp->next = *(irlparms->irl);
1132 *(irlparms->irl) = irlp;
1138 * Implements the RENAME command
1141 void imap_rename(int num_parms, char *parms[])
1143 char old_room[ROOMNAMELEN];
1144 char new_room[ROOMNAMELEN];
1148 struct irl *irl = NULL; /* the list */
1149 struct irl *irlp = NULL; /* scratch pointer */
1150 struct irlparms irlparms;
1152 if (strchr(parms[3], '\\') != NULL) {
1153 cprintf("%s NO Invalid character in folder name\r\n",
1158 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1159 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1160 new_floor = (newr & 0xFF);
1162 r = CtdlRenameRoom(old_room, new_room, new_floor);
1164 if (r == crr_room_not_found) {
1165 cprintf("%s NO Could not locate this folder\r\n",
1169 if (r == crr_already_exists) {
1170 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1173 if (r == crr_noneditable) {
1174 cprintf("%s This folder is not editable.\r\n", parms[0]);
1177 if (r == crr_invalid_floor) {
1178 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1181 if (r == crr_access_denied) {
1182 cprintf("%s You do not have permission to edit "
1183 "this folder.\r\n", parms[0]);
1187 cprintf("%s NO Rename failed - undefined error %d\r\n",
1193 /* If this is the INBOX, then RFC2060 says we have to just move the
1194 * contents. In a Citadel environment it's easier to rename the room
1195 * (already did that) and create a new inbox.
1197 if (!strcasecmp(parms[2], "INBOX")) {
1198 create_room(MAILROOM, 4, "", 0, 1, 0);
1201 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1203 irlparms.oldname = parms[2];
1204 irlparms.newname = parms[3];
1205 irlparms.irl = &irl;
1206 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1208 /* ... and now rename them. */
1209 while (irl != NULL) {
1210 r = CtdlRenameRoom(irl->irl_oldroom,
1214 /* FIXME handle error returns better */
1215 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1224 cprintf("%s OK RENAME completed\r\n", parms[0]);
1231 * Main command loop for IMAP sessions.
1233 void imap_command_loop(void)
1240 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1241 if (client_gets(cmdbuf) < 1) {
1242 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1247 lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
1248 while (strlen(cmdbuf) < 5)
1249 strcat(cmdbuf, " ");
1252 /* strip off l/t whitespace and CRLF */
1253 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1254 cmdbuf[strlen(cmdbuf) - 1] = 0;
1255 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1256 cmdbuf[strlen(cmdbuf) - 1] = 0;
1259 /* If we're in the middle of a multi-line command, handle that */
1260 if (IMAP->authstate == imap_as_expecting_username) {
1261 imap_auth_login_user(cmdbuf);
1264 if (IMAP->authstate == imap_as_expecting_password) {
1265 imap_auth_login_pass(cmdbuf);
1270 /* Ok, at this point we're in normal command mode. The first thing
1271 * we do is print any incoming pages (yeah! we really do!)
1273 imap_print_instant_messages();
1276 * Before processing the command that was just entered... if we happen
1277 * to have a folder selected, we'd like to rescan that folder for new
1278 * messages, and for deletions/changes of existing messages. This
1279 * could probably be optimized somehow, but IMAP sucks...
1281 if (IMAP->selected) {
1282 imap_rescan_msgids();
1285 /* Now for the command set. */
1287 /* Grab the tag, command, and parameters. Check syntax. */
1288 num_parms = imap_parameterize(parms, cmdbuf);
1289 if (num_parms < 2) {
1290 cprintf("BAD syntax error\r\n");
1293 /* The commands below may be executed in any state */
1295 else if ((!strcasecmp(parms[1], "NOOP"))
1296 || (!strcasecmp(parms[1], "CHECK"))) {
1297 cprintf("%s OK This command successfully did nothing.\r\n",
1301 else if (!strcasecmp(parms[1], "LOGOUT")) {
1302 if (IMAP->selected) {
1303 imap_do_expunge(); /* yes, we auto-expunge */
1305 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1306 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1312 else if (!strcasecmp(parms[1], "LOGIN")) {
1313 imap_login(num_parms, parms);
1316 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1317 imap_authenticate(num_parms, parms);
1320 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1321 imap_capability(num_parms, parms);
1324 else if (!strcasecmp(parms[1], "STARTTLS")) {
1325 imap_starttls(num_parms, parms);
1328 else if (!CC->logged_in) {
1329 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1332 /* The commans below require a logged-in state */
1334 else if (!strcasecmp(parms[1], "SELECT")) {
1335 imap_select(num_parms, parms);
1338 else if (!strcasecmp(parms[1], "EXAMINE")) {
1339 imap_select(num_parms, parms);
1342 else if (!strcasecmp(parms[1], "LSUB")) {
1343 imap_lsub(num_parms, parms);
1346 else if (!strcasecmp(parms[1], "LIST")) {
1347 imap_list(num_parms, parms);
1350 else if (!strcasecmp(parms[1], "CREATE")) {
1351 imap_create(num_parms, parms);
1354 else if (!strcasecmp(parms[1], "DELETE")) {
1355 imap_delete(num_parms, parms);
1358 else if (!strcasecmp(parms[1], "RENAME")) {
1359 imap_rename(num_parms, parms);
1362 else if (!strcasecmp(parms[1], "STATUS")) {
1363 imap_status(num_parms, parms);
1366 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1367 imap_subscribe(num_parms, parms);
1370 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1371 imap_unsubscribe(num_parms, parms);
1374 else if (!strcasecmp(parms[1], "APPEND")) {
1375 imap_append(num_parms, parms);
1378 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1379 imap_namespace(num_parms, parms);
1382 else if (IMAP->selected == 0) {
1383 cprintf("%s BAD no folder selected\r\n", parms[0]);
1386 /* The commands below require the SELECT state on a mailbox */
1388 else if (!strcasecmp(parms[1], "FETCH")) {
1389 imap_fetch(num_parms, parms);
1392 else if ((!strcasecmp(parms[1], "UID"))
1393 && (!strcasecmp(parms[2], "FETCH"))) {
1394 imap_uidfetch(num_parms, parms);
1397 else if (!strcasecmp(parms[1], "SEARCH")) {
1398 imap_search(num_parms, parms);
1401 else if ((!strcasecmp(parms[1], "UID"))
1402 && (!strcasecmp(parms[2], "SEARCH"))) {
1403 imap_uidsearch(num_parms, parms);
1406 else if (!strcasecmp(parms[1], "STORE")) {
1407 imap_store(num_parms, parms);
1410 else if ((!strcasecmp(parms[1], "UID"))
1411 && (!strcasecmp(parms[2], "STORE"))) {
1412 imap_uidstore(num_parms, parms);
1415 else if (!strcasecmp(parms[1], "COPY")) {
1416 imap_copy(num_parms, parms);
1419 else if ((!strcasecmp(parms[1], "UID"))
1420 && (!strcasecmp(parms[2], "COPY"))) {
1421 imap_uidcopy(num_parms, parms);
1424 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1425 imap_expunge(num_parms, parms);
1428 else if (!strcasecmp(parms[1], "CLOSE")) {
1429 imap_close(num_parms, parms);
1432 /* End of commands. If we get here, the command is either invalid
1437 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1440 /* If the client transmitted a message we can free it now */
1441 imap_free_transmitted_message();
1447 * This function is called to register the IMAP extension with Citadel.
1449 char *serv_imap_init(void)
1451 CtdlRegisterServiceHook(config.c_imap_port,
1452 NULL, imap_greeting, imap_command_loop, NULL);
1453 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);