]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* Probable completion of STATUS, COPY, STORE, and EXPUNGE commands in IMAP
[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  * Back end for imap_load_msgids()
74  *
75  * FIXME: this should be optimized by figuring out a way to allocate memory
76  * once rather than doing a reallok() for each message.
77  */
78 void imap_add_single_msgid(long msgnum, void *userdata) {
79         
80         IMAP->num_msgs = IMAP->num_msgs + 1;
81         if (IMAP->msgids == NULL) {
82                 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
83         }
84         else {
85                 IMAP->msgids = reallok(IMAP->msgids,
86                         IMAP->num_msgs * sizeof(long));
87         }
88         if (IMAP->flags == NULL) {
89                 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
90         }
91         else {
92                 IMAP->flags = reallok(IMAP->flags,
93                         IMAP->num_msgs * sizeof(long));
94         }
95         IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
96         IMAP->flags[IMAP->num_msgs - 1] = 0;
97 }
98
99
100
101 /*
102  * Set up a message ID map for the current room (folder)
103  */
104 void imap_load_msgids(void) {
105          
106         if (IMAP->selected == 0) {
107                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
108                 return;
109         }
110
111         imap_free_msgids();     /* If there was already a map, free it */
112
113         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
114                 imap_add_single_msgid, NULL);
115
116         lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
117 }
118
119
120 /*
121  * Re-scan the selected room (folder) and see if it's been changed at all
122  */
123 void imap_rescan_msgids(void) {
124
125         int original_num_msgs = 0;
126         long original_highest = 0L;
127         int i;
128         int count;
129
130         if (IMAP->selected == 0) {
131                 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
132                 return;
133         }
134
135
136         /*
137          * Check to see if any of the messages we know about have been expunged
138          */
139         if (IMAP->num_msgs > 0)
140          for (i=0; i<IMAP->num_msgs; ++i) {
141
142                 count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
143                         (-63), NULL, NULL, NULL, NULL);
144
145                 if (count == 0) {
146                         cprintf("* %d EXPUNGE\r\n", i+1);
147
148                         /* Here's some nice stupid nonsense.  When a message
149                          * is expunged, we have to slide all the existing
150                          * messages up in the message array.
151                          */
152                         --IMAP->num_msgs;
153                         memcpy(&IMAP->msgids[i], &IMAP->msgids[i+1],
154                                 (sizeof(long)*(IMAP->num_msgs-i)) );
155                         memcpy(&IMAP->flags[i], &IMAP->flags[i+1],
156                                 (sizeof(long)*(IMAP->num_msgs-i)) );
157
158                         --i;
159                 }
160
161         }
162
163         /*
164          * Remember how many messages were here before we re-scanned.
165          */
166         original_num_msgs = IMAP->num_msgs;
167         if (IMAP->num_msgs > 0) {
168                 original_highest = IMAP->msgids[IMAP->num_msgs - 1];
169         }
170         else {
171                 original_highest = 0L;
172         }
173
174         /*
175          * Now peruse the room for *new* messages only.
176          */
177         CtdlForEachMessage(MSGS_GT, original_highest, (-63), NULL, NULL,
178                 imap_add_single_msgid, NULL);
179
180         /*
181          * If new messages have arrived, tell the client about them.
182          */
183         if (IMAP->num_msgs > original_num_msgs) {
184                 cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
185         }
186
187 }
188
189
190
191
192
193
194
195 /*
196  * This cleanup function blows away the temporary memory and files used by
197  * the IMAP server.
198  */
199 void imap_cleanup_function(void) {
200
201         /* Don't do this stuff if this is not a IMAP session! */
202         if (CC->h_command_function != imap_command_loop) return;
203
204         lprintf(9, "Performing IMAP cleanup hook\n");
205         imap_free_msgids();
206         lprintf(9, "Finished IMAP cleanup hook\n");
207 }
208
209
210
211 /*
212  * Here's where our IMAP session begins its happy day.
213  */
214 void imap_greeting(void) {
215
216         strcpy(CC->cs_clientname, "IMAP session");
217         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
218         IMAP->authstate = imap_as_normal;
219
220         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
221                 config.c_fqdn);
222 }
223
224
225 /*
226  * implements the LOGIN command (ordinary username/password login)
227  */
228 void imap_login(int num_parms, char *parms[]) {
229         if (CtdlLoginExistingUser(parms[2]) == login_ok) {
230                 if (CtdlTryPassword(parms[3]) == pass_ok) {
231                         cprintf("%s OK login successful\r\n", parms[0]);
232                         return;
233                 }
234         }
235
236         cprintf("%s BAD Login incorrect\r\n", parms[0]);
237 }
238
239
240 /*
241  * Implements the AUTHENTICATE command
242  */
243 void imap_authenticate(int num_parms, char *parms[]) {
244         char buf[SIZ];
245
246         if (num_parms != 3) {
247                 cprintf("%s BAD incorrect number of parameters\r\n", parms[0]);
248                 return;
249         }
250
251         if (!strcasecmp(parms[2], "LOGIN")) {
252                 encode_base64(buf, "Username:");
253                 cprintf("+ %s\r\n", buf);
254                 IMAP->authstate = imap_as_expecting_username;
255                 strcpy(IMAP->authseq, parms[0]);
256                 return;
257         }
258
259         else {
260                 cprintf("%s NO AUTHENTICATE %s failed\r\n",
261                         parms[0], parms[1]);
262         }
263 }
264
265 void imap_auth_login_user(char *cmd) {
266         char buf[SIZ];
267
268         decode_base64(buf, cmd);
269         CtdlLoginExistingUser(buf);
270         encode_base64(buf, "Password:");
271         cprintf("+ %s\r\n", buf);
272         IMAP->authstate = imap_as_expecting_password;
273         return;
274 }
275
276 void imap_auth_login_pass(char *cmd) {
277         char buf[SIZ];
278
279         decode_base64(buf, cmd);
280         if (CtdlTryPassword(buf) == pass_ok) {
281                 cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
282         }
283         else {
284                 cprintf("%s NO authentication failed\r\n", IMAP->authseq);
285         }
286         IMAP->authstate = imap_as_normal;
287         return;
288 }
289
290
291
292 /*
293  * implements the CAPABILITY command
294  */
295 void imap_capability(int num_parms, char *parms[]) {
296         cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
297         cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
298 }
299
300
301
302
303
304 /*
305  * implements the SELECT command
306  */
307 void imap_select(int num_parms, char *parms[]) {
308         char towhere[SIZ];
309         char augmented_roomname[ROOMNAMELEN];
310         int c = 0;
311         int ok = 0;
312         int ra = 0;
313         struct quickroom QRscratch;
314         int msgs, new;
315         int floornum;
316         int roomflags;
317         int i;
318
319         /* Convert the supplied folder name to a roomname */
320         i = imap_roomname(towhere, sizeof towhere, parms[2]);
321         if (i < 0) {
322                 cprintf("%s NO Invalid mailbox name.\r\n", parms[0]);
323                 IMAP->selected = 0;
324                 return;
325         }
326         floornum = (i & 0x00ff);
327         roomflags = (i & 0xff00);
328
329         /* First try a regular match */
330         c = getroom(&QRscratch, towhere);
331
332         /* Then try a mailbox name match */
333         if (c != 0) {
334                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
335                 c = getroom(&QRscratch, augmented_roomname);
336                 if (c == 0)
337                         strcpy(towhere, augmented_roomname);
338         }
339
340         /* If the room exists, check security/access */
341         if (c == 0) {
342                 /* See if there is an existing user/room relationship */
343                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
344
345                 /* normal clients have to pass through security */
346                 if (ra & UA_KNOWN) {
347                         ok = 1;
348                 }
349         }
350
351         /* Fail here if no such room */
352         if (!ok) {
353                 cprintf("%s NO ... no such room, or access denied\r\n",
354                         parms[0]);
355                 IMAP->selected = 0;
356                 return;
357         }
358
359         /*
360          * usergoto() formally takes us to the desired room, happily returning
361          * the number of messages and number of new messages.
362          */
363         usergoto(QRscratch.QRname, 0, &msgs, &new);
364         IMAP->selected = 1;
365
366         if (!strcasecmp(parms[1], "EXAMINE")) {
367                 IMAP->readonly = 1;
368         }
369         else {
370                 IMAP->readonly = 0;
371         }
372
373         imap_load_msgids();
374
375         /* FIXME ... much more info needs to be supplied here */
376         cprintf("* %d EXISTS\r\n", msgs);
377         cprintf("* %d RECENT\r\n", new);
378         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
379         cprintf("%s OK [%s] %s completed\r\n",
380                 parms[0],
381                 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
382                 parms[1]);
383 }
384
385
386
387 /*
388  * does the real work for expunge
389  */
390 int imap_do_expunge(void) {
391         int i;
392         int num_expunged = 0;
393
394         if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
395                 if (IMAP->flags[i] & IMAP_DELETED) {
396                         CtdlDeleteMessages(CC->quickroom.QRname,
397                                         IMAP->msgids[i], "");
398                         ++num_expunged;
399                 }
400         }
401
402         if (num_expunged > 0) {
403                 imap_rescan_msgids();
404         }
405
406         return(num_expunged);
407 }
408
409
410 /*
411  * implements the EXPUNGE command syntax
412  */
413 void imap_expunge(int num_parms, char *parms[]) {
414         int num_expunged = 0;
415         imap_do_expunge();
416         cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
417 }
418
419
420 /*
421  * implements the CLOSE command
422  */
423 void imap_close(int num_parms, char *parms[]) {
424
425         /* Yes, we always expunge on close. */
426         imap_do_expunge();
427
428         IMAP->selected = 0;
429         IMAP->readonly = 0;
430         imap_free_msgids();
431         cprintf("%s OK CLOSE completed\r\n", parms[0]);
432 }
433
434
435
436
437 /*
438  * Used by LIST and LSUB to show the floors in the listing
439  */
440 void imap_list_floors(char *cmd) {
441         int i;
442         struct floor *fl;
443
444         for (i=0; i<MAXFLOORS; ++i) {
445                 fl = cgetfloor(i);
446                 if (fl->f_flags & F_INUSE) {
447                         cprintf("* %s (\\NoSelect) \"|\" ", cmd);
448                         imap_strout(fl->f_name);
449                         cprintf("\r\n");
450                 }
451         }
452 }
453
454
455
456 /*
457  * Back end for imap_lsub()
458  *
459  * IMAP "subscribed folder" is equivocated to Citadel "known rooms."  This
460  * may or may not be the desired behavior in the future.
461  */
462 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
463         char buf[SIZ];
464         int ra;
465
466         /* Only list rooms to which the user has access!! */
467         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
468         if (ra & UA_KNOWN) {
469                 imap_mailboxname(buf, sizeof buf, qrbuf);
470                 cprintf("* LSUB () \"|\" ");
471                 imap_strout(buf);
472                 cprintf("\r\n");
473         }
474 }
475
476
477 /*
478  * Implements the LSUB command
479  *
480  * FIXME: Handle wildcards, please.
481  */
482 void imap_lsub(int num_parms, char *parms[]) {
483         imap_list_floors("LSUB");
484         ForEachRoom(imap_lsub_listroom, NULL);
485         cprintf("%s OK LSUB completed\r\n", parms[0]);
486 }
487
488
489
490 /*
491  * Back end for imap_list()
492  */
493 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
494         char buf[SIZ];
495         int ra;
496
497         /* Only list rooms to which the user has access!! */
498         ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
499         if ( (ra & UA_KNOWN) 
500           || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
501                 imap_mailboxname(buf, sizeof buf, qrbuf);
502                 cprintf("* LIST () \"|\" ");
503                 imap_strout(buf);
504                 cprintf("\r\n");
505         }
506 }
507
508
509 /*
510  * Implements the LIST command
511  *
512  * FIXME: Handle wildcards, please.
513  */
514 void imap_list(int num_parms, char *parms[]) {
515         imap_list_floors("LIST");
516         ForEachRoom(imap_list_listroom, NULL);
517         cprintf("%s OK LIST completed\r\n", parms[0]);
518 }
519
520
521
522 /*
523  * Implements the CREATE command
524  *
525  */
526 void imap_create(int num_parms, char *parms[]) {
527         int ret;
528         char roomname[ROOMNAMELEN];
529         int floornum;
530         int flags;
531         int newroomtype;
532
533         ret = imap_roomname(roomname, sizeof roomname, parms[2]);
534         if (ret < 0) {
535                 cprintf("%s NO Invalid mailbox name or location\r\n",
536                         parms[0]);
537                 return;
538         }
539         floornum = ( ret & 0x00ff );    /* lower 8 bits = floor number */
540         flags =    ( ret & 0xff00 );    /* upper 8 bits = flags        */
541
542         if (flags & IR_MAILBOX) {
543                 newroomtype = 4;        /* private mailbox */
544         }
545         else {
546                 newroomtype = 0;        /* public folder */
547         }
548
549         lprintf(7, "Create new room <%s> on floor <%d> with type <%d>\n",
550                 roomname, floornum, newroomtype);
551
552         ret = create_room(roomname, newroomtype, "", floornum, 1);
553         if (ret == 0) {
554                 cprintf("%s NO Mailbox already exists, or create failed\r\n",
555                         parms[0]);
556         }
557         else {
558                 cprintf("%s OK CREATE completed\r\n", parms[0]);
559         }
560 }
561
562
563 /*
564  * Locate a room by its IMAP folder name, and check access to it
565  */
566 int imap_grabroom(char *returned_roomname, char *foldername) {
567         int ret;
568         char augmented_roomname[ROOMNAMELEN];
569         char roomname[ROOMNAMELEN];
570         int c;
571         struct quickroom QRscratch;
572         int ra;
573         int ok = 0;
574
575         ret = imap_roomname(roomname, sizeof roomname, foldername);
576         if (ret < 0) {
577                 return(1);
578         }
579
580         /* First try a regular match */
581         c = getroom(&QRscratch, roomname);
582
583         /* Then try a mailbox name match */
584         if (c != 0) {
585                 MailboxName(augmented_roomname, &CC->usersupp, roomname);
586                 c = getroom(&QRscratch, augmented_roomname);
587                 if (c == 0)
588                         strcpy(roomname, augmented_roomname);
589         }
590
591         /* If the room exists, check security/access */
592         if (c == 0) {
593                 /* See if there is an existing user/room relationship */
594                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
595
596                 /* normal clients have to pass through security */
597                 if (ra & UA_KNOWN) {
598                         ok = 1;
599                 }
600         }
601
602         /* Fail here if no such room */
603         if (!ok) {
604                 strcpy(returned_roomname, "");
605                 return(2);
606         }
607         else {
608                 strcpy(returned_roomname, QRscratch.QRname);
609                 return(0);
610         }
611 }
612
613
614 /*
615  * Implements the STATUS command (sort of)
616  *
617  */
618 void imap_status(int num_parms, char *parms[]) {
619         int ret;
620         char roomname[ROOMNAMELEN];
621         char buf[SIZ];
622         char savedroom[ROOMNAMELEN];
623         int msgs, new;
624
625         ret = imap_grabroom(roomname, parms[2]);
626         if (ret != 0) {
627                 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
628                         parms[0]);
629                 return;
630         }
631
632         /*
633          * usergoto() formally takes us to the desired room, happily returning
634          * the number of messages and number of new messages.  (If another
635          * folder is selected, save its name so we can return there!!!!!)
636          */
637         if (IMAP->selected) {
638                 strcpy(savedroom, CC->quickroom.QRname);
639         }
640         usergoto(roomname, 0, &msgs, &new);
641
642         /*
643          * Tell the client what it wants to know.  In fact, tell it *more* than
644          * it wants to know.  We happily IGnore the supplied status data item
645          * names and simply spew all possible data items.  It's far easier to
646          * code and probably saves us some processing time too.
647          *
648          * FIXME we need to implement RECENT and UNSEEN eventually...
649          */
650
651         imap_mailboxname(buf, sizeof buf, &CC->quickroom);
652         cprintf("* STATUS ");
653         imap_strout(buf);
654         cprintf(" (MESSAGES %d RECENT 0 UIDNEXT %ld "
655                 "UIDVALIDITY 0 UNSEEN 0)\r\n",
656                 msgs,
657                 CitControl.MMhighest + 1
658         );
659
660         /*
661          * If another folder is selected, go back to that room so we can resume
662          * our happy day without violent explosions.
663          */
664         if (IMAP->selected) {
665                 usergoto(savedroom, 0, &msgs, &new);
666         }
667
668         /*
669          * Oooh, look, we're done!
670          */
671         cprintf("%s OK STATUS completed\r\n", parms[0]);
672 }
673
674
675
676
677 /* 
678  * Main command loop for IMAP sessions.
679  */
680 void imap_command_loop(void) {
681         char cmdbuf[SIZ];
682         char *parms[SIZ];
683         int num_parms;
684
685         time(&CC->lastcmd);
686         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
687         if (client_gets(cmdbuf) < 1) {
688                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
689                 CC->kill_me = 1;
690                 return;
691         }
692
693         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
694         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
695
696
697         /* strip off l/t whitespace and CRLF */
698         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
699         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
700         striplt(cmdbuf);
701
702         /* If we're in the middle of a multi-line command, handle that */
703         if (IMAP->authstate == imap_as_expecting_username) {
704                 imap_auth_login_user(cmdbuf);
705                 return;
706         }
707         if (IMAP->authstate == imap_as_expecting_password) {
708                 imap_auth_login_pass(cmdbuf);
709                 return;
710         }
711
712
713         /* Ok, at this point we're in normal command mode */
714
715         /*
716          * Before processing the command that was just entered... if we happen
717          * to have a folder selected, we'd like to rescan that folder for new
718          * messages, and for deletions/changes of existing messages.  This
719          * could probably be optimized somehow, but IMAP sucks...
720          */
721         if (IMAP->selected) {
722                 imap_rescan_msgids();
723         }
724
725         /* Now for the command set. */
726
727         /* Grab the tag, command, and parameters.  Check syntax. */
728         num_parms = imap_parameterize(parms, cmdbuf);
729         if (num_parms < 2) {
730                 cprintf("BAD syntax error\r\n");
731         }
732
733         /* The commands below may be executed in any state */
734
735         else if ( (!strcasecmp(parms[1], "NOOP"))
736            || (!strcasecmp(parms[1], "CHECK")) ) {
737                 cprintf("%s OK This command successfully did nothing.\r\n",
738                         parms[0]);
739         }
740
741         else if (!strcasecmp(parms[1], "LOGOUT")) {
742                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
743                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
744                 CC->kill_me = 1;
745                 return;
746         }
747
748         else if (!strcasecmp(parms[1], "LOGIN")) {
749                 imap_login(num_parms, parms);
750         }
751
752         else if (!strcasecmp(parms[1], "AUTHENTICATE")) {
753                 imap_authenticate(num_parms, parms);
754         }
755
756         else if (!strcasecmp(parms[1], "CAPABILITY")) {
757                 imap_capability(num_parms, parms);
758         }
759
760         else if (!CC->logged_in) {
761                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
762         }
763
764         /* The commans below require a logged-in state */
765
766         else if (!strcasecmp(parms[1], "SELECT")) {
767                 imap_select(num_parms, parms);
768         }
769
770         else if (!strcasecmp(parms[1], "EXAMINE")) {
771                 imap_select(num_parms, parms);
772         }
773
774         else if (!strcasecmp(parms[1], "LSUB")) {
775                 imap_lsub(num_parms, parms);
776         }
777
778         else if (!strcasecmp(parms[1], "LIST")) {
779                 imap_list(num_parms, parms);
780         }
781
782         else if (!strcasecmp(parms[1], "CREATE")) {
783                 imap_create(num_parms, parms);
784         }
785
786         else if (!strcasecmp(parms[1], "STATUS")) {
787                 imap_status(num_parms, parms);
788         }
789
790         else if (IMAP->selected == 0) {
791                 cprintf("%s BAD no folder selected\r\n", parms[0]);
792         }
793
794         /* The commands below require the SELECT state on a mailbox */
795
796         else if (!strcasecmp(parms[1], "FETCH")) {
797                 imap_fetch(num_parms, parms);
798         }
799
800         else if ( (!strcasecmp(parms[1], "UID"))
801                 && (!strcasecmp(parms[2], "FETCH")) ) {
802                 imap_uidfetch(num_parms, parms);
803         }
804
805         else if (!strcasecmp(parms[1], "SEARCH")) {
806                 imap_search(num_parms, parms);
807         }
808
809         else if ( (!strcasecmp(parms[1], "UID"))
810                 && (!strcasecmp(parms[2], "SEARCH")) ) {
811                 imap_uidsearch(num_parms, parms);
812         }
813
814         else if (!strcasecmp(parms[1], "STORE")) {
815                 imap_store(num_parms, parms);
816         }
817
818         else if ( (!strcasecmp(parms[1], "UID"))
819                 && (!strcasecmp(parms[2], "STORE")) ) {
820                 imap_uidstore(num_parms, parms);
821         }
822
823         else if (!strcasecmp(parms[1], "COPY")) {
824                 imap_copy(num_parms, parms);
825         }
826
827         else if ( (!strcasecmp(parms[1], "UID"))
828                 && (!strcasecmp(parms[2], "COPY")) ) {
829                 imap_uidcopy(num_parms, parms);
830         }
831
832         else if (!strcasecmp(parms[1], "EXPUNGE")) {
833                 imap_expunge(num_parms, parms);
834         }
835
836         else if (!strcasecmp(parms[1], "CLOSE")) {
837                 imap_close(num_parms, parms);
838         }
839
840         /* End of commands.  If we get here, the command is either invalid
841          * or unimplemented.
842          */
843
844         else {
845                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
846         }
847
848 }
849
850
851
852 /*
853  * This function is called by dynloader.c to register the IMAP module
854  * with the Citadel server.
855  */
856 char *Dynamic_Module_Init(void)
857 {
858         SYM_IMAP = CtdlGetDynamicSymbol();
859         CtdlRegisterServiceHook(config.c_imap_port,
860                                 NULL,
861                                 imap_greeting,
862                                 imap_command_loop);
863         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
864         return "$Id$";
865 }