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;
}
}
}
-}
+}
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;
}
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) {
}
+/*
+ * 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.
IMAP->authstate = imap_as_normal;
IMAP->cached_rfc822_data = NULL;
IMAP->cached_rfc822_msgnum = (-1);
+ IMAP->cached_rfc822_withbody = 0;
- cprintf("* OK %s Citadel IMAP4rev1 server ready\r\n",
- config.c_fqdn);
+ cprintf("* OK [");
+ imap_output_capability_string();
+ cprintf("] %s IMAP4rev1 %s ready\r\n", config.c_fqdn, CITADEL);
}
/*
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]);
* 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]);
}