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;
190 if (IMAP->selected == 0) {
191 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
197 * Check to see if any of the messages we know about have been expunged
199 if (IMAP->num_msgs > 0)
200 for (i=0; i<IMAP->num_msgs; ++i) {
202 count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
203 NULL, NULL, NULL, NULL);
206 cprintf("* %d EXPUNGE\r\n", i+1);
208 /* Here's some nice stupid nonsense. When a message
209 * is expunged, we have to slide all the existing
210 * messages up in the message array.
213 memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
214 (sizeof(long)*(IMAP->num_msgs-i)) );
215 memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
216 (sizeof(long)*(IMAP->num_msgs-i)) );
224 * Remember how many messages were here before we re-scanned.
226 original_num_msgs = IMAP->num_msgs;
227 if (IMAP->num_msgs > 0) {
228 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
231 original_highest = 0L;
235 * Now peruse the room for *new* messages only.
237 CtdlForEachMessage(MSGS_GT, original_highest, NULL, NULL,
238 imap_add_single_msgid, NULL);
240 imap_set_seen_flags();
243 * If new messages have arrived, tell the client about them.
245 if (IMAP->num_msgs > original_num_msgs) {
246 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
258 * This cleanup function blows away the temporary memory and files used by
261 void imap_cleanup_function(void) {
263 /* Don't do this stuff if this is not a IMAP session! */
264 if (CC->h_command_function != imap_command_loop) return;
266 lprintf(9, "Performing IMAP cleanup hook\n");
268 imap_free_transmitted_message();
269 lprintf(9, "Finished IMAP cleanup hook\n");
275 * Here's where our IMAP session begins its happy day.
277 void imap_greeting(void) {
279 strcpy(CC->cs_clientname, "IMAP session");
280 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
281 IMAP->authstate = imap_as_normal;
283 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
289 * implements the LOGIN command (ordinary username/password login)
291 void imap_login(int num_parms, char *parms[]) {
292 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
293 if (CtdlTryPassword(parms[3]) == pass_ok) {
294 cprintf("%s OK login successful\r\n", parms[0]);
299 cprintf("%s BAD Login incorrect\r\n", parms[0]);
304 * Implements the AUTHENTICATE command
306 void imap_authenticate(int num_parms, char *parms[]) {
309 if (num_parms != 3) {
310 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
314 if (!strcasecmp(parms[2], "LOGIN")) {
315 CtdlEncodeBase64(buf, "Username:", 9);
316 cprintf("+ %s\r\n", buf);
317 IMAP->authstate = imap_as_expecting_username;
318 strcpy(IMAP->authseq, parms[0]);
323 cprintf("%s NO AUTHENTICATE %s failed\r\n",
328 void imap_auth_login_user(char *cmd) {
331 CtdlDecodeBase64(buf, cmd, SIZ);
332 CtdlLoginExistingUser(buf);
333 CtdlEncodeBase64(buf, "Password:", 9);
334 cprintf("+ %s\r\n", buf);
335 IMAP->authstate = imap_as_expecting_password;
339 void imap_auth_login_pass(char *cmd) {
342 CtdlDecodeBase64(buf, cmd, SIZ);
343 if (CtdlTryPassword(buf) == pass_ok) {
344 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
347 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
349 IMAP->authstate = imap_as_normal;
356 * implements the CAPABILITY command
358 void imap_capability(int num_parms, char *parms[]) {
359 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
360 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
368 * implements the SELECT command
370 void imap_select(int num_parms, char *parms[]) {
372 char augmented_roomname[ROOMNAMELEN];
376 struct quickroom QRscratch;
382 /* Convert the supplied folder name to a roomname */
383 i = imap_roomname(towhere, sizeof towhere, parms[2]);
385 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
389 floornum = (i & 0x00ff);
390 roomflags = (i & 0xff00);
392 /* First try a regular match */
393 c = getroom(&QRscratch, towhere);
395 /* Then try a mailbox name match */
397 MailboxName(augmented_roomname, sizeof augmented_roomname,
398 &CC->usersupp, towhere);
399 c = getroom(&QRscratch, augmented_roomname);
401 strcpy(towhere, augmented_roomname);
404 /* If the room exists, check security/access */
406 /* See if there is an existing user/room relationship */
407 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
409 /* normal clients have to pass through security */
415 /* Fail here if no such room */
417 cprintf("%s NO ... no such room, or access denied\r\n",
424 * usergoto() formally takes us to the desired room, happily returning
425 * the number of messages and number of new messages.
427 memcpy(&CC->quickroom, &QRscratch, sizeof(struct quickroom));
428 usergoto(NULL, 0, 0, &msgs, &new);
431 if (!strcasecmp(parms[1], "EXAMINE")) {
440 /* FIXME ... much more info needs to be supplied here */
441 cprintf("* %d EXISTS\r\n", msgs);
442 cprintf("* %d RECENT\r\n", new);
443 cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
444 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
445 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
446 cprintf("%s OK [%s] %s completed\r\n",
448 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
455 * does the real work for expunge
457 int imap_do_expunge(void) {
459 int num_expunged = 0;
461 if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
462 if (IMAP->flags[i] & IMAP_DELETED) {
463 CtdlDeleteMessages(CC->quickroom.QRname,
464 IMAP->msgids[i], "");
469 if (num_expunged > 0) {
470 imap_rescan_msgids();
473 return(num_expunged);
478 * implements the EXPUNGE command syntax
480 void imap_expunge(int num_parms, char *parms[]) {
481 int num_expunged = 0;
483 cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
488 * implements the CLOSE command
490 void imap_close(int num_parms, char *parms[]) {
492 /* Yes, we always expunge on close. */
498 cprintf("%s OK CLOSE completed\r\n", parms[0]);
505 * Used by LIST and LSUB to show the floors in the listing
507 void imap_list_floors(char *cmd, char *pattern) {
511 for (i=0; i<MAXFLOORS; ++i) {
513 if (fl->f_flags & F_INUSE) {
514 if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
515 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
516 imap_strout(fl->f_name);
526 * Back end for imap_lsub()
528 * IMAP "subscribed folder" is equivocated to Citadel "known rooms." This
529 * may or may not be the desired behavior in the future.
531 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
536 pattern = (char *)data;
538 /* Only list rooms to which the user has access!! */
539 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
541 imap_mailboxname(buf, sizeof buf, qrbuf);
542 if (imap_mailbox_matches_pattern(pattern, buf)) {
543 cprintf("* LSUB () \"|\" ");
552 * Implements the LSUB command
554 void imap_lsub(int num_parms, char *parms[]) {
557 cprintf("%s BAD arguments invalid\r\n", parms[0]);
560 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
562 if (strlen(parms[3])==0) {
563 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
567 imap_list_floors("LSUB", pattern);
568 ForEachRoom(imap_lsub_listroom, pattern);
571 cprintf("%s OK LSUB completed\r\n", parms[0]);
577 * Back end for imap_list()
579 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
584 pattern = (char *)data;
586 /* Only list rooms to which the user has access!! */
587 ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
589 || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
590 imap_mailboxname(buf, sizeof buf, qrbuf);
591 if (imap_mailbox_matches_pattern(pattern, buf)) {
592 cprintf("* LIST () \"|\" ");
601 * Implements the LIST command
603 void imap_list(int num_parms, char *parms[]) {
606 cprintf("%s BAD arguments invalid\r\n", parms[0]);
609 snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
611 if (strlen(parms[3])==0) {
612 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
616 imap_list_floors("LIST", pattern);
617 ForEachRoom(imap_list_listroom, pattern);
620 cprintf("%s OK LIST completed\r\n", parms[0]);
626 * Implements the CREATE command
629 void imap_create(int num_parms, char *parms[]) {
631 char roomname[ROOMNAMELEN];
636 ret = imap_roomname(roomname, sizeof roomname, parms[2]);
638 cprintf("%s NO Invalid mailbox name or location\r\n",
642 floornum = ( ret & 0x00ff ); /* lower 8 bits = floor number */
643 flags = ( ret & 0xff00 ); /* upper 8 bits = flags */
645 if (flags & IR_MAILBOX) {
646 newroomtype = 4; /* private mailbox */
649 newroomtype = 0; /* public folder */
652 lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
653 roomname, floornum, newroomtype);
655 ret = create_room(roomname, newroomtype, "", floornum, 1, 0);
657 cprintf("%s NO Mailbox already exists, or create failed\r\n",
661 cprintf("%s OK CREATE completed\r\n", parms[0]);
667 * Locate a room by its IMAP folder name, and check access to it
669 int imap_grabroom(char *returned_roomname, char *foldername) {
671 char augmented_roomname[ROOMNAMELEN];
672 char roomname[ROOMNAMELEN];
674 struct quickroom QRscratch;
678 ret = imap_roomname(roomname, sizeof roomname, foldername);
683 /* First try a regular match */
684 c = getroom(&QRscratch, roomname);
686 /* Then try a mailbox name match */
688 MailboxName(augmented_roomname, sizeof augmented_roomname,
689 &CC->usersupp, roomname);
690 c = getroom(&QRscratch, augmented_roomname);
692 strcpy(roomname, augmented_roomname);
695 /* If the room exists, check security/access */
697 /* See if there is an existing user/room relationship */
698 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
700 /* normal clients have to pass through security */
706 /* Fail here if no such room */
708 strcpy(returned_roomname, "");
712 strcpy(returned_roomname, QRscratch.QRname);
719 * Implements the STATUS command (sort of)
722 void imap_status(int num_parms, char *parms[]) {
724 char roomname[ROOMNAMELEN];
726 char savedroom[ROOMNAMELEN];
729 ret = imap_grabroom(roomname, parms[2]);
731 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
737 * usergoto() formally takes us to the desired room, happily returning
738 * the number of messages and number of new messages. (If another
739 * folder is selected, save its name so we can return there!!!!!)
741 if (IMAP->selected) {
742 strcpy(savedroom, CC->quickroom.QRname);
744 usergoto(roomname, 0, 0, &msgs, &new);
747 * Tell the client what it wants to know. In fact, tell it *more* than
748 * it wants to know. We happily IGnore the supplied status data item
749 * names and simply spew all possible data items. It's far easier to
750 * code and probably saves us some processing time too.
752 imap_mailboxname(buf, sizeof buf, &CC->quickroom);
753 cprintf("* STATUS ");
755 cprintf(" (MESSAGES %d ", msgs);
756 cprintf("RECENT 0 "); /* FIXME we need to implement this */
757 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
758 cprintf("UNSEEN %d)\r\n", new);
761 * If another folder is selected, go back to that room so we can resume
762 * our happy day without violent explosions.
764 if (IMAP->selected) {
765 usergoto(savedroom, 0, 0, &msgs, &new);
769 * Oooh, look, we're done!
771 cprintf("%s OK STATUS completed\r\n", parms[0]);
777 * Implements the SUBSCRIBE command
780 void imap_subscribe(int num_parms, char *parms[]) {
782 char roomname[ROOMNAMELEN];
783 char savedroom[ROOMNAMELEN];
786 ret = imap_grabroom(roomname, parms[2]);
788 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
794 * usergoto() formally takes us to the desired room, which has the side
795 * effect of marking the room as not-zapped ... exactly the effect
798 if (IMAP->selected) {
799 strcpy(savedroom, CC->quickroom.QRname);
801 usergoto(roomname, 0, 0, &msgs, &new);
804 * If another folder is selected, go back to that room so we can resume
805 * our happy day without violent explosions.
807 if (IMAP->selected) {
808 usergoto(savedroom, 0, 0, &msgs, &new);
811 cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
816 * Implements the UNSUBSCRIBE command
819 void imap_unsubscribe(int num_parms, char *parms[]) {
821 char roomname[ROOMNAMELEN];
822 char savedroom[ROOMNAMELEN];
825 ret = imap_grabroom(roomname, parms[2]);
827 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
833 * usergoto() formally takes us to the desired room.
835 if (IMAP->selected) {
836 strcpy(savedroom, CC->quickroom.QRname);
838 usergoto(roomname, 0, 0, &msgs, &new);
841 * Now make the API call to zap the room
843 if (CtdlForgetThisRoom() == 0) {
844 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
847 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
852 * If another folder is selected, go back to that room so we can resume
853 * our happy day without violent explosions.
855 if (IMAP->selected) {
856 usergoto(savedroom, 0, 0, &msgs, &new);
863 * Implements the DELETE command
866 void imap_delete(int num_parms, char *parms[]) {
868 char roomname[ROOMNAMELEN];
869 char savedroom[ROOMNAMELEN];
872 ret = imap_grabroom(roomname, parms[2]);
874 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
880 * usergoto() formally takes us to the desired room, happily returning
881 * the number of messages and number of new messages. (If another
882 * folder is selected, save its name so we can return there!!!!!)
884 if (IMAP->selected) {
885 strcpy(savedroom, CC->quickroom.QRname);
887 usergoto(roomname, 0, 0, &msgs, &new);
890 * Now delete the room.
892 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom)) {
893 cprintf("%s OK DELETE completed\r\n", parms[0]);
894 delete_room(&CC->quickroom);
897 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
901 * If another folder is selected, go back to that room so we can resume
902 * our happy day without violent explosions.
904 if (IMAP->selected) {
905 usergoto(savedroom, 0, 0, &msgs, &new);
911 * Back end function for imap_rename()
913 void imap_rename_backend(struct quickroom *qrbuf, void *data) {
914 char foldername[SIZ];
915 char newfoldername[SIZ];
916 char newroomname[ROOMNAMELEN];
918 struct irl *irlp = NULL; /* scratch pointer */
919 struct irlparms *irlparms;
921 irlparms = (struct irlparms *)data;
922 imap_mailboxname(foldername, sizeof foldername, qrbuf);
924 /* Rename subfolders */
925 if ( (!strncasecmp(foldername, irlparms->oldname,
926 strlen(irlparms->oldname))
927 && (foldername[strlen(irlparms->oldname)] == '|')) ) {
929 sprintf(newfoldername, "%s|%s",
931 &foldername[strlen(irlparms->oldname)+1]
934 newfloor = imap_roomname(newroomname,
935 sizeof newroomname, newfoldername) & 0xFF;
937 irlp = (struct irl *) mallok(sizeof(struct irl));
938 strcpy(irlp->irl_newroom, newroomname);
939 strcpy(irlp->irl_oldroom, qrbuf->QRname);
940 irlp->irl_newfloor = newfloor;
941 irlp->next = *(irlparms->irl);
942 *(irlparms->irl) = irlp;
948 * Implements the RENAME command
951 void imap_rename(int num_parms, char *parms[]) {
952 char old_room[ROOMNAMELEN];
953 char new_room[ROOMNAMELEN];
957 struct irl *irl = NULL; /* the list */
958 struct irl *irlp = NULL; /* scratch pointer */
959 struct irlparms irlparms;
961 oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
962 newr = imap_roomname(new_room, sizeof new_room, parms[3]);
963 new_floor = (newr & 0xFF);
965 r = CtdlRenameRoom(old_room, new_room, new_floor);
967 if (r == crr_room_not_found) {
968 cprintf("%s NO Could not locate this folder\r\n", parms[0]);
971 if (r == crr_already_exists) {
972 cprintf("%s '%s' already exists.\r\n", parms[0], parms[2]);
975 if (r == crr_noneditable) {
976 cprintf("%s This folder is not editable.\r\n", parms[0]);
979 if (r == crr_invalid_floor) {
980 cprintf("%s Folder root does not exist.\r\n", parms[0]);
983 if (r == crr_access_denied) {
984 cprintf("%s You do not have permission to edit "
985 "this folder.\r\n", parms[0]);
989 cprintf("%s NO Rename failed - undefined error %d\r\n",
995 /* If this is the INBOX, then RFC2060 says we have to just move the
996 * contents. In a Citadel environment it's easier to rename the room
997 * (already did that) and create a new inbox.
999 if (!strcasecmp(parms[2], "INBOX")) {
1000 create_room(MAILROOM, 4, "", 0, 1, 0);
1003 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1005 irlparms.oldname = parms[2];
1006 irlparms.newname = parms[3];
1007 irlparms.irl = &irl;
1008 ForEachRoom(imap_rename_backend, (void *)&irlparms);
1010 /* ... and now rename them. */
1011 while (irl != NULL) {
1012 r = CtdlRenameRoom(irl->irl_oldroom,
1013 irl->irl_newroom, irl->irl_newfloor);
1015 /* FIXME handle error returns better */
1016 lprintf(5, "CtdlRenameRoom() error %d\n", r);
1024 cprintf("%s OK RENAME completed\r\n", parms[0]);
1031 * Main command loop for IMAP sessions.
1033 void imap_command_loop(void) {
1039 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1040 if (client_gets(cmdbuf) < 1) {
1041 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
1046 lprintf(5, "IMAP: %s\r\n", cmdbuf);
1047 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
1050 /* strip off l/t whitespace and CRLF */
1051 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
1052 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
1055 /* If we're in the middle of a multi-line command, handle that */
1056 if (IMAP->authstate == imap_as_expecting_username) {
1057 imap_auth_login_user(cmdbuf);
1060 if (IMAP->authstate == imap_as_expecting_password) {
1061 imap_auth_login_pass(cmdbuf);
1066 /* Ok, at this point we're in normal command mode. The first thing
1067 * we do is print any incoming pages (yeah! we really do!)
1069 imap_print_express_messages();
1072 * Before processing the command that was just entered... if we happen
1073 * to have a folder selected, we'd like to rescan that folder for new
1074 * messages, and for deletions/changes of existing messages. This
1075 * could probably be optimized somehow, but IMAP sucks...
1077 if (IMAP->selected) {
1078 imap_rescan_msgids();
1081 /* Now for the command set. */
1083 /* Grab the tag, command, and parameters. Check syntax. */
1084 num_parms = imap_parameterize(parms, cmdbuf);
1085 if (num_parms < 2) {
1086 cprintf("BAD syntax error\r\n");
1089 /* The commands below may be executed in any state */
1091 else if ( (!strcasecmp(parms[1], "NOOP"))
1092 || (!strcasecmp(parms[1], "CHECK")) ) {
1093 cprintf("%s OK This command successfully did nothing.\r\n",
1097 else if (!strcasecmp(parms[1], "LOGOUT")) {
1098 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1099 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
1104 else if (!strcasecmp(parms[1], "LOGIN")) {
1105 imap_login(num_parms, parms);
1108 else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1109 imap_authenticate(num_parms, parms);
1112 else if (!strcasecmp(parms[1], "CAPABILITY")) {
1113 imap_capability(num_parms, parms);
1116 else if (!CC->logged_in) {
1117 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1120 /* The commans below require a logged-in state */
1122 else if (!strcasecmp(parms[1], "SELECT")) {
1123 imap_select(num_parms, parms);
1126 else if (!strcasecmp(parms[1], "EXAMINE")) {
1127 imap_select(num_parms, parms);
1130 else if (!strcasecmp(parms[1], "LSUB")) {
1131 imap_lsub(num_parms, parms);
1134 else if (!strcasecmp(parms[1], "LIST")) {
1135 imap_list(num_parms, parms);
1138 else if (!strcasecmp(parms[1], "CREATE")) {
1139 imap_create(num_parms, parms);
1142 else if (!strcasecmp(parms[1], "DELETE")) {
1143 imap_delete(num_parms, parms);
1146 else if (!strcasecmp(parms[1], "RENAME")) {
1147 imap_rename(num_parms, parms);
1150 else if (!strcasecmp(parms[1], "STATUS")) {
1151 imap_status(num_parms, parms);
1154 else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1155 imap_subscribe(num_parms, parms);
1158 else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1159 imap_unsubscribe(num_parms, parms);
1162 else if (!strcasecmp(parms[1], "APPEND")) {
1163 imap_append(num_parms, parms);
1166 else if (IMAP->selected == 0) {
1167 cprintf("%s BAD no folder selected\r\n", parms[0]);
1170 /* The commands below require the SELECT state on a mailbox */
1172 else if (!strcasecmp(parms[1], "FETCH")) {
1173 imap_fetch(num_parms, parms);
1176 else if ( (!strcasecmp(parms[1], "UID"))
1177 && (!strcasecmp(parms[2], "FETCH")) ) {
1178 imap_uidfetch(num_parms, parms);
1181 else if (!strcasecmp(parms[1], "SEARCH")) {
1182 imap_search(num_parms, parms);
1185 else if ( (!strcasecmp(parms[1], "UID"))
1186 && (!strcasecmp(parms[2], "SEARCH")) ) {
1187 imap_uidsearch(num_parms, parms);
1190 else if (!strcasecmp(parms[1], "STORE")) {
1191 imap_store(num_parms, parms);
1194 else if ( (!strcasecmp(parms[1], "UID"))
1195 && (!strcasecmp(parms[2], "STORE")) ) {
1196 imap_uidstore(num_parms, parms);
1199 else if (!strcasecmp(parms[1], "COPY")) {
1200 imap_copy(num_parms, parms);
1203 else if ( (!strcasecmp(parms[1], "UID"))
1204 && (!strcasecmp(parms[2], "COPY")) ) {
1205 imap_uidcopy(num_parms, parms);
1208 else if (!strcasecmp(parms[1], "EXPUNGE")) {
1209 imap_expunge(num_parms, parms);
1212 else if (!strcasecmp(parms[1], "CLOSE")) {
1213 imap_close(num_parms, parms);
1216 /* End of commands. If we get here, the command is either invalid
1221 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1224 /* If the client transmitted a message we can free it now */
1225 imap_free_transmitted_message();
1231 * This function is called to register the IMAP extension with Citadel.
1233 char *serv_imap_init(void)
1235 SYM_IMAP = CtdlGetDynamicSymbol();
1236 CtdlRegisterServiceHook(config.c_imap_port,
1240 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);