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.
25 #include <sys/types.h>
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
44 #include "sysdep_decls.h"
45 #include "citserver.h"
48 #include "dynloader.h"
55 #include "internet_addressing.h"
56 #include "serv_imap.h"
57 #include "imap_tools.h"
58 #include "imap_fetch.h"
59 #include "imap_search.h"
60 #include "imap_store.h"
61 #include "imap_misc.h"
68 * If there is a message ID map in memory, free it
70 void imap_free_msgids(void) {
71 if (IMAP->msgids != NULL) {
76 if (IMAP->flags != NULL) {
84 * If there is a transmitted message in memory, free it
86 void imap_free_transmitted_message(void) {
87 if (IMAP->transmitted_message != NULL) {
88 phree(IMAP->transmitted_message);
89 IMAP->transmitted_message = NULL;
90 IMAP->transmitted_length = 0;
96 * Set the \\Seen flag for messages which aren't new
98 void imap_set_seen_flags(void) {
102 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
103 if (IMAP->num_msgs > 0) {
104 for (i=0; i<IMAP->num_msgs; ++i) {
105 if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
106 IMAP->flags[i] |= IMAP_SEEN;
116 * Back end for imap_load_msgids()
118 * Optimization: instead of calling realloc() to add each message, we
119 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
120 * allows the mapping to proceed much faster.
122 void imap_add_single_msgid(long msgnum, void *userdata) {
124 IMAP->num_msgs = IMAP->num_msgs + 1;
125 if (IMAP->msgids == NULL) {
126 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long)
127 * REALLOC_INCREMENT);
129 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
130 IMAP->msgids = reallok(IMAP->msgids,
131 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
133 if (IMAP->flags == NULL) {
134 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long)
135 * REALLOC_INCREMENT);
137 else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
138 IMAP->flags = reallok(IMAP->flags,
139 (IMAP->num_msgs + REALLOC_INCREMENT) * sizeof(long));
141 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
142 IMAP->flags[IMAP->num_msgs - 1] = 0;
148 * Set up a message ID map for the current room (folder)
150 void imap_load_msgids(void) {
152 if (IMAP->selected == 0) {
153 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
157 imap_free_msgids(); /* If there was already a map, free it */
159 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
160 imap_add_single_msgid, NULL);
162 imap_set_seen_flags();
163 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
168 * Re-scan the selected room (folder) and see if it's been changed at all
170 void imap_rescan_msgids(void) {
172 int original_num_msgs = 0;
173 long original_highest = 0L;
177 if (IMAP->selected == 0) {
178 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
184 * Check to see if any of the messages we know about have been expunged
186 if (IMAP->num_msgs > 0)
187 for (i=0; i<IMAP->num_msgs; ++i) {
189 count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
190 NULL, NULL, NULL, NULL);
193 cprintf("* %d EXPUNGE\r\n", i+1);
195 /* Here's some nice stupid nonsense. When a message
196 * is expunged, we have to slide all the existing
197 * messages up in the message array.
200 memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
201 (sizeof(long)*(IMAP->num_msgs-i)) );
202 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
203 (sizeof(long)*(IMAP->num_msgs-i)) );
211 * Remember how many messages were here before we re-scanned.
213 original_num_msgs = IMAP->num_msgs;
214 if (IMAP->num_msgs > 0) {
215 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
218 original_highest = 0L;
222 * Now peruse the room for *new* messages only.
224 CtdlForEachMessage(MSGS_GT, original_highest, NULL, NULL,
225 imap_add_single_msgid, NULL);
227 imap_set_seen_flags();
230 * If new messages have arrived, tell the client about them.
232 if (IMAP->num_msgs > original_num_msgs) {
233 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
245 * This cleanup function blows away the temporary memory and files used by
248 void imap_cleanup_function(void) {
250 /* Don't do this stuff if this is not a IMAP session! */
251 if (CC->h_command_function != imap_command_loop) return;
253 lprintf(9, "Performing IMAP cleanup hook\n");
255 imap_free_transmitted_message();
256 lprintf(9, "Finished IMAP cleanup hook\n");
262 * Here's where our IMAP session begins its happy day.
264 void imap_greeting(void) {
266 strcpy(CC->cs_clientname, "IMAP session");
267 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
268 IMAP->authstate = imap_as_normal;
270 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
276 * implements the LOGIN command (ordinary username/password login)
278 void imap_login(int num_parms, char *parms[]) {
279 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
280 if (CtdlTryPassword(parms[3]) == pass_ok) {
281 cprintf("%s OK login successful\r\n", parms[0]);
286 cprintf("%s BAD Login incorrect\r\n", parms[0]);
291 * Implements the AUTHENTICATE command
293 void imap_authenticate(int num_parms, char *parms[]) {
296 if (num_parms != 3) {
297 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
301 if (!strcasecmp(parms[2], "LOGIN")) {
302 encode_base64(buf, "Username:");
303 cprintf("+ %s\r\n", buf);
304 IMAP->authstate = imap_as_expecting_username;
305 strcpy(IMAP->authseq, parms[0]);
310 cprintf("%s NO AUTHENTICATE %s failed\r\n",
315 void imap_auth_login_user(char *cmd) {
318 decode_base64(buf, cmd, SIZ);
319 CtdlLoginExistingUser(buf);
320 encode_base64(buf, "Password:");
321 cprintf("+ %s\r\n", buf);
322 IMAP->authstate = imap_as_expecting_password;
326 void imap_auth_login_pass(char *cmd) {
329 decode_base64(buf, cmd, SIZ);
330 if (CtdlTryPassword(buf) == pass_ok) {
331 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
334 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
336 IMAP->authstate = imap_as_normal;
343 * implements the CAPABILITY command
345 void imap_capability(int num_parms, char *parms[]) {
346 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
347 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
355 * implements the SELECT command
357 void imap_select(int num_parms, char *parms[]) {
359 char augmented_roomname[ROOMNAMELEN];
363 struct quickroom QRscratch;
369 /* Convert the supplied folder name to a roomname */
370 i = imap_roomname(towhere, sizeof towhere, parms[2]);
372 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
376 floornum = (i & 0x00ff);
377 roomflags = (i & 0xff00);
379 /* First try a regular match */
380 c = getroom(&QRscratch, towhere);
382 /* Then try a mailbox name match */
384 MailboxName(augmented_roomname, sizeof augmented_roomname,
385 &CC->usersupp, towhere);
386 c = getroom(&QRscratch, augmented_roomname);
388 strcpy(towhere, augmented_roomname);
391 /* If the room exists, check security/access */
393 /* See if there is an existing user/room relationship */
394 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
396 /* normal clients have to pass through security */
402 /* Fail here if no such room */
404 cprintf("%s NO ... no such room, or access denied\r\n",
411 * usergoto() formally takes us to the desired room, happily returning
412 * the number of messages and number of new messages.
414 memcpy(&CC->quickroom, &QRscratch, sizeof(struct quickroom));
415 usergoto(NULL, 0, &msgs, &new);
418 if (!strcasecmp(parms[1], "EXAMINE")) {
427 /* FIXME ... much more info needs to be supplied here */
428 cprintf("* %d EXISTS\r\n", msgs);
429 cprintf("* %d RECENT\r\n", new);
430 cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
431 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
432 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
433 cprintf("%s OK [%s] %s completed\r\n",
435 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
442 * does the real work for expunge
444 int imap_do_expunge(void) {
446 int num_expunged = 0;
448 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
449 if (IMAP->flags[i] & IMAP_DELETED) {
450 CtdlDeleteMessages(CC->quickroom.QRname,
451 IMAP->msgids[i], "");
456 if (num_expunged > 0) {
457 imap_rescan_msgids();
460 return(num_expunged);
465 * implements the EXPUNGE command syntax
467 void imap_expunge(int num_parms, char *parms[]) {
468 int num_expunged = 0;
470 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
475 * implements the CLOSE command
477 void imap_close(int num_parms, char *parms[]) {
479 /* Yes, we always expunge on close. */
485 cprintf("%s OK CLOSE completed\r\n", parms[0]);
492 * Used by LIST and LSUB to show the floors in the listing
494 void imap_list_floors(char *cmd, char *pattern) {
498 for (i=0; i<MAXFLOORS; ++i) {
500 if (fl->f_flags & F_INUSE) {
501 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
502 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
503 imap_strout(fl->f_name);
513 * Back end for imap_lsub()
515 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
516 * may or may not be the desired behavior in the future.
518 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
523 pattern = (char *)data;
525 /* Only list rooms to which the user has access!! */
526 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
528 imap_mailboxname(buf, sizeof buf, qrbuf);
529 if (imap_mailbox_matches_pattern(pattern, buf)) {
530 cprintf("* LSUB () \"|\" ");
539 * Implements the LSUB command
541 void imap_lsub(int num_parms, char *parms[]) {
544 cprintf("%s BAD arguments invalid\r\n", parms[0]);
547 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
549 if (strlen(parms[3])==0) {
550 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
554 imap_list_floors("LSUB", pattern);
555 ForEachRoom(imap_lsub_listroom, pattern);
558 cprintf("%s OK LSUB completed\r\n", parms[0]);
564 * Back end for imap_list()
566 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
571 pattern = (char *)data;
573 /* Only list rooms to which the user has access!! */
574 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
576 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
577 imap_mailboxname(buf, sizeof buf, qrbuf);
578 if (imap_mailbox_matches_pattern(pattern, buf)) {
579 cprintf("* LIST () \"|\" ");
588 * Implements the LIST command
590 void imap_list(int num_parms, char *parms[]) {
593 cprintf("%s BAD arguments invalid\r\n", parms[0]);
596 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
598 if (strlen(parms[3])==0) {
599 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
603 imap_list_floors("LIST", pattern);
604 ForEachRoom(imap_list_listroom, pattern);
607 cprintf("%s OK LIST completed\r\n", parms[0]);
613 * Implements the CREATE command
616 void imap_create(int num_parms, char *parms[]) {
618 char roomname[ROOMNAMELEN];
623 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
625 cprintf("%s NO Invalid mailbox name or location\r\n",
629 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
630 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
632 if (flags & IR_MAILBOX) {
633 newroomtype = 4; /* private mailbox */
636 newroomtype = 0; /* public folder */
639 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
640 roomname, floornum, newroomtype);
642 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
644 cprintf("%s NO Mailbox already exists, or create failed\r\n",
648 cprintf("%s OK CREATE completed\r\n", parms[0]);
654 * Locate a room by its IMAP folder name, and check access to it
656 int imap_grabroom(char *returned_roomname, char *foldername) {
658 char augmented_roomname[ROOMNAMELEN];
659 char roomname[ROOMNAMELEN];
661 struct quickroom QRscratch;
665 ret = imap_roomname(roomname, sizeof roomname, foldername);
670 /* First try a regular match */
671 c = getroom(&QRscratch, roomname);
673 /* Then try a mailbox name match */
675 MailboxName(augmented_roomname, sizeof augmented_roomname,
676 &CC->usersupp, roomname);
677 c = getroom(&QRscratch, augmented_roomname);
679 strcpy(roomname, augmented_roomname);
682 /* If the room exists, check security/access */
684 /* See if there is an existing user/room relationship */
685 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
687 /* normal clients have to pass through security */
693 /* Fail here if no such room */
695 strcpy(returned_roomname, "");
699 strcpy(returned_roomname, QRscratch.QRname);
706 * Implements the STATUS command (sort of)
709 void imap_status(int num_parms, char *parms[]) {
711 char roomname[ROOMNAMELEN];
713 char savedroom[ROOMNAMELEN];
716 ret = imap_grabroom(roomname, parms[2]);
718 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
724 * usergoto() formally takes us to the desired room, happily returning
725 * the number of messages and number of new messages. (If another
726 * folder is selected, save its name so we can return there!!!!!)
728 if (IMAP->selected) {
729 strcpy(savedroom, CC->quickroom.QRname);
731 usergoto(roomname, 0, &msgs, &new);
734 * Tell the client what it wants to know. In fact, tell it *more* than
735 * it wants to know. We happily IGnore the supplied status data item
736 * names and simply spew all possible data items. It's far easier to
737 * code and probably saves us some processing time too.
739 imap_mailboxname(buf, sizeof buf, &CC->quickroom);
740 cprintf("* STATUS ");
742 cprintf(" (MESSAGES %d ", msgs);
743 cprintf("RECENT 0 "); /* FIXME we need to implement this */
744 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
745 cprintf("UNSEEN %d)\r\n", new);
748 * If another folder is selected, go back to that room so we can resume
749 * our happy day without violent explosions.
751 if (IMAP->selected) {
752 usergoto(savedroom, 0, &msgs, &new);
756 * Oooh, look, we're done!
758 cprintf("%s OK STATUS completed\r\n", parms[0]);
764 * Implements the SUBSCRIBE command
767 void imap_subscribe(int num_parms, char *parms[]) {
769 char roomname[ROOMNAMELEN];
770 char savedroom[ROOMNAMELEN];
773 ret = imap_grabroom(roomname, parms[2]);
775 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
781 * usergoto() formally takes us to the desired room, which has the side
782 * effect of marking the room as not-zapped ... exactly the effect
785 if (IMAP->selected) {
786 strcpy(savedroom, CC->quickroom.QRname);
788 usergoto(roomname, 0, &msgs, &new);
791 * If another folder is selected, go back to that room so we can resume
792 * our happy day without violent explosions.
794 if (IMAP->selected) {
795 usergoto(savedroom, 0, &msgs, &new);
798 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
803 * Implements the UNSUBSCRIBE command
806 void imap_unsubscribe(int num_parms, char *parms[]) {
808 char roomname[ROOMNAMELEN];
809 char savedroom[ROOMNAMELEN];
812 ret = imap_grabroom(roomname, parms[2]);
814 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
820 * usergoto() formally takes us to the desired room.
822 if (IMAP->selected) {
823 strcpy(savedroom, CC->quickroom.QRname);
825 usergoto(roomname, 0, &msgs, &new);
828 * Now make the API call to zap the room
830 if (CtdlForgetThisRoom() == 0) {
831 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
834 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
839 * If another folder is selected, go back to that room so we can resume
840 * our happy day without violent explosions.
842 if (IMAP->selected) {
843 usergoto(savedroom, 0, &msgs, &new);
850 * Implements the DELETE command
853 void imap_delete(int num_parms, char *parms[]) {
855 char roomname[ROOMNAMELEN];
856 char savedroom[ROOMNAMELEN];
859 ret = imap_grabroom(roomname, parms[2]);
861 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
867 * usergoto() formally takes us to the desired room, happily returning
868 * the number of messages and number of new messages. (If another
869 * folder is selected, save its name so we can return there!!!!!)
871 if (IMAP->selected) {
872 strcpy(savedroom, CC->quickroom.QRname);
874 usergoto(roomname, 0, &msgs, &new);
877 * Now delete the room.
879 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom)) {
880 cprintf("%s OK DELETE completed\r\n", parms[0]);
881 delete_room(&CC->quickroom);
884 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
888 * If another folder is selected, go back to that room so we can resume
889 * our happy day without violent explosions.
891 if (IMAP->selected) {
892 usergoto(savedroom, 0, &msgs, &new);
898 * Implements the RENAME command
901 void imap_rename(int num_parms, char *parms[]) {
902 char old_room[ROOMNAMELEN];
903 char new_room[ROOMNAMELEN];
908 /* struct containing list of rooms to rename */
911 char irl_oldroom[ROOMNAMELEN];
912 char irl_newroom[ROOMNAMELEN];
915 struct irl *irl = NULL; /* the list */
916 struct irl *irlp = NULL; /* scratch pointer */
919 * Back end function for imap_rename()
921 void imap_rename_backend(struct quickroom *qrbuf, void *data) {
922 char foldername[SIZ];
923 char newfoldername[SIZ];
924 char newroomname[ROOMNAMELEN];
927 imap_mailboxname(foldername, sizeof foldername, qrbuf);
929 if ( (!strncasecmp(foldername, parms[2], strlen(parms[2]))
930 && (foldername[strlen(parms[2])] == '|')) ) {
932 sprintf(newfoldername, "%s|%s",
934 &foldername[strlen(parms[2])+1]
937 newfloor = imap_roomname(newroomname,
938 sizeof newroomname, newfoldername) & 0xFF;
940 irlp = (struct irl *) mallok(sizeof(struct irl));
941 strcpy(irlp->irl_newroom, newroomname);
942 strcpy(irlp->irl_oldroom, qrbuf->QRname);
943 irlp->irl_newfloor = newfloor;
950 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
951 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
952 new_floor = (newr & 0xFF);
954 r = CtdlRenameRoom(old_room, new_room, new_floor);
956 if (r == crr_room_not_found) {
957 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
960 if (r == crr_already_exists) {
961 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
964 if (r == crr_noneditable) {
965 cprintf("%s This folder is not editable.\r\n", parms[0]);
968 if (r == crr_invalid_floor) {
969 cprintf("%s Folder root does not exist.\r\n", parms[0]);
972 if (r == crr_access_denied) {
973 cprintf("%s You do not have permission to edit "
974 "this folder.\r\n", parms[0]);
978 cprintf("%s NO Rename failed - undefined error %d\r\n",
984 /* If this is the INBOX, then RFC2060 says we have to just move the
985 * contents. In a Citadel environment it's easier to rename the room
986 * (already did that) and create a new inbox.
988 if (!strcasecmp(parms[2], "INBOX")) {
989 create_room(MAILROOM, 4, "", 0, 1, 0);
992 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
994 ForEachRoom(imap_rename_backend, NULL);
996 /* ... and now rename them. */
997 while (irl != NULL) {
998 r = CtdlRenameRoom(irl->irl_oldroom,
999 irl->irl_newroom, irl->irl_newfloor);
1001 /* FIXME handle error returns better */
1002 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1010 cprintf("%s OK RENAME completed\r\n", parms[0]);
1017 * Main command loop for IMAP sessions.
1019 void imap_command_loop(void) {
1025 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1026 if (client_gets(cmdbuf) < 1) {
1027 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1032 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1033 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1036 /* strip off l/t whitespace and CRLF */
1037 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1038 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1041 /* If we're in the middle of a multi-line command, handle that */
1042 if (IMAP->authstate == imap_as_expecting_username) {
1043 imap_auth_login_user(cmdbuf);
1046 if (IMAP->authstate == imap_as_expecting_password) {
1047 imap_auth_login_pass(cmdbuf);
1052 /* Ok, at this point we're in normal command mode. The first thing
1053 * we do is print any incoming pages (yeah! we really do!)
1055 imap_print_express_messages();
1058 * Before processing the command that was just entered... if we happen
1059 * to have a folder selected, we'd like to rescan that folder for new
1060 * messages, and for deletions/changes of existing messages. This
1061 * could probably be optimized somehow, but IMAP sucks...
1063 if (IMAP->selected) {
1064 imap_rescan_msgids();
1067 /* Now for the command set. */
1069 /* Grab the tag, command, and parameters. Check syntax. */
1070 num_parms = imap_parameterize(parms, cmdbuf);
1071 if (num_parms < 2) {
1072 cprintf("BAD syntax error\r\n");
1075 /* The commands below may be executed in any state */
1077 else if ( (!strcasecmp(parms[1], "NOOP"))
1078 || (!strcasecmp(parms[1], "CHECK")) ) {
1079 cprintf("%s OK This command successfully did nothing.\r\n",
1083 else if (!strcasecmp(parms[1], "LOGOUT")) {
1084 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1085 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1090 else if (!strcasecmp(parms[1], "LOGIN")) {
1091 imap_login(num_parms, parms);
1094 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1095 imap_authenticate(num_parms, parms);
1098 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1099 imap_capability(num_parms, parms);
1102 else if (!CC->logged_in) {
1103 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1106 /* The commans below require a logged-in state */
1108 else if (!strcasecmp(parms[1], "SELECT")) {
1109 imap_select(num_parms, parms);
1112 else if (!strcasecmp(parms[1], "EXAMINE")) {
1113 imap_select(num_parms, parms);
1116 else if (!strcasecmp(parms[1], "LSUB")) {
1117 imap_lsub(num_parms, parms);
1120 else if (!strcasecmp(parms[1], "LIST")) {
1121 imap_list(num_parms, parms);
1124 else if (!strcasecmp(parms[1], "CREATE")) {
1125 imap_create(num_parms, parms);
1128 else if (!strcasecmp(parms[1], "DELETE")) {
1129 imap_delete(num_parms, parms);
1132 else if (!strcasecmp(parms[1], "RENAME")) {
1133 imap_rename(num_parms, parms);
1136 else if (!strcasecmp(parms[1], "STATUS")) {
1137 imap_status(num_parms, parms);
1140 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1141 imap_subscribe(num_parms, parms);
1144 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1145 imap_unsubscribe(num_parms, parms);
1148 else if (!strcasecmp(parms[1], "APPEND")) {
1149 imap_append(num_parms, parms);
1152 else if (IMAP->selected == 0) {
1153 cprintf("%s BAD no folder selected\r\n", parms[0]);
1156 /* The commands below require the SELECT state on a mailbox */
1158 else if (!strcasecmp(parms[1], "FETCH")) {
1159 imap_fetch(num_parms, parms);
1162 else if ( (!strcasecmp(parms[1], "UID"))
1163 && (!strcasecmp(parms[2], "FETCH")) ) {
1164 imap_uidfetch(num_parms, parms);
1167 else if (!strcasecmp(parms[1], "SEARCH")) {
1168 imap_search(num_parms, parms);
1171 else if ( (!strcasecmp(parms[1], "UID"))
1172 && (!strcasecmp(parms[2], "SEARCH")) ) {
1173 imap_uidsearch(num_parms, parms);
1176 else if (!strcasecmp(parms[1], "STORE")) {
1177 imap_store(num_parms, parms);
1180 else if ( (!strcasecmp(parms[1], "UID"))
1181 && (!strcasecmp(parms[2], "STORE")) ) {
1182 imap_uidstore(num_parms, parms);
1185 else if (!strcasecmp(parms[1], "COPY")) {
1186 imap_copy(num_parms, parms);
1189 else if ( (!strcasecmp(parms[1], "UID"))
1190 && (!strcasecmp(parms[2], "COPY")) ) {
1191 imap_uidcopy(num_parms, parms);
1194 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1195 imap_expunge(num_parms, parms);
1198 else if (!strcasecmp(parms[1], "CLOSE")) {
1199 imap_close(num_parms, parms);
1202 /* End of commands. If we get here, the command is either invalid
1207 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1210 /* If the client transmitted a message we can free it now */
1211 imap_free_transmitted_message();
1217 * This function is called by dynloader.c to register the IMAP module
1218 * with the Citadel server.
1220 char *Dynamic_Module_Init(void)
1222 SYM_IMAP = CtdlGetDynamicSymbol();
1223 CtdlRegisterServiceHook(config.c_imap_port,
1227 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);