]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* Began implementation of per-message seen/unseen attribute
[citadel.git] / citadel / serv_imap.c
1 /*
2  * $Id$ 
3  *
4  * IMAP server for the Citadel/UX system
5  * Copyright (C) 2000-2001 by Art Cancro and others.
6  * This code is released under the terms of the GNU General Public License.
7  *
8  * WARNING: this is an incomplete implementation, still in progress.  Parts of
9  * it work, but it's not really usable yet from a user perspective.
10  *
11  * WARNING: Mark Crispin is an idiot.  IMAP is the most brain-damaged protocol
12  * you will ever have the profound lack of pleasure to encounter.
13  * 
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <sys/wait.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <limits.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include <time.h>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
35 #include "support.h"
36 #include "config.h"
37 #include "dynloader.h"
38 #include "room_ops.h"
39 #include "user_ops.h"
40 #include "policy.h"
41 #include "database.h"
42 #include "msgbase.h"
43 #include "tools.h"
44 #include "internet_addressing.h"
45 #include "serv_imap.h"
46 #include "imap_tools.h"
47 #include "imap_fetch.h"
48 #include "imap_search.h"
49 #include "imap_store.h"
50 #include "imap_misc.h"
51
52
53 long SYM_IMAP;
54
55
56 /*
57  * If there is a message ID map in memory, free it
58  */
59 void imap_free_msgids(void) {
60         if (IMAP->msgids != NULL) {
61                 phree(IMAP->msgids);
62                 IMAP->msgids = NULL;
63                 IMAP->num_msgs = 0;
64         }
65         if (IMAP->flags != NULL) {
66                 phree(IMAP->flags);
67                 IMAP->flags = NULL;
68         }
69 }
70
71
72 /*
73  * If there is a transmitted message in memory, free it
74  */
75 void imap_free_transmitted_message(void) {
76         if (IMAP->transmitted_message != NULL) {
77                 phree(IMAP->transmitted_message);
78                 IMAP->transmitted_message = NULL;
79                 IMAP->transmitted_length = 0;
80         }
81 }
82
83
84 /*
85  * Set the \\Seen flag for messages which aren't new
86  */
87 void imap_set_seen_flags(void) {
88         struct visit vbuf;
89         int i;
90
91         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
92         if (IMAP->num_msgs > 0) {
93                 for (i=0; i<IMAP->num_msgs; ++i) {
94                         if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
95                                 IMAP->flags[i] |= IMAP_SEEN;
96                         }
97                 }
98         }
99 }
100
101
102
103
104 /*
105  * Back end for imap_load_msgids()
106  *
107  * FIXME: this should be optimized by figuring out a way to allocate memory
108  * once rather than doing a reallok() for each message.
109  */
110 void imap_add_single_msgid(long msgnum, void *userdata) {
111         
112         IMAP->num_msgs = IMAP->num_msgs + 1;
113         if (IMAP->msgids == NULL) {
114                 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
115         }
116         else {
117                 IMAP->msgids = reallok(IMAP->msgids,
118                         IMAP->num_msgs * sizeof(long));
119         }
120         if (IMAP->flags == NULL) {
121                 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
122         }
123         else {
124                 IMAP->flags = reallok(IMAP->flags,
125                         IMAP->num_msgs * sizeof(long));
126         }
127         IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
128         IMAP->flags[IMAP->num_msgs - 1] = 0;
129 }
130
131
132
133 /*
134  * Set up a message ID map for the current room (folder)
135  */
136 void imap_load_msgids(void) {
137          
138         if (IMAP->selected == 0) {
139                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
140                 return;
141         }
142
143         imap_free_msgids();     /* If there was already a map, free it */
144
145         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
146                 imap_add_single_msgid, NULL);
147
148         imap_set_seen_flags();
149         lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
150 }
151
152
153 /*
154  * Re-scan the selected room (folder) and see if it's been changed at all
155  */
156 void imap_rescan_msgids(void) {
157
158         int original_num_msgs = 0;
159         long original_highest = 0L;
160         int i;
161         int count;
162
163         if (IMAP->selected == 0) {
164                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
165                 return;
166         }
167
168
169         /*
170          * Check to see if any of the messages we know about have been expunged
171          */
172         if (IMAP->num_msgs > 0)
173          for (i=0; i<IMAP->num_msgs; ++i) {
174
175                 count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
176                         (-63), NULL, NULL, NULL, NULL);
177
178                 if (count == 0) {
179                         cprintf("* %d EXPUNGE\r\n", i+1);
180
181                         /* Here's some nice stupid nonsense.  When a message
182                          * is expunged, we have to slide all the existing
183                          * messages up in the message array.
184                          */
185                         --IMAP->num_msgs;
186                         memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
187                                 (sizeof(long)*(IMAP->num_msgs-i)) );
188                         memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
189                                 (sizeof(long)*(IMAP->num_msgs-i)) );
190
191                         --i;
192                 }
193
194         }
195
196         /*
197          * Remember how many messages were here before we re-scanned.
198          */
199         original_num_msgs = IMAP->num_msgs;
200         if (IMAP->num_msgs > 0) {
201                 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
202         }
203         else {
204                 original_highest = 0L;
205         }
206
207         /*
208          * Now peruse the room for *new* messages only.
209          */
210         CtdlForEachMessage(MSGS_GT, original_highest, (-63), NULL, NULL,
211                 imap_add_single_msgid, NULL);
212
213         imap_set_seen_flags();
214
215         /*
216          * If new messages have arrived, tell the client about them.
217          */
218         if (IMAP->num_msgs > original_num_msgs) {
219                 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
220         }
221
222 }
223
224
225
226
227
228
229
230 /*
231  * This cleanup function blows away the temporary memory and files used by
232  * the IMAP server.
233  */
234 void imap_cleanup_function(void) {
235
236         /* Don't do this stuff if this is not a IMAP session! */
237         if (CC->h_command_function != imap_command_loop) return;
238
239         lprintf(9, "Performing IMAP cleanup hook\n");
240         imap_free_msgids();
241         imap_free_transmitted_message();
242         lprintf(9, "Finished IMAP cleanup hook\n");
243 }
244
245
246
247 /*
248  * Here's where our IMAP session begins its happy day.
249  */
250 void imap_greeting(void) {
251
252         strcpy(CC->cs_clientname, "IMAP session");
253         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
254         IMAP->authstate = imap_as_normal;
255
256         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
257                 config.c_fqdn);
258 }
259
260
261 /*
262  * implements the LOGIN command (ordinary username/password login)
263  */
264 void imap_login(int num_parms, char *parms[]) {
265         if (CtdlLoginExistingUser(parms[2]) == login_ok) {
266                 if (CtdlTryPassword(parms[3]) == pass_ok) {
267                         cprintf("%s OK login successful\r\n", parms[0]);
268                         return;
269                 }
270         }
271
272         cprintf("%s BAD Login incorrect\r\n", parms[0]);
273 }
274
275
276 /*
277  * Implements the AUTHENTICATE command
278  */
279 void imap_authenticate(int num_parms, char *parms[]) {
280         char buf[SIZ];
281
282         if (num_parms != 3) {
283                 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
284                 return;
285         }
286
287         if (!strcasecmp(parms[2], "LOGIN")) {
288                 encode_base64(buf, "Username:");
289                 cprintf("+ %s\r\n", buf);
290                 IMAP->authstate = imap_as_expecting_username;
291                 strcpy(IMAP->authseq, parms[0]);
292                 return;
293         }
294
295         else {
296                 cprintf("%s NO AUTHENTICATE %s failed\r\n",
297                         parms[0], parms[1]);
298         }
299 }
300
301 void imap_auth_login_user(char *cmd) {
302         char buf[SIZ];
303
304         decode_base64(buf, cmd);
305         CtdlLoginExistingUser(buf);
306         encode_base64(buf, "Password:");
307         cprintf("+ %s\r\n", buf);
308         IMAP->authstate = imap_as_expecting_password;
309         return;
310 }
311
312 void imap_auth_login_pass(char *cmd) {
313         char buf[SIZ];
314
315         decode_base64(buf, cmd);
316         if (CtdlTryPassword(buf) == pass_ok) {
317                 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
318         }
319         else {
320                 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
321         }
322         IMAP->authstate = imap_as_normal;
323         return;
324 }
325
326
327
328 /*
329  * implements the CAPABILITY command
330  */
331 void imap_capability(int num_parms, char *parms[]) {
332         cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
333         cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
334 }
335
336
337
338
339
340 /*
341  * implements the SELECT command
342  */
343 void imap_select(int num_parms, char *parms[]) {
344         char towhere[SIZ];
345         char augmented_roomname[ROOMNAMELEN];
346         int c = 0;
347         int ok = 0;
348         int ra = 0;
349         struct quickroom QRscratch;
350         int msgs, new;
351         int floornum;
352         int roomflags;
353         int i;
354
355         /* Convert the supplied folder name to a roomname */
356         i = imap_roomname(towhere, sizeof towhere, parms[2]);
357         if (i < 0) {
358                 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
359                 IMAP->selected = 0;
360                 return;
361         }
362         floornum = (i & 0x00ff);
363         roomflags = (i & 0xff00);
364
365         /* First try a regular match */
366         c = getroom(&QRscratch, towhere);
367
368         /* Then try a mailbox name match */
369         if (c != 0) {
370                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
371                 c = getroom(&QRscratch, augmented_roomname);
372                 if (c == 0)
373                         strcpy(towhere, augmented_roomname);
374         }
375
376         /* If the room exists, check security/access */
377         if (c == 0) {
378                 /* See if there is an existing user/room relationship */
379                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
380
381                 /* normal clients have to pass through security */
382                 if (ra & UA_KNOWN) {
383                         ok = 1;
384                 }
385         }
386
387         /* Fail here if no such room */
388         if (!ok) {
389                 cprintf("%s NO ... no such room, or access denied\r\n",
390                         parms[0]);
391                 IMAP->selected = 0;
392                 return;
393         }
394
395         /*
396          * usergoto() formally takes us to the desired room, happily returning
397          * the number of messages and number of new messages.
398          */
399         usergoto(QRscratch.QRname, 0, &msgs, &new);
400         IMAP->selected = 1;
401
402         if (!strcasecmp(parms[1], "EXAMINE")) {
403                 IMAP->readonly = 1;
404         }
405         else {
406                 IMAP->readonly = 0;
407         }
408
409         imap_load_msgids();
410
411         /* FIXME ... much more info needs to be supplied here */
412         cprintf("* %d EXISTS\r\n", msgs);
413         cprintf("* %d RECENT\r\n", new);
414         cprintf("* FLAGS (\\Deleted \\Seen)\r\n");
415         cprintf("* OK [PERMANENTFLAGS (\\Deleted \\Seen)] permanent flags\r\n");
416         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
417         cprintf("%s OK [%s] %s completed\r\n",
418                 parms[0],
419                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
420                 parms[1]);
421 }
422
423
424
425 /*
426  * does the real work for expunge
427  */
428 int imap_do_expunge(void) {
429         int i;
430         int num_expunged = 0;
431
432         if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
433                 if (IMAP->flags[i] & IMAP_DELETED) {
434                         CtdlDeleteMessages(CC->quickroom.QRname,
435                                         IMAP->msgids[i], "");
436                         ++num_expunged;
437                 }
438         }
439
440         if (num_expunged > 0) {
441                 imap_rescan_msgids();
442         }
443
444         return(num_expunged);
445 }
446
447
448 /*
449  * implements the EXPUNGE command syntax
450  */
451 void imap_expunge(int num_parms, char *parms[]) {
452         int num_expunged = 0;
453         imap_do_expunge();
454         cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
455 }
456
457
458 /*
459  * implements the CLOSE command
460  */
461 void imap_close(int num_parms, char *parms[]) {
462
463         /* Yes, we always expunge on close. */
464         imap_do_expunge();
465
466         IMAP->selected = 0;
467         IMAP->readonly = 0;
468         imap_free_msgids();
469         cprintf("%s OK CLOSE completed\r\n", parms[0]);
470 }
471
472
473
474
475 /*
476  * Used by LIST and LSUB to show the floors in the listing
477  */
478 void imap_list_floors(char *cmd, char *pattern) {
479         int i;
480         struct floor *fl;
481
482         for (i=0; i<MAXFLOORS; ++i) {
483                 fl = cgetfloor(i);
484                 if (fl->f_flags & F_INUSE) {
485                         if (imap_mailbox_matches_pattern(pattern, fl->f_name)) {
486                                 cprintf("* %s (\\NoSelect) \"|\" ", cmd);
487                                 imap_strout(fl->f_name);
488                                 cprintf("\r\n");
489                         }
490                 }
491         }
492 }
493
494
495
496 /*
497  * Back end for imap_lsub()
498  *
499  * IMAP "subscribed folder" is equivocated to Citadel "known rooms."  This
500  * may or may not be the desired behavior in the future.
501  */
502 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
503         char buf[SIZ];
504         int ra;
505         char *pattern;
506
507         pattern = (char *)data;
508
509         /* Only list rooms to which the user has access!! */
510         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
511         if (ra & UA_KNOWN) {
512                 imap_mailboxname(buf, sizeof buf, qrbuf);
513                 if (imap_mailbox_matches_pattern(pattern, buf)) {
514                         cprintf("* LSUB (\\NoInferiors) \"|\" ");
515                         imap_strout(buf);
516                         cprintf("\r\n");
517                 }
518         }
519 }
520
521
522 /*
523  * Implements the LSUB command
524  */
525 void imap_lsub(int num_parms, char *parms[]) {
526         char pattern[SIZ];
527         if (num_parms < 4) {
528                 cprintf("%s BAD arguments invalid\r\n", parms[0]);
529                 return;
530         }
531         sprintf(pattern, "%s%s", parms[2], parms[3]);
532
533         if (strlen(parms[3])==0) {
534                 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
535         }
536
537         else {
538                 imap_list_floors("LSUB", pattern);
539                 ForEachRoom(imap_lsub_listroom, pattern);
540         }
541
542         cprintf("%s OK LSUB completed\r\n", parms[0]);
543 }
544
545
546
547 /*
548  * Back end for imap_list()
549  */
550 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
551         char buf[SIZ];
552         int ra;
553         char *pattern;
554
555         pattern = (char *)data;
556
557         /* Only list rooms to which the user has access!! */
558         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
559         if ( (ra & UA_KNOWN) 
560           || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
561                 imap_mailboxname(buf, sizeof buf, qrbuf);
562                 if (imap_mailbox_matches_pattern(pattern, buf)) {
563                         cprintf("* LIST (\\NoInferiors) \"|\" ");
564                         imap_strout(buf);
565                         cprintf("\r\n");
566                 }
567         }
568 }
569
570
571 /*
572  * Implements the LIST command
573  */
574 void imap_list(int num_parms, char *parms[]) {
575         char pattern[SIZ];
576         if (num_parms < 4) {
577                 cprintf("%s BAD arguments invalid\r\n", parms[0]);
578                 return;
579         }
580         sprintf(pattern, "%s%s", parms[2], parms[3]);
581
582         if (strlen(parms[3])==0) {
583                 cprintf("* LIST (\\Noselect) \"|\" \"\"\r\n");
584         }
585
586         else {
587                 imap_list_floors("LIST", pattern);
588                 ForEachRoom(imap_list_listroom, pattern);
589         }
590
591         cprintf("%s OK LIST completed\r\n", parms[0]);
592 }
593
594
595
596 /*
597  * Implements the CREATE command
598  *
599  */
600 void imap_create(int num_parms, char *parms[]) {
601         int ret;
602         char roomname[ROOMNAMELEN];
603         int floornum;
604         int flags;
605         int newroomtype;
606
607         ret = imap_roomname(roomname, sizeof roomname, parms[2]);
608         if (ret < 0) {
609                 cprintf("%s NO Invalid mailbox name or location\r\n",
610                         parms[0]);
611                 return;
612         }
613         floornum = ( ret & 0x00ff );    /* lower 8 bits = floor number */
614         flags =    ( ret & 0xff00 );    /* upper 8 bits = flags        */
615
616         if (flags & IR_MAILBOX) {
617                 newroomtype = 4;        /* private mailbox */
618         }
619         else {
620                 newroomtype = 0;        /* public folder */
621         }
622
623         lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
624                 roomname, floornum, newroomtype);
625
626         ret = create_room(roomname, newroomtype, "", floornum, 1);
627         if (ret == 0) {
628                 cprintf("%s NO Mailbox already exists, or create failed\r\n",
629                         parms[0]);
630         }
631         else {
632                 cprintf("%s OK CREATE completed\r\n", parms[0]);
633         }
634 }
635
636
637 /*
638  * Locate a room by its IMAP folder name, and check access to it
639  */
640 int imap_grabroom(char *returned_roomname, char *foldername) {
641         int ret;
642         char augmented_roomname[ROOMNAMELEN];
643         char roomname[ROOMNAMELEN];
644         int c;
645         struct quickroom QRscratch;
646         int ra;
647         int ok = 0;
648
649         ret = imap_roomname(roomname, sizeof roomname, foldername);
650         if (ret < 0) {
651                 return(1);
652         }
653
654         /* First try a regular match */
655         c = getroom(&QRscratch, roomname);
656
657         /* Then try a mailbox name match */
658         if (c != 0) {
659                 MailboxName(augmented_roomname, &CC->usersupp, roomname);
660                 c = getroom(&QRscratch, augmented_roomname);
661                 if (c == 0)
662                         strcpy(roomname, augmented_roomname);
663         }
664
665         /* If the room exists, check security/access */
666         if (c == 0) {
667                 /* See if there is an existing user/room relationship */
668                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
669
670                 /* normal clients have to pass through security */
671                 if (ra & UA_KNOWN) {
672                         ok = 1;
673                 }
674         }
675
676         /* Fail here if no such room */
677         if (!ok) {
678                 strcpy(returned_roomname, "");
679                 return(2);
680         }
681         else {
682                 strcpy(returned_roomname, QRscratch.QRname);
683                 return(0);
684         }
685 }
686
687
688 /*
689  * Implements the STATUS command (sort of)
690  *
691  */
692 void imap_status(int num_parms, char *parms[]) {
693         int ret;
694         char roomname[ROOMNAMELEN];
695         char buf[SIZ];
696         char savedroom[ROOMNAMELEN];
697         int msgs, new;
698
699         ret = imap_grabroom(roomname, parms[2]);
700         if (ret != 0) {
701                 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
702                         parms[0]);
703                 return;
704         }
705
706         /*
707          * usergoto() formally takes us to the desired room, happily returning
708          * the number of messages and number of new messages.  (If another
709          * folder is selected, save its name so we can return there!!!!!)
710          */
711         if (IMAP->selected) {
712                 strcpy(savedroom, CC->quickroom.QRname);
713         }
714         usergoto(roomname, 0, &msgs, &new);
715
716         /*
717          * Tell the client what it wants to know.  In fact, tell it *more* than
718          * it wants to know.  We happily IGnore the supplied status data item
719          * names and simply spew all possible data items.  It's far easier to
720          * code and probably saves us some processing time too.
721          */
722         imap_mailboxname(buf, sizeof buf, &CC->quickroom);
723         cprintf("* STATUS ");
724         imap_strout(buf);
725         cprintf(" (MESSAGES %d ", msgs);
726         cprintf("RECENT 0 ");   /* FIXME we need to implement this */
727         cprintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
728         cprintf("UNSEEN %d)\r\n", new);
729
730         /*
731          * If another folder is selected, go back to that room so we can resume
732          * our happy day without violent explosions.
733          */
734         if (IMAP->selected) {
735                 usergoto(savedroom, 0, &msgs, &new);
736         }
737
738         /*
739          * Oooh, look, we're done!
740          */
741         cprintf("%s OK STATUS completed\r\n", parms[0]);
742 }
743
744
745
746 /*
747  * Implements the SUBSCRIBE command
748  *
749  */
750 void imap_subscribe(int num_parms, char *parms[]) {
751         int ret;
752         char roomname[ROOMNAMELEN];
753         char savedroom[ROOMNAMELEN];
754         int msgs, new;
755
756         ret = imap_grabroom(roomname, parms[2]);
757         if (ret != 0) {
758                 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
759                         parms[0]);
760                 return;
761         }
762
763         /*
764          * usergoto() formally takes us to the desired room, which has the side
765          * effect of marking the room as not-zapped ... exactly the effect
766          * we're looking for.
767          */
768         if (IMAP->selected) {
769                 strcpy(savedroom, CC->quickroom.QRname);
770         }
771         usergoto(roomname, 0, &msgs, &new);
772
773         /*
774          * If another folder is selected, go back to that room so we can resume
775          * our happy day without violent explosions.
776          */
777         if (IMAP->selected) {
778                 usergoto(savedroom, 0, &msgs, &new);
779         }
780
781         cprintf("%s OK SUBSCRIBE completed\r\n", parms[0]);
782 }
783
784
785 /*
786  * Implements the UNSUBSCRIBE command
787  *
788  */
789 void imap_unsubscribe(int num_parms, char *parms[]) {
790         int ret;
791         char roomname[ROOMNAMELEN];
792         char savedroom[ROOMNAMELEN];
793         int msgs, new;
794
795         ret = imap_grabroom(roomname, parms[2]);
796         if (ret != 0) {
797                 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
798                         parms[0]);
799                 return;
800         }
801
802         /*
803          * usergoto() formally takes us to the desired room.
804          */
805         if (IMAP->selected) {
806                 strcpy(savedroom, CC->quickroom.QRname);
807         }
808         usergoto(roomname, 0, &msgs, &new);
809
810         /* 
811          * Now make the API call to zap the room
812          */
813         if (CtdlForgetThisRoom() == 0) {
814                 cprintf("%s OK UNSUBSCRIBE completed\r\n", parms[0]);
815         }
816         else {
817                 cprintf("%s NO You may not unsubscribe from this folder.\r\n",
818                         parms[0]);
819         }
820
821         /*
822          * If another folder is selected, go back to that room so we can resume
823          * our happy day without violent explosions.
824          */
825         if (IMAP->selected) {
826                 usergoto(savedroom, 0, &msgs, &new);
827         }
828 }
829
830
831
832 /*
833  * Implements the DELETE command
834  *
835  */
836 void imap_delete(int num_parms, char *parms[]) {
837         int ret;
838         char roomname[ROOMNAMELEN];
839         char savedroom[ROOMNAMELEN];
840         int msgs, new;
841
842         ret = imap_grabroom(roomname, parms[2]);
843         if (ret != 0) {
844                 cprintf("%s NO Invalid mailbox name, or access denied\r\n",
845                         parms[0]);
846                 return;
847         }
848
849         /*
850          * usergoto() formally takes us to the desired room, happily returning
851          * the number of messages and number of new messages.  (If another
852          * folder is selected, save its name so we can return there!!!!!)
853          */
854         if (IMAP->selected) {
855                 strcpy(savedroom, CC->quickroom.QRname);
856         }
857         usergoto(roomname, 0, &msgs, &new);
858
859         /*
860          * Now delete the room.
861          */
862         if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom)) {
863                 cprintf("%s OK DELETE completed\r\n", parms[0]);
864                 delete_room(&CC->quickroom);
865         }
866         else {
867                 cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
868         }
869
870         /*
871          * If another folder is selected, go back to that room so we can resume
872          * our happy day without violent explosions.
873          */
874         if (IMAP->selected) {
875                 usergoto(savedroom, 0, &msgs, &new);
876         }
877 }
878
879
880
881 /*
882  * Implements the RENAME command
883  *
884  */
885 void imap_rename(int num_parms, char *parms[]) {
886         cprintf("%s NO The RENAME command is not yet implemented (FIXME)\r\n",
887                 parms[0]);
888 }
889
890
891
892 /* 
893  * Main command loop for IMAP sessions.
894  */
895 void imap_command_loop(void) {
896         char cmdbuf[SIZ];
897         char *parms[SIZ];
898         int num_parms;
899
900         time(&CC->lastcmd);
901         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
902         if (client_gets(cmdbuf) < 1) {
903                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
904                 CC->kill_me = 1;
905                 return;
906         }
907
908         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
909         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
910
911
912         /* strip off l/t whitespace and CRLF */
913         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
914         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
915         striplt(cmdbuf);
916
917         /* If we're in the middle of a multi-line command, handle that */
918         if (IMAP->authstate == imap_as_expecting_username) {
919                 imap_auth_login_user(cmdbuf);
920                 return;
921         }
922         if (IMAP->authstate == imap_as_expecting_password) {
923                 imap_auth_login_pass(cmdbuf);
924                 return;
925         }
926
927
928         /* Ok, at this point we're in normal command mode.  The first thing
929          * we do is print any incoming pages (yeah! we really do!)
930          */
931         imap_print_express_messages();
932
933         /*
934          * Before processing the command that was just entered... if we happen
935          * to have a folder selected, we'd like to rescan that folder for new
936          * messages, and for deletions/changes of existing messages.  This
937          * could probably be optimized somehow, but IMAP sucks...
938          */
939         if (IMAP->selected) {
940                 imap_rescan_msgids();
941         }
942
943         /* Now for the command set. */
944
945         /* Grab the tag, command, and parameters.  Check syntax. */
946         num_parms = imap_parameterize(parms, cmdbuf);
947         if (num_parms < 2) {
948                 cprintf("BAD syntax error\r\n");
949         }
950
951         /* The commands below may be executed in any state */
952
953         else if ( (!strcasecmp(parms[1], "NOOP"))
954            || (!strcasecmp(parms[1], "CHECK")) ) {
955                 cprintf("%s OK This command successfully did nothing.\r\n",
956                         parms[0]);
957         }
958
959         else if (!strcasecmp(parms[1], "LOGOUT")) {
960                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
961                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
962                 CC->kill_me = 1;
963                 return;
964         }
965
966         else if (!strcasecmp(parms[1], "LOGIN")) {
967                 imap_login(num_parms, parms);
968         }
969
970         else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
971                 imap_authenticate(num_parms, parms);
972         }
973
974         else if (!strcasecmp(parms[1], "CAPABILITY")) {
975                 imap_capability(num_parms, parms);
976         }
977
978         else if (!CC->logged_in) {
979                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
980         }
981
982         /* The commans below require a logged-in state */
983
984         else if (!strcasecmp(parms[1], "SELECT")) {
985                 imap_select(num_parms, parms);
986         }
987
988         else if (!strcasecmp(parms[1], "EXAMINE")) {
989                 imap_select(num_parms, parms);
990         }
991
992         else if (!strcasecmp(parms[1], "LSUB")) {
993                 imap_lsub(num_parms, parms);
994         }
995
996         else if (!strcasecmp(parms[1], "LIST")) {
997                 imap_list(num_parms, parms);
998         }
999
1000         else if (!strcasecmp(parms[1], "CREATE")) {
1001                 imap_create(num_parms, parms);
1002         }
1003
1004         else if (!strcasecmp(parms[1], "DELETE")) {
1005                 imap_delete(num_parms, parms);
1006         }
1007
1008         else if (!strcasecmp(parms[1], "RENAME")) {
1009                 imap_rename(num_parms, parms);
1010         }
1011
1012         else if (!strcasecmp(parms[1], "STATUS")) {
1013                 imap_status(num_parms, parms);
1014         }
1015
1016         else if (!strcasecmp(parms[1], "SUBSCRIBE")) {
1017                 imap_subscribe(num_parms, parms);
1018         }
1019
1020         else if (!strcasecmp(parms[1], "UNSUBSCRIBE")) {
1021                 imap_unsubscribe(num_parms, parms);
1022         }
1023
1024         else if (!strcasecmp(parms[1], "APPEND")) {
1025                 imap_append(num_parms, parms);
1026         }
1027
1028         else if (IMAP->selected == 0) {
1029                 cprintf("%s BAD no folder selected\r\n", parms[0]);
1030         }
1031
1032         /* The commands below require the SELECT state on a mailbox */
1033
1034         else if (!strcasecmp(parms[1], "FETCH")) {
1035                 imap_fetch(num_parms, parms);
1036         }
1037
1038         else if ( (!strcasecmp(parms[1], "UID"))
1039                 && (!strcasecmp(parms[2], "FETCH")) ) {
1040                 imap_uidfetch(num_parms, parms);
1041         }
1042
1043         else if (!strcasecmp(parms[1], "SEARCH")) {
1044                 imap_search(num_parms, parms);
1045         }
1046
1047         else if ( (!strcasecmp(parms[1], "UID"))
1048                 && (!strcasecmp(parms[2], "SEARCH")) ) {
1049                 imap_uidsearch(num_parms, parms);
1050         }
1051
1052         else if (!strcasecmp(parms[1], "STORE")) {
1053                 imap_store(num_parms, parms);
1054         }
1055
1056         else if ( (!strcasecmp(parms[1], "UID"))
1057                 && (!strcasecmp(parms[2], "STORE")) ) {
1058                 imap_uidstore(num_parms, parms);
1059         }
1060
1061         else if (!strcasecmp(parms[1], "COPY")) {
1062                 imap_copy(num_parms, parms);
1063         }
1064
1065         else if ( (!strcasecmp(parms[1], "UID"))
1066                 && (!strcasecmp(parms[2], "COPY")) ) {
1067                 imap_uidcopy(num_parms, parms);
1068         }
1069
1070         else if (!strcasecmp(parms[1], "EXPUNGE")) {
1071                 imap_expunge(num_parms, parms);
1072         }
1073
1074         else if (!strcasecmp(parms[1], "CLOSE")) {
1075                 imap_close(num_parms, parms);
1076         }
1077
1078         /* End of commands.  If we get here, the command is either invalid
1079          * or unimplemented.
1080          */
1081
1082         else {
1083                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
1084         }
1085
1086         /* If the client transmitted a message we can free it now */
1087         imap_free_transmitted_message();
1088 }
1089
1090
1091
1092 /*
1093  * This function is called by dynloader.c to register the IMAP module
1094  * with the Citadel server.
1095  */
1096 char *Dynamic_Module_Init(void)
1097 {
1098         SYM_IMAP = CtdlGetDynamicSymbol();
1099         CtdlRegisterServiceHook(config.c_imap_port,
1100                                 NULL,
1101                                 imap_greeting,
1102                                 imap_command_loop);
1103         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
1104         return "$Id$";
1105 }