2 * IMAP server for the Citadel system
4 * Copyright (C) 2000-2011 by Art Cancro and others.
5 * This code is released under the terms of the GNU General Public License.
7 * WARNING: the IMAP protocol is badly designed. No implementation of it
8 * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP
9 * implementations have bugs.
11 * This program is open source software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.h"
63 #include "imap_list.h"
64 #include "imap_fetch.h"
65 #include "imap_search.h"
66 #include "imap_store.h"
68 #include "imap_metadata.h"
69 #include "imap_misc.h"
71 #include "ctdl_module.h"
72 int IMAPDebugEnabled = 0;
73 HashList *ImapCmds = NULL;
74 void registerImapCMD(const char *First, long FLen,
75 const char *Second, long SLen,
81 h = (imap_handler_hook*) malloc(sizeof(imap_handler_hook));
82 memset(h, 0, sizeof(imap_handler_hook));
87 Put(ImapCmds, First, FLen, h, NULL);
91 memcpy(CMD, First, FLen);
92 memcpy(CMD+FLen, Second, SLen);
93 CMD[FLen+SLen] = '\0';
94 Put(ImapCmds, CMD, FLen + SLen, h, NULL);
98 void imap_cleanup(void)
100 DeleteHash(&ImapCmds);
103 const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
105 struct CitContext *CCC = CC;
107 citimap *Imap = CCCIMAP;
112 /* we abuse the Reply-buffer for uppercasing... */
113 StrBufPlain(Imap->Reply, CKEY(Params[1]));
114 StrBufUpCase(Imap->Reply);
116 IMAP_syslog(LOG_DEBUG, "---- Looking up [%s] -----",
117 ChrPtr(Imap->Reply));
118 if (GetHash(ImapCmds, SKEY(Imap->Reply), &v))
120 IMAPM_syslog(LOG_DEBUG, "Found.");
121 FlushStrBuf(Imap->Reply);
122 return (imap_handler_hook *) v;
127 IMAPM_syslog(LOG_DEBUG, "NOT Found.");
128 FlushStrBuf(Imap->Reply);
132 IMAP_syslog(LOG_DEBUG, "---- Looking up [%s] -----",
133 ChrPtr(Imap->Reply));
134 StrBufAppendBufPlain(Imap->Reply, CKEY(Params[2]), 0);
135 StrBufUpCase(Imap->Reply);
136 if (GetHash(ImapCmds, SKEY(Imap->Reply), &v))
138 IMAPM_syslog(LOG_DEBUG, "Found.");
139 FlushStrBuf(Imap->Reply);
140 return (imap_handler_hook *) v;
142 IMAPM_syslog(LOG_DEBUG, "NOT Found.");
143 FlushStrBuf(Imap->Reply);
147 /* imap_rename() uses this struct containing list of rooms to rename */
150 char irl_oldroom[ROOMNAMELEN];
151 char irl_newroom[ROOMNAMELEN];
155 /* Data which is passed between imap_rename() and imap_rename_backend() */
156 typedef struct __irlparms {
166 * If there is a message ID map in memory, free it
168 void imap_free_msgids(void)
170 citimap *Imap = IMAP;
171 if (Imap->msgids != NULL) {
177 if (Imap->flags != NULL) {
181 Imap->last_mtime = (-1);
186 * If there is a transmitted message in memory, free it
188 void imap_free_transmitted_message(void)
190 FreeStrBuf(&IMAP->TransmittedMessage);
195 * Set the \Seen, \Recent. and \Answered flags, based on the sequence
196 * sets stored in the visit record for this user/room. Note that we have
197 * to parse each sequence set manually here, because calling the utility
198 * function is_msg_in_sequence_set() over and over again is too expensive.
200 * first_msg should be set to 0 to rescan the flags for every message in the
201 * room, or some other value if we're only interested in an incremental
204 void imap_set_seen_flags(int first_msg)
206 citimap *Imap = IMAP;
211 char setstr[64], lostr[64], histr[64];
214 if (Imap->num_msgs < 1) return;
215 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
217 for (i = first_msg; i < Imap->num_msgs; ++i) {
218 Imap->flags[i] = Imap->flags[i] & ~IMAP_SEEN;
219 Imap->flags[i] |= IMAP_RECENT;
220 Imap->flags[i] = Imap->flags[i] & ~IMAP_ANSWERED;
224 * Do the "\Seen" flag.
225 * (Any message not "\Seen" is considered "\Recent".)
227 num_sets = num_tokens(vbuf.v_seen, ',');
228 for (s=0; s<num_sets; ++s) {
229 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
231 extract_token(lostr, setstr, 0, ':', sizeof lostr);
232 if (num_tokens(setstr, ':') >= 2) {
233 extract_token(histr, setstr, 1, ':', sizeof histr);
234 if (!strcmp(histr, "*")) {
235 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
239 strcpy(histr, lostr);
244 for (i = first_msg; i < Imap->num_msgs; ++i) {
245 if ((Imap->msgids[i] >= lo) && (Imap->msgids[i] <= hi)){
246 Imap->flags[i] |= IMAP_SEEN;
247 Imap->flags[i] = Imap->flags[i] & ~IMAP_RECENT;
252 /* Do the ANSWERED flag */
253 num_sets = num_tokens(vbuf.v_answered, ',');
254 for (s=0; s<num_sets; ++s) {
255 extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
257 extract_token(lostr, setstr, 0, ':', sizeof lostr);
258 if (num_tokens(setstr, ':') >= 2) {
259 extract_token(histr, setstr, 1, ':', sizeof histr);
260 if (!strcmp(histr, "*")) {
261 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
265 strcpy(histr, lostr);
270 for (i = first_msg; i < Imap->num_msgs; ++i) {
271 if ((Imap->msgids[i] >= lo) && (Imap->msgids[i] <= hi)){
272 Imap->flags[i] |= IMAP_ANSWERED;
282 * Back end for imap_load_msgids()
284 * Optimization: instead of calling realloc() to add each message, we
285 * allocate space in the list for REALLOC_INCREMENT messages at a time. This
286 * allows the mapping to proceed much faster.
288 void imap_add_single_msgid(long msgnum, void *userdata)
290 citimap *Imap = IMAP;
293 if (Imap->num_msgs > Imap->num_alloc) {
294 Imap->num_alloc += REALLOC_INCREMENT;
295 Imap->msgids = realloc(Imap->msgids, (Imap->num_alloc * sizeof(long)) );
296 Imap->flags = realloc(Imap->flags, (Imap->num_alloc * sizeof(unsigned int)) );
298 Imap->msgids[Imap->num_msgs - 1] = msgnum;
299 Imap->flags[Imap->num_msgs - 1] = 0;
305 * Set up a message ID map for the current room (folder)
307 void imap_load_msgids(void)
309 struct CitContext *CCC = CC;
310 struct cdbdata *cdbfr;
311 citimap *Imap = CCCIMAP;
313 if (Imap->selected == 0) {
314 IMAPM_syslog(LOG_ERR, "imap_load_msgids() can't run; no room selected");
318 imap_free_msgids(); /* If there was already a map, free it */
320 /* Load the message list */
321 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
323 Imap->msgids = (long*)cdbfr->ptr;
324 Imap->num_msgs = cdbfr->len / sizeof(long);
325 Imap->num_alloc = cdbfr->len / sizeof(long);
331 if (Imap->num_msgs) {
332 Imap->flags = malloc(Imap->num_alloc * sizeof(unsigned int));
333 memset(Imap->flags, 0, (Imap->num_alloc * sizeof(unsigned int)) );
336 imap_set_seen_flags(0);
341 * Re-scan the selected room (folder) and see if it's been changed at all
343 void imap_rescan_msgids(void)
345 struct CitContext *CCC = CC;
346 citimap *Imap = CCCIMAP;
347 int original_num_msgs = 0;
348 long original_highest = 0L;
350 int message_still_exists;
351 struct cdbdata *cdbfr;
352 long *msglist = NULL;
356 if (Imap->selected == 0) {
357 IMAPM_syslog(LOG_ERR, "imap_load_msgids() can't run; no room selected");
362 * Check to see if the room's contents have changed.
363 * If not, we can avoid this rescan.
365 CtdlGetRoom(&CC->room, CC->room.QRname);
366 if (Imap->last_mtime == CC->room.QRmtime) { /* No changes! */
370 /* Load the *current* message list from disk, so we can compare it
371 * to what we have in memory.
373 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
375 msglist = (long*)cdbfr->ptr;
377 num_msgs = cdbfr->len / sizeof(long);
385 * Check to see if any of the messages we know about have been expunged
387 if (Imap->num_msgs > 0) {
389 for (i = 0; i < Imap->num_msgs; ++i) {
391 message_still_exists = 0;
393 for (j = jstart; j < num_msgs; ++j) {
394 if (msglist[j] == Imap->msgids[i]) {
395 message_still_exists = 1;
402 if (message_still_exists == 0) {
403 IAPrintf("* %d EXPUNGE\r\n", i + 1);
405 /* Here's some nice stupid nonsense. When a
406 * message is expunged, we have to slide all
407 * the existing messages up in the message
411 memmove(&Imap->msgids[i],
412 &Imap->msgids[i + 1],
414 (Imap->num_msgs - i)));
415 memmove(&Imap->flags[i],
417 (sizeof(unsigned int) *
418 (Imap->num_msgs - i)));
426 * Remember how many messages were here before we re-scanned.
428 original_num_msgs = Imap->num_msgs;
429 if (Imap->num_msgs > 0) {
430 original_highest = Imap->msgids[Imap->num_msgs - 1];
432 original_highest = 0L;
436 * Now peruse the room for *new* messages only.
437 * This logic is probably the cause of Bug # 368
438 * [ http://bugzilla.citadel.org/show_bug.cgi?id=368 ]
441 for (j = 0; j < num_msgs; ++j) {
442 if (msglist[j] > original_highest) {
443 imap_add_single_msgid(msglist[j], NULL);
447 imap_set_seen_flags(original_num_msgs);
450 * If new messages have arrived, tell the client about them.
452 if (Imap->num_msgs > original_num_msgs) {
454 for (j = 0; j < num_msgs; ++j) {
455 if (Imap->flags[j] & IMAP_RECENT) {
460 IAPrintf("* %d EXISTS\r\n", Imap->num_msgs);
461 IAPrintf("* %d RECENT\r\n", num_recent);
464 if (msglist != NULL) {
467 Imap->last_mtime = CC->room.QRmtime;
472 * This cleanup function blows away the temporary memory and files used by
475 void imap_cleanup_function(void)
477 struct CitContext *CCC = CC;
478 citimap *Imap = CCCIMAP;
480 /* Don't do this stuff if this is not a Imap session! */
481 if (CC->h_command_function != imap_command_loop)
484 /* If there is a mailbox selected, auto-expunge it. */
485 if (Imap->selected) {
489 IMAPM_syslog(LOG_DEBUG, "Performing IMAP cleanup hook");
491 imap_free_transmitted_message();
493 if (Imap->cached_rfc822 != NULL) {
494 FreeStrBuf(&Imap->cached_rfc822);
495 Imap->cached_rfc822_msgnum = (-1);
496 Imap->cached_rfc822_withbody = 0;
499 if (Imap->cached_body != NULL) {
500 free(Imap->cached_body);
501 Imap->cached_body = NULL;
502 Imap->cached_body_len = 0;
503 Imap->cached_bodymsgnum = (-1);
505 FreeStrBuf(&Imap->Cmd.CmdBuf);
506 FreeStrBuf(&Imap->Reply);
507 if (Imap->Cmd.Params != NULL) free(Imap->Cmd.Params);
509 IMAPM_syslog(LOG_DEBUG, "Finished IMAP cleanup hook");
514 * Does the actual work of the CAPABILITY command (because we need to
515 * output this stuff in other places as well)
517 void imap_output_capability_string(void) {
518 IAPuts("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN UIDPLUS");
521 if (!CC->redirect_ssl) IAPuts(" STARTTLS");
524 #ifndef DISABLE_IMAP_ACL
528 /* We are building a partial implementation of METADATA for the sole purpose
529 * of interoperating with the ical/vcard version of the Bynari Insight Connector.
530 * It is not a full RFC5464 implementation, but it should refuse non-Bynari
531 * metadata in a compatible and graceful way.
536 * LIST-EXTENDED was originally going to be required by the METADATA extension.
537 * It was mercifully removed prior to the finalization of RFC5464. We started
538 * implementing this but stopped when we learned that it would not be needed.
539 * If you uncomment this declaration you are responsible for writing a lot of new
542 * IAPuts(" LIST-EXTENDED")
548 * implements the CAPABILITY command
550 void imap_capability(int num_parms, ConstStr *Params)
553 imap_output_capability_string();
555 IReply("OK CAPABILITY completed");
560 * Implements the ID command (specified by RFC2971)
562 * We ignore the client-supplied information, and output a NIL response.
563 * Although this is technically a valid implementation of the extension, it
564 * is quite useless. It exists only so that we may see which clients are
565 * making use of this extension.
568 void imap_id(int num_parms, ConstStr *Params)
570 IAPuts("* ID NIL\r\n");
571 IReply("OK ID completed");
576 * Here's where our IMAP session begins its happy day.
578 void imap_greeting(void)
581 CitContext *CCC = CC;
583 strcpy(CCC->cs_clientname, "IMAP session");
584 CCC->session_specific_data = malloc(sizeof(citimap));
585 Imap = (citimap *)CCC->session_specific_data;
586 memset(Imap, 0, sizeof(citimap));
587 Imap->authstate = imap_as_normal;
588 Imap->cached_rfc822_msgnum = (-1);
589 Imap->cached_rfc822_withbody = 0;
590 Imap->Reply = NewStrBufPlain(NULL, SIZ * 10); /* 40k */
594 IAPuts("* BYE; Server busy, try later\r\n");
595 CCC->kill_me = KILLME_NOLOGIN;
601 imap_output_capability_string();
602 IAPrintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
608 * IMAPS is just like IMAP, except it goes crypto right away.
610 void imaps_greeting(void) {
611 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
613 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
620 * implements the LOGIN command (ordinary username/password login)
622 void imap_login(int num_parms, ConstStr *Params)
627 if (Params[2].Key[0] == '{') {
628 IAPuts("+ go ahead\r\n");
629 IMAP->authstate = imap_as_expecting_multilineusername;
630 strcpy(IMAP->authseq, Params[0].Key);
634 IReply("BAD incorrect number of parameters");
638 if (CtdlLoginExistingUser(NULL, Params[2].Key) == login_ok) {
639 if (CtdlTryPassword(Params[3].Key, Params[3].len) == pass_ok) {
640 /* hm, thats not doable by IReply :-( */
641 IAPrintf("%s OK [", Params[0].Key);
642 imap_output_capability_string();
643 IAPrintf("] Hello, %s\r\n", CC->user.fullname);
648 IReplyPrintf("NO AUTHENTICATE %s failed",
653 IReply("BAD Login incorrect");
655 IReply("BAD incorrect number of parameters");
663 * Implements the AUTHENTICATE command
665 void imap_authenticate(int num_parms, ConstStr *Params)
669 if (num_parms != 3) {
670 IReply("BAD incorrect number of parameters");
675 IReply("BAD Already logged in.");
679 if (!strcasecmp(Params[2].Key, "LOGIN")) {
680 size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
681 if (UsrBuf[len - 1] == '\n') {
682 UsrBuf[len - 1] = '\0';
685 IAPrintf("+ %s\r\n", UsrBuf);
686 IMAP->authstate = imap_as_expecting_username;
687 strcpy(IMAP->authseq, Params[0].Key);
691 if (!strcasecmp(Params[2].Key, "PLAIN")) {
692 // size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, 0);
693 // if (UsrBuf[len - 1] == '\n') {
694 // UsrBuf[len - 1] = '\0';
696 // IAPuts("+ %s\r\n", UsrBuf);
698 IMAP->authstate = imap_as_expecting_plainauth;
699 strcpy(IMAP->authseq, Params[0].Key);
704 IReplyPrintf("NO AUTHENTICATE %s failed",
710 void imap_auth_plain(void)
712 citimap *Imap = IMAP;
713 const char *decoded_authstring;
714 char ident[256] = "";
722 memset(pass, 0, sizeof(pass));
723 decoded_len = StrBufDecodeBase64(Imap->Cmd.CmdBuf);
727 decoded_authstring = ChrPtr(Imap->Cmd.CmdBuf);
729 len = safestrncpy(ident, decoded_authstring, sizeof ident);
731 decoded_len -= len - 1;
732 decoded_authstring += len + 1;
736 len = safestrncpy(user, decoded_authstring, sizeof user);
738 decoded_authstring += len + 1;
739 decoded_len -= len - 1;
744 plen = safestrncpy(pass, decoded_authstring, sizeof pass);
747 plen = sizeof(pass) - 1;
750 Imap->authstate = imap_as_normal;
752 if (!IsEmptyStr(ident)) {
753 result = CtdlLoginExistingUser(user, ident);
756 result = CtdlLoginExistingUser(NULL, user);
759 if (result == login_ok) {
760 if (CtdlTryPassword(pass, plen) == pass_ok) {
761 IAPrintf("%s OK authentication succeeded\r\n", Imap->authseq);
765 IAPrintf("%s NO authentication failed\r\n", Imap->authseq);
769 void imap_auth_login_user(long state)
772 citimap *Imap = IMAP;
775 case imap_as_expecting_username:
776 StrBufDecodeBase64(Imap->Cmd.CmdBuf);
777 CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
778 size_t len = CtdlEncodeBase64(PWBuf, "Password:", 9, 0);
779 if (PWBuf[len - 1] == '\n') {
780 PWBuf[len - 1] = '\0';
783 IAPrintf("+ %s\r\n", PWBuf);
785 Imap->authstate = imap_as_expecting_password;
787 case imap_as_expecting_multilineusername:
788 extract_token(PWBuf, ChrPtr(Imap->Cmd.CmdBuf), 1, ' ', sizeof(PWBuf));
789 CtdlLoginExistingUser(NULL, ChrPtr(Imap->Cmd.CmdBuf));
790 IAPuts("+ go ahead\r\n");
791 Imap->authstate = imap_as_expecting_multilinepassword;
797 void imap_auth_login_pass(long state)
799 citimap *Imap = IMAP;
800 const char *pass = NULL;
805 case imap_as_expecting_password:
806 StrBufDecodeBase64(Imap->Cmd.CmdBuf);
807 pass = ChrPtr(Imap->Cmd.CmdBuf);
808 len = StrLength(Imap->Cmd.CmdBuf);
810 case imap_as_expecting_multilinepassword:
811 pass = ChrPtr(Imap->Cmd.CmdBuf);
812 len = StrLength(Imap->Cmd.CmdBuf);
815 if (len > USERNAME_SIZE)
816 StrBufCutAt(Imap->Cmd.CmdBuf, USERNAME_SIZE, NULL);
818 if (CtdlTryPassword(pass, len) == pass_ok) {
819 IAPrintf("%s OK authentication succeeded\r\n", Imap->authseq);
821 IAPrintf("%s NO authentication failed\r\n", Imap->authseq);
823 Imap->authstate = imap_as_normal;
829 * implements the STARTTLS command (Citadel API version)
831 void imap_starttls(int num_parms, ConstStr *Params)
833 char ok_response[SIZ];
834 char nosup_response[SIZ];
835 char error_response[SIZ];
837 snprintf(ok_response, SIZ, "%s OK begin TLS negotiation now\r\n", Params[0].Key);
838 snprintf(nosup_response, SIZ, "%s NO TLS not supported here\r\n", Params[0].Key);
839 snprintf(error_response, SIZ, "%s BAD Internal error\r\n", Params[0].Key);
840 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
845 * implements the SELECT command
847 void imap_select(int num_parms, ConstStr *Params)
849 citimap *Imap = IMAP;
850 char towhere[ROOMNAMELEN];
851 char augmented_roomname[ROOMNAMELEN];
855 struct ctdlroom QRscratch;
859 /* Convert the supplied folder name to a roomname */
860 i = imap_roomname(towhere, sizeof towhere, Params[2].Key);
862 IReply("NO Invalid mailbox name.");
867 /* First try a regular match */
868 c = CtdlGetRoom(&QRscratch, towhere);
870 /* Then try a mailbox name match */
872 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, towhere);
873 c = CtdlGetRoom(&QRscratch, augmented_roomname);
875 safestrncpy(towhere, augmented_roomname, sizeof(towhere));
879 /* If the room exists, check security/access */
881 /* See if there is an existing user/room relationship */
882 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
884 /* normal clients have to pass through security */
890 /* Fail here if no such room */
892 IReply("NO ... no such room, or access denied");
896 /* If we already had some other folder selected, auto-expunge it */
900 * CtdlUserGoto() formally takes us to the desired room, happily returning
901 * the number of messages and number of new messages.
903 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
904 CtdlUserGoto(NULL, 0, 0, &msgs, &new, NULL, NULL);
907 if (!strcasecmp(Params[1].Key, "EXAMINE")) {
914 Imap->last_mtime = CC->room.QRmtime;
916 IAPrintf("* %d EXISTS\r\n", msgs);
917 IAPrintf("* %d RECENT\r\n", new);
919 IAPrintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE);
920 IAPrintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
922 /* Technically, \Deleted is a valid flag, but not a permanent flag,
923 * because we don't maintain its state across sessions. Citadel
924 * automatically expunges mailboxes when they are de-selected.
926 * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes
927 * some clients (particularly Thunderbird) to misbehave -- they simply
928 * elect not to transmit the flag at all. So we have to advertise
929 * \Deleted as a PERMANENTFLAGS flag, even though it technically isn't.
931 IAPuts("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
932 IAPuts("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n");
934 IReplyPrintf("OK [%s] %s completed",
935 (Imap->readonly ? "READ-ONLY" : "READ-WRITE"), Params[1].Key
941 * Does the real work for expunge.
943 int imap_do_expunge(void)
945 struct CitContext *CCC = CC;
946 citimap *Imap = CCCIMAP;
948 int num_expunged = 0;
949 long *delmsgs = NULL;
952 IMAPM_syslog(LOG_DEBUG, "imap_do_expunge() called");
953 if (Imap->selected == 0) {
957 if (Imap->num_msgs > 0) {
958 delmsgs = malloc(Imap->num_msgs * sizeof(long));
959 for (i = 0; i < Imap->num_msgs; ++i) {
960 if (Imap->flags[i] & IMAP_DELETED) {
961 delmsgs[num_delmsgs++] = Imap->msgids[i];
964 if (num_delmsgs > 0) {
965 CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
967 num_expunged += num_delmsgs;
971 if (num_expunged > 0) {
972 imap_rescan_msgids();
975 IMAP_syslog(LOG_DEBUG, "Expunged %d messages from <%s>", num_expunged, CC->room.QRname);
976 return (num_expunged);
981 * implements the EXPUNGE command syntax
983 void imap_expunge(int num_parms, ConstStr *Params)
985 int num_expunged = 0;
987 num_expunged = imap_do_expunge();
988 IReplyPrintf("OK expunged %d messages.", num_expunged);
993 * implements the CLOSE command
995 void imap_close(int num_parms, ConstStr *Params)
998 /* Yes, we always expunge on close. */
999 if (IMAP->selected) {
1006 IReply("OK CLOSE completed");
1011 * Implements the NAMESPACE command.
1013 void imap_namespace(int num_parms, ConstStr *Params)
1019 char Namespace[SIZ];
1021 IAPuts("* NAMESPACE ");
1023 /* All personal folders are subordinate to INBOX. */
1024 IAPuts("((\"INBOX/\" \"/\")) ");
1026 /* Other users' folders ... coming soon! FIXME */
1029 /* Show all floors as shared namespaces. Neato! */
1031 for (i = 0; i < MAXFLOORS; ++i) {
1032 fl = CtdlGetCachedFloor(i);
1033 if (fl->f_flags & F_INUSE) {
1034 /* if (floors > 0) IAPuts(" "); samjam says this confuses javamail */
1036 len = snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
1037 IPutStr(Namespace, len);
1044 /* Wind it up with a newline and a completion message. */
1046 IReply("OK NAMESPACE completed");
1051 * Implements the CREATE command
1054 void imap_create(int num_parms, ConstStr *Params)
1056 struct CitContext *CCC = CC;
1058 char roomname[ROOMNAMELEN];
1061 int newroomtype = 0;
1062 int newroomview = 0;
1063 char *notification_message = NULL;
1065 if (num_parms < 3) {
1066 IReply("NO A foder name must be specified");
1070 if (strchr(Params[2].Key, '\\') != NULL) {
1071 IReply("NO Invalid character in folder name");
1072 IMAPM_syslog(LOG_ERR, "invalid character in folder name");
1076 ret = imap_roomname(roomname, sizeof roomname, Params[2].Key);
1078 IReply("NO Invalid mailbox name or location");
1079 IMAPM_syslog(LOG_ERR, "invalid mailbox name or location");
1082 floornum = (ret & 0x00ff); /* lower 8 bits = floor number */
1083 flags = (ret & 0xff00); /* upper 8 bits = flags */
1085 if (flags & IR_MAILBOX) {
1086 if (strncasecmp(Params[2].Key, "INBOX/", 6)) {
1087 IReply("NO Personal folders must be created under INBOX");
1088 IMAPM_syslog(LOG_ERR, "not subordinate to inbox");
1093 if (flags & IR_MAILBOX) {
1094 newroomtype = 4; /* private mailbox */
1095 newroomview = VIEW_MAILBOX;
1097 newroomtype = 0; /* public folder */
1098 newroomview = VIEW_BBS;
1101 IMAP_syslog(LOG_INFO, "Create new room <%s> on floor <%d> with type <%d>",
1102 roomname, floornum, newroomtype);
1104 ret = CtdlCreateRoom(roomname, newroomtype, "", floornum, 1, 0, newroomview);
1106 /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY! BYNARI CONNECTOR DEPENDS ON IT! ***/
1107 IReply("NO Mailbox already exists, or create failed");
1109 IReply("OK CREATE completed");
1110 /* post a message in Aide> describing the new room */
1111 notification_message = malloc(1024);
1112 snprintf(notification_message, 1024,
1113 "A new room called \"%s\" has been created by %s%s%s%s\n",
1116 ((ret & QR_MAILBOX) ? " [personal]" : ""),
1117 ((ret & QR_PRIVATE) ? " [private]" : ""),
1118 ((ret & QR_GUESSNAME) ? " [hidden]" : "")
1120 CtdlAideMessage(notification_message, "Room Creation Message");
1121 free(notification_message);
1123 IMAPM_syslog(LOG_DEBUG, "imap_create() completed");
1128 * Locate a room by its IMAP folder name, and check access to it.
1129 * If zapped_ok is nonzero, we can also look for the room in the zapped list.
1131 int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok)
1134 char augmented_roomname[ROOMNAMELEN];
1135 char roomname[ROOMNAMELEN];
1137 struct ctdlroom QRscratch;
1141 ret = imap_roomname(roomname, sizeof roomname, foldername);
1146 /* First try a regular match */
1147 c = CtdlGetRoom(&QRscratch, roomname);
1149 /* Then try a mailbox name match */
1151 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
1152 &CC->user, roomname);
1153 c = CtdlGetRoom(&QRscratch, augmented_roomname);
1155 safestrncpy(roomname, augmented_roomname, sizeof(roomname));
1158 /* If the room exists, check security/access */
1160 /* See if there is an existing user/room relationship */
1161 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
1163 /* normal clients have to pass through security */
1164 if (ra & UA_KNOWN) {
1167 if ((zapped_ok) && (ra & UA_ZAPPED)) {
1172 /* Fail here if no such room */
1174 strcpy(returned_roomname, "");
1177 safestrncpy(returned_roomname, QRscratch.QRname, ROOMNAMELEN);
1184 * Implements the STATUS command (sort of)
1187 void imap_status(int num_parms, ConstStr *Params)
1191 char roomname[ROOMNAMELEN];
1192 char imaproomname[SIZ];
1193 char savedroom[ROOMNAMELEN];
1196 ret = imap_grabroom(roomname, Params[2].Key, 1);
1198 IReply("NO Invalid mailbox name or location, or access denied");
1203 * CtdlUserGoto() formally takes us to the desired room, happily returning
1204 * the number of messages and number of new messages. (If another
1205 * folder is selected, save its name so we can return there!!!!!)
1207 if (IMAP->selected) {
1208 strcpy(savedroom, CC->room.QRname);
1210 CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
1213 * Tell the client what it wants to know. In fact, tell it *more* than
1214 * it wants to know. We happily IGnore the supplied status data item
1215 * names and simply spew all possible data items. It's far easier to
1216 * code and probably saves us some processing time too.
1218 len = imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
1219 IAPuts("* STATUS ");
1220 IPutStr(imaproomname, len);
1221 IAPrintf(" (MESSAGES %d ", msgs);
1222 IAPrintf("RECENT %d ", new); /* Initially, new==recent */
1223 IAPrintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
1224 IAPrintf("UNSEEN %d)\r\n", new);
1227 * If another folder is selected, go back to that room so we can resume
1228 * our happy day without violent explosions.
1230 if (IMAP->selected) {
1231 CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
1235 * Oooh, look, we're done!
1237 IReply("OK STATUS completed");
1242 * Implements the SUBSCRIBE command
1245 void imap_subscribe(int num_parms, ConstStr *Params)
1248 char roomname[ROOMNAMELEN];
1249 char savedroom[ROOMNAMELEN];
1252 ret = imap_grabroom(roomname, Params[2].Key, 1);
1255 "NO Error %d: invalid mailbox name or location, or access denied",
1262 * CtdlUserGoto() formally takes us to the desired room, which has the side
1263 * effect of marking the room as not-zapped ... exactly the effect
1264 * we're looking for.
1266 if (IMAP->selected) {
1267 strcpy(savedroom, CC->room.QRname);
1269 CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
1272 * If another folder is selected, go back to that room so we can resume
1273 * our happy day without violent explosions.
1275 if (IMAP->selected) {
1276 CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
1279 IReply("OK SUBSCRIBE completed");
1284 * Implements the UNSUBSCRIBE command
1287 void imap_unsubscribe(int num_parms, ConstStr *Params)
1290 char roomname[ROOMNAMELEN];
1291 char savedroom[ROOMNAMELEN];
1294 ret = imap_grabroom(roomname, Params[2].Key, 1);
1296 IReply("NO Invalid mailbox name or location, or access denied");
1301 * CtdlUserGoto() formally takes us to the desired room.
1303 if (IMAP->selected) {
1304 strcpy(savedroom, CC->room.QRname);
1306 CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
1309 * Now make the API call to zap the room
1311 if (CtdlForgetThisRoom() == 0) {
1312 IReply("OK UNSUBSCRIBE completed");
1314 IReply("NO You may not unsubscribe from this folder.");
1318 * If another folder is selected, go back to that room so we can resume
1319 * our happy day without violent explosions.
1321 if (IMAP->selected) {
1322 CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
1328 * Implements the DELETE command
1331 void imap_delete(int num_parms, ConstStr *Params)
1334 char roomname[ROOMNAMELEN];
1335 char savedroom[ROOMNAMELEN];
1338 ret = imap_grabroom(roomname, Params[2].Key, 1);
1340 IReply("NO Invalid mailbox name, or access denied");
1345 * CtdlUserGoto() formally takes us to the desired room, happily returning
1346 * the number of messages and number of new messages. (If another
1347 * folder is selected, save its name so we can return there!!!!!)
1349 if (IMAP->selected) {
1350 strcpy(savedroom, CC->room.QRname);
1352 CtdlUserGoto(roomname, 0, 0, &msgs, &new, NULL, NULL);
1355 * Now delete the room.
1357 if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1358 CtdlScheduleRoomForDeletion(&CC->room);
1359 IReply("OK DELETE completed");
1361 IReply("NO Can't delete this folder.");
1365 * If another folder is selected, go back to that room so we can resume
1366 * our happy day without violent explosions.
1368 if (IMAP->selected) {
1369 CtdlUserGoto(savedroom, 0, 0, &msgs, &new, NULL, NULL);
1375 * Back end function for imap_rename()
1377 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1379 char foldername[SIZ];
1380 char newfoldername[SIZ];
1381 char newroomname[ROOMNAMELEN];
1383 struct irl *irlp = NULL; /* scratch pointer */
1384 irlparms *myirlparms;
1386 myirlparms = (irlparms *) data;
1387 imap_mailboxname(foldername, sizeof foldername, qrbuf);
1389 /* Rename subfolders */
1390 if ((!strncasecmp(foldername, myirlparms->oldname,
1391 myirlparms->oldnamelen)
1392 && (foldername[myirlparms->oldnamelen] == '/'))) {
1394 sprintf(newfoldername, "%s/%s",
1395 myirlparms->newname,
1396 &foldername[myirlparms->oldnamelen + 1]
1399 newfloor = imap_roomname(newroomname,
1401 newfoldername) & 0xFF;
1403 irlp = (struct irl *) malloc(sizeof(struct irl));
1404 strcpy(irlp->irl_newroom, newroomname);
1405 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1406 irlp->irl_newfloor = newfloor;
1407 irlp->next = *(myirlparms->irl);
1408 *(myirlparms->irl) = irlp;
1414 * Implements the RENAME command
1417 void imap_rename(int num_parms, ConstStr *Params)
1419 char old_room[ROOMNAMELEN];
1420 char new_room[ROOMNAMELEN];
1424 struct irl *irl = NULL; /* the list */
1425 struct irl *irlp = NULL; /* scratch pointer */
1429 if (strchr(Params[3].Key, '\\') != NULL) {
1430 IReply("NO Invalid character in folder name");
1434 imap_roomname(old_room, sizeof old_room, Params[2].Key);
1435 newr = imap_roomname(new_room, sizeof new_room, Params[3].Key);
1436 new_floor = (newr & 0xFF);
1438 r = CtdlRenameRoom(old_room, new_room, new_floor);
1440 if (r == crr_room_not_found) {
1441 IReply("NO Could not locate this folder");
1444 if (r == crr_already_exists) {
1445 IReplyPrintf("NO '%s' already exists.");
1448 if (r == crr_noneditable) {
1449 IReply("NO This folder is not editable.");
1452 if (r == crr_invalid_floor) {
1453 IReply("NO Folder root does not exist.");
1456 if (r == crr_access_denied) {
1457 IReply("NO You do not have permission to edit this folder.");
1461 IReplyPrintf("NO Rename failed - undefined error %d", r);
1465 /* If this is the INBOX, then RFC2060 says we have to just move the
1466 * contents. In a Citadel environment it's easier to rename the room
1467 * (already did that) and create a new inbox.
1469 if (!strcasecmp(Params[2].Key, "INBOX")) {
1470 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
1473 /* Otherwise, do the subfolders. Build a list of rooms to rename... */
1475 irlparms.oldname = Params[2].Key;
1476 irlparms.oldnamelen = Params[2].len;
1477 irlparms.newname = Params[3].Key;
1478 irlparms.newnamelen = Params[3].len;
1479 irlparms.irl = &irl;
1480 CtdlForEachRoom(imap_rename_backend, (void *) &irlparms);
1482 /* ... and now rename them. */
1483 while (irl != NULL) {
1484 r = CtdlRenameRoom(irl->irl_oldroom,
1488 struct CitContext *CCC = CC;
1489 /* FIXME handle error returns better */
1490 IMAP_syslog(LOG_ERR, "CtdlRenameRoom() error %d", r);
1498 snprintf(aidemsg, sizeof aidemsg, "IMAP folder \"%s\" renamed to \"%s\" by %s\n",
1503 CtdlAideMessage(aidemsg, "IMAP folder rename");
1505 IReply("OK RENAME completed");
1510 * Main command loop for IMAP sessions.
1512 void imap_command_loop(void)
1514 struct CitContext *CCC = CC;
1515 struct timeval tv1, tv2;
1516 suseconds_t total_time = 0;
1518 const char *pchs, *pche;
1519 const imap_handler_hook *h;
1521 gettimeofday(&tv1, NULL);
1522 CCC->lastcmd = time(NULL);
1526 if (Imap->Cmd.CmdBuf == NULL)
1527 Imap->Cmd.CmdBuf = NewStrBufPlain(NULL, SIZ);
1529 FlushStrBuf(Imap->Cmd.CmdBuf);
1531 if (CtdlClientGetLine(Imap->Cmd.CmdBuf) < 1) {
1532 IMAPM_syslog(LOG_ERR, "client disconnected: ending session.");
1533 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
1537 if (Imap->authstate == imap_as_expecting_password) {
1538 IMAPM_syslog(LOG_INFO, "<password>");
1540 else if (Imap->authstate == imap_as_expecting_plainauth) {
1541 IMAPM_syslog(LOG_INFO, "<plain_auth>");
1543 else if ((Imap->authstate == imap_as_expecting_multilineusername) ||
1544 cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
1545 IMAPM_syslog(LOG_INFO, "LOGIN...");
1548 IMAP_syslog(LOG_DEBUG, "%s", ChrPtr(Imap->Cmd.CmdBuf));
1551 pchs = ChrPtr(Imap->Cmd.CmdBuf);
1552 pche = pchs + StrLength(Imap->Cmd.CmdBuf);
1554 while ((pche > pchs) &&
1559 StrBufCutRight(Imap->Cmd.CmdBuf, 1);
1561 StrBufTrim(Imap->Cmd.CmdBuf);
1563 /* If we're in the middle of a multi-line command, handle that */
1564 switch (Imap->authstate){
1565 case imap_as_expecting_username:
1566 imap_auth_login_user(imap_as_expecting_username);
1569 case imap_as_expecting_multilineusername:
1570 imap_auth_login_user(imap_as_expecting_multilineusername);
1573 case imap_as_expecting_plainauth:
1577 case imap_as_expecting_password:
1578 imap_auth_login_pass(imap_as_expecting_password);
1581 case imap_as_expecting_multilinepassword:
1582 imap_auth_login_pass(imap_as_expecting_multilinepassword);
1589 /* Ok, at this point we're in normal command mode.
1590 * If the command just submitted does not contain a literal, we
1591 * might think about delivering some untagged stuff...
1594 /* Grab the tag, command, and parameters. */
1595 imap_parameterize(&Imap->Cmd);
1597 /* debug output the parsed vector */
1600 IMAP_syslog(LOG_DEBUG, "----- %ld params", Imap->Cmd.num_parms);
1602 for (i=0; i < Imap->Cmd.num_parms; i++) {
1603 if (Imap->Cmd.Params[i].len != strlen(Imap->Cmd.Params[i].Key))
1604 IMAP_syslog(LOG_DEBUG, "*********** %ld != %ld : %s",
1605 Imap->Cmd.Params[i].len,
1606 strlen(Imap->Cmd.Params[i].Key),
1607 Imap->Cmd.Params[i].Key);
1609 IMAP_syslog(LOG_DEBUG, "%ld : %s",
1610 Imap->Cmd.Params[i].len,
1611 Imap->Cmd.Params[i].Key);
1615 /* Now for the command set. */
1616 h = imap_lookup(Imap->Cmd.num_parms, Imap->Cmd.Params);
1620 IReply("BAD command unrecognized");
1624 /* RFC3501 says that we cannot output untagged data during these commands */
1625 if ((h->Flags & I_FLAG_UNTAGGED) == 0) {
1627 /* we can put any additional untagged stuff right here in the future */
1630 * Before processing the command that was just entered... if we happen
1631 * to have a folder selected, we'd like to rescan that folder for new
1632 * messages, and for deletions/changes of existing messages. This
1633 * could probably be optimized better with some deep thought...
1635 if (Imap->selected) {
1636 imap_rescan_msgids();
1640 /* does our command require a logged-in state */
1641 if ((!CC->logged_in) && ((h->Flags & I_FLAG_LOGGED_IN) != 0)) {
1642 IReply("BAD Not logged in.");
1646 /* does our command require the SELECT state on a mailbox */
1647 if ((Imap->selected == 0) && ((h->Flags & I_FLAG_SELECT) != 0)){
1648 IReply("BAD no folder selected");
1651 h->h(Imap->Cmd.num_parms, Imap->Cmd.Params);
1653 /* If the client transmitted a message we can free it now */
1658 imap_free_transmitted_message();
1660 gettimeofday(&tv2, NULL);
1661 total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
1662 IMAP_syslog(LOG_DEBUG, "IMAP command completed in %ld.%ld seconds",
1663 (total_time / 1000000),
1664 (total_time % 1000000)
1668 void imap_noop (int num_parms, ConstStr *Params)
1670 IReply("OK No operation");
1673 void imap_logout(int num_parms, ConstStr *Params)
1675 if (IMAP->selected) {
1676 imap_do_expunge(); /* yes, we auto-expunge at logout */
1678 IAPrintf("* BYE %s logging out\r\n", config.c_fqdn);
1679 IReply("OK Citadel IMAP session ended.");
1680 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
1684 const char *CitadelServiceIMAP="IMAP";
1685 const char *CitadelServiceIMAPS="IMAPS";
1687 void SetIMAPDebugEnabled(const int n)
1689 IMAPDebugEnabled = n;
1692 * This function is called to register the IMAP extension with Citadel.
1694 CTDL_MODULE_INIT(imap)
1696 if (ImapCmds == NULL)
1697 ImapCmds = NewHash(1, NULL);
1699 RegisterImapCMD("NOOP", "", imap_noop, I_FLAG_NONE);
1700 RegisterImapCMD("CHECK", "", imap_noop, I_FLAG_NONE);
1701 RegisterImapCMD("ID", "", imap_id, I_FLAG_NONE);
1702 RegisterImapCMD("LOGOUT", "", imap_logout, I_FLAG_NONE);
1703 RegisterImapCMD("LOGIN", "", imap_login, I_FLAG_NONE);
1704 RegisterImapCMD("AUTHENTICATE", "", imap_authenticate, I_FLAG_NONE);
1705 RegisterImapCMD("CAPABILITY", "", imap_capability, I_FLAG_NONE);
1707 RegisterImapCMD("STARTTLS", "", imap_starttls, I_FLAG_NONE);
1710 /* The commans below require a logged-in state */
1711 RegisterImapCMD("SELECT", "", imap_select, I_FLAG_LOGGED_IN);
1712 RegisterImapCMD("EXAMINE", "", imap_select, I_FLAG_LOGGED_IN);
1713 RegisterImapCMD("LSUB", "", imap_list, I_FLAG_LOGGED_IN);
1714 RegisterImapCMD("LIST", "", imap_list, I_FLAG_LOGGED_IN);
1715 RegisterImapCMD("CREATE", "", imap_create, I_FLAG_LOGGED_IN);
1716 RegisterImapCMD("DELETE", "", imap_delete, I_FLAG_LOGGED_IN);
1717 RegisterImapCMD("RENAME", "", imap_rename, I_FLAG_LOGGED_IN);
1718 RegisterImapCMD("STATUS", "", imap_status, I_FLAG_LOGGED_IN);
1719 RegisterImapCMD("SUBSCRIBE", "", imap_subscribe, I_FLAG_LOGGED_IN);
1720 RegisterImapCMD("UNSUBSCRIBE", "", imap_unsubscribe, I_FLAG_LOGGED_IN);
1721 RegisterImapCMD("APPEND", "", imap_append, I_FLAG_LOGGED_IN);
1722 RegisterImapCMD("NAMESPACE", "", imap_namespace, I_FLAG_LOGGED_IN);
1723 RegisterImapCMD("SETACL", "", imap_setacl, I_FLAG_LOGGED_IN);
1724 RegisterImapCMD("DELETEACL", "", imap_deleteacl, I_FLAG_LOGGED_IN);
1725 RegisterImapCMD("GETACL", "", imap_getacl, I_FLAG_LOGGED_IN);
1726 RegisterImapCMD("LISTRIGHTS", "", imap_listrights, I_FLAG_LOGGED_IN);
1727 RegisterImapCMD("MYRIGHTS", "", imap_myrights, I_FLAG_LOGGED_IN);
1728 RegisterImapCMD("GETMETADATA", "", imap_getmetadata, I_FLAG_LOGGED_IN);
1729 RegisterImapCMD("SETMETADATA", "", imap_setmetadata, I_FLAG_LOGGED_IN);
1731 /* The commands below require the SELECT state on a mailbox */
1732 RegisterImapCMD("FETCH", "", imap_fetch, I_FLAG_LOGGED_IN | I_FLAG_SELECT | I_FLAG_UNTAGGED);
1733 RegisterImapCMD("UID", "FETCH", imap_uidfetch, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1734 RegisterImapCMD("SEARCH", "", imap_search, I_FLAG_LOGGED_IN | I_FLAG_SELECT | I_FLAG_UNTAGGED);
1735 RegisterImapCMD("UID", "SEARCH", imap_uidsearch, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1736 RegisterImapCMD("STORE", "", imap_store, I_FLAG_LOGGED_IN | I_FLAG_SELECT | I_FLAG_UNTAGGED);
1737 RegisterImapCMD("UID", "STORE", imap_uidstore, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1738 RegisterImapCMD("COPY", "", imap_copy, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1739 RegisterImapCMD("UID", "COPY", imap_uidcopy, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1740 RegisterImapCMD("EXPUNGE", "", imap_expunge, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1741 RegisterImapCMD("UID", "EXPUNGE", imap_expunge, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1742 RegisterImapCMD("CLOSE", "", imap_close, I_FLAG_LOGGED_IN | I_FLAG_SELECT);
1746 CtdlRegisterDebugFlagHook(HKEY("imapsrv"), SetIMAPDebugEnabled, &IMAPDebugEnabled);
1747 CtdlRegisterServiceHook(config.c_imap_port,
1748 NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP);
1750 CtdlRegisterServiceHook(config.c_imaps_port,
1751 NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS);
1753 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP, PRIO_STOP + 30);
1754 CtdlRegisterCleanupHook(imap_cleanup);
1757 /* return our module name for the log */