3 * IMAP server for the Citadel/UX system
4 * Copyright (C) 2000 by Art Cancro and others.
5 * This code is released under the terms of the GNU General Public License.
7 * Current status of standards conformance:
9 * *** ABSOLUTELY NOTHING WORKS ***
21 #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"
48 * This cleanup function blows away the temporary memory and files used by
51 void imap_cleanup_function(void) {
53 /* Don't do this stuff if this is not a IMAP session! */
54 if (CC->h_command_function != imap_command_loop) return;
56 lprintf(9, "Performing IMAP cleanup hook\n");
59 lprintf(9, "Finished IMAP cleanup hook\n");
65 * Here's where our IMAP session begins its happy day.
67 void imap_greeting(void) {
69 strcpy(CC->cs_clientname, "IMAP session");
71 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
73 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
79 * implements the LOGIN command (ordinary username/password login)
81 void imap_login(char *tag, char *cmd, char *parms) {
85 extract_token(username, parms, 0, ' ');
86 extract_token(password, parms, 1, ' ');
88 if (CtdlLoginExistingUser(username) == login_ok) {
89 if (CtdlTryPassword(password) == pass_ok) {
90 cprintf("%s OK login successful\r\n", tag);
95 cprintf("%s BAD Login incorrect\r\n", tag);
100 * implements the CAPABILITY command
102 void imap_capability(char *tag, char *cmd, char *parms) {
103 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
104 cprintf("%s OK CAPABILITY completed\r\n", tag);
112 * implements the SELECT command
114 void imap_select(char *tag, char *cmd, char *parms) {
116 char augmented_roomname[256];
120 struct quickroom QRscratch;
122 extract_token(towhere, parms, 0, ' ');
124 /* IMAP uses the reserved name "INBOX" for the user's default incoming
125 * mail folder. Convert this to Citadel's reserved name "_MAIL_".
127 if (!strcasecmp(towhere, "INBOX"))
128 strcpy(towhere, MAILROOM);
130 /* First try a regular match */
131 c = getroom(&QRscratch, towhere);
133 /* Then try a mailbox name match */
135 MailboxName(augmented_roomname, &CC->usersupp, towhere);
136 c = getroom(&QRscratch, augmented_roomname);
138 strcpy(towhere, augmented_roomname);
141 /* If the room exists, check security/access */
143 /* See if there is an existing user/room relationship */
144 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
146 /* normal clients have to pass through security */
151 /* Fail here if no such room */
153 cprintf("%s NO ... no such room, or access denied\r\n", tag);
159 cprintf("%s OK [FIXME] SELECT completed\r\n", tag);
164 * Main command loop for IMAP sessions.
166 void imap_command_loop(void) {
172 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
173 if (client_gets(cmdbuf) < 1) {
174 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
179 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
180 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
183 /* strip off l/t whitespace and CRLF */
184 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
185 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
189 extract_token(tag, cmdbuf, 0, ' ');
190 extract_token(cmd, cmdbuf, 1, ' ');
191 remove_token(cmdbuf, 0, ' ');
192 remove_token(cmdbuf, 0, ' ');
193 lprintf(9, "tag=<%s> cmd=<%s> parms=<%s>\n", tag, cmd, cmdbuf);
195 /* commands which may be executed in any state */
197 if (!strcasecmp(cmd, "NOOP")) {
198 cprintf("%s OK This command successfully did nothing.\r\n",
202 else if (!strcasecmp(cmd, "LOGOUT")) {
203 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
204 cprintf("%s OK thank you for using Citadel IMAP\r\n", tag);
209 else if (!strcasecmp(cmd, "LOGIN")) {
210 imap_login(tag, cmd, cmdbuf);
213 else if (!strcasecmp(cmd, "CAPABILITY")) {
214 imap_capability(tag, cmd, cmdbuf);
217 else if (!CC->logged_in) {
218 cprintf("%s BAD Not logged in.\r\n", tag);
221 /* commands requiring the client to be logged in */
223 else if (!strcasecmp(cmd, "SELECT")) {
224 imap_select(tag, cmd, cmdbuf);
227 /* end of commands */
230 cprintf("%s BAD command unrecognized\r\n", tag);
237 char *Dynamic_Module_Init(void)
239 SYM_IMAP = CtdlGetDynamicSymbol();
240 CtdlRegisterServiceHook(1143, /* FIXME put in config setup */
244 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);