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