* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / serv_imap.c
1 /*
2  * $Id$ 
3  *
4  * IMAP server for the Citadel/UX system
5  * Copyright (C) 2000 by Art Cancro and others.
6  * This code is released under the terms of the GNU General Public License.
7  *
8  * Current status of standards conformance:
9  *
10  *               ***  ABSOLUTELY NOTHING WORKS  ***
11  * 
12  */
13
14 #include "sysdep.h"
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <sys/wait.h>
25 #include <string.h>
26 #include <limits.h>
27 #include "citadel.h"
28 #include "server.h"
29 #include <time.h>
30 #include "sysdep_decls.h"
31 #include "citserver.h"
32 #include "support.h"
33 #include "config.h"
34 #include "dynloader.h"
35 #include "room_ops.h"
36 #include "user_ops.h"
37 #include "policy.h"
38 #include "database.h"
39 #include "msgbase.h"
40 #include "tools.h"
41 #include "internet_addressing.h"
42 #include "serv_imap.h"
43 #include "imap_tools.h"
44
45
46 long SYM_IMAP;
47
48
49 /*
50  * This cleanup function blows away the temporary memory and files used by
51  * the IMAP server.
52  */
53 void imap_cleanup_function(void) {
54
55         /* Don't do this stuff if this is not a IMAP session! */
56         if (CC->h_command_function != imap_command_loop) return;
57
58         lprintf(9, "Performing IMAP cleanup hook\n");
59
60
61         lprintf(9, "Finished IMAP cleanup hook\n");
62 }
63
64
65
66 /*
67  * Here's where our IMAP session begins its happy day.
68  */
69 void imap_greeting(void) {
70
71         strcpy(CC->cs_clientname, "IMAP session");
72         CC->internal_pgm = 1;
73         CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
74
75         cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
76                 config.c_fqdn);
77 }
78
79
80 /*
81  * implements the LOGIN command (ordinary username/password login)
82  */
83 void imap_login(int num_parms, char *parms[]) {
84         if (CtdlLoginExistingUser(parms[2]) == login_ok) {
85                 if (CtdlTryPassword(parms[3]) == pass_ok) {
86                         cprintf("%s OK login successful\r\n", parms[0]);
87                         return;
88                 }
89         }
90
91         cprintf("%s BAD Login incorrect\r\n", parms[0]);
92 }
93
94
95 /*
96  * implements the CAPABILITY command
97  */
98 void imap_capability(int num_parms, char *parms[]) {
99         cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
100         cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
101 }
102
103
104
105
106
107 /*
108  * implements the SELECT command
109  */
110 void imap_select(int num_parms, char *parms[]) {
111         char towhere[256];
112         char augmented_roomname[256];
113         int c = 0;
114         int ok = 0;
115         int ra = 0;
116         struct quickroom QRscratch;
117         int msgs, new;
118
119         strcpy(towhere, parms[2]);
120
121         /* IMAP uses the reserved name "INBOX" for the user's default incoming
122          * mail folder.  Convert this to Citadel's reserved name "_MAIL_".
123          */
124         if (!strcasecmp(towhere, "INBOX"))
125                 strcpy(towhere, MAILROOM);
126
127         /* First try a regular match */
128         c = getroom(&QRscratch, towhere);
129
130         /* Then try a mailbox name match */
131         if (c != 0) {
132                 MailboxName(augmented_roomname, &CC->usersupp, towhere);
133                 c = getroom(&QRscratch, augmented_roomname);
134                 if (c == 0)
135                         strcpy(towhere, augmented_roomname);
136         }
137
138         /* If the room exists, check security/access */
139         if (c == 0) {
140                 /* See if there is an existing user/room relationship */
141                 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
142
143                 /* normal clients have to pass through security */
144                 if (ra & UA_KNOWN)
145                         ok = 1;
146         }
147
148         /* Fail here if no such room */
149         if (!ok) {
150                 cprintf("%s NO ... no such room, or access denied\r\n",
151                         parms[0]);
152                 IMAP->selected = 0;
153                 return;
154         }
155
156         /*
157          * usergoto() formally takes us to the desired room, happily returning
158          * the number of messages and number of new messages.
159          */
160         usergoto(QRscratch.QRname, 0, &msgs, &new);
161
162         cprintf("* %d EXISTS\r\n", msgs);
163         cprintf("* %d RECENT\r\n", new);
164         cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
165         cprintf("%s OK [FIXME] SELECT completed\r\n", parms[0]);
166 }
167
168
169 /* 
170  * Main command loop for IMAP sessions.
171  */
172 void imap_command_loop(void) {
173         char cmdbuf[256];
174         char *parms[16];
175         int num_parms;
176         int i;
177
178         time(&CC->lastcmd);
179         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
180         if (client_gets(cmdbuf) < 1) {
181                 lprintf(3, "IMAP socket is broken.  Ending session.\r\n");
182                 CC->kill_me = 1;
183                 return;
184         }
185
186         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
187         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
188
189
190         /* strip off l/t whitespace and CRLF */
191         if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
192         if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
193         striplt(cmdbuf);
194
195         /* grab the tag */
196         num_parms = imap_parameterize(parms, cmdbuf);
197         for (i=0; i<num_parms; ++i) {
198                 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
199         }
200
201         /* commands which may be executed in any state */
202
203         if (!strcasecmp(parms[1], "NOOP")) {
204                 cprintf("%s OK This command successfully did nothing.\r\n",
205                         parms[0]);
206         }
207
208         else if (!strcasecmp(parms[1], "LOGOUT")) {
209                 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
210                 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
211                 CC->kill_me = 1;
212                 return;
213         }
214
215         else if (!strcasecmp(parms[1], "LOGIN")) {
216                 imap_login(num_parms, parms);
217         }
218
219         else if (!strcasecmp(parms[1], "CAPABILITY")) {
220                 imap_capability(num_parms, parms);
221         }
222
223         else if (!CC->logged_in) {
224                 cprintf("%s BAD Not logged in.\r\n", parms[0]);
225         }
226
227         /*  commands requiring the client to be logged in */
228
229         else if (!strcasecmp(parms[1], "SELECT")) {
230                 imap_select(num_parms, parms);
231         }
232
233         /* end of commands */
234
235         else {
236                 cprintf("%s BAD command unrecognized\r\n", parms[0]);
237         }
238
239 }
240
241
242
243 char *Dynamic_Module_Init(void)
244 {
245         SYM_IMAP = CtdlGetDynamicSymbol();
246         CtdlRegisterServiceHook(2243,   /* FIXME put in config setup */
247                                 NULL,
248                                 imap_greeting,
249                                 imap_command_loop);
250         CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
251         return "$Id$";
252 }