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