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() */
90 * If there is a message ID map in memory, free it
92 void imap_free_msgids(void) {
93 if (IMAP->msgids != NULL) {
98 if (IMAP->flags != NULL) {
106 * If there is a transmitted message in memory, free it
108 void imap_free_transmitted_message(void) {
109 if (IMAP->transmitted_message != NULL) {
110 phree(IMAP->transmitted_message);
111 IMAP->transmitted_message = NULL;
112 IMAP->transmitted_length = 0;
118 * Set the \\Seen flag for messages which aren't new
120 void imap_set_seen_flags(void) {
124 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
125 if (IMAP->num_msgs > 0) {
126 for (i=0; i<IMAP->num_msgs; ++i) {
127 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
128 IMAP->flags[i] |= IMAP_SEEN;
138 * Back end for imap_load_msgids()
140 * Optimization: instead of calling realloc() to add each message, we
141 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
142 * allows the mapping to proceed much faster.
144 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 = mallok(IMAP->num_msgs * sizeof(long)
149 * REALLOC_INCREMENT);
151 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
152 IMAP->msgids = reallok(IMAP->msgids,
153 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
155 if (IMAP->flags == NULL) {
156 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
157 * REALLOC_INCREMENT);
159 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
160 IMAP->flags = reallok(IMAP->flags,
161 (IMAP->num_msgs + 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) {
174 if (IMAP->selected == 0) {
175 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
179 imap_free_msgids(); /* If there was already a map, free it */
181 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
182 imap_add_single_msgid, NULL);
184 imap_set_seen_flags();
185 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
190 * Re-scan the selected room (folder) and see if it's been changed at all
192 void imap_rescan_msgids(void) {
194 int original_num_msgs = 0;
195 long original_highest = 0L;
197 int message_still_exists;
198 struct cdbdata *cdbfr;
199 long *msglist = NULL;
203 if (IMAP->selected == 0) {
204 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
208 /* Load the *current* message list from disk, so we can compare it
209 * to what we have in memory.
211 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
213 msglist = mallok(cdbfr->len);
214 memcpy(msglist, cdbfr->ptr, cdbfr->len);
215 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;
229 if (num_msgs > 0) for (j = 0; j < num_msgs; ++j) {
230 if (msglist[j] == IMAP->msgids[i]) {
231 message_still_exists = 1;
235 if (message_still_exists == 0) {
236 cprintf("* %d EXPUNGE\r\n", i+1);
238 /* Here's some nice stupid nonsense. When a message
239 * is expunged, we have to slide all the existing
240 * messages up in the message array.
243 memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
244 (sizeof(long)*(IMAP->num_msgs-i)) );
245 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
246 (sizeof(long)*(IMAP->num_msgs-i)) );
254 * Remember how many messages were here before we re-scanned.
256 original_num_msgs = IMAP->num_msgs;
257 if (IMAP->num_msgs > 0) {
258 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
261 original_highest = 0L;
265 * Now peruse the room for *new* messages only.
267 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
268 if (msglist[j] > original_highest) {
269 imap_add_single_msgid(msglist[j], NULL);
272 imap_set_seen_flags();
275 * If new messages have arrived, tell the client about them.
277 if (IMAP->num_msgs > original_num_msgs) {
278 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
281 if (num_msgs != 0) phree(msglist);
291 * This cleanup function blows away the temporary memory and files used by
294 void imap_cleanup_function(void) {
296 /* Don't do this stuff if this is not a IMAP session! */
297 if (CC->h_command_function != imap_command_loop) return;
299 lprintf(9, "Performing IMAP cleanup hook\n");
301 imap_free_transmitted_message();
302 lprintf(9, "Finished IMAP cleanup hook\n");
308 * Here's where our IMAP session begins its happy day.
310 void imap_greeting(void) {
312 strcpy(CC->cs_clientname, "IMAP session");
313 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
314 IMAP->authstate = imap_as_normal;
316 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
322 * implements the LOGIN command (ordinary username/password login)
324 void imap_login(int num_parms, char *parms[]) {
325 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
326 if (CtdlTryPassword(parms[3]) == pass_ok) {
327 cprintf("%s OK login successful\r\n", parms[0]);
332 cprintf("%s BAD Login incorrect\r\n", parms[0]);
337 * Implements the AUTHENTICATE command
339 void imap_authenticate(int num_parms, char *parms[]) {
342 if (num_parms != 3) {
343 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
347 if (!strcasecmp(parms[2], "LOGIN")) {
348 CtdlEncodeBase64(buf, "Username:", 9);
349 cprintf("+ %s\r\n", buf);
350 IMAP->authstate = imap_as_expecting_username;
351 strcpy(IMAP->authseq, parms[0]);
356 cprintf("%s NO AUTHENTICATE %s failed\r\n",
361 void imap_auth_login_user(char *cmd) {
364 CtdlDecodeBase64(buf, cmd, SIZ);
365 CtdlLoginExistingUser(buf);
366 CtdlEncodeBase64(buf, "Password:", 9);
367 cprintf("+ %s\r\n", buf);
368 IMAP->authstate = imap_as_expecting_password;
372 void imap_auth_login_pass(char *cmd) {
375 CtdlDecodeBase64(buf, cmd, SIZ);
376 if (CtdlTryPassword(buf) == pass_ok) {
377 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
380 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
382 IMAP->authstate = imap_as_normal;
389 * implements the CAPABILITY command
391 void imap_capability(int num_parms, char *parms[]) {
392 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
394 cprintf(" STARTTLS");
397 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
402 * implements the STARTTLS command
405 void imap_starttls(int num_parms, char *parms[]) {
406 int retval, bits, alg_bits;
409 cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
412 if (!(CC->ssl = SSL_new(ssl_ctx))) {
413 lprintf(2, "SSL_new failed: %s\n",
414 ERR_reason_error_string(ERR_peek_error()));
415 cprintf("%s NO SSL_new: %s\r\n", parms[0],
416 ERR_reason_error_string(ERR_get_error()));
419 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
420 lprintf(2, "SSL_set_fd failed: %s\n",
421 ERR_reason_error_string(ERR_peek_error()));
424 cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
425 ERR_reason_error_string(ERR_get_error()));
428 cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
429 retval = SSL_accept(CC->ssl);
432 * Can't notify the client of an error here; they will
433 * discover the problem at the SSL layer and should
434 * revert to unencrypted communications.
438 errval = SSL_get_error(CC->ssl, retval);
439 lprintf(2, "SSL_accept failed: %s\n",
440 ERR_reason_error_string(ERR_get_error()));
445 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
446 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
447 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
448 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
449 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
451 CC->redirect_ssl = 1;
458 * implements the SELECT command
460 void imap_select(int num_parms, char *parms[]) {
462 char augmented_roomname[ROOMNAMELEN];
466 struct ctdlroom QRscratch;
472 /* Convert the supplied folder name to a roomname */
473 i = imap_roomname(towhere, sizeof towhere, parms[2]);
475 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
479 floornum = (i & 0x00ff);
480 roomflags = (i & 0xff00);
482 /* First try a regular match */
483 c = getroom(&QRscratch, towhere);
485 /* Then try a mailbox name match */
487 MailboxName(augmented_roomname, sizeof augmented_roomname,
489 c = getroom(&QRscratch, augmented_roomname);
491 strcpy(towhere, augmented_roomname);
494 /* If the room exists, check security/access */
496 /* See if there is an existing user/room relationship */
497 ra = CtdlRoomAccess(&QRscratch, &CC->user);
499 /* normal clients have to pass through security */
505 /* Fail here if no such room */
507 cprintf("%s NO ... no such room, or access denied\r\n",
514 * usergoto() formally takes us to the desired room, happily returning
515 * the number of messages and number of new messages.
517 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
518 usergoto(NULL, 0, 0, &msgs, &new);
521 if (!strcasecmp(parms[1], "EXAMINE")) {
530 /* FIXME ... much more info needs to be supplied here */
531 cprintf("* %d EXISTS\r\n", msgs);
532 cprintf("* %d RECENT\r\n", new);
533 cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
534 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
535 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
536 cprintf("%s OK [%s] %s completed\r\n",
538 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
545 * does the real work for expunge
547 int imap_do_expunge(void) {
549 int num_expunged = 0;
551 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
552 if (IMAP->flags[i] & IMAP_DELETED) {
553 CtdlDeleteMessages(CC->room.QRname,
554 IMAP->msgids[i], "");
556 lprintf(9, "%ld ... deleted\n", IMAP->msgids[i]);
559 lprintf(9, "%ld ... not deleted\n", IMAP->msgids[i]);
563 if (num_expunged > 0) {
564 imap_rescan_msgids();
567 return(num_expunged);
572 * implements the EXPUNGE command syntax
574 void imap_expunge(int num_parms, char *parms[]) {
575 int num_expunged = 0;
577 num_expunged = imap_do_expunge();
578 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
583 * implements the CLOSE command
585 void imap_close(int num_parms, char *parms[]) {
587 /* Yes, we always expunge on close. */
593 cprintf("%s OK CLOSE completed\r\n", parms[0]);
600 * Used by LIST and LSUB to show the floors in the listing
602 void imap_list_floors(char *cmd, char *pattern) {
606 for (i=0; i<MAXFLOORS; ++i) {
608 if (fl->f_flags & F_INUSE) {
609 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
610 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
611 imap_strout(fl->f_name);
621 * Back end for imap_lsub()
623 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
624 * may or may not be the desired behavior in the future.
626 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data) {
631 pattern = (char *)data;
633 /* Only list rooms to which the user has access!! */
634 ra = CtdlRoomAccess(qrbuf, &CC->user);
636 imap_mailboxname(buf, sizeof buf, qrbuf);
637 if (imap_mailbox_matches_pattern(pattern, buf)) {
638 cprintf("* LSUB () \"|\" ");
647 * Implements the LSUB command
649 void imap_lsub(int num_parms, char *parms[]) {
652 cprintf("%s BAD arguments invalid\r\n", parms[0]);
655 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
657 if (strlen(parms[3])==0) {
658 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
662 imap_list_floors("LSUB", pattern);
663 ForEachRoom(imap_lsub_listroom, pattern);
666 cprintf("%s OK LSUB completed\r\n", parms[0]);
672 * Back end for imap_list()
674 void imap_list_listroom(struct ctdlroom *qrbuf, void *data) {
679 pattern = (char *)data;
681 /* Only list rooms to which the user has access!! */
682 ra = CtdlRoomAccess(qrbuf, &CC->user);
684 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
685 imap_mailboxname(buf, sizeof buf, qrbuf);
686 if (imap_mailbox_matches_pattern(pattern, buf)) {
687 cprintf("* LIST () \"|\" ");
696 * Implements the LIST command
698 void imap_list(int num_parms, char *parms[]) {
701 cprintf("%s BAD arguments invalid\r\n", parms[0]);
704 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
706 if (strlen(parms[3])==0) {
707 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
711 imap_list_floors("LIST", pattern);
712 ForEachRoom(imap_list_listroom, pattern);
715 cprintf("%s OK LIST completed\r\n", parms[0]);
721 * Implements the CREATE command
724 void imap_create(int num_parms, char *parms[]) {
726 char roomname[ROOMNAMELEN];
731 if (strchr(parms[2], '\\') != NULL) {
732 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
736 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
738 cprintf("%s NO Invalid mailbox name or location\r\n",
742 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
743 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
745 if (flags & IR_MAILBOX) {
746 newroomtype = 4; /* private mailbox */
749 newroomtype = 0; /* public folder */
752 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
753 roomname, floornum, newroomtype);
755 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
757 cprintf("%s NO Mailbox already exists, or create failed\r\n",
761 cprintf("%s OK CREATE completed\r\n", parms[0]);
767 * Locate a room by its IMAP folder name, and check access to it
769 int imap_grabroom(char *returned_roomname, char *foldername) {
771 char augmented_roomname[ROOMNAMELEN];
772 char roomname[ROOMNAMELEN];
774 struct ctdlroom QRscratch;
778 ret = imap_roomname(roomname, sizeof roomname, foldername);
783 /* First try a regular match */
784 c = getroom(&QRscratch, roomname);
786 /* Then try a mailbox name match */
788 MailboxName(augmented_roomname, sizeof augmented_roomname,
789 &CC->user, roomname);
790 c = getroom(&QRscratch, augmented_roomname);
792 strcpy(roomname, augmented_roomname);
795 /* If the room exists, check security/access */
797 /* See if there is an existing user/room relationship */
798 ra = CtdlRoomAccess(&QRscratch, &CC->user);
800 /* normal clients have to pass through security */
806 /* Fail here if no such room */
808 strcpy(returned_roomname, "");
812 strcpy(returned_roomname, QRscratch.QRname);
819 * Implements the STATUS command (sort of)
822 void imap_status(int num_parms, char *parms[]) {
824 char roomname[ROOMNAMELEN];
826 char savedroom[ROOMNAMELEN];
829 ret = imap_grabroom(roomname, parms[2]);
831 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
837 * usergoto() formally takes us to the desired room, happily returning
838 * the number of messages and number of new messages. (If another
839 * folder is selected, save its name so we can return there!!!!!)
841 if (IMAP->selected) {
842 strcpy(savedroom, CC->room.QRname);
844 usergoto(roomname, 0, 0, &msgs, &new);
847 * Tell the client what it wants to know. In fact, tell it *more* than
848 * it wants to know. We happily IGnore the supplied status data item
849 * names and simply spew all possible data items. It's far easier to
850 * code and probably saves us some processing time too.
852 imap_mailboxname(buf, sizeof buf, &CC->room);
853 cprintf("* STATUS ");
855 cprintf(" (MESSAGES %d ", msgs);
856 cprintf("RECENT 0 "); /* FIXME we need to implement this */
857 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
858 cprintf("UNSEEN %d)\r\n", new);
861 * If another folder is selected, go back to that room so we can resume
862 * our happy day without violent explosions.
864 if (IMAP->selected) {
865 usergoto(savedroom, 0, 0, &msgs, &new);
869 * Oooh, look, we're done!
871 cprintf("%s OK STATUS completed\r\n", parms[0]);
877 * Implements the SUBSCRIBE command
880 void imap_subscribe(int num_parms, char *parms[]) {
882 char roomname[ROOMNAMELEN];
883 char savedroom[ROOMNAMELEN];
886 ret = imap_grabroom(roomname, parms[2]);
888 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
894 * usergoto() formally takes us to the desired room, which has the side
895 * effect of marking the room as not-zapped ... exactly the effect
898 if (IMAP->selected) {
899 strcpy(savedroom, CC->room.QRname);
901 usergoto(roomname, 0, 0, &msgs, &new);
904 * If another folder is selected, go back to that room so we can resume
905 * our happy day without violent explosions.
907 if (IMAP->selected) {
908 usergoto(savedroom, 0, 0, &msgs, &new);
911 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
916 * Implements the UNSUBSCRIBE command
919 void imap_unsubscribe(int num_parms, char *parms[]) {
921 char roomname[ROOMNAMELEN];
922 char savedroom[ROOMNAMELEN];
925 ret = imap_grabroom(roomname, parms[2]);
927 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
933 * usergoto() formally takes us to the desired room.
935 if (IMAP->selected) {
936 strcpy(savedroom, CC->room.QRname);
938 usergoto(roomname, 0, 0, &msgs, &new);
941 * Now make the API call to zap the room
943 if (CtdlForgetThisRoom() == 0) {
944 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
947 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
952 * If another folder is selected, go back to that room so we can resume
953 * our happy day without violent explosions.
955 if (IMAP->selected) {
956 usergoto(savedroom, 0, 0, &msgs, &new);
963 * Implements the DELETE command
966 void imap_delete(int num_parms, char *parms[]) {
968 char roomname[ROOMNAMELEN];
969 char savedroom[ROOMNAMELEN];
972 ret = imap_grabroom(roomname, parms[2]);
974 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
980 * usergoto() formally takes us to the desired room, happily returning
981 * the number of messages and number of new messages. (If another
982 * folder is selected, save its name so we can return there!!!!!)
984 if (IMAP->selected) {
985 strcpy(savedroom, CC->room.QRname);
987 usergoto(roomname, 0, 0, &msgs, &new);
990 * Now delete the room.
992 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
993 cprintf("%s OK DELETE completed\r\n", parms[0]);
994 delete_room(&CC->room);
997 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1001 * If another folder is selected, go back to that room so we can resume
1002 * our happy day without violent explosions.
1004 if (IMAP->selected) {
1005 usergoto(savedroom, 0, 0, &msgs, &new);
1011 * Back end function for imap_rename()
1013 void imap_rename_backend(struct ctdlroom *qrbuf, void *data) {
1014 char foldername[SIZ];
1015 char newfoldername[SIZ];
1016 char newroomname[ROOMNAMELEN];
1018 struct irl *irlp = NULL; /* scratch pointer */
1019 struct irlparms *irlparms;
1021 irlparms = (struct irlparms *)data;
1022 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1024 /* Rename subfolders */
1025 if ( (!strncasecmp(foldername, irlparms->oldname,
1026 strlen(irlparms->oldname))
1027 && (foldername[strlen(irlparms->oldname)] == '|')) ) {
1029 sprintf(newfoldername, "%s|%s",
1031 &foldername[strlen(irlparms->oldname)+1]
1034 newfloor = imap_roomname(newroomname,
1035 sizeof newroomname, newfoldername) & 0xFF;
1037 irlp = (struct irl *) mallok(sizeof(struct irl));
1038 strcpy(irlp->irl_newroom, newroomname);
1039 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1040 irlp->irl_newfloor = newfloor;
1041 irlp->next = *(irlparms->irl);
1042 *(irlparms->irl) = irlp;
1048 * Implements the RENAME command
1051 void imap_rename(int num_parms, char *parms[]) {
1052 char old_room[ROOMNAMELEN];
1053 char new_room[ROOMNAMELEN];
1057 struct irl *irl = NULL; /* the list */
1058 struct irl *irlp = NULL; /* scratch pointer */
1059 struct irlparms irlparms;
1061 if (strchr(parms[3], '\\') != NULL) {
1062 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
1066 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1067 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1068 new_floor = (newr & 0xFF);
1070 r = CtdlRenameRoom(old_room, new_room, new_floor);
1072 if (r == crr_room_not_found) {
1073 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
1076 if (r == crr_already_exists) {
1077 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1080 if (r == crr_noneditable) {
1081 cprintf("%s This folder is not editable.\r\n", parms[0]);
1084 if (r == crr_invalid_floor) {
1085 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1088 if (r == crr_access_denied) {
1089 cprintf("%s You do not have permission to edit "
1090 "this folder.\r\n", parms[0]);
1094 cprintf("%s NO Rename failed - undefined error %d\r\n",
1100 /* If this is the INBOX, then RFC2060 says we have to just move the
1101 * contents. In a Citadel environment it's easier to rename the room
1102 * (already did that) and create a new inbox.
1104 if (!strcasecmp(parms[2], "INBOX")) {
1105 create_room(MAILROOM, 4, "", 0, 1, 0);
1108 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1110 irlparms.oldname = parms[2];
1111 irlparms.newname = parms[3];
1112 irlparms.irl = &irl;
1113 ForEachRoom(imap_rename_backend, (void *)&irlparms);
1115 /* ... and now rename them. */
1116 while (irl != NULL) {
1117 r = CtdlRenameRoom(irl->irl_oldroom,
1118 irl->irl_newroom, irl->irl_newfloor);
1120 /* FIXME handle error returns better */
1121 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1129 cprintf("%s OK RENAME completed\r\n", parms[0]);
1136 * Main command loop for IMAP sessions.
1138 void imap_command_loop(void) {
1144 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1145 if (client_gets(cmdbuf) < 1) {
1146 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1151 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1152 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1155 /* strip off l/t whitespace and CRLF */
1156 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1157 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1160 /* If we're in the middle of a multi-line command, handle that */
1161 if (IMAP->authstate == imap_as_expecting_username) {
1162 imap_auth_login_user(cmdbuf);
1165 if (IMAP->authstate == imap_as_expecting_password) {
1166 imap_auth_login_pass(cmdbuf);
1171 /* Ok, at this point we're in normal command mode. The first thing
1172 * we do is print any incoming pages (yeah! we really do!)
1174 imap_print_express_messages();
1177 * Before processing the command that was just entered... if we happen
1178 * to have a folder selected, we'd like to rescan that folder for new
1179 * messages, and for deletions/changes of existing messages. This
1180 * could probably be optimized somehow, but IMAP sucks...
1182 if (IMAP->selected) {
1183 imap_rescan_msgids();
1186 /* Now for the command set. */
1188 /* Grab the tag, command, and parameters. Check syntax. */
1189 num_parms = imap_parameterize(parms, cmdbuf);
1190 if (num_parms < 2) {
1191 cprintf("BAD syntax error\r\n");
1194 /* The commands below may be executed in any state */
1196 else if ( (!strcasecmp(parms[1], "NOOP"))
1197 || (!strcasecmp(parms[1], "CHECK")) ) {
1198 cprintf("%s OK This command successfully did nothing.\r\n",
1202 else if (!strcasecmp(parms[1], "LOGOUT")) {
1203 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1204 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1209 else if (!strcasecmp(parms[1], "LOGIN")) {
1210 imap_login(num_parms, parms);
1213 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1214 imap_authenticate(num_parms, parms);
1217 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1218 imap_capability(num_parms, parms);
1222 else if (!strcasecmp(parms[1], "STARTTLS")) {
1223 imap_starttls(num_parms, parms);
1227 else if (!CC->logged_in) {
1228 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1231 /* The commans below require a logged-in state */
1233 else if (!strcasecmp(parms[1], "SELECT")) {
1234 imap_select(num_parms, parms);
1237 else if (!strcasecmp(parms[1], "EXAMINE")) {
1238 imap_select(num_parms, parms);
1241 else if (!strcasecmp(parms[1], "LSUB")) {
1242 imap_lsub(num_parms, parms);
1245 else if (!strcasecmp(parms[1], "LIST")) {
1246 imap_list(num_parms, parms);
1249 else if (!strcasecmp(parms[1], "CREATE")) {
1250 imap_create(num_parms, parms);
1253 else if (!strcasecmp(parms[1], "DELETE")) {
1254 imap_delete(num_parms, parms);
1257 else if (!strcasecmp(parms[1], "RENAME")) {
1258 imap_rename(num_parms, parms);
1261 else if (!strcasecmp(parms[1], "STATUS")) {
1262 imap_status(num_parms, parms);
1265 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1266 imap_subscribe(num_parms, parms);
1269 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1270 imap_unsubscribe(num_parms, parms);
1273 else if (!strcasecmp(parms[1], "APPEND")) {
1274 imap_append(num_parms, parms);
1277 else if (IMAP->selected == 0) {
1278 cprintf("%s BAD no folder selected\r\n", parms[0]);
1281 /* The commands below require the SELECT state on a mailbox */
1283 else if (!strcasecmp(parms[1], "FETCH")) {
1284 imap_fetch(num_parms, parms);
1287 else if ( (!strcasecmp(parms[1], "UID"))
1288 && (!strcasecmp(parms[2], "FETCH")) ) {
1289 imap_uidfetch(num_parms, parms);
1292 else if (!strcasecmp(parms[1], "SEARCH")) {
1293 imap_search(num_parms, parms);
1296 else if ( (!strcasecmp(parms[1], "UID"))
1297 && (!strcasecmp(parms[2], "SEARCH")) ) {
1298 imap_uidsearch(num_parms, parms);
1301 else if (!strcasecmp(parms[1], "STORE")) {
1302 imap_store(num_parms, parms);
1305 else if ( (!strcasecmp(parms[1], "UID"))
1306 && (!strcasecmp(parms[2], "STORE")) ) {
1307 imap_uidstore(num_parms, parms);
1310 else if (!strcasecmp(parms[1], "COPY")) {
1311 imap_copy(num_parms, parms);
1314 else if ( (!strcasecmp(parms[1], "UID"))
1315 && (!strcasecmp(parms[2], "COPY")) ) {
1316 imap_uidcopy(num_parms, parms);
1319 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1320 imap_expunge(num_parms, parms);
1323 else if (!strcasecmp(parms[1], "CLOSE")) {
1324 imap_close(num_parms, parms);
1327 /* End of commands. If we get here, the command is either invalid
1332 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1335 /* If the client transmitted a message we can free it now */
1336 imap_free_transmitted_message();
1342 * This function is called to register the IMAP extension with Citadel.
1344 char *serv_imap_init(void)
1346 CtdlRegisterServiceHook(config.c_imap_port,
1350 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);