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 * This cleanup function blows away the temporary memory and files used by
53 void imap_cleanup_function(void) {
55 /* Don't do this stuff if this is not a IMAP session! */
56 if (CC->h_command_function != imap_command_loop) return;
58 lprintf(9, "Performing IMAP cleanup hook\n");
61 lprintf(9, "Finished IMAP cleanup hook\n");
67 * Here's where our IMAP session begins its happy day.
69 void imap_greeting(void) {
71 strcpy(CC->cs_clientname, "IMAP session");
73 CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
75 cprintf("* OK %s Citadel/UX IMAP4rev1 server ready\r\n",
81 * implements the LOGIN command (ordinary username/password login)
83 void imap_login(int num_parms, char *parms[]) {
84 if (CtdlLoginExistingUser(parms[2]) == login_ok) {
85 if (CtdlTryPassword(parms[3]) == pass_ok) {
86 cprintf("%s OK login successful\r\n", parms[0]);
91 cprintf("%s BAD Login incorrect\r\n", parms[0]);
96 * implements the CAPABILITY command
98 void imap_capability(int num_parms, char *parms[]) {
99 cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
100 cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
108 * implements the SELECT command
110 void imap_select(int num_parms, char *parms[]) {
112 char augmented_roomname[256];
116 struct quickroom QRscratch;
119 strcpy(towhere, parms[2]);
121 /* IMAP uses the reserved name "INBOX" for the user's default incoming
122 * mail folder. Convert this to Citadel's reserved name "_MAIL_".
124 if (!strcasecmp(towhere, "INBOX"))
125 strcpy(towhere, MAILROOM);
127 /* First try a regular match */
128 c = getroom(&QRscratch, towhere);
130 /* Then try a mailbox name match */
132 MailboxName(augmented_roomname, &CC->usersupp, towhere);
133 c = getroom(&QRscratch, augmented_roomname);
135 strcpy(towhere, augmented_roomname);
138 /* If the room exists, check security/access */
140 /* See if there is an existing user/room relationship */
141 ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
143 /* normal clients have to pass through security */
148 /* Fail here if no such room */
150 cprintf("%s NO ... no such room, or access denied\r\n",
157 * usergoto() formally takes us to the desired room, happily returning
158 * the number of messages and number of new messages.
160 usergoto(QRscratch.QRname, 0, &msgs, &new);
163 if (!strcasecmp(parms[1], "EXAMINE")) {
170 /* FIXME ... much more info needs to be supplied here */
171 cprintf("* %d EXISTS\r\n", msgs);
172 cprintf("* %d RECENT\r\n", new);
173 cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
174 cprintf("%s OK [%s] %s completed\r\n",
176 (IMAP->readonly ? "READ-ONLY" : "READ-WRITE"),
183 * implements the CLOSE command
185 void imap_close(int num_parms, char *parms[]) {
188 cprintf("%s OK CLOSE completed\r\n", parms[0]);
195 * Main command loop for IMAP sessions.
197 void imap_command_loop(void) {
204 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
205 if (client_gets(cmdbuf) < 1) {
206 lprintf(3, "IMAP socket is broken. Ending session.\r\n");
211 lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
212 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
215 /* strip off l/t whitespace and CRLF */
216 if (cmdbuf[strlen(cmdbuf)-1]=='\n') cmdbuf[strlen(cmdbuf)-1]=0;
217 if (cmdbuf[strlen(cmdbuf)-1]=='\r') cmdbuf[strlen(cmdbuf)-1]=0;
221 num_parms = imap_parameterize(parms, cmdbuf);
222 for (i=0; i<num_parms; ++i) {
223 lprintf(9, " parms[%d]='%s'\n", i, parms[i]);
226 /* commands which may be executed in any state */
228 if ( (!strcasecmp(parms[1], "NOOP"))
229 || (!strcasecmp(parms[1], "CHECK")) ) {
230 cprintf("%s OK This command successfully did nothing.\r\n",
234 else if (!strcasecmp(parms[1], "LOGOUT")) {
235 cprintf("* BYE %s logging out\r\n", config.c_fqdn);
236 cprintf("%s OK thank you for using Citadel IMAP\r\n", parms[0]);
241 else if (!strcasecmp(parms[1], "LOGIN")) {
242 imap_login(num_parms, parms);
245 else if (!strcasecmp(parms[1], "CAPABILITY")) {
246 imap_capability(num_parms, parms);
249 else if (!CC->logged_in) {
250 cprintf("%s BAD Not logged in.\r\n", parms[0]);
253 /* commands requiring the client to be logged in */
255 else if (!strcasecmp(parms[1], "SELECT")) {
256 imap_select(num_parms, parms);
259 else if (!strcasecmp(parms[1], "EXAMINE")) {
260 imap_select(num_parms, parms);
263 else if (IMAP->selected == 0) {
264 cprintf("%s BAD command unrecognized\r\n", parms[0]);
267 /* commands requiring the SELECT state */
269 else if (!strcasecmp(parms[1], "CLOSE")) {
270 imap_close(num_parms, parms);
273 /* end of commands */
276 cprintf("%s BAD command unrecognized\r\n", parms[0]);
283 char *Dynamic_Module_Init(void)
285 SYM_IMAP = CtdlGetDynamicSymbol();
286 CtdlRegisterServiceHook(2243, /* FIXME put in config setup */
290 CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);