4 * Implements the SEARCH command in IMAP.
5 * This command is way too convoluted. Marc Crispin is a fscking idiot.
7 * NOTE: this is a partial implementation. It is NOT FINISHED.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
39 #include "sysdep_decls.h"
40 #include "citserver.h"
43 #include "serv_extensions.h"
50 #include "internet_addressing.h"
51 #include "serv_imap.h"
52 #include "imap_tools.h"
53 #include "imap_fetch.h"
54 #include "imap_search.h"
60 * imap_do_search() calls imap_do_search_msg() to search an individual
61 * message after it has been fetched from the disk. This function returns
62 * nonzero if there is a match.
64 int imap_do_search_msg(int seq, struct CtdlMessage *msg,
65 int num_items, char **itemlist, int is_uid) {
73 if (num_items == 0) return(0);
75 /* Initially we start at the beginning. */
78 /* Check for the dreaded NOT criterion. */
79 if (!strcasecmp(itemlist[0], "NOT")) {
84 /* Check for the dreaded OR criterion. */
85 if (!strcasecmp(itemlist[0], "OR")) {
90 /* Now look for criteria. */
91 if (!strcasecmp(itemlist[pos], "ALL")) {
96 else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
97 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
103 else if (!strcasecmp(itemlist[pos], "BCC")) {
108 else if (!strcasecmp(itemlist[pos], "BEFORE")) {
109 if (msg->cm_fields['T'] != NULL) {
110 if (imap_datecmp(itemlist[pos+1],
111 atol(msg->cm_fields['T'])) < 0) {
118 else if (!strcasecmp(itemlist[pos], "BODY")) {
119 if (bmstrstr(msg->cm_fields['M'], itemlist[pos+1], strncasecmp)) {
125 else if (!strcasecmp(itemlist[pos], "CC")) {
130 else if (!strcasecmp(itemlist[pos], "DELETED")) {
131 if (IMAP->flags[seq-1] & IMAP_DELETED) {
137 else if (!strcasecmp(itemlist[pos], "DRAFT")) {
138 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
144 else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
145 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
151 else if (!strcasecmp(itemlist[pos], "FROM")) {
152 if (bmstrstr(msg->cm_fields['A'], itemlist[pos+1], strncasecmp)) {
158 else if (!strcasecmp(itemlist[pos], "HEADER")) {
160 pos += 3; /* Yes, three */
163 else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
168 else if (!strcasecmp(itemlist[pos], "LARGER")) {
169 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
175 else if (!strcasecmp(itemlist[pos], "NEW")) {
180 else if (!strcasecmp(itemlist[pos], "OLD")) {
185 else if (!strcasecmp(itemlist[pos], "ON")) {
186 if (msg->cm_fields['T'] != NULL) {
187 if (imap_datecmp(itemlist[pos+1],
188 atol(msg->cm_fields['T'])) == 0) {
195 else if (!strcasecmp(itemlist[pos], "RECENT")) {
200 else if (!strcasecmp(itemlist[pos], "SEEN")) {
201 if (IMAP->flags[seq-1] & IMAP_SEEN) {
207 else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
208 if (msg->cm_fields['T'] != NULL) {
209 if (imap_datecmp(itemlist[pos+1],
210 atol(msg->cm_fields['T'])) < 0) {
217 else if (!strcasecmp(itemlist[pos], "SENTON")) {
218 if (msg->cm_fields['T'] != NULL) {
219 if (imap_datecmp(itemlist[pos+1],
220 atol(msg->cm_fields['T'])) == 0) {
227 else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
228 if (msg->cm_fields['T'] != NULL) {
229 if (imap_datecmp(itemlist[pos+1],
230 atol(msg->cm_fields['T'])) >= 0) {
237 else if (!strcasecmp(itemlist[pos], "SINCE")) {
238 if (msg->cm_fields['T'] != NULL) {
239 if (imap_datecmp(itemlist[pos+1],
240 atol(msg->cm_fields['T'])) >= 0) {
247 else if (!strcasecmp(itemlist[pos], "SMALLER")) {
248 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
254 else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
255 if (bmstrstr(msg->cm_fields['U'], itemlist[pos+1], strncasecmp)) {
261 else if (!strcasecmp(itemlist[pos], "TEXT")) {
262 for (i='A'; i<='Z'; ++i) {
263 if (bmstrstr(msg->cm_fields[i], itemlist[pos+1], strncasecmp)) {
270 else if (!strcasecmp(itemlist[pos], "TO")) {
271 if (bmstrstr(msg->cm_fields['R'], itemlist[pos+1], strncasecmp)) {
277 else if (!strcasecmp(itemlist[pos], "UID")) {
278 if (is_msg_in_mset(itemlist[pos+1], IMAP->msgids[seq-1])) {
284 /* Now here come the 'UN' criteria. Why oh why do we have to
285 * implement *both* the 'UN' criteria *and* the 'NOT' keyword? Why
286 * can't there be *one* way to do things? Answer: because Mark
287 * Crispin is an idiot.
290 else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
291 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
297 else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
298 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
304 else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
305 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
311 else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
312 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
318 else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
323 else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
324 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
330 /* Remember to negate if we were told to */
335 /* Keep going if there are more criteria! */
336 if (pos < num_items) {
339 match = (match || imap_do_search_msg(seq, msg,
340 num_items - pos, &itemlist[pos], is_uid));
343 match = (match && imap_do_search_msg(seq, msg,
344 num_items - pos, &itemlist[pos], is_uid));
354 * imap_search() calls imap_do_search() to do its actual work, once it's
355 * validated and boiled down the request a bit.
357 void imap_do_search(int num_items, char **itemlist, int is_uid) {
359 struct CtdlMessage *msg;
361 cprintf("* SEARCH ");
362 if (IMAP->num_msgs > 0)
363 for (i = 0; i < IMAP->num_msgs; ++i)
364 if (IMAP->flags[i] && IMAP_SELECTED) {
365 msg = CtdlFetchMessage(IMAP->msgids[i]);
367 if (imap_do_search_msg(i+1, msg, num_items,
370 cprintf("%ld ", IMAP->msgids[i]);
376 CtdlFreeMessage(msg);
379 lprintf(1, "SEARCH internal error\n");
387 * This function is called by the main command loop.
389 void imap_search(int num_parms, char *parms[]) {
393 cprintf("%s BAD invalid parameters\r\n", parms[0]);
397 for (i=1; i<num_parms; ++i) {
398 if (imap_is_message_set(parms[i])) {
399 imap_pick_range(parms[i], 0);
403 imap_do_search(num_parms-2, &parms[2], 0);
404 cprintf("%s OK SEARCH completed\r\n", parms[0]);
408 * This function is called by the main command loop.
410 void imap_uidsearch(int num_parms, char *parms[]) {
414 cprintf("%s BAD invalid parameters\r\n", parms[0]);
418 for (i=1; i<num_parms; ++i) {
419 if (imap_is_message_set(parms[i])) {
420 imap_pick_range(parms[i], 1);
424 imap_do_search(num_parms-3, &parms[3], 1);
425 cprintf("%s OK UID SEARCH completed\r\n", parms[0]);