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 * Current status of standards conformance:
10 * *** ABSOLUTELY NOTHING WORKS ***
22 #include <sys/types.h>
30 #include "sysdep_decls.h"
31 #include "citserver.h"
34 #include "dynloader.h"
41 #include "internet_addressing.h"
42 #include "serv_imap.h"
43 #include "imap_tools.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 Citadel's reserved name "_MAIL_".
177 if (!strcasecmp(towhere, "INBOX"))
178 strcpy(towhere, MAILROOM);
180 /* First try a regular match */
181 c = getroom(&QRscratch, towhere);
183 /* Then try a mailbox name match */
185 MailboxName(augmented_roomname, &CC->usersupp, towhere);
186 c = getroom(&QRscratch, augmented_roomname);
188 strcpy(towhere, augmented_roomname);
191 /* If the room exists, check security/access */
193 /* See if there is an existing user/room relationship */
194 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
196 /* normal clients have to pass through security */
201 /* Fail here if no such room */
203 cprintf("%s NO ... no such room, or access denied\r\n",
210 * usergoto() formally takes us to the desired room, happily returning
211 * the number of messages and number of new messages.
213 usergoto(QRscratch.QRname, 0, &msgs, &new);
216 if (!strcasecmp(parms[1], "EXAMINE")) {
225 /* FIXME ... much more info needs to be supplied here */
226 cprintf("* %d EXISTS\r\n", msgs);
227 cprintf("* %d RECENT\r\n", new);
228 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
229 cprintf("%s OK [%s] %s completed\r\n",
231 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
238 * implements the CLOSE command
240 void imap_close(int num_parms, char *parms[]) {
244 cprintf("%s OK CLOSE completed\r\n", parms[0]);
252 * Back end for imap_lsub()
254 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
257 imap_mailboxname(buf, sizeof buf, qrbuf);
258 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
263 * Implements the LSUB command
265 * FIXME: Handle wildcards, please.
266 * FIXME: Currently we show all rooms as subscribed folders. Need to handle
267 * subscriptions properly.
269 void imap_lsub(int num_parms, char *parms[]) {
270 ForEachRoom(imap_lsub_listroom, NULL);
271 cprintf("%s OK LSUB completed\r\n", parms[0]);
277 * Back end for imap_list()
279 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
282 imap_mailboxname(buf, sizeof buf, qrbuf);
283 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
288 * Implements the LIST command
290 * FIXME: Handle wildcards, please.
292 void imap_list(int num_parms, char *parms[]) {
293 ForEachRoom(imap_list_listroom, NULL);
294 cprintf("%s OK LIST completed\r\n", parms[0]);
300 * Main command loop for IMAP sessions.
302 void imap_command_loop(void) {
309 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
310 if (client_gets(cmdbuf) < 1) {
311 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
316 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
317 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
320 /* strip off l/t whitespace and CRLF */
321 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
322 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
326 num_parms = imap_parameterize(parms, cmdbuf);
327 for (i=0; i<num_parms; ++i) {
328 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
331 /* commands which may be executed in any state */
333 if ( (!strcasecmp(parms[1], "NOOP"))
334 || (!strcasecmp(parms[1], "CHECK")) ) {
335 cprintf("%s OK This command successfully did nothing.\r\n",
339 else if (!strcasecmp(parms[1], "LOGOUT")) {
340 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
341 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
346 else if (!strcasecmp(parms[1], "LOGIN")) {
347 imap_login(num_parms, parms);
350 else if (!strcasecmp(parms[1], "CAPABILITY")) {
351 imap_capability(num_parms, parms);
354 else if (!CC->logged_in) {
355 cprintf("%s BAD Not logged in.\r\n", parms[0]);
358 /* commands requiring the client to be logged in */
360 else if (!strcasecmp(parms[1], "SELECT")) {
361 imap_select(num_parms, parms);
364 else if (!strcasecmp(parms[1], "EXAMINE")) {
365 imap_select(num_parms, parms);
368 else if (!strcasecmp(parms[1], "LSUB")) {
369 imap_lsub(num_parms, parms);
372 else if (!strcasecmp(parms[1], "LIST")) {
373 imap_list(num_parms, parms);
376 else if (IMAP->selected == 0) {
377 cprintf("%s BAD no folder selected\r\n", parms[0]);
380 /* commands requiring the SELECT state */
382 else if (!strcasecmp(parms[1], "CLOSE")) {
383 imap_close(num_parms, parms);
386 /* end of commands */
389 cprintf("%s BAD command unrecognized\r\n", parms[0]);
396 char *Dynamic_Module_Init(void)
398 SYM_IMAP = CtdlGetDynamicSymbol();
399 CtdlRegisterServiceHook(143, /* FIXME put in config setup */
403 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);