cad499ceb63ad2091768ae552a5d8c99b2243758
[citadel.git] / citadel / modules / imap / imap_search.c
1 /*
2  * Implements IMAP's gratuitously complex SEARCH command.
3  *
4  * Copyright (c) 2001-2012 by the citadel.org team
5  *
6  *  This program is open source software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 3.
8  *  
9  *  
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  
17  *  
18  *  
19  */
20
21 #include "ctdl_module.h"
22
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <ctype.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <libcitadel.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "sysdep_decls.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "user_ops.h"
57 #include "database.h"
58 #include "msgbase.h"
59 #include "internet_addressing.h"
60 #include "serv_imap.h"
61 #include "imap_tools.h"
62 #include "imap_fetch.h"
63 #include "imap_search.h"
64 #include "genstamp.h"
65
66
67 /*
68  * imap_do_search() calls imap_do_search_msg() to search an individual
69  * message after it has been fetched from the disk.  This function returns
70  * nonzero if there is a match.
71  *
72  * supplied_msg MAY be used to pass a pointer to the message in memory,
73  * if for some reason it's already been loaded.  If not, the message will
74  * be loaded only if one or more search criteria require it.
75  */
76 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
77                         int num_items, ConstStr *itemlist, int is_uid) {
78
79         citimap *Imap = IMAP;
80         int match = 0;
81         int is_not = 0;
82         int is_or = 0;
83         int pos = 0;
84         int i;
85         char *fieldptr;
86         struct CtdlMessage *msg = NULL;
87         int need_to_free_msg = 0;
88
89         if (num_items == 0) {
90                 return(0);
91         }
92         msg = supplied_msg;
93
94         /* Initially we start at the beginning. */
95         pos = 0;
96
97         /* Check for the dreaded NOT criterion. */
98         if (!strcasecmp(itemlist[0].Key, "NOT")) {
99                 is_not = 1;
100                 pos = 1;
101         }
102
103         /* Check for the dreaded OR criterion. */
104         if (!strcasecmp(itemlist[0].Key, "OR")) {
105                 is_or = 1;
106                 pos = 1;
107         }
108
109         /* Now look for criteria. */
110         if (!strcasecmp(itemlist[pos].Key, "ALL")) {
111                 match = 1;
112                 ++pos;
113         }
114         
115         else if (!strcasecmp(itemlist[pos].Key, "ANSWERED")) {
116                 if (Imap->flags[seq-1] & IMAP_ANSWERED) {
117                         match = 1;
118                 }
119                 ++pos;
120         }
121
122         else if (!strcasecmp(itemlist[pos].Key, "BCC")) {
123                 if (msg == NULL) {
124                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
125                         need_to_free_msg = 1;
126                 }
127                 if (msg != NULL) {
128                         fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Bcc");
129                         if (fieldptr != NULL) {
130                                 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
131                                         match = 1;
132                                 }
133                                 free(fieldptr);
134                         }
135                 }
136                 pos += 2;
137         }
138
139         else if (!strcasecmp(itemlist[pos].Key, "BEFORE")) {
140                 if (msg == NULL) {
141                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
142                         need_to_free_msg = 1;
143                 }
144                 if (msg != NULL) {
145                         if (!CM_IsEmpty(msg, eTimestamp)) {
146                                 if (imap_datecmp(itemlist[pos+1].Key,
147                                                 atol(msg->cm_fields[eTimestamp])) < 0) {
148                                         match = 1;
149                                 }
150                         }
151                 }
152                 pos += 2;
153         }
154
155         else if (!strcasecmp(itemlist[pos].Key, "BODY")) {
156
157                 /* If fulltext indexing is active, on this server,
158                  *  all messages have already been qualified.
159                  */
160                 if (config.c_enable_fulltext) {
161                         match = 1;
162                 }
163
164                 /* Otherwise, we have to do a slow search. */
165                 else {
166                         if (msg == NULL) {
167                                 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
168                                 need_to_free_msg = 1;
169                         }
170                         if (msg != NULL) {
171                                 if (bmstrcasestr(msg->cm_fields[eMesageText], itemlist[pos+1].Key)) {
172                                         match = 1;
173                                 }
174                         }
175                 }
176
177                 pos += 2;
178         }
179
180         else if (!strcasecmp(itemlist[pos].Key, "CC")) {
181                 if (msg == NULL) {
182                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
183                         need_to_free_msg = 1;
184                 }
185                 if (msg != NULL) {
186                         fieldptr = msg->cm_fields[eCarbonCopY];
187                         if (fieldptr != NULL) {
188                                 if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
189                                         match = 1;
190                                 }
191                         }
192                         else {
193                                 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Cc");
194                                 if (fieldptr != NULL) {
195                                         if (bmstrcasestr(fieldptr, itemlist[pos+1].Key)) {
196                                                 match = 1;
197                                         }
198                                         free(fieldptr);
199                                 }
200                         }
201                 }
202                 pos += 2;
203         }
204
205         else if (!strcasecmp(itemlist[pos].Key, "DELETED")) {
206                 if (Imap->flags[seq-1] & IMAP_DELETED) {
207                         match = 1;
208                 }
209                 ++pos;
210         }
211
212         else if (!strcasecmp(itemlist[pos].Key, "DRAFT")) {
213                 if (Imap->flags[seq-1] & IMAP_DRAFT) {
214                         match = 1;
215                 }
216                 ++pos;
217         }
218
219         else if (!strcasecmp(itemlist[pos].Key, "FLAGGED")) {
220                 if (Imap->flags[seq-1] & IMAP_FLAGGED) {
221                         match = 1;
222                 }
223                 ++pos;
224         }
225
226         else if (!strcasecmp(itemlist[pos].Key, "FROM")) {
227                 if (msg == NULL) {
228                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
229                         need_to_free_msg = 1;
230                 }
231                 if (msg != NULL) {
232                         if (bmstrcasestr(msg->cm_fields[eAuthor], itemlist[pos+1].Key)) {
233                                 match = 1;
234                         }
235                         if (bmstrcasestr(msg->cm_fields[erFc822Addr], itemlist[pos+1].Key)) {
236                                 match = 1;
237                         }
238                 }
239                 pos += 2;
240         }
241
242         else if (!strcasecmp(itemlist[pos].Key, "HEADER")) {
243
244                 /* We've got to do a slow search for this because the client
245                  * might be asking for an RFC822 header field that has not been
246                  * converted into a Citadel header field.  That requires
247                  * examining the message body.
248                  */
249                 if (msg == NULL) {
250                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
251                         need_to_free_msg = 1;
252                 }
253
254                 if (msg != NULL) {
255         
256                         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
257                         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
258         
259                         fieldptr = rfc822_fetch_field(ChrPtr(CC->redirect_buffer), itemlist[pos+1].Key);
260                         if (fieldptr != NULL) {
261                                 if (bmstrcasestr(fieldptr, itemlist[pos+2].Key)) {
262                                         match = 1;
263                                 }
264                                 free(fieldptr);
265                         }
266         
267                         FreeStrBuf(&CC->redirect_buffer);
268                 }
269
270                 pos += 3;       /* Yes, three */
271         }
272
273         else if (!strcasecmp(itemlist[pos].Key, "KEYWORD")) {
274                 /* not implemented */
275                 pos += 2;
276         }
277
278         else if (!strcasecmp(itemlist[pos].Key, "LARGER")) {
279                 if (msg == NULL) {
280                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
281                         need_to_free_msg = 1;
282                 }
283                 if (msg != NULL) {
284                         if (msg->cm_lengths[eMesageText] > atoi(itemlist[pos+1].Key)) {
285                                 match = 1;
286                         }
287                 }
288                 pos += 2;
289         }
290
291         else if (!strcasecmp(itemlist[pos].Key, "NEW")) {
292                 if ( (Imap->flags[seq-1] & IMAP_RECENT) && (!(Imap->flags[seq-1] & IMAP_SEEN))) {
293                         match = 1;
294                 }
295                 ++pos;
296         }
297
298         else if (!strcasecmp(itemlist[pos].Key, "OLD")) {
299                 if (!(Imap->flags[seq-1] & IMAP_RECENT)) {
300                         match = 1;
301                 }
302                 ++pos;
303         }
304
305         else if (!strcasecmp(itemlist[pos].Key, "ON")) {
306                 if (msg == NULL) {
307                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
308                         need_to_free_msg = 1;
309                 }
310                 if (msg != NULL) {
311                         if (!CM_IsEmpty(msg, eTimestamp)) {
312                                 if (imap_datecmp(itemlist[pos+1].Key,
313                                                 atol(msg->cm_fields[eTimestamp])) == 0) {
314                                         match = 1;
315                                 }
316                         }
317                 }
318                 pos += 2;
319         }
320
321         else if (!strcasecmp(itemlist[pos].Key, "RECENT")) {
322                 if (Imap->flags[seq-1] & IMAP_RECENT) {
323                         match = 1;
324                 }
325                 ++pos;
326         }
327
328         else if (!strcasecmp(itemlist[pos].Key, "SEEN")) {
329                 if (Imap->flags[seq-1] & IMAP_SEEN) {
330                         match = 1;
331                 }
332                 ++pos;
333         }
334
335         else if (!strcasecmp(itemlist[pos].Key, "SENTBEFORE")) {
336                 if (msg == NULL) {
337                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
338                         need_to_free_msg = 1;
339                 }
340                 if (msg != NULL) {
341                         if (!CM_IsEmpty(msg, eTimestamp)) {
342                                 if (imap_datecmp(itemlist[pos+1].Key,
343                                                 atol(msg->cm_fields[eTimestamp])) < 0) {
344                                         match = 1;
345                                 }
346                         }
347                 }
348                 pos += 2;
349         }
350
351         else if (!strcasecmp(itemlist[pos].Key, "SENTON")) {
352                 if (msg == NULL) {
353                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
354                         need_to_free_msg = 1;
355                 }
356                 if (msg != NULL) {
357                         if (!CM_IsEmpty(msg, eTimestamp)) {
358                                 if (imap_datecmp(itemlist[pos+1].Key,
359                                                 atol(msg->cm_fields[eTimestamp])) == 0) {
360                                         match = 1;
361                                 }
362                         }
363                 }
364                 pos += 2;
365         }
366
367         else if (!strcasecmp(itemlist[pos].Key, "SENTSINCE")) {
368                 if (msg == NULL) {
369                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
370                         need_to_free_msg = 1;
371                 }
372                 if (msg != NULL) {
373                         if (!CM_IsEmpty(msg, eTimestamp)) {
374                                 if (imap_datecmp(itemlist[pos+1].Key,
375                                                 atol(msg->cm_fields[eTimestamp])) >= 0) {
376                                         match = 1;
377                                 }
378                         }
379                 }
380                 pos += 2;
381         }
382
383         else if (!strcasecmp(itemlist[pos].Key, "SINCE")) {
384                 if (msg == NULL) {
385                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
386                         need_to_free_msg = 1;
387                 }
388                 if (msg != NULL) {
389                         if (!CM_IsEmpty(msg, eTimestamp)) {
390                                 if (imap_datecmp(itemlist[pos+1].Key,
391                                                 atol(msg->cm_fields[eTimestamp])) >= 0) {
392                                         match = 1;
393                                 }
394                         }
395                 }
396                 pos += 2;
397         }
398
399         else if (!strcasecmp(itemlist[pos].Key, "SMALLER")) {
400                 if (msg == NULL) {
401                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
402                         need_to_free_msg = 1;
403                 }
404                 if (msg != NULL) {
405                         if (msg->cm_lengths[eMesageText] < atoi(itemlist[pos+1].Key)) {
406                                 match = 1;
407                         }
408                 }
409                 pos += 2;
410         }
411
412         else if (!strcasecmp(itemlist[pos].Key, "SUBJECT")) {
413                 if (msg == NULL) {
414                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
415                         need_to_free_msg = 1;
416                 }
417                 if (msg != NULL) {
418                         if (bmstrcasestr(msg->cm_fields[eMsgSubject], itemlist[pos+1].Key)) {
419                                 match = 1;
420                         }
421                 }
422                 pos += 2;
423         }
424
425         else if (!strcasecmp(itemlist[pos].Key, "TEXT")) {
426                 if (msg == NULL) {
427                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
428                         need_to_free_msg = 1;
429                 }
430                 if (msg != NULL) {
431                         for (i='A'; i<='Z'; ++i) {
432                                 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1].Key)) {
433                                         match = 1;
434                                 }
435                         }
436                 }
437                 pos += 2;
438         }
439
440         else if (!strcasecmp(itemlist[pos].Key, "TO")) {
441                 if (msg == NULL) {
442                         msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
443                         need_to_free_msg = 1;
444                 }
445                 if (msg != NULL) {
446                         if (bmstrcasestr(msg->cm_fields[eRecipient], itemlist[pos+1].Key)) {
447                                 match = 1;
448                         }
449                 }
450                 pos += 2;
451         }
452
453         /* FIXME this is b0rken.  fix it. */
454         else if (imap_is_message_set(itemlist[pos].Key)) {
455                 if (is_msg_in_sequence_set(itemlist[pos].Key, seq)) {
456                         match = 1;
457                 }
458                 pos += 1;
459         }
460
461         /* FIXME this is b0rken.  fix it. */
462         else if (!strcasecmp(itemlist[pos].Key, "UID")) {
463                 if (is_msg_in_sequence_set(itemlist[pos+1].Key, Imap->msgids[seq-1])) {
464                         match = 1;
465                 }
466                 pos += 2;
467         }
468
469         /* Now here come the 'UN' criteria.  Why oh why do we have to
470          * implement *both* the 'UN' criteria *and* the 'NOT' keyword?  Why
471          * can't there be *one* way to do things?  More gratuitous complexity.
472          */
473
474         else if (!strcasecmp(itemlist[pos].Key, "UNANSWERED")) {
475                 if ((Imap->flags[seq-1] & IMAP_ANSWERED) == 0) {
476                         match = 1;
477                 }
478                 ++pos;
479         }
480
481         else if (!strcasecmp(itemlist[pos].Key, "UNDELETED")) {
482                 if ((Imap->flags[seq-1] & IMAP_DELETED) == 0) {
483                         match = 1;
484                 }
485                 ++pos;
486         }
487
488         else if (!strcasecmp(itemlist[pos].Key, "UNDRAFT")) {
489                 if ((Imap->flags[seq-1] & IMAP_DRAFT) == 0) {
490                         match = 1;
491                 }
492                 ++pos;
493         }
494
495         else if (!strcasecmp(itemlist[pos].Key, "UNFLAGGED")) {
496                 if ((Imap->flags[seq-1] & IMAP_FLAGGED) == 0) {
497                         match = 1;
498                 }
499                 ++pos;
500         }
501
502         else if (!strcasecmp(itemlist[pos].Key, "UNKEYWORD")) {
503                 /* FIXME */
504                 pos += 2;
505         }
506
507         else if (!strcasecmp(itemlist[pos].Key, "UNSEEN")) {
508                 if ((Imap->flags[seq-1] & IMAP_SEEN) == 0) {
509                         match = 1;
510                 }
511                 ++pos;
512         }
513
514         /* Remember to negate if we were told to */
515         if (is_not) {
516                 match = !match;
517         }
518
519         /* Keep going if there are more criteria! */
520         if (pos < num_items) {
521
522                 if (is_or) {
523                         match = (match || imap_do_search_msg(seq, msg,
524                                 num_items - pos, &itemlist[pos], is_uid));
525                 }
526                 else {
527                         match = (match && imap_do_search_msg(seq, msg,
528                                 num_items - pos, &itemlist[pos], is_uid));
529                 }
530
531         }
532
533         if (need_to_free_msg) {
534                 CM_Free(msg);
535         }
536         return(match);
537 }
538
539
540 /*
541  * imap_search() calls imap_do_search() to do its actual work, once it's
542  * validated and boiled down the request a bit.
543  */
544 void imap_do_search(int num_items, ConstStr *itemlist, int is_uid) {
545         citimap *Imap = IMAP;
546         int i, j, k;
547         int fts_num_msgs = 0;
548         long *fts_msgs = NULL;
549         int is_in_list = 0;
550         int num_results = 0;
551
552         /* Strip parentheses.  We realize that this method will not work
553          * in all cases, but it seems to work with all currently available
554          * client software.  Revisit later...
555          */
556         for (i=0; i<num_items; ++i) {
557                 if (itemlist[i].Key[0] == '(') {
558                         
559                         TokenCutLeft(&Imap->Cmd, 
560                                      &itemlist[i], 
561                                      1);
562                 }
563                 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
564                         TokenCutRight(&Imap->Cmd, 
565                                       &itemlist[i], 
566                                       1);
567                 }
568         }
569
570         /* If there is a BODY search criterion in the query, use our full
571          * text index to disqualify messages that don't have any chance of
572          * matching.  (Only do this if the index is enabled!!)
573          */
574         if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
575                 if (!strcasecmp(itemlist[i].Key, "BODY")) {
576                         CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "fulltext");
577                         if (fts_num_msgs > 0) {
578                                 for (j=0; j < Imap->num_msgs; ++j) {
579                                         if (Imap->flags[j] & IMAP_SELECTED) {
580                                                 is_in_list = 0;
581                                                 for (k=0; k<fts_num_msgs; ++k) {
582                                                         if (Imap->msgids[j] == fts_msgs[k]) {
583                                                                 ++is_in_list;
584                                                         }
585                                                 }
586                                         }
587                                         if (!is_in_list) {
588                                                 Imap->flags[j] = Imap->flags[j] & ~IMAP_SELECTED;
589                                         }
590                                 }
591                         }
592                         else {          /* no hits on the index; disqualify every message */
593                                 for (j=0; j < Imap->num_msgs; ++j) {
594                                         Imap->flags[j] = Imap->flags[j] & ~IMAP_SELECTED;
595                                 }
596                         }
597                         if (fts_msgs) {
598                                 free(fts_msgs);
599                         }
600                 }
601         }
602
603         /* Now go through the messages and apply all search criteria. */
604         buffer_output();
605         IAPuts("* SEARCH ");
606         if (Imap->num_msgs > 0)
607          for (i = 0; i < Imap->num_msgs; ++i)
608           if (Imap->flags[i] & IMAP_SELECTED) {
609                 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
610                         if (num_results != 0) {
611                                 IAPuts(" ");
612                         }
613                         if (is_uid) {
614                                 IAPrintf("%ld", Imap->msgids[i]);
615                         }
616                         else {
617                                 IAPrintf("%d", i+1);
618                         }
619                         ++num_results;
620                 }
621         }
622         IAPuts("\r\n");
623         unbuffer_output();
624 }
625
626
627 /*
628  * This function is called by the main command loop.
629  */
630 void imap_search(int num_parms, ConstStr *Params) {
631         int i;
632
633         if (num_parms < 3) {
634                 IReply("BAD invalid parameters");
635                 return;
636         }
637
638         for (i = 0; i < IMAP->num_msgs; ++i) {
639                 IMAP->flags[i] |= IMAP_SELECTED;
640         }
641
642         imap_do_search(num_parms-2, &Params[2], 0);
643         IReply("OK SEARCH completed");
644 }
645
646 /*
647  * This function is called by the main command loop.
648  */
649 void imap_uidsearch(int num_parms, ConstStr *Params) {
650         int i;
651
652         if (num_parms < 4) {
653                 IReply("BAD invalid parameters");
654                 return;
655         }
656
657         for (i = 0; i < IMAP->num_msgs; ++i) {
658                 IMAP->flags[i] |= IMAP_SELECTED;
659         }
660
661         imap_do_search(num_parms-3, &Params[3], 1);
662         IReply("OK UID SEARCH completed");
663 }
664
665