90644b1e4efea9cc70f7a20f14a30c3d1955fffc
[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 "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 "policy.h"
58 #include "database.h"
59 #include "msgbase.h"
60 #include "internet_addressing.h"
61 #include "serv_imap.h"
62 #include "imap_tools.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, char **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], "NOT")) {
99                 is_not = 1;
100                 pos = 1;
101         }
102
103         /* Check for the dreaded OR criterion. */
104         if (!strcasecmp(itemlist[0], "OR")) {
105                 is_or = 1;
106                 pos = 1;
107         }
108
109         /* Now look for criteria. */
110         if (!strcasecmp(itemlist[pos], "ALL")) {
111                 match = 1;
112                 ++pos;
113         }
114         
115         else if (!strcasecmp(itemlist[pos], "ANSWERED")) {
116                 if (IMAP->flags[seq-1] & IMAP_ANSWERED) {
117                         match = 1;
118                 }
119                 ++pos;
120         }
121
122         else if (!strcasecmp(itemlist[pos], "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])) {
131                                         match = 1;
132                                 }
133                                 free(fieldptr);
134                         }
135                 }
136                 pos += 2;
137         }
138
139         else if (!strcasecmp(itemlist[pos], "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],
147                                                 atol(msg->cm_fields['T'])) < 0) {
148                                         match = 1;
149                                 }
150                         }
151                 }
152                 pos += 2;
153         }
154
155         else if (!strcasecmp(itemlist[pos], "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])) {
172                                         match = 1;
173                                 }
174                         }
175                 }
176
177                 pos += 2;
178         }
179
180         else if (!strcasecmp(itemlist[pos], "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])) {
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])) {
196                                                 match = 1;
197                                         }
198                                         free(fieldptr);
199                                 }
200                         }
201                 }
202                 pos += 2;
203         }
204
205         else if (!strcasecmp(itemlist[pos], "DELETED")) {
206                 if (IMAP->flags[seq-1] & IMAP_DELETED) {
207                         match = 1;
208                 }
209                 ++pos;
210         }
211
212         else if (!strcasecmp(itemlist[pos], "DRAFT")) {
213                 if (IMAP->flags[seq-1] & IMAP_DRAFT) {
214                         match = 1;
215                 }
216                 ++pos;
217         }
218
219         else if (!strcasecmp(itemlist[pos], "FLAGGED")) {
220                 if (IMAP->flags[seq-1] & IMAP_FLAGGED) {
221                         match = 1;
222                 }
223                 ++pos;
224         }
225
226         else if (!strcasecmp(itemlist[pos], "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])) {
233                                 match = 1;
234                         }
235                         if (bmstrcasestr(msg->cm_fields['F'], itemlist[pos+1])) {
236                                 match = 1;
237                         }
238                 }
239                 pos += 2;
240         }
241
242         else if (!strcasecmp(itemlist[pos], "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 = malloc(SIZ);
257                         CC->redirect_len = 0;
258                         CC->redirect_alloc = SIZ;
259                         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_FAST, 0, 1, 0);
260         
261                         fieldptr = rfc822_fetch_field(CC->redirect_buffer, itemlist[pos+1]);
262                         if (fieldptr != NULL) {
263                                 if (bmstrcasestr(fieldptr, itemlist[pos+2])) {
264                                         match = 1;
265                                 }
266                                 free(fieldptr);
267                         }
268         
269                         free(CC->redirect_buffer);
270                         CC->redirect_buffer = NULL;
271                         CC->redirect_len = 0;
272                         CC->redirect_alloc = 0;
273                 }
274
275                 pos += 3;       /* Yes, three */
276         }
277
278         else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
279                 /* not implemented */
280                 pos += 2;
281         }
282
283         else if (!strcasecmp(itemlist[pos], "LARGER")) {
284                 if (msg == NULL) {
285                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
286                         need_to_free_msg = 1;
287                 }
288                 if (msg != NULL) {
289                         if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
290                                 match = 1;
291                         }
292                 }
293                 pos += 2;
294         }
295
296         else if (!strcasecmp(itemlist[pos], "NEW")) {
297                 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
298                         match = 1;
299                 }
300                 ++pos;
301         }
302
303         else if (!strcasecmp(itemlist[pos], "OLD")) {
304                 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
305                         match = 1;
306                 }
307                 ++pos;
308         }
309
310         else if (!strcasecmp(itemlist[pos], "ON")) {
311                 if (msg == NULL) {
312                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
313                         need_to_free_msg = 1;
314                 }
315                 if (msg != NULL) {
316                         if (msg->cm_fields['T'] != NULL) {
317                                 if (imap_datecmp(itemlist[pos+1],
318                                                 atol(msg->cm_fields['T'])) == 0) {
319                                         match = 1;
320                                 }
321                         }
322                 }
323                 pos += 2;
324         }
325
326         else if (!strcasecmp(itemlist[pos], "RECENT")) {
327                 if (IMAP->flags[seq-1] & IMAP_RECENT) {
328                         match = 1;
329                 }
330                 ++pos;
331         }
332
333         else if (!strcasecmp(itemlist[pos], "SEEN")) {
334                 if (IMAP->flags[seq-1] & IMAP_SEEN) {
335                         match = 1;
336                 }
337                 ++pos;
338         }
339
340         else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
341                 if (msg == NULL) {
342                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
343                         need_to_free_msg = 1;
344                 }
345                 if (msg != NULL) {
346                         if (msg->cm_fields['T'] != NULL) {
347                                 if (imap_datecmp(itemlist[pos+1],
348                                                 atol(msg->cm_fields['T'])) < 0) {
349                                         match = 1;
350                                 }
351                         }
352                 }
353                 pos += 2;
354         }
355
356         else if (!strcasecmp(itemlist[pos], "SENTON")) {
357                 if (msg == NULL) {
358                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
359                         need_to_free_msg = 1;
360                 }
361                 if (msg != NULL) {
362                         if (msg->cm_fields['T'] != NULL) {
363                                 if (imap_datecmp(itemlist[pos+1],
364                                                 atol(msg->cm_fields['T'])) == 0) {
365                                         match = 1;
366                                 }
367                         }
368                 }
369                 pos += 2;
370         }
371
372         else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
373                 if (msg == NULL) {
374                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
375                         need_to_free_msg = 1;
376                 }
377                 if (msg != NULL) {
378                         if (msg->cm_fields['T'] != NULL) {
379                                 if (imap_datecmp(itemlist[pos+1],
380                                                 atol(msg->cm_fields['T'])) >= 0) {
381                                         match = 1;
382                                 }
383                         }
384                 }
385                 pos += 2;
386         }
387
388         else if (!strcasecmp(itemlist[pos], "SINCE")) {
389                 if (msg == NULL) {
390                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
391                         need_to_free_msg = 1;
392                 }
393                 if (msg != NULL) {
394                         if (msg->cm_fields['T'] != NULL) {
395                                 if (imap_datecmp(itemlist[pos+1],
396                                                 atol(msg->cm_fields['T'])) >= 0) {
397                                         match = 1;
398                                 }
399                         }
400                 }
401                 pos += 2;
402         }
403
404         else if (!strcasecmp(itemlist[pos], "SMALLER")) {
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 (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
411                                 match = 1;
412                         }
413                 }
414                 pos += 2;
415         }
416
417         else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
418                 if (msg == NULL) {
419                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
420                         need_to_free_msg = 1;
421                 }
422                 if (msg != NULL) {
423                         if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
424                                 match = 1;
425                         }
426                 }
427                 pos += 2;
428         }
429
430         else if (!strcasecmp(itemlist[pos], "TEXT")) {
431                 if (msg == NULL) {
432                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
433                         need_to_free_msg = 1;
434                 }
435                 if (msg != NULL) {
436                         for (i='A'; i<='Z'; ++i) {
437                                 if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
438                                         match = 1;
439                                 }
440                         }
441                 }
442                 pos += 2;
443         }
444
445         else if (!strcasecmp(itemlist[pos], "TO")) {
446                 if (msg == NULL) {
447                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
448                         need_to_free_msg = 1;
449                 }
450                 if (msg != NULL) {
451                         if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
452                                 match = 1;
453                         }
454                 }
455                 pos += 2;
456         }
457
458         /* FIXME this is b0rken.  fix it. */
459         else if (imap_is_message_set(itemlist[pos])) {
460                 if (is_msg_in_sequence_set(itemlist[pos], seq)) {
461                         match = 1;
462                 }
463                 pos += 1;
464         }
465
466         /* FIXME this is b0rken.  fix it. */
467         else if (!strcasecmp(itemlist[pos], "UID")) {
468                 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
469                         match = 1;
470                 }
471                 pos += 2;
472         }
473
474         /* Now here come the 'UN' criteria.  Why oh why do we have to
475          * implement *both* the 'UN' criteria *and* the 'NOT' keyword?  Why
476          * can't there be *one* way to do things?  More gratuitous complexity.
477          */
478
479         else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
480                 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
481                         match = 1;
482                 }
483                 ++pos;
484         }
485
486         else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
487                 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
488                         match = 1;
489                 }
490                 ++pos;
491         }
492
493         else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
494                 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
495                         match = 1;
496                 }
497                 ++pos;
498         }
499
500         else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
501                 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
502                         match = 1;
503                 }
504                 ++pos;
505         }
506
507         else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
508                 /* FIXME */
509                 pos += 2;
510         }
511
512         else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
513                 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
514                         match = 1;
515                 }
516                 ++pos;
517         }
518
519         /* Remember to negate if we were told to */
520         if (is_not) {
521                 match = !match;
522         }
523
524         /* Keep going if there are more criteria! */
525         if (pos < num_items) {
526
527                 if (is_or) {
528                         match = (match || imap_do_search_msg(seq, msg,
529                                 num_items - pos, &itemlist[pos], is_uid));
530                 }
531                 else {
532                         match = (match && imap_do_search_msg(seq, msg,
533                                 num_items - pos, &itemlist[pos], is_uid));
534                 }
535
536         }
537
538         if (need_to_free_msg) {
539                 CtdlFreeMessage(msg);
540         }
541         return(match);
542 }
543
544
545 /*
546  * imap_search() calls imap_do_search() to do its actual work, once it's
547  * validated and boiled down the request a bit.
548  */
549 void imap_do_search(int num_items, char **itemlist, int is_uid) {
550         int i, j, k;
551         int fts_num_msgs = 0;
552         long *fts_msgs = NULL;
553         int is_in_list = 0;
554         int num_results = 0;
555
556         /* Strip parentheses.  We realize that this method will not work
557          * in all cases, but it seems to work with all currently available
558          * client software.  Revisit later...
559          */
560         for (i=0; i<num_items; ++i) {
561                 if (itemlist[i][0] == '(') {
562                         strcpy(&itemlist[i][0], &itemlist[i][1]);
563                 }
564                 if (itemlist[i][strlen(itemlist[i])-1] == ')') {
565                         itemlist[i][strlen(itemlist[i])-1] = 0;
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], "BODY")) {
575                         CtdlModuleDoSearch(&fts_num_msgs, &fts_msgs, itemlist[i+1], "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, char *parms[]) {
630         int i;
631
632         if (num_parms < 3) {
633                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
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, &parms[2], 0);
642         cprintf("%s OK SEARCH completed\r\n", parms[0]);
643 }
644
645 /*
646  * This function is called by the main command loop.
647  */
648 void imap_uidsearch(int num_parms, char *parms[]) {
649         int i;
650
651         if (num_parms < 4) {
652                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
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, &parms[3], 1);
661         cprintf("%s OK UID SEARCH completed\r\n", parms[0]);
662 }
663
664