]> code.citadel.org Git - citadel.git/blob - citadel/imap_search.c
* fix_scrollbar_bug is now a class instead of an id. Fixes validator warnings.
[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                 pos += 2;
214         }
215
216         else if (!strcasecmp(itemlist[pos], "HEADER")) {
217                 /* FIXME */
218                 pos += 3;       /* Yes, three */
219         }
220
221         else if (!strcasecmp(itemlist[pos], "KEYWORD")) {
222                 /* FIXME */
223                 pos += 2;
224         }
225
226         else if (!strcasecmp(itemlist[pos], "LARGER")) {
227                 if (msg == NULL) {
228                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
229                         need_to_free_msg = 1;
230                 }
231                 if (strlen(msg->cm_fields['M']) > atoi(itemlist[pos+1])) {
232                         match = 1;
233                 }
234                 pos += 2;
235         }
236
237         else if (!strcasecmp(itemlist[pos], "NEW")) {
238                 if ( (IMAP->flags[seq-1] & IMAP_RECENT) && (!(IMAP->flags[seq-1] & IMAP_SEEN))) {
239                         match = 1;
240                 }
241                 ++pos;
242         }
243
244         else if (!strcasecmp(itemlist[pos], "OLD")) {
245                 if (!(IMAP->flags[seq-1] & IMAP_RECENT)) {
246                         match = 1;
247                 }
248                 ++pos;
249         }
250
251         else if (!strcasecmp(itemlist[pos], "ON")) {
252                 if (msg == NULL) {
253                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
254                         need_to_free_msg = 1;
255                 }
256                 if (msg->cm_fields['T'] != NULL) {
257                         if (imap_datecmp(itemlist[pos+1],
258                                         atol(msg->cm_fields['T'])) == 0) {
259                                 match = 1;
260                         }
261                 }
262                 pos += 2;
263         }
264
265         else if (!strcasecmp(itemlist[pos], "RECENT")) {
266                 if (IMAP->flags[seq-1] & IMAP_RECENT) {
267                         match = 1;
268                 }
269                 ++pos;
270         }
271
272         else if (!strcasecmp(itemlist[pos], "SEEN")) {
273                 if (IMAP->flags[seq-1] & IMAP_SEEN) {
274                         match = 1;
275                 }
276                 ++pos;
277         }
278
279         else if (!strcasecmp(itemlist[pos], "SENTBEFORE")) {
280                 if (msg == NULL) {
281                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
282                         need_to_free_msg = 1;
283                 }
284                 if (msg->cm_fields['T'] != NULL) {
285                         if (imap_datecmp(itemlist[pos+1],
286                                         atol(msg->cm_fields['T'])) < 0) {
287                                 match = 1;
288                         }
289                 }
290                 pos += 2;
291         }
292
293         else if (!strcasecmp(itemlist[pos], "SENTON")) {
294                 if (msg == NULL) {
295                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
296                         need_to_free_msg = 1;
297                 }
298                 if (msg->cm_fields['T'] != NULL) {
299                         if (imap_datecmp(itemlist[pos+1],
300                                         atol(msg->cm_fields['T'])) == 0) {
301                                 match = 1;
302                         }
303                 }
304                 pos += 2;
305         }
306
307         else if (!strcasecmp(itemlist[pos], "SENTSINCE")) {
308                 if (msg == NULL) {
309                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
310                         need_to_free_msg = 1;
311                 }
312                 if (msg->cm_fields['T'] != NULL) {
313                         if (imap_datecmp(itemlist[pos+1],
314                                         atol(msg->cm_fields['T'])) >= 0) {
315                                 match = 1;
316                         }
317                 }
318                 pos += 2;
319         }
320
321         else if (!strcasecmp(itemlist[pos], "SINCE")) {
322                 if (msg == NULL) {
323                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
324                         need_to_free_msg = 1;
325                 }
326                 if (msg->cm_fields['T'] != NULL) {
327                         if (imap_datecmp(itemlist[pos+1],
328                                         atol(msg->cm_fields['T'])) >= 0) {
329                                 match = 1;
330                         }
331                 }
332                 pos += 2;
333         }
334
335         else if (!strcasecmp(itemlist[pos], "SMALLER")) {
336                 if (msg == NULL) {
337                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
338                         need_to_free_msg = 1;
339                 }
340                 if (strlen(msg->cm_fields['M']) < atoi(itemlist[pos+1])) {
341                         match = 1;
342                 }
343                 pos += 2;
344         }
345
346         else if (!strcasecmp(itemlist[pos], "SUBJECT")) {
347                 if (msg == NULL) {
348                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
349                         need_to_free_msg = 1;
350                 }
351                 if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
352                         match = 1;
353                 }
354                 pos += 2;
355         }
356
357         else if (!strcasecmp(itemlist[pos], "TEXT")) {
358                 if (msg == NULL) {
359                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
360                         need_to_free_msg = 1;
361                 }
362                 for (i='A'; i<='Z'; ++i) {
363                         if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
364                                 match = 1;
365                         }
366                 }
367                 pos += 2;
368         }
369
370         else if (!strcasecmp(itemlist[pos], "TO")) {
371                 if (msg == NULL) {
372                         msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
373                         need_to_free_msg = 1;
374                 }
375                 if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
376                         match = 1;
377                 }
378                 pos += 2;
379         }
380
381         else if (!strcasecmp(itemlist[pos], "UID")) {
382                 if (is_msg_in_sequence_set(itemlist[pos+1], IMAP->msgids[seq-1])) {
383                         match = 1;
384                 }
385                 pos += 2;
386         }
387
388         /* Now here come the 'UN' criteria.  Why oh why do we have to
389          * implement *both* the 'UN' criteria *and* the 'NOT' keyword?  Why
390          * can't there be *one* way to do things?  Answer: the design of
391          * IMAP suffers from gratuitous complexity.
392          */
393
394         else if (!strcasecmp(itemlist[pos], "UNANSWERED")) {
395                 if ((IMAP->flags[seq-1] & IMAP_ANSWERED) == 0) {
396                         match = 1;
397                 }
398                 ++pos;
399         }
400
401         else if (!strcasecmp(itemlist[pos], "UNDELETED")) {
402                 if ((IMAP->flags[seq-1] & IMAP_DELETED) == 0) {
403                         match = 1;
404                 }
405                 ++pos;
406         }
407
408         else if (!strcasecmp(itemlist[pos], "UNDRAFT")) {
409                 if ((IMAP->flags[seq-1] & IMAP_DRAFT) == 0) {
410                         match = 1;
411                 }
412                 ++pos;
413         }
414
415         else if (!strcasecmp(itemlist[pos], "UNFLAGGED")) {
416                 if ((IMAP->flags[seq-1] & IMAP_FLAGGED) == 0) {
417                         match = 1;
418                 }
419                 ++pos;
420         }
421
422         else if (!strcasecmp(itemlist[pos], "UNKEYWORD")) {
423                 /* FIXME */
424                 pos += 2;
425         }
426
427         else if (!strcasecmp(itemlist[pos], "UNSEEN")) {
428                 if ((IMAP->flags[seq-1] & IMAP_SEEN) == 0) {
429                         match = 1;
430                 }
431                 ++pos;
432         }
433
434         /* Remember to negate if we were told to */
435         if (is_not) {
436                 match = !match;
437         }
438
439         /* Keep going if there are more criteria! */
440         if (pos < num_items) {
441
442                 if (is_or) {
443                         match = (match || imap_do_search_msg(seq, msg,
444                                 num_items - pos, &itemlist[pos], is_uid));
445                 }
446                 else {
447                         match = (match && imap_do_search_msg(seq, msg,
448                                 num_items - pos, &itemlist[pos], is_uid));
449                 }
450
451         }
452
453         if (need_to_free_msg) {
454                 CtdlFreeMessage(msg);
455         }
456         return(match);
457 }
458
459
460 /*
461  * imap_search() calls imap_do_search() to do its actual work, once it's
462  * validated and boiled down the request a bit.
463  */
464 void imap_do_search(int num_items, char **itemlist, int is_uid) {
465         int i, j, k;
466         int fts_num_msgs = 0;
467         long *fts_msgs = NULL;
468         int is_in_list = 0;
469         int num_results = 0;
470
471         /* If there is a BODY search criterion in the query, use our full
472          * text index to disqualify messages that don't have any chance of
473          * matching.  (Only do this if the index is enabled!!)
474          */
475         if (config.c_enable_fulltext) for (i=0; i<(num_items-1); ++i) {
476                 if (!strcasecmp(itemlist[i], "BODY")) {
477                         ft_search(&fts_num_msgs, &fts_msgs, itemlist[i+1]);
478                         if (fts_num_msgs > 0) {
479                                 for (j=0; j < IMAP->num_msgs; ++j) {
480                                         if (IMAP->flags[j] & IMAP_SELECTED) {
481                                                 is_in_list = 0;
482                                                 for (k=0; k<fts_num_msgs; ++k) {
483                                                         if (IMAP->msgids[j] == fts_msgs[k]) {
484                                                                 ++is_in_list;
485                                                         }
486                                                 }
487                                         }
488                                         if (!is_in_list) {
489                                                 IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
490                                         }
491                                 }
492                         }
493                         else {          /* no hits on the index; disqualify every message */
494                                 for (j=0; j < IMAP->num_msgs; ++j) {
495                                         IMAP->flags[j] = IMAP->flags[j] & ~IMAP_SELECTED;
496                                 }
497                         }
498                         if (fts_msgs) {
499                                 free(fts_msgs);
500                         }
501                 }
502         }
503
504         /* Now go through the messages and apply all search criteria. */
505         buffer_output();
506         cprintf("* SEARCH ");
507         if (IMAP->num_msgs > 0)
508          for (i = 0; i < IMAP->num_msgs; ++i)
509           if (IMAP->flags[i] & IMAP_SELECTED) {
510                 if (imap_do_search_msg(i+1, NULL, num_items, itemlist, is_uid)) {
511                         if (num_results != 0) {
512                                 cprintf(" ");
513                         }
514                         if (is_uid) {
515                                 cprintf("%ld", IMAP->msgids[i]);
516                         }
517                         else {
518                                 cprintf("%d", i+1);
519                         }
520                         ++num_results;
521                 }
522         }
523         cprintf("\r\n");
524         unbuffer_output();
525 }
526
527
528 /*
529  * This function is called by the main command loop.
530  */
531 void imap_search(int num_parms, char *parms[]) {
532         int i;
533
534         if (num_parms < 3) {
535                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
536                 return;
537         }
538
539         for (i = 0; i < IMAP->num_msgs; ++i) {
540                 IMAP->flags[i] |= IMAP_SELECTED;
541         }
542
543         for (i=1; i<num_parms; ++i) {
544                 if (imap_is_message_set(parms[i])) {
545                         imap_pick_range(parms[i], 0);
546                 }
547         }
548
549         imap_do_search(num_parms-2, &parms[2], 0);
550         cprintf("%s OK SEARCH completed\r\n", parms[0]);
551 }
552
553 /*
554  * This function is called by the main command loop.
555  */
556 void imap_uidsearch(int num_parms, char *parms[]) {
557         int i;
558
559         if (num_parms < 4) {
560                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
561                 return;
562         }
563
564         for (i = 0; i < IMAP->num_msgs; ++i) {
565                 IMAP->flags[i] |= IMAP_SELECTED;
566         }
567
568         for (i=1; i<num_parms; ++i) {
569                 if (imap_is_message_set(parms[i])) {
570                         imap_pick_range(parms[i], 1);
571                 }
572         }
573
574         imap_do_search(num_parms-3, &parms[3], 1);
575         cprintf("%s OK UID SEARCH completed\r\n", parms[0]);
576 }
577
578