* New bmstrcasestr() function to perform very fast case-insensitive
[citadel.git] / citadel / imap_search.c
1 /*
2  * $Id$
3  *
4  * Implements IMAP's gratuitously complex SEARCH command.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "serv_extensions.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "policy.h"
43 #include "database.h"
44 #include "msgbase.h"
45 #include "tools.h"
46 #include "internet_addressing.h"
47 #include "serv_imap.h"
48 #include "imap_tools.h"
49 #include "imap_fetch.h"
50 #include "imap_search.h"
51 #include "genstamp.h"
52 #include "serv_fulltext.h"
53
54
55 /*
56  * imap_do_search() calls imap_do_search_msg() to search an individual
57  * message after it has been fetched from the disk.  This function returns
58  * nonzero if there is a match.
59  *
60  * supplied_msg MAY be used to pass a pointer to the message in memory,
61  * if for some reason it's already been loaded.  If not, the message will
62  * be loaded only if one or more search criteria require it.
63  */
64 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
65                         int num_items, char **itemlist, int is_uid) {
66
67         int match = 0;
68         int is_not = 0;
69         int is_or = 0;
70         int pos = 0;
71         int i;
72         char *fieldptr;
73         struct CtdlMessage *msg = NULL;
74         int need_to_free_msg = 0;
75
76         if (num_items == 0) {
77                 return(0);
78         }
79         msg = supplied_msg;
80
81         /* Initially we start at the beginning. */
82         pos = 0;
83
84         /* Check for the dreaded NOT criterion. */
85         if (!strcasecmp(itemlist[0], "NOT")) {
86                 is_not = 1;
87                 pos = 1;
88         }
89
90         /* Check for the dreaded OR criterion. */
91         if (!strcasecmp(itemlist[0], "OR")) {
92                 is_or = 1;
93                 pos = 1;
94         }
95
96         /* Now look for criteria. */
97         if (!strcasecmp(itemlist[pos], "ALL")) {
98                 match = 1;
99                 ++pos;
100         }
101         
102         else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
103                 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
104                         match = 1;
105                 }
106                 ++pos;
107         }
108
109         else if (!strcasecmp(itemlist[pos], "BCC")) {
110                 if (msg == NULL) {
111                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
112                         need_to_free_msg = 1;
113                 }
114                 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
115                 if (fieldptr != NULL) {
116                         if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
117                                 match = 1;
118                         }
119                         free(fieldptr);
120                 }
121                 pos += 2;
122         }
123
124         else if (!strcasecmp(itemlist[pos], "BEFORE")) {
125                 if (msg == NULL) {
126                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
127                         need_to_free_msg = 1;
128                 }
129                 if (msg->cm_fields['T'] != NULL) {
130                         if (imap_datecmp(itemlist[pos+1],
131                                         atol(msg->cm_fields['T'])) < 0) {
132                                 match = 1;
133                         }
134                 }
135                 pos += 2;
136         }
137
138         else if (!strcasecmp(itemlist[pos], "BODY")) {
139                 if (msg == NULL) {
140                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
141                         need_to_free_msg = 1;
142                 }
143                 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
144                         match = 1;
145                 }
146                 pos += 2;
147         }
148
149         else if (!strcasecmp(itemlist[pos], "CC")) {
150                 if (msg == NULL) {
151                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
152                         need_to_free_msg = 1;
153                 }
154                 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
155                 if (fieldptr != NULL) {
156                         if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
157                                 match = 1;
158                         }
159                         free(fieldptr);
160                 }
161                 pos += 2;
162         }
163
164         else if (!strcasecmp(itemlist[pos], "DELETED")) {
165                 if (IMAP->flags[seq-1] & IMAP_DELETED) {
166                         match = 1;
167                 }
168                 ++pos;
169         }
170
171         else if (!strcasecmp(itemlist[pos], "DRAFT")) {
172                 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
173                         match = 1;
174                 }
175                 ++pos;
176         }
177
178         else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
179                 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
180                         match = 1;
181                 }
182                 ++pos;
183         }
184
185         else if (!strcasecmp(itemlist[pos], "FROM")) {
186                 if (msg == NULL) {
187                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
188                         need_to_free_msg = 1;
189                 }
190                 if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
191                         match = 1;
192                 }
193                 pos += 2;
194         }
195
196         else if (!strcasecmp(itemlist[pos], "HEADER")) {
197                 /* FIXME */
198                 pos += 3;       /* Yes, three */
199         }
200
201         else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
202                 /* FIXME */
203                 pos += 2;
204         }
205
206         else if (!strcasecmp(itemlist[pos], "LARGER")) {
207                 if (msg == NULL) {
208                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
209                         need_to_free_msg = 1;
210                 }
211                 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
212                         match = 1;
213                 }
214                 pos += 2;
215         }
216
217         else if (!strcasecmp(itemlist[pos], "NEW")) {
218                 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
219                         match = 1;
220                 }
221                 ++pos;
222         }
223
224         else if (!strcasecmp(itemlist[pos], "OLD")) {
225                 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
226                         match = 1;
227                 }
228                 ++pos;
229         }
230
231         else if (!strcasecmp(itemlist[pos], "ON")) {
232                 if (msg == NULL) {
233                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
234                         need_to_free_msg = 1;
235                 }
236                 if (msg->cm_fields['T'] != NULL) {
237                         if (imap_datecmp(itemlist[pos+1],
238                                         atol(msg->cm_fields['T'])) == 0) {
239                                 match = 1;
240                         }
241                 }
242                 pos += 2;
243         }
244
245         else if (!strcasecmp(itemlist[pos], "RECENT")) {
246                 if (IMAP->flags[seq-1] & IMAP_RECENT) {
247                         match = 1;
248                 }
249                 ++pos;
250         }
251
252         else if (!strcasecmp(itemlist[pos], "SEEN")) {
253                 if (IMAP->flags[seq-1] & IMAP_SEEN) {
254                         match = 1;
255                 }
256                 ++pos;
257         }
258
259         else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
260                 if (msg == NULL) {
261                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
262                         need_to_free_msg = 1;
263                 }
264                 if (msg->cm_fields['T'] != NULL) {
265                         if (imap_datecmp(itemlist[pos+1],
266                                         atol(msg->cm_fields['T'])) < 0) {
267                                 match = 1;
268                         }
269                 }
270                 pos += 2;
271         }
272
273         else if (!strcasecmp(itemlist[pos], "SENTON")) {
274                 if (msg == NULL) {
275                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
276                         need_to_free_msg = 1;
277                 }
278                 if (msg->cm_fields['T'] != NULL) {
279                         if (imap_datecmp(itemlist[pos+1],
280                                         atol(msg->cm_fields['T'])) == 0) {
281                                 match = 1;
282                         }
283                 }
284                 pos += 2;
285         }
286
287         else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
288                 if (msg == NULL) {
289                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
290                         need_to_free_msg = 1;
291                 }
292                 if (msg->cm_fields['T'] != NULL) {
293                         if (imap_datecmp(itemlist[pos+1],
294                                         atol(msg->cm_fields['T'])) >= 0) {
295                                 match = 1;
296                         }
297                 }
298                 pos += 2;
299         }
300
301         else if (!strcasecmp(itemlist[pos], "SINCE")) {
302                 if (msg == NULL) {
303                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
304                         need_to_free_msg = 1;
305                 }
306                 if (msg->cm_fields['T'] != NULL) {
307                         if (imap_datecmp(itemlist[pos+1],
308                                         atol(msg->cm_fields['T'])) >= 0) {
309                                 match = 1;
310                         }
311                 }
312                 pos += 2;
313         }
314
315         else if (!strcasecmp(itemlist[pos], "SMALLER")) {
316                 if (msg == NULL) {
317                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
318                         need_to_free_msg = 1;
319                 }
320                 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
321                         match = 1;
322                 }
323                 pos += 2;
324         }
325
326         else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
327                 if (msg == NULL) {
328                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
329                         need_to_free_msg = 1;
330                 }
331                 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
332                         match = 1;
333                 }
334                 pos += 2;
335         }
336
337         else if (!strcasecmp(itemlist[pos], "TEXT")) {
338                 if (msg == NULL) {
339                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
340                         need_to_free_msg = 1;
341                 }
342                 for (i='A'; i<='Z'; ++i) {
343                         if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
344                                 match = 1;
345                         }
346                 }
347                 pos += 2;
348         }
349
350         else if (!strcasecmp(itemlist[pos], "TO")) {
351                 if (msg == NULL) {
352                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
353                         need_to_free_msg = 1;
354                 }
355                 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
356                         match = 1;
357                 }
358                 pos += 2;
359         }
360
361         else if (!strcasecmp(itemlist[pos], "UID")) {
362                 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
363                         match = 1;
364                 }
365                 pos += 2;
366         }
367
368         /* Now here come the 'UN' criteria.  Why oh why do we have to
369          * implement *both* the 'UN' criteria *and* the 'NOT' keyword?  Why
370          * can't there be *one* way to do things?  Answer: the design of
371          * IMAP suffers from gratuitous complexity.
372          */
373
374         else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
375                 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
376                         match = 1;
377                 }
378                 ++pos;
379         }
380
381         else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
382                 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
383                         match = 1;
384                 }
385                 ++pos;
386         }
387
388         else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
389                 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
390                         match = 1;
391                 }
392                 ++pos;
393         }
394
395         else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
396                 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
397                         match = 1;
398                 }
399                 ++pos;
400         }
401
402         else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
403                 /* FIXME */
404                 pos += 2;
405         }
406
407         else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
408                 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
409                         match = 1;
410                 }
411                 ++pos;
412         }
413
414         /* Remember to negate if we were told to */
415         if (is_not) {
416                 match = !match;
417         }
418
419         /* Keep going if there are more criteria! */
420         if (pos < num_items) {
421
422                 if (is_or) {
423                         match = (match || imap_do_search_msg(seq, msg,
424                                 num_items - pos, &itemlist[pos], is_uid));
425                 }
426                 else {
427                         match = (match && imap_do_search_msg(seq, msg,
428                                 num_items - pos, &itemlist[pos], is_uid));
429                 }
430
431         }
432
433         if (need_to_free_msg) {
434                 CtdlFreeMessage(msg);
435         }
436         return(match);
437 }
438
439
440 /*
441  * imap_search() calls imap_do_search() to do its actual work, once it's
442  * validated and boiled down the request a bit.
443  */
444 void imap_do_search(int num_items, char **itemlist, int is_uid) {
445         int i, j, k;
446         int fts_num_msgs = 0;
447         long *fts_msgs = NULL;
448         int is_in_list = 0;
449         int num_results = 0;
450
451         /* If there is a BODY search criterion in the query, use our full
452          * text index to disqualify messages that don't have any chance of
453          * matching.  (Only do this if the index is enabled!!)
454          */
455         if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
456                 if (!strcasecmp(itemlist[i], "BODY")) {
457                         ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
458                         if (fts_num_msgs > 0) {
459                                 for (j=0; j < IMAP->num_msgs; ++j) {
460                                         if (IMAP->flags[j] & IMAP_SELECTED) {
461                                                 is_in_list = 0;
462                                                 for (k=0; k<fts_num_msgs; ++k) {
463                                                         if (IMAP->msgids[j] == fts_msgs[k]) {
464                                                                 ++is_in_list;
465                                                         }
466                                                 }
467                                         }
468                                         if (!is_in_list) {
469                                                 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
470                                         }
471                                 }
472                         }
473                         else {          /* no hits on the index; disqualify every message */
474                                 for (j=0; j < IMAP->num_msgs; ++j) {
475                                         IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
476                                 }
477                         }
478                         if (fts_msgs) {
479                                 free(fts_msgs);
480                         }
481                 }
482         }
483
484         /* Now go through the messages and apply all search criteria. */
485         buffer_output();
486         cprintf("* SEARCH ");
487         if (IMAP->num_msgs > 0)
488          for (i = 0; i < IMAP->num_msgs; ++i)
489           if (IMAP->flags[i] & IMAP_SELECTED) {
490                 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
491                         if (num_results != 0) {
492                                 cprintf(" ");
493                         }
494                         if (is_uid) {
495                                 cprintf("%ld", IMAP->msgids[i]);
496                         }
497                         else {
498                                 cprintf("%d", i+1);
499                         }
500                         ++num_results;
501                 }
502         }
503         cprintf("\r\n");
504         unbuffer_output();
505 }
506
507
508 /*
509  * This function is called by the main command loop.
510  */
511 void imap_search(int num_parms, char *parms[]) {
512         int i;
513
514         if (num_parms < 3) {
515                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
516                 return;
517         }
518
519         for (i = 0; i < IMAP->num_msgs; ++i) {
520                 IMAP->flags[i] |= IMAP_SELECTED;
521         }
522
523         for (i=1; i<num_parms; ++i) {
524                 if (imap_is_message_set(parms[i])) {
525                         imap_pick_range(parms[i], 0);
526                 }
527         }
528
529         imap_do_search(num_parms-2, &parms[2], 0);
530         cprintf("%s OK SEARCH completed\r\n", parms[0]);
531 }
532
533 /*
534  * This function is called by the main command loop.
535  */
536 void imap_uidsearch(int num_parms, char *parms[]) {
537         int i;
538
539         if (num_parms < 4) {
540                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
541                 return;
542         }
543
544         for (i = 0; i < IMAP->num_msgs; ++i) {
545                 IMAP->flags[i] |= IMAP_SELECTED;
546         }
547
548         for (i=1; i<num_parms; ++i) {
549                 if (imap_is_message_set(parms[i])) {
550                         imap_pick_range(parms[i], 1);
551                 }
552         }
553
554         imap_do_search(num_parms-3, &parms[3], 1);
555         cprintf("%s OK UID SEARCH completed\r\n", parms[0]);
556 }
557
558