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