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 "sysdep_decls.h"
44 #include "citserver.h"
47 #include "serv_extensions.h"
54 #include "internet_addressing.h"
55 #include "serv_imap.h"
56 #include "imap_tools.h"
57 #include "imap_fetch.h"
58 #include "imap_search.h"
59 #include "imap_store.h"
60 #include "imap_misc.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() */
81 * If there is a message ID map in memory, free it
83 void imap_free_msgids(void) {
84 if (IMAP->msgids != NULL) {
89 if (IMAP->flags != NULL) {
97 * If there is a transmitted message in memory, free it
99 void imap_free_transmitted_message(void) {
100 if (IMAP->transmitted_message != NULL) {
101 phree(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) {
115 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
116 if (IMAP->num_msgs > 0) {
117 for (i=0; i<IMAP->num_msgs; ++i) {
118 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
119 IMAP->flags[i] |= IMAP_SEEN;
129 * Back end for imap_load_msgids()
131 * Optimization: instead of calling realloc() to add each message, we
132 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
133 * allows the mapping to proceed much faster.
135 void imap_add_single_msgid(long msgnum, void *userdata) {
137 IMAP->num_msgs = IMAP->num_msgs + 1;
138 if (IMAP->msgids == NULL) {
139 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
140 * REALLOC_INCREMENT);
142 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
143 IMAP->msgids = reallok(IMAP->msgids,
144 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
146 if (IMAP->flags == NULL) {
147 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
148 * REALLOC_INCREMENT);
150 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
151 IMAP->flags = reallok(IMAP->flags,
152 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
154 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
155 IMAP->flags[IMAP->num_msgs - 1] = 0;
161 * Set up a message ID map for the current room (folder)
163 void imap_load_msgids(void) {
165 if (IMAP->selected == 0) {
166 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
170 imap_free_msgids(); /* If there was already a map, free it */
172 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
173 imap_add_single_msgid, NULL);
175 imap_set_seen_flags();
176 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
181 * Re-scan the selected room (folder) and see if it's been changed at all
183 void imap_rescan_msgids(void) {
185 int original_num_msgs = 0;
186 long original_highest = 0L;
188 int message_still_exists;
189 struct cdbdata *cdbfr;
190 long *msglist = NULL;
194 if (IMAP->selected == 0) {
195 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
199 /* Load the *current* message list from disk, so we can compare it
200 * to what we have in memory.
202 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
204 msglist = mallok(cdbfr->len);
205 memcpy(msglist, cdbfr->ptr, cdbfr->len);
206 num_msgs = cdbfr->len / sizeof(long);
214 * Check to see if any of the messages we know about have been expunged
216 if (IMAP->num_msgs > 0)
217 for (i=0; i<IMAP->num_msgs; ++i) {
219 message_still_exists = 0;
220 if (num_msgs > 0) for (j = 0; j < num_msgs; ++j) {
221 if (msglist[j] == IMAP->msgids[i]) {
222 message_still_exists = 1;
226 if (message_still_exists == 0) {
227 cprintf("* %d EXPUNGE\r\n", i+1);
229 /* Here's some nice stupid nonsense. When a message
230 * is expunged, we have to slide all the existing
231 * messages up in the message array.
234 memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
235 (sizeof(long)*(IMAP->num_msgs-i)) );
236 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
237 (sizeof(long)*(IMAP->num_msgs-i)) );
245 * Remember how many messages were here before we re-scanned.
247 original_num_msgs = IMAP->num_msgs;
248 if (IMAP->num_msgs > 0) {
249 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
252 original_highest = 0L;
256 * Now peruse the room for *new* messages only.
258 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
259 if (msglist[j] > original_highest) {
260 imap_add_single_msgid(msglist[j], NULL);
263 imap_set_seen_flags();
266 * If new messages have arrived, tell the client about them.
268 if (IMAP->num_msgs > original_num_msgs) {
269 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
272 if (num_msgs != 0) phree(msglist);
282 * This cleanup function blows away the temporary memory and files used by
285 void imap_cleanup_function(void) {
287 /* Don't do this stuff if this is not a IMAP session! */
288 if (CC->h_command_function != imap_command_loop) return;
290 lprintf(9, "Performing IMAP cleanup hook\n");
292 imap_free_transmitted_message();
293 lprintf(9, "Finished IMAP cleanup hook\n");
299 * Here's where our IMAP session begins its happy day.
301 void imap_greeting(void) {
303 strcpy(CC->cs_clientname, "IMAP session");
304 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
305 IMAP->authstate = imap_as_normal;
307 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
313 * implements the LOGIN command (ordinary username/password login)
315 void imap_login(int num_parms, char *parms[]) {
316 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
317 if (CtdlTryPassword(parms[3]) == pass_ok) {
318 cprintf("%s OK login successful\r\n", parms[0]);
323 cprintf("%s BAD Login incorrect\r\n", parms[0]);
328 * Implements the AUTHENTICATE command
330 void imap_authenticate(int num_parms, char *parms[]) {
333 if (num_parms != 3) {
334 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
338 if (!strcasecmp(parms[2], "LOGIN")) {
339 CtdlEncodeBase64(buf, "Username:", 9);
340 cprintf("+ %s\r\n", buf);
341 IMAP->authstate = imap_as_expecting_username;
342 strcpy(IMAP->authseq, parms[0]);
347 cprintf("%s NO AUTHENTICATE %s failed\r\n",
352 void imap_auth_login_user(char *cmd) {
355 CtdlDecodeBase64(buf, cmd, SIZ);
356 CtdlLoginExistingUser(buf);
357 CtdlEncodeBase64(buf, "Password:", 9);
358 cprintf("+ %s\r\n", buf);
359 IMAP->authstate = imap_as_expecting_password;
363 void imap_auth_login_pass(char *cmd) {
366 CtdlDecodeBase64(buf, cmd, SIZ);
367 if (CtdlTryPassword(buf) == pass_ok) {
368 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
371 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
373 IMAP->authstate = imap_as_normal;
380 * implements the CAPABILITY command
382 void imap_capability(int num_parms, char *parms[]) {
383 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
384 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
392 * implements the SELECT command
394 void imap_select(int num_parms, char *parms[]) {
396 char augmented_roomname[ROOMNAMELEN];
400 struct quickroom QRscratch;
406 /* Convert the supplied folder name to a roomname */
407 i = imap_roomname(towhere, sizeof towhere, parms[2]);
409 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
413 floornum = (i & 0x00ff);
414 roomflags = (i & 0xff00);
416 /* First try a regular match */
417 c = getroom(&QRscratch, towhere);
419 /* Then try a mailbox name match */
421 MailboxName(augmented_roomname, sizeof augmented_roomname,
422 &CC->usersupp, towhere);
423 c = getroom(&QRscratch, augmented_roomname);
425 strcpy(towhere, augmented_roomname);
428 /* If the room exists, check security/access */
430 /* See if there is an existing user/room relationship */
431 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
433 /* normal clients have to pass through security */
439 /* Fail here if no such room */
441 cprintf("%s NO ... no such room, or access denied\r\n",
448 * usergoto() formally takes us to the desired room, happily returning
449 * the number of messages and number of new messages.
451 memcpy(&CC->quickroom, &QRscratch, sizeof(struct quickroom));
452 usergoto(NULL, 0, 0, &msgs, &new);
455 if (!strcasecmp(parms[1], "EXAMINE")) {
464 /* FIXME ... much more info needs to be supplied here */
465 cprintf("* %d EXISTS\r\n", msgs);
466 cprintf("* %d RECENT\r\n", new);
467 cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
468 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
469 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
470 cprintf("%s OK [%s] %s completed\r\n",
472 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
479 * does the real work for expunge
481 int imap_do_expunge(void) {
483 int num_expunged = 0;
485 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
486 if (IMAP->flags[i] & IMAP_DELETED) {
487 CtdlDeleteMessages(CC->quickroom.QRname,
488 IMAP->msgids[i], "");
490 lprintf(9, "%ld ... deleted\n", IMAP->msgids[i]);
493 lprintf(9, "%ld ... not deleted\n", IMAP->msgids[i]);
497 if (num_expunged > 0) {
498 imap_rescan_msgids();
501 return(num_expunged);
506 * implements the EXPUNGE command syntax
508 void imap_expunge(int num_parms, char *parms[]) {
509 int num_expunged = 0;
511 num_expunged = imap_do_expunge();
512 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
517 * implements the CLOSE command
519 void imap_close(int num_parms, char *parms[]) {
521 /* Yes, we always expunge on close. */
527 cprintf("%s OK CLOSE completed\r\n", parms[0]);
534 * Used by LIST and LSUB to show the floors in the listing
536 void imap_list_floors(char *cmd, char *pattern) {
540 for (i=0; i<MAXFLOORS; ++i) {
542 if (fl->f_flags & F_INUSE) {
543 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
544 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
545 imap_strout(fl->f_name);
555 * Back end for imap_lsub()
557 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
558 * may or may not be the desired behavior in the future.
560 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
565 pattern = (char *)data;
567 /* Only list rooms to which the user has access!! */
568 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
570 imap_mailboxname(buf, sizeof buf, qrbuf);
571 if (imap_mailbox_matches_pattern(pattern, buf)) {
572 cprintf("* LSUB () \"|\" ");
581 * Implements the LSUB command
583 void imap_lsub(int num_parms, char *parms[]) {
586 cprintf("%s BAD arguments invalid\r\n", parms[0]);
589 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
591 if (strlen(parms[3])==0) {
592 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
596 imap_list_floors("LSUB", pattern);
597 ForEachRoom(imap_lsub_listroom, pattern);
600 cprintf("%s OK LSUB completed\r\n", parms[0]);
606 * Back end for imap_list()
608 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
613 pattern = (char *)data;
615 /* Only list rooms to which the user has access!! */
616 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
618 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
619 imap_mailboxname(buf, sizeof buf, qrbuf);
620 if (imap_mailbox_matches_pattern(pattern, buf)) {
621 cprintf("* LIST () \"|\" ");
630 * Implements the LIST command
632 void imap_list(int num_parms, char *parms[]) {
635 cprintf("%s BAD arguments invalid\r\n", parms[0]);
638 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
640 if (strlen(parms[3])==0) {
641 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
645 imap_list_floors("LIST", pattern);
646 ForEachRoom(imap_list_listroom, pattern);
649 cprintf("%s OK LIST completed\r\n", parms[0]);
655 * Implements the CREATE command
658 void imap_create(int num_parms, char *parms[]) {
660 char roomname[ROOMNAMELEN];
665 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
667 cprintf("%s NO Invalid mailbox name or location\r\n",
671 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
672 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
674 if (flags & IR_MAILBOX) {
675 newroomtype = 4; /* private mailbox */
678 newroomtype = 0; /* public folder */
681 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
682 roomname, floornum, newroomtype);
684 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
686 cprintf("%s NO Mailbox already exists, or create failed\r\n",
690 cprintf("%s OK CREATE completed\r\n", parms[0]);
696 * Locate a room by its IMAP folder name, and check access to it
698 int imap_grabroom(char *returned_roomname, char *foldername) {
700 char augmented_roomname[ROOMNAMELEN];
701 char roomname[ROOMNAMELEN];
703 struct quickroom QRscratch;
707 ret = imap_roomname(roomname, sizeof roomname, foldername);
712 /* First try a regular match */
713 c = getroom(&QRscratch, roomname);
715 /* Then try a mailbox name match */
717 MailboxName(augmented_roomname, sizeof augmented_roomname,
718 &CC->usersupp, roomname);
719 c = getroom(&QRscratch, augmented_roomname);
721 strcpy(roomname, augmented_roomname);
724 /* If the room exists, check security/access */
726 /* See if there is an existing user/room relationship */
727 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
729 /* normal clients have to pass through security */
735 /* Fail here if no such room */
737 strcpy(returned_roomname, "");
741 strcpy(returned_roomname, QRscratch.QRname);
748 * Implements the STATUS command (sort of)
751 void imap_status(int num_parms, char *parms[]) {
753 char roomname[ROOMNAMELEN];
755 char savedroom[ROOMNAMELEN];
758 ret = imap_grabroom(roomname, parms[2]);
760 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
766 * usergoto() formally takes us to the desired room, happily returning
767 * the number of messages and number of new messages. (If another
768 * folder is selected, save its name so we can return there!!!!!)
770 if (IMAP->selected) {
771 strcpy(savedroom, CC->quickroom.QRname);
773 usergoto(roomname, 0, 0, &msgs, &new);
776 * Tell the client what it wants to know. In fact, tell it *more* than
777 * it wants to know. We happily IGnore the supplied status data item
778 * names and simply spew all possible data items. It's far easier to
779 * code and probably saves us some processing time too.
781 imap_mailboxname(buf, sizeof buf, &CC->quickroom);
782 cprintf("* STATUS ");
784 cprintf(" (MESSAGES %d ", msgs);
785 cprintf("RECENT 0 "); /* FIXME we need to implement this */
786 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
787 cprintf("UNSEEN %d)\r\n", new);
790 * If another folder is selected, go back to that room so we can resume
791 * our happy day without violent explosions.
793 if (IMAP->selected) {
794 usergoto(savedroom, 0, 0, &msgs, &new);
798 * Oooh, look, we're done!
800 cprintf("%s OK STATUS completed\r\n", parms[0]);
806 * Implements the SUBSCRIBE command
809 void imap_subscribe(int num_parms, char *parms[]) {
811 char roomname[ROOMNAMELEN];
812 char savedroom[ROOMNAMELEN];
815 ret = imap_grabroom(roomname, parms[2]);
817 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
823 * usergoto() formally takes us to the desired room, which has the side
824 * effect of marking the room as not-zapped ... exactly the effect
827 if (IMAP->selected) {
828 strcpy(savedroom, CC->quickroom.QRname);
830 usergoto(roomname, 0, 0, &msgs, &new);
833 * If another folder is selected, go back to that room so we can resume
834 * our happy day without violent explosions.
836 if (IMAP->selected) {
837 usergoto(savedroom, 0, 0, &msgs, &new);
840 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
845 * Implements the UNSUBSCRIBE command
848 void imap_unsubscribe(int num_parms, char *parms[]) {
850 char roomname[ROOMNAMELEN];
851 char savedroom[ROOMNAMELEN];
854 ret = imap_grabroom(roomname, parms[2]);
856 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
862 * usergoto() formally takes us to the desired room.
864 if (IMAP->selected) {
865 strcpy(savedroom, CC->quickroom.QRname);
867 usergoto(roomname, 0, 0, &msgs, &new);
870 * Now make the API call to zap the room
872 if (CtdlForgetThisRoom() == 0) {
873 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
876 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
881 * If another folder is selected, go back to that room so we can resume
882 * our happy day without violent explosions.
884 if (IMAP->selected) {
885 usergoto(savedroom, 0, 0, &msgs, &new);
892 * Implements the DELETE command
895 void imap_delete(int num_parms, char *parms[]) {
897 char roomname[ROOMNAMELEN];
898 char savedroom[ROOMNAMELEN];
901 ret = imap_grabroom(roomname, parms[2]);
903 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
909 * usergoto() formally takes us to the desired room, happily returning
910 * the number of messages and number of new messages. (If another
911 * folder is selected, save its name so we can return there!!!!!)
913 if (IMAP->selected) {
914 strcpy(savedroom, CC->quickroom.QRname);
916 usergoto(roomname, 0, 0, &msgs, &new);
919 * Now delete the room.
921 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom)) {
922 cprintf("%s OK DELETE completed\r\n", parms[0]);
923 delete_room(&CC->quickroom);
926 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
930 * If another folder is selected, go back to that room so we can resume
931 * our happy day without violent explosions.
933 if (IMAP->selected) {
934 usergoto(savedroom, 0, 0, &msgs, &new);
940 * Back end function for imap_rename()
942 void imap_rename_backend(struct quickroom *qrbuf, void *data) {
943 char foldername[SIZ];
944 char newfoldername[SIZ];
945 char newroomname[ROOMNAMELEN];
947 struct irl *irlp = NULL; /* scratch pointer */
948 struct irlparms *irlparms;
950 irlparms = (struct irlparms *)data;
951 imap_mailboxname(foldername, sizeof foldername, qrbuf);
953 /* Rename subfolders */
954 if ( (!strncasecmp(foldername, irlparms->oldname,
955 strlen(irlparms->oldname))
956 && (foldername[strlen(irlparms->oldname)] == '|')) ) {
958 sprintf(newfoldername, "%s|%s",
960 &foldername[strlen(irlparms->oldname)+1]
963 newfloor = imap_roomname(newroomname,
964 sizeof newroomname, newfoldername) & 0xFF;
966 irlp = (struct irl *) mallok(sizeof(struct irl));
967 strcpy(irlp->irl_newroom, newroomname);
968 strcpy(irlp->irl_oldroom, qrbuf->QRname);
969 irlp->irl_newfloor = newfloor;
970 irlp->next = *(irlparms->irl);
971 *(irlparms->irl) = irlp;
977 * Implements the RENAME command
980 void imap_rename(int num_parms, char *parms[]) {
981 char old_room[ROOMNAMELEN];
982 char new_room[ROOMNAMELEN];
986 struct irl *irl = NULL; /* the list */
987 struct irl *irlp = NULL; /* scratch pointer */
988 struct irlparms irlparms;
990 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
991 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
992 new_floor = (newr & 0xFF);
994 r = CtdlRenameRoom(old_room, new_room, new_floor);
996 if (r == crr_room_not_found) {
997 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
1000 if (r == crr_already_exists) {
1001 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
1004 if (r == crr_noneditable) {
1005 cprintf("%s This folder is not editable.\r\n", parms[0]);
1008 if (r == crr_invalid_floor) {
1009 cprintf("%s Folder root does not exist.\r\n", parms[0]);
1012 if (r == crr_access_denied) {
1013 cprintf("%s You do not have permission to edit "
1014 "this folder.\r\n", parms[0]);
1018 cprintf("%s NO Rename failed - undefined error %d\r\n",
1024 /* If this is the INBOX, then RFC2060 says we have to just move the
1025 * contents. In a Citadel environment it's easier to rename the room
1026 * (already did that) and create a new inbox.
1028 if (!strcasecmp(parms[2], "INBOX")) {
1029 create_room(MAILROOM, 4, "", 0, 1, 0);
1032 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1034 irlparms.oldname = parms[2];
1035 irlparms.newname = parms[3];
1036 irlparms.irl = &irl;
1037 ForEachRoom(imap_rename_backend, (void *)&irlparms);
1039 /* ... and now rename them. */
1040 while (irl != NULL) {
1041 r = CtdlRenameRoom(irl->irl_oldroom,
1042 irl->irl_newroom, irl->irl_newfloor);
1044 /* FIXME handle error returns better */
1045 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1053 cprintf("%s OK RENAME completed\r\n", parms[0]);
1060 * Main command loop for IMAP sessions.
1062 void imap_command_loop(void) {
1068 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1069 if (client_gets(cmdbuf) < 1) {
1070 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1075 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1076 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1079 /* strip off l/t whitespace and CRLF */
1080 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1081 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1084 /* If we're in the middle of a multi-line command, handle that */
1085 if (IMAP->authstate == imap_as_expecting_username) {
1086 imap_auth_login_user(cmdbuf);
1089 if (IMAP->authstate == imap_as_expecting_password) {
1090 imap_auth_login_pass(cmdbuf);
1095 /* Ok, at this point we're in normal command mode. The first thing
1096 * we do is print any incoming pages (yeah! we really do!)
1098 imap_print_express_messages();
1101 * Before processing the command that was just entered... if we happen
1102 * to have a folder selected, we'd like to rescan that folder for new
1103 * messages, and for deletions/changes of existing messages. This
1104 * could probably be optimized somehow, but IMAP sucks...
1106 if (IMAP->selected) {
1107 imap_rescan_msgids();
1110 /* Now for the command set. */
1112 /* Grab the tag, command, and parameters. Check syntax. */
1113 num_parms = imap_parameterize(parms, cmdbuf);
1114 if (num_parms < 2) {
1115 cprintf("BAD syntax error\r\n");
1118 /* The commands below may be executed in any state */
1120 else if ( (!strcasecmp(parms[1], "NOOP"))
1121 || (!strcasecmp(parms[1], "CHECK")) ) {
1122 cprintf("%s OK This command successfully did nothing.\r\n",
1126 else if (!strcasecmp(parms[1], "LOGOUT")) {
1127 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1128 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1133 else if (!strcasecmp(parms[1], "LOGIN")) {
1134 imap_login(num_parms, parms);
1137 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1138 imap_authenticate(num_parms, parms);
1141 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1142 imap_capability(num_parms, parms);
1145 else if (!CC->logged_in) {
1146 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1149 /* The commans below require a logged-in state */
1151 else if (!strcasecmp(parms[1], "SELECT")) {
1152 imap_select(num_parms, parms);
1155 else if (!strcasecmp(parms[1], "EXAMINE")) {
1156 imap_select(num_parms, parms);
1159 else if (!strcasecmp(parms[1], "LSUB")) {
1160 imap_lsub(num_parms, parms);
1163 else if (!strcasecmp(parms[1], "LIST")) {
1164 imap_list(num_parms, parms);
1167 else if (!strcasecmp(parms[1], "CREATE")) {
1168 imap_create(num_parms, parms);
1171 else if (!strcasecmp(parms[1], "DELETE")) {
1172 imap_delete(num_parms, parms);
1175 else if (!strcasecmp(parms[1], "RENAME")) {
1176 imap_rename(num_parms, parms);
1179 else if (!strcasecmp(parms[1], "STATUS")) {
1180 imap_status(num_parms, parms);
1183 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1184 imap_subscribe(num_parms, parms);
1187 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1188 imap_unsubscribe(num_parms, parms);
1191 else if (!strcasecmp(parms[1], "APPEND")) {
1192 imap_append(num_parms, parms);
1195 else if (IMAP->selected == 0) {
1196 cprintf("%s BAD no folder selected\r\n", parms[0]);
1199 /* The commands below require the SELECT state on a mailbox */
1201 else if (!strcasecmp(parms[1], "FETCH")) {
1202 imap_fetch(num_parms, parms);
1205 else if ( (!strcasecmp(parms[1], "UID"))
1206 && (!strcasecmp(parms[2], "FETCH")) ) {
1207 imap_uidfetch(num_parms, parms);
1210 else if (!strcasecmp(parms[1], "SEARCH")) {
1211 imap_search(num_parms, parms);
1214 else if ( (!strcasecmp(parms[1], "UID"))
1215 && (!strcasecmp(parms[2], "SEARCH")) ) {
1216 imap_uidsearch(num_parms, parms);
1219 else if (!strcasecmp(parms[1], "STORE")) {
1220 imap_store(num_parms, parms);
1223 else if ( (!strcasecmp(parms[1], "UID"))
1224 && (!strcasecmp(parms[2], "STORE")) ) {
1225 imap_uidstore(num_parms, parms);
1228 else if (!strcasecmp(parms[1], "COPY")) {
1229 imap_copy(num_parms, parms);
1232 else if ( (!strcasecmp(parms[1], "UID"))
1233 && (!strcasecmp(parms[2], "COPY")) ) {
1234 imap_uidcopy(num_parms, parms);
1237 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1238 imap_expunge(num_parms, parms);
1241 else if (!strcasecmp(parms[1], "CLOSE")) {
1242 imap_close(num_parms, parms);
1245 /* End of commands. If we get here, the command is either invalid
1250 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1253 /* If the client transmitted a message we can free it now */
1254 imap_free_transmitted_message();
1260 * This function is called to register the IMAP extension with Citadel.
1262 char *serv_imap_init(void)
1264 SYM_IMAP = CtdlGetDynamicSymbol();
1265 CtdlRegisterServiceHook(config.c_imap_port,
1269 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);