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