4 * IMAP server for the Citadel/UX 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: this is a workable implementation, but it could use some
9 * additional tweaking. Some commands may be implemented incompletely. The
10 * 'SEARCH' command is not implemented at all.
12 * WARNING: Mark Crispin is an idiot. IMAP is the most brain-damaged protocol
13 * you will ever have the profound lack of pleasure to encounter.
24 #include <sys/types.h>
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
31 # include <sys/time.h>
43 #include <openssl/ssl.h>
44 #include <openssl/err.h>
45 #include <openssl/rand.h>
50 #include "sysdep_decls.h"
51 #include "citserver.h"
54 #include "serv_extensions.h"
61 #include "internet_addressing.h"
62 #include "serv_imap.h"
63 #include "imap_tools.h"
64 #include "imap_fetch.h"
65 #include "imap_search.h"
66 #include "imap_store.h"
67 #include "imap_misc.h"
70 #include "serv_crypto.h"
73 /* imap_rename() uses this struct containing list of rooms to rename */
76 char irl_oldroom[ROOMNAMELEN];
77 char irl_newroom[ROOMNAMELEN];
81 /* Data which is passed between imap_rename() and imap_rename_backend() */
92 * If there is a message ID map in memory, free it
94 void imap_free_msgids(void) {
95 if (IMAP->msgids != NULL) {
100 if (IMAP->flags != NULL) {
108 * If there is a transmitted message in memory, free it
110 void imap_free_transmitted_message(void) {
111 if (IMAP->transmitted_message != NULL) {
112 phree(IMAP->transmitted_message);
113 IMAP->transmitted_message = NULL;
114 IMAP->transmitted_length = 0;
120 * Set the \\Seen flag for messages which aren't new
122 void imap_set_seen_flags(void) {
126 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
127 if (IMAP->num_msgs > 0) {
128 for (i=0; i<IMAP->num_msgs; ++i) {
129 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
130 IMAP->flags[i] |= IMAP_SEEN;
140 * Back end for imap_load_msgids()
142 * Optimization: instead of calling realloc() to add each message, we
143 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
144 * allows the mapping to proceed much faster.
146 void imap_add_single_msgid(long msgnum, void *userdata) {
148 IMAP->num_msgs = IMAP->num_msgs + 1;
149 if (IMAP->msgids == NULL) {
150 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
151 * REALLOC_INCREMENT);
153 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
154 IMAP->msgids = reallok(IMAP->msgids,
155 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
157 if (IMAP->flags == NULL) {
158 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
159 * REALLOC_INCREMENT);
161 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
162 IMAP->flags = reallok(IMAP->flags,
163 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
165 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
166 IMAP->flags[IMAP->num_msgs - 1] = 0;
172 * Set up a message ID map for the current room (folder)
174 void imap_load_msgids(void) {
176 if (IMAP->selected == 0) {
177 lprintf(5, "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(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
192 * Re-scan the selected room (folder) and see if it's been changed at all
194 void imap_rescan_msgids(void) {
196 int original_num_msgs = 0;
197 long original_highest = 0L;
199 int message_still_exists;
200 struct cdbdata *cdbfr;
201 long *msglist = NULL;
205 if (IMAP->selected == 0) {
206 lprintf(5, "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 = mallok(cdbfr->len);
216 memcpy(msglist, cdbfr->ptr, cdbfr->len);
217 num_msgs = cdbfr->len / sizeof(long);
225 * Check to see if any of the messages we know about have been expunged
227 if (IMAP->num_msgs > 0)
228 for (i=0; i<IMAP->num_msgs; ++i) {
230 message_still_exists = 0;
231 if (num_msgs > 0) 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], &IMAP->msgids[i+1],
246 (sizeof(long)*(IMAP->num_msgs-i)) );
247 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
248 (sizeof(long)*(IMAP->num_msgs-i)) );
256 * Remember how many messages were here before we re-scanned.
258 original_num_msgs = IMAP->num_msgs;
259 if (IMAP->num_msgs > 0) {
260 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
263 original_highest = 0L;
267 * Now peruse the room for *new* messages only.
269 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
270 if (msglist[j] > original_highest) {
271 imap_add_single_msgid(msglist[j], NULL);
274 imap_set_seen_flags();
277 * If new messages have arrived, tell the client about them.
279 if (IMAP->num_msgs > original_num_msgs) {
280 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
283 if (num_msgs != 0) phree(msglist);
293 * This cleanup function blows away the temporary memory and files used by
296 void imap_cleanup_function(void) {
298 /* Don't do this stuff if this is not a IMAP session! */
299 if (CC->h_command_function != imap_command_loop) return;
301 lprintf(9, "Performing IMAP cleanup hook\n");
303 imap_free_transmitted_message();
304 lprintf(9, "Finished IMAP cleanup hook\n");
310 * Here's where our IMAP session begins its happy day.
312 void imap_greeting(void) {
314 strcpy(CC->cs_clientname, "IMAP session");
315 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
316 IMAP->authstate = imap_as_normal;
318 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
324 * implements the LOGIN command (ordinary username/password login)
326 void imap_login(int num_parms, char *parms[]) {
327 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
328 if (CtdlTryPassword(parms[3]) == pass_ok) {
329 cprintf("%s OK login successful\r\n", parms[0]);
334 cprintf("%s BAD Login incorrect\r\n", parms[0]);
339 * Implements the AUTHENTICATE command
341 void imap_authenticate(int num_parms, char *parms[]) {
344 if (num_parms != 3) {
345 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
349 if (!strcasecmp(parms[2], "LOGIN")) {
350 CtdlEncodeBase64(buf, "Username:", 9);
351 cprintf("+ %s\r\n", buf);
352 IMAP->authstate = imap_as_expecting_username;
353 strcpy(IMAP->authseq, parms[0]);
358 cprintf("%s NO AUTHENTICATE %s failed\r\n",
363 void imap_auth_login_user(char *cmd) {
366 CtdlDecodeBase64(buf, cmd, SIZ);
367 CtdlLoginExistingUser(buf);
368 CtdlEncodeBase64(buf, "Password:", 9);
369 cprintf("+ %s\r\n", buf);
370 IMAP->authstate = imap_as_expecting_password;
374 void imap_auth_login_pass(char *cmd) {
377 CtdlDecodeBase64(buf, cmd, SIZ);
378 if (CtdlTryPassword(buf) == pass_ok) {
379 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
382 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
384 IMAP->authstate = imap_as_normal;
391 * implements the CAPABILITY command
393 void imap_capability(int num_parms, char *parms[]) {
394 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
396 cprintf(" STARTTLS");
399 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
404 * implements the STARTTLS command
407 void imap_starttls(int num_parms, char *parms[]) {
408 int retval, bits, alg_bits;
411 cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
414 if (!(CC->ssl = SSL_new(ssl_ctx))) {
415 lprintf(2, "SSL_new failed: %s\n",
416 ERR_reason_error_string(ERR_peek_error()));
417 cprintf("%s NO SSL_new: %s\r\n", parms[0],
418 ERR_reason_error_string(ERR_get_error()));
421 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
422 lprintf(2, "SSL_set_fd failed: %s\n",
423 ERR_reason_error_string(ERR_peek_error()));
426 cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
427 ERR_reason_error_string(ERR_get_error()));
430 cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
431 retval = SSL_accept(CC->ssl);
434 * Can't notify the client of an error here; they will
435 * discover the problem at the SSL layer and should
436 * revert to unencrypted communications.
440 errval = SSL_get_error(CC->ssl, retval);
441 lprintf(2, "SSL_accept failed: %s\n",
442 ERR_reason_error_string(ERR_get_error()));
447 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
448 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
449 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
450 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
451 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
453 CC->redirect_ssl = 1;
460 * implements the SELECT command
462 void imap_select(int num_parms, char *parms[]) {
464 char augmented_roomname[ROOMNAMELEN];
468 struct ctdlroom QRscratch;
474 /* Convert the supplied folder name to a roomname */
475 i = imap_roomname(towhere, sizeof towhere, parms[2]);
477 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
481 floornum = (i & 0x00ff);
482 roomflags = (i & 0xff00);
484 /* First try a regular match */
485 c = getroom(&QRscratch, towhere);
487 /* Then try a mailbox name match */
489 MailboxName(augmented_roomname, sizeof augmented_roomname,
491 c = getroom(&QRscratch, augmented_roomname);
493 strcpy(towhere, augmented_roomname);
496 /* If the room exists, check security/access */
498 /* See if there is an existing user/room relationship */
499 ra = CtdlRoomAccess(&QRscratch, &CC->user);
501 /* normal clients have to pass through security */
507 /* Fail here if no such room */
509 cprintf("%s NO ... no such room, or access denied\r\n",
516 * usergoto() formally takes us to the desired room, happily returning
517 * the number of messages and number of new messages.
519 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
520 usergoto(NULL, 0, 0, &msgs, &new);
523 if (!strcasecmp(parms[1], "EXAMINE")) {
532 /* FIXME ... much more info needs to be supplied here */
533 cprintf("* %d EXISTS\r\n", msgs);
534 cprintf("* %d RECENT\r\n", new);
535 cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
536 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
537 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
538 cprintf("%s OK [%s] %s completed\r\n",
540 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
547 * does the real work for expunge
549 int imap_do_expunge(void) {
551 int num_expunged = 0;
553 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
554 if (IMAP->flags[i] & IMAP_DELETED) {
555 CtdlDeleteMessages(CC->room.QRname,
556 IMAP->msgids[i], "");
558 lprintf(9, "%ld ... deleted\n", IMAP->msgids[i]);
561 lprintf(9, "%ld ... not deleted\n", IMAP->msgids[i]);
565 if (num_expunged > 0) {
566 imap_rescan_msgids();
569 return(num_expunged);
574 * implements the EXPUNGE command syntax
576 void imap_expunge(int num_parms, char *parms[]) {
577 int num_expunged = 0;
579 num_expunged = imap_do_expunge();
580 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
585 * implements the CLOSE command
587 void imap_close(int num_parms, char *parms[]) {
589 /* Yes, we always expunge on close. */
595 cprintf("%s OK CLOSE completed\r\n", parms[0]);
602 * Used by LIST and LSUB to show the floors in the listing
604 void imap_list_floors(char *cmd, char *pattern) {
608 for (i=0; i<MAXFLOORS; ++i) {
610 if (fl->f_flags & F_INUSE) {
611 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
612 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
613 imap_strout(fl->f_name);
623 * Back end for imap_lsub()
625 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
626 * may or may not be the desired behavior in the future.
628 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data) {
633 pattern = (char *)data;
635 /* Only list rooms to which the user has access!! */
636 ra = CtdlRoomAccess(qrbuf, &CC->user);
638 imap_mailboxname(buf, sizeof buf, qrbuf);
639 if (imap_mailbox_matches_pattern(pattern, buf)) {
640 cprintf("* LSUB () \"|\" ");
649 * Implements the LSUB command
651 void imap_lsub(int num_parms, char *parms[]) {
654 cprintf("%s BAD arguments invalid\r\n", parms[0]);
657 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
659 if (strlen(parms[3])==0) {
660 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
664 imap_list_floors("LSUB", pattern);
665 ForEachRoom(imap_lsub_listroom, pattern);
668 cprintf("%s OK LSUB completed\r\n", parms[0]);
674 * Back end for imap_list()
676 void imap_list_listroom(struct ctdlroom *qrbuf, void *data) {
681 pattern = (char *)data;
683 /* Only list rooms to which the user has access!! */
684 ra = CtdlRoomAccess(qrbuf, &CC->user);
686 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
687 imap_mailboxname(buf, sizeof buf, qrbuf);
688 if (imap_mailbox_matches_pattern(pattern, buf)) {
689 cprintf("* LIST () \"|\" ");
698 * Implements the LIST command
700 void imap_list(int num_parms, char *parms[]) {
703 cprintf("%s BAD arguments invalid\r\n", parms[0]);
706 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
708 if (strlen(parms[3])==0) {
709 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
713 imap_list_floors("LIST", pattern);
714 ForEachRoom(imap_list_listroom, pattern);
717 cprintf("%s OK LIST completed\r\n", parms[0]);
723 * Implements the CREATE command
726 void imap_create(int num_parms, char *parms[]) {
728 char roomname[ROOMNAMELEN];
733 if (strchr(parms[2], '\\') != NULL) {
734 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
738 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
740 cprintf("%s NO Invalid mailbox name or location\r\n",
744 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
745 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
747 if (flags & IR_MAILBOX) {
748 newroomtype = 4; /* private mailbox */
751 newroomtype = 0; /* public folder */
754 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
755 roomname, floornum, newroomtype);
757 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
759 cprintf("%s NO Mailbox already exists, or create failed\r\n",
763 cprintf("%s OK CREATE completed\r\n", parms[0]);
769 * Locate a room by its IMAP folder name, and check access to it
771 int imap_grabroom(char *returned_roomname, char *foldername) {
773 char augmented_roomname[ROOMNAMELEN];
774 char roomname[ROOMNAMELEN];
776 struct ctdlroom QRscratch;
780 ret = imap_roomname(roomname, sizeof roomname, foldername);
785 /* First try a regular match */
786 c = getroom(&QRscratch, roomname);
788 /* Then try a mailbox name match */
790 MailboxName(augmented_roomname, sizeof augmented_roomname,
791 &CC->user, roomname);
792 c = getroom(&QRscratch, augmented_roomname);
794 strcpy(roomname, augmented_roomname);
797 /* If the room exists, check security/access */
799 /* See if there is an existing user/room relationship */
800 ra = CtdlRoomAccess(&QRscratch, &CC->user);
802 /* normal clients have to pass through security */
808 /* Fail here if no such room */
810 strcpy(returned_roomname, "");
814 strcpy(returned_roomname, QRscratch.QRname);
821 * Implements the STATUS command (sort of)
824 void imap_status(int num_parms, char *parms[]) {
826 char roomname[ROOMNAMELEN];
828 char savedroom[ROOMNAMELEN];
831 ret = imap_grabroom(roomname, parms[2]);
833 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
839 * usergoto() formally takes us to the desired room, happily returning
840 * the number of messages and number of new messages. (If another
841 * folder is selected, save its name so we can return there!!!!!)
843 if (IMAP->selected) {
844 strcpy(savedroom, CC->room.QRname);
846 usergoto(roomname, 0, 0, &msgs, &new);
849 * Tell the client what it wants to know. In fact, tell it *more* than
850 * it wants to know. We happily IGnore the supplied status data item
851 * names and simply spew all possible data items. It's far easier to
852 * code and probably saves us some processing time too.
854 imap_mailboxname(buf, sizeof buf, &CC->room);
855 cprintf("* STATUS ");
857 cprintf(" (MESSAGES %d ", msgs);
858 cprintf("RECENT 0 "); /* FIXME we need to implement this */
859 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
860 cprintf("UNSEEN %d)\r\n", new);
863 * If another folder is selected, go back to that room so we can resume
864 * our happy day without violent explosions.
866 if (IMAP->selected) {
867 usergoto(savedroom, 0, 0, &msgs, &new);
871 * Oooh, look, we're done!
873 cprintf("%s OK STATUS completed\r\n", parms[0]);
879 * Implements the SUBSCRIBE command
882 void imap_subscribe(int num_parms, char *parms[]) {
884 char roomname[ROOMNAMELEN];
885 char savedroom[ROOMNAMELEN];
888 ret = imap_grabroom(roomname, parms[2]);
890 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
896 * usergoto() formally takes us to the desired room, which has the side
897 * effect of marking the room as not-zapped ... exactly the effect
900 if (IMAP->selected) {
901 strcpy(savedroom, CC->room.QRname);
903 usergoto(roomname, 0, 0, &msgs, &new);
906 * If another folder is selected, go back to that room so we can resume
907 * our happy day without violent explosions.
909 if (IMAP->selected) {
910 usergoto(savedroom, 0, 0, &msgs, &new);
913 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
918 * Implements the UNSUBSCRIBE command
921 void imap_unsubscribe(int num_parms, char *parms[]) {
923 char roomname[ROOMNAMELEN];
924 char savedroom[ROOMNAMELEN];
927 ret = imap_grabroom(roomname, parms[2]);
929 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
935 * usergoto() formally takes us to the desired room.
937 if (IMAP->selected) {
938 strcpy(savedroom, CC->room.QRname);
940 usergoto(roomname, 0, 0, &msgs, &new);
943 * Now make the API call to zap the room
945 if (CtdlForgetThisRoom() == 0) {
946 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
949 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
954 * If another folder is selected, go back to that room so we can resume
955 * our happy day without violent explosions.
957 if (IMAP->selected) {
958 usergoto(savedroom, 0, 0, &msgs, &new);
965 * Implements the DELETE command
968 void imap_delete(int num_parms, char *parms[]) {
970 char roomname[ROOMNAMELEN];
971 char savedroom[ROOMNAMELEN];
974 ret = imap_grabroom(roomname, parms[2]);
976 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
982 * usergoto() formally takes us to the desired room, happily returning
983 * the number of messages and number of new messages. (If another
984 * folder is selected, save its name so we can return there!!!!!)
986 if (IMAP->selected) {
987 strcpy(savedroom, CC->room.QRname);
989 usergoto(roomname, 0, 0, &msgs, &new);
992 * Now delete the room.
994 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
995 cprintf("%s OK DELETE completed\r\n", parms[0]);
996 delete_room(&CC->room);
999 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1003 * If another folder is selected, go back to that room so we can resume
1004 * our happy day without violent explosions.
1006 if (IMAP->selected) {
1007 usergoto(savedroom, 0, 0, &msgs, &new);
1013 * Back end function for imap_rename()
1015 void imap_rename_backend(struct ctdlroom *qrbuf, void *data) {
1016 char foldername[SIZ];
1017 char newfoldername[SIZ];
1018 char newroomname[ROOMNAMELEN];
1020 struct irl *irlp = NULL; /* scratch pointer */
1021 struct irlparms *irlparms;
1023 irlparms = (struct irlparms *)data;
1024 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1026 /* Rename subfolders */
1027 if ( (!strncasecmp(foldername, irlparms->oldname,
1028 strlen(irlparms->oldname))
1029 && (foldername[strlen(irlparms->oldname)] == '|')) ) {
1031 sprintf(newfoldername, "%s|%s",
1033 &foldername[strlen(irlparms->oldname)+1]
1036 newfloor = imap_roomname(newroomname,
1037 sizeof newroomname, newfoldername) & 0xFF;
1039 irlp = (struct irl *) mallok(sizeof(struct irl));
1040 strcpy(irlp->irl_newroom, newroomname);
1041 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1042 irlp->irl_newfloor = newfloor;
1043 irlp->next = *(irlparms->irl);
1044 *(irlparms->irl) = irlp;
1050 * Implements the RENAME command
1053 void imap_rename(int num_parms, char *parms[]) {
1054 char old_room[ROOMNAMELEN];
1055 char new_room[ROOMNAMELEN];
1059 struct irl *irl = NULL; /* the list */
1060 struct irl *irlp = NULL; /* scratch pointer */
1061 struct irlparms irlparms;
1063 if (strchr(parms[3], '\\') != NULL) {
1064 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
1068 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1069 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1070 new_floor = (newr & 0xFF);
1072 r = CtdlRenameRoom(old_room, new_room, new_floor);
1074 if (r == crr_room_not_found) {
1075 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
1078 if (r == crr_already_exists) {
1079 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1082 if (r == crr_noneditable) {
1083 cprintf("%s This folder is not editable.\r\n", parms[0]);
1086 if (r == crr_invalid_floor) {
1087 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1090 if (r == crr_access_denied) {
1091 cprintf("%s You do not have permission to edit "
1092 "this folder.\r\n", parms[0]);
1096 cprintf("%s NO Rename failed - undefined error %d\r\n",
1102 /* If this is the INBOX, then RFC2060 says we have to just move the
1103 * contents. In a Citadel environment it's easier to rename the room
1104 * (already did that) and create a new inbox.
1106 if (!strcasecmp(parms[2], "INBOX")) {
1107 create_room(MAILROOM, 4, "", 0, 1, 0);
1110 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1112 irlparms.oldname = parms[2];
1113 irlparms.newname = parms[3];
1114 irlparms.irl = &irl;
1115 ForEachRoom(imap_rename_backend, (void *)&irlparms);
1117 /* ... and now rename them. */
1118 while (irl != NULL) {
1119 r = CtdlRenameRoom(irl->irl_oldroom,
1120 irl->irl_newroom, irl->irl_newfloor);
1122 /* FIXME handle error returns better */
1123 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1131 cprintf("%s OK RENAME completed\r\n", parms[0]);
1138 * Main command loop for IMAP sessions.
1140 void imap_command_loop(void) {
1146 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1147 if (client_gets(cmdbuf) < 1) {
1148 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1153 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1154 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1157 /* strip off l/t whitespace and CRLF */
1158 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1159 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1162 /* If we're in the middle of a multi-line command, handle that */
1163 if (IMAP->authstate == imap_as_expecting_username) {
1164 imap_auth_login_user(cmdbuf);
1167 if (IMAP->authstate == imap_as_expecting_password) {
1168 imap_auth_login_pass(cmdbuf);
1173 /* Ok, at this point we're in normal command mode. The first thing
1174 * we do is print any incoming pages (yeah! we really do!)
1176 imap_print_express_messages();
1179 * Before processing the command that was just entered... if we happen
1180 * to have a folder selected, we'd like to rescan that folder for new
1181 * messages, and for deletions/changes of existing messages. This
1182 * could probably be optimized somehow, but IMAP sucks...
1184 if (IMAP->selected) {
1185 imap_rescan_msgids();
1188 /* Now for the command set. */
1190 /* Grab the tag, command, and parameters. Check syntax. */
1191 num_parms = imap_parameterize(parms, cmdbuf);
1192 if (num_parms < 2) {
1193 cprintf("BAD syntax error\r\n");
1196 /* The commands below may be executed in any state */
1198 else if ( (!strcasecmp(parms[1], "NOOP"))
1199 || (!strcasecmp(parms[1], "CHECK")) ) {
1200 cprintf("%s OK This command successfully did nothing.\r\n",
1204 else if (!strcasecmp(parms[1], "LOGOUT")) {
1205 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1206 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1211 else if (!strcasecmp(parms[1], "LOGIN")) {
1212 imap_login(num_parms, parms);
1215 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1216 imap_authenticate(num_parms, parms);
1219 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1220 imap_capability(num_parms, parms);
1224 else if (!strcasecmp(parms[1], "STARTTLS")) {
1225 imap_starttls(num_parms, parms);
1229 else if (!CC->logged_in) {
1230 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1233 /* The commans below require a logged-in state */
1235 else if (!strcasecmp(parms[1], "SELECT")) {
1236 imap_select(num_parms, parms);
1239 else if (!strcasecmp(parms[1], "EXAMINE")) {
1240 imap_select(num_parms, parms);
1243 else if (!strcasecmp(parms[1], "LSUB")) {
1244 imap_lsub(num_parms, parms);
1247 else if (!strcasecmp(parms[1], "LIST")) {
1248 imap_list(num_parms, parms);
1251 else if (!strcasecmp(parms[1], "CREATE")) {
1252 imap_create(num_parms, parms);
1255 else if (!strcasecmp(parms[1], "DELETE")) {
1256 imap_delete(num_parms, parms);
1259 else if (!strcasecmp(parms[1], "RENAME")) {
1260 imap_rename(num_parms, parms);
1263 else if (!strcasecmp(parms[1], "STATUS")) {
1264 imap_status(num_parms, parms);
1267 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1268 imap_subscribe(num_parms, parms);
1271 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1272 imap_unsubscribe(num_parms, parms);
1275 else if (!strcasecmp(parms[1], "APPEND")) {
1276 imap_append(num_parms, parms);
1279 else if (IMAP->selected == 0) {
1280 cprintf("%s BAD no folder selected\r\n", parms[0]);
1283 /* The commands below require the SELECT state on a mailbox */
1285 else if (!strcasecmp(parms[1], "FETCH")) {
1286 imap_fetch(num_parms, parms);
1289 else if ( (!strcasecmp(parms[1], "UID"))
1290 && (!strcasecmp(parms[2], "FETCH")) ) {
1291 imap_uidfetch(num_parms, parms);
1294 else if (!strcasecmp(parms[1], "SEARCH")) {
1295 imap_search(num_parms, parms);
1298 else if ( (!strcasecmp(parms[1], "UID"))
1299 && (!strcasecmp(parms[2], "SEARCH")) ) {
1300 imap_uidsearch(num_parms, parms);
1303 else if (!strcasecmp(parms[1], "STORE")) {
1304 imap_store(num_parms, parms);
1307 else if ( (!strcasecmp(parms[1], "UID"))
1308 && (!strcasecmp(parms[2], "STORE")) ) {
1309 imap_uidstore(num_parms, parms);
1312 else if (!strcasecmp(parms[1], "COPY")) {
1313 imap_copy(num_parms, parms);
1316 else if ( (!strcasecmp(parms[1], "UID"))
1317 && (!strcasecmp(parms[2], "COPY")) ) {
1318 imap_uidcopy(num_parms, parms);
1321 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1322 imap_expunge(num_parms, parms);
1325 else if (!strcasecmp(parms[1], "CLOSE")) {
1326 imap_close(num_parms, parms);
1329 /* End of commands. If we get here, the command is either invalid
1334 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1337 /* If the client transmitted a message we can free it now */
1338 imap_free_transmitted_message();
1344 * This function is called to register the IMAP extension with Citadel.
1346 char *serv_imap_init(void)
1348 SYM_IMAP = CtdlGetDynamicSymbol();
1349 CtdlRegisterServiceHook(config.c_imap_port,
1353 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);