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.
8 * *** THIS IS UNDER DEVELOPMENT. IT DOES NOT WORK. DO NOT USE IT. ***
20 #include <sys/types.h>
29 #include "sysdep_decls.h"
30 #include "citserver.h"
33 #include "dynloader.h"
40 #include "internet_addressing.h"
41 #include "serv_imap.h"
42 #include "imap_tools.h"
49 * If there is a message ID map in memory, free it
51 void imap_free_msgids(void) {
52 if (IMAP->msgids != NULL) {
61 * Back end for imap_load_msgids()
63 * FIXME: this should be optimized by figuring out a way to allocate memory
64 * once rather than doing a reallok() for each message.
66 void imap_add_single_msgid(long msgnum, void *userdata) {
68 IMAP->num_msgs = IMAP->num_msgs + 1;
69 if (IMAP->msgids == NULL) {
70 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
73 IMAP->msgids = reallok(IMAP->msgids,
74 IMAP->num_msgs * sizeof(long));
76 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
82 * Set up a message ID map for the current room (folder)
84 void imap_load_msgids(void) {
86 if (IMAP->selected == 0) {
87 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
91 imap_free_msgids(); /* If there was already a map, free it */
93 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
94 imap_add_single_msgid, NULL);
96 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
103 * This cleanup function blows away the temporary memory and files used by
106 void imap_cleanup_function(void) {
108 /* Don't do this stuff if this is not a IMAP session! */
109 if (CC->h_command_function != imap_command_loop) return;
111 lprintf(9, "Performing IMAP cleanup hook\n");
113 lprintf(9, "Finished IMAP cleanup hook\n");
119 * Here's where our IMAP session begins its happy day.
121 void imap_greeting(void) {
123 strcpy(CC->cs_clientname, "IMAP session");
124 CC->internal_pgm = 1;
125 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
127 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
133 * implements the LOGIN command (ordinary username/password login)
135 void imap_login(int num_parms, char *parms[]) {
136 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
137 if (CtdlTryPassword(parms[3]) == pass_ok) {
138 cprintf("%s OK login successful\r\n", parms[0]);
143 cprintf("%s BAD Login incorrect\r\n", parms[0]);
148 * implements the CAPABILITY command
150 void imap_capability(int num_parms, char *parms[]) {
151 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
152 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
160 * implements the SELECT command
162 void imap_select(int num_parms, char *parms[]) {
164 char augmented_roomname[256];
168 struct quickroom QRscratch;
171 strcpy(towhere, parms[2]);
173 /* IMAP uses the reserved name "INBOX" for the user's default incoming
174 * mail folder. Convert this to Citadel's reserved name "_MAIL_".
176 if (!strcasecmp(towhere, "INBOX"))
177 strcpy(towhere, MAILROOM);
179 /* First try a regular match */
180 c = getroom(&QRscratch, towhere);
182 /* Then try a mailbox name match */
184 MailboxName(augmented_roomname, &CC->usersupp, towhere);
185 c = getroom(&QRscratch, augmented_roomname);
187 strcpy(towhere, augmented_roomname);
190 /* If the room exists, check security/access */
192 /* See if there is an existing user/room relationship */
193 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
195 /* normal clients have to pass through security */
200 /* Fail here if no such room */
202 cprintf("%s NO ... no such room, or access denied\r\n",
209 * usergoto() formally takes us to the desired room, happily returning
210 * the number of messages and number of new messages.
212 usergoto(QRscratch.QRname, 0, &msgs, &new);
215 if (!strcasecmp(parms[1], "EXAMINE")) {
224 /* FIXME ... much more info needs to be supplied here */
225 cprintf("* %d EXISTS\r\n", msgs);
226 cprintf("* %d RECENT\r\n", new);
227 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
228 cprintf("%s OK [%s] %s completed\r\n",
230 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
237 * implements the CLOSE command
239 void imap_close(int num_parms, char *parms[]) {
243 cprintf("%s OK CLOSE completed\r\n", parms[0]);
251 * Back end for imap_lsub()
253 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
256 imap_mailboxname(buf, sizeof buf, qrbuf);
257 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
262 * Implements the LSUB command
264 * FIXME: Handle wildcards, please.
265 * FIXME: Currently we show all rooms as subscribed folders. Need to handle
266 * subscriptions properly.
268 void imap_lsub(int num_parms, char *parms[]) {
269 ForEachRoom(imap_lsub_listroom, NULL);
270 cprintf("%s OK LSUB completed\r\n", parms[0]);
276 * Back end for imap_list()
278 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
281 imap_mailboxname(buf, sizeof buf, qrbuf);
282 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
287 * Implements the LIST command
289 * FIXME: Handle wildcards, please.
291 void imap_list(int num_parms, char *parms[]) {
292 ForEachRoom(imap_list_listroom, NULL);
293 cprintf("%s OK LIST completed\r\n", parms[0]);
298 * Do the actual work for imap_fetch(). By the time this function is called,
299 * the "lo" and "hi" sequence numbers are already validated, and the data item
300 * names are, um... something.
302 void imap_do_fetch(int lo, int hi, char *items) {
303 cprintf("* lo=%d hi=%d items=<%s>\r\n", lo, hi, items);
308 * Mark Crispin is a fscking idiot.
310 void imap_fetch(int num_parms, char *parms[]) {
313 char lostr[1024], histr[1024], items[1024];
317 cprintf("%s BAD invalid parameters\r\n", parms[0]);
321 extract_token(lostr, parms[2], 0, ':');
323 extract_token(histr, parms[2], 1, ':');
326 if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
327 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
333 for (i=3; i<num_parms; ++i) {
334 strcat(items, parms[i]);
335 if (i < (num_parms-1)) strcat(items, " ");
337 for (i=0; i<strlen(items); ++i) {
338 if (isspace(items[i])) items[i] = ' ';
341 imap_do_fetch(lo, hi, items);
342 cprintf("%s OK FETCH completed\r\n", parms[0]);
349 * Main command loop for IMAP sessions.
351 void imap_command_loop(void) {
358 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
359 if (client_gets(cmdbuf) < 1) {
360 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
365 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
366 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
369 /* strip off l/t whitespace and CRLF */
370 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
371 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
375 num_parms = imap_parameterize(parms, cmdbuf);
376 for (i=0; i<num_parms; ++i) {
377 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
380 /* commands which may be executed in any state */
382 if ( (!strcasecmp(parms[1], "NOOP"))
383 || (!strcasecmp(parms[1], "CHECK")) ) {
384 cprintf("%s OK This command successfully did nothing.\r\n",
388 else if (!strcasecmp(parms[1], "LOGOUT")) {
389 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
390 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
395 else if (!strcasecmp(parms[1], "LOGIN")) {
396 imap_login(num_parms, parms);
399 else if (!strcasecmp(parms[1], "CAPABILITY")) {
400 imap_capability(num_parms, parms);
403 else if (!CC->logged_in) {
404 cprintf("%s BAD Not logged in.\r\n", parms[0]);
407 /* commands requiring the client to be logged in */
409 else if (!strcasecmp(parms[1], "SELECT")) {
410 imap_select(num_parms, parms);
413 else if (!strcasecmp(parms[1], "EXAMINE")) {
414 imap_select(num_parms, parms);
417 else if (!strcasecmp(parms[1], "LSUB")) {
418 imap_lsub(num_parms, parms);
421 else if (!strcasecmp(parms[1], "LIST")) {
422 imap_list(num_parms, parms);
425 else if (IMAP->selected == 0) {
426 cprintf("%s BAD no folder selected\r\n", parms[0]);
429 /* commands requiring the SELECT state */
431 else if (!strcasecmp(parms[1], "FETCH")) {
432 imap_fetch(num_parms, parms);
435 else if (!strcasecmp(parms[1], "CLOSE")) {
436 imap_close(num_parms, parms);
439 /* end of commands */
442 cprintf("%s BAD command unrecognized\r\n", parms[0]);
449 char *Dynamic_Module_Init(void)
451 SYM_IMAP = CtdlGetDynamicSymbol();
452 CtdlRegisterServiceHook(143, /* FIXME put in config setup */
456 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);