3dd552192b9d0c4c8c2f957d613aa2fe82961859
[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                 if (msg != NULL) {
115                         fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
116                         if (fieldptr != NULL) {
117                                 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
118                                         match = 1;
119                                 }
120                                 free(fieldptr);
121                         }
122                 }
123                 pos += 2;
124         }
125
126         else if (!strcasecmp(itemlist[pos], "BEFORE")) {
127                 if (msg == NULL) {
128                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
129                         need_to_free_msg = 1;
130                 }
131                 if (msg != NULL) {
132                         if (msg->cm_fields['T'] != NULL) {
133                                 if (imap_datecmp(itemlist[pos+1],
134                                                 atol(msg->cm_fields['T'])) < 0) {
135                                         match = 1;
136                                 }
137                         }
138                 }
139                 pos += 2;
140         }
141
142         else if (!strcasecmp(itemlist[pos], "BODY")) {
143
144                 /* If fulltext indexing is active, on this server,
145                  *  all messages have already been qualified.
146                  */
147                 if (config.c_enable_fulltext) {
148                         match = 1;
149                 }
150
151                 /* Otherwise, we have to do a slow search. */
152                 else {
153                         if (msg == NULL) {
154                                 msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
155                                 need_to_free_msg = 1;
156                         }
157                         if (msg != NULL) {
158                                 if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
159                                         match = 1;
160                                 }
161                         }
162                 }
163
164                 pos += 2;
165         }
166
167         else if (!strcasecmp(itemlist[pos], "CC")) {
168                 if (msg == NULL) {
169                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
170                         need_to_free_msg = 1;
171                 }
172                 if (msg != NULL) {
173                         fieldptr = msg->cm_fields['Y'];
174                         if (fieldptr != NULL) {
175                                 if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
176                                         match = 1;
177                                 }
178                         }
179                         else {
180                                 fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
181                                 if (fieldptr != NULL) {
182                                         if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
183                                                 match = 1;
184                                         }
185                                         free(fieldptr);
186                                 }
187                         }
188                 }
189                 pos += 2;
190         }
191
192         else if (!strcasecmp(itemlist[pos], "DELETED")) {
193                 if (IMAP->flags[seq-1] & IMAP_DELETED) {
194                         match = 1;
195                 }
196                 ++pos;
197         }
198
199         else if (!strcasecmp(itemlist[pos], "DRAFT")) {
200                 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
201                         match = 1;
202                 }
203                 ++pos;
204         }
205
206         else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
207                 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
208                         match = 1;
209                 }
210                 ++pos;
211         }
212
213         else if (!strcasecmp(itemlist[pos], "FROM")) {
214                 if (msg == NULL) {
215                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
216                         need_to_free_msg = 1;
217                 }
218                 if (msg != NULL) {
219                         if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
220                                 match = 1;
221                         }
222                         if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
223                                 match = 1;
224                         }
225                 }
226                 pos += 2;
227         }
228
229         else if (!strcasecmp(itemlist[pos], "HEADER")) {
230
231                 /* We've got to do a slow search for this because the client
232                  * might be asking for an RFC822 header field that has not been
233                  * converted into a Citadel header field.  That requires
234                  * examining the message body.
235                  */
236                 if (msg == NULL) {
237                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
238                         need_to_free_msg = 1;
239                 }
240
241                 if (msg != NULL) {
242         
243                         CC->redirect_buffer = malloc(SIZ);
244                         CC->redirect_len = 0;
245                         CC->redirect_alloc = SIZ;
246                         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1);
247         
248                         fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
249                         if (fieldptr != NULL) {
250                                 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
251                                         match = 1;
252                                 }
253                                 free(fieldptr);
254                         }
255         
256                         free(CC->redirect_buffer);
257                         CC->redirect_buffer = NULL;
258                         CC->redirect_len = 0;
259                         CC->redirect_alloc = 0;
260                 }
261
262                 pos += 3;       /* Yes, three */
263         }
264
265         else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
266                 /* not implemented */
267                 pos += 2;
268         }
269
270         else if (!strcasecmp(itemlist[pos], "LARGER")) {
271                 if (msg == NULL) {
272                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
273                         need_to_free_msg = 1;
274                 }
275                 if (msg != NULL) {
276                         if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
277                                 match = 1;
278                         }
279                 }
280                 pos += 2;
281         }
282
283         else if (!strcasecmp(itemlist[pos], "NEW")) {
284                 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
285                         match = 1;
286                 }
287                 ++pos;
288         }
289
290         else if (!strcasecmp(itemlist[pos], "OLD")) {
291                 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
292                         match = 1;
293                 }
294                 ++pos;
295         }
296
297         else if (!strcasecmp(itemlist[pos], "ON")) {
298                 if (msg == NULL) {
299                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
300                         need_to_free_msg = 1;
301                 }
302                 if (msg != NULL) {
303                         if (msg->cm_fields['T'] != NULL) {
304                                 if (imap_datecmp(itemlist[pos+1],
305                                                 atol(msg->cm_fields['T'])) == 0) {
306                                         match = 1;
307                                 }
308                         }
309                 }
310                 pos += 2;
311         }
312
313         else if (!strcasecmp(itemlist[pos], "RECENT")) {
314                 if (IMAP->flags[seq-1] & IMAP_RECENT) {
315                         match = 1;
316                 }
317                 ++pos;
318         }
319
320         else if (!strcasecmp(itemlist[pos], "SEEN")) {
321                 if (IMAP->flags[seq-1] & IMAP_SEEN) {
322                         match = 1;
323                 }
324                 ++pos;
325         }
326
327         else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
328                 if (msg == NULL) {
329                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
330                         need_to_free_msg = 1;
331                 }
332                 if (msg != NULL) {
333                         if (msg->cm_fields['T'] != NULL) {
334                                 if (imap_datecmp(itemlist[pos+1],
335                                                 atol(msg->cm_fields['T'])) < 0) {
336                                         match = 1;
337                                 }
338                         }
339                 }
340                 pos += 2;
341         }
342
343         else if (!strcasecmp(itemlist[pos], "SENTON")) {
344                 if (msg == NULL) {
345                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
346                         need_to_free_msg = 1;
347                 }
348                 if (msg != NULL) {
349                         if (msg->cm_fields['T'] != NULL) {
350                                 if (imap_datecmp(itemlist[pos+1],
351                                                 atol(msg->cm_fields['T'])) == 0) {
352                                         match = 1;
353                                 }
354                         }
355                 }
356                 pos += 2;
357         }
358
359         else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
360                 if (msg == NULL) {
361                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
362                         need_to_free_msg = 1;
363                 }
364                 if (msg != NULL) {
365                         if (msg->cm_fields['T'] != NULL) {
366                                 if (imap_datecmp(itemlist[pos+1],
367                                                 atol(msg->cm_fields['T'])) >= 0) {
368                                         match = 1;
369                                 }
370                         }
371                 }
372                 pos += 2;
373         }
374
375         else if (!strcasecmp(itemlist[pos], "SINCE")) {
376                 if (msg == NULL) {
377                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
378                         need_to_free_msg = 1;
379                 }
380                 if (msg != NULL) {
381                         if (msg->cm_fields['T'] != NULL) {
382                                 if (imap_datecmp(itemlist[pos+1],
383                                                 atol(msg->cm_fields['T'])) >= 0) {
384                                         match = 1;
385                                 }
386                         }
387                 }
388                 pos += 2;
389         }
390
391         else if (!strcasecmp(itemlist[pos], "SMALLER")) {
392                 if (msg == NULL) {
393                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
394                         need_to_free_msg = 1;
395                 }
396                 if (msg != NULL) {
397                         if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
398                                 match = 1;
399                         }
400                 }
401                 pos += 2;
402         }
403
404         else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
405                 if (msg == NULL) {
406                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
407                         need_to_free_msg = 1;
408                 }
409                 if (msg != NULL) {
410                         if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
411                                 match = 1;
412                         }
413                 }
414                 pos += 2;
415         }
416
417         else if (!strcasecmp(itemlist[pos], "TEXT")) {
418                 if (msg == NULL) {
419                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
420                         need_to_free_msg = 1;
421                 }
422                 if (msg != NULL) {
423                         for (i='A'; i<='Z'; ++i) {
424                                 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
425                                         match = 1;
426                                 }
427                         }
428                 }
429                 pos += 2;
430         }
431
432         else if (!strcasecmp(itemlist[pos], "TO")) {
433                 if (msg == NULL) {
434                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
435                         need_to_free_msg = 1;
436                 }
437                 if (msg != NULL) {
438                         if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
439                                 match = 1;
440                         }
441                 }
442                 pos += 2;
443         }
444
445         /* FIXME this is b0rken.  fix it. */
446         else if (imap_is_message_set(itemlist[pos])) {
447                 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
448                         match = 1;
449                 }
450                 pos += 1;
451         }
452
453         /* FIXME this is b0rken.  fix it. */
454         else if (!strcasecmp(itemlist[pos], "UID")) {
455                 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
456                         match = 1;
457                 }
458                 pos += 2;
459         }
460
461         /* Now here come the 'UN' criteria.  Why oh why do we have to
462          * implement *both* the 'UN' criteria *and* the 'NOT' keyword?  Why
463          * can't there be *one* way to do things?  More gratuitous complexity.
464          */
465
466         else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
467                 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
468                         match = 1;
469                 }
470                 ++pos;
471         }
472
473         else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
474                 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
475                         match = 1;
476                 }
477                 ++pos;
478         }
479
480         else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
481                 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
482                         match = 1;
483                 }
484                 ++pos;
485         }
486
487         else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
488                 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
489                         match = 1;
490                 }
491                 ++pos;
492         }
493
494         else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
495                 /* FIXME */
496                 pos += 2;
497         }
498
499         else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
500                 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
501                         match = 1;
502                 }
503                 ++pos;
504         }
505
506         /* Remember to negate if we were told to */
507         if (is_not) {
508                 match = !match;
509         }
510
511         /* Keep going if there are more criteria! */
512         if (pos < num_items) {
513
514                 if (is_or) {
515                         match = (match || imap_do_search_msg(seq, msg,
516                                 num_items - pos, &itemlist[pos], is_uid));
517                 }
518                 else {
519                         match = (match && imap_do_search_msg(seq, msg,
520                                 num_items - pos, &itemlist[pos], is_uid));
521                 }
522
523         }
524
525         if (need_to_free_msg) {
526                 CtdlFreeMessage(msg);
527         }
528         return(match);
529 }
530
531
532 /*
533  * imap_search() calls imap_do_search() to do its actual work, once it's
534  * validated and boiled down the request a bit.
535  */
536 void imap_do_search(int num_items, char **itemlist, int is_uid) {
537         int i, j, k;
538         int fts_num_msgs = 0;
539         long *fts_msgs = NULL;
540         int is_in_list = 0;
541         int num_results = 0;
542
543         /* If there is a BODY search criterion in the query, use our full
544          * text index to disqualify messages that don't have any chance of
545          * matching.  (Only do this if the index is enabled!!)
546          */
547         if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
548                 if (!strcasecmp(itemlist[i], "BODY")) {
549                         ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
550                         if (fts_num_msgs > 0) {
551                                 for (j=0; j < IMAP->num_msgs; ++j) {
552                                         if (IMAP->flags[j] & IMAP_SELECTED) {
553                                                 is_in_list = 0;
554                                                 for (k=0; k<fts_num_msgs; ++k) {
555                                                         if (IMAP->msgids[j] == fts_msgs[k]) {
556                                                                 ++is_in_list;
557                                                         }
558                                                 }
559                                         }
560                                         if (!is_in_list) {
561                                                 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
562                                         }
563                                 }
564                         }
565                         else {          /* no hits on the index; disqualify every message */
566                                 for (j=0; j < IMAP->num_msgs; ++j) {
567                                         IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
568                                 }
569                         }
570                         if (fts_msgs) {
571                                 free(fts_msgs);
572                         }
573                 }
574         }
575
576         /* Now go through the messages and apply all search criteria. */
577         buffer_output();
578         cprintf("* SEARCH ");
579         if (IMAP->num_msgs > 0)
580          for (i = 0; i < IMAP->num_msgs; ++i)
581           if (IMAP->flags[i] & IMAP_SELECTED) {
582                 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
583                         if (num_results != 0) {
584                                 cprintf(" ");
585                         }
586                         if (is_uid) {
587                                 cprintf("%ld", IMAP->msgids[i]);
588                         }
589                         else {
590                                 cprintf("%d", i+1);
591                         }
592                         ++num_results;
593                 }
594         }
595         cprintf("\r\n");
596         unbuffer_output();
597 }
598
599
600 /*
601  * This function is called by the main command loop.
602  */
603 void imap_search(int num_parms, char *parms[]) {
604         int i;
605
606         if (num_parms < 3) {
607                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
608                 return;
609         }
610
611         for (i = 0; i < IMAP->num_msgs; ++i) {
612                 IMAP->flags[i] |= IMAP_SELECTED;
613         }
614
615         imap_do_search(num_parms-2, &parms[2], 0);
616         cprintf("%s OK SEARCH completed\r\n", parms[0]);
617 }
618
619 /*
620  * This function is called by the main command loop.
621  */
622 void imap_uidsearch(int num_parms, char *parms[]) {
623         int i;
624
625         if (num_parms < 4) {
626                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
627                 return;
628         }
629
630         for (i = 0; i < IMAP->num_msgs; ++i) {
631                 IMAP->flags[i] |= IMAP_SELECTED;
632         }
633
634         imap_do_search(num_parms-3, &parms[3], 1);
635         cprintf("%s OK UID SEARCH completed\r\n", parms[0]);
636 }
637
638