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 (num_parms != 4) {
369 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
373 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
374 if (CtdlTryPassword(parms[3]) == pass_ok) {
375 cprintf("%s OK login successful\r\n", parms[0]);
380 cprintf("%s BAD Login incorrect\r\n", parms[0]);
385 * Implements the AUTHENTICATE command
387 void imap_authenticate(int num_parms, char *parms[])
391 if (num_parms != 3) {
392 cprintf("%s BAD incorrect number of parameters\r\n",
398 cprintf("%s BAD Already logged in.\r\n", parms[0]);
402 if (!strcasecmp(parms[2], "LOGIN")) {
403 CtdlEncodeBase64(buf, "Username:", 9);
404 cprintf("+ %s\r\n", buf);
405 IMAP->authstate = imap_as_expecting_username;
406 strcpy(IMAP->authseq, parms[0]);
411 cprintf("%s NO AUTHENTICATE %s failed\r\n",
416 void imap_auth_login_user(char *cmd)
420 CtdlDecodeBase64(buf, cmd, SIZ);
421 CtdlLoginExistingUser(buf);
422 CtdlEncodeBase64(buf, "Password:", 9);
423 cprintf("+ %s\r\n", buf);
424 IMAP->authstate = imap_as_expecting_password;
428 void imap_auth_login_pass(char *cmd)
432 CtdlDecodeBase64(buf, cmd, SIZ);
433 if (CtdlTryPassword(buf) == pass_ok) {
434 cprintf("%s OK authentication succeeded\r\n",
437 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
439 IMAP->authstate = imap_as_normal;
445 * implements the CAPABILITY command
447 void imap_capability(int num_parms, char *parms[])
449 cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
452 cprintf(" STARTTLS");
456 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
462 * implements the STARTTLS command (Citadel API version)
465 void imap_starttls(int num_parms, char *parms[])
467 char ok_response[SIZ];
468 char nosup_response[SIZ];
469 char error_response[SIZ];
472 "%s OK begin TLS negotiation now\r\n",
474 sprintf(nosup_response,
475 "%s NO TLS not supported here\r\n",
477 sprintf(error_response,
478 "%s BAD Internal error\r\n",
480 CtdlStartTLS(ok_response, nosup_response, error_response);
486 * implements the SELECT command
488 void imap_select(int num_parms, char *parms[])
491 char augmented_roomname[ROOMNAMELEN];
495 struct ctdlroom QRscratch;
501 /* Convert the supplied folder name to a roomname */
502 i = imap_roomname(towhere, sizeof towhere, parms[2]);
504 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
508 floornum = (i & 0x00ff);
509 roomflags = (i & 0xff00);
511 /* First try a regular match */
512 c = getroom(&QRscratch, towhere);
514 /* Then try a mailbox name match */
516 MailboxName(augmented_roomname, sizeof augmented_roomname,
518 c = getroom(&QRscratch, augmented_roomname);
520 strcpy(towhere, augmented_roomname);
523 /* If the room exists, check security/access */
525 /* See if there is an existing user/room relationship */
526 ra = CtdlRoomAccess(&QRscratch, &CC->user);
528 /* normal clients have to pass through security */
534 /* Fail here if no such room */
536 cprintf("%s NO ... no such room, or access denied\r\n",
541 /* If we already had some other folder selected, auto-expunge it */
545 * usergoto() formally takes us to the desired room, happily returning
546 * the number of messages and number of new messages.
548 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
549 usergoto(NULL, 0, 0, &msgs, &new);
552 if (!strcasecmp(parms[1], "EXAMINE")) {
560 cprintf("* %d EXISTS\r\n", msgs);
561 cprintf("* %d RECENT\r\n", new);
563 /* Note that \Deleted is a valid flag, but not a permanent flag,
564 * because we don't maintain its state across sessions. Citadel
565 * automatically expunges mailboxes when they are de-selected.
567 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
568 cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
569 "permanent flags\r\n");
571 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
572 cprintf("%s OK [%s] %s completed\r\n",
574 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
580 * does the real work for expunge
582 int imap_do_expunge(void)
585 int num_expunged = 0;
587 lprintf(CTDL_DEBUG, "imap_do_expunge() called\n");
588 if (IMAP->selected == 0) {
592 if (IMAP->num_msgs > 0)
593 for (i = 0; i < IMAP->num_msgs; ++i) {
594 if (IMAP->flags[i] & IMAP_DELETED) {
595 CtdlDeleteMessages(CC->room.QRname,
596 IMAP->msgids[i], "");
601 if (num_expunged > 0) {
602 imap_rescan_msgids();
605 lprintf(9, "Expunged %d messages.\n", num_expunged);
606 return (num_expunged);
611 * implements the EXPUNGE command syntax
613 void imap_expunge(int num_parms, char *parms[])
615 int num_expunged = 0;
617 num_expunged = imap_do_expunge();
618 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
623 * implements the CLOSE command
625 void imap_close(int num_parms, char *parms[])
628 /* Yes, we always expunge on close. */
629 if (IMAP->selected) {
636 cprintf("%s OK CLOSE completed\r\n", parms[0]);
641 * Implements the NAMESPACE command.
643 void imap_namespace(int num_parms, char *parms[])
650 cprintf("* NAMESPACE ");
652 /* All personal folders are subordinate to INBOX. */
653 cprintf("((\"INBOX/\" \"/\")) ");
655 /* Other users' folders ... coming soon! FIXME */
658 /* Show all floors as shared namespaces. Neato! */
660 for (i = 0; i < MAXFLOORS; ++i) {
662 if (fl->f_flags & F_INUSE) {
663 if (floors > 0) cprintf(" ");
665 sprintf(buf, "%s/", fl->f_name);
673 /* Wind it up with a newline and a completion message. */
675 cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
681 * Used by LIST and LSUB to show the floors in the listing
683 void imap_list_floors(char *cmd, char *pattern)
688 for (i = 0; i < MAXFLOORS; ++i) {
690 if (fl->f_flags & F_INUSE) {
691 if (imap_mailbox_matches_pattern
692 (pattern, fl->f_name)) {
693 cprintf("* %s (\\NoSelect) \"/\" ", cmd);
694 imap_strout(fl->f_name);
704 * Back end for imap_lsub()
706 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
707 * may or may not be the desired behavior in the future.
709 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data)
715 pattern = (char *) data;
717 /* Only list rooms to which the user has access!! */
718 ra = CtdlRoomAccess(qrbuf, &CC->user);
720 imap_mailboxname(buf, sizeof buf, qrbuf);
721 if (imap_mailbox_matches_pattern(pattern, buf)) {
722 cprintf("* LSUB () \"/\" ");
731 * Implements the LSUB command
733 void imap_lsub(int num_parms, char *parms[])
737 cprintf("%s BAD arguments invalid\r\n", parms[0]);
740 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
742 if (strlen(parms[3]) == 0) {
743 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
747 imap_list_floors("LSUB", pattern);
748 ForEachRoom(imap_lsub_listroom, pattern);
751 cprintf("%s OK LSUB completed\r\n", parms[0]);
757 * Back end for imap_list()
759 void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
765 pattern = (char *) data;
767 /* Only list rooms to which the user has access!! */
768 ra = CtdlRoomAccess(qrbuf, &CC->user);
770 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
771 imap_mailboxname(buf, sizeof buf, qrbuf);
772 if (imap_mailbox_matches_pattern(pattern, buf)) {
773 cprintf("* LIST () \"/\" ");
782 * Implements the LIST command
784 void imap_list(int num_parms, char *parms[])
788 cprintf("%s BAD arguments invalid\r\n", parms[0]);
791 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
793 if (strlen(parms[3]) == 0) {
794 cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
798 imap_list_floors("LIST", pattern);
799 ForEachRoom(imap_list_listroom, pattern);
802 cprintf("%s OK LIST completed\r\n", parms[0]);
808 * Implements the CREATE command
811 void imap_create(int num_parms, char *parms[])
814 char roomname[ROOMNAMELEN];
819 if (strchr(parms[2], '\\') != NULL) {
820 cprintf("%s NO Invalid character in folder name\r\n",
822 lprintf(CTDL_DEBUG, "invalid character in folder name\n");
826 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
828 cprintf("%s NO Invalid mailbox name or location\r\n",
830 lprintf(CTDL_DEBUG, "invalid mailbox name or location\n");
833 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
834 flags = (ret & 0xff00); /* upper 8 bits = flags */
836 if (flags & IR_MAILBOX) {
837 if (strncasecmp(parms[2], "INBOX/", 6)) {
838 cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
839 lprintf(CTDL_DEBUG, "not subordinate to inbox\n");
844 if (flags & IR_MAILBOX) {
845 newroomtype = 4; /* private mailbox */
847 newroomtype = 0; /* public folder */
850 lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
851 roomname, floornum, newroomtype);
853 ret = create_room(roomname, newroomtype, "", floornum, 1, 0, VIEW_BBS);
856 ("%s NO Mailbox already exists, or create failed\r\n",
859 cprintf("%s OK CREATE completed\r\n", parms[0]);
861 lprintf(CTDL_DEBUG, "imap_create() completed\n");
866 * Locate a room by its IMAP folder name, and check access to it
868 int imap_grabroom(char *returned_roomname, char *foldername)
871 char augmented_roomname[ROOMNAMELEN];
872 char roomname[ROOMNAMELEN];
874 struct ctdlroom QRscratch;
878 ret = imap_roomname(roomname, sizeof roomname, foldername);
883 /* First try a regular match */
884 c = getroom(&QRscratch, roomname);
886 /* Then try a mailbox name match */
888 MailboxName(augmented_roomname, sizeof augmented_roomname,
889 &CC->user, roomname);
890 c = getroom(&QRscratch, augmented_roomname);
892 strcpy(roomname, augmented_roomname);
895 /* If the room exists, check security/access */
897 /* See if there is an existing user/room relationship */
898 ra = CtdlRoomAccess(&QRscratch, &CC->user);
900 /* normal clients have to pass through security */
906 /* Fail here if no such room */
908 strcpy(returned_roomname, "");
911 strcpy(returned_roomname, QRscratch.QRname);
918 * Implements the STATUS command (sort of)
921 void imap_status(int num_parms, char *parms[])
924 char roomname[ROOMNAMELEN];
926 char savedroom[ROOMNAMELEN];
929 ret = imap_grabroom(roomname, parms[2]);
932 ("%s NO Invalid mailbox name or location, or access denied\r\n",
938 * usergoto() formally takes us to the desired room, happily returning
939 * the number of messages and number of new messages. (If another
940 * folder is selected, save its name so we can return there!!!!!)
942 if (IMAP->selected) {
943 strcpy(savedroom, CC->room.QRname);
945 usergoto(roomname, 0, 0, &msgs, &new);
948 * Tell the client what it wants to know. In fact, tell it *more* than
949 * it wants to know. We happily IGnore the supplied status data item
950 * names and simply spew all possible data items. It's far easier to
951 * code and probably saves us some processing time too.
953 imap_mailboxname(buf, sizeof buf, &CC->room);
954 cprintf("* STATUS ");
956 cprintf(" (MESSAGES %d ", msgs);
957 cprintf("RECENT %d ", new); /* Initially, new==recent */
958 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
959 cprintf("UNSEEN %d)\r\n", new);
962 * If another folder is selected, go back to that room so we can resume
963 * our happy day without violent explosions.
965 if (IMAP->selected) {
966 usergoto(savedroom, 0, 0, &msgs, &new);
970 * Oooh, look, we're done!
972 cprintf("%s OK STATUS completed\r\n", parms[0]);
978 * Implements the SUBSCRIBE command
981 void imap_subscribe(int num_parms, char *parms[])
984 char roomname[ROOMNAMELEN];
985 char savedroom[ROOMNAMELEN];
988 ret = imap_grabroom(roomname, parms[2]);
991 ("%s NO Invalid mailbox name or location, or access denied\r\n",
997 * usergoto() formally takes us to the desired room, which has the side
998 * effect of marking the room as not-zapped ... exactly the effect
1001 if (IMAP->selected) {
1002 strcpy(savedroom, CC->room.QRname);
1004 usergoto(roomname, 0, 0, &msgs, &new);
1007 * If another folder is selected, go back to that room so we can resume
1008 * our happy day without violent explosions.
1010 if (IMAP->selected) {
1011 usergoto(savedroom, 0, 0, &msgs, &new);
1014 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1019 * Implements the UNSUBSCRIBE command
1022 void imap_unsubscribe(int num_parms, char *parms[])
1025 char roomname[ROOMNAMELEN];
1026 char savedroom[ROOMNAMELEN];
1029 ret = imap_grabroom(roomname, parms[2]);
1032 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1038 * usergoto() formally takes us to the desired room.
1040 if (IMAP->selected) {
1041 strcpy(savedroom, CC->room.QRname);
1043 usergoto(roomname, 0, 0, &msgs, &new);
1046 * Now make the API call to zap the room
1048 if (CtdlForgetThisRoom() == 0) {
1049 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1052 ("%s NO You may not unsubscribe from this folder.\r\n",
1057 * If another folder is selected, go back to that room so we can resume
1058 * our happy day without violent explosions.
1060 if (IMAP->selected) {
1061 usergoto(savedroom, 0, 0, &msgs, &new);
1068 * Implements the DELETE command
1071 void imap_delete(int num_parms, char *parms[])
1074 char roomname[ROOMNAMELEN];
1075 char savedroom[ROOMNAMELEN];
1078 ret = imap_grabroom(roomname, parms[2]);
1080 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1086 * usergoto() formally takes us to the desired room, happily returning
1087 * the number of messages and number of new messages. (If another
1088 * folder is selected, save its name so we can return there!!!!!)
1090 if (IMAP->selected) {
1091 strcpy(savedroom, CC->room.QRname);
1093 usergoto(roomname, 0, 0, &msgs, &new);
1096 * Now delete the room.
1098 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1099 cprintf("%s OK DELETE completed\r\n", parms[0]);
1100 delete_room(&CC->room);
1102 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1106 * If another folder is selected, go back to that room so we can resume
1107 * our happy day without violent explosions.
1109 if (IMAP->selected) {
1110 usergoto(savedroom, 0, 0, &msgs, &new);
1116 * Back end function for imap_rename()
1118 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1120 char foldername[SIZ];
1121 char newfoldername[SIZ];
1122 char newroomname[ROOMNAMELEN];
1124 struct irl *irlp = NULL; /* scratch pointer */
1125 struct irlparms *irlparms;
1127 irlparms = (struct irlparms *) data;
1128 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1130 /* Rename subfolders */
1131 if ((!strncasecmp(foldername, irlparms->oldname,
1132 strlen(irlparms->oldname))
1133 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1135 sprintf(newfoldername, "%s/%s",
1137 &foldername[strlen(irlparms->oldname) + 1]
1140 newfloor = imap_roomname(newroomname,
1142 newfoldername) & 0xFF;
1144 irlp = (struct irl *) malloc(sizeof(struct irl));
1145 strcpy(irlp->irl_newroom, newroomname);
1146 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1147 irlp->irl_newfloor = newfloor;
1148 irlp->next = *(irlparms->irl);
1149 *(irlparms->irl) = irlp;
1155 * Implements the RENAME command
1158 void imap_rename(int num_parms, char *parms[])
1160 char old_room[ROOMNAMELEN];
1161 char new_room[ROOMNAMELEN];
1165 struct irl *irl = NULL; /* the list */
1166 struct irl *irlp = NULL; /* scratch pointer */
1167 struct irlparms irlparms;
1169 if (strchr(parms[3], '\\') != NULL) {
1170 cprintf("%s NO Invalid character in folder name\r\n",
1175 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1176 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1177 new_floor = (newr & 0xFF);
1179 r = CtdlRenameRoom(old_room, new_room, new_floor);
1181 if (r == crr_room_not_found) {
1182 cprintf("%s NO Could not locate this folder\r\n",
1186 if (r == crr_already_exists) {
1187 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1190 if (r == crr_noneditable) {
1191 cprintf("%s This folder is not editable.\r\n", parms[0]);
1194 if (r == crr_invalid_floor) {
1195 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1198 if (r == crr_access_denied) {
1199 cprintf("%s You do not have permission to edit "
1200 "this folder.\r\n", parms[0]);
1204 cprintf("%s NO Rename failed - undefined error %d\r\n",
1210 /* If this is the INBOX, then RFC2060 says we have to just move the
1211 * contents. In a Citadel environment it's easier to rename the room
1212 * (already did that) and create a new inbox.
1214 if (!strcasecmp(parms[2], "INBOX")) {
1215 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_BBS);
1218 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1220 irlparms.oldname = parms[2];
1221 irlparms.newname = parms[3];
1222 irlparms.irl = &irl;
1223 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1225 /* ... and now rename them. */
1226 while (irl != NULL) {
1227 r = CtdlRenameRoom(irl->irl_oldroom,
1231 /* FIXME handle error returns better */
1232 lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
1241 cprintf("%s OK RENAME completed\r\n", parms[0]);
1248 * Main command loop for IMAP sessions.
1250 void imap_command_loop(void)
1257 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1259 if (client_gets(cmdbuf) < 1) {
1260 lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
1265 lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
1266 while (strlen(cmdbuf) < 5)
1267 strcat(cmdbuf, " ");
1270 /* strip off l/t whitespace and CRLF */
1271 if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1272 cmdbuf[strlen(cmdbuf) - 1] = 0;
1273 if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1274 cmdbuf[strlen(cmdbuf) - 1] = 0;
1277 /* If we're in the middle of a multi-line command, handle that */
1278 if (IMAP->authstate == imap_as_expecting_username) {
1279 imap_auth_login_user(cmdbuf);
1282 if (IMAP->authstate == imap_as_expecting_password) {
1283 imap_auth_login_pass(cmdbuf);
1288 /* Ok, at this point we're in normal command mode. The first thing
1289 * we do is print any incoming pages (yeah! we really do!)
1291 imap_print_instant_messages();
1294 * Before processing the command that was just entered... if we happen
1295 * to have a folder selected, we'd like to rescan that folder for new
1296 * messages, and for deletions/changes of existing messages. This
1297 * could probably be optimized somehow, but IMAP sucks...
1299 if (IMAP->selected) {
1300 imap_rescan_msgids();
1303 /* Now for the command set. */
1305 /* Grab the tag, command, and parameters. Check syntax. */
1306 num_parms = imap_parameterize(parms, cmdbuf);
1307 if (num_parms < 2) {
1308 cprintf("BAD syntax error\r\n");
1311 /* The commands below may be executed in any state */
1313 else if ((!strcasecmp(parms[1], "NOOP"))
1314 || (!strcasecmp(parms[1], "CHECK"))) {
1315 cprintf("%s OK This command successfully did nothing.\r\n",
1319 else if (!strcasecmp(parms[1], "LOGOUT")) {
1320 if (IMAP->selected) {
1321 imap_do_expunge(); /* yes, we auto-expunge */
1323 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1324 cprintf("%s OK thank you for using Citadel IMAP\r\n",
1330 else if (!strcasecmp(parms[1], "LOGIN")) {
1331 imap_login(num_parms, parms);
1334 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1335 imap_authenticate(num_parms, parms);
1338 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1339 imap_capability(num_parms, parms);
1342 else if (!strcasecmp(parms[1], "STARTTLS")) {
1343 imap_starttls(num_parms, parms);
1346 else if (!CC->logged_in) {
1347 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1350 /* The commans below require a logged-in state */
1352 else if (!strcasecmp(parms[1], "SELECT")) {
1353 imap_select(num_parms, parms);
1356 else if (!strcasecmp(parms[1], "EXAMINE")) {
1357 imap_select(num_parms, parms);
1360 else if (!strcasecmp(parms[1], "LSUB")) {
1361 imap_lsub(num_parms, parms);
1364 else if (!strcasecmp(parms[1], "LIST")) {
1365 imap_list(num_parms, parms);
1368 else if (!strcasecmp(parms[1], "CREATE")) {
1369 imap_create(num_parms, parms);
1372 else if (!strcasecmp(parms[1], "DELETE")) {
1373 imap_delete(num_parms, parms);
1376 else if (!strcasecmp(parms[1], "RENAME")) {
1377 imap_rename(num_parms, parms);
1380 else if (!strcasecmp(parms[1], "STATUS")) {
1381 imap_status(num_parms, parms);
1384 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1385 imap_subscribe(num_parms, parms);
1388 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1389 imap_unsubscribe(num_parms, parms);
1392 else if (!strcasecmp(parms[1], "APPEND")) {
1393 imap_append(num_parms, parms);
1396 else if (!strcasecmp(parms[1], "NAMESPACE")) {
1397 imap_namespace(num_parms, parms);
1400 else if (IMAP->selected == 0) {
1401 cprintf("%s BAD no folder selected\r\n", parms[0]);
1404 /* The commands below require the SELECT state on a mailbox */
1406 else if (!strcasecmp(parms[1], "FETCH")) {
1407 imap_fetch(num_parms, parms);
1410 else if ((!strcasecmp(parms[1], "UID"))
1411 && (!strcasecmp(parms[2], "FETCH"))) {
1412 imap_uidfetch(num_parms, parms);
1415 else if (!strcasecmp(parms[1], "SEARCH")) {
1416 imap_search(num_parms, parms);
1419 else if ((!strcasecmp(parms[1], "UID"))
1420 && (!strcasecmp(parms[2], "SEARCH"))) {
1421 imap_uidsearch(num_parms, parms);
1424 else if (!strcasecmp(parms[1], "STORE")) {
1425 imap_store(num_parms, parms);
1428 else if ((!strcasecmp(parms[1], "UID"))
1429 && (!strcasecmp(parms[2], "STORE"))) {
1430 imap_uidstore(num_parms, parms);
1433 else if (!strcasecmp(parms[1], "COPY")) {
1434 imap_copy(num_parms, parms);
1437 else if ((!strcasecmp(parms[1], "UID"))
1438 && (!strcasecmp(parms[2], "COPY"))) {
1439 imap_uidcopy(num_parms, parms);
1442 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1443 imap_expunge(num_parms, parms);
1446 else if (!strcasecmp(parms[1], "CLOSE")) {
1447 imap_close(num_parms, parms);
1450 /* End of commands. If we get here, the command is either invalid
1455 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1458 /* If the client transmitted a message we can free it now */
1459 imap_free_transmitted_message();
1465 * This function is called to register the IMAP extension with Citadel.
1467 char *serv_imap_init(void)
1469 CtdlRegisterServiceHook(config.c_imap_port,
1470 NULL, imap_greeting, imap_command_loop, NULL);
1471 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);