4 * IMAP server for the Citadel system
5 * Copyright (C) 2000-2009 by Art Cancro and others.
6 * This code is released under the terms of the GNU General Public License.
8 * WARNING: the IMAP protocol is badly designed. No implementation of it
9 * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP
10 * implementations have bugs.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
52 #include <libcitadel.h>
55 #include "citserver.h"
61 #include "internet_addressing.h"
62 #include "imap_tools.h"
63 #include "serv_imap.h"
64 #include "imap_list.h"
65 #include "imap_fetch.h"
66 #include "imap_search.h"
67 #include "imap_store.h"
69 #include "imap_metadata.h"
70 #include "imap_misc.h"
72 #include "ctdl_module.h"
75 /* imap_rename() uses this struct containing list of rooms to rename */
78 char irl_oldroom[ROOMNAMELEN];
79 char irl_newroom[ROOMNAMELEN];
83 /* Data which is passed between imap_rename() and imap_rename_backend() */
92 * If there is a message ID map in memory, free it
94 void imap_free_msgids(void)
96 if (IMAP->msgids != NULL) {
102 if (IMAP->flags != NULL) {
106 IMAP->last_mtime = (-1);
111 * If there is a transmitted message in memory, free it
113 void imap_free_transmitted_message(void)
115 FreeStrBuf(&IMAP->TransmittedMessage);
120 * Set the \Seen, \Recent. and \Answered flags, based on the sequence
121 * sets stored in the visit record for this user/room. Note that we have
122 * to parse each sequence set manually here, because calling the utility
123 * function is_msg_in_sequence_set() over and over again is too expensive.
125 * first_msg should be set to 0 to rescan the flags for every message in the
126 * room, or some other value if we're only interested in an incremental
129 void imap_set_seen_flags(int first_msg)
135 char setstr[64], lostr[64], histr[64];
138 if (IMAP->num_msgs < 1) return;
139 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
141 for (i = first_msg; i < IMAP->num_msgs; ++i) {
142 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SEEN;
143 IMAP->flags[i] |= IMAP_RECENT;
144 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_ANSWERED;
148 * Do the "\Seen" flag.
149 * (Any message not "\Seen" is considered "\Recent".)
151 num_sets = num_tokens(vbuf.v_seen, ',');
152 for (s=0; s<num_sets; ++s) {
153 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
155 extract_token(lostr, setstr, 0, ':', sizeof lostr);
156 if (num_tokens(setstr, ':') >= 2) {
157 extract_token(histr, setstr, 1, ':', sizeof histr);
158 if (!strcmp(histr, "*")) {
159 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
163 strcpy(histr, lostr);
168 for (i = first_msg; i < IMAP->num_msgs; ++i) {
169 if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
170 IMAP->flags[i] |= IMAP_SEEN;
171 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
176 /* Do the ANSWERED flag */
177 num_sets = num_tokens(vbuf.v_answered, ',');
178 for (s=0; s<num_sets; ++s) {
179 extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
181 extract_token(lostr, setstr, 0, ':', sizeof lostr);
182 if (num_tokens(setstr, ':') >= 2) {
183 extract_token(histr, setstr, 1, ':', sizeof histr);
184 if (!strcmp(histr, "*")) {
185 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
189 strcpy(histr, lostr);
194 for (i = first_msg; i < IMAP->num_msgs; ++i) {
195 if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
196 IMAP->flags[i] |= IMAP_ANSWERED;
206 * Back end for imap_load_msgids()
208 * Optimization: instead of calling realloc() to add each message, we
209 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
210 * allows the mapping to proceed much faster.
212 void imap_add_single_msgid(long msgnum, void *userdata)
216 if (IMAP->num_msgs > IMAP->num_alloc) {
217 IMAP->num_alloc += REALLOC_INCREMENT;
218 IMAP->msgids = realloc(IMAP->msgids, (IMAP->num_alloc * sizeof(long)) );
219 IMAP->flags = realloc(IMAP->flags, (IMAP->num_alloc * sizeof(long)) );
221 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
222 IMAP->flags[IMAP->num_msgs - 1] = 0;
228 * Set up a message ID map for the current room (folder)
230 void imap_load_msgids(void)
232 struct cdbdata *cdbfr;
234 if (IMAP->selected == 0) {
235 CtdlLogPrintf(CTDL_ERR,
236 "imap_load_msgids() can't run; no room selected\n");
240 imap_free_msgids(); /* If there was already a map, free it */
242 /* Load the message list */
243 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
245 IMAP->msgids = malloc(cdbfr->len);
246 memcpy(IMAP->msgids, cdbfr->ptr, cdbfr->len);
247 IMAP->num_msgs = cdbfr->len / sizeof(long);
248 IMAP->num_alloc = cdbfr->len / sizeof(long);
252 if (IMAP->num_msgs) {
253 IMAP->flags = malloc(IMAP->num_alloc * sizeof(long));
254 memset(IMAP->flags, 0, (IMAP->num_alloc * sizeof(long)) );
257 imap_set_seen_flags(0);
262 * Re-scan the selected room (folder) and see if it's been changed at all
264 void imap_rescan_msgids(void)
267 int original_num_msgs = 0;
268 long original_highest = 0L;
270 int message_still_exists;
271 struct cdbdata *cdbfr;
272 long *msglist = NULL;
276 if (IMAP->selected == 0) {
277 CtdlLogPrintf(CTDL_ERR, "imap_load_msgids() can't run; no room selected\n");
282 * Check to see if the room's contents have changed.
283 * If not, we can avoid this rescan.
285 CtdlGetRoom(&CC->room, CC->room.QRname);
286 if (IMAP->last_mtime == CC->room.QRmtime) { /* No changes! */
290 /* Load the *current* message list from disk, so we can compare it
291 * to what we have in memory.
293 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
295 msglist = malloc(cdbfr->len);
296 if (msglist == NULL) {
297 CtdlLogPrintf(CTDL_CRIT, "malloc() failed\n");
300 memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
301 num_msgs = cdbfr->len / sizeof(long);
308 * Check to see if any of the messages we know about have been expunged
310 if (IMAP->num_msgs > 0) {
312 for (i = 0; i < IMAP->num_msgs; ++i) {
314 message_still_exists = 0;
316 for (j = jstart; j < num_msgs; ++j) {
317 if (msglist[j] == IMAP->msgids[i]) {
318 message_still_exists = 1;
325 if (message_still_exists == 0) {
326 cprintf("* %d EXPUNGE\r\n", i + 1);
328 /* Here's some nice stupid nonsense. When a
329 * message is expunged, we have to slide all
330 * the existing messages up in the message
334 memcpy(&IMAP->msgids[i],
335 &IMAP->msgids[i + 1],
337 (IMAP->num_msgs - i)));
338 memcpy(&IMAP->flags[i],
341 (IMAP->num_msgs - i)));
350 * Remember how many messages were here before we re-scanned.
352 original_num_msgs = IMAP->num_msgs;
353 if (IMAP->num_msgs > 0) {
354 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
356 original_highest = 0L;
360 * Now peruse the room for *new* messages only.
361 * This logic is probably the cause of Bug # 368
362 * [ http://bugzilla.citadel.org/show_bug.cgi?id=368 ]
365 for (j = 0; j < num_msgs; ++j) {
366 if (msglist[j] > original_highest) {
367 imap_add_single_msgid(msglist[j], NULL);
371 imap_set_seen_flags(original_num_msgs);
374 * If new messages have arrived, tell the client about them.
376 if (IMAP->num_msgs > original_num_msgs) {
378 for (j = 0; j < num_msgs; ++j) {
379 if (IMAP->flags[j] & IMAP_RECENT) {
384 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
385 cprintf("* %d RECENT\r\n", num_recent);
391 IMAP->last_mtime = CC->room.QRmtime;
396 * This cleanup function blows away the temporary memory and files used by
399 void imap_cleanup_function(void)
402 /* Don't do this stuff if this is not a IMAP session! */
403 if (CC->h_command_function != imap_command_loop)
406 /* If there is a mailbox selected, auto-expunge it. */
407 if (IMAP->selected) {
411 CtdlLogPrintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
413 imap_free_transmitted_message();
415 if (IMAP->cached_rfc822 != NULL) {
416 FreeStrBuf(&IMAP->cached_rfc822);
417 IMAP->cached_rfc822_msgnum = (-1);
418 IMAP->cached_rfc822_withbody = 0;
421 if (IMAP->cached_body != NULL) {
422 free(IMAP->cached_body);
423 IMAP->cached_body = NULL;
424 IMAP->cached_body_len = 0;
425 IMAP->cached_bodymsgnum = (-1);
427 FreeStrBuf(&IMAP->Cmd.CmdBuf);
428 if (IMAP->Cmd.Params != NULL) free(IMAP->Cmd.Params);
430 CtdlLogPrintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
435 * Does the actual work of the CAPABILITY command (because we need to
436 * output this stuff in other places as well)
438 void imap_output_capability_string(void) {
439 cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN UIDPLUS");
442 if (!CC->redirect_ssl) cprintf(" STARTTLS");
445 #ifndef DISABLE_IMAP_ACL
449 /* We are building a partial implementation of METADATA for the sole purpose
450 * of interoperating with the ical/vcard version of the Bynari Insight Connector.
451 * It is not a full RFC5464 implementation, but it should refuse non-Bynari
452 * metadata in a compatible and graceful way.
454 cprintf(" METADATA");
457 * LIST-EXTENDED was originally going to be required by the METADATA extension.
458 * It was mercifully removed prior to the finalization of RFC5464. We started
459 * implementing this but stopped when we learned that it would not be needed.
460 * If you uncomment this declaration you are responsible for writing a lot of new
463 * cprintf(" LIST-EXTENDED")
469 * implements the CAPABILITY command
471 void imap_capability(int num_parms, ConstStr *Params)
474 imap_output_capability_string();
476 cprintf("%s OK CAPABILITY completed\r\n", Params[0].Key);
481 * Implements the ID command (specified by RFC2971)
483 * We ignore the client-supplied information, and output a NIL response.
484 * Although this is technically a valid implementation of the extension, it
485 * is quite useless. It exists only so that we may see which clients are
486 * making use of this extension.
489 void imap_id(int num_parms, ConstStr *Params)
491 cprintf("* ID NIL\r\n");
492 cprintf("%s OK ID completed\r\n", Params[0].Key);
497 * Here's where our IMAP session begins its happy day.
499 void imap_greeting(void)
502 strcpy(CC->cs_clientname, "IMAP session");
503 CC->session_specific_data = malloc(sizeof(citimap));
504 memset(IMAP, 0, sizeof(citimap));
505 IMAP->authstate = imap_as_normal;
506 IMAP->cached_rfc822_msgnum = (-1);
507 IMAP->cached_rfc822_withbody = 0;
511 cprintf("* BYE; Server busy, try later\r\n");
516 imap_output_capability_string();
517 cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
522 * IMAPS is just like IMAP, except it goes crypto right away.
524 void imaps_greeting(void) {
525 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
527 if (!CC->redirect_ssl) CC->kill_me = 1; /* kill session if no crypto */
534 * implements the LOGIN command (ordinary username/password login)
536 void imap_login(int num_parms, ConstStr *Params)
541 if (Params[2].Key[0] == '{') {
542 cprintf("+ go ahead\r\n");
543 IMAP->authstate = imap_as_expecting_multilineusername;
544 strcpy(IMAP->authseq, Params[0].Key);
548 cprintf("%s BAD incorrect number of parameters\r\n", Params[0].Key);
552 if (CtdlLoginExistingUser(NULL, Params[2].Key) == login_ok) {
553 if (CtdlTryPassword(Params[3].Key, Params[3].len) == pass_ok) {
554 cprintf("%s OK [", Params[0].Key);
555 imap_output_capability_string();
556 cprintf("] Hello, %s\r\n", CC->user.fullname);
561 cprintf("%s BAD Login incorrect\r\n", Params[0].Key);
563 cprintf("%s BAD incorrect number of parameters\r\n", Params[0].Key);
571 * Implements the AUTHENTICATE command
573 void imap_authenticate(int num_parms, ConstStr *Params)
577 if (num_parms != 3) {
578 cprintf("%s BAD incorrect number of parameters\r\n",
584 cprintf("%s BAD Already logged in.\r\n", Params[0].Key);
588 if (!strcasecmp(Params[2].Key, "LOGIN")) {
589 CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
590 cprintf("+ %s\r\n", UsrBuf);
591 IMAP->authstate = imap_as_expecting_username;
592 strcpy(IMAP->authseq, Params[0].Key);
596 if (!strcasecmp(Params[2].Key, "PLAIN")) {
597 // CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
598 // cprintf("+ %s\r\n", UsrBuf);
600 IMAP->authstate = imap_as_expecting_plainauth;
601 strcpy(IMAP->authseq, Params[0].Key);
606 cprintf("%s NO AUTHENTICATE %s failed\r\n",
607 Params[0].Key, Params[1].Key);
612 void imap_auth_plain(void)
614 const char *decoded_authstring;
621 memset(pass, 0, sizeof(pass));
622 StrBufDecodeBase64(IMAP->Cmd.CmdBuf);
624 decoded_authstring = ChrPtr(IMAP->Cmd.CmdBuf);
625 safestrncpy(ident, decoded_authstring, sizeof ident);
626 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
627 len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
629 len = sizeof(pass) - 1;
631 IMAP->authstate = imap_as_normal;
633 if (!IsEmptyStr(ident)) {
634 result = CtdlLoginExistingUser(user, ident);
637 result = CtdlLoginExistingUser(NULL, user);
640 if (result == login_ok) {
641 if (CtdlTryPassword(pass, len) == pass_ok) {
642 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
646 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
650 void imap_auth_login_user(long state)
653 citimap *Imap = IMAP;
656 case imap_as_expecting_username:
657 StrBufDecodeBase64(Imap->Cmd.CmdBuf);
658 CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
659 CtdlEncodeBase64(PWBuf, "Password:", 9, 0);
660 cprintf("+ %s\r\n", PWBuf);
662 Imap->authstate = imap_as_expecting_password;
664 case imap_as_expecting_multilineusername:
665 extract_token(PWBuf, ChrPtr(Imap->Cmd.CmdBuf), 1, ' ', sizeof(PWBuf));
666 CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
667 cprintf("+ go ahead\r\n");
668 IMAP->authstate = imap_as_expecting_multilinepassword;
674 void imap_auth_login_pass(long state)
676 citimap *Imap = IMAP;
677 const char *pass = NULL;
682 case imap_as_expecting_password:
683 StrBufDecodeBase64(Imap->Cmd.CmdBuf);
684 pass = ChrPtr(Imap->Cmd.CmdBuf);
685 len = StrLength(Imap->Cmd.CmdBuf);
687 case imap_as_expecting_multilinepassword:
688 pass = ChrPtr(Imap->Cmd.CmdBuf);
689 len = StrLength(Imap->Cmd.CmdBuf);
692 if (len > USERNAME_SIZE)
693 StrBufCutAt(Imap->Cmd.CmdBuf, USERNAME_SIZE, NULL);
695 if (CtdlTryPassword(pass, len) == pass_ok) {
696 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
698 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
700 IMAP->authstate = imap_as_normal;
706 * implements the STARTTLS command (Citadel API version)
708 void imap_starttls(int num_parms, ConstStr *Params)
710 char ok_response[SIZ];
711 char nosup_response[SIZ];
712 char error_response[SIZ];
714 snprintf(ok_response, SIZ, "%s OK begin TLS negotiation now\r\n", Params[0].Key);
715 snprintf(nosup_response, SIZ, "%s NO TLS not supported here\r\n", Params[0].Key);
716 snprintf(error_response, SIZ, "%s BAD Internal error\r\n", Params[0].Key);
717 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
722 * implements the SELECT command
724 void imap_select(int num_parms, ConstStr *Params)
726 char towhere[ROOMNAMELEN];
727 char augmented_roomname[ROOMNAMELEN];
731 struct ctdlroom QRscratch;
737 /* Convert the supplied folder name to a roomname */
738 i = imap_roomname(towhere, sizeof towhere, Params[2].Key);
740 cprintf("%s NO Invalid mailbox name.\r\n", Params[0].Key);
744 floornum = (i & 0x00ff);
745 roomflags = (i & 0xff00);
747 /* First try a regular match */
748 c = CtdlGetRoom(&QRscratch, towhere);
750 /* Then try a mailbox name match */
752 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, towhere);
753 c = CtdlGetRoom(&QRscratch, augmented_roomname);
755 safestrncpy(towhere, augmented_roomname, sizeof(towhere));
759 /* If the room exists, check security/access */
761 /* See if there is an existing user/room relationship */
762 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
764 /* normal clients have to pass through security */
770 /* Fail here if no such room */
772 cprintf("%s NO ... no such room, or access denied\r\n", Params[0].Key);
776 /* If we already had some other folder selected, auto-expunge it */
780 * CtdlUserGoto() formally takes us to the desired room, happily returning
781 * the number of messages and number of new messages.
783 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
784 CtdlUserGoto(NULL, 0, 0, &msgs, &new);
787 if (!strcasecmp(Params[1].Key, "EXAMINE")) {
794 IMAP->last_mtime = CC->room.QRmtime;
796 cprintf("* %d EXISTS\r\n", msgs);
797 cprintf("* %d RECENT\r\n", new);
799 cprintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE);
800 cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
802 /* Technically, \Deleted is a valid flag, but not a permanent flag,
803 * because we don't maintain its state across sessions. Citadel
804 * automatically expunges mailboxes when they are de-selected.
806 * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes
807 * some clients (particularly Thunderbird) to misbehave -- they simply
808 * elect not to transmit the flag at all. So we have to advertise
809 * \Deleted as a PERMANENTFLAGS flag, even though it technically isn't.
811 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
812 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n");
814 cprintf("%s OK [%s] %s completed\r\n",
816 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), Params[1].Key
822 * Does the real work for expunge.
824 int imap_do_expunge(void)
827 int num_expunged = 0;
828 long *delmsgs = NULL;
831 CtdlLogPrintf(CTDL_DEBUG, "imap_do_expunge() called\n");
832 if (IMAP->selected == 0) {
836 if (IMAP->num_msgs > 0) {
837 delmsgs = malloc(IMAP->num_msgs * sizeof(long));
838 for (i = 0; i < IMAP->num_msgs; ++i) {
839 if (IMAP->flags[i] & IMAP_DELETED) {
840 delmsgs[num_delmsgs++] = IMAP->msgids[i];
843 if (num_delmsgs > 0) {
844 CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
846 num_expunged += num_delmsgs;
850 if (num_expunged > 0) {
851 imap_rescan_msgids();
854 CtdlLogPrintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n", num_expunged, CC->room.QRname);
855 return (num_expunged);
860 * implements the EXPUNGE command syntax
862 void imap_expunge(int num_parms, ConstStr *Params)
864 int num_expunged = 0;
866 num_expunged = imap_do_expunge();
867 cprintf("%s OK expunged %d messages.\r\n", Params[0].Key, num_expunged);
872 * implements the CLOSE command
874 void imap_close(int num_parms, ConstStr *Params)
877 /* Yes, we always expunge on close. */
878 if (IMAP->selected) {
885 cprintf("%s OK CLOSE completed\r\n", Params[0].Key);
890 * Implements the NAMESPACE command.
892 void imap_namespace(int num_parms, ConstStr *Params)
899 cprintf("* NAMESPACE ");
901 /* All personal folders are subordinate to INBOX. */
902 cprintf("((\"INBOX/\" \"/\")) ");
904 /* Other users' folders ... coming soon! FIXME */
907 /* Show all floors as shared namespaces. Neato! */
909 for (i = 0; i < MAXFLOORS; ++i) {
910 fl = CtdlGetCachedFloor(i);
911 if (fl->f_flags & F_INUSE) {
912 if (floors > 0) cprintf(" ");
914 snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
915 plain_imap_strout(Namespace);
922 /* Wind it up with a newline and a completion message. */
924 cprintf("%s OK NAMESPACE completed\r\n", Params[0].Key);
929 * Implements the CREATE command
932 void imap_create(int num_parms, ConstStr *Params)
935 char roomname[ROOMNAMELEN];
940 char *notification_message = NULL;
943 cprintf("%s NO A foder name must be specified\r\n", Params[0].Key);
947 if (strchr(Params[2].Key, '\\') != NULL) {
948 cprintf("%s NO Invalid character in folder name\r\n", Params[0].Key);
949 CtdlLogPrintf(CTDL_DEBUG, "invalid character in folder name\n");
953 ret = imap_roomname(roomname, sizeof roomname, Params[2].Key);
955 cprintf("%s NO Invalid mailbox name or location\r\n",
957 CtdlLogPrintf(CTDL_DEBUG, "invalid mailbox name or location\n");
960 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
961 flags = (ret & 0xff00); /* upper 8 bits = flags */
963 if (flags & IR_MAILBOX) {
964 if (strncasecmp(Params[2].Key, "INBOX/", 6)) {
965 cprintf("%s NO Personal folders must be created under INBOX\r\n", Params[0].Key);
966 CtdlLogPrintf(CTDL_DEBUG, "not subordinate to inbox\n");
971 if (flags & IR_MAILBOX) {
972 newroomtype = 4; /* private mailbox */
973 newroomview = VIEW_MAILBOX;
975 newroomtype = 0; /* public folder */
976 newroomview = VIEW_BBS;
979 CtdlLogPrintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
980 roomname, floornum, newroomtype);
982 ret = CtdlCreateRoom(roomname, newroomtype, "", floornum, 1, 0, newroomview);
984 /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY! BYNARI CONNECTOR DEPENDS ON IT! ***/
985 cprintf("%s NO Mailbox already exists, or create failed\r\n", Params[0].Key);
987 cprintf("%s OK CREATE completed\r\n", Params[0].Key);
988 /* post a message in Aide> describing the new room */
989 notification_message = malloc(1024);
990 snprintf(notification_message, 1024,
991 "A new room called \"%s\" has been created by %s%s%s%s\n",
994 ((ret & QR_MAILBOX) ? " [personal]" : ""),
995 ((ret & QR_PRIVATE) ? " [private]" : ""),
996 ((ret & QR_GUESSNAME) ? " [hidden]" : "")
998 CtdlAideMessage(notification_message, "Room Creation Message");
999 free(notification_message);
1001 CtdlLogPrintf(CTDL_DEBUG, "imap_create() completed\n");
1006 * Locate a room by its IMAP folder name, and check access to it.
1007 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
1009 int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok)
1012 char augmented_roomname[ROOMNAMELEN];
1013 char roomname[ROOMNAMELEN];
1015 struct ctdlroom QRscratch;
1019 ret = imap_roomname(roomname, sizeof roomname, foldername);
1024 /* First try a regular match */
1025 c = CtdlGetRoom(&QRscratch, roomname);
1027 /* Then try a mailbox name match */
1029 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1030 &CC->user, roomname);
1031 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1033 safestrncpy(roomname, augmented_roomname, sizeof(roomname));
1036 /* If the room exists, check security/access */
1038 /* See if there is an existing user/room relationship */
1039 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1041 /* normal clients have to pass through security */
1042 if (ra & UA_KNOWN) {
1045 if ((zapped_ok) && (ra & UA_ZAPPED)) {
1050 /* Fail here if no such room */
1052 strcpy(returned_roomname, "");
1055 safestrncpy(returned_roomname, QRscratch.QRname, ROOMNAMELEN);
1062 * Implements the STATUS command (sort of)
1065 void imap_status(int num_parms, ConstStr *Params)
1068 char roomname[ROOMNAMELEN];
1069 char imaproomname[SIZ];
1070 char savedroom[ROOMNAMELEN];
1073 ret = imap_grabroom(roomname, Params[2].Key, 1);
1076 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1082 * CtdlUserGoto() formally takes us to the desired room, happily returning
1083 * the number of messages and number of new messages. (If another
1084 * folder is selected, save its name so we can return there!!!!!)
1086 if (IMAP->selected) {
1087 strcpy(savedroom, CC->room.QRname);
1089 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1092 * Tell the client what it wants to know. In fact, tell it *more* than
1093 * it wants to know. We happily IGnore the supplied status data item
1094 * names and simply spew all possible data items. It's far easier to
1095 * code and probably saves us some processing time too.
1097 imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
1098 cprintf("* STATUS ");
1099 plain_imap_strout(imaproomname);
1100 cprintf(" (MESSAGES %d ", msgs);
1101 cprintf("RECENT %d ", new); /* Initially, new==recent */
1102 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
1103 cprintf("UNSEEN %d)\r\n", new);
1106 * If another folder is selected, go back to that room so we can resume
1107 * our happy day without violent explosions.
1109 if (IMAP->selected) {
1110 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1114 * Oooh, look, we're done!
1116 cprintf("%s OK STATUS completed\r\n", Params[0].Key);
1121 * Implements the SUBSCRIBE command
1124 void imap_subscribe(int num_parms, ConstStr *Params)
1127 char roomname[ROOMNAMELEN];
1128 char savedroom[ROOMNAMELEN];
1131 ret = imap_grabroom(roomname, Params[2].Key, 1);
1134 "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1142 * CtdlUserGoto() formally takes us to the desired room, which has the side
1143 * effect of marking the room as not-zapped ... exactly the effect
1144 * we're looking for.
1146 if (IMAP->selected) {
1147 strcpy(savedroom, CC->room.QRname);
1149 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1152 * If another folder is selected, go back to that room so we can resume
1153 * our happy day without violent explosions.
1155 if (IMAP->selected) {
1156 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1159 cprintf("%s OK SUBSCRIBE completed\r\n", Params[0].Key);
1164 * Implements the UNSUBSCRIBE command
1167 void imap_unsubscribe(int num_parms, ConstStr *Params)
1170 char roomname[ROOMNAMELEN];
1171 char savedroom[ROOMNAMELEN];
1174 ret = imap_grabroom(roomname, Params[2].Key, 1);
1177 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1183 * CtdlUserGoto() formally takes us to the desired room.
1185 if (IMAP->selected) {
1186 strcpy(savedroom, CC->room.QRname);
1188 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1191 * Now make the API call to zap the room
1193 if (CtdlForgetThisRoom() == 0) {
1194 cprintf("%s OK UNSUBSCRIBE completed\r\n", Params[0].Key);
1197 ("%s NO You may not unsubscribe from this folder.\r\n",
1202 * If another folder is selected, go back to that room so we can resume
1203 * our happy day without violent explosions.
1205 if (IMAP->selected) {
1206 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1212 * Implements the DELETE command
1215 void imap_delete(int num_parms, ConstStr *Params)
1218 char roomname[ROOMNAMELEN];
1219 char savedroom[ROOMNAMELEN];
1222 ret = imap_grabroom(roomname, Params[2].Key, 1);
1224 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1230 * CtdlUserGoto() formally takes us to the desired room, happily returning
1231 * the number of messages and number of new messages. (If another
1232 * folder is selected, save its name so we can return there!!!!!)
1234 if (IMAP->selected) {
1235 strcpy(savedroom, CC->room.QRname);
1237 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1240 * Now delete the room.
1242 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1243 CtdlScheduleRoomForDeletion(&CC->room);
1244 cprintf("%s OK DELETE completed\r\n", Params[0].Key);
1246 cprintf("%s NO Can't delete this folder.\r\n", Params[0].Key);
1250 * If another folder is selected, go back to that room so we can resume
1251 * our happy day without violent explosions.
1253 if (IMAP->selected) {
1254 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1260 * Back end function for imap_rename()
1262 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1264 char foldername[SIZ];
1265 char newfoldername[SIZ];
1266 char newroomname[ROOMNAMELEN];
1268 struct irl *irlp = NULL; /* scratch pointer */
1269 struct irlparms *irlparms;
1271 irlparms = (struct irlparms *) data;
1272 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1274 /* Rename subfolders */
1275 if ((!strncasecmp(foldername, irlparms->oldname,
1276 strlen(irlparms->oldname))
1277 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1279 sprintf(newfoldername, "%s/%s",
1281 &foldername[strlen(irlparms->oldname) + 1]
1284 newfloor = imap_roomname(newroomname,
1286 newfoldername) & 0xFF;
1288 irlp = (struct irl *) malloc(sizeof(struct irl));
1289 strcpy(irlp->irl_newroom, newroomname);
1290 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1291 irlp->irl_newfloor = newfloor;
1292 irlp->next = *(irlparms->irl);
1293 *(irlparms->irl) = irlp;
1299 * Implements the RENAME command
1302 void imap_rename(int num_parms, ConstStr *Params)
1304 char old_room[ROOMNAMELEN];
1305 char new_room[ROOMNAMELEN];
1309 struct irl *irl = NULL; /* the list */
1310 struct irl *irlp = NULL; /* scratch pointer */
1311 struct irlparms irlparms;
1314 if (strchr(Params[3].Key, '\\') != NULL) {
1315 cprintf("%s NO Invalid character in folder name\r\n",
1320 oldr = imap_roomname(old_room, sizeof old_room, Params[2].Key);
1321 newr = imap_roomname(new_room, sizeof new_room, Params[3].Key);
1322 new_floor = (newr & 0xFF);
1324 r = CtdlRenameRoom(old_room, new_room, new_floor);
1326 if (r == crr_room_not_found) {
1327 cprintf("%s NO Could not locate this folder\r\n",
1331 if (r == crr_already_exists) {
1332 cprintf("%s NO '%s' already exists.\r\n", Params[0].Key, Params[2].Key);
1335 if (r == crr_noneditable) {
1336 cprintf("%s NO This folder is not editable.\r\n", Params[0].Key);
1339 if (r == crr_invalid_floor) {
1340 cprintf("%s NO Folder root does not exist.\r\n", Params[0].Key);
1343 if (r == crr_access_denied) {
1344 cprintf("%s NO You do not have permission to edit this folder.\r\n",
1349 cprintf("%s NO Rename failed - undefined error %d\r\n",
1354 /* If this is the INBOX, then RFC2060 says we have to just move the
1355 * contents. In a Citadel environment it's easier to rename the room
1356 * (already did that) and create a new inbox.
1358 if (!strcasecmp(Params[2].Key, "INBOX")) {
1359 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
1362 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1364 irlparms.oldname = Params[2].Key;
1365 irlparms.newname = Params[3].Key;
1366 irlparms.irl = &irl;
1367 CtdlForEachRoom(imap_rename_backend, (void *) &irlparms);
1369 /* ... and now rename them. */
1370 while (irl != NULL) {
1371 r = CtdlRenameRoom(irl->irl_oldroom,
1375 /* FIXME handle error returns better */
1376 CtdlLogPrintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r);
1384 snprintf(aidemsg, sizeof aidemsg, "IMAP folder \"%s\" renamed to \"%s\" by %s\n",
1389 CtdlAideMessage(aidemsg, "IMAP folder rename");
1391 cprintf("%s OK RENAME completed\r\n", Params[0].Key);
1396 * Main command loop for IMAP sessions.
1398 void imap_command_loop(void)
1400 struct timeval tv1, tv2;
1401 suseconds_t total_time = 0;
1402 int untagged_ok = 1;
1404 const char *pchs, *pche;
1406 gettimeofday(&tv1, NULL);
1407 CC->lastcmd = time(NULL);
1411 if (Imap->Cmd.CmdBuf == NULL)
1412 Imap->Cmd.CmdBuf = NewStrBufPlain(NULL, SIZ);
1414 FlushStrBuf(Imap->Cmd.CmdBuf);
1416 if (CtdlClientGetLine(Imap->Cmd.CmdBuf) < 1) {
1417 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
1422 if (Imap->authstate == imap_as_expecting_password) {
1423 CtdlLogPrintf(CTDL_INFO, "IMAP: <password>\n");
1425 else if (Imap->authstate == imap_as_expecting_plainauth) {
1426 CtdlLogPrintf(CTDL_INFO, "IMAP: <plain_auth>\n");
1428 else if ((Imap->authstate == imap_as_expecting_multilineusername) ||
1429 cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
1430 CtdlLogPrintf(CTDL_INFO, "IMAP: LOGIN...\n");
1433 CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", ChrPtr(Imap->Cmd.CmdBuf));
1436 pchs = ChrPtr(Imap->Cmd.CmdBuf);
1437 pche = pchs + StrLength(Imap->Cmd.CmdBuf);
1439 while ((pche > pchs) &&
1444 StrBufCutRight(Imap->Cmd.CmdBuf, 1);
1446 StrBufTrim(Imap->Cmd.CmdBuf);
1448 /* If we're in the middle of a multi-line command, handle that */
1449 switch (Imap->authstate){
1450 case imap_as_expecting_username:
1451 imap_auth_login_user(imap_as_expecting_username);
1453 case imap_as_expecting_multilineusername:
1454 imap_auth_login_user(imap_as_expecting_multilineusername);
1456 case imap_as_expecting_plainauth:
1459 case imap_as_expecting_password:
1460 imap_auth_login_pass(imap_as_expecting_password);
1462 case imap_as_expecting_multilinepassword:
1463 imap_auth_login_pass(imap_as_expecting_multilinepassword);
1470 /* Ok, at this point we're in normal command mode.
1471 * If the command just submitted does not contain a literal, we
1472 * might think about delivering some untagged stuff...
1474 if (*(ChrPtr(Imap->Cmd.CmdBuf) + StrLength(Imap->Cmd.CmdBuf) - 1)
1479 /* Grab the tag, command, and parameters. */
1480 imap_parameterize(&Imap->Cmd);
1482 /* debug output the parsed vector */
1485 CtdlLogPrintf(CTDL_DEBUG, "----- %ld params \n",
1486 Imap->Cmd.num_parms);
1488 for (i=0; i < Imap->Cmd.num_parms; i++) {
1489 if (Imap->Cmd.Params[i].len != strlen(Imap->Cmd.Params[i].Key))
1490 CtdlLogPrintf(CTDL_DEBUG, "*********** %ld != %ld : %s\n",
1491 Imap->Cmd.Params[i].len,
1492 strlen(Imap->Cmd.Params[i].Key),
1493 Imap->Cmd.Params[i].Key);
1495 CtdlLogPrintf(CTDL_DEBUG, "%ld : %s\n",
1496 Imap->Cmd.Params[i].len,
1497 Imap->Cmd.Params[i].Key);
1501 /* RFC3501 says that we cannot output untagged data during these commands */
1502 if (Imap->Cmd.num_parms >= 2) {
1503 if ( (!strcasecmp(Imap->Cmd.Params[1].Key, "FETCH"))
1504 || (!strcasecmp(Imap->Cmd.Params[1].Key, "STORE"))
1505 || (!strcasecmp(Imap->Cmd.Params[1].Key, "SEARCH"))
1513 /* we can put any additional untagged stuff right here in the future */
1516 * Before processing the command that was just entered... if we happen
1517 * to have a folder selected, we'd like to rescan that folder for new
1518 * messages, and for deletions/changes of existing messages. This
1519 * could probably be optimized better with some deep thought...
1521 if (Imap->selected) {
1522 imap_rescan_msgids();
1526 /* Now for the command set. */
1528 if (Imap->Cmd.num_parms < 2) {
1529 cprintf("BAD syntax error\r\n");
1532 /* The commands below may be executed in any state */
1534 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "NOOP"))
1535 || (!strcasecmp(Imap->Cmd.Params[1].Key, "CHECK"))) {
1536 cprintf("%s OK No operation\r\n",
1537 Imap->Cmd.Params[0].Key);
1540 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "ID")) {
1541 imap_id(Imap->Cmd.num_parms, Imap->Cmd.Params);
1545 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LOGOUT")) {
1546 if (Imap->selected) {
1547 imap_do_expunge(); /* yes, we auto-expunge at logout */
1549 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1550 cprintf("%s OK Citadel IMAP session ended.\r\n",
1551 Imap->Cmd.Params[0].Key);
1556 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LOGIN")) {
1557 imap_login(Imap->Cmd.num_parms, Imap->Cmd.Params);
1560 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "AUTHENTICATE")) {
1561 imap_authenticate(Imap->Cmd.num_parms, Imap->Cmd.Params);
1564 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "CAPABILITY")) {
1565 imap_capability(Imap->Cmd.num_parms, Imap->Cmd.Params);
1568 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "STARTTLS")) {
1569 imap_starttls(Imap->Cmd.num_parms, Imap->Cmd.Params);
1572 else if (!CC->logged_in) {
1573 cprintf("%s BAD Not logged in.\r\n", Imap->Cmd.Params[0].Key);
1576 /* The commans below require a logged-in state */
1578 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SELECT")) {
1579 imap_select(Imap->Cmd.num_parms, Imap->Cmd.Params);
1582 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "EXAMINE")) {
1583 imap_select(Imap->Cmd.num_parms, Imap->Cmd.Params);
1586 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LSUB")) {
1587 imap_list(Imap->Cmd.num_parms, Imap->Cmd.Params);
1590 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LIST")) {
1591 imap_list(Imap->Cmd.num_parms, Imap->Cmd.Params);
1594 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "CREATE")) {
1595 imap_create(Imap->Cmd.num_parms, Imap->Cmd.Params);
1598 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "DELETE")) {
1599 imap_delete(Imap->Cmd.num_parms, Imap->Cmd.Params);
1602 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "RENAME")) {
1603 imap_rename(Imap->Cmd.num_parms, Imap->Cmd.Params);
1606 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "STATUS")) {
1607 imap_status(Imap->Cmd.num_parms, Imap->Cmd.Params);
1610 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SUBSCRIBE")) {
1611 imap_subscribe(Imap->Cmd.num_parms, Imap->Cmd.Params);
1614 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "UNSUBSCRIBE")) {
1615 imap_unsubscribe(Imap->Cmd.num_parms, Imap->Cmd.Params);
1618 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "APPEND")) {
1619 imap_append(Imap->Cmd.num_parms, Imap->Cmd.Params);
1622 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "NAMESPACE")) {
1623 imap_namespace(Imap->Cmd.num_parms, Imap->Cmd.Params);
1626 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SETACL")) {
1627 imap_setacl(Imap->Cmd.num_parms, Imap->Cmd.Params);
1630 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "DELETEACL")) {
1631 imap_deleteacl(Imap->Cmd.num_parms, Imap->Cmd.Params);
1634 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "GETACL")) {
1635 imap_getacl(Imap->Cmd.num_parms, Imap->Cmd.Params);
1638 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LISTRIGHTS")) {
1639 imap_listrights(Imap->Cmd.num_parms, Imap->Cmd.Params);
1642 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "MYRIGHTS")) {
1643 imap_myrights(Imap->Cmd.num_parms, Imap->Cmd.Params);
1646 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "GETMETADATA")) {
1647 imap_getmetadata(Imap->Cmd.num_parms, Imap->Cmd.Params);
1650 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SETMETADATA")) {
1651 imap_setmetadata(Imap->Cmd.num_parms, Imap->Cmd.Params);
1654 else if (Imap->selected == 0) {
1655 cprintf("%s BAD no folder selected\r\n", Imap->Cmd.Params[0].Key);
1658 /* The commands below require the SELECT state on a mailbox */
1660 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "FETCH")) {
1661 imap_fetch(Imap->Cmd.num_parms, Imap->Cmd.Params);
1664 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID"))
1665 && (!strcasecmp(Imap->Cmd.Params[2].Key, "FETCH"))) {
1666 imap_uidfetch(Imap->Cmd.num_parms, Imap->Cmd.Params);
1669 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SEARCH")) {
1670 imap_search(Imap->Cmd.num_parms, Imap->Cmd.Params);
1673 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID"))
1674 && (!strcasecmp(Imap->Cmd.Params[2].Key, "SEARCH"))) {
1675 imap_uidsearch(Imap->Cmd.num_parms, Imap->Cmd.Params);
1678 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "STORE")) {
1679 imap_store(Imap->Cmd.num_parms, Imap->Cmd.Params);
1682 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID"))
1683 && (!strcasecmp(Imap->Cmd.Params[2].Key, "STORE"))) {
1684 imap_uidstore(Imap->Cmd.num_parms, Imap->Cmd.Params);
1687 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "COPY")) {
1688 imap_copy(Imap->Cmd.num_parms, Imap->Cmd.Params);
1691 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID")) && (!strcasecmp(Imap->Cmd.Params[2].Key, "COPY"))) {
1692 imap_uidcopy(Imap->Cmd.num_parms, Imap->Cmd.Params);
1695 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "EXPUNGE")) {
1696 imap_expunge(Imap->Cmd.num_parms, Imap->Cmd.Params);
1699 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID")) && (!strcasecmp(Imap->Cmd.Params[2].Key, "EXPUNGE"))) {
1700 imap_expunge(Imap->Cmd.num_parms, Imap->Cmd.Params);
1703 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "CLOSE")) {
1704 imap_close(Imap->Cmd.num_parms, Imap->Cmd.Params);
1707 /* End of commands. If we get here, the command is either invalid
1712 cprintf("%s BAD command unrecognized\r\n", Imap->Cmd.Params[0].Key);
1715 /* If the client transmitted a message we can free it now */
1716 imap_free_transmitted_message();
1718 gettimeofday(&tv2, NULL);
1719 total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
1720 CtdlLogPrintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n",
1721 (total_time / 1000000),
1722 (total_time % 1000000)
1727 const char *CitadelServiceIMAP="IMAP";
1728 const char *CitadelServiceIMAPS="IMAPS";
1731 * This function is called to register the IMAP extension with Citadel.
1733 CTDL_MODULE_INIT(imap)
1737 CtdlRegisterServiceHook(config.c_imap_port,
1738 NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP);
1740 CtdlRegisterServiceHook(config.c_imaps_port,
1741 NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS);
1743 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
1746 /* return our Subversion id for the Log */