2 * IMAP server for the Citadel system
3 * Copyright (C) 2000-2009 by Art Cancro and others.
4 * This code is released under the terms of the GNU General Public License.
6 * WARNING: the IMAP protocol is badly designed. No implementation of it
7 * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP
8 * implementations have bugs.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "imap_tools.h"
61 #include "serv_imap.h"
62 #include "imap_list.h"
63 #include "imap_fetch.h"
64 #include "imap_search.h"
65 #include "imap_store.h"
67 #include "imap_metadata.h"
68 #include "imap_misc.h"
70 #include "ctdl_module.h"
73 /* imap_rename() uses this struct containing list of rooms to rename */
76 char irl_oldroom[ROOMNAMELEN];
77 char irl_newroom[ROOMNAMELEN];
81 /* Data which is passed between imap_rename() and imap_rename_backend() */
90 * If there is a message ID map in memory, free it
92 void imap_free_msgids(void)
94 if (IMAP->msgids != NULL) {
100 if (IMAP->flags != NULL) {
104 IMAP->last_mtime = (-1);
109 * If there is a transmitted message in memory, free it
111 void imap_free_transmitted_message(void)
113 FreeStrBuf(&IMAP->TransmittedMessage);
118 * Set the \Seen, \Recent. and \Answered flags, based on the sequence
119 * sets stored in the visit record for this user/room. Note that we have
120 * to parse each sequence set manually here, because calling the utility
121 * function is_msg_in_sequence_set() over and over again is too expensive.
123 * first_msg should be set to 0 to rescan the flags for every message in the
124 * room, or some other value if we're only interested in an incremental
127 void imap_set_seen_flags(int first_msg)
133 char setstr[64], lostr[64], histr[64];
136 if (IMAP->num_msgs < 1) return;
137 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
139 for (i = first_msg; i < IMAP->num_msgs; ++i) {
140 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SEEN;
141 IMAP->flags[i] |= IMAP_RECENT;
142 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_ANSWERED;
146 * Do the "\Seen" flag.
147 * (Any message not "\Seen" is considered "\Recent".)
149 num_sets = num_tokens(vbuf.v_seen, ',');
150 for (s=0; s<num_sets; ++s) {
151 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
153 extract_token(lostr, setstr, 0, ':', sizeof lostr);
154 if (num_tokens(setstr, ':') >= 2) {
155 extract_token(histr, setstr, 1, ':', sizeof histr);
156 if (!strcmp(histr, "*")) {
157 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
161 strcpy(histr, lostr);
166 for (i = first_msg; i < IMAP->num_msgs; ++i) {
167 if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
168 IMAP->flags[i] |= IMAP_SEEN;
169 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
174 /* Do the ANSWERED flag */
175 num_sets = num_tokens(vbuf.v_answered, ',');
176 for (s=0; s<num_sets; ++s) {
177 extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
179 extract_token(lostr, setstr, 0, ':', sizeof lostr);
180 if (num_tokens(setstr, ':') >= 2) {
181 extract_token(histr, setstr, 1, ':', sizeof histr);
182 if (!strcmp(histr, "*")) {
183 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
187 strcpy(histr, lostr);
192 for (i = first_msg; i < IMAP->num_msgs; ++i) {
193 if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
194 IMAP->flags[i] |= IMAP_ANSWERED;
204 * Back end for imap_load_msgids()
206 * Optimization: instead of calling realloc() to add each message, we
207 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
208 * allows the mapping to proceed much faster.
210 void imap_add_single_msgid(long msgnum, void *userdata)
214 if (IMAP->num_msgs > IMAP->num_alloc) {
215 IMAP->num_alloc += REALLOC_INCREMENT;
216 IMAP->msgids = realloc(IMAP->msgids, (IMAP->num_alloc * sizeof(long)) );
217 IMAP->flags = realloc(IMAP->flags, (IMAP->num_alloc * sizeof(long)) );
219 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
220 IMAP->flags[IMAP->num_msgs - 1] = 0;
226 * Set up a message ID map for the current room (folder)
228 void imap_load_msgids(void)
230 struct cdbdata *cdbfr;
232 if (IMAP->selected == 0) {
233 CtdlLogPrintf(CTDL_ERR,
234 "imap_load_msgids() can't run; no room selected\n");
238 imap_free_msgids(); /* If there was already a map, free it */
240 /* Load the message list */
241 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
243 IMAP->msgids = malloc(cdbfr->len);
244 memcpy(IMAP->msgids, cdbfr->ptr, cdbfr->len);
245 IMAP->num_msgs = cdbfr->len / sizeof(long);
246 IMAP->num_alloc = cdbfr->len / sizeof(long);
250 if (IMAP->num_msgs) {
251 IMAP->flags = malloc(IMAP->num_alloc * sizeof(long));
252 memset(IMAP->flags, 0, (IMAP->num_alloc * sizeof(long)) );
255 imap_set_seen_flags(0);
260 * Re-scan the selected room (folder) and see if it's been changed at all
262 void imap_rescan_msgids(void)
265 int original_num_msgs = 0;
266 long original_highest = 0L;
268 int message_still_exists;
269 struct cdbdata *cdbfr;
270 long *msglist = NULL;
274 if (IMAP->selected == 0) {
275 CtdlLogPrintf(CTDL_ERR, "imap_load_msgids() can't run; no room selected\n");
280 * Check to see if the room's contents have changed.
281 * If not, we can avoid this rescan.
283 CtdlGetRoom(&CC->room, CC->room.QRname);
284 if (IMAP->last_mtime == CC->room.QRmtime) { /* No changes! */
288 /* Load the *current* message list from disk, so we can compare it
289 * to what we have in memory.
291 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
293 msglist = malloc(cdbfr->len);
294 if (msglist == NULL) {
295 CtdlLogPrintf(CTDL_CRIT, "malloc() failed\n");
298 memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
299 num_msgs = cdbfr->len / sizeof(long);
306 * Check to see if any of the messages we know about have been expunged
308 if (IMAP->num_msgs > 0) {
310 for (i = 0; i < IMAP->num_msgs; ++i) {
312 message_still_exists = 0;
314 for (j = jstart; j < num_msgs; ++j) {
315 if (msglist[j] == IMAP->msgids[i]) {
316 message_still_exists = 1;
323 if (message_still_exists == 0) {
324 cprintf("* %d EXPUNGE\r\n", i + 1);
326 /* Here's some nice stupid nonsense. When a
327 * message is expunged, we have to slide all
328 * the existing messages up in the message
332 memcpy(&IMAP->msgids[i],
333 &IMAP->msgids[i + 1],
335 (IMAP->num_msgs - i)));
336 memcpy(&IMAP->flags[i],
339 (IMAP->num_msgs - i)));
348 * Remember how many messages were here before we re-scanned.
350 original_num_msgs = IMAP->num_msgs;
351 if (IMAP->num_msgs > 0) {
352 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
354 original_highest = 0L;
358 * Now peruse the room for *new* messages only.
359 * This logic is probably the cause of Bug # 368
360 * [ http://bugzilla.citadel.org/show_bug.cgi?id=368 ]
363 for (j = 0; j < num_msgs; ++j) {
364 if (msglist[j] > original_highest) {
365 imap_add_single_msgid(msglist[j], NULL);
369 imap_set_seen_flags(original_num_msgs);
372 * If new messages have arrived, tell the client about them.
374 if (IMAP->num_msgs > original_num_msgs) {
376 for (j = 0; j < num_msgs; ++j) {
377 if (IMAP->flags[j] & IMAP_RECENT) {
382 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
383 cprintf("* %d RECENT\r\n", num_recent);
389 IMAP->last_mtime = CC->room.QRmtime;
394 * This cleanup function blows away the temporary memory and files used by
397 void imap_cleanup_function(void)
400 /* Don't do this stuff if this is not a IMAP session! */
401 if (CC->h_command_function != imap_command_loop)
404 /* If there is a mailbox selected, auto-expunge it. */
405 if (IMAP->selected) {
409 CtdlLogPrintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
411 imap_free_transmitted_message();
413 if (IMAP->cached_rfc822 != NULL) {
414 FreeStrBuf(&IMAP->cached_rfc822);
415 IMAP->cached_rfc822_msgnum = (-1);
416 IMAP->cached_rfc822_withbody = 0;
419 if (IMAP->cached_body != NULL) {
420 free(IMAP->cached_body);
421 IMAP->cached_body = NULL;
422 IMAP->cached_body_len = 0;
423 IMAP->cached_bodymsgnum = (-1);
425 FreeStrBuf(&IMAP->Cmd.CmdBuf);
426 if (IMAP->Cmd.Params != NULL) free(IMAP->Cmd.Params);
428 CtdlLogPrintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
433 * Does the actual work of the CAPABILITY command (because we need to
434 * output this stuff in other places as well)
436 void imap_output_capability_string(void) {
437 cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN UIDPLUS");
440 if (!CC->redirect_ssl) cprintf(" STARTTLS");
443 #ifndef DISABLE_IMAP_ACL
447 /* We are building a partial implementation of METADATA for the sole purpose
448 * of interoperating with the ical/vcard version of the Bynari Insight Connector.
449 * It is not a full RFC5464 implementation, but it should refuse non-Bynari
450 * metadata in a compatible and graceful way.
452 cprintf(" METADATA");
455 * LIST-EXTENDED was originally going to be required by the METADATA extension.
456 * It was mercifully removed prior to the finalization of RFC5464. We started
457 * implementing this but stopped when we learned that it would not be needed.
458 * If you uncomment this declaration you are responsible for writing a lot of new
461 * cprintf(" LIST-EXTENDED")
467 * implements the CAPABILITY command
469 void imap_capability(int num_parms, ConstStr *Params)
472 imap_output_capability_string();
474 cprintf("%s OK CAPABILITY completed\r\n", Params[0].Key);
479 * Implements the ID command (specified by RFC2971)
481 * We ignore the client-supplied information, and output a NIL response.
482 * Although this is technically a valid implementation of the extension, it
483 * is quite useless. It exists only so that we may see which clients are
484 * making use of this extension.
487 void imap_id(int num_parms, ConstStr *Params)
489 cprintf("* ID NIL\r\n");
490 cprintf("%s OK ID completed\r\n", Params[0].Key);
495 * Here's where our IMAP session begins its happy day.
497 void imap_greeting(void)
500 strcpy(CC->cs_clientname, "IMAP session");
501 CC->session_specific_data = malloc(sizeof(citimap));
502 memset(IMAP, 0, sizeof(citimap));
503 IMAP->authstate = imap_as_normal;
504 IMAP->cached_rfc822_msgnum = (-1);
505 IMAP->cached_rfc822_withbody = 0;
509 cprintf("* BYE; Server busy, try later\r\n");
514 imap_output_capability_string();
515 cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
520 * IMAPS is just like IMAP, except it goes crypto right away.
522 void imaps_greeting(void) {
523 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
525 if (!CC->redirect_ssl) CC->kill_me = 1; /* kill session if no crypto */
532 * implements the LOGIN command (ordinary username/password login)
534 void imap_login(int num_parms, ConstStr *Params)
539 if (Params[2].Key[0] == '{') {
540 cprintf("+ go ahead\r\n");
541 IMAP->authstate = imap_as_expecting_multilineusername;
542 strcpy(IMAP->authseq, Params[0].Key);
546 cprintf("%s BAD incorrect number of parameters\r\n", Params[0].Key);
550 if (CtdlLoginExistingUser(NULL, Params[2].Key) == login_ok) {
551 if (CtdlTryPassword(Params[3].Key, Params[3].len) == pass_ok) {
552 cprintf("%s OK [", Params[0].Key);
553 imap_output_capability_string();
554 cprintf("] Hello, %s\r\n", CC->user.fullname);
559 cprintf("%s NO AUTHENTICATE %s failed\r\n",
560 Params[0].Key, Params[3].Key);
564 cprintf("%s BAD Login incorrect\r\n", Params[0].Key);
566 cprintf("%s BAD incorrect number of parameters\r\n", Params[0].Key);
574 * Implements the AUTHENTICATE command
576 void imap_authenticate(int num_parms, ConstStr *Params)
580 if (num_parms != 3) {
581 cprintf("%s BAD incorrect number of parameters\r\n",
587 cprintf("%s BAD Already logged in.\r\n", Params[0].Key);
591 if (!strcasecmp(Params[2].Key, "LOGIN")) {
592 CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
593 cprintf("+ %s\r\n", UsrBuf);
594 IMAP->authstate = imap_as_expecting_username;
595 strcpy(IMAP->authseq, Params[0].Key);
599 if (!strcasecmp(Params[2].Key, "PLAIN")) {
600 // CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
601 // cprintf("+ %s\r\n", UsrBuf);
603 IMAP->authstate = imap_as_expecting_plainauth;
604 strcpy(IMAP->authseq, Params[0].Key);
609 cprintf("%s NO AUTHENTICATE %s failed\r\n",
610 Params[0].Key, Params[1].Key);
615 void imap_auth_plain(void)
617 const char *decoded_authstring;
624 memset(pass, 0, sizeof(pass));
625 StrBufDecodeBase64(IMAP->Cmd.CmdBuf);
627 decoded_authstring = ChrPtr(IMAP->Cmd.CmdBuf);
628 safestrncpy(ident, decoded_authstring, sizeof ident);
629 safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
630 len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
632 len = sizeof(pass) - 1;
634 IMAP->authstate = imap_as_normal;
636 if (!IsEmptyStr(ident)) {
637 result = CtdlLoginExistingUser(user, ident);
640 result = CtdlLoginExistingUser(NULL, user);
643 if (result == login_ok) {
644 if (CtdlTryPassword(pass, len) == pass_ok) {
645 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
649 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
653 void imap_auth_login_user(long state)
656 citimap *Imap = IMAP;
659 case imap_as_expecting_username:
660 StrBufDecodeBase64(Imap->Cmd.CmdBuf);
661 CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
662 CtdlEncodeBase64(PWBuf, "Password:", 9, 0);
663 cprintf("+ %s\r\n", PWBuf);
665 Imap->authstate = imap_as_expecting_password;
667 case imap_as_expecting_multilineusername:
668 extract_token(PWBuf, ChrPtr(Imap->Cmd.CmdBuf), 1, ' ', sizeof(PWBuf));
669 CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
670 cprintf("+ go ahead\r\n");
671 IMAP->authstate = imap_as_expecting_multilinepassword;
677 void imap_auth_login_pass(long state)
679 citimap *Imap = IMAP;
680 const char *pass = NULL;
685 case imap_as_expecting_password:
686 StrBufDecodeBase64(Imap->Cmd.CmdBuf);
687 pass = ChrPtr(Imap->Cmd.CmdBuf);
688 len = StrLength(Imap->Cmd.CmdBuf);
690 case imap_as_expecting_multilinepassword:
691 pass = ChrPtr(Imap->Cmd.CmdBuf);
692 len = StrLength(Imap->Cmd.CmdBuf);
695 if (len > USERNAME_SIZE)
696 StrBufCutAt(Imap->Cmd.CmdBuf, USERNAME_SIZE, NULL);
698 if (CtdlTryPassword(pass, len) == pass_ok) {
699 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
701 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
703 IMAP->authstate = imap_as_normal;
709 * implements the STARTTLS command (Citadel API version)
711 void imap_starttls(int num_parms, ConstStr *Params)
713 char ok_response[SIZ];
714 char nosup_response[SIZ];
715 char error_response[SIZ];
717 snprintf(ok_response, SIZ, "%s OK begin TLS negotiation now\r\n", Params[0].Key);
718 snprintf(nosup_response, SIZ, "%s NO TLS not supported here\r\n", Params[0].Key);
719 snprintf(error_response, SIZ, "%s BAD Internal error\r\n", Params[0].Key);
720 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
725 * implements the SELECT command
727 void imap_select(int num_parms, ConstStr *Params)
729 char towhere[ROOMNAMELEN];
730 char augmented_roomname[ROOMNAMELEN];
734 struct ctdlroom QRscratch;
740 /* Convert the supplied folder name to a roomname */
741 i = imap_roomname(towhere, sizeof towhere, Params[2].Key);
743 cprintf("%s NO Invalid mailbox name.\r\n", Params[0].Key);
747 floornum = (i & 0x00ff);
748 roomflags = (i & 0xff00);
750 /* First try a regular match */
751 c = CtdlGetRoom(&QRscratch, towhere);
753 /* Then try a mailbox name match */
755 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, towhere);
756 c = CtdlGetRoom(&QRscratch, augmented_roomname);
758 safestrncpy(towhere, augmented_roomname, sizeof(towhere));
762 /* If the room exists, check security/access */
764 /* See if there is an existing user/room relationship */
765 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
767 /* normal clients have to pass through security */
773 /* Fail here if no such room */
775 cprintf("%s NO ... no such room, or access denied\r\n", Params[0].Key);
779 /* If we already had some other folder selected, auto-expunge it */
783 * CtdlUserGoto() formally takes us to the desired room, happily returning
784 * the number of messages and number of new messages.
786 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
787 CtdlUserGoto(NULL, 0, 0, &msgs, &new);
790 if (!strcasecmp(Params[1].Key, "EXAMINE")) {
797 IMAP->last_mtime = CC->room.QRmtime;
799 cprintf("* %d EXISTS\r\n", msgs);
800 cprintf("* %d RECENT\r\n", new);
802 cprintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE);
803 cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
805 /* Technically, \Deleted is a valid flag, but not a permanent flag,
806 * because we don't maintain its state across sessions. Citadel
807 * automatically expunges mailboxes when they are de-selected.
809 * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes
810 * some clients (particularly Thunderbird) to misbehave -- they simply
811 * elect not to transmit the flag at all. So we have to advertise
812 * \Deleted as a PERMANENTFLAGS flag, even though it technically isn't.
814 cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
815 cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n");
817 cprintf("%s OK [%s] %s completed\r\n",
819 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), Params[1].Key
825 * Does the real work for expunge.
827 int imap_do_expunge(void)
830 int num_expunged = 0;
831 long *delmsgs = NULL;
834 CtdlLogPrintf(CTDL_DEBUG, "imap_do_expunge() called\n");
835 if (IMAP->selected == 0) {
839 if (IMAP->num_msgs > 0) {
840 delmsgs = malloc(IMAP->num_msgs * sizeof(long));
841 for (i = 0; i < IMAP->num_msgs; ++i) {
842 if (IMAP->flags[i] & IMAP_DELETED) {
843 delmsgs[num_delmsgs++] = IMAP->msgids[i];
846 if (num_delmsgs > 0) {
847 CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
849 num_expunged += num_delmsgs;
853 if (num_expunged > 0) {
854 imap_rescan_msgids();
857 CtdlLogPrintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n", num_expunged, CC->room.QRname);
858 return (num_expunged);
863 * implements the EXPUNGE command syntax
865 void imap_expunge(int num_parms, ConstStr *Params)
867 int num_expunged = 0;
869 num_expunged = imap_do_expunge();
870 cprintf("%s OK expunged %d messages.\r\n", Params[0].Key, num_expunged);
875 * implements the CLOSE command
877 void imap_close(int num_parms, ConstStr *Params)
880 /* Yes, we always expunge on close. */
881 if (IMAP->selected) {
888 cprintf("%s OK CLOSE completed\r\n", Params[0].Key);
893 * Implements the NAMESPACE command.
895 void imap_namespace(int num_parms, ConstStr *Params)
902 cprintf("* NAMESPACE ");
904 /* All personal folders are subordinate to INBOX. */
905 cprintf("((\"INBOX/\" \"/\")) ");
907 /* Other users' folders ... coming soon! FIXME */
910 /* Show all floors as shared namespaces. Neato! */
912 for (i = 0; i < MAXFLOORS; ++i) {
913 fl = CtdlGetCachedFloor(i);
914 if (fl->f_flags & F_INUSE) {
915 if (floors > 0) cprintf(" ");
917 snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
918 plain_imap_strout(Namespace);
925 /* Wind it up with a newline and a completion message. */
927 cprintf("%s OK NAMESPACE completed\r\n", Params[0].Key);
932 * Implements the CREATE command
935 void imap_create(int num_parms, ConstStr *Params)
938 char roomname[ROOMNAMELEN];
943 char *notification_message = NULL;
946 cprintf("%s NO A foder name must be specified\r\n", Params[0].Key);
950 if (strchr(Params[2].Key, '\\') != NULL) {
951 cprintf("%s NO Invalid character in folder name\r\n", Params[0].Key);
952 CtdlLogPrintf(CTDL_DEBUG, "invalid character in folder name\n");
956 ret = imap_roomname(roomname, sizeof roomname, Params[2].Key);
958 cprintf("%s NO Invalid mailbox name or location\r\n",
960 CtdlLogPrintf(CTDL_DEBUG, "invalid mailbox name or location\n");
963 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
964 flags = (ret & 0xff00); /* upper 8 bits = flags */
966 if (flags & IR_MAILBOX) {
967 if (strncasecmp(Params[2].Key, "INBOX/", 6)) {
968 cprintf("%s NO Personal folders must be created under INBOX\r\n", Params[0].Key);
969 CtdlLogPrintf(CTDL_DEBUG, "not subordinate to inbox\n");
974 if (flags & IR_MAILBOX) {
975 newroomtype = 4; /* private mailbox */
976 newroomview = VIEW_MAILBOX;
978 newroomtype = 0; /* public folder */
979 newroomview = VIEW_BBS;
982 CtdlLogPrintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
983 roomname, floornum, newroomtype);
985 ret = CtdlCreateRoom(roomname, newroomtype, "", floornum, 1, 0, newroomview);
987 /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY! BYNARI CONNECTOR DEPENDS ON IT! ***/
988 cprintf("%s NO Mailbox already exists, or create failed\r\n", Params[0].Key);
990 cprintf("%s OK CREATE completed\r\n", Params[0].Key);
991 /* post a message in Aide> describing the new room */
992 notification_message = malloc(1024);
993 snprintf(notification_message, 1024,
994 "A new room called \"%s\" has been created by %s%s%s%s\n",
997 ((ret & QR_MAILBOX) ? " [personal]" : ""),
998 ((ret & QR_PRIVATE) ? " [private]" : ""),
999 ((ret & QR_GUESSNAME) ? " [hidden]" : "")
1001 CtdlAideMessage(notification_message, "Room Creation Message");
1002 free(notification_message);
1004 CtdlLogPrintf(CTDL_DEBUG, "imap_create() completed\n");
1009 * Locate a room by its IMAP folder name, and check access to it.
1010 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
1012 int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok)
1015 char augmented_roomname[ROOMNAMELEN];
1016 char roomname[ROOMNAMELEN];
1018 struct ctdlroom QRscratch;
1022 ret = imap_roomname(roomname, sizeof roomname, foldername);
1027 /* First try a regular match */
1028 c = CtdlGetRoom(&QRscratch, roomname);
1030 /* Then try a mailbox name match */
1032 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1033 &CC->user, roomname);
1034 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1036 safestrncpy(roomname, augmented_roomname, sizeof(roomname));
1039 /* If the room exists, check security/access */
1041 /* See if there is an existing user/room relationship */
1042 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1044 /* normal clients have to pass through security */
1045 if (ra & UA_KNOWN) {
1048 if ((zapped_ok) && (ra & UA_ZAPPED)) {
1053 /* Fail here if no such room */
1055 strcpy(returned_roomname, "");
1058 safestrncpy(returned_roomname, QRscratch.QRname, ROOMNAMELEN);
1065 * Implements the STATUS command (sort of)
1068 void imap_status(int num_parms, ConstStr *Params)
1071 char roomname[ROOMNAMELEN];
1072 char imaproomname[SIZ];
1073 char savedroom[ROOMNAMELEN];
1076 ret = imap_grabroom(roomname, Params[2].Key, 1);
1079 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1085 * CtdlUserGoto() formally takes us to the desired room, happily returning
1086 * the number of messages and number of new messages. (If another
1087 * folder is selected, save its name so we can return there!!!!!)
1089 if (IMAP->selected) {
1090 strcpy(savedroom, CC->room.QRname);
1092 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1095 * Tell the client what it wants to know. In fact, tell it *more* than
1096 * it wants to know. We happily IGnore the supplied status data item
1097 * names and simply spew all possible data items. It's far easier to
1098 * code and probably saves us some processing time too.
1100 imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
1101 cprintf("* STATUS ");
1102 plain_imap_strout(imaproomname);
1103 cprintf(" (MESSAGES %d ", msgs);
1104 cprintf("RECENT %d ", new); /* Initially, new==recent */
1105 cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
1106 cprintf("UNSEEN %d)\r\n", new);
1109 * If another folder is selected, go back to that room so we can resume
1110 * our happy day without violent explosions.
1112 if (IMAP->selected) {
1113 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1117 * Oooh, look, we're done!
1119 cprintf("%s OK STATUS completed\r\n", Params[0].Key);
1124 * Implements the SUBSCRIBE command
1127 void imap_subscribe(int num_parms, ConstStr *Params)
1130 char roomname[ROOMNAMELEN];
1131 char savedroom[ROOMNAMELEN];
1134 ret = imap_grabroom(roomname, Params[2].Key, 1);
1137 "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1145 * CtdlUserGoto() formally takes us to the desired room, which has the side
1146 * effect of marking the room as not-zapped ... exactly the effect
1147 * we're looking for.
1149 if (IMAP->selected) {
1150 strcpy(savedroom, CC->room.QRname);
1152 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1155 * If another folder is selected, go back to that room so we can resume
1156 * our happy day without violent explosions.
1158 if (IMAP->selected) {
1159 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1162 cprintf("%s OK SUBSCRIBE completed\r\n", Params[0].Key);
1167 * Implements the UNSUBSCRIBE command
1170 void imap_unsubscribe(int num_parms, ConstStr *Params)
1173 char roomname[ROOMNAMELEN];
1174 char savedroom[ROOMNAMELEN];
1177 ret = imap_grabroom(roomname, Params[2].Key, 1);
1180 ("%s NO Invalid mailbox name or location, or access denied\r\n",
1186 * CtdlUserGoto() formally takes us to the desired room.
1188 if (IMAP->selected) {
1189 strcpy(savedroom, CC->room.QRname);
1191 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1194 * Now make the API call to zap the room
1196 if (CtdlForgetThisRoom() == 0) {
1197 cprintf("%s OK UNSUBSCRIBE completed\r\n", Params[0].Key);
1200 ("%s NO You may not unsubscribe from this folder.\r\n",
1205 * If another folder is selected, go back to that room so we can resume
1206 * our happy day without violent explosions.
1208 if (IMAP->selected) {
1209 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1215 * Implements the DELETE command
1218 void imap_delete(int num_parms, ConstStr *Params)
1221 char roomname[ROOMNAMELEN];
1222 char savedroom[ROOMNAMELEN];
1225 ret = imap_grabroom(roomname, Params[2].Key, 1);
1227 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1233 * CtdlUserGoto() formally takes us to the desired room, happily returning
1234 * the number of messages and number of new messages. (If another
1235 * folder is selected, save its name so we can return there!!!!!)
1237 if (IMAP->selected) {
1238 strcpy(savedroom, CC->room.QRname);
1240 CtdlUserGoto(roomname, 0, 0, &msgs, &new);
1243 * Now delete the room.
1245 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1246 CtdlScheduleRoomForDeletion(&CC->room);
1247 cprintf("%s OK DELETE completed\r\n", Params[0].Key);
1249 cprintf("%s NO Can't delete this folder.\r\n", Params[0].Key);
1253 * If another folder is selected, go back to that room so we can resume
1254 * our happy day without violent explosions.
1256 if (IMAP->selected) {
1257 CtdlUserGoto(savedroom, 0, 0, &msgs, &new);
1263 * Back end function for imap_rename()
1265 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1267 char foldername[SIZ];
1268 char newfoldername[SIZ];
1269 char newroomname[ROOMNAMELEN];
1271 struct irl *irlp = NULL; /* scratch pointer */
1272 struct irlparms *irlparms;
1274 irlparms = (struct irlparms *) data;
1275 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1277 /* Rename subfolders */
1278 if ((!strncasecmp(foldername, irlparms->oldname,
1279 strlen(irlparms->oldname))
1280 && (foldername[strlen(irlparms->oldname)] == '/'))) {
1282 sprintf(newfoldername, "%s/%s",
1284 &foldername[strlen(irlparms->oldname) + 1]
1287 newfloor = imap_roomname(newroomname,
1289 newfoldername) & 0xFF;
1291 irlp = (struct irl *) malloc(sizeof(struct irl));
1292 strcpy(irlp->irl_newroom, newroomname);
1293 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1294 irlp->irl_newfloor = newfloor;
1295 irlp->next = *(irlparms->irl);
1296 *(irlparms->irl) = irlp;
1302 * Implements the RENAME command
1305 void imap_rename(int num_parms, ConstStr *Params)
1307 char old_room[ROOMNAMELEN];
1308 char new_room[ROOMNAMELEN];
1312 struct irl *irl = NULL; /* the list */
1313 struct irl *irlp = NULL; /* scratch pointer */
1314 struct irlparms irlparms;
1317 if (strchr(Params[3].Key, '\\') != NULL) {
1318 cprintf("%s NO Invalid character in folder name\r\n",
1323 oldr = imap_roomname(old_room, sizeof old_room, Params[2].Key);
1324 newr = imap_roomname(new_room, sizeof new_room, Params[3].Key);
1325 new_floor = (newr & 0xFF);
1327 r = CtdlRenameRoom(old_room, new_room, new_floor);
1329 if (r == crr_room_not_found) {
1330 cprintf("%s NO Could not locate this folder\r\n",
1334 if (r == crr_already_exists) {
1335 cprintf("%s NO '%s' already exists.\r\n", Params[0].Key, Params[2].Key);
1338 if (r == crr_noneditable) {
1339 cprintf("%s NO This folder is not editable.\r\n", Params[0].Key);
1342 if (r == crr_invalid_floor) {
1343 cprintf("%s NO Folder root does not exist.\r\n", Params[0].Key);
1346 if (r == crr_access_denied) {
1347 cprintf("%s NO You do not have permission to edit this folder.\r\n",
1352 cprintf("%s NO Rename failed - undefined error %d\r\n",
1357 /* If this is the INBOX, then RFC2060 says we have to just move the
1358 * contents. In a Citadel environment it's easier to rename the room
1359 * (already did that) and create a new inbox.
1361 if (!strcasecmp(Params[2].Key, "INBOX")) {
1362 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
1365 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1367 irlparms.oldname = Params[2].Key;
1368 irlparms.newname = Params[3].Key;
1369 irlparms.irl = &irl;
1370 CtdlForEachRoom(imap_rename_backend, (void *) &irlparms);
1372 /* ... and now rename them. */
1373 while (irl != NULL) {
1374 r = CtdlRenameRoom(irl->irl_oldroom,
1378 /* FIXME handle error returns better */
1379 CtdlLogPrintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r);
1387 snprintf(aidemsg, sizeof aidemsg, "IMAP folder \"%s\" renamed to \"%s\" by %s\n",
1392 CtdlAideMessage(aidemsg, "IMAP folder rename");
1394 cprintf("%s OK RENAME completed\r\n", Params[0].Key);
1399 * Main command loop for IMAP sessions.
1401 void imap_command_loop(void)
1403 struct timeval tv1, tv2;
1404 suseconds_t total_time = 0;
1405 int untagged_ok = 1;
1407 const char *pchs, *pche;
1409 gettimeofday(&tv1, NULL);
1410 CC->lastcmd = time(NULL);
1414 if (Imap->Cmd.CmdBuf == NULL)
1415 Imap->Cmd.CmdBuf = NewStrBufPlain(NULL, SIZ);
1417 FlushStrBuf(Imap->Cmd.CmdBuf);
1419 if (CtdlClientGetLine(Imap->Cmd.CmdBuf) < 1) {
1420 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
1425 if (Imap->authstate == imap_as_expecting_password) {
1426 CtdlLogPrintf(CTDL_INFO, "IMAP: <password>\n");
1428 else if (Imap->authstate == imap_as_expecting_plainauth) {
1429 CtdlLogPrintf(CTDL_INFO, "IMAP: <plain_auth>\n");
1431 else if ((Imap->authstate == imap_as_expecting_multilineusername) ||
1432 cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
1433 CtdlLogPrintf(CTDL_INFO, "IMAP: LOGIN...\n");
1436 CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", ChrPtr(Imap->Cmd.CmdBuf));
1439 pchs = ChrPtr(Imap->Cmd.CmdBuf);
1440 pche = pchs + StrLength(Imap->Cmd.CmdBuf);
1442 while ((pche > pchs) &&
1447 StrBufCutRight(Imap->Cmd.CmdBuf, 1);
1449 StrBufTrim(Imap->Cmd.CmdBuf);
1451 /* If we're in the middle of a multi-line command, handle that */
1452 switch (Imap->authstate){
1453 case imap_as_expecting_username:
1454 imap_auth_login_user(imap_as_expecting_username);
1456 case imap_as_expecting_multilineusername:
1457 imap_auth_login_user(imap_as_expecting_multilineusername);
1459 case imap_as_expecting_plainauth:
1462 case imap_as_expecting_password:
1463 imap_auth_login_pass(imap_as_expecting_password);
1465 case imap_as_expecting_multilinepassword:
1466 imap_auth_login_pass(imap_as_expecting_multilinepassword);
1473 /* Ok, at this point we're in normal command mode.
1474 * If the command just submitted does not contain a literal, we
1475 * might think about delivering some untagged stuff...
1477 if (*(ChrPtr(Imap->Cmd.CmdBuf) + StrLength(Imap->Cmd.CmdBuf) - 1)
1482 /* Grab the tag, command, and parameters. */
1483 imap_parameterize(&Imap->Cmd);
1485 /* debug output the parsed vector */
1488 CtdlLogPrintf(CTDL_DEBUG, "----- %ld params \n",
1489 Imap->Cmd.num_parms);
1491 for (i=0; i < Imap->Cmd.num_parms; i++) {
1492 if (Imap->Cmd.Params[i].len != strlen(Imap->Cmd.Params[i].Key))
1493 CtdlLogPrintf(CTDL_DEBUG, "*********** %ld != %ld : %s\n",
1494 Imap->Cmd.Params[i].len,
1495 strlen(Imap->Cmd.Params[i].Key),
1496 Imap->Cmd.Params[i].Key);
1498 CtdlLogPrintf(CTDL_DEBUG, "%ld : %s\n",
1499 Imap->Cmd.Params[i].len,
1500 Imap->Cmd.Params[i].Key);
1504 /* RFC3501 says that we cannot output untagged data during these commands */
1505 if (Imap->Cmd.num_parms >= 2) {
1506 if ( (!strcasecmp(Imap->Cmd.Params[1].Key, "FETCH"))
1507 || (!strcasecmp(Imap->Cmd.Params[1].Key, "STORE"))
1508 || (!strcasecmp(Imap->Cmd.Params[1].Key, "SEARCH"))
1516 /* we can put any additional untagged stuff right here in the future */
1519 * Before processing the command that was just entered... if we happen
1520 * to have a folder selected, we'd like to rescan that folder for new
1521 * messages, and for deletions/changes of existing messages. This
1522 * could probably be optimized better with some deep thought...
1524 if (Imap->selected) {
1525 imap_rescan_msgids();
1529 /* Now for the command set. */
1531 if (Imap->Cmd.num_parms < 2) {
1532 cprintf("BAD syntax error\r\n");
1535 /* The commands below may be executed in any state */
1537 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "NOOP"))
1538 || (!strcasecmp(Imap->Cmd.Params[1].Key, "CHECK"))) {
1539 cprintf("%s OK No operation\r\n",
1540 Imap->Cmd.Params[0].Key);
1543 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "ID")) {
1544 imap_id(Imap->Cmd.num_parms, Imap->Cmd.Params);
1548 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LOGOUT")) {
1549 if (Imap->selected) {
1550 imap_do_expunge(); /* yes, we auto-expunge at logout */
1552 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1553 cprintf("%s OK Citadel IMAP session ended.\r\n",
1554 Imap->Cmd.Params[0].Key);
1559 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LOGIN")) {
1560 imap_login(Imap->Cmd.num_parms, Imap->Cmd.Params);
1563 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "AUTHENTICATE")) {
1564 imap_authenticate(Imap->Cmd.num_parms, Imap->Cmd.Params);
1567 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "CAPABILITY")) {
1568 imap_capability(Imap->Cmd.num_parms, Imap->Cmd.Params);
1571 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "STARTTLS")) {
1572 imap_starttls(Imap->Cmd.num_parms, Imap->Cmd.Params);
1575 else if (!CC->logged_in) {
1576 cprintf("%s BAD Not logged in.\r\n", Imap->Cmd.Params[0].Key);
1579 /* The commans below require a logged-in state */
1581 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SELECT")) {
1582 imap_select(Imap->Cmd.num_parms, Imap->Cmd.Params);
1585 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "EXAMINE")) {
1586 imap_select(Imap->Cmd.num_parms, Imap->Cmd.Params);
1589 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LSUB")) {
1590 imap_list(Imap->Cmd.num_parms, Imap->Cmd.Params);
1593 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LIST")) {
1594 imap_list(Imap->Cmd.num_parms, Imap->Cmd.Params);
1597 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "CREATE")) {
1598 imap_create(Imap->Cmd.num_parms, Imap->Cmd.Params);
1601 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "DELETE")) {
1602 imap_delete(Imap->Cmd.num_parms, Imap->Cmd.Params);
1605 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "RENAME")) {
1606 imap_rename(Imap->Cmd.num_parms, Imap->Cmd.Params);
1609 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "STATUS")) {
1610 imap_status(Imap->Cmd.num_parms, Imap->Cmd.Params);
1613 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SUBSCRIBE")) {
1614 imap_subscribe(Imap->Cmd.num_parms, Imap->Cmd.Params);
1617 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "UNSUBSCRIBE")) {
1618 imap_unsubscribe(Imap->Cmd.num_parms, Imap->Cmd.Params);
1621 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "APPEND")) {
1622 imap_append(Imap->Cmd.num_parms, Imap->Cmd.Params);
1625 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "NAMESPACE")) {
1626 imap_namespace(Imap->Cmd.num_parms, Imap->Cmd.Params);
1629 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SETACL")) {
1630 imap_setacl(Imap->Cmd.num_parms, Imap->Cmd.Params);
1633 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "DELETEACL")) {
1634 imap_deleteacl(Imap->Cmd.num_parms, Imap->Cmd.Params);
1637 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "GETACL")) {
1638 imap_getacl(Imap->Cmd.num_parms, Imap->Cmd.Params);
1641 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "LISTRIGHTS")) {
1642 imap_listrights(Imap->Cmd.num_parms, Imap->Cmd.Params);
1645 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "MYRIGHTS")) {
1646 imap_myrights(Imap->Cmd.num_parms, Imap->Cmd.Params);
1649 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "GETMETADATA")) {
1650 imap_getmetadata(Imap->Cmd.num_parms, Imap->Cmd.Params);
1653 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SETMETADATA")) {
1654 imap_setmetadata(Imap->Cmd.num_parms, Imap->Cmd.Params);
1657 else if (Imap->selected == 0) {
1658 cprintf("%s BAD no folder selected\r\n", Imap->Cmd.Params[0].Key);
1661 /* The commands below require the SELECT state on a mailbox */
1663 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "FETCH")) {
1664 imap_fetch(Imap->Cmd.num_parms, Imap->Cmd.Params);
1667 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID"))
1668 && (!strcasecmp(Imap->Cmd.Params[2].Key, "FETCH"))) {
1669 imap_uidfetch(Imap->Cmd.num_parms, Imap->Cmd.Params);
1672 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "SEARCH")) {
1673 imap_search(Imap->Cmd.num_parms, Imap->Cmd.Params);
1676 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID"))
1677 && (!strcasecmp(Imap->Cmd.Params[2].Key, "SEARCH"))) {
1678 imap_uidsearch(Imap->Cmd.num_parms, Imap->Cmd.Params);
1681 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "STORE")) {
1682 imap_store(Imap->Cmd.num_parms, Imap->Cmd.Params);
1685 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID"))
1686 && (!strcasecmp(Imap->Cmd.Params[2].Key, "STORE"))) {
1687 imap_uidstore(Imap->Cmd.num_parms, Imap->Cmd.Params);
1690 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "COPY")) {
1691 imap_copy(Imap->Cmd.num_parms, Imap->Cmd.Params);
1694 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID")) && (!strcasecmp(Imap->Cmd.Params[2].Key, "COPY"))) {
1695 imap_uidcopy(Imap->Cmd.num_parms, Imap->Cmd.Params);
1698 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "EXPUNGE")) {
1699 imap_expunge(Imap->Cmd.num_parms, Imap->Cmd.Params);
1702 else if ((!strcasecmp(Imap->Cmd.Params[1].Key, "UID")) && (!strcasecmp(Imap->Cmd.Params[2].Key, "EXPUNGE"))) {
1703 imap_expunge(Imap->Cmd.num_parms, Imap->Cmd.Params);
1706 else if (!strcasecmp(Imap->Cmd.Params[1].Key, "CLOSE")) {
1707 imap_close(Imap->Cmd.num_parms, Imap->Cmd.Params);
1710 /* End of commands. If we get here, the command is either invalid
1715 cprintf("%s BAD command unrecognized\r\n", Imap->Cmd.Params[0].Key);
1718 /* If the client transmitted a message we can free it now */
1719 imap_free_transmitted_message();
1721 gettimeofday(&tv2, NULL);
1722 total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
1723 CtdlLogPrintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n",
1724 (total_time / 1000000),
1725 (total_time % 1000000)
1730 const char *CitadelServiceIMAP="IMAP";
1731 const char *CitadelServiceIMAPS="IMAPS";
1734 * This function is called to register the IMAP extension with Citadel.
1736 CTDL_MODULE_INIT(imap)
1740 CtdlRegisterServiceHook(config.c_imap_port,
1741 NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP);
1743 CtdlRegisterServiceHook(config.c_imaps_port,
1744 NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS);
1746 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
1749 /* return our Subversion id for the Log */