]> code.citadel.org Git - citadel.git/blob - citadel/serv_imap.c
* worked on imap
[citadel.git] / citadel / serv_imap.c
1 /* $Id$ 
2  *
3  * IMAP server for the Citadel/UX system
4  * Copyright (C) 2000 by Art Cancro and others.
5  * This code is released under the terms of the GNU General Public License.
6  *
7  * Current status of standards conformance:
8  *
9  *               ***  ABSOLUTELY NOTHING WORKS  ***
10  * 
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 #include <sys/time.h>
23 #include <sys/wait.h>
24 #include <string.h>
25 #include <limits.h>
26 #include "citadel.h"
27 #include "server.h"
28 #include <time.h>
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "dynloader.h"
34 #include "room_ops.h"
35 #include "user_ops.h"
36 #include "policy.h"
37 #include "database.h"
38 #include "msgbase.h"
39 #include "tools.h"
40 #include "internet_addressing.h"
41 #include "serv_imap.h"
42
43
44 long SYM_IMAP;
45
46
47 /*
48  * This cleanup function blows away the temporary memory and files used by
49  * the IMAP server.
50  */
51 void imap_cleanup_function(void) {
52
53         /* Don't do this stuff if this is not a IMAP session! */
54         if (CC->h_command_function != imap_command_loop) return;
55
56         lprintf(9, "Performing IMAP cleanup hook\n");
57
58
59         lprintf(9, "Finished IMAP cleanup hook\n");
60 }
61
62
63
64 /*
65  * Here's where our IMAP session begins its happy day.
66  */
67 void imap_greeting(void) {
68
69         strcpy(CC->cs_clientname, "IMAP session");
70         CC->internal_pgm = 1;
71         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
72
73         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
74                 config.c_fqdn);
75 }
76
77
78
79
80 /* 
81  * Main command loop for IMAP sessions.
82  */
83 void imap_command_loop(void) {
84         char cmdbuf[256];
85         char tag[256];
86
87         time(&CC->lastcmd);
88         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
89         if (client_gets(cmdbuf) < 1) {
90                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
91                 CC->kill_me = 1;
92                 return;
93         }
94
95         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
96         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
97
98
99         /* strip off l/t whitespace and CRLF */
100         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
101         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
102         striplt(cmdbuf);
103
104         /* grab the tag */
105         extract_token(tag, cmdbuf, 0, ' ');
106         remove_token(cmdbuf, 0, ' ');
107         lprintf(9, "tag=<%s> cmd=<%s>\n", tag, cmdbuf);
108
109         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
110                 cprintf("%s OK This command successfully did nothing.\r\n",
111                         tag);
112         }
113
114         else if (!strncasecmp(cmdbuf, "LOGOUT", 4)) {
115                 cprintf("%s OK thank you for using Citadel IMAP\r\n", tag);
116                 CC->kill_me = 1;
117                 return;
118         }
119
120         /*   FIXME   ...   implement login commands HERE      */
121
122         else if (!CC->logged_in) {
123                 cprintf("%s BAD Not logged in.\r\n", tag);
124         }
125
126         /*    FIXME    ...   implement commands requiring login here   */
127
128         else {
129                 cprintf("%s BAD command unrecognized\r\n", tag);
130         }
131
132 }
133
134
135
136 char *Dynamic_Module_Init(void)
137 {
138         SYM_IMAP = CtdlGetDynamicSymbol();
139         CtdlRegisterServiceHook(143,    /* FIXME put in config setup */
140                                 NULL,
141                                 imap_greeting,
142                                 imap_command_loop);
143         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
144         return "$Id$";
145 }