More removal of $Id$ tags
[citadel.git] / citadel / modules / imap / imap_search.c
1 /*
2  * Implements IMAP's gratuitously complex SEARCH command.
3  *
4  *
5  * Copyright (c) 2001-2009 by the citadel.org team
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "ctdl_module.h"
23
24
25 #include "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <pwd.h>
32 #include <errno.h>
33 #include <sys/types.h>
34
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45
46 #include <sys/wait.h>
47 #include <ctype.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "sysdep_decls.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "user_ops.h"
58 #include "database.h"
59 #include "msgbase.h"
60 #include "internet_addressing.h"
61 #include "imap_tools.h"
62 #include "serv_imap.h"
63 #include "imap_fetch.h"
64 #include "imap_search.h"
65 #include "genstamp.h"
66
67
68 /*
69  * imap_do_search() calls imap_do_search_msg() to search an individual
70  * message after it has been fetched from the disk.  This function returns
71  * nonzero if there is a match.
72  *
73  * supplied_msg MAY be used to pass a pointer to the message in memory,
74  * if for some reason it's already been loaded.  If not, the message will
75  * be loaded only if one or more search criteria require it.
76  */
77 int imap_do_search_msg(int seq, struct CtdlMessage *supplied_msg,
78                         int num_items, ConstStr *itemlist, int is_uid) {
79
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['M'], "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 (msg->cm_fields['T'] != NULL) {
146                                 if (imap_datecmp(itemlist[pos+1].Key,
147                                                 atol(msg->cm_fields['T'])) < 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['M'], 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['Y'];
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['M'], "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['A'], itemlist[pos+1].Key)) {
233                                 match = 1;
234                         }
235                         if (bmstrcasestr(msg->cm_fields['F'], 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 (strlen(msg->cm_fields['M']) > 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 (msg->cm_fields['T'] != NULL) {
312                                 if (imap_datecmp(itemlist[pos+1].Key,
313                                                 atol(msg->cm_fields['T'])) == 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 (msg->cm_fields['T'] != NULL) {
342                                 if (imap_datecmp(itemlist[pos+1].Key,
343                                                 atol(msg->cm_fields['T'])) < 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 (msg->cm_fields['T'] != NULL) {
358                                 if (imap_datecmp(itemlist[pos+1].Key,
359                                                 atol(msg->cm_fields['T'])) == 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 (msg->cm_fields['T'] != NULL) {
374                                 if (imap_datecmp(itemlist[pos+1].Key,
375                                                 atol(msg->cm_fields['T'])) >= 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 (msg->cm_fields['T'] != NULL) {
390                                 if (imap_datecmp(itemlist[pos+1].Key,
391                                                 atol(msg->cm_fields['T'])) >= 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 (strlen(msg->cm_fields['M']) < 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['U'], 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['R'], 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                 CtdlFreeMessage(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         int i, j, k;
546         int fts_num_msgs = 0;
547         long *fts_msgs = NULL;
548         int is_in_list = 0;
549         int num_results = 0;
550
551         /* Strip parentheses.  We realize that this method will not work
552          * in all cases, but it seems to work with all currently available
553          * client software.  Revisit later...
554          */
555         for (i=0; i<num_items; ++i) {
556                 if (itemlist[i].Key[0] == '(') {
557                         
558                         TokenCutLeft(&IMAP->Cmd, 
559                                      &itemlist[i], 
560                                      1);
561                 }
562                 if (itemlist[i].Key[itemlist[i].len-1] == ')') {
563                         TokenCutRight(&IMAP->Cmd, 
564                                       &itemlist[i], 
565                                       1);
566                 }
567         }
568
569         /* If there is a BODY search criterion in the query, use our full
570          * text index to disqualify messages that don't have any chance of
571          * matching.  (Only do this if the index is enabled!!)
572          */
573         if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
574                 if (!strcasecmp(itemlist[i].Key, "BODY")) {
575                         CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1].Key, "fulltext");
576                         if (fts_num_msgs > 0) {
577                                 for (j=0; j < IMAP->num_msgs; ++j) {
578                                         if (IMAP->flags[j] & IMAP_SELECTED) {
579                                                 is_in_list = 0;
580                                                 for (k=0; k<fts_num_msgs; ++k) {
581                                                         if (IMAP->msgids[j] == fts_msgs[k]) {
582                                                                 ++is_in_list;
583                                                         }
584                                                 }
585                                         }
586                                         if (!is_in_list) {
587                                                 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
588                                         }
589                                 }
590                         }
591                         else {          /* no hits on the index; disqualify every message */
592                                 for (j=0; j < IMAP->num_msgs; ++j) {
593                                         IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
594                                 }
595                         }
596                         if (fts_msgs) {
597                                 free(fts_msgs);
598                         }
599                 }
600         }
601
602         /* Now go through the messages and apply all search criteria. */
603         buffer_output();
604         cprintf("* SEARCH ");
605         if (IMAP->num_msgs > 0)
606          for (i = 0; i < IMAP->num_msgs; ++i)
607           if (IMAP->flags[i] & IMAP_SELECTED) {
608                 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
609                         if (num_results != 0) {
610                                 cprintf(" ");
611                         }
612                         if (is_uid) {
613                                 cprintf("%ld", IMAP->msgids[i]);
614                         }
615                         else {
616                                 cprintf("%d", i+1);
617                         }
618                         ++num_results;
619                 }
620         }
621         cprintf("\r\n");
622         unbuffer_output();
623 }
624
625
626 /*
627  * This function is called by the main command loop.
628  */
629 void imap_search(int num_parms, ConstStr *Params) {
630         int i;
631
632         if (num_parms < 3) {
633                 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
634                 return;
635         }
636
637         for (i = 0; i < IMAP->num_msgs; ++i) {
638                 IMAP->flags[i] |= IMAP_SELECTED;
639         }
640
641         imap_do_search(num_parms-2, &Params[2], 0);
642         cprintf("%s OK SEARCH completed\r\n", Params[0].Key);
643 }
644
645 /*
646  * This function is called by the main command loop.
647  */
648 void imap_uidsearch(int num_parms, ConstStr *Params) {
649         int i;
650
651         if (num_parms < 4) {
652                 cprintf("%s BAD invalid parameters\r\n", Params[0].Key);
653                 return;
654         }
655
656         for (i = 0; i < IMAP->num_msgs; ++i) {
657                 IMAP->flags[i] |= IMAP_SELECTED;
658         }
659
660         imap_do_search(num_parms-3, &Params[3], 1);
661         cprintf("%s OK UID SEARCH completed\r\n", Params[0].Key);
662 }
663
664