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: 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>
38 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/rand.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
50 #include "serv_extensions.h"
57 #include "internet_addressing.h"
58 #include "serv_imap.h"
59 #include "imap_tools.h"
60 #include "imap_fetch.h"
61 #include "imap_search.h"
62 #include "imap_store.h"
63 #include "imap_misc.h"
65 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
66 #include "serv_crypto.h"
69 /* imap_rename() uses this struct containing list of rooms to rename */
72 char irl_oldroom[ROOMNAMELEN];
73 char irl_newroom[ROOMNAMELEN];
77 /* Data which is passed between imap_rename() and imap_rename_backend() */
86 * If there is a message ID map in memory, free it
88 void imap_free_msgids(void) {
89 if (IMAP->msgids != NULL) {
94 if (IMAP->flags != NULL) {
102 * If there is a transmitted message in memory, free it
104 void imap_free_transmitted_message(void) {
105 if (IMAP->transmitted_message != NULL) {
106 phree(IMAP->transmitted_message);
107 IMAP->transmitted_message = NULL;
108 IMAP->transmitted_length = 0;
114 * Set the \\Seen flag for messages which aren't new
116 void imap_set_seen_flags(void) {
120 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
121 if (IMAP->num_msgs > 0) {
122 for (i=0; i<IMAP->num_msgs; ++i) {
123 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
124 IMAP->flags[i] |= IMAP_SEEN;
134 * Back end for imap_load_msgids()
136 * Optimization: instead of calling realloc() to add each message, we
137 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
138 * allows the mapping to proceed much faster.
140 void imap_add_single_msgid(long msgnum, void *userdata) {
142 IMAP->num_msgs = IMAP->num_msgs + 1;
143 if (IMAP->msgids == NULL) {
144 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
145 * REALLOC_INCREMENT);
147 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
148 IMAP->msgids = reallok(IMAP->msgids,
149 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
151 if (IMAP->flags == NULL) {
152 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
153 * REALLOC_INCREMENT);
155 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
156 IMAP->flags = reallok(IMAP->flags,
157 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
159 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
160 IMAP->flags[IMAP->num_msgs - 1] = 0;
166 * Set up a message ID map for the current room (folder)
168 void imap_load_msgids(void) {
170 if (IMAP->selected == 0) {
171 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
175 imap_free_msgids(); /* If there was already a map, free it */
177 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
178 imap_add_single_msgid, NULL);
180 imap_set_seen_flags();
181 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
186 * Re-scan the selected room (folder) and see if it's been changed at all
188 void imap_rescan_msgids(void) {
190 int original_num_msgs = 0;
191 long original_highest = 0L;
193 int message_still_exists;
194 struct cdbdata *cdbfr;
195 long *msglist = NULL;
199 if (IMAP->selected == 0) {
200 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
204 /* Load the *current* message list from disk, so we can compare it
205 * to what we have in memory.
207 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
209 msglist = mallok(cdbfr->len);
210 memcpy(msglist, cdbfr->ptr, cdbfr->len);
211 num_msgs = cdbfr->len / sizeof(long);
219 * Check to see if any of the messages we know about have been expunged
221 if (IMAP->num_msgs > 0)
222 for (i=0; i<IMAP->num_msgs; ++i) {
224 message_still_exists = 0;
225 if (num_msgs > 0) for (j = 0; j < num_msgs; ++j) {
226 if (msglist[j] == IMAP->msgids[i]) {
227 message_still_exists = 1;
231 if (message_still_exists == 0) {
232 cprintf("* %d EXPUNGE\r\n", i+1);
234 /* Here's some nice stupid nonsense. When a message
235 * is expunged, we have to slide all the existing
236 * messages up in the message array.
239 memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
240 (sizeof(long)*(IMAP->num_msgs-i)) );
241 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
242 (sizeof(long)*(IMAP->num_msgs-i)) );
250 * Remember how many messages were here before we re-scanned.
252 original_num_msgs = IMAP->num_msgs;
253 if (IMAP->num_msgs > 0) {
254 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
257 original_highest = 0L;
261 * Now peruse the room for *new* messages only.
263 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
264 if (msglist[j] > original_highest) {
265 imap_add_single_msgid(msglist[j], NULL);
268 imap_set_seen_flags();
271 * If new messages have arrived, tell the client about them.
273 if (IMAP->num_msgs > original_num_msgs) {
274 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
277 if (num_msgs != 0) phree(msglist);
287 * This cleanup function blows away the temporary memory and files used by
290 void imap_cleanup_function(void) {
292 /* Don't do this stuff if this is not a IMAP session! */
293 if (CC->h_command_function != imap_command_loop) return;
295 lprintf(9, "Performing IMAP cleanup hook\n");
297 imap_free_transmitted_message();
298 lprintf(9, "Finished IMAP cleanup hook\n");
304 * Here's where our IMAP session begins its happy day.
306 void imap_greeting(void) {
308 strcpy(CC->cs_clientname, "IMAP session");
309 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
310 IMAP->authstate = imap_as_normal;
312 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
318 * implements the LOGIN command (ordinary username/password login)
320 void imap_login(int num_parms, char *parms[]) {
321 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
322 if (CtdlTryPassword(parms[3]) == pass_ok) {
323 cprintf("%s OK login successful\r\n", parms[0]);
328 cprintf("%s BAD Login incorrect\r\n", parms[0]);
333 * Implements the AUTHENTICATE command
335 void imap_authenticate(int num_parms, char *parms[]) {
338 if (num_parms != 3) {
339 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
343 if (!strcasecmp(parms[2], "LOGIN")) {
344 CtdlEncodeBase64(buf, "Username:", 9);
345 cprintf("+ %s\r\n", buf);
346 IMAP->authstate = imap_as_expecting_username;
347 strcpy(IMAP->authseq, parms[0]);
352 cprintf("%s NO AUTHENTICATE %s failed\r\n",
357 void imap_auth_login_user(char *cmd) {
360 CtdlDecodeBase64(buf, cmd, SIZ);
361 CtdlLoginExistingUser(buf);
362 CtdlEncodeBase64(buf, "Password:", 9);
363 cprintf("+ %s\r\n", buf);
364 IMAP->authstate = imap_as_expecting_password;
368 void imap_auth_login_pass(char *cmd) {
371 CtdlDecodeBase64(buf, cmd, SIZ);
372 if (CtdlTryPassword(buf) == pass_ok) {
373 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
376 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
378 IMAP->authstate = imap_as_normal;
385 * implements the CAPABILITY command
387 void imap_capability(int num_parms, char *parms[]) {
388 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
390 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
391 cprintf(" STARTTLS");
395 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
400 * implements the STARTTLS command
402 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
403 void imap_starttls(int num_parms, char *parms[]) {
404 int retval, bits, alg_bits;
407 cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
410 if (!(CC->ssl = SSL_new(ssl_ctx))) {
411 lprintf(2, "SSL_new failed: %s\n",
412 ERR_reason_error_string(ERR_peek_error()));
413 cprintf("%s NO SSL_new: %s\r\n", parms[0],
414 ERR_reason_error_string(ERR_get_error()));
417 if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
418 lprintf(2, "SSL_set_fd failed: %s\n",
419 ERR_reason_error_string(ERR_peek_error()));
422 cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
423 ERR_reason_error_string(ERR_get_error()));
426 cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
427 retval = SSL_accept(CC->ssl);
430 * Can't notify the client of an error here; they will
431 * discover the problem at the SSL layer and should
432 * revert to unencrypted communications.
436 errval = SSL_get_error(CC->ssl, retval);
437 lprintf(2, "SSL_accept failed: %s\n",
438 ERR_reason_error_string(ERR_get_error()));
443 BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
444 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
445 lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
446 SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
447 SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
449 CC->redirect_ssl = 1;
456 * implements the SELECT command
458 void imap_select(int num_parms, char *parms[]) {
460 char augmented_roomname[ROOMNAMELEN];
464 struct ctdlroom QRscratch;
470 /* Convert the supplied folder name to a roomname */
471 i = imap_roomname(towhere, sizeof towhere, parms[2]);
473 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
477 floornum = (i & 0x00ff);
478 roomflags = (i & 0xff00);
480 /* First try a regular match */
481 c = getroom(&QRscratch, towhere);
483 /* Then try a mailbox name match */
485 MailboxName(augmented_roomname, sizeof augmented_roomname,
487 c = getroom(&QRscratch, augmented_roomname);
489 strcpy(towhere, augmented_roomname);
492 /* If the room exists, check security/access */
494 /* See if there is an existing user/room relationship */
495 ra = CtdlRoomAccess(&QRscratch, &CC->user);
497 /* normal clients have to pass through security */
503 /* Fail here if no such room */
505 cprintf("%s NO ... no such room, or access denied\r\n",
512 * usergoto() formally takes us to the desired room, happily returning
513 * the number of messages and number of new messages.
515 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
516 usergoto(NULL, 0, 0, &msgs, &new);
519 if (!strcasecmp(parms[1], "EXAMINE")) {
528 /* FIXME ... much more info needs to be supplied here */
529 cprintf("* %d EXISTS\r\n", msgs);
530 cprintf("* %d RECENT\r\n", new);
531 cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
532 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
533 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
534 cprintf("%s OK [%s] %s completed\r\n",
536 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
543 * does the real work for expunge
545 int imap_do_expunge(void) {
547 int num_expunged = 0;
549 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
550 if (IMAP->flags[i] & IMAP_DELETED) {
551 CtdlDeleteMessages(CC->room.QRname,
552 IMAP->msgids[i], "");
554 lprintf(9, "%ld ... deleted\n", IMAP->msgids[i]);
557 lprintf(9, "%ld ... not deleted\n", IMAP->msgids[i]);
561 if (num_expunged > 0) {
562 imap_rescan_msgids();
565 return(num_expunged);
570 * implements the EXPUNGE command syntax
572 void imap_expunge(int num_parms, char *parms[]) {
573 int num_expunged = 0;
575 num_expunged = imap_do_expunge();
576 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
581 * implements the CLOSE command
583 void imap_close(int num_parms, char *parms[]) {
585 /* Yes, we always expunge on close. */
591 cprintf("%s OK CLOSE completed\r\n", parms[0]);
598 * Used by LIST and LSUB to show the floors in the listing
600 void imap_list_floors(char *cmd, char *pattern) {
604 for (i=0; i<MAXFLOORS; ++i) {
606 if (fl->f_flags & F_INUSE) {
607 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
608 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
609 imap_strout(fl->f_name);
619 * Back end for imap_lsub()
621 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
622 * may or may not be the desired behavior in the future.
624 void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data) {
629 pattern = (char *)data;
631 /* Only list rooms to which the user has access!! */
632 ra = CtdlRoomAccess(qrbuf, &CC->user);
634 imap_mailboxname(buf, sizeof buf, qrbuf);
635 if (imap_mailbox_matches_pattern(pattern, buf)) {
636 cprintf("* LSUB () \"|\" ");
645 * Implements the LSUB command
647 void imap_lsub(int num_parms, char *parms[]) {
650 cprintf("%s BAD arguments invalid\r\n", parms[0]);
653 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
655 if (strlen(parms[3])==0) {
656 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
660 imap_list_floors("LSUB", pattern);
661 ForEachRoom(imap_lsub_listroom, pattern);
664 cprintf("%s OK LSUB completed\r\n", parms[0]);
670 * Back end for imap_list()
672 void imap_list_listroom(struct ctdlroom *qrbuf, void *data) {
677 pattern = (char *)data;
679 /* Only list rooms to which the user has access!! */
680 ra = CtdlRoomAccess(qrbuf, &CC->user);
682 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
683 imap_mailboxname(buf, sizeof buf, qrbuf);
684 if (imap_mailbox_matches_pattern(pattern, buf)) {
685 cprintf("* LIST () \"|\" ");
694 * Implements the LIST command
696 void imap_list(int num_parms, char *parms[]) {
699 cprintf("%s BAD arguments invalid\r\n", parms[0]);
702 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
704 if (strlen(parms[3])==0) {
705 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
709 imap_list_floors("LIST", pattern);
710 ForEachRoom(imap_list_listroom, pattern);
713 cprintf("%s OK LIST completed\r\n", parms[0]);
719 * Implements the CREATE command
722 void imap_create(int num_parms, char *parms[]) {
724 char roomname[ROOMNAMELEN];
729 if (strchr(parms[2], '\\') != NULL) {
730 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
734 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
736 cprintf("%s NO Invalid mailbox name or location\r\n",
740 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
741 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
743 if (flags & IR_MAILBOX) {
744 newroomtype = 4; /* private mailbox */
747 newroomtype = 0; /* public folder */
750 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
751 roomname, floornum, newroomtype);
753 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
755 cprintf("%s NO Mailbox already exists, or create failed\r\n",
759 cprintf("%s OK CREATE completed\r\n", parms[0]);
765 * Locate a room by its IMAP folder name, and check access to it
767 int imap_grabroom(char *returned_roomname, char *foldername) {
769 char augmented_roomname[ROOMNAMELEN];
770 char roomname[ROOMNAMELEN];
772 struct ctdlroom QRscratch;
776 ret = imap_roomname(roomname, sizeof roomname, foldername);
781 /* First try a regular match */
782 c = getroom(&QRscratch, roomname);
784 /* Then try a mailbox name match */
786 MailboxName(augmented_roomname, sizeof augmented_roomname,
787 &CC->user, roomname);
788 c = getroom(&QRscratch, augmented_roomname);
790 strcpy(roomname, augmented_roomname);
793 /* If the room exists, check security/access */
795 /* See if there is an existing user/room relationship */
796 ra = CtdlRoomAccess(&QRscratch, &CC->user);
798 /* normal clients have to pass through security */
804 /* Fail here if no such room */
806 strcpy(returned_roomname, "");
810 strcpy(returned_roomname, QRscratch.QRname);
817 * Implements the STATUS command (sort of)
820 void imap_status(int num_parms, char *parms[]) {
822 char roomname[ROOMNAMELEN];
824 char savedroom[ROOMNAMELEN];
827 ret = imap_grabroom(roomname, parms[2]);
829 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
835 * usergoto() formally takes us to the desired room, happily returning
836 * the number of messages and number of new messages. (If another
837 * folder is selected, save its name so we can return there!!!!!)
839 if (IMAP->selected) {
840 strcpy(savedroom, CC->room.QRname);
842 usergoto(roomname, 0, 0, &msgs, &new);
845 * Tell the client what it wants to know. In fact, tell it *more* than
846 * it wants to know. We happily IGnore the supplied status data item
847 * names and simply spew all possible data items. It's far easier to
848 * code and probably saves us some processing time too.
850 imap_mailboxname(buf, sizeof buf, &CC->room);
851 cprintf("* STATUS ");
853 cprintf(" (MESSAGES %d ", msgs);
854 cprintf("RECENT 0 "); /* FIXME we need to implement this */
855 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
856 cprintf("UNSEEN %d)\r\n", new);
859 * If another folder is selected, go back to that room so we can resume
860 * our happy day without violent explosions.
862 if (IMAP->selected) {
863 usergoto(savedroom, 0, 0, &msgs, &new);
867 * Oooh, look, we're done!
869 cprintf("%s OK STATUS completed\r\n", parms[0]);
875 * Implements the SUBSCRIBE command
878 void imap_subscribe(int num_parms, char *parms[]) {
880 char roomname[ROOMNAMELEN];
881 char savedroom[ROOMNAMELEN];
884 ret = imap_grabroom(roomname, parms[2]);
886 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
892 * usergoto() formally takes us to the desired room, which has the side
893 * effect of marking the room as not-zapped ... exactly the effect
896 if (IMAP->selected) {
897 strcpy(savedroom, CC->room.QRname);
899 usergoto(roomname, 0, 0, &msgs, &new);
902 * If another folder is selected, go back to that room so we can resume
903 * our happy day without violent explosions.
905 if (IMAP->selected) {
906 usergoto(savedroom, 0, 0, &msgs, &new);
909 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
914 * Implements the UNSUBSCRIBE command
917 void imap_unsubscribe(int num_parms, char *parms[]) {
919 char roomname[ROOMNAMELEN];
920 char savedroom[ROOMNAMELEN];
923 ret = imap_grabroom(roomname, parms[2]);
925 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
931 * usergoto() formally takes us to the desired room.
933 if (IMAP->selected) {
934 strcpy(savedroom, CC->room.QRname);
936 usergoto(roomname, 0, 0, &msgs, &new);
939 * Now make the API call to zap the room
941 if (CtdlForgetThisRoom() == 0) {
942 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
945 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
950 * If another folder is selected, go back to that room so we can resume
951 * our happy day without violent explosions.
953 if (IMAP->selected) {
954 usergoto(savedroom, 0, 0, &msgs, &new);
961 * Implements the DELETE command
964 void imap_delete(int num_parms, char *parms[]) {
966 char roomname[ROOMNAMELEN];
967 char savedroom[ROOMNAMELEN];
970 ret = imap_grabroom(roomname, parms[2]);
972 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
978 * usergoto() formally takes us to the desired room, happily returning
979 * the number of messages and number of new messages. (If another
980 * folder is selected, save its name so we can return there!!!!!)
982 if (IMAP->selected) {
983 strcpy(savedroom, CC->room.QRname);
985 usergoto(roomname, 0, 0, &msgs, &new);
988 * Now delete the room.
990 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
991 cprintf("%s OK DELETE completed\r\n", parms[0]);
992 delete_room(&CC->room);
995 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
999 * If another folder is selected, go back to that room so we can resume
1000 * our happy day without violent explosions.
1002 if (IMAP->selected) {
1003 usergoto(savedroom, 0, 0, &msgs, &new);
1009 * Back end function for imap_rename()
1011 void imap_rename_backend(struct ctdlroom *qrbuf, void *data) {
1012 char foldername[SIZ];
1013 char newfoldername[SIZ];
1014 char newroomname[ROOMNAMELEN];
1016 struct irl *irlp = NULL; /* scratch pointer */
1017 struct irlparms *irlparms;
1019 irlparms = (struct irlparms *)data;
1020 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1022 /* Rename subfolders */
1023 if ( (!strncasecmp(foldername, irlparms->oldname,
1024 strlen(irlparms->oldname))
1025 && (foldername[strlen(irlparms->oldname)] == '|')) ) {
1027 sprintf(newfoldername, "%s|%s",
1029 &foldername[strlen(irlparms->oldname)+1]
1032 newfloor = imap_roomname(newroomname,
1033 sizeof newroomname, newfoldername) & 0xFF;
1035 irlp = (struct irl *) mallok(sizeof(struct irl));
1036 strcpy(irlp->irl_newroom, newroomname);
1037 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1038 irlp->irl_newfloor = newfloor;
1039 irlp->next = *(irlparms->irl);
1040 *(irlparms->irl) = irlp;
1046 * Implements the RENAME command
1049 void imap_rename(int num_parms, char *parms[]) {
1050 char old_room[ROOMNAMELEN];
1051 char new_room[ROOMNAMELEN];
1055 struct irl *irl = NULL; /* the list */
1056 struct irl *irlp = NULL; /* scratch pointer */
1057 struct irlparms irlparms;
1059 if (strchr(parms[3], '\\') != NULL) {
1060 cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
1064 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1065 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1066 new_floor = (newr & 0xFF);
1068 r = CtdlRenameRoom(old_room, new_room, new_floor);
1070 if (r == crr_room_not_found) {
1071 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
1074 if (r == crr_already_exists) {
1075 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1078 if (r == crr_noneditable) {
1079 cprintf("%s This folder is not editable.\r\n", parms[0]);
1082 if (r == crr_invalid_floor) {
1083 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1086 if (r == crr_access_denied) {
1087 cprintf("%s You do not have permission to edit "
1088 "this folder.\r\n", parms[0]);
1092 cprintf("%s NO Rename failed - undefined error %d\r\n",
1098 /* If this is the INBOX, then RFC2060 says we have to just move the
1099 * contents. In a Citadel environment it's easier to rename the room
1100 * (already did that) and create a new inbox.
1102 if (!strcasecmp(parms[2], "INBOX")) {
1103 create_room(MAILROOM, 4, "", 0, 1, 0);
1106 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1108 irlparms.oldname = parms[2];
1109 irlparms.newname = parms[3];
1110 irlparms.irl = &irl;
1111 ForEachRoom(imap_rename_backend, (void *)&irlparms);
1113 /* ... and now rename them. */
1114 while (irl != NULL) {
1115 r = CtdlRenameRoom(irl->irl_oldroom,
1116 irl->irl_newroom, irl->irl_newfloor);
1118 /* FIXME handle error returns better */
1119 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1127 cprintf("%s OK RENAME completed\r\n", parms[0]);
1134 * Main command loop for IMAP sessions.
1136 void imap_command_loop(void) {
1142 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1143 if (client_gets(cmdbuf) < 1) {
1144 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1149 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1150 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1153 /* strip off l/t whitespace and CRLF */
1154 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1155 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1158 /* If we're in the middle of a multi-line command, handle that */
1159 if (IMAP->authstate == imap_as_expecting_username) {
1160 imap_auth_login_user(cmdbuf);
1163 if (IMAP->authstate == imap_as_expecting_password) {
1164 imap_auth_login_pass(cmdbuf);
1169 /* Ok, at this point we're in normal command mode. The first thing
1170 * we do is print any incoming pages (yeah! we really do!)
1172 imap_print_express_messages();
1175 * Before processing the command that was just entered... if we happen
1176 * to have a folder selected, we'd like to rescan that folder for new
1177 * messages, and for deletions/changes of existing messages. This
1178 * could probably be optimized somehow, but IMAP sucks...
1180 if (IMAP->selected) {
1181 imap_rescan_msgids();
1184 /* Now for the command set. */
1186 /* Grab the tag, command, and parameters. Check syntax. */
1187 num_parms = imap_parameterize(parms, cmdbuf);
1188 if (num_parms < 2) {
1189 cprintf("BAD syntax error\r\n");
1192 /* The commands below may be executed in any state */
1194 else if ( (!strcasecmp(parms[1], "NOOP"))
1195 || (!strcasecmp(parms[1], "CHECK")) ) {
1196 cprintf("%s OK This command successfully did nothing.\r\n",
1200 else if (!strcasecmp(parms[1], "LOGOUT")) {
1201 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1202 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1207 else if (!strcasecmp(parms[1], "LOGIN")) {
1208 imap_login(num_parms, parms);
1211 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1212 imap_authenticate(num_parms, parms);
1215 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1216 imap_capability(num_parms, parms);
1219 #ifdef HAVE_OPENSSL_XXX /* temporarily disabled due to bugs */
1220 else if (!strcasecmp(parms[1], "STARTTLS")) {
1221 imap_starttls(num_parms, parms);
1225 else if (!CC->logged_in) {
1226 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1229 /* The commans below require a logged-in state */
1231 else if (!strcasecmp(parms[1], "SELECT")) {
1232 imap_select(num_parms, parms);
1235 else if (!strcasecmp(parms[1], "EXAMINE")) {
1236 imap_select(num_parms, parms);
1239 else if (!strcasecmp(parms[1], "LSUB")) {
1240 imap_lsub(num_parms, parms);
1243 else if (!strcasecmp(parms[1], "LIST")) {
1244 imap_list(num_parms, parms);
1247 else if (!strcasecmp(parms[1], "CREATE")) {
1248 imap_create(num_parms, parms);
1251 else if (!strcasecmp(parms[1], "DELETE")) {
1252 imap_delete(num_parms, parms);
1255 else if (!strcasecmp(parms[1], "RENAME")) {
1256 imap_rename(num_parms, parms);
1259 else if (!strcasecmp(parms[1], "STATUS")) {
1260 imap_status(num_parms, parms);
1263 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1264 imap_subscribe(num_parms, parms);
1267 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1268 imap_unsubscribe(num_parms, parms);
1271 else if (!strcasecmp(parms[1], "APPEND")) {
1272 imap_append(num_parms, parms);
1275 else if (IMAP->selected == 0) {
1276 cprintf("%s BAD no folder selected\r\n", parms[0]);
1279 /* The commands below require the SELECT state on a mailbox */
1281 else if (!strcasecmp(parms[1], "FETCH")) {
1282 imap_fetch(num_parms, parms);
1285 else if ( (!strcasecmp(parms[1], "UID"))
1286 && (!strcasecmp(parms[2], "FETCH")) ) {
1287 imap_uidfetch(num_parms, parms);
1290 else if (!strcasecmp(parms[1], "SEARCH")) {
1291 imap_search(num_parms, parms);
1294 else if ( (!strcasecmp(parms[1], "UID"))
1295 && (!strcasecmp(parms[2], "SEARCH")) ) {
1296 imap_uidsearch(num_parms, parms);
1299 else if (!strcasecmp(parms[1], "STORE")) {
1300 imap_store(num_parms, parms);
1303 else if ( (!strcasecmp(parms[1], "UID"))
1304 && (!strcasecmp(parms[2], "STORE")) ) {
1305 imap_uidstore(num_parms, parms);
1308 else if (!strcasecmp(parms[1], "COPY")) {
1309 imap_copy(num_parms, parms);
1312 else if ( (!strcasecmp(parms[1], "UID"))
1313 && (!strcasecmp(parms[2], "COPY")) ) {
1314 imap_uidcopy(num_parms, parms);
1317 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1318 imap_expunge(num_parms, parms);
1321 else if (!strcasecmp(parms[1], "CLOSE")) {
1322 imap_close(num_parms, parms);
1325 /* End of commands. If we get here, the command is either invalid
1330 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1333 /* If the client transmitted a message we can free it now */
1334 imap_free_transmitted_message();
1340 * This function is called to register the IMAP extension with Citadel.
1342 char *serv_imap_init(void)
1344 CtdlRegisterServiceHook(config.c_imap_port,
1348 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);