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) {
62 * Back end for imap_load_msgids()
64 * FIXME: this should be optimized by figuring out a way to allocate memory
65 * once rather than doing a reallok() for each message.
67 void imap_add_single_msgid(long msgnum, void *userdata) {
69 IMAP->num_msgs = IMAP->num_msgs + 1;
70 if (IMAP->msgids == NULL) {
71 IMAP->msgids = mallok(IMAP->num_msgs * sizeof(long));
74 IMAP->msgids = reallok(IMAP->msgids,
75 IMAP->num_msgs * sizeof(long));
77 IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
83 * Set up a message ID map for the current room (folder)
85 void imap_load_msgids(void) {
87 if (IMAP->selected == 0) {
88 lprintf(5, "imap_load_msgids() can't run; no room selected\n");
92 imap_free_msgids(); /* If there was already a map, free it */
94 CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL,
95 imap_add_single_msgid, NULL);
97 lprintf(9, "imap_load_msgids() mapped %d messages\n", IMAP->num_msgs);
104 * This cleanup function blows away the temporary memory and files used by
107 void imap_cleanup_function(void) {
109 /* Don't do this stuff if this is not a IMAP session! */
110 if (CC->h_command_function != imap_command_loop) return;
112 lprintf(9, "Performing IMAP cleanup hook\n");
114 lprintf(9, "Finished IMAP cleanup hook\n");
120 * Here's where our IMAP session begins its happy day.
122 void imap_greeting(void) {
124 strcpy(CC->cs_clientname, "IMAP session");
125 CC->internal_pgm = 1;
126 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
128 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
134 * implements the LOGIN command (ordinary username/password login)
136 void imap_login(int num_parms, char *parms[]) {
137 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
138 if (CtdlTryPassword(parms[3]) == pass_ok) {
139 cprintf("%s OK login successful\r\n", parms[0]);
144 cprintf("%s BAD Login incorrect\r\n", parms[0]);
149 * implements the CAPABILITY command
151 void imap_capability(int num_parms, char *parms[]) {
152 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
153 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
161 * implements the SELECT command
163 void imap_select(int num_parms, char *parms[]) {
165 char augmented_roomname[256];
169 struct quickroom QRscratch;
172 strcpy(towhere, parms[2]);
174 /* IMAP uses the reserved name "INBOX" for the user's default incoming
175 * mail folder. Convert this to whatever Citadel is using for the
176 * default mail room name (usually "Mail>").
178 if (!strcasecmp(towhere, "INBOX")) {
179 strcpy(towhere, MAILROOM);
182 /* First try a regular match */
183 c = getroom(&QRscratch, towhere);
185 /* Then try a mailbox name match */
187 MailboxName(augmented_roomname, &CC->usersupp, towhere);
188 c = getroom(&QRscratch, augmented_roomname);
190 strcpy(towhere, augmented_roomname);
193 /* If the room exists, check security/access */
195 /* See if there is an existing user/room relationship */
196 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
198 /* normal clients have to pass through security */
204 /* Fail here if no such room */
206 cprintf("%s NO ... no such room, or access denied\r\n",
213 * usergoto() formally takes us to the desired room, happily returning
214 * the number of messages and number of new messages.
216 usergoto(QRscratch.QRname, 0, &msgs, &new);
219 if (!strcasecmp(parms[1], "EXAMINE")) {
228 /* FIXME ... much more info needs to be supplied here */
229 cprintf("* %d EXISTS\r\n", msgs);
230 cprintf("* %d RECENT\r\n", new);
231 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
232 cprintf("%s OK [%s] %s completed\r\n",
234 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
241 * implements the CLOSE command
243 void imap_close(int num_parms, char *parms[]) {
247 cprintf("%s OK CLOSE completed\r\n", parms[0]);
255 * Back end for imap_lsub()
257 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
260 imap_mailboxname(buf, sizeof buf, qrbuf);
261 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
266 * Implements the LSUB command
268 * FIXME: Handle wildcards, please.
269 * FIXME: Currently we show all rooms as subscribed folders. Need to handle
270 * subscriptions properly.
272 void imap_lsub(int num_parms, char *parms[]) {
273 ForEachRoom(imap_lsub_listroom, NULL);
274 cprintf("%s OK LSUB completed\r\n", parms[0]);
280 * Back end for imap_list()
282 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
285 imap_mailboxname(buf, sizeof buf, qrbuf);
286 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
291 * Implements the LIST command
293 * FIXME: Handle wildcards, please.
295 void imap_list(int num_parms, char *parms[]) {
296 ForEachRoom(imap_list_listroom, NULL);
297 cprintf("%s OK LIST completed\r\n", parms[0]);
303 * Main command loop for IMAP sessions.
305 void imap_command_loop(void) {
312 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
313 if (client_gets(cmdbuf) < 1) {
314 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
319 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
320 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
323 /* strip off l/t whitespace and CRLF */
324 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
325 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
329 num_parms = imap_parameterize(parms, cmdbuf);
330 for (i=0; i<num_parms; ++i) {
331 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
334 /* commands which may be executed in any state */
336 if ( (!strcasecmp(parms[1], "NOOP"))
337 || (!strcasecmp(parms[1], "CHECK")) ) {
338 cprintf("%s OK This command successfully did nothing.\r\n",
342 else if (!strcasecmp(parms[1], "LOGOUT")) {
343 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
344 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
349 else if (!strcasecmp(parms[1], "LOGIN")) {
350 imap_login(num_parms, parms);
353 else if (!strcasecmp(parms[1], "CAPABILITY")) {
354 imap_capability(num_parms, parms);
357 else if (!CC->logged_in) {
358 cprintf("%s BAD Not logged in.\r\n", parms[0]);
361 /* commands requiring the client to be logged in */
363 else if (!strcasecmp(parms[1], "SELECT")) {
364 imap_select(num_parms, parms);
367 else if (!strcasecmp(parms[1], "EXAMINE")) {
368 imap_select(num_parms, parms);
371 else if (!strcasecmp(parms[1], "LSUB")) {
372 imap_lsub(num_parms, parms);
375 else if (!strcasecmp(parms[1], "LIST")) {
376 imap_list(num_parms, parms);
379 else if (IMAP->selected == 0) {
380 cprintf("%s BAD no folder selected\r\n", parms[0]);
383 /* commands requiring the SELECT state */
385 else if (!strcasecmp(parms[1], "FETCH")) {
386 imap_fetch(num_parms, parms);
389 else if (!strcasecmp(parms[1], "CLOSE")) {
390 imap_close(num_parms, parms);
393 /* end of commands */
396 cprintf("%s BAD command unrecognized\r\n", parms[0]);
403 char *Dynamic_Module_Init(void)
405 SYM_IMAP = CtdlGetDynamicSymbol();
406 CtdlRegisterServiceHook(config.c_imap_port,
410 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);