* $Id$
*
* IMAP server for the Citadel system
- * Copyright (C) 2000-2002 by Art Cancro and others.
+ * Copyright (C) 2000-2005 by Art Cancro and others.
* This code is released under the terms of the GNU General Public License.
*
- * WARNING: Mark Crispin is an idiot. IMAP is the most brain-damaged protocol
- * you will ever have the profound lack of pleasure to encounter.
+ * WARNING: the IMAP protocol is badly designed. No implementation of it
+ * is perfect. Indeed, with so much gratuitous complexity, *all* IMAP
+ * implementations have bugs.
*/
#include "sysdep.h"
free(IMAP->msgids);
IMAP->msgids = NULL;
IMAP->num_msgs = 0;
+ IMAP->num_alloc = 0;
}
if (IMAP->flags != NULL) {
free(IMAP->flags);
IMAP->flags = NULL;
}
+ IMAP->last_mtime = (-1);
}
/*
- * Set the \\Seen flag for messages which aren't new
+ * Set the \Seen, \Recent. and \Answered flags, based on the sequence
+ * sets stored in the visit record for this user/room. Note that we have
+ * to parse each sequence set manually here, because calling the utility
+ * function is_msg_in_sequence_set() over and over again is too expensive.
*/
void imap_set_seen_flags(void)
{
struct visit vbuf;
int i;
+ int num_sets;
+ int s;
+ char setstr[SIZ], lostr[SIZ], histr[SIZ];
+ long lo, hi;
+ if (IMAP->num_msgs < 1) return;
CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
- if (IMAP->num_msgs > 0) {
+
+ for (i = 0; i < IMAP->num_msgs; ++i) {
+ IMAP->flags[i] = IMAP->flags[i] & ~IMAP_SEEN;
+ IMAP->flags[i] |= IMAP_RECENT;
+ IMAP->flags[i] = IMAP->flags[i] & ~IMAP_ANSWERED;
+ }
+
+ /* Do the "\Seen" flag. (Any message not "\Seen" is considered "\Recent".) */
+ num_sets = num_tokens(vbuf.v_seen, ',');
+ for (s=0; s<num_sets; ++s) {
+ extract_token(setstr, vbuf.v_seen, s, ',', sizeof setstr);
+
+ extract_token(lostr, setstr, 0, ':', sizeof lostr);
+ if (num_tokens(setstr, ':') >= 2) {
+ extract_token(histr, setstr, 1, ':', sizeof histr);
+ if (!strcmp(histr, "*")) {
+ snprintf(histr, sizeof histr, "%ld", LONG_MAX);
+ }
+ }
+ else {
+ strcpy(histr, lostr);
+ }
+ lo = atol(lostr);
+ hi = atol(histr);
+
for (i = 0; i < IMAP->num_msgs; ++i) {
- if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
+ if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)) {
IMAP->flags[i] |= IMAP_SEEN;
+ IMAP->flags[i] = IMAP->flags[i] & ~IMAP_RECENT;
}
- else {
- IMAP->flags[i] |= IMAP_RECENT;
+ }
+ }
+
+ /* Do the ANSWERED flag */
+ num_sets = num_tokens(vbuf.v_answered, ',');
+ for (s=0; s<num_sets; ++s) {
+ extract_token(setstr, vbuf.v_answered, s, ',', sizeof setstr);
+
+ extract_token(lostr, setstr, 0, ':', sizeof lostr);
+ if (num_tokens(setstr, ':') >= 2) {
+ extract_token(histr, setstr, 1, ':', sizeof histr);
+ if (!strcmp(histr, "*")) {
+ snprintf(histr, sizeof histr, "%ld", LONG_MAX);
}
- if (is_msg_in_mset
- (vbuf.v_answered, IMAP->msgids[i])) {
+ }
+ else {
+ strcpy(histr, lostr);
+ }
+ lo = atol(lostr);
+ hi = atol(histr);
+
+ for (i = 0; i < IMAP->num_msgs; ++i) {
+ if ((IMAP->msgids[i] >= lo) && (IMAP->msgids[i] <= hi)) {
IMAP->flags[i] |= IMAP_ANSWERED;
}
}
}
-}
+}
void imap_add_single_msgid(long msgnum, void *userdata)
{
- IMAP->num_msgs = IMAP->num_msgs + 1;
- if (IMAP->msgids == NULL) {
- IMAP->msgids = malloc(IMAP->num_msgs * sizeof(long)
- * REALLOC_INCREMENT);
- } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
- IMAP->msgids = realloc(IMAP->msgids,
- (IMAP->num_msgs +
- REALLOC_INCREMENT) * sizeof(long));
- }
- if (IMAP->flags == NULL) {
- IMAP->flags = malloc(IMAP->num_msgs * sizeof(long)
- * REALLOC_INCREMENT);
- } else if (IMAP->num_msgs % REALLOC_INCREMENT == 0) {
- IMAP->flags = realloc(IMAP->flags,
- (IMAP->num_msgs +
- REALLOC_INCREMENT) * sizeof(long));
+ ++IMAP->num_msgs;
+ if (IMAP->num_msgs > IMAP->num_alloc) {
+ IMAP->num_alloc += REALLOC_INCREMENT;
+ IMAP->msgids = realloc(IMAP->msgids, (IMAP->num_alloc * sizeof(long)));
+ IMAP->flags = realloc(IMAP->flags, (IMAP->num_alloc * sizeof(long)));
}
IMAP->msgids[IMAP->num_msgs - 1] = msgnum;
IMAP->flags[IMAP->num_msgs - 1] = 0;
imap_add_single_msgid, NULL);
imap_set_seen_flags();
- lprintf(CTDL_DEBUG, "imap_load_msgids() mapped %d messages\n",
- IMAP->num_msgs);
}
int num_msgs = 0;
int num_recent = 0;
-
if (IMAP->selected == 0) {
lprintf(CTDL_ERR,
"imap_load_msgids() can't run; no room selected\n");
return;
}
+ /*
+ * Check to see if the room's contents have changed.
+ * If not, we can avoid this rescan.
+ */
+ getroom(&CC->room, CC->room.QRname);
+ if (IMAP->last_mtime == CC->room.QRmtime) { /* No changes! */
+ return;
+ }
+
/* Load the *current* message list from disk, so we can compare it
* to what we have in memory.
*/
if (message_still_exists == 0) {
cprintf("* %d EXPUNGE\r\n", i + 1);
- /* Here's some nice stupid nonsense. When a message
- * is expunged, we have to slide all the existing
- * messages up in the message array.
+ /* Here's some nice stupid nonsense. When a
+ * message is expunged, we have to slide all
+ * the existing messages up in the message
+ * array.
*/
--IMAP->num_msgs;
memcpy(&IMAP->msgids[i],
/*
* Now peruse the room for *new* messages only.
*/
- if (num_msgs > 0)
+ if (num_msgs > 0) {
for (j = 0; j < num_msgs; ++j) {
if (msglist[j] > original_highest) {
imap_add_single_msgid(msglist[j], NULL);
}
}
+ }
imap_set_seen_flags();
/*
cprintf("* %d RECENT\r\n", num_recent);
}
- if (num_msgs != 0)
+ if (num_msgs != 0) {
free(msglist);
+ }
+ IMAP->last_mtime = CC->room.QRmtime;
}
imap_free_msgids();
imap_free_transmitted_message();
- if (IMAP->cached_fetch != NULL) {
- fclose(IMAP->cached_fetch);
- IMAP->cached_fetch = NULL;
- IMAP->cached_msgnum = (-1);
+ if (IMAP->cached_rfc822_data != NULL) {
+ free(IMAP->cached_rfc822_data);
+ IMAP->cached_rfc822_data = NULL;
+ IMAP->cached_rfc822_msgnum = (-1);
+ IMAP->cached_rfc822_withbody = 0;
}
if (IMAP->cached_body != NULL) {
- fclose(IMAP->cached_body);
+ free(IMAP->cached_body);
IMAP->cached_body = NULL;
+ IMAP->cached_body_len = 0;
IMAP->cached_bodymsgnum = (-1);
}
+ free(IMAP);
lprintf(CTDL_DEBUG, "Finished IMAP cleanup hook\n");
}
+/*
+ * Does the actual work of the CAPABILITY command (because we need to
+ * output this stuff in other places as well)
+ */
+void imap_output_capability_string(void) {
+ cprintf("CAPABILITY IMAP4REV1 NAMESPACE AUTH=LOGIN");
+#ifdef HAVE_OPENSSL
+ if (!CC->redirect_ssl) cprintf(" STARTTLS");
+#endif
+}
+
+/*
+ * implements the CAPABILITY command
+ */
+void imap_capability(int num_parms, char *parms[])
+{
+ cprintf("* ");
+ imap_output_capability_string();
+ cprintf("\r\n");
+ cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
+}
+
+
/*
* Here's where our IMAP session begins its happy day.
{
strcpy(CC->cs_clientname, "IMAP session");
- CtdlAllocUserData(SYM_IMAP, sizeof(struct citimap));
+ IMAP = malloc(sizeof (struct citimap));
+ memset(IMAP, 0, sizeof(struct citimap));
IMAP->authstate = imap_as_normal;
- IMAP->cached_fetch = NULL;
- IMAP->cached_msgnum = (-1);
+ IMAP->cached_rfc822_data = NULL;
+ IMAP->cached_rfc822_msgnum = (-1);
+ IMAP->cached_rfc822_withbody = 0;
+
+ cprintf("* OK [");
+ imap_output_capability_string();
+ cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
+}
- cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
- config.c_fqdn);
+/*
+ * IMAPS is just like IMAP, except it goes crypto right away.
+ */
+#ifdef HAVE_OPENSSL
+void imaps_greeting(void) {
+ CtdlStartTLS(NULL, NULL, NULL);
+ imap_greeting();
}
+#endif
/*
if (CtdlLoginExistingUser(parms[2]) == login_ok) {
if (CtdlTryPassword(parms[3]) == pass_ok) {
- cprintf("%s OK login successful\r\n", parms[0]);
+ cprintf("%s OK [", parms[0]);
+ imap_output_capability_string();
+ cprintf("] Hello, %s\r\n", CC->user.fullname);
return;
}
}
}
-/*
- * implements the CAPABILITY command
- */
-void imap_capability(int num_parms, char *parms[])
-{
- cprintf("* CAPABILITY IMAP4 IMAP4REV1 NAMESPACE AUTH=LOGIN");
-
-#ifdef HAVE_OPENSSL
- cprintf(" STARTTLS");
-#endif
-
- cprintf("\r\n");
- cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
-}
-
-
-
/*
* implements the STARTTLS command (Citadel API version)
*/
}
imap_load_msgids();
+ IMAP->last_mtime = CC->room.QRmtime;
cprintf("* %d EXISTS\r\n", msgs);
cprintf("* %d RECENT\r\n", new);
+ cprintf("* OK [UIDVALIDITY 1] UID validity status\r\n");
+ cprintf("* OK [UIDNEXT %ld] Predicted next UID\r\n", CitControl.MMhighest + 1);
+
/* Note that \Deleted is a valid flag, but not a permanent flag,
* because we don't maintain its state across sessions. Citadel
* automatically expunges mailboxes when they are de-selected.
cprintf("* OK [PERMANENTFLAGS (\\Seen \\Answered)] "
"permanent flags\r\n");
- cprintf("* OK [UIDVALIDITY 0] UIDs valid\r\n");
cprintf("%s OK [%s] %s completed\r\n",
parms[0],
(IMAP->readonly ? "READ-ONLY" : "READ-WRITE"), parms[1]);
char roomname[ROOMNAMELEN];
int floornum;
int flags;
- int newroomtype;
+ int newroomtype = 0;
+ int newroomview = 0;
if (strchr(parms[2], '\\') != NULL) {
cprintf("%s NO Invalid character in folder name\r\n",
}
if (flags & IR_MAILBOX) {
- newroomtype = 4; /* private mailbox */
+ newroomtype = 4; /* private mailbox */
+ newroomview = VIEW_MAILBOX;
} else {
- newroomtype = 0; /* public folder */
+ newroomtype = 0; /* public folder */
+ newroomview = VIEW_BBS;
}
lprintf(CTDL_INFO, "Create new room <%s> on floor <%d> with type <%d>\n",
roomname, floornum, newroomtype);
- ret = create_room(roomname, newroomtype, "", floornum, 1, 0, VIEW_BBS);
+ ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
if (ret == 0) {
cprintf
("%s NO Mailbox already exists, or create failed\r\n",
/*
- * Locate a room by its IMAP folder name, and check access to it
+ * Locate a room by its IMAP folder name, and check access to it.
+ * If zapped_ok is nonzero, we can also look for the room in the zapped list.
*/
-int imap_grabroom(char *returned_roomname, char *foldername)
+int imap_grabroom(char *returned_roomname, char *foldername, int zapped_ok)
{
int ret;
char augmented_roomname[ROOMNAMELEN];
if (ra & UA_KNOWN) {
ok = 1;
}
+ if ((zapped_ok) && (ra & UA_ZAPPED)) {
+ ok = 1;
+ }
}
/* Fail here if no such room */
char savedroom[ROOMNAMELEN];
int msgs, new;
- ret = imap_grabroom(roomname, parms[2]);
+ ret = imap_grabroom(roomname, parms[2], 0);
if (ret != 0) {
cprintf
("%s NO Invalid mailbox name or location, or access denied\r\n",
char savedroom[ROOMNAMELEN];
int msgs, new;
- ret = imap_grabroom(roomname, parms[2]);
+ ret = imap_grabroom(roomname, parms[2], 1);
if (ret != 0) {
- cprintf
- ("%s NO Invalid mailbox name or location, or access denied\r\n",
- parms[0]);
+ cprintf(
+ "%s NO Error %d: invalid mailbox name or location, or access denied\r\n",
+ parms[0],
+ ret
+ );
return;
}
char savedroom[ROOMNAMELEN];
int msgs, new;
- ret = imap_grabroom(roomname, parms[2]);
+ ret = imap_grabroom(roomname, parms[2], 0);
if (ret != 0) {
cprintf
("%s NO Invalid mailbox name or location, or access denied\r\n",
char savedroom[ROOMNAMELEN];
int msgs, new;
- ret = imap_grabroom(roomname, parms[2]);
+ ret = imap_grabroom(roomname, parms[2], 0);
if (ret != 0) {
cprintf("%s NO Invalid mailbox name, or access denied\r\n",
parms[0]);
* Now delete the room.
*/
if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
+ schedule_room_for_deletion(&CC->room);
cprintf("%s OK DELETE completed\r\n", parms[0]);
- delete_room(&CC->room);
} else {
cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
}
* (already did that) and create a new inbox.
*/
if (!strcasecmp(parms[2], "INBOX")) {
- create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_BBS);
+ create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
}
/* Otherwise, do the subfolders. Build a list of rooms to rename... */
CC->lastcmd = time(NULL);
memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
flush_output();
- if (client_gets(cmdbuf) < 1) {
+ if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
lprintf(CTDL_ERR, "IMAP socket is broken. Ending session.\r\n");
CC->kill_me = 1;
return;
}
- lprintf(CTDL_INFO, "IMAP: %s\r\n", cmdbuf);
+ lprintf(CTDL_INFO, "IMAP: %s\n", cmdbuf);
while (strlen(cmdbuf) < 5)
strcat(cmdbuf, " ");
{
CtdlRegisterServiceHook(config.c_imap_port,
NULL, imap_greeting, imap_command_loop, NULL);
+#ifdef HAVE_OPENSSL
+ CtdlRegisterServiceHook(config.c_imaps_port,
+ NULL, imaps_greeting, imap_command_loop, NULL);
+#endif
CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
return "$Id$";
}