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"
43 #include "imap_fetch.h"
50 * If there is a message ID map in memory, free it
52 void imap_free_msgids(void) {
53 if (IMAP->msgids != NULL) {
58 if (IMAP->flags != NULL) {
66 * Back end for imap_load_msgids()
68 * FIXME: this should be optimized by figuring out a way to allocate memory
69 * once rather than doing a reallok() for each message.
71 void imap_add_single_msgid(long msgnum, void *userdata) {
73 IMAP->num_msgs = IMAP->num_msgs + 1;
74 if (IMAP->msgids == NULL) {
75 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
78 IMAP->msgids = reallok(IMAP->msgids,
79 IMAP->num_msgs * sizeof(long));
81 if (IMAP->flags == NULL) {
82 IMAP->flags = mallok(IMAP->num_msgs * sizeof(long));
85 IMAP->flags = reallok(IMAP->flags,
86 IMAP->num_msgs * sizeof(long));
88 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
89 IMAP->flags[IMAP->num_msgs - 1] = 0;
95 * Set up a message ID map for the current room (folder)
97 void imap_load_msgids(void) {
99 if (IMAP->selected == 0) {
100 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
104 imap_free_msgids(); /* If there was already a map, free it */
106 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
107 imap_add_single_msgid, NULL);
109 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
116 * This cleanup function blows away the temporary memory and files used by
119 void imap_cleanup_function(void) {
121 /* Don't do this stuff if this is not a IMAP session! */
122 if (CC->h_command_function != imap_command_loop) return;
124 lprintf(9, "Performing IMAP cleanup hook\n");
126 lprintf(9, "Finished IMAP cleanup hook\n");
132 * Here's where our IMAP session begins its happy day.
134 void imap_greeting(void) {
136 strcpy(CC->cs_clientname, "IMAP session");
137 CC->internal_pgm = 1;
138 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
140 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
146 * implements the LOGIN command (ordinary username/password login)
148 void imap_login(int num_parms, char *parms[]) {
149 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
150 if (CtdlTryPassword(parms[3]) == pass_ok) {
151 cprintf("%s OK login successful\r\n", parms[0]);
156 cprintf("%s BAD Login incorrect\r\n", parms[0]);
161 * implements the CAPABILITY command
163 void imap_capability(int num_parms, char *parms[]) {
164 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
165 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
173 * implements the SELECT command
175 void imap_select(int num_parms, char *parms[]) {
177 char augmented_roomname[256];
181 struct quickroom QRscratch;
184 strcpy(towhere, parms[2]);
186 /* IMAP uses the reserved name "INBOX" for the user's default incoming
187 * mail folder. Convert this to whatever Citadel is using for the
188 * default mail room name (usually "Mail>").
190 if (!strcasecmp(towhere, "INBOX")) {
191 strcpy(towhere, MAILROOM);
194 /* First try a regular match */
195 c = getroom(&QRscratch, towhere);
197 /* Then try a mailbox name match */
199 MailboxName(augmented_roomname, &CC->usersupp, towhere);
200 c = getroom(&QRscratch, augmented_roomname);
202 strcpy(towhere, augmented_roomname);
205 /* If the room exists, check security/access */
207 /* See if there is an existing user/room relationship */
208 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
210 /* normal clients have to pass through security */
216 /* Fail here if no such room */
218 cprintf("%s NO ... no such room, or access denied\r\n",
225 * usergoto() formally takes us to the desired room, happily returning
226 * the number of messages and number of new messages.
228 usergoto(QRscratch.QRname, 0, &msgs, &new);
231 if (!strcasecmp(parms[1], "EXAMINE")) {
240 /* FIXME ... much more info needs to be supplied here */
241 cprintf("* %d EXISTS\r\n", msgs);
242 cprintf("* %d RECENT\r\n", new);
243 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
244 cprintf("%s OK [%s] %s completed\r\n",
246 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
253 * implements the CLOSE command
255 void imap_close(int num_parms, char *parms[]) {
259 cprintf("%s OK CLOSE completed\r\n", parms[0]);
267 * Back end for imap_lsub()
269 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
272 imap_mailboxname(buf, sizeof buf, qrbuf);
273 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
278 * Implements the LSUB command
280 * FIXME: Handle wildcards, please.
281 * FIXME: Currently we show all rooms as subscribed folders. Need to handle
282 * subscriptions properly.
284 void imap_lsub(int num_parms, char *parms[]) {
285 ForEachRoom(imap_lsub_listroom, NULL);
286 cprintf("%s OK LSUB completed\r\n", parms[0]);
292 * Back end for imap_list()
294 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
297 imap_mailboxname(buf, sizeof buf, qrbuf);
298 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
303 * Implements the LIST command
305 * FIXME: Handle wildcards, please.
307 void imap_list(int num_parms, char *parms[]) {
308 ForEachRoom(imap_list_listroom, NULL);
309 cprintf("%s OK LIST completed\r\n", parms[0]);
315 * Main command loop for IMAP sessions.
317 void imap_command_loop(void) {
324 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
325 if (client_gets(cmdbuf) < 1) {
326 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
331 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
332 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
335 /* strip off l/t whitespace and CRLF */
336 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
337 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
341 num_parms = imap_parameterize(parms, cmdbuf);
342 for (i=0; i<num_parms; ++i) {
343 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
346 /* commands which may be executed in any state */
348 if ( (!strcasecmp(parms[1], "NOOP"))
349 || (!strcasecmp(parms[1], "CHECK")) ) {
350 cprintf("%s OK This command successfully did nothing.\r\n",
354 else if (!strcasecmp(parms[1], "LOGOUT")) {
355 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
356 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
361 else if (!strcasecmp(parms[1], "LOGIN")) {
362 imap_login(num_parms, parms);
365 else if (!strcasecmp(parms[1], "CAPABILITY")) {
366 imap_capability(num_parms, parms);
369 else if (!CC->logged_in) {
370 cprintf("%s BAD Not logged in.\r\n", parms[0]);
373 /* commands requiring the client to be logged in */
375 else if (!strcasecmp(parms[1], "SELECT")) {
376 imap_select(num_parms, parms);
379 else if (!strcasecmp(parms[1], "EXAMINE")) {
380 imap_select(num_parms, parms);
383 else if (!strcasecmp(parms[1], "LSUB")) {
384 imap_lsub(num_parms, parms);
387 else if (!strcasecmp(parms[1], "LIST")) {
388 imap_list(num_parms, parms);
391 else if (IMAP->selected == 0) {
392 cprintf("%s BAD no folder selected\r\n", parms[0]);
395 /* commands requiring the SELECT state */
397 else if (!strcasecmp(parms[1], "FETCH")) {
398 imap_fetch(num_parms, parms);
401 else if ( (!strcasecmp(parms[1], "UID"))
402 && (!strcasecmp(parms[2], "FETCH")) ) {
403 imap_uidfetch(num_parms, parms);
407 else if (!strcasecmp(parms[1], "CLOSE")) {
408 imap_close(num_parms, parms);
411 /* end of commands */
414 cprintf("%s BAD command unrecognized\r\n", parms[0]);
421 char *Dynamic_Module_Init(void)
423 SYM_IMAP = CtdlGetDynamicSymbol();
424 CtdlRegisterServiceHook(config.c_imap_port,
428 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);