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