08e6dd5cb8f8769842886165af501367c003518c
[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
34 struct _AdvancedSearch {
35         struct {
36                 AdvancedSearchType       type;
37                 gchar                   *matchstring;
38         } request;
39
40         MatcherList                     *predicate;
41         gboolean                         is_fast;
42         gboolean                         search_aborted;
43
44         struct {
45                 gboolean (*cb)(gpointer data, guint at, guint matched, guint total);
46                 gpointer data;
47         } on_progress_cb;
48         struct {
49                 void (*cb)(gpointer data);
50                 gpointer data;
51         } on_error_cb;
52 };
53
54 void advsearch_set_on_progress_cb(AdvancedSearch *search, gboolean (*cb)(gpointer, guint, guint, guint), gpointer data)
55 {
56         search->on_progress_cb.cb = cb;
57         search->on_progress_cb.data = data;
58 }
59
60 void advsearch_set_on_error_cb(AdvancedSearch* search, void (*cb)(gpointer data), gpointer data)
61 {
62         search->on_error_cb.cb = cb;
63         search->on_error_cb.data = data;
64 }
65
66 static void prepare_matcher(AdvancedSearch *search);
67 static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
68                             FolderItem* folderItem, gboolean recursive);
69
70 // --------------------------
71
72 AdvancedSearch* advsearch_new()
73 {
74         AdvancedSearch *result;
75
76         result = g_new0(AdvancedSearch, 1);
77
78         return result;
79 }
80
81 void advsearch_free(AdvancedSearch *search)
82 {
83         if (search->predicate != NULL)
84                 matcherlist_free(search->predicate);
85
86         g_free(search->request.matchstring);
87         g_free(search);
88 }
89
90 void advsearch_set(AdvancedSearch *search, AdvancedSearchType type, const gchar *matchstring)
91 {
92         cm_return_if_fail(search != NULL);
93
94         search->request.type = type;
95
96         g_free(search->request.matchstring);
97         search->request.matchstring = g_strdup(matchstring);
98
99         prepare_matcher(search);
100 }
101
102 gboolean advsearch_is_fast(AdvancedSearch *search)
103 {
104         cm_return_val_if_fail(search != NULL, FALSE);
105
106         return search->is_fast;
107 }
108
109 gboolean advsearch_has_proper_predicate(AdvancedSearch *search)
110 {
111         cm_return_val_if_fail(search != NULL, FALSE);
112
113         return search->predicate != NULL;
114 }
115
116 gboolean advsearch_search_msgs_in_folders(AdvancedSearch* search, MsgInfoList **messages,
117                                           FolderItem* folderItem, gboolean recursive)
118 {
119         if (search == NULL || search->predicate == NULL)
120                 return FALSE;
121
122         search->search_aborted = FALSE;
123         return search_impl(messages, search, folderItem, recursive);
124 }
125
126 void advsearch_abort(AdvancedSearch *search)
127 {
128         search->search_aborted = TRUE;
129 }
130
131 gchar *advsearch_expand_search_string(const gchar *search_string)
132 {
133         int i = 0;
134         gchar term_char, save_char;
135         gchar *cmd_start, *cmd_end;
136         GString *matcherstr;
137         gchar *returnstr = NULL;
138         gchar *copy_str;
139         gboolean casesens, dontmatch, regex;
140         /* list of allowed pattern abbreviations */
141         struct {
142                 gchar           *abbreviated;   /* abbreviation */
143                 gchar           *command;       /* actual matcher command */
144                 gint            numparams;      /* number of params for cmd */
145                 gboolean        qualifier;      /* do we append stringmatch operations */
146                 gboolean        quotes;         /* do we need quotes */
147         }
148         cmds[] = {
149                 { "a",  "all",                          0,      FALSE,  FALSE },
150                 { "ag", "age_greater",                  1,      FALSE,  FALSE },
151                 { "al", "age_lower",                    1,      FALSE,  FALSE },
152                 { "b",  "body_part",                    1,      TRUE,   TRUE  },
153                 { "B",  "message",                      1,      TRUE,   TRUE  },
154                 { "c",  "cc",                           1,      TRUE,   TRUE  },
155                 { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
156                 { "D",  "deleted",                      0,      FALSE,  FALSE },
157                 { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
158                 { "E",  "execute",                      1,      FALSE,  TRUE  },
159                 { "f",  "from",                         1,      TRUE,   TRUE  },
160                 { "F",  "forwarded",                    0,      FALSE,  FALSE },
161                 { "h",  "headers_part",                 1,      TRUE,   TRUE  },
162                 { "ha", "has_attachments",              0,      FALSE,  FALSE },
163                 { "i",  "header \"Message-ID\"",        1,      TRUE,   TRUE  },
164                 { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
165                 { "k",  "colorlabel",                   1,      FALSE,  FALSE },
166                 { "L",  "locked",                       0,      FALSE,  FALSE },
167                 { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
168                 { "N",  "new",                          0,      FALSE,  FALSE },
169                 { "O",  "~new",                         0,      FALSE,  FALSE },
170                 { "r",  "replied",                      0,      FALSE,  FALSE },
171                 { "R",  "~unread",                      0,      FALSE,  FALSE },
172                 { "s",  "subject",                      1,      TRUE,   TRUE  },
173                 { "se", "score_equal",                  1,      FALSE,  FALSE },
174                 { "sg", "score_greater",                1,      FALSE,  FALSE },
175                 { "sl", "score_lower",                  1,      FALSE,  FALSE },
176                 { "Se", "size_equal",                   1,      FALSE,  FALSE },
177                 { "Sg", "size_greater",                 1,      FALSE,  FALSE },
178                 { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
179                 { "t",  "to",                           1,      TRUE,   TRUE  },
180                 { "tg", "tag",                          1,      TRUE,   TRUE  },
181                 { "T",  "marked",                       0,      FALSE,  FALSE },
182                 { "U",  "unread",                       0,      FALSE,  FALSE },
183                 { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
184                 { "X",  "test",                         1,      FALSE,  FALSE },
185                 { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
186                 { "&",  "&",                            0,      FALSE,  FALSE },
187                 { "|",  "|",                            0,      FALSE,  FALSE },
188                 { "p",  "partial",                      0,      FALSE,  FALSE },
189                 { NULL, NULL,                           0,      FALSE,  FALSE }
190         };
191
192         if (search_string == NULL)
193                 return NULL;
194
195         copy_str = g_strdup(search_string);
196
197         matcherstr = g_string_sized_new(16);
198         cmd_start = copy_str;
199         while (cmd_start && *cmd_start) {
200                 /* skip all white spaces */
201                 while (*cmd_start && isspace((guchar)*cmd_start))
202                         cmd_start++;
203                 cmd_end = cmd_start;
204
205                 /* extract a command */
206                 while (*cmd_end && !isspace((guchar)*cmd_end))
207                         cmd_end++;
208
209                 /* save character */
210                 save_char = *cmd_end;
211                 *cmd_end = '\0';
212
213                 dontmatch = FALSE;
214                 casesens = FALSE;
215                 regex = FALSE;
216
217                 /* ~ and ! mean logical NOT */
218                 if (*cmd_start == '~' || *cmd_start == '!')
219                 {
220                         dontmatch = TRUE;
221                         cmd_start++;
222                 }
223                 /* % means case sensitive match */
224                 if (*cmd_start == '%')
225                 {
226                         casesens = TRUE;
227                         cmd_start++;
228                 }
229                 /* # means regex match */
230                 if (*cmd_start == '#') {
231                         regex = TRUE;
232                         cmd_start++;
233                 }
234
235                 /* find matching abbreviation */
236                 for (i = 0; cmds[i].command; i++) {
237                         if (!strcmp(cmd_start, cmds[i].abbreviated)) {
238                                 /* restore character */
239                                 *cmd_end = save_char;
240
241                                 /* copy command */
242                                 if (matcherstr->len > 0) {
243                                         g_string_append(matcherstr, " ");
244                                 }
245                                 if (dontmatch)
246                                         g_string_append(matcherstr, "~");
247                                 g_string_append(matcherstr, cmds[i].command);
248                                 g_string_append(matcherstr, " ");
249
250                                 /* stop if no params required */
251                                 if (cmds[i].numparams == 0)
252                                         break;
253
254                                 /* extract a parameter, allow quotes */
255                                 while (*cmd_end && isspace((guchar)*cmd_end))
256                                         cmd_end++;
257
258                                 cmd_start = cmd_end;
259                                 if (*cmd_start == '"') {
260                                         term_char = '"';
261                                         cmd_end++;
262                                 }
263                                 else
264                                         term_char = ' ';
265
266                                 /* extract actual parameter */
267                                 while ((*cmd_end) && (*cmd_end != term_char))
268                                         cmd_end++;
269
270                                 if (*cmd_end == '"')
271                                         cmd_end++;
272
273                                 save_char = *cmd_end;
274                                 *cmd_end = '\0';
275
276                                 if (cmds[i].qualifier) {
277                                         if (casesens)
278                                                 g_string_append(matcherstr, regex ? "regexp " : "match ");
279                                         else
280                                                 g_string_append(matcherstr, regex ? "regexpcase " : "matchcase ");
281                                 }
282
283                                 /* do we need to add quotes ? */
284                                 if (cmds[i].quotes && term_char != '"')
285                                         g_string_append(matcherstr, "\"");
286
287                                 /* copy actual parameter */
288                                 g_string_append(matcherstr, cmd_start);
289
290                                 /* do we need to add quotes ? */
291                                 if (cmds[i].quotes && term_char != '"')
292                                         g_string_append(matcherstr, "\"");
293
294                                 /* restore original character */
295                                 *cmd_end = save_char;
296
297                                 break;
298                         }
299                 }
300
301                 if (*cmd_end)
302                         cmd_end++;
303                 cmd_start = cmd_end;
304         }
305
306         g_free(copy_str);
307
308         /* return search string if no match is found to allow
309            all available filtering expressions in advanced search */
310         if (matcherstr->len > 0) returnstr = matcherstr->str;
311         else returnstr = g_strdup(search_string);
312         g_string_free(matcherstr, FALSE);
313         return returnstr;
314 }
315
316 // --------------------------
317
318 static gchar *expand_tag_search_string(const gchar *search_string)
319 {
320         gchar *newstr = NULL;
321         gchar **words = search_string ? g_strsplit(search_string, " ", -1):NULL;
322         gint i = 0;
323         while (words && words[i] && *words[i]) {
324                 g_strstrip(words[i]);
325                 if (!newstr) {
326                         newstr = g_strdup_printf("tag matchcase \"%s\"", words[i]);
327                 } else {
328                         gint o_len = strlen(newstr);
329                         gint s_len = 17; /* strlen("|tag matchcase \"\"") */
330                         gint n_len = s_len + strlen(words[i]);
331                         newstr = g_realloc(newstr, o_len + n_len + 1);
332                         strcpy(newstr + o_len, "|tag matchcase \"");
333                         strcpy(newstr + o_len + (s_len - 1), words[i]);
334                         strcpy(newstr + o_len + (n_len - 1), "\"");
335                 }
336                 i++;
337         }
338         g_strfreev(words);
339         return newstr;
340 }
341
342 static void prepare_matcher_extended(AdvancedSearch *search)
343 {
344         gchar *newstr = advsearch_expand_search_string(search->request.matchstring);
345
346         if (newstr && newstr[0] != '\0') {
347                 search->predicate = matcher_parser_get_cond(newstr, &search->is_fast);
348                 g_free(newstr);
349         }
350 }
351
352 static void prepare_matcher_tag(AdvancedSearch *search)
353 {
354         char *newstr = expand_tag_search_string(search->request.matchstring);
355         search->predicate = matcher_parser_get_cond(newstr, &search->is_fast);
356         g_free(newstr);
357 }
358
359 static void prepare_matcher_header(AdvancedSearch *search, gint match_header)
360 {
361         MatcherProp *matcher;
362
363         if (search->predicate == NULL)
364                 search->predicate = g_new0(MatcherList, 1);
365
366         matcher = matcherprop_new(match_header, NULL, MATCHTYPE_MATCHCASE,
367                         search->request.matchstring, 0);
368
369         search->predicate->matchers = g_slist_prepend(search->predicate->matchers, matcher);
370 }
371
372 static void prepare_matcher_mixed(AdvancedSearch *search)
373 {
374         prepare_matcher_tag(search);
375
376         prepare_matcher_header(search, MATCHCRITERIA_SUBJECT);
377         prepare_matcher_header(search, MATCHCRITERIA_FROM);
378         prepare_matcher_header(search, MATCHCRITERIA_TO);
379         prepare_matcher_header(search, MATCHCRITERIA_TAG);
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
448         matched = folder_item_search_msgs(folderItem->folder,
449                 folderItem,
450                 msgnums,
451                 &onServer,
452                 search->predicate,
453                 search_progress_notify_cb,
454                 search);
455
456         if (matched < 0) {
457                 if (search->on_error_cb.cb != NULL)
458                         search->on_error_cb.cb(search->on_error_cb.data);
459                 return FALSE;
460         }
461
462         if (folderItem->folder->klass->supports_server_search && !onServer) {
463                 return search_filter_folder(msgnums, search, folderItem, onServer);
464         } else {
465                 return TRUE;
466         }
467 }
468
469 static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
470                             FolderItem* folderItem, gboolean recursive)
471 {
472         if (recursive) {
473                 if (!search_impl(messages, search, folderItem, FALSE))
474                         return FALSE;
475
476                 if (folderItem->node->children != NULL && !search->search_aborted) {
477                         GNode *node;
478                         for (node = folderItem->node->children; node != NULL; node = node->next) {
479                                 FolderItem *cur = FOLDER_ITEM(node->data);
480                                 debug_print("in: %s\n", cur->path);
481                                 if (!search_impl(messages, search, cur, TRUE))
482                                         return FALSE;
483                         }
484                 }
485         } else {
486                 MsgNumberList *msgnums = NULL;
487                 MsgNumberList *cur;
488                 MsgInfoList *msgs = NULL;
489
490                 if (!search_filter_folder(&msgnums, search, folderItem,
491                                         folderItem->folder->klass->supports_server_search)) {
492                         g_slist_free(msgnums);
493                         return FALSE;
494                 }
495
496                 for (cur = msgnums; cur != NULL; cur = cur->next) {
497                         MsgInfo *msg = folder_item_get_msginfo(folderItem, GPOINTER_TO_UINT(cur->data));
498
499                         msgs = g_slist_prepend(msgs, msg);
500                 }
501
502                 while (msgs != NULL) {
503                         MsgInfoList *front = msgs;
504
505                         msgs = msgs->next;
506
507                         front->next = *messages;
508                         *messages = front;
509                 }
510
511                 g_slist_free(msgnums);
512         }
513
514         return TRUE;
515 }