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