d649c3cfae09bbd738452da4599f416f07706c00
[citadel.git] / citadel / modules / imap / serv_imap.c
1 /*
2  * $Id$ 
3  *
4  * IMAP server for the Citadel system
5  * Copyright (C) 2000-2007 by Art Cancro and others.
6  * This code is released under the terms of the GNU General Public License.
7  *
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.
11  */
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
25 # include <time.h>
26 #else
27 # if HAVE_SYS_TIME_H
28 #  include <sys/time.h>
29 # else
30 #  include <time.h>
31 # endif
32 #endif
33
34 #include <sys/wait.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <limits.h>
38 #include <libcitadel.h>
39 #include "citadel.h"
40 #include "server.h"
41 #include "citserver.h"
42 #include "support.h"
43 #include "config.h"
44 #include "room_ops.h"
45 #include "user_ops.h"
46 #include "policy.h"
47 #include "database.h"
48 #include "msgbase.h"
49 #include "internet_addressing.h"
50 #include "serv_imap.h"
51 #include "imap_tools.h"
52 #include "imap_list.h"
53 #include "imap_fetch.h"
54 #include "imap_search.h"
55 #include "imap_store.h"
56 #include "imap_acl.h"
57 #include "imap_metadata.h"
58 #include "imap_misc.h"
59
60
61 #include "ctdl_module.h"
62
63
64 /* imap_rename() uses this struct containing list of rooms to rename */
65 struct irl {
66         struct irl *next;
67         char irl_oldroom[ROOMNAMELEN];
68         char irl_newroom[ROOMNAMELEN];
69         int irl_newfloor;
70 };
71
72 /* Data which is passed between imap_rename() and imap_rename_backend() */
73 struct irlparms {
74         char *oldname;
75         char *newname;
76         struct irl **irl;
77 };
78
79
80 /*
81  * If there is a message ID map in memory, free it
82  */
83 void imap_free_msgids(void)
84 {
85         if (IMAP->msgids != NULL) {
86                 free(IMAP->msgids);
87                 IMAP->msgids = NULL;
88                 IMAP->num_msgs = 0;
89                 IMAP->num_alloc = 0;
90         }
91         if (IMAP->flags != NULL) {
92                 free(IMAP->flags);
93                 IMAP->flags = NULL;
94         }
95         IMAP->last_mtime = (-1);
96 }
97
98
99 /*
100  * If there is a transmitted message in memory, free it
101  */
102 void imap_free_transmitted_message(void)
103 {
104         if (IMAP->transmitted_message != NULL) {
105                 free(IMAP->transmitted_message);
106                 IMAP->transmitted_message = NULL;
107                 IMAP->transmitted_length = 0;
108         }
109 }
110
111
112 /*
113  * Set the \Seen, \Recent. and \Answered flags, based on the sequence
114  * sets stored in the visit record for this user/room.  Note that we have
115  * to parse each sequence set manually here, because calling the utility
116  * function is_msg_in_sequence_set() over and over again is too expensive.
117  *
118  * first_msg should be set to 0 to rescan the flags for every message in the
119  * room, or some other value if we're only interested in an incremental
120  * update.
121  */
122 void imap_set_seen_flags(int first_msg)
123 {
124         struct visit vbuf;
125         int i;
126         int num_sets;
127         int s;
128         char setstr[64], lostr[64], histr[64];
129         long lo, hi;
130
131         if (IMAP->num_msgs < 1) return;
132         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
133
134         for (i = first_msg; i < IMAP->num_msgs; ++i) {
135                 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SEEN;
136                 IMAP->flags[i] |= IMAP_RECENT;
137                 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_ANSWERED;
138         }
139
140         /*
141          * Do the "\Seen" flag.
142          * (Any message not "\Seen" is considered "\Recent".)
143          */
144         num_sets = num_tokens(vbuf.v_seen, ',');
145         for (s=0; s<num_sets; ++s) {
146                 extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
147
148                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
149                 if (num_tokens(setstr, ':') >= 2) {
150                         extract_token(histr, setstr, 1, ':', sizeof histr);
151                         if (!strcmp(histr, "*")) {
152                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
153                         }
154                 } 
155                 else {
156                         strcpy(histr, lostr);
157                 }
158                 lo = atol(lostr);
159                 hi = atol(histr);
160
161                 for (i = first_msg; i < IMAP->num_msgs; ++i) {
162                         if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
163                                 IMAP->flags[i] |= IMAP_SEEN;
164                                 IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
165                         }
166                 }
167         }
168
169         /* Do the ANSWERED flag */
170         num_sets = num_tokens(vbuf.v_answered, ',');
171         for (s=0; s<num_sets; ++s) {
172                 extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
173
174                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
175                 if (num_tokens(setstr, ':') >= 2) {
176                         extract_token(histr, setstr, 1, ':', sizeof histr);
177                         if (!strcmp(histr, "*")) {
178                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
179                         }
180                 } 
181                 else {
182                         strcpy(histr, lostr);
183                 }
184                 lo = atol(lostr);
185                 hi = atol(histr);
186
187                 for (i = first_msg; i < IMAP->num_msgs; ++i) {
188                         if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)){
189                                 IMAP->flags[i] |= IMAP_ANSWERED;
190                         }
191                 }
192         }
193
194 }
195
196
197
198 /*
199  * Back end for imap_load_msgids()
200  *
201  * Optimization: instead of calling realloc() to add each message, we
202  * allocate space in the list for REALLOC_INCREMENT messages at a time.  This
203  * allows the mapping to proceed much faster.
204  */
205 void imap_add_single_msgid(long msgnum, void *userdata)
206 {
207
208         ++IMAP->num_msgs;
209         if (IMAP->num_msgs > IMAP->num_alloc) {
210                 IMAP->num_alloc += REALLOC_INCREMENT;
211                 IMAP->msgids = realloc(IMAP->msgids,
212                                         (IMAP->num_alloc * sizeof(long)) );
213                 IMAP->flags = realloc(IMAP->flags,
214                                         (IMAP->num_alloc * sizeof(long)) );
215         }
216         IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
217         IMAP->flags[IMAP->num_msgs - 1] = 0;
218 }
219
220
221
222 /*
223  * Set up a message ID map for the current room (folder)
224  */
225 void imap_load_msgids(void)
226 {
227         struct cdbdata *cdbfr;
228
229         if (IMAP->selected == 0) {
230                 CtdlLogPrintf(CTDL_ERR,
231                         "imap_load_msgids() can't run; no room selected\n");
232                 return;
233         }
234
235         imap_free_msgids();     /* If there was already a map, free it */
236
237         /* Load the message list */
238         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
239         if (cdbfr != NULL) {
240                 IMAP->msgids = malloc(cdbfr->len);
241                 memcpy(IMAP->msgids, cdbfr->ptr, cdbfr->len);
242                 IMAP->num_msgs = cdbfr->len / sizeof(long);
243                 IMAP->num_alloc = cdbfr->len / sizeof(long);
244                 cdb_free(cdbfr);
245         }
246
247         if (IMAP->num_msgs) {
248                 IMAP->flags = malloc(IMAP->num_alloc * sizeof(long));
249                 memset(IMAP->flags, 0, (IMAP->num_alloc * sizeof(long)) );
250         }
251
252         imap_set_seen_flags(0);
253 }
254
255
256 /*
257  * Re-scan the selected room (folder) and see if it's been changed at all
258  */
259 void imap_rescan_msgids(void)
260 {
261
262         int original_num_msgs = 0;
263         long original_highest = 0L;
264         int i, j, jstart;
265         int message_still_exists;
266         struct cdbdata *cdbfr;
267         long *msglist = NULL;
268         int num_msgs = 0;
269         int num_recent = 0;
270
271         if (IMAP->selected == 0) {
272                 CtdlLogPrintf(CTDL_ERR,
273                         "imap_load_msgids() can't run; no room selected\n");
274                 return;
275         }
276
277         /*
278          * Check to see if the room's contents have changed.
279          * If not, we can avoid this rescan.
280          */
281         getroom(&CC->room, CC->room.QRname);
282         if (IMAP->last_mtime == CC->room.QRmtime) {     /* No changes! */
283                 return;
284         }
285
286         /* Load the *current* message list from disk, so we can compare it
287          * to what we have in memory.
288          */
289         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
290         if (cdbfr != NULL) {
291                 msglist = malloc(cdbfr->len);
292                 if (msglist == NULL) {
293                         CtdlLogPrintf(CTDL_CRIT, "malloc() failed\n");
294                         abort();
295                 }
296                 memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
297                 num_msgs = cdbfr->len / sizeof(long);
298                 cdb_free(cdbfr);
299         } else {
300                 num_msgs = 0;
301         }
302
303         /*
304          * Check to see if any of the messages we know about have been expunged
305          */
306         if (IMAP->num_msgs > 0) {
307                 jstart = 0;
308                 for (i = 0; i < IMAP->num_msgs; ++i) {
309
310                         message_still_exists = 0;
311                         if (num_msgs > 0) {
312                                 for (j = jstart; j < num_msgs; ++j) {
313                                         if (msglist[j] == IMAP->msgids[i]) {
314                                                 message_still_exists = 1;
315                                                 jstart = j;
316                                                 break;
317                                         }
318                                 }
319                         }
320
321                         if (message_still_exists == 0) {
322                                 cprintf("* %d EXPUNGE\r\n", i + 1);
323
324                                 /* Here's some nice stupid nonsense.  When a
325                                  * message is expunged, we have to slide all
326                                  * the existing messages up in the message
327                                  * array.
328                                  */
329                                 --IMAP->num_msgs;
330                                 memcpy(&IMAP->msgids[i],
331                                        &IMAP->msgids[i + 1],
332                                        (sizeof(long) *
333                                         (IMAP->num_msgs - i)));
334                                 memcpy(&IMAP->flags[i],
335                                        &IMAP->flags[i + 1],
336                                        (sizeof(long) *
337                                         (IMAP->num_msgs - i)));
338
339                                 --i;
340                         }
341
342                 }
343         }
344
345         /*
346          * Remember how many messages were here before we re-scanned.
347          */
348         original_num_msgs = IMAP->num_msgs;
349         if (IMAP->num_msgs > 0) {
350                 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
351         } else {
352                 original_highest = 0L;
353         }
354
355         /*
356          * Now peruse the room for *new* messages only.
357          */
358         if (num_msgs > 0) {
359                 for (j = 0; j < num_msgs; ++j) {
360                         if (msglist[j] > original_highest) {
361                                 imap_add_single_msgid(msglist[j], NULL);
362                         }
363                 }
364         }
365         imap_set_seen_flags(original_num_msgs);
366
367         /*
368          * If new messages have arrived, tell the client about them.
369          */
370         if (IMAP->num_msgs > original_num_msgs) {
371
372                 for (j = 0; j < num_msgs; ++j) {
373                         if (IMAP->flags[j] & IMAP_RECENT) {
374                                 ++num_recent;
375                         }
376                 }
377
378                 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
379                 cprintf("* %d RECENT\r\n", num_recent);
380         }
381
382         if (num_msgs != 0) {
383                 free(msglist);
384         }
385         IMAP->last_mtime = CC->room.QRmtime;
386 }
387
388
389
390
391
392
393
394 /*
395  * This cleanup function blows away the temporary memory and files used by
396  * the IMAP server.
397  */
398 void imap_cleanup_function(void)
399 {
400
401         /* Don't do this stuff if this is not a IMAP session! */
402         if (CC->h_command_function != imap_command_loop)
403                 return;
404
405         /* If there is a mailbox selected, auto-expunge it. */
406         if (IMAP->selected) {
407                 imap_do_expunge();
408         }
409
410         CtdlLogPrintf(CTDL_DEBUG, "Performing IMAP cleanup hook\n");
411         imap_free_msgids();
412         imap_free_transmitted_message();
413
414         if (IMAP->cached_rfc822_data != NULL) {
415                 free(IMAP->cached_rfc822_data);
416                 IMAP->cached_rfc822_data = NULL;
417                 IMAP->cached_rfc822_msgnum = (-1);
418                 IMAP->cached_rfc822_withbody = 0;
419         }
420
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);
426         }
427
428         free(IMAP);
429         CtdlLogPrintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
430 }
431
432
433 /*
434  * Does the actual work of the CAPABILITY command (because we need to
435  * output this stuff in other places as well)
436  */
437 void imap_output_capability_string(void) {
438         cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN UIDPLUS");
439
440 #ifdef HAVE_OPENSSL
441         if (!CC->redirect_ssl) cprintf(" STARTTLS");
442 #endif
443
444 #ifndef DISABLE_IMAP_ACL
445         cprintf(" ACL");
446 #endif
447
448         /* We are building a partial implementation of METADATA for the sole purpose
449          * of interoperating with the ical/vcard version of the Bynari Insight Connector.
450          * It is not a full RFC5464 implementation, but it should refuse non-Bynari
451          * metadata in a compatible and graceful way.
452          */
453         cprintf(" METADATA");
454
455         /*
456          * LIST-EXTENDED was originally going to be required by the METADATA extension.
457          * It was mercifully removed prior to the finalization of RFC5464.  We started
458          * implementing this but stopped when we learned that it would not be needed.
459          * If you uncomment this declaration you are responsible for writing a lot of new
460          * code.
461          *
462          * cprintf(" LIST-EXTENDED")
463          */
464 }
465
466 /*
467  * implements the CAPABILITY command
468  */
469 void imap_capability(int num_parms, char *parms[])
470 {
471         cprintf("* ");
472         imap_output_capability_string();
473         cprintf("\r\n");
474         cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
475 }
476
477
478
479 /*
480  * Implements the ID command (specified by RFC2971)
481  *
482  * We ignore the client-supplied information, and output a NIL response.
483  * Although this is technically a valid implementation of the extension, it
484  * is quite useless.  It exists only so that we may see which clients are
485  * making use of this extension.
486  * 
487  */
488 void imap_id(int num_parms, char *parms[])
489 {
490         cprintf("* ID NIL\r\n");
491         cprintf("%s OK ID completed\r\n", parms[0]);
492 }
493
494
495
496 /*
497  * Here's where our IMAP session begins its happy day.
498  */
499 void imap_greeting(void)
500 {
501
502         strcpy(CC->cs_clientname, "IMAP session");
503         CC->session_specific_data = malloc(sizeof(struct citimap));
504         memset(IMAP, 0, sizeof(struct citimap));
505         IMAP->authstate = imap_as_normal;
506         IMAP->cached_rfc822_data = NULL;
507         IMAP->cached_rfc822_msgnum = (-1);
508         IMAP->cached_rfc822_withbody = 0;
509
510         if (CC->nologin)
511         {
512                 cprintf("* BYE; Server busy, try later\r\n");
513                 CC->kill_me = 1;
514                 return;
515         }
516         cprintf("* OK [");
517         imap_output_capability_string();
518         cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
519 }
520
521 /*
522  * IMAPS is just like IMAP, except it goes crypto right away.
523  */
524 void imaps_greeting(void) {
525         CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
526 #ifdef HAVE_OPENSSL
527         if (!CC->redirect_ssl) CC->kill_me = 1;         /* kill session if no crypto */
528 #endif
529         imap_greeting();
530 }
531
532
533 /*
534  * implements the LOGIN command (ordinary username/password login)
535  */
536 void imap_login(int num_parms, char *parms[])
537 {
538         if (num_parms != 4) {
539                 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
540                 return;
541         }
542
543         if (CtdlLoginExistingUser(NULL, parms[2]) == login_ok) {
544                 if (CtdlTryPassword(parms[3]) == pass_ok) {
545                         cprintf("%s OK [", parms[0]);
546                         imap_output_capability_string();
547                         cprintf("] Hello, %s\r\n", CC->user.fullname);
548                         return;
549                 }
550         }
551
552         cprintf("%s BAD Login incorrect\r\n", parms[0]);
553 }
554
555
556 /*
557  * Implements the AUTHENTICATE command
558  */
559 void imap_authenticate(int num_parms, char *parms[])
560 {
561         char buf[SIZ];
562
563         if (num_parms != 3) {
564                 cprintf("%s BAD incorrect number of parameters\r\n",
565                         parms[0]);
566                 return;
567         }
568
569         if (CC->logged_in) {
570                 cprintf("%s BAD Already logged in.\r\n", parms[0]);
571                 return;
572         }
573
574         if (!strcasecmp(parms[2], "LOGIN")) {
575                 CtdlEncodeBase64(buf, "Username:", 9, 0);
576                 cprintf("+ %s\r\n", buf);
577                 IMAP->authstate = imap_as_expecting_username;
578                 strcpy(IMAP->authseq, parms[0]);
579                 return;
580         }
581
582         if (!strcasecmp(parms[2], "PLAIN")) {
583                 // CtdlEncodeBase64(buf, "Username:", 9, 0);
584                 // cprintf("+ %s\r\n", buf);
585                 cprintf("+ \r\n");
586                 IMAP->authstate = imap_as_expecting_plainauth;
587                 strcpy(IMAP->authseq, parms[0]);
588                 return;
589         }
590
591         else {
592                 cprintf("%s NO AUTHENTICATE %s failed\r\n",
593                         parms[0], parms[1]);
594         }
595 }
596
597 void imap_auth_plain(char *cmd)
598 {
599         char decoded_authstring[1024];
600         char ident[256];
601         char user[256];
602         char pass[256];
603         int result;
604
605         CtdlDecodeBase64(decoded_authstring, cmd, strlen(cmd));
606         safestrncpy(ident, decoded_authstring, sizeof ident);
607         safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
608         safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
609
610         IMAP->authstate = imap_as_normal;
611
612         if (!IsEmptyStr(ident)) {
613                 result = CtdlLoginExistingUser(user, ident);
614         }
615         else {
616                 result = CtdlLoginExistingUser(NULL, user);
617         }
618
619         if (result == login_ok) {
620                 if (CtdlTryPassword(pass) == pass_ok) {
621                         cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
622                         return;
623                 }
624         }
625         cprintf("%s NO authentication failed\r\n", IMAP->authseq);
626 }
627
628 void imap_auth_login_user(char *cmd)
629 {
630         char buf[SIZ];
631
632         CtdlDecodeBase64(buf, cmd, SIZ);
633         CtdlLoginExistingUser(NULL, buf);
634         CtdlEncodeBase64(buf, "Password:", 9, 0);
635         cprintf("+ %s\r\n", buf);
636         IMAP->authstate = imap_as_expecting_password;
637         return;
638 }
639
640 void imap_auth_login_pass(char *cmd)
641 {
642         char buf[SIZ];
643
644         CtdlDecodeBase64(buf, cmd, SIZ);
645         if (CtdlTryPassword(buf) == pass_ok) {
646                 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
647         } else {
648                 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
649         }
650         IMAP->authstate = imap_as_normal;
651         return;
652 }
653
654
655 /*
656  * implements the STARTTLS command (Citadel API version)
657  */
658 void imap_starttls(int num_parms, char *parms[])
659 {
660         char ok_response[SIZ];
661         char nosup_response[SIZ];
662         char error_response[SIZ];
663
664         sprintf(ok_response,
665                 "%s OK begin TLS negotiation now\r\n",
666                 parms[0]);
667         sprintf(nosup_response,
668                 "%s NO TLS not supported here\r\n",
669                 parms[0]);
670         sprintf(error_response,
671                 "%s BAD Internal error\r\n",
672                 parms[0]);
673         CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
674 }
675
676
677 /*
678  * implements the SELECT command
679  */
680 void imap_select(int num_parms, char *parms[])
681 {
682         char towhere[SIZ];
683         char augmented_roomname[ROOMNAMELEN];
684         int c = 0;
685         int ok = 0;
686         int ra = 0;
687         struct ctdlroom QRscratch;
688         int msgs, new;
689         int floornum;
690         int roomflags;
691         int i;
692
693         /* Convert the supplied folder name to a roomname */
694         i = imap_roomname(towhere, sizeof towhere, parms[2]);
695         if (i < 0) {
696                 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
697                 IMAP->selected = 0;
698                 return;
699         }
700         floornum = (i & 0x00ff);
701         roomflags = (i & 0xff00);
702
703         /* First try a regular match */
704         c = getroom(&QRscratch, towhere);
705
706         /* Then try a mailbox name match */
707         if (c != 0) {
708                 MailboxName(augmented_roomname, sizeof augmented_roomname,
709                             &CC->user, towhere);
710                 c = getroom(&QRscratch, augmented_roomname);
711                 if (c == 0)
712                         strcpy(towhere, augmented_roomname);
713         }
714
715         /* If the room exists, check security/access */
716         if (c == 0) {
717                 /* See if there is an existing user/room relationship */
718                 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
719
720                 /* normal clients have to pass through security */
721                 if (ra & UA_KNOWN) {
722                         ok = 1;
723                 }
724         }
725
726         /* Fail here if no such room */
727         if (!ok) {
728                 cprintf("%s NO ... no such room, or access denied\r\n",
729                         parms[0]);
730                 return;
731         }
732
733         /* If we already had some other folder selected, auto-expunge it */
734         imap_do_expunge();
735
736         /*
737          * usergoto() formally takes us to the desired room, happily returning
738          * the number of messages and number of new messages.
739          */
740         memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
741         usergoto(NULL, 0, 0, &msgs, &new);
742         IMAP->selected = 1;
743
744         if (!strcasecmp(parms[1], "EXAMINE")) {
745                 IMAP->readonly = 1;
746         } else {
747                 IMAP->readonly = 0;
748         }
749
750         imap_load_msgids();
751         IMAP->last_mtime = CC->room.QRmtime;
752
753         cprintf("* %d EXISTS\r\n", msgs);
754         cprintf("* %d RECENT\r\n", new);
755
756         cprintf("* OK [UIDVALIDITY %ld] UID validity status\r\n", GLOBAL_UIDVALIDITY_VALUE);
757         cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
758
759         /* Technically, \Deleted is a valid flag, but not a permanent flag,
760          * because we don't maintain its state across sessions.  Citadel
761          * automatically expunges mailboxes when they are de-selected.
762          * 
763          * Unfortunately, omitting \Deleted as a PERMANENTFLAGS flag causes
764          * some clients (particularly Thunderbird) to misbehave -- they simply
765          * elect not to transmit the flag at all.  So we have to advertise
766          * \Deleted as a PERMANENTFLAGS flag, even though it technically isn't.
767          */
768         cprintf("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
769         cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n");
770
771         cprintf("%s OK [%s] %s completed\r\n",
772                 parms[0],
773                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
774 }
775
776
777
778 /*
779  * Does the real work for expunge.
780  */
781 int imap_do_expunge(void)
782 {
783         int i;
784         int num_expunged = 0;
785         long *delmsgs = NULL;
786         int num_delmsgs = 0;
787
788         CtdlLogPrintf(CTDL_DEBUG, "imap_do_expunge() called\n");
789         if (IMAP->selected == 0) {
790                 return (0);
791         }
792
793         if (IMAP->num_msgs > 0) {
794                 delmsgs = malloc(IMAP->num_msgs * sizeof(long));
795                 for (i = 0; i < IMAP->num_msgs; ++i) {
796                         if (IMAP->flags[i] & IMAP_DELETED) {
797                                 delmsgs[num_delmsgs++] = IMAP->msgids[i];
798                         }
799                 }
800                 if (num_delmsgs > 0) {
801                         CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
802                 }
803                 num_expunged += num_delmsgs;
804                 free(delmsgs);
805         }
806
807         if (num_expunged > 0) {
808                 imap_rescan_msgids();
809         }
810
811         CtdlLogPrintf(CTDL_DEBUG, "Expunged %d messages from <%s>\n",
812                 num_expunged, CC->room.QRname);
813         return (num_expunged);
814 }
815
816
817 /*
818  * implements the EXPUNGE command syntax
819  */
820 void imap_expunge(int num_parms, char *parms[])
821 {
822         int num_expunged = 0;
823
824         num_expunged = imap_do_expunge();
825         cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
826 }
827
828
829 /*
830  * implements the CLOSE command
831  */
832 void imap_close(int num_parms, char *parms[])
833 {
834
835         /* Yes, we always expunge on close. */
836         if (IMAP->selected) {
837                 imap_do_expunge();
838         }
839
840         IMAP->selected = 0;
841         IMAP->readonly = 0;
842         imap_free_msgids();
843         cprintf("%s OK CLOSE completed\r\n", parms[0]);
844 }
845
846
847 /*
848  * Implements the NAMESPACE command.
849  */
850 void imap_namespace(int num_parms, char *parms[])
851 {
852         int i;
853         struct floor *fl;
854         int floors = 0;
855         char buf[SIZ];
856
857         cprintf("* NAMESPACE ");
858
859         /* All personal folders are subordinate to INBOX. */
860         cprintf("((\"INBOX/\" \"/\")) ");
861
862         /* Other users' folders ... coming soon! FIXME */
863         cprintf("NIL ");
864
865         /* Show all floors as shared namespaces.  Neato! */
866         cprintf("(");
867         for (i = 0; i < MAXFLOORS; ++i) {
868                 fl = cgetfloor(i);
869                 if (fl->f_flags & F_INUSE) {
870                         if (floors > 0) cprintf(" ");
871                         cprintf("(");
872                         sprintf(buf, "%s/", fl->f_name);
873                         imap_strout(buf);
874                         cprintf(" \"/\")");
875                         ++floors;
876                 }
877         }
878         cprintf(")");
879
880         /* Wind it up with a newline and a completion message. */
881         cprintf("\r\n");
882         cprintf("%s OK NAMESPACE completed\r\n", parms[0]);
883 }
884
885
886
887 /*
888  * Implements the CREATE command
889  *
890  */
891 void imap_create(int num_parms, char *parms[])
892 {
893         int ret;
894         char roomname[ROOMNAMELEN];
895         int floornum;
896         int flags;
897         int newroomtype = 0;
898         int newroomview = 0;
899         char *notification_message = NULL;
900
901         if (strchr(parms[2], '\\') != NULL) {
902                 cprintf("%s NO Invalid character in folder name\r\n",
903                         parms[0]);
904                 CtdlLogPrintf(CTDL_DEBUG, "invalid character in folder name\n");
905                 return;
906         }
907
908         ret = imap_roomname(roomname, sizeof roomname, parms[2]);
909         if (ret < 0) {
910                 cprintf("%s NO Invalid mailbox name or location\r\n",
911                         parms[0]);
912                 CtdlLogPrintf(CTDL_DEBUG, "invalid mailbox name or location\n");
913                 return;
914         }
915         floornum = (ret & 0x00ff);      /* lower 8 bits = floor number */
916         flags = (ret & 0xff00); /* upper 8 bits = flags        */
917
918         if (flags & IR_MAILBOX) {
919                 if (strncasecmp(parms[2], "INBOX/", 6)) {
920                         cprintf("%s NO Personal folders must be created under INBOX\r\n", parms[0]);
921                         CtdlLogPrintf(CTDL_DEBUG, "not subordinate to inbox\n");
922                         return;
923                 }
924         }
925
926         if (flags & IR_MAILBOX) {
927                 newroomtype = 4;                /* private mailbox */
928                 newroomview = VIEW_MAILBOX;
929         } else {
930                 newroomtype = 0;                /* public folder */
931                 newroomview = VIEW_BBS;
932         }
933
934         CtdlLogPrintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
935                 roomname, floornum, newroomtype);
936
937         ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
938         if (ret == 0) {
939                 /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY!  BYNARI CONNECTOR DEPENDS ON IT! ***/
940                 cprintf("%s NO Mailbox already exists, or create failed\r\n", parms[0]);
941         } else {
942                 cprintf("%s OK CREATE completed\r\n", parms[0]);
943                 /* post a message in Aide> describing the new room */
944                 notification_message = malloc(1024);
945                 snprintf(notification_message, 1024,
946                         "A new room called \"%s\" has been created by %s%s%s%s\n",
947                         roomname,
948                         CC->user.fullname,
949                         ((ret & QR_MAILBOX) ? " [personal]" : ""),
950                         ((ret & QR_PRIVATE) ? " [private]" : ""),
951                         ((ret & QR_GUESSNAME) ? " [hidden]" : "")
952                 );
953                 aide_message(notification_message, "Room Creation Message");
954                 free(notification_message);
955         }
956         CtdlLogPrintf(CTDL_DEBUG, "imap_create() completed\n");
957 }
958
959
960 /*
961  * Locate a room by its IMAP folder name, and check access to it.
962  * If zapped_ok is nonzero, we can also look for the room in the zapped list.
963  */
964 int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
965 {
966         int ret;
967         char augmented_roomname[ROOMNAMELEN];
968         char roomname[ROOMNAMELEN];
969         int c;
970         struct ctdlroom QRscratch;
971         int ra;
972         int ok = 0;
973
974         ret = imap_roomname(roomname, sizeof roomname, foldername);
975         if (ret < 0) {
976                 return (1);
977         }
978
979         /* First try a regular match */
980         c = getroom(&QRscratch, roomname);
981
982         /* Then try a mailbox name match */
983         if (c != 0) {
984                 MailboxName(augmented_roomname, sizeof augmented_roomname,
985                             &CC->user, roomname);
986                 c = getroom(&QRscratch, augmented_roomname);
987                 if (c == 0)
988                         strcpy(roomname, augmented_roomname);
989         }
990
991         /* If the room exists, check security/access */
992         if (c == 0) {
993                 /* See if there is an existing user/room relationship */
994                 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
995
996                 /* normal clients have to pass through security */
997                 if (ra & UA_KNOWN) {
998                         ok = 1;
999                 }
1000                 if ((zapped_ok) && (ra & UA_ZAPPED)) {
1001                         ok = 1;
1002                 }
1003         }
1004
1005         /* Fail here if no such room */
1006         if (!ok) {
1007                 strcpy(returned_roomname, "");
1008                 return (2);
1009         } else {
1010                 strcpy(returned_roomname, QRscratch.QRname);
1011                 return (0);
1012         }
1013 }
1014
1015
1016 /*
1017  * Implements the STATUS command (sort of)
1018  *
1019  */
1020 void imap_status(int num_parms, char *parms[])
1021 {
1022         int ret;
1023         char roomname[ROOMNAMELEN];
1024         char buf[SIZ];
1025         char savedroom[ROOMNAMELEN];
1026         int msgs, new;
1027
1028         ret = imap_grabroom(roomname, parms[2], 0);
1029         if (ret != 0) {
1030                 cprintf
1031                     ("%s NO Invalid mailbox name or location, or access denied\r\n",
1032                      parms[0]);
1033                 return;
1034         }
1035
1036         /*
1037          * usergoto() formally takes us to the desired room, happily returning
1038          * the number of messages and number of new messages.  (If another
1039          * folder is selected, save its name so we can return there!!!!!)
1040          */
1041         if (IMAP->selected) {
1042                 strcpy(savedroom, CC->room.QRname);
1043         }
1044         usergoto(roomname, 0, 0, &msgs, &new);
1045
1046         /*
1047          * Tell the client what it wants to know.  In fact, tell it *more* than
1048          * it wants to know.  We happily IGnore the supplied status data item
1049          * names and simply spew all possible data items.  It's far easier to
1050          * code and probably saves us some processing time too.
1051          */
1052         imap_mailboxname(buf, sizeof buf, &CC->room);
1053         cprintf("* STATUS ");
1054         imap_strout(buf);
1055         cprintf(" (MESSAGES %d ", msgs);
1056         cprintf("RECENT %d ", new);     /* Initially, new==recent */
1057         cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
1058         cprintf("UNSEEN %d)\r\n", new);
1059
1060         /*
1061          * If another folder is selected, go back to that room so we can resume
1062          * our happy day without violent explosions.
1063          */
1064         if (IMAP->selected) {
1065                 usergoto(savedroom, 0, 0, &msgs, &new);
1066         }
1067
1068         /*
1069          * Oooh, look, we're done!
1070          */
1071         cprintf("%s OK STATUS completed\r\n", parms[0]);
1072 }
1073
1074
1075
1076 /*
1077  * Implements the SUBSCRIBE command
1078  *
1079  */
1080 void imap_subscribe(int num_parms, char *parms[])
1081 {
1082         int ret;
1083         char roomname[ROOMNAMELEN];
1084         char savedroom[ROOMNAMELEN];
1085         int msgs, new;
1086
1087         ret = imap_grabroom(roomname, parms[2], 1);
1088         if (ret != 0) {
1089                 cprintf(
1090                         "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
1091                         parms[0],
1092                         ret
1093                 );
1094                 return;
1095         }
1096
1097         /*
1098          * usergoto() formally takes us to the desired room, which has the side
1099          * effect of marking the room as not-zapped ... exactly the effect
1100          * we're looking for.
1101          */
1102         if (IMAP->selected) {
1103                 strcpy(savedroom, CC->room.QRname);
1104         }
1105         usergoto(roomname, 0, 0, &msgs, &new);
1106
1107         /*
1108          * If another folder is selected, go back to that room so we can resume
1109          * our happy day without violent explosions.
1110          */
1111         if (IMAP->selected) {
1112                 usergoto(savedroom, 0, 0, &msgs, &new);
1113         }
1114
1115         cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
1116 }
1117
1118
1119 /*
1120  * Implements the UNSUBSCRIBE command
1121  *
1122  */
1123 void imap_unsubscribe(int num_parms, char *parms[])
1124 {
1125         int ret;
1126         char roomname[ROOMNAMELEN];
1127         char savedroom[ROOMNAMELEN];
1128         int msgs, new;
1129
1130         ret = imap_grabroom(roomname, parms[2], 0);
1131         if (ret != 0) {
1132                 cprintf
1133                     ("%s NO Invalid mailbox name or location, or access denied\r\n",
1134                      parms[0]);
1135                 return;
1136         }
1137
1138         /*
1139          * usergoto() formally takes us to the desired room.
1140          */
1141         if (IMAP->selected) {
1142                 strcpy(savedroom, CC->room.QRname);
1143         }
1144         usergoto(roomname, 0, 0, &msgs, &new);
1145
1146         /* 
1147          * Now make the API call to zap the room
1148          */
1149         if (CtdlForgetThisRoom() == 0) {
1150                 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
1151         } else {
1152                 cprintf
1153                     ("%s NO You may not unsubscribe from this folder.\r\n",
1154                      parms[0]);
1155         }
1156
1157         /*
1158          * If another folder is selected, go back to that room so we can resume
1159          * our happy day without violent explosions.
1160          */
1161         if (IMAP->selected) {
1162                 usergoto(savedroom, 0, 0, &msgs, &new);
1163         }
1164 }
1165
1166
1167
1168 /*
1169  * Implements the DELETE command
1170  *
1171  */
1172 void imap_delete(int num_parms, char *parms[])
1173 {
1174         int ret;
1175         char roomname[ROOMNAMELEN];
1176         char savedroom[ROOMNAMELEN];
1177         int msgs, new;
1178
1179         ret = imap_grabroom(roomname, parms[2], 1);
1180         if (ret != 0) {
1181                 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
1182                         parms[0]);
1183                 return;
1184         }
1185
1186         /*
1187          * usergoto() formally takes us to the desired room, happily returning
1188          * the number of messages and number of new messages.  (If another
1189          * folder is selected, save its name so we can return there!!!!!)
1190          */
1191         if (IMAP->selected) {
1192                 strcpy(savedroom, CC->room.QRname);
1193         }
1194         usergoto(roomname, 0, 0, &msgs, &new);
1195
1196         /*
1197          * Now delete the room.
1198          */
1199         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
1200                 schedule_room_for_deletion(&CC->room);
1201                 cprintf("%s OK DELETE completed\r\n", parms[0]);
1202         } else {
1203                 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
1204         }
1205
1206         /*
1207          * If another folder is selected, go back to that room so we can resume
1208          * our happy day without violent explosions.
1209          */
1210         if (IMAP->selected) {
1211                 usergoto(savedroom, 0, 0, &msgs, &new);
1212         }
1213 }
1214
1215
1216 /*
1217  * Back end function for imap_rename()
1218  */
1219 void imap_rename_backend(struct ctdlroom *qrbuf, void *data)
1220 {
1221         char foldername[SIZ];
1222         char newfoldername[SIZ];
1223         char newroomname[ROOMNAMELEN];
1224         int newfloor = 0;
1225         struct irl *irlp = NULL;        /* scratch pointer */
1226         struct irlparms *irlparms;
1227
1228         irlparms = (struct irlparms *) data;
1229         imap_mailboxname(foldername, sizeof foldername, qrbuf);
1230
1231         /* Rename subfolders */
1232         if ((!strncasecmp(foldername, irlparms->oldname,
1233                           strlen(irlparms->oldname))
1234              && (foldername[strlen(irlparms->oldname)] == '/'))) {
1235
1236                 sprintf(newfoldername, "%s/%s",
1237                         irlparms->newname,
1238                         &foldername[strlen(irlparms->oldname) + 1]
1239                     );
1240
1241                 newfloor = imap_roomname(newroomname,
1242                                          sizeof newroomname,
1243                                          newfoldername) & 0xFF;
1244
1245                 irlp = (struct irl *) malloc(sizeof(struct irl));
1246                 strcpy(irlp->irl_newroom, newroomname);
1247                 strcpy(irlp->irl_oldroom, qrbuf->QRname);
1248                 irlp->irl_newfloor = newfloor;
1249                 irlp->next = *(irlparms->irl);
1250                 *(irlparms->irl) = irlp;
1251         }
1252 }
1253
1254
1255 /*
1256  * Implements the RENAME command
1257  *
1258  */
1259 void imap_rename(int num_parms, char *parms[])
1260 {
1261         char old_room[ROOMNAMELEN];
1262         char new_room[ROOMNAMELEN];
1263         int oldr, newr;
1264         int new_floor;
1265         int r;
1266         struct irl *irl = NULL; /* the list */
1267         struct irl *irlp = NULL;        /* scratch pointer */
1268         struct irlparms irlparms;
1269         char buf[1024];
1270
1271         if (strchr(parms[3], '\\') != NULL) {
1272                 cprintf("%s NO Invalid character in folder name\r\n",
1273                         parms[0]);
1274                 return;
1275         }
1276
1277         oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
1278         newr = imap_roomname(new_room, sizeof new_room, parms[3]);
1279         new_floor = (newr & 0xFF);
1280
1281         r = CtdlRenameRoom(old_room, new_room, new_floor);
1282
1283         if (r == crr_room_not_found) {
1284                 cprintf("%s NO Could not locate this folder\r\n",
1285                         parms[0]);
1286                 return;
1287         }
1288         if (r == crr_already_exists) {
1289                 cprintf("%s NO '%s' already exists.\r\n", parms[0], parms[2]);
1290                 return;
1291         }
1292         if (r == crr_noneditable) {
1293                 cprintf("%s NO This folder is not editable.\r\n", parms[0]);
1294                 return;
1295         }
1296         if (r == crr_invalid_floor) {
1297                 cprintf("%s NO Folder root does not exist.\r\n", parms[0]);
1298                 return;
1299         }
1300         if (r == crr_access_denied) {
1301                 cprintf("%s NO You do not have permission to edit this folder.\r\n",
1302                         parms[0]);
1303                 return;
1304         }
1305         if (r != crr_ok) {
1306                 cprintf("%s NO Rename failed - undefined error %d\r\n",
1307                         parms[0], r);
1308                 return;
1309         }
1310
1311         /* If this is the INBOX, then RFC2060 says we have to just move the
1312          * contents.  In a Citadel environment it's easier to rename the room
1313          * (already did that) and create a new inbox.
1314          */
1315         if (!strcasecmp(parms[2], "INBOX")) {
1316                 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
1317         }
1318
1319         /* Otherwise, do the subfolders.  Build a list of rooms to rename... */
1320         else {
1321                 irlparms.oldname = parms[2];
1322                 irlparms.newname = parms[3];
1323                 irlparms.irl = &irl;
1324                 ForEachRoom(imap_rename_backend, (void *) &irlparms);
1325
1326                 /* ... and now rename them. */
1327                 while (irl != NULL) {
1328                         r = CtdlRenameRoom(irl->irl_oldroom,
1329                                            irl->irl_newroom,
1330                                            irl->irl_newfloor);
1331                         if (r != crr_ok) {
1332                                 /* FIXME handle error returns better */
1333                                 CtdlLogPrintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r);
1334                         }
1335                         irlp = irl;
1336                         irl = irl->next;
1337                         free(irlp);
1338                 }
1339         }
1340
1341         snprintf(buf, sizeof buf, "IMAP folder \"%s\" renamed to \"%s\" by %s\n",
1342                 parms[2],
1343                 parms[3],
1344                 CC->curr_user
1345         );
1346         aide_message(buf, "IMAP folder rename");
1347
1348         cprintf("%s OK RENAME completed\r\n", parms[0]);
1349 }
1350
1351
1352
1353
1354 /* 
1355  * Main command loop for IMAP sessions.
1356  */
1357 void imap_command_loop(void)
1358 {
1359         char cmdbuf[SIZ];
1360         char *parms[SIZ];
1361         int num_parms;
1362         struct timeval tv1, tv2;
1363         suseconds_t total_time = 0;
1364         int untagged_ok = 1;
1365
1366         gettimeofday(&tv1, NULL);
1367         CC->lastcmd = time(NULL);
1368         memset(cmdbuf, 0, sizeof cmdbuf);       /* Clear it, just in case */
1369         flush_output();
1370         if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1371                 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\r\n");
1372                 CC->kill_me = 1;
1373                 return;
1374         }
1375
1376         if (IMAP->authstate == imap_as_expecting_password) {
1377                 CtdlLogPrintf(CTDL_INFO, "IMAP: <password>\n");
1378         }
1379         else if (IMAP->authstate == imap_as_expecting_plainauth) {
1380                 CtdlLogPrintf(CTDL_INFO, "IMAP: <plain_auth>\n");
1381         }
1382         else if (bmstrcasestr(cmdbuf, " LOGIN ")) {
1383                 CtdlLogPrintf(CTDL_INFO, "IMAP: LOGIN...\n");
1384         }
1385         else {
1386                 CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
1387         }
1388
1389         while (strlen(cmdbuf) < 5)
1390                 strcat(cmdbuf, " ");
1391
1392         /* strip off l/t whitespace and CRLF */
1393         if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
1394                 cmdbuf[strlen(cmdbuf) - 1] = 0;
1395         if (cmdbuf[strlen(cmdbuf) - 1] == '\r')
1396                 cmdbuf[strlen(cmdbuf) - 1] = 0;
1397         striplt(cmdbuf);
1398
1399         /* If we're in the middle of a multi-line command, handle that */
1400         if (IMAP->authstate == imap_as_expecting_username) {
1401                 imap_auth_login_user(cmdbuf);
1402                 return;
1403         }
1404         if (IMAP->authstate == imap_as_expecting_plainauth) {
1405                 imap_auth_plain(cmdbuf);
1406                 return;
1407         }
1408         if (IMAP->authstate == imap_as_expecting_password) {
1409                 imap_auth_login_pass(cmdbuf);
1410                 return;
1411         }
1412
1413
1414         /* Ok, at this point we're in normal command mode.
1415          * If the command just submitted does not contain a literal, we
1416          * might think about delivering some untagged stuff...
1417          */
1418         if (cmdbuf[strlen(cmdbuf)-1] == '}') {
1419                 untagged_ok = 0;
1420         }
1421
1422         /* Grab the tag, command, and parameters. */
1423         num_parms = imap_parameterize(parms, cmdbuf);
1424
1425         /* RFC3501 says that we cannot output untagged data during these commands */
1426         if (num_parms >= 2) {
1427                 if (  (!strcasecmp(parms[1], "FETCH"))
1428                    || (!strcasecmp(parms[1], "STORE"))
1429                    || (!strcasecmp(parms[1], "SEARCH"))
1430                 ) {
1431                         untagged_ok = 0;
1432                 }
1433         }
1434         
1435         if (untagged_ok) {
1436
1437                 /* we can put any additional untagged stuff right here in the future */
1438
1439                 /*
1440                  * Before processing the command that was just entered... if we happen
1441                  * to have a folder selected, we'd like to rescan that folder for new
1442                  * messages, and for deletions/changes of existing messages.  This
1443                  * could probably be optimized better with some deep thought...
1444                  */
1445                 if (IMAP->selected) {
1446                         imap_rescan_msgids();
1447                 }
1448         }
1449
1450         /* Now for the command set. */
1451
1452         if (num_parms < 2) {
1453                 cprintf("BAD syntax error\r\n");
1454         }
1455
1456         /* The commands below may be executed in any state */
1457
1458         else if ((!strcasecmp(parms[1], "NOOP"))
1459                  || (!strcasecmp(parms[1], "CHECK"))) {
1460                 cprintf("%s OK No operation\r\n",
1461                         parms[0]);
1462         }
1463
1464         else if (!strcasecmp(parms[1], "ID")) {
1465                 imap_id(num_parms, parms);
1466         }
1467
1468
1469         else if (!strcasecmp(parms[1], "LOGOUT")) {
1470                 if (IMAP->selected) {
1471                         imap_do_expunge();      /* yes, we auto-expunge at logout */
1472                 }
1473                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
1474                 cprintf("%s OK Citadel IMAP session ended.\r\n",
1475                         parms[0]);
1476                 CC->kill_me = 1;
1477                 return;
1478         }
1479
1480         else if (!strcasecmp(parms[1], "LOGIN")) {
1481                 imap_login(num_parms, parms);
1482         }
1483
1484         else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
1485                 imap_authenticate(num_parms, parms);
1486         }
1487
1488         else if (!strcasecmp(parms[1], "CAPABILITY")) {
1489                 imap_capability(num_parms, parms);
1490         }
1491 #ifdef HAVE_OPENSSL
1492         else if (!strcasecmp(parms[1], "STARTTLS")) {
1493                 imap_starttls(num_parms, parms);
1494         }
1495 #endif
1496         else if (!CC->logged_in) {
1497                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
1498         }
1499
1500         /* The commans below require a logged-in state */
1501
1502         else if (!strcasecmp(parms[1], "SELECT")) {
1503                 imap_select(num_parms, parms);
1504         }
1505
1506         else if (!strcasecmp(parms[1], "EXAMINE")) {
1507                 imap_select(num_parms, parms);
1508         }
1509
1510         else if (!strcasecmp(parms[1], "LSUB")) {
1511                 imap_list(num_parms, parms);
1512         }
1513
1514         else if (!strcasecmp(parms[1], "LIST")) {
1515                 imap_list(num_parms, parms);
1516         }
1517
1518         else if (!strcasecmp(parms[1], "CREATE")) {
1519                 imap_create(num_parms, parms);
1520         }
1521
1522         else if (!strcasecmp(parms[1], "DELETE")) {
1523                 imap_delete(num_parms, parms);
1524         }
1525
1526         else if (!strcasecmp(parms[1], "RENAME")) {
1527                 imap_rename(num_parms, parms);
1528         }
1529
1530         else if (!strcasecmp(parms[1], "STATUS")) {
1531                 imap_status(num_parms, parms);
1532         }
1533
1534         else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1535                 imap_subscribe(num_parms, parms);
1536         }
1537
1538         else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1539                 imap_unsubscribe(num_parms, parms);
1540         }
1541
1542         else if (!strcasecmp(parms[1], "APPEND")) {
1543                 imap_append(num_parms, parms);
1544         }
1545
1546         else if (!strcasecmp(parms[1], "NAMESPACE")) {
1547                 imap_namespace(num_parms, parms);
1548         }
1549
1550         else if (!strcasecmp(parms[1], "SETACL")) {
1551                 imap_setacl(num_parms, parms);
1552         }
1553
1554         else if (!strcasecmp(parms[1], "DELETEACL")) {
1555                 imap_deleteacl(num_parms, parms);
1556         }
1557
1558         else if (!strcasecmp(parms[1], "GETACL")) {
1559                 imap_getacl(num_parms, parms);
1560         }
1561
1562         else if (!strcasecmp(parms[1], "LISTRIGHTS")) {
1563                 imap_listrights(num_parms, parms);
1564         }
1565
1566         else if (!strcasecmp(parms[1], "MYRIGHTS")) {
1567                 imap_myrights(num_parms, parms);
1568         }
1569
1570         else if (!strcasecmp(parms[1], "GETMETADATA")) {
1571                 imap_getmetadata(num_parms, parms);
1572         }
1573
1574         else if (!strcasecmp(parms[1], "SETMETADATA")) {
1575                 imap_setmetadata(num_parms, parms);
1576         }
1577
1578         else if (IMAP->selected == 0) {
1579                 cprintf("%s BAD no folder selected\r\n", parms[0]);
1580         }
1581
1582         /* The commands below require the SELECT state on a mailbox */
1583
1584         else if (!strcasecmp(parms[1], "FETCH")) {
1585                 imap_fetch(num_parms, parms);
1586         }
1587
1588         else if ((!strcasecmp(parms[1], "UID"))
1589                  && (!strcasecmp(parms[2], "FETCH"))) {
1590                 imap_uidfetch(num_parms, parms);
1591         }
1592
1593         else if (!strcasecmp(parms[1], "SEARCH")) {
1594                 imap_search(num_parms, parms);
1595         }
1596
1597         else if ((!strcasecmp(parms[1], "UID"))
1598                  && (!strcasecmp(parms[2], "SEARCH"))) {
1599                 imap_uidsearch(num_parms, parms);
1600         }
1601
1602         else if (!strcasecmp(parms[1], "STORE")) {
1603                 imap_store(num_parms, parms);
1604         }
1605
1606         else if ((!strcasecmp(parms[1], "UID"))
1607                  && (!strcasecmp(parms[2], "STORE"))) {
1608                 imap_uidstore(num_parms, parms);
1609         }
1610
1611         else if (!strcasecmp(parms[1], "COPY")) {
1612                 imap_copy(num_parms, parms);
1613         }
1614
1615         else if ((!strcasecmp(parms[1], "UID")) && (!strcasecmp(parms[2], "COPY"))) {
1616                 imap_uidcopy(num_parms, parms);
1617         }
1618
1619         else if (!strcasecmp(parms[1], "EXPUNGE")) {
1620                 imap_expunge(num_parms, parms);
1621         }
1622
1623         else if ((!strcasecmp(parms[1], "UID")) && (!strcasecmp(parms[2], "EXPUNGE"))) {
1624                 imap_expunge(num_parms, parms);
1625         }
1626
1627         else if (!strcasecmp(parms[1], "CLOSE")) {
1628                 imap_close(num_parms, parms);
1629         }
1630
1631         /* End of commands.  If we get here, the command is either invalid
1632          * or unimplemented.
1633          */
1634
1635         else {
1636                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1637         }
1638
1639         /* If the client transmitted a message we can free it now */
1640         imap_free_transmitted_message();
1641
1642         gettimeofday(&tv2, NULL);
1643         total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
1644         CtdlLogPrintf(CTDL_INFO, "IMAP: %s\n", cmdbuf); // FIXME FIXME FIXME REMOVE THIS NOW
1645         CtdlLogPrintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n",
1646                 (total_time / 1000000),
1647                 (total_time % 1000000)
1648         );
1649 }
1650
1651
1652 const char *CitadelServiceIMAP="IMAP";
1653 const char *CitadelServiceIMAPS="IMAPS";
1654
1655 /*
1656  * This function is called to register the IMAP extension with Citadel.
1657  */
1658 CTDL_MODULE_INIT(imap)
1659 {
1660         if (!threading)
1661         {
1662                 CtdlRegisterServiceHook(config.c_imap_port,
1663                                         NULL, imap_greeting, imap_command_loop, NULL, CitadelServiceIMAP);
1664 #ifdef HAVE_OPENSSL
1665                 CtdlRegisterServiceHook(config.c_imaps_port,
1666                                         NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS);
1667 #endif
1668                 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
1669         }
1670         
1671         /* return our Subversion id for the Log */
1672         return "$Id$";
1673 }