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