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 whatever Citadel is using for the
175 * default mail room name (usually "Mail>").
177 if (!strcasecmp(towhere, "INBOX")) {
178 strcpy(towhere, MAILROOM);
181 /* First try a regular match */
182 c = getroom(&QRscratch, towhere);
184 /* Then try a mailbox name match */
186 MailboxName(augmented_roomname, &CC->usersupp, towhere);
187 c = getroom(&QRscratch, augmented_roomname);
189 strcpy(towhere, augmented_roomname);
192 /* If the room exists, check security/access */
194 /* See if there is an existing user/room relationship */
195 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
197 /* normal clients have to pass through security */
203 /* Fail here if no such room */
205 cprintf("%s NO ... no such room, or access denied\r\n",
212 * usergoto() formally takes us to the desired room, happily returning
213 * the number of messages and number of new messages.
215 usergoto(QRscratch.QRname, 0, &msgs, &new);
218 if (!strcasecmp(parms[1], "EXAMINE")) {
227 /* FIXME ... much more info needs to be supplied here */
228 cprintf("* %d EXISTS\r\n", msgs);
229 cprintf("* %d RECENT\r\n", new);
230 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
231 cprintf("%s OK [%s] %s completed\r\n",
233 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
240 * implements the CLOSE command
242 void imap_close(int num_parms, char *parms[]) {
246 cprintf("%s OK CLOSE completed\r\n", parms[0]);
254 * Back end for imap_lsub()
256 void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
259 imap_mailboxname(buf, sizeof buf, qrbuf);
260 cprintf("* LSUB () \"|\" \"%s\"\r\n", buf);
265 * Implements the LSUB command
267 * FIXME: Handle wildcards, please.
268 * FIXME: Currently we show all rooms as subscribed folders. Need to handle
269 * subscriptions properly.
271 void imap_lsub(int num_parms, char *parms[]) {
272 ForEachRoom(imap_lsub_listroom, NULL);
273 cprintf("%s OK LSUB completed\r\n", parms[0]);
279 * Back end for imap_list()
281 void imap_list_listroom(struct quickroom *qrbuf, void *data) {
284 imap_mailboxname(buf, sizeof buf, qrbuf);
285 cprintf("* LIST () \"|\" \"%s\"\r\n", buf);
290 * Implements the LIST command
292 * FIXME: Handle wildcards, please.
294 void imap_list(int num_parms, char *parms[]) {
295 ForEachRoom(imap_list_listroom, NULL);
296 cprintf("%s OK LIST completed\r\n", parms[0]);
301 * Do the actual work for imap_fetch(). By the time this function is called,
302 * the "lo" and "hi" sequence numbers are already validated, and the data item
303 * names are, um... something.
305 void imap_do_fetch(int lo, int hi, char *items) {
306 cprintf("* lo=%d hi=%d items=<%s>\r\n", lo, hi, items);
310 * Mark Crispin is a fscking idiot.
312 void imap_fetch(int num_parms, char *parms[]) {
315 char lostr[1024], histr[1024], items[1024];
319 cprintf("%s BAD invalid parameters\r\n", parms[0]);
323 extract_token(lostr, parms[2], 0, ':');
325 extract_token(histr, parms[2], 1, ':');
328 if ( (lo < 1) || (hi < 1) || (lo > hi) || (hi > IMAP->num_msgs) ) {
329 cprintf("%s BAD invalid sequence numbers %d:%d\r\n",
335 for (i=3; i<num_parms; ++i) {
336 strcat(items, parms[i]);
337 if (i < (num_parms-1)) strcat(items, " ");
340 imap_extract_data_items(items);
342 imap_do_fetch(lo, hi, items);
343 cprintf("%s OK FETCH completed\r\n", parms[0]);
350 * Main command loop for IMAP sessions.
352 void imap_command_loop(void) {
359 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
360 if (client_gets(cmdbuf) < 1) {
361 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
366 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
367 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
370 /* strip off l/t whitespace and CRLF */
371 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
372 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
376 num_parms = imap_parameterize(parms, cmdbuf);
377 for (i=0; i<num_parms; ++i) {
378 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
381 /* commands which may be executed in any state */
383 if ( (!strcasecmp(parms[1], "NOOP"))
384 || (!strcasecmp(parms[1], "CHECK")) ) {
385 cprintf("%s OK This command successfully did nothing.\r\n",
389 else if (!strcasecmp(parms[1], "LOGOUT")) {
390 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
391 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
396 else if (!strcasecmp(parms[1], "LOGIN")) {
397 imap_login(num_parms, parms);
400 else if (!strcasecmp(parms[1], "CAPABILITY")) {
401 imap_capability(num_parms, parms);
404 else if (!CC->logged_in) {
405 cprintf("%s BAD Not logged in.\r\n", parms[0]);
408 /* commands requiring the client to be logged in */
410 else if (!strcasecmp(parms[1], "SELECT")) {
411 imap_select(num_parms, parms);
414 else if (!strcasecmp(parms[1], "EXAMINE")) {
415 imap_select(num_parms, parms);
418 else if (!strcasecmp(parms[1], "LSUB")) {
419 imap_lsub(num_parms, parms);
422 else if (!strcasecmp(parms[1], "LIST")) {
423 imap_list(num_parms, parms);
426 else if (IMAP->selected == 0) {
427 cprintf("%s BAD no folder selected\r\n", parms[0]);
430 /* commands requiring the SELECT state */
432 else if (!strcasecmp(parms[1], "FETCH")) {
433 imap_fetch(num_parms, parms);
436 else if (!strcasecmp(parms[1], "CLOSE")) {
437 imap_close(num_parms, parms);
440 /* end of commands */
443 cprintf("%s BAD command unrecognized\r\n", parms[0]);
450 char *Dynamic_Module_Init(void)
452 SYM_IMAP = CtdlGetDynamicSymbol();
453 CtdlRegisterServiceHook(143, /* FIXME put in config setup */
457 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);