ae14b59b953db23a18a2d6cb101f33bd652d532b
[claws.git] / src / advsearch.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2012 the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 # include "claws-features.h"
23 #endif
24
25 #include "advsearch.h"
26
27 #include <glib.h>
28 #include <ctype.h>
29
30 #include "matcher.h"
31 #include "matcher_parser.h"
32 #include "utils.h"
33 #include "prefs_common.h"
34
35 struct _AdvancedSearch {
36         struct {
37                 AdvancedSearchType       type;
38                 gchar                   *matchstring;
39         } request;
40
41         MatcherList                     *predicate;
42         gboolean                         is_fast;
43         gboolean                         search_aborted;
44
45         struct {
46                 gboolean (*cb)(gpointer data, guint at, guint matched, guint total);
47                 gpointer data;
48         } on_progress_cb;
49         struct {
50                 void (*cb)(gpointer data);
51                 gpointer data;
52         } on_error_cb;
53 };
54
55 void advsearch_set_on_progress_cb(AdvancedSearch *search, gboolean (*cb)(gpointer, guint, guint, guint), gpointer data)
56 {
57         search->on_progress_cb.cb = cb;
58         search->on_progress_cb.data = data;
59 }
60
61 void advsearch_set_on_error_cb(AdvancedSearch* search, void (*cb)(gpointer data), gpointer data)
62 {
63         search->on_error_cb.cb = cb;
64         search->on_error_cb.data = data;
65 }
66
67 static void prepare_matcher(AdvancedSearch *search);
68 static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
69                             FolderItem* folderItem, gboolean recursive);
70
71 // --------------------------
72
73 AdvancedSearch* advsearch_new()
74 {
75         AdvancedSearch *result;
76
77         result = g_new0(AdvancedSearch, 1);
78
79         return result;
80 }
81
82 void advsearch_free(AdvancedSearch *search)
83 {
84         if (search->predicate != NULL)
85                 matcherlist_free(search->predicate);
86
87         g_free(search->request.matchstring);
88         g_free(search);
89 }
90
91 void advsearch_set(AdvancedSearch *search, AdvancedSearchType type, const gchar *matchstring)
92 {
93         cm_return_if_fail(search != NULL);
94
95         search->request.type = type;
96
97         g_free(search->request.matchstring);
98         search->request.matchstring = g_strdup(matchstring);
99
100         prepare_matcher(search);
101 }
102
103 gboolean advsearch_is_fast(AdvancedSearch *search)
104 {
105         cm_return_val_if_fail(search != NULL, FALSE);
106
107         return search->is_fast;
108 }
109
110 gboolean advsearch_has_proper_predicate(AdvancedSearch *search)
111 {
112         cm_return_val_if_fail(search != NULL, FALSE);
113
114         return search->predicate != NULL;
115 }
116
117 gboolean advsearch_search_msgs_in_folders(AdvancedSearch* search, MsgInfoList **messages,
118                                           FolderItem* folderItem, gboolean recursive)
119 {
120         if (search == NULL || search->predicate == NULL)
121                 return FALSE;
122
123         search->search_aborted = FALSE;
124         return search_impl(messages, search, folderItem, recursive);
125 }
126
127 void advsearch_abort(AdvancedSearch *search)
128 {
129         search->search_aborted = TRUE;
130 }
131
132 gchar *advsearch_expand_search_string(const gchar *search_string)
133 {
134         int i = 0;
135         gchar term_char, save_char;
136         gchar *cmd_start, *cmd_end;
137         GString *matcherstr;
138         gchar *returnstr = NULL;
139         gchar *copy_str;
140         gboolean casesens, dontmatch, regex;
141         /* list of allowed pattern abbreviations */
142         struct {
143                 gchar           *abbreviated;   /* abbreviation */
144                 gchar           *command;       /* actual matcher command */
145                 gint            numparams;      /* number of params for cmd */
146                 gboolean        qualifier;      /* do we append stringmatch operations */
147                 gboolean        quotes;         /* do we need quotes */
148         }
149         cmds[] = {
150                 { "a",  "all",                          0,      FALSE,  FALSE },
151                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
152                 { "al", "age_lower",                    1,      FALSE,  FALSE },
153                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
154                 { "B",  "message",                      1,      TRUE,   TRUE  },
155                 { "c",  "cc",                           1,      TRUE,   TRUE  },
156                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
157                 { "D",  "deleted",                      0,      FALSE,  FALSE },
158                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
159                 { "E",  "execute",                      1,      FALSE,  TRUE  },
160                 { "f",  "from",                         1,      TRUE,   TRUE  },
161                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
162                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
163                 { "ha", "has_attachments",              0,      FALSE,  FALSE },
164                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
165                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
166                 { "k",  "colorlabel",                   1,      FALSE,  FALSE },
167                 { "L",  "locked",                       0,      FALSE,  FALSE },
168                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
169                 { "N",  "new",                          0,      FALSE,  FALSE },
170                 { "O",  "~new",                         0,      FALSE,  FALSE },
171                 { "r",  "replied",                      0,      FALSE,  FALSE },
172                 { "R",  "~unread",                      0,      FALSE,  FALSE },
173                 { "s",  "subject",                      1,      TRUE,   TRUE  },
174                 { "se", "score_equal",                  1,      FALSE,  FALSE },
175                 { "sg", "score_greater",                1,      FALSE,  FALSE },
176                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
177                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
178                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
179                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
180                 { "t",  "to",                           1,      TRUE,   TRUE  },
181                 { "tg", "tag",                          1,      TRUE,   TRUE  },
182                 { "T",  "marked",                       0,      FALSE,  FALSE },
183                 { "U",  "unread",                       0,      FALSE,  FALSE },
184                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
185                 { "X",  "test",                         1,      FALSE,  FALSE },
186                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
187                 { "&",  "&",                            0,      FALSE,  FALSE },
188                 { "|",  "|",                            0,      FALSE,  FALSE },
189                 { "p",  "partial",                      0,      FALSE,  FALSE },
190                 { NULL, NULL,                           0,      FALSE,  FALSE }
191         };
192
193         if (search_string == NULL)
194                 return NULL;
195
196         copy_str = g_strdup(search_string);
197
198         matcherstr = g_string_sized_new(16);
199         cmd_start = copy_str;
200         while (cmd_start && *cmd_start) {
201                 /* skip all white spaces */
202                 while (*cmd_start && isspace((guchar)*cmd_start))
203                         cmd_start++;
204                 cmd_end = cmd_start;
205
206                 /* extract a command */
207                 while (*cmd_end && !isspace((guchar)*cmd_end))
208                         cmd_end++;
209
210                 /* save character */
211                 save_char = *cmd_end;
212                 *cmd_end = '\0';
213
214                 dontmatch = FALSE;
215                 casesens = FALSE;
216                 regex = FALSE;
217
218                 /* ~ and ! mean logical NOT */
219                 if (*cmd_start == '~' || *cmd_start == '!')
220                 {
221                         dontmatch = TRUE;
222                         cmd_start++;
223                 }
224                 /* % means case sensitive match */
225                 if (*cmd_start == '%')
226                 {
227                         casesens = TRUE;
228                         cmd_start++;
229                 }
230                 /* # means regex match */
231                 if (*cmd_start == '#') {
232                         regex = TRUE;
233                         cmd_start++;
234                 }
235
236                 /* find matching abbreviation */
237                 for (i = 0; cmds[i].command; i++) {
238                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
239                                 /* restore character */
240                                 *cmd_end = save_char;
241
242                                 /* copy command */
243                                 if (matcherstr->len > 0) {
244                                         g_string_append(matcherstr, " ");
245                                 }
246                                 if (dontmatch)
247                                         g_string_append(matcherstr, "~");
248                                 g_string_append(matcherstr, cmds[i].command);
249                                 g_string_append(matcherstr, " ");
250
251                                 /* stop if no params required */
252                                 if (cmds[i].numparams == 0)
253                                         break;
254
255                                 /* extract a parameter, allow quotes */
256                                 while (*cmd_end && isspace((guchar)*cmd_end))
257                                         cmd_end++;
258
259                                 cmd_start = cmd_end;
260                                 if (*cmd_start == '"') {
261                                         term_char = '"';
262                                         cmd_end++;
263                                 }
264                                 else
265                                         term_char = ' ';
266
267                                 /* extract actual parameter */
268                                 while ((*cmd_end) && (*cmd_end != term_char))
269                                         cmd_end++;
270
271                                 if (*cmd_end == '"')
272                                         cmd_end++;
273
274                                 save_char = *cmd_end;
275                                 *cmd_end = '\0';
276
277                                 if (cmds[i].qualifier) {
278                                         if (casesens)
279                                                 g_string_append(matcherstr, regex ? "regexp " : "match ");
280                                         else
281                                                 g_string_append(matcherstr, regex ? "regexpcase " : "matchcase ");
282                                 }
283
284                                 /* do we need to add quotes ? */
285                                 if (cmds[i].quotes && term_char != '"')
286                                         g_string_append(matcherstr, "\"");
287
288                                 /* copy actual parameter */
289                                 g_string_append(matcherstr, cmd_start);
290
291                                 /* do we need to add quotes ? */
292                                 if (cmds[i].quotes && term_char != '"')
293                                         g_string_append(matcherstr, "\"");
294
295                                 /* restore original character */
296                                 *cmd_end = save_char;
297
298                                 break;
299                         }
300                 }
301
302                 if (*cmd_end)
303                         cmd_end++;
304                 cmd_start = cmd_end;
305         }
306
307         g_free(copy_str);
308
309         /* return search string if no match is found to allow
310            all available filtering expressions in advanced search */
311         if (matcherstr->len > 0) returnstr = matcherstr->str;
312         else returnstr = g_strdup(search_string);
313         g_string_free(matcherstr, FALSE);
314         return returnstr;
315 }
316
317 // --------------------------
318
319 static gchar *expand_tag_search_string(const gchar *search_string)
320 {
321         gchar *newstr = NULL;
322         gchar **words = search_string ? g_strsplit(search_string, " ", -1):NULL;
323         gint i = 0;
324         while (words && words[i] && *words[i]) {
325                 g_strstrip(words[i]);
326                 if (!newstr) {
327                         newstr = g_strdup_printf("tag matchcase \"%s\"", words[i]);
328                 } else {
329                         gint o_len = strlen(newstr);
330                         gint s_len = 17; /* strlen("|tag matchcase \"\"") */
331                         gint n_len = s_len + strlen(words[i]);
332                         newstr = g_realloc(newstr, o_len + n_len + 1);
333                         strcpy(newstr + o_len, "|tag matchcase \"");
334                         strcpy(newstr + o_len + (s_len - 1), words[i]);
335                         strcpy(newstr + o_len + (n_len - 1), "\"");
336                 }
337                 i++;
338         }
339         g_strfreev(words);
340         return newstr;
341 }
342
343 static void prepare_matcher_extended(AdvancedSearch *search)
344 {
345         gchar *newstr = advsearch_expand_search_string(search->request.matchstring);
346
347         if (newstr && newstr[0] != '\0') {
348                 search->predicate = matcher_parser_get_cond(newstr, &search->is_fast);
349                 g_free(newstr);
350         }
351 }
352
353 static void prepare_matcher_tag(AdvancedSearch *search)
354 {
355         char *newstr = expand_tag_search_string(search->request.matchstring);
356         search->predicate = matcher_parser_get_cond(newstr, &search->is_fast);
357         g_free(newstr);
358 }
359
360 static void prepare_matcher_header(AdvancedSearch *search, gint match_header)
361 {
362         MatcherProp *matcher;
363
364         if (search->predicate == NULL)
365                 search->predicate = g_new0(MatcherList, 1);
366
367         matcher = matcherprop_new(match_header, NULL, MATCHTYPE_MATCHCASE,
368                         search->request.matchstring, 0);
369
370         search->predicate->matchers = g_slist_prepend(search->predicate->matchers, matcher);
371 }
372
373 static void prepare_matcher_mixed(AdvancedSearch *search)
374 {
375         prepare_matcher_tag(search);
376
377         prepare_matcher_header(search, MATCHCRITERIA_SUBJECT);
378         prepare_matcher_header(search, MATCHCRITERIA_FROM);
379         prepare_matcher_header(search, MATCHCRITERIA_TO);
380 }
381
382 static void prepare_matcher(AdvancedSearch *search)
383 {
384         const gchar *search_string;
385
386         cm_return_if_fail(search != NULL);
387
388         if (search->predicate) {
389                 matcherlist_free(search->predicate);
390                 search->predicate = NULL;
391         }
392
393         search_string = search->request.matchstring;
394
395         if (search_string == NULL || search_string[0] == '\0')
396                 return;
397
398         switch (search->request.type) {
399                 case ADVANCED_SEARCH_SUBJECT:
400                         prepare_matcher_header(search, MATCHCRITERIA_SUBJECT);
401                         break;
402
403                 case ADVANCED_SEARCH_FROM:
404                         prepare_matcher_header(search, MATCHCRITERIA_FROM);
405                         break;
406
407                 case ADVANCED_SEARCH_TO:
408                         prepare_matcher_header(search, MATCHCRITERIA_TO);
409                         break;
410
411                 case ADVANCED_SEARCH_TAG:
412                         prepare_matcher_header(search, MATCHCRITERIA_TAG);
413                         break;
414
415                 case ADVANCED_SEARCH_MIXED:
416                         prepare_matcher_mixed(search);
417                         break;
418
419                 case ADVANCED_SEARCH_EXTENDED:
420                         prepare_matcher_extended(search);
421                         break;
422
423                 default:
424                         debug_print("unknown search type (%d)\n", search->request.type);
425                         break;
426         }
427 }
428
429 static gboolean search_progress_notify_cb(gpointer data, gboolean on_server, guint at,
430                 guint matched, guint total)
431 {
432         AdvancedSearch *search = (AdvancedSearch*) data;
433
434         if (search->search_aborted)
435                 return FALSE;
436
437         if (on_server || search->on_progress_cb.cb == NULL)
438                 return TRUE;
439
440         return search->on_progress_cb.cb(search->on_progress_cb.data, at, matched, total);
441 }
442
443 static gboolean search_filter_folder(MsgNumberList **msgnums, AdvancedSearch *search,
444                                           FolderItem *folderItem, gboolean onServer)
445 {
446         gint matched;
447         gboolean tried_server = onServer;
448
449         matched = folder_item_search_msgs(folderItem->folder,
450                 folderItem,
451                 msgnums,
452                 &onServer,
453                 search->predicate,
454                 search_progress_notify_cb,
455                 search);
456
457         if (matched < 0) {
458                 if (search->on_error_cb.cb != NULL)
459                         search->on_error_cb.cb(search->on_error_cb.data);
460                 return FALSE;
461         }
462
463         if (folderItem->folder->klass->supports_server_search && tried_server && !onServer) {
464                 return search_filter_folder(msgnums, search, folderItem, onServer);
465         } else {
466                 return TRUE;
467         }
468 }
469
470 static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
471                             FolderItem* folderItem, gboolean recursive)
472 {
473         if (recursive) {
474                 if (!search_impl(messages, search, folderItem, FALSE))
475                         return FALSE;
476
477                 if (folderItem->node->children != NULL && !search->search_aborted) {
478                         GNode *node;
479                         for (node = folderItem->node->children; node != NULL; node = node->next) {
480                                 FolderItem *cur = FOLDER_ITEM(node->data);
481                                 debug_print("in: %s\n", cur->path);
482                                 if (!search_impl(messages, search, cur, TRUE))
483                                         return FALSE;
484                         }
485                 }
486         } else if (!folderItem->no_select) {
487                 MsgNumberList *msgnums = NULL;
488                 MsgNumberList *cur;
489                 MsgInfoList *msgs = NULL;
490                 gboolean can_search_on_server = folderItem->folder->klass->supports_server_search;
491
492                 if (!search_filter_folder(&msgnums, search, folderItem,
493                                           can_search_on_server)) {
494                         g_slist_free(msgnums);
495                         return FALSE;
496                 }
497
498                 for (cur = msgnums; cur != NULL; cur = cur->next) {
499                         MsgInfo *msg = folder_item_get_msginfo(folderItem, GPOINTER_TO_UINT(cur->data));
500
501                         msgs = g_slist_prepend(msgs, msg);
502                 }
503
504                 while (msgs != NULL) {
505                         MsgInfoList *front = msgs;
506
507                         msgs = msgs->next;
508
509                         front->next = *messages;
510                         *messages = front;
511                 }
512
513                 g_slist_free(msgnums);
514         }
515
516         return TRUE;
517 }