4 * IMAP server for the Citadel system
5 * Copyright (C) 2000-2005 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * WARNING: the IMAP protocol is badly designed. No implementation of it
9 * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP
10 * implementations have bugs.
21 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
40 #include "sysdep_decls.h"
41 #include "citserver.h"
44 #include "serv_extensions.h"
51 #include "internet_addressing.h"
52 #include "serv_imap.h"
53 #include "imap_tools.h"
54 #include "imap_fetch.h"
55 #include "imap_search.h"
56 #include "imap_store.h"
57 #include "imap_misc.h"
60 #include "serv_crypto.h"
63 /* imap_rename() uses this struct containing list of rooms to rename */
66 char irl_oldroom[ROOMNAMELEN];
67 char irl_newroom[ROOMNAMELEN];
71 /* Data which is passed between imap_rename() and imap_rename_backend() */
80 * If there is a message ID map in memory, free it
82 void imap_free_msgids(void)
84 if (IMAP->msgids != NULL) {
90 if (IMAP->flags != NULL) {
98 * If there is a transmitted message in memory, free it
100 void imap_free_transmitted_message(void)
102 if (IMAP->transmitted_message != NULL) {
103 free(IMAP->transmitted_message);
104 IMAP->transmitted_message = NULL;
105 IMAP->transmitted_length = 0;
111 * Set the \\Seen flag for messages which aren't new
113 void imap_set_seen_flags(void)
118 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
119 if (IMAP->num_msgs > 0) {
120 for (i = 0; i < IMAP->num_msgs; ++i) {
121 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
122 IMAP->flags[i] |= IMAP_SEEN;
125 IMAP->flags[i] |= IMAP_RECENT;
128 (vbuf.v_answered, IMAP->msgids[i])) {
129 IMAP->flags[i] |= IMAP_ANSWERED;
139 * Back end for imap_load_msgids()
141 * Optimization: instead of calling realloc() to add each message, we
142 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
143 * allows the mapping to proceed much faster.
145 void imap_add_single_msgid(long msgnum, void *userdata)
149 if (IMAP->num_msgs > IMAP->num_alloc) {
150 IMAP->num_alloc += REALLOC_INCREMENT;
151 IMAP->msgids = realloc(IMAP->msgids, (IMAP->num_alloc * sizeof(long)));
152 IMAP->flags = realloc(IMAP->flags, (IMAP->num_alloc * sizeof(long)));
154 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
155 IMAP->flags[IMAP->num_msgs - 1] = 0;
161 * Set up a message ID map for the current room (folder)
163 void imap_load_msgids(void)
166 if (IMAP->selected == 0) {
168 "imap_load_msgids() can't run; no room selected\n");
172 imap_free_msgids(); /* If there was already a map, free it */
174 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
175 imap_add_single_msgid, NULL);
177 imap_set_seen_flags();
178 /* lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
184 * Re-scan the selected room (folder) and see if it's been changed at all
186 void imap_rescan_msgids(void)
189 int original_num_msgs = 0;
190 long original_highest = 0L;
192 int message_still_exists;
193 struct cdbdata *cdbfr;
194 long *msglist = NULL;
199 if (IMAP->selected == 0) {
201 "imap_load_msgids() can't run; no room selected\n");
205 /* Load the *current* message list from disk, so we can compare it
206 * to what we have in memory.
208 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
210 msglist = malloc(cdbfr->len);
211 if (msglist == NULL) {
212 lprintf(CTDL_CRIT, "malloc() failed\n");
215 memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
216 num_msgs = cdbfr->len / sizeof(long);
223 * Check to see if any of the messages we know about have been expunged
225 if (IMAP->num_msgs > 0)
226 for (i = 0; i < IMAP->num_msgs; ++i) {
228 message_still_exists = 0;
230 for (j = 0; j < num_msgs; ++j) {
231 if (msglist[j] == IMAP->msgids[i]) {
232 message_still_exists = 1;
236 if (message_still_exists == 0) {
237 cprintf("* %d EXPUNGE\r\n", i + 1);
239 /* Here's some nice stupid nonsense. When a message
240 * is expunged, we have to slide all the existing
241 * messages up in the message array.
244 memcpy(&IMAP->msgids[i],
245 &IMAP->msgids[i + 1],
247 (IMAP->num_msgs - i)));
248 memcpy(&IMAP->flags[i],
251 (IMAP->num_msgs - i)));
259 * Remember how many messages were here before we re-scanned.
261 original_num_msgs = IMAP->num_msgs;
262 if (IMAP->num_msgs > 0) {
263 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
265 original_highest = 0L;
269 * Now peruse the room for *new* messages only.
272 for (j = 0; j < num_msgs; ++j) {
273 if (msglist[j] > original_highest) {
274 imap_add_single_msgid(msglist[j], NULL);
277 imap_set_seen_flags();
280 * If new messages have arrived, tell the client about them.
282 if (IMAP->num_msgs > original_num_msgs) {
284 for (j = 0; j < num_msgs; ++j) {
285 if (IMAP->flags[j] & IMAP_RECENT) {
290 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
291 cprintf("* %d RECENT\r\n", num_recent);
305 * This cleanup function blows away the temporary memory and files used by
308 void imap_cleanup_function(void)
311 /* Don't do this stuff if this is not a IMAP session! */
312 if (CC->h_command_function != imap_command_loop)
315 /* If there is a mailbox selected, auto-expunge it. */
316 if (IMAP->selected) {
320 lprintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
322 imap_free_transmitted_message();
324 if (IMAP->cached_rfc822_data != NULL) {
325 free(IMAP->cached_rfc822_data);
326 IMAP->cached_rfc822_data = NULL;
327 IMAP->cached_rfc822_msgnum = (-1);
330 if (IMAP->cached_body != NULL) {
331 free(IMAP->cached_body);
332 IMAP->cached_body = NULL;
333 IMAP->cached_body_len = 0;
334 IMAP->cached_bodymsgnum = (-1);
338 lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
344 * Here's where our IMAP session begins its happy day.
346 void imap_greeting(void)
349 strcpy(CC->cs_clientname, "IMAP session");
350 IMAP = malloc(sizeof (struct citimap));
351 IMAP->authstate = imap_as_normal;
352 IMAP->cached_rfc822_data = NULL;
353 IMAP->cached_rfc822_msgnum = (-1);
355 cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
360 * IMAPS is just like IMAP, except it goes crypto right away.
363 void imaps_greeting(void) {
364 CtdlStartTLS(NULL, NULL, NULL);
371 * implements the LOGIN command (ordinary username/password login)
373 void imap_login(int num_parms, char *parms[])
375 if (num_parms != 4) {
376 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
380 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
381 if (CtdlTryPassword(parms[3]) == pass_ok) {
382 cprintf("%s OK login successful\r\n", parms[0]);
387 cprintf("%s BAD Login incorrect\r\n", parms[0]);
392 * Implements the AUTHENTICATE command
394 void imap_authenticate(int num_parms, char *parms[])
398 if (num_parms != 3) {
399 cprintf("%s BAD incorrect number of parameters\r\n",
405 cprintf("%s BAD Already logged in.\r\n", parms[0]);
409 if (!strcasecmp(parms[2], "LOGIN")) {
410 CtdlEncodeBase64(buf, "Username:", 9);
411 cprintf("+ %s\r\n", buf);
412 IMAP->authstate = imap_as_expecting_username;
413 strcpy(IMAP->authseq, parms[0]);
418 cprintf("%s NO AUTHENTICATE %s failed\r\n",
423 void imap_auth_login_user(char *cmd)
427 CtdlDecodeBase64(buf, cmd, SIZ);
428 CtdlLoginExistingUser(buf);
429 CtdlEncodeBase64(buf, "Password:", 9);
430 cprintf("+ %s\r\n", buf);
431 IMAP->authstate = imap_as_expecting_password;
435 void imap_auth_login_pass(char *cmd)
439 CtdlDecodeBase64(buf, cmd, SIZ);
440 if (CtdlTryPassword(buf) == pass_ok) {
441 cprintf("%s OK authentication succeeded\r\n",
444 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
446 IMAP->authstate = imap_as_normal;
452 * implements the CAPABILITY command
454 void imap_capability(int num_parms, char *parms[])
456 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
459 cprintf(" STARTTLS");
463 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
469 * implements the STARTTLS command (Citadel API version)
472 void imap_starttls(int num_parms, char *parms[])
474 char ok_response[SIZ];
475 char nosup_response[SIZ];
476 char error_response[SIZ];
479 "%s OK begin TLS negotiation now\r\n",
481 sprintf(nosup_response,
482 "%s NO TLS not supported here\r\n",
484 sprintf(error_response,
485 "%s BAD Internal error\r\n",
487 CtdlStartTLS(ok_response, nosup_response, error_response);
493 * implements the SELECT command
495 void imap_select(int num_parms, char *parms[])
498 char augmented_roomname[ROOMNAMELEN];
502 struct ctdlroom QRscratch;
508 /* Convert the supplied folder name to a roomname */
509 i = imap_roomname(towhere, sizeof towhere, parms[2]);
511 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
515 floornum = (i & 0x00ff);
516 roomflags = (i & 0xff00);
518 /* First try a regular match */
519 c = getroom(&QRscratch, towhere);
521 /* Then try a mailbox name match */
523 MailboxName(augmented_roomname, sizeof augmented_roomname,
525 c = getroom(&QRscratch, augmented_roomname);
527 strcpy(towhere, augmented_roomname);
530 /* If the room exists, check security/access */
532 /* See if there is an existing user/room relationship */
533 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
535 /* normal clients have to pass through security */
541 /* Fail here if no such room */
543 cprintf("%s NO ... no such room, or access denied\r\n",
548 /* If we already had some other folder selected, auto-expunge it */
552 * usergoto() formally takes us to the desired room, happily returning
553 * the number of messages and number of new messages.
555 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
556 usergoto(NULL, 0, 0, &msgs, &new);
559 if (!strcasecmp(parms[1], "EXAMINE")) {
567 cprintf("* %d EXISTS\r\n", msgs);
568 cprintf("* %d RECENT\r\n", new);
570 /* Note that \Deleted is a valid flag, but not a permanent flag,
571 * because we don't maintain its state across sessions. Citadel
572 * automatically expunges mailboxes when they are de-selected.
574 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
575 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
576 "permanent flags\r\n");
578 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
579 cprintf("%s OK [%s] %s completed\r\n",
581 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
587 * does the real work for expunge
589 int imap_do_expunge(void)
592 int num_expunged = 0;
594 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
595 if (IMAP->selected == 0) {
599 if (IMAP->num_msgs > 0)
600 for (i = 0; i < IMAP->num_msgs; ++i) {
601 if (IMAP->flags[i] & IMAP_DELETED) {
602 CtdlDeleteMessages(CC->room.QRname,
603 IMAP->msgids[i], "");
608 if (num_expunged > 0) {
609 imap_rescan_msgids();
612 lprintf(CTDL_DEBUG, "Expunged %d messages.\n", num_expunged);
613 return (num_expunged);
618 * implements the EXPUNGE command syntax
620 void imap_expunge(int num_parms, char *parms[])
622 int num_expunged = 0;
624 num_expunged = imap_do_expunge();
625 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
630 * implements the CLOSE command
632 void imap_close(int num_parms, char *parms[])
635 /* Yes, we always expunge on close. */
636 if (IMAP->selected) {
643 cprintf("%s OK CLOSE completed\r\n", parms[0]);
648 * Implements the NAMESPACE command.
650 void imap_namespace(int num_parms, char *parms[])
657 cprintf("* NAMESPACE ");
659 /* All personal folders are subordinate to INBOX. */
660 cprintf("((\"INBOX/\" \"/\")) ");
662 /* Other users' folders ... coming soon! FIXME */
665 /* Show all floors as shared namespaces. Neato! */
667 for (i = 0; i < MAXFLOORS; ++i) {
669 if (fl->f_flags & F_INUSE) {
670 if (floors > 0) cprintf(" ");
672 sprintf(buf, "%s/", fl->f_name);
680 /* Wind it up with a newline and a completion message. */
682 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
688 * Used by LIST and LSUB to show the floors in the listing
690 void imap_list_floors(char *cmd, char *pattern)
695 for (i = 0; i < MAXFLOORS; ++i) {
697 if (fl->f_flags & F_INUSE) {
698 if (imap_mailbox_matches_pattern
699 (pattern, fl->f_name)) {
700 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
701 imap_strout(fl->f_name);
711 * Back end for imap_lsub()
713 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
714 * may or may not be the desired behavior in the future.
716 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
722 pattern = (char *) data;
724 /* Only list rooms to which the user has access!! */
725 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
727 imap_mailboxname(buf, sizeof buf, qrbuf);
728 if (imap_mailbox_matches_pattern(pattern, buf)) {
729 cprintf("* LSUB () \"/\" ");
738 * Implements the LSUB command
740 void imap_lsub(int num_parms, char *parms[])
744 cprintf("%s BAD arguments invalid\r\n", parms[0]);
747 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
749 if (strlen(parms[3]) == 0) {
750 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
754 imap_list_floors("LSUB", pattern);
755 ForEachRoom(imap_lsub_listroom, pattern);
758 cprintf("%s OK LSUB completed\r\n", parms[0]);
764 * Back end for imap_list()
766 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
772 pattern = (char *) data;
774 /* Only list rooms to which the user has access!! */
775 CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
777 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
778 imap_mailboxname(buf, sizeof buf, qrbuf);
779 if (imap_mailbox_matches_pattern(pattern, buf)) {
780 cprintf("* LIST () \"/\" ");
789 * Implements the LIST command
791 void imap_list(int num_parms, char *parms[])
795 cprintf("%s BAD arguments invalid\r\n", parms[0]);
798 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
800 if (strlen(parms[3]) == 0) {
801 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
805 imap_list_floors("LIST", pattern);
806 ForEachRoom(imap_list_listroom, pattern);
809 cprintf("%s OK LIST completed\r\n", parms[0]);
815 * Implements the CREATE command
818 void imap_create(int num_parms, char *parms[])
821 char roomname[ROOMNAMELEN];
827 if (strchr(parms[2], '\\') != NULL) {
828 cprintf("%s NO Invalid character in folder name\r\n",
830 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
834 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
836 cprintf("%s NO Invalid mailbox name or location\r\n",
838 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
841 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
842 flags = (ret & 0xff00); /* upper 8 bits = flags */
844 if (flags & IR_MAILBOX) {
845 if (strncasecmp(parms[2], "INBOX/", 6)) {
846 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
847 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
852 if (flags & IR_MAILBOX) {
853 newroomtype = 4; /* private mailbox */
854 newroomview = VIEW_MAILBOX;
856 newroomtype = 0; /* public folder */
857 newroomview = VIEW_BBS;
860 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
861 roomname, floornum, newroomtype);
863 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
866 ("%s NO Mailbox already exists, or create failed\r\n",
869 cprintf("%s OK CREATE completed\r\n", parms[0]);
871 lprintf(CTDL_DEBUG, "imap_create() completed\n");
876 * Locate a room by its IMAP folder name, and check access to it.
877 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
879 int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
882 char augmented_roomname[ROOMNAMELEN];
883 char roomname[ROOMNAMELEN];
885 struct ctdlroom QRscratch;
889 ret = imap_roomname(roomname, sizeof roomname, foldername);
894 /* First try a regular match */
895 c = getroom(&QRscratch, roomname);
897 /* Then try a mailbox name match */
899 MailboxName(augmented_roomname, sizeof augmented_roomname,
900 &CC->user, roomname);
901 c = getroom(&QRscratch, augmented_roomname);
903 strcpy(roomname, augmented_roomname);
906 /* If the room exists, check security/access */
908 /* See if there is an existing user/room relationship */
909 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
911 /* normal clients have to pass through security */
915 if ((zapped_ok) && (ra & UA_ZAPPED)) {
920 /* Fail here if no such room */
922 strcpy(returned_roomname, "");
925 strcpy(returned_roomname, QRscratch.QRname);
932 * Implements the STATUS command (sort of)
935 void imap_status(int num_parms, char *parms[])
938 char roomname[ROOMNAMELEN];
940 char savedroom[ROOMNAMELEN];
943 ret = imap_grabroom(roomname, parms[2], 0);
946 ("%s NO Invalid mailbox name or location, or access denied\r\n",
952 * usergoto() formally takes us to the desired room, happily returning
953 * the number of messages and number of new messages. (If another
954 * folder is selected, save its name so we can return there!!!!!)
956 if (IMAP->selected) {
957 strcpy(savedroom, CC->room.QRname);
959 usergoto(roomname, 0, 0, &msgs, &new);
962 * Tell the client what it wants to know. In fact, tell it *more* than
963 * it wants to know. We happily IGnore the supplied status data item
964 * names and simply spew all possible data items. It's far easier to
965 * code and probably saves us some processing time too.
967 imap_mailboxname(buf, sizeof buf, &CC->room);
968 cprintf("* STATUS ");
970 cprintf(" (MESSAGES %d ", msgs);
971 cprintf("RECENT %d ", new); /* Initially, new==recent */
972 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
973 cprintf("UNSEEN %d)\r\n", new);
976 * If another folder is selected, go back to that room so we can resume
977 * our happy day without violent explosions.
979 if (IMAP->selected) {
980 usergoto(savedroom, 0, 0, &msgs, &new);
984 * Oooh, look, we're done!
986 cprintf("%s OK STATUS completed\r\n", parms[0]);
992 * Implements the SUBSCRIBE command
995 void imap_subscribe(int num_parms, char *parms[])
998 char roomname[ROOMNAMELEN];
999 char savedroom[ROOMNAMELEN];
1002 ret = imap_grabroom(roomname, parms[2], 1);
1005 "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1013 * usergoto() formally takes us to the desired room, which has the side
1014 * effect of marking the room as not-zapped ... exactly the effect
1015 * we're looking for.
1017 if (IMAP->selected) {
1018 strcpy(savedroom, CC->room.QRname);
1020 usergoto(roomname, 0, 0, &msgs, &new);
1023 * If another folder is selected, go back to that room so we can resume
1024 * our happy day without violent explosions.
1026 if (IMAP->selected) {
1027 usergoto(savedroom, 0, 0, &msgs, &new);
1030 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1035 * Implements the UNSUBSCRIBE command
1038 void imap_unsubscribe(int num_parms, char *parms[])
1041 char roomname[ROOMNAMELEN];
1042 char savedroom[ROOMNAMELEN];
1045 ret = imap_grabroom(roomname, parms[2], 0);
1048 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1054 * usergoto() formally takes us to the desired room.
1056 if (IMAP->selected) {
1057 strcpy(savedroom, CC->room.QRname);
1059 usergoto(roomname, 0, 0, &msgs, &new);
1062 * Now make the API call to zap the room
1064 if (CtdlForgetThisRoom() == 0) {
1065 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1068 ("%s NO You may not unsubscribe from this folder.\r\n",
1073 * If another folder is selected, go back to that room so we can resume
1074 * our happy day without violent explosions.
1076 if (IMAP->selected) {
1077 usergoto(savedroom, 0, 0, &msgs, &new);
1084 * Implements the DELETE command
1087 void imap_delete(int num_parms, char *parms[])
1090 char roomname[ROOMNAMELEN];
1091 char savedroom[ROOMNAMELEN];
1094 ret = imap_grabroom(roomname, parms[2], 0);
1096 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1102 * usergoto() formally takes us to the desired room, happily returning
1103 * the number of messages and number of new messages. (If another
1104 * folder is selected, save its name so we can return there!!!!!)
1106 if (IMAP->selected) {
1107 strcpy(savedroom, CC->room.QRname);
1109 usergoto(roomname, 0, 0, &msgs, &new);
1112 * Now delete the room.
1114 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1115 cprintf("%s OK DELETE completed\r\n", parms[0]);
1116 delete_room(&CC->room);
1118 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1122 * If another folder is selected, go back to that room so we can resume
1123 * our happy day without violent explosions.
1125 if (IMAP->selected) {
1126 usergoto(savedroom, 0, 0, &msgs, &new);
1132 * Back end function for imap_rename()
1134 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1136 char foldername[SIZ];
1137 char newfoldername[SIZ];
1138 char newroomname[ROOMNAMELEN];
1140 struct irl *irlp = NULL; /* scratch pointer */
1141 struct irlparms *irlparms;
1143 irlparms = (struct irlparms *) data;
1144 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1146 /* Rename subfolders */
1147 if ((!strncasecmp(foldername, irlparms->oldname,
1148 strlen(irlparms->oldname))
1149 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1151 sprintf(newfoldername, "%s/%s",
1153 &foldername[strlen(irlparms->oldname) + 1]
1156 newfloor = imap_roomname(newroomname,
1158 newfoldername) & 0xFF;
1160 irlp = (struct irl *) malloc(sizeof(struct irl));
1161 strcpy(irlp->irl_newroom, newroomname);
1162 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1163 irlp->irl_newfloor = newfloor;
1164 irlp->next = *(irlparms->irl);
1165 *(irlparms->irl) = irlp;
1171 * Implements the RENAME command
1174 void imap_rename(int num_parms, char *parms[])
1176 char old_room[ROOMNAMELEN];
1177 char new_room[ROOMNAMELEN];
1181 struct irl *irl = NULL; /* the list */
1182 struct irl *irlp = NULL; /* scratch pointer */
1183 struct irlparms irlparms;
1185 if (strchr(parms[3], '\\') != NULL) {
1186 cprintf("%s NO Invalid character in folder name\r\n",
1191 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1192 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1193 new_floor = (newr & 0xFF);
1195 r = CtdlRenameRoom(old_room, new_room, new_floor);
1197 if (r == crr_room_not_found) {
1198 cprintf("%s NO Could not locate this folder\r\n",
1202 if (r == crr_already_exists) {
1203 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1206 if (r == crr_noneditable) {
1207 cprintf("%s This folder is not editable.\r\n", parms[0]);
1210 if (r == crr_invalid_floor) {
1211 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1214 if (r == crr_access_denied) {
1215 cprintf("%s You do not have permission to edit "
1216 "this folder.\r\n", parms[0]);
1220 cprintf("%s NO Rename failed - undefined error %d\r\n",
1226 /* If this is the INBOX, then RFC2060 says we have to just move the
1227 * contents. In a Citadel environment it's easier to rename the room
1228 * (already did that) and create a new inbox.
1230 if (!strcasecmp(parms[2], "INBOX")) {
1231 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
1234 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1236 irlparms.oldname = parms[2];
1237 irlparms.newname = parms[3];
1238 irlparms.irl = &irl;
1239 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1241 /* ... and now rename them. */
1242 while (irl != NULL) {
1243 r = CtdlRenameRoom(irl->irl_oldroom,
1247 /* FIXME handle error returns better */
1248 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1257 cprintf("%s OK RENAME completed\r\n", parms[0]);
1264 * Main command loop for IMAP sessions.
1266 void imap_command_loop(void)
1272 CC->lastcmd = time(NULL);
1273 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1275 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1276 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1281 lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
1282 while (strlen(cmdbuf) < 5)
1283 strcat(cmdbuf, " ");
1285 /* strip off l/t whitespace and CRLF */
1286 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1287 cmdbuf[strlen(cmdbuf) - 1] = 0;
1288 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1289 cmdbuf[strlen(cmdbuf) - 1] = 0;
1292 /* If we're in the middle of a multi-line command, handle that */
1293 if (IMAP->authstate == imap_as_expecting_username) {
1294 imap_auth_login_user(cmdbuf);
1297 if (IMAP->authstate == imap_as_expecting_password) {
1298 imap_auth_login_pass(cmdbuf);
1302 /* Ok, at this point we're in normal command mode. The first thing
1303 * we do is print any incoming pages (yeah! we really do!)
1305 imap_print_instant_messages();
1308 * Before processing the command that was just entered... if we happen
1309 * to have a folder selected, we'd like to rescan that folder for new
1310 * messages, and for deletions/changes of existing messages. This
1311 * could probably be optimized somehow, but IMAP sucks...
1313 if (IMAP->selected) {
1314 imap_rescan_msgids();
1317 /* Now for the command set. */
1319 /* Grab the tag, command, and parameters. Check syntax. */
1320 num_parms = imap_parameterize(parms, cmdbuf);
1321 if (num_parms < 2) {
1322 cprintf("BAD syntax error\r\n");
1325 /* The commands below may be executed in any state */
1327 else if ((!strcasecmp(parms[1], "NOOP"))
1328 || (!strcasecmp(parms[1], "CHECK"))) {
1329 cprintf("%s OK This command successfully did nothing.\r\n",
1333 else if (!strcasecmp(parms[1], "LOGOUT")) {
1334 if (IMAP->selected) {
1335 imap_do_expunge(); /* yes, we auto-expunge */
1337 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1338 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1344 else if (!strcasecmp(parms[1], "LOGIN")) {
1345 imap_login(num_parms, parms);
1348 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1349 imap_authenticate(num_parms, parms);
1352 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1353 imap_capability(num_parms, parms);
1356 else if (!strcasecmp(parms[1], "STARTTLS")) {
1357 imap_starttls(num_parms, parms);
1360 else if (!CC->logged_in) {
1361 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1364 /* The commans below require a logged-in state */
1366 else if (!strcasecmp(parms[1], "SELECT")) {
1367 imap_select(num_parms, parms);
1370 else if (!strcasecmp(parms[1], "EXAMINE")) {
1371 imap_select(num_parms, parms);
1374 else if (!strcasecmp(parms[1], "LSUB")) {
1375 imap_lsub(num_parms, parms);
1378 else if (!strcasecmp(parms[1], "LIST")) {
1379 imap_list(num_parms, parms);
1382 else if (!strcasecmp(parms[1], "CREATE")) {
1383 imap_create(num_parms, parms);
1386 else if (!strcasecmp(parms[1], "DELETE")) {
1387 imap_delete(num_parms, parms);
1390 else if (!strcasecmp(parms[1], "RENAME")) {
1391 imap_rename(num_parms, parms);
1394 else if (!strcasecmp(parms[1], "STATUS")) {
1395 imap_status(num_parms, parms);
1398 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1399 imap_subscribe(num_parms, parms);
1402 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1403 imap_unsubscribe(num_parms, parms);
1406 else if (!strcasecmp(parms[1], "APPEND")) {
1407 imap_append(num_parms, parms);
1410 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1411 imap_namespace(num_parms, parms);
1414 else if (IMAP->selected == 0) {
1415 cprintf("%s BAD no folder selected\r\n", parms[0]);
1418 /* The commands below require the SELECT state on a mailbox */
1420 else if (!strcasecmp(parms[1], "FETCH")) {
1421 imap_fetch(num_parms, parms);
1424 else if ((!strcasecmp(parms[1], "UID"))
1425 && (!strcasecmp(parms[2], "FETCH"))) {
1426 imap_uidfetch(num_parms, parms);
1429 else if (!strcasecmp(parms[1], "SEARCH")) {
1430 imap_search(num_parms, parms);
1433 else if ((!strcasecmp(parms[1], "UID"))
1434 && (!strcasecmp(parms[2], "SEARCH"))) {
1435 imap_uidsearch(num_parms, parms);
1438 else if (!strcasecmp(parms[1], "STORE")) {
1439 imap_store(num_parms, parms);
1442 else if ((!strcasecmp(parms[1], "UID"))
1443 && (!strcasecmp(parms[2], "STORE"))) {
1444 imap_uidstore(num_parms, parms);
1447 else if (!strcasecmp(parms[1], "COPY")) {
1448 imap_copy(num_parms, parms);
1451 else if ((!strcasecmp(parms[1], "UID"))
1452 && (!strcasecmp(parms[2], "COPY"))) {
1453 imap_uidcopy(num_parms, parms);
1456 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1457 imap_expunge(num_parms, parms);
1460 else if (!strcasecmp(parms[1], "CLOSE")) {
1461 imap_close(num_parms, parms);
1464 /* End of commands. If we get here, the command is either invalid
1469 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1472 /* If the client transmitted a message we can free it now */
1473 imap_free_transmitted_message();
1479 * This function is called to register the IMAP extension with Citadel.
1481 char *serv_imap_init(void)
1483 CtdlRegisterServiceHook(config.c_imap_port,
1484 NULL, imap_greeting, imap_command_loop, NULL);
1486 CtdlRegisterServiceHook(config.c_imaps_port,
1487 NULL, imaps_greeting, imap_command_loop, NULL);
1489 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);