2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2004 by the Sylpheed Claws Team and Hiroyuki Yamamoto
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 2 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <glib/gi18n.h>
29 #include "procheader.h"
31 #include "matcher_parser.h"
32 #include "prefs_gtk.h"
33 #include "addr_compl.h"
37 *\brief Keyword lookup element
40 gint id; /*!< keyword id */
41 gchar *str; /*!< keyword */
43 typedef struct _MatchParser MatchParser;
46 *\brief Table with strings and ids used by the lexer and
47 * the parser. New keywords can be added here.
49 static const MatchParser matchparser_tab[] = {
51 {MATCHCRITERIA_ALL, "all"},
52 {MATCHCRITERIA_UNREAD, "unread"},
53 {MATCHCRITERIA_NOT_UNREAD, "~unread"},
54 {MATCHCRITERIA_NEW, "new"},
55 {MATCHCRITERIA_NOT_NEW, "~new"},
56 {MATCHCRITERIA_MARKED, "marked"},
57 {MATCHCRITERIA_NOT_MARKED, "~marked"},
58 {MATCHCRITERIA_DELETED, "deleted"},
59 {MATCHCRITERIA_NOT_DELETED, "~deleted"},
60 {MATCHCRITERIA_REPLIED, "replied"},
61 {MATCHCRITERIA_NOT_REPLIED, "~replied"},
62 {MATCHCRITERIA_FORWARDED, "forwarded"},
63 {MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
64 {MATCHCRITERIA_LOCKED, "locked"},
65 {MATCHCRITERIA_NOT_LOCKED, "~locked"},
66 {MATCHCRITERIA_COLORLABEL, "colorlabel"},
67 {MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
68 {MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
69 {MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
72 {MATCHCRITERIA_SUBJECT, "subject"},
73 {MATCHCRITERIA_NOT_SUBJECT, "~subject"},
74 {MATCHCRITERIA_FROM, "from"},
75 {MATCHCRITERIA_NOT_FROM, "~from"},
76 {MATCHCRITERIA_TO, "to"},
77 {MATCHCRITERIA_NOT_TO, "~to"},
78 {MATCHCRITERIA_CC, "cc"},
79 {MATCHCRITERIA_NOT_CC, "~cc"},
80 {MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
81 {MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
82 {MATCHCRITERIA_AGE_GREATER, "age_greater"},
83 {MATCHCRITERIA_AGE_LOWER, "age_lower"},
84 {MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
85 {MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
86 {MATCHCRITERIA_INREPLYTO, "inreplyto"},
87 {MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
88 {MATCHCRITERIA_REFERENCES, "references"},
89 {MATCHCRITERIA_NOT_REFERENCES, "~references"},
90 {MATCHCRITERIA_SCORE_GREATER, "score_greater"},
91 {MATCHCRITERIA_SCORE_LOWER, "score_lower"},
92 {MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
93 {MATCHCRITERIA_PARTIAL, "partial"},
94 {MATCHCRITERIA_NOT_PARTIAL, "~partial"},
96 {MATCHCRITERIA_SIZE_GREATER, "size_greater"},
97 {MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
98 {MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
100 /* content have to be read */
101 {MATCHCRITERIA_HEADER, "header"},
102 {MATCHCRITERIA_NOT_HEADER, "~header"},
103 {MATCHCRITERIA_HEADERS_PART, "headers_part"},
104 {MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
105 {MATCHCRITERIA_MESSAGE, "message"},
106 {MATCHCRITERIA_NOT_MESSAGE, "~message"},
107 {MATCHCRITERIA_BODY_PART, "body_part"},
108 {MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
109 {MATCHCRITERIA_TEST, "test"},
110 {MATCHCRITERIA_NOT_TEST, "~test"},
113 {MATCHTYPE_MATCHCASE, "matchcase"},
114 {MATCHTYPE_MATCH, "match"},
115 {MATCHTYPE_REGEXPCASE, "regexpcase"},
116 {MATCHTYPE_REGEXP, "regexp"},
117 {MATCHTYPE_ANY_IN_ADDRESSBOOK, "any_in_addressbook"},
118 {MATCHTYPE_ALL_IN_ADDRESSBOOK, "all_in_addressbook"},
121 {MATCHACTION_SCORE, "score"}, /* for backward compatibility */
122 {MATCHACTION_MOVE, "move"},
123 {MATCHACTION_COPY, "copy"},
124 {MATCHACTION_DELETE, "delete"},
125 {MATCHACTION_MARK, "mark"},
126 {MATCHACTION_UNMARK, "unmark"},
127 {MATCHACTION_LOCK, "lock"},
128 {MATCHACTION_UNLOCK, "unlock"},
129 {MATCHACTION_MARK_AS_READ, "mark_as_read"},
130 {MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
131 {MATCHACTION_FORWARD, "forward"},
132 {MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
133 {MATCHACTION_EXECUTE, "execute"},
134 {MATCHACTION_COLOR, "color"},
135 {MATCHACTION_REDIRECT, "redirect"},
136 {MATCHACTION_CHANGE_SCORE, "change_score"},
137 {MATCHACTION_SET_SCORE, "set_score"},
138 {MATCHACTION_STOP, "stop"},
139 {MATCHACTION_HIDE, "hide"},
143 *\brief Look up table with keywords defined in \sa matchparser_tab
145 static GHashTable *matchparser_hashtab;
148 *\brief Translate keyword id to keyword string
150 *\param id Id of keyword
152 *\return const gchar * Keyword
154 const gchar *get_matchparser_tab_str(gint id)
158 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
159 if (matchparser_tab[i].id == id)
160 return matchparser_tab[i].str;
166 *\brief Create keyword lookup table
168 static void create_matchparser_hashtab(void)
172 if (matchparser_hashtab) return;
173 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
174 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
175 g_hash_table_insert(matchparser_hashtab,
176 matchparser_tab[i].str,
177 (gpointer) &matchparser_tab[i]);
181 *\brief Return a keyword id from a keyword string
183 *\param str Keyword string
185 *\return gint Keyword id
187 gint get_matchparser_tab_id(const gchar *str)
191 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
197 /* **************** data structure allocation **************** */
200 *\brief Allocate a structure for a filtering / scoring
201 * "condition" (a matcher structure)
203 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
204 *\param header Header string (if criteria is MATCHCRITERIA_HEADER)
205 *\param matchtype Type of action (MATCHTYPE_XXX)
206 *\param expr String value or expression to check
207 *\param value Integer value to check
209 *\return MatcherProp * Pointer to newly allocated structure
211 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
212 gint matchtype, const gchar *expr,
217 prop = g_new0(MatcherProp, 1);
218 prop->criteria = criteria;
219 prop->header = header != NULL ? g_strdup(header) : NULL;
220 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
221 prop->matchtype = matchtype;
230 *\brief Free a matcher structure
232 *\param prop Pointer to matcher structure allocated with
235 void matcherprop_free(MatcherProp *prop)
240 g_free(prop->header);
241 if (prop->preg != NULL) {
249 *\brief Copy a matcher structure
251 *\param src Matcher structure to copy
253 *\return MatcherProp * Pointer to newly allocated matcher structure
255 MatcherProp *matcherprop_copy(const MatcherProp *src)
257 MatcherProp *prop = g_new0(MatcherProp, 1);
259 prop->criteria = src->criteria;
260 prop->header = src->header ? g_strdup(src->header) : NULL;
261 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
262 prop->matchtype = src->matchtype;
264 prop->preg = NULL; /* will be re-evaluated */
265 prop->value = src->value;
266 prop->error = src->error;
270 /* ************** match ******************************/
272 static gboolean match_with_addresses_in_addressbook
273 (MatcherProp *prop, const gchar *str, gint type)
275 GSList *address_list = NULL;
277 gboolean res = FALSE;
279 if (str == NULL || *str == 0)
282 /* XXX: perhaps complete with comments too */
283 address_list = address_list_append(address_list, str);
287 start_address_completion();
289 for (walk = address_list; walk != NULL; walk = walk->next) {
290 gboolean found = complete_address(walk->data) ? TRUE : FALSE;
293 if (!found && type == MATCHTYPE_ALL_IN_ADDRESSBOOK) {
300 g_slist_free(address_list);
302 end_address_completion();
308 *\brief Find out if a string matches a condition
310 *\param prop Matcher structure
311 *\param str String to check
313 *\return gboolean TRUE if str matches the condition in the
316 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str)
324 switch (prop->matchtype) {
325 case MATCHTYPE_REGEXPCASE:
326 case MATCHTYPE_REGEXP:
327 if (!prop->preg && (prop->error == 0)) {
328 prop->preg = g_new0(regex_t, 1);
329 /* if regexp then don't use the escaped string */
330 if (regcomp(prop->preg, prop->expr,
331 REG_NOSUB | REG_EXTENDED
332 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
333 ? REG_ICASE : 0)) != 0) {
339 if (prop->preg == NULL)
342 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
347 case MATCHTYPE_ALL_IN_ADDRESSBOOK:
348 case MATCHTYPE_ANY_IN_ADDRESSBOOK:
349 return match_with_addresses_in_addressbook
350 (prop, str, prop->matchtype);
352 case MATCHTYPE_MATCH:
353 return (strstr(str, prop->expr) != NULL);
355 /* FIXME: put upper in unesc_str */
356 case MATCHTYPE_MATCHCASE:
357 str2 = alloca(strlen(prop->expr) + 1);
358 strcpy(str2, prop->expr);
360 str1 = alloca(strlen(str) + 1);
363 return (strstr(str1, str2) != NULL);
371 *\brief Execute a command defined in the matcher structure
373 *\param prop Pointer to matcher structure
374 *\param info Pointer to message info structure
376 *\return gboolean TRUE if command was executed succesfully
378 static gboolean matcherprop_match_test(const MatcherProp *prop,
385 file = procmsg_get_message_file(info);
390 cmd = matching_build_command(prop->expr, info);
394 retval = system(cmd);
395 debug_print("Command exit code: %d\n", retval);
398 return (retval == 0);
402 *\brief Check if a message matches the condition in a matcher
405 *\param prop Pointer to matcher structure
406 *\param info Pointer to message info
408 *\return gboolean TRUE if a match
410 gboolean matcherprop_match(MatcherProp *prop,
415 switch(prop->criteria) {
416 case MATCHCRITERIA_ALL:
418 case MATCHCRITERIA_UNREAD:
419 return MSG_IS_UNREAD(info->flags);
420 case MATCHCRITERIA_NOT_UNREAD:
421 return !MSG_IS_UNREAD(info->flags);
422 case MATCHCRITERIA_NEW:
423 return MSG_IS_NEW(info->flags);
424 case MATCHCRITERIA_NOT_NEW:
425 return !MSG_IS_NEW(info->flags);
426 case MATCHCRITERIA_MARKED:
427 return MSG_IS_MARKED(info->flags);
428 case MATCHCRITERIA_NOT_MARKED:
429 return !MSG_IS_MARKED(info->flags);
430 case MATCHCRITERIA_DELETED:
431 return MSG_IS_DELETED(info->flags);
432 case MATCHCRITERIA_NOT_DELETED:
433 return !MSG_IS_DELETED(info->flags);
434 case MATCHCRITERIA_REPLIED:
435 return MSG_IS_REPLIED(info->flags);
436 case MATCHCRITERIA_NOT_REPLIED:
437 return !MSG_IS_REPLIED(info->flags);
438 case MATCHCRITERIA_FORWARDED:
439 return MSG_IS_FORWARDED(info->flags);
440 case MATCHCRITERIA_NOT_FORWARDED:
441 return !MSG_IS_FORWARDED(info->flags);
442 case MATCHCRITERIA_LOCKED:
443 return MSG_IS_LOCKED(info->flags);
444 case MATCHCRITERIA_NOT_LOCKED:
445 return !MSG_IS_LOCKED(info->flags);
446 case MATCHCRITERIA_COLORLABEL:
447 return MSG_GET_COLORLABEL_VALUE(info->flags) == prop->value;
448 case MATCHCRITERIA_NOT_COLORLABEL:
449 return MSG_GET_COLORLABEL_VALUE(info->flags) != prop->value;
450 case MATCHCRITERIA_IGNORE_THREAD:
451 return MSG_IS_IGNORE_THREAD(info->flags);
452 case MATCHCRITERIA_NOT_IGNORE_THREAD:
453 return !MSG_IS_IGNORE_THREAD(info->flags);
454 case MATCHCRITERIA_SUBJECT:
455 return matcherprop_string_match(prop, info->subject);
456 case MATCHCRITERIA_NOT_SUBJECT:
457 return !matcherprop_string_match(prop, info->subject);
458 case MATCHCRITERIA_FROM:
459 return matcherprop_string_match(prop, info->from);
460 case MATCHCRITERIA_NOT_FROM:
461 return !matcherprop_string_match(prop, info->from);
462 case MATCHCRITERIA_TO:
463 return matcherprop_string_match(prop, info->to);
464 case MATCHCRITERIA_NOT_TO:
465 return !matcherprop_string_match(prop, info->to);
466 case MATCHCRITERIA_CC:
467 return matcherprop_string_match(prop, info->cc);
468 case MATCHCRITERIA_NOT_CC:
469 return !matcherprop_string_match(prop, info->cc);
470 case MATCHCRITERIA_TO_OR_CC:
471 return matcherprop_string_match(prop, info->to)
472 || matcherprop_string_match(prop, info->cc);
473 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
474 return !(matcherprop_string_match(prop, info->to)
475 || matcherprop_string_match(prop, info->cc));
476 case MATCHCRITERIA_AGE_GREATER:
478 return ((t - info->date_t) / (60 * 60 * 24)) >= prop->value;
479 case MATCHCRITERIA_AGE_LOWER:
481 return ((t - info->date_t) / (60 * 60 * 24)) <= prop->value;
482 case MATCHCRITERIA_SCORE_GREATER:
483 return info->score >= prop->value;
484 case MATCHCRITERIA_SCORE_LOWER:
485 return info->score <= prop->value;
486 case MATCHCRITERIA_SCORE_EQUAL:
487 return info->score == prop->value;
488 case MATCHCRITERIA_SIZE_GREATER:
489 /* FIXME: info->size is an off_t */
490 return info->size > (off_t) prop->value;
491 case MATCHCRITERIA_SIZE_EQUAL:
492 /* FIXME: info->size is an off_t */
493 return info->size == (off_t) prop->value;
494 case MATCHCRITERIA_SIZE_SMALLER:
495 /* FIXME: info->size is an off_t */
496 return info->size < (off_t) prop->value;
497 case MATCHCRITERIA_PARTIAL:
498 /* FIXME: info->size is an off_t */
499 return (info->total_size != 0 && info->size != (off_t)info->total_size);
500 case MATCHCRITERIA_NOT_PARTIAL:
501 /* FIXME: info->size is an off_t */
502 return (info->total_size == 0 || info->size == (off_t)info->total_size);
503 case MATCHCRITERIA_NEWSGROUPS:
504 return matcherprop_string_match(prop, info->newsgroups);
505 case MATCHCRITERIA_NOT_NEWSGROUPS:
506 return !matcherprop_string_match(prop, info->newsgroups);
507 case MATCHCRITERIA_INREPLYTO:
508 return matcherprop_string_match(prop, info->inreplyto);
509 case MATCHCRITERIA_NOT_INREPLYTO:
510 return !matcherprop_string_match(prop, info->inreplyto);
511 /* FIXME: Using inreplyto, but matching the (newly implemented)
512 * list of references is better */
513 case MATCHCRITERIA_REFERENCES:
514 return matcherprop_string_match(prop, info->inreplyto);
515 case MATCHCRITERIA_NOT_REFERENCES:
516 return !matcherprop_string_match(prop, info->inreplyto);
517 case MATCHCRITERIA_TEST:
518 return matcherprop_match_test(prop, info);
519 case MATCHCRITERIA_NOT_TEST:
520 return !matcherprop_match_test(prop, info);
526 /* ********************* MatcherList *************************** */
529 *\brief Create a new list of matchers
531 *\param matchers List of matcher structures
532 *\param bool_and Operator
534 *\return MatcherList * New list
536 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
540 cond = g_new0(MatcherList, 1);
542 cond->matchers = matchers;
543 cond->bool_and = bool_and;
549 *\brief Frees a list of matchers
551 *\param cond List of matchers
553 void matcherlist_free(MatcherList *cond)
557 g_return_if_fail(cond);
558 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
559 matcherprop_free((MatcherProp *) l->data);
565 *\brief Skip all headers in a message file
567 *\param fp Message file
569 static void matcherlist_skip_headers(FILE *fp)
573 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
578 *\brief Check if a header matches a matcher condition
580 *\param matcher Matcher structure to check header for
581 *\param buf Header name
583 *\return boolean TRUE if matching header
585 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
591 switch (matcher->criteria) {
592 case MATCHCRITERIA_HEADER:
593 case MATCHCRITERIA_NOT_HEADER:
594 header = procheader_parse_header(buf);
597 if (procheader_headername_equal(header->name,
599 if (matcher->criteria == MATCHCRITERIA_HEADER)
600 result = matcherprop_string_match(matcher, header->body);
602 result = !matcherprop_string_match(matcher, header->body);
603 procheader_header_free(header);
607 procheader_header_free(header);
610 case MATCHCRITERIA_HEADERS_PART:
611 case MATCHCRITERIA_MESSAGE:
612 return matcherprop_string_match(matcher, buf);
613 case MATCHCRITERIA_NOT_MESSAGE:
614 case MATCHCRITERIA_NOT_HEADERS_PART:
615 return !matcherprop_string_match(matcher, buf);
621 *\brief Check if the matcher structure wants headers to
624 *\param matcher Matcher structure
626 *\return gboolean TRUE if the matcher structure describes
627 * a header match condition
629 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
631 switch (matcher->criteria) {
632 case MATCHCRITERIA_HEADER:
633 case MATCHCRITERIA_NOT_HEADER:
634 case MATCHCRITERIA_HEADERS_PART:
635 case MATCHCRITERIA_NOT_HEADERS_PART:
643 *\brief Check if the matcher structure wants the message
644 * to be matched (just perform an action on any
647 *\param matcher Matcher structure
649 *\return gboolean TRUE if matcher condition should match
652 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
654 switch (matcher->criteria) {
655 case MATCHCRITERIA_MESSAGE:
656 case MATCHCRITERIA_NOT_MESSAGE:
664 *\brief Check if a list of conditions matches one header in
667 *\param matchers List of conditions
668 *\param fp Message file
670 *\return gboolean TRUE if one of the headers is matched by
671 * the list of conditions.
673 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
678 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
679 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
680 MatcherProp *matcher = (MatcherProp *) l->data;
685 /* if the criteria is ~headers_part or ~message, ZERO lines
686 * must NOT match for the rule to match. */
687 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
688 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
689 if (matcherprop_match_one_header(matcher, buf)) {
690 matcher->result = TRUE;
692 matcher->result = FALSE;
693 matcher->done = TRUE;
695 /* else, just one line matching is enough for the rule to match
697 } else if (matcherprop_criteria_headers(matcher) ||
698 matcherprop_criteria_message(matcher)){
699 if (matcherprop_match_one_header(matcher, buf)) {
700 matcher->result = TRUE;
701 matcher->done = TRUE;
705 /* if the rule matched and the matchers are OR, no need to
706 * check the others */
707 if (matcher->result && matcher->done) {
708 if (!matchers->bool_and)
717 *\brief Check if a matcher wants to check the message body
719 *\param matcher Matcher structure
721 *\return gboolean TRUE if body must be matched.
723 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
725 switch (matcher->criteria) {
726 case MATCHCRITERIA_BODY_PART:
727 case MATCHCRITERIA_NOT_BODY_PART:
735 *\brief Check if a (line) string matches the criteria
736 * described by a matcher structure
738 *\param matcher Matcher structure
741 *\return gboolean TRUE if string matches criteria
743 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
745 switch (matcher->criteria) {
746 case MATCHCRITERIA_BODY_PART:
747 case MATCHCRITERIA_MESSAGE:
748 return matcherprop_string_match(matcher, line);
749 case MATCHCRITERIA_NOT_BODY_PART:
750 case MATCHCRITERIA_NOT_MESSAGE:
751 return !matcherprop_string_match(matcher, line);
757 *\brief Check if a line in a message file's body matches
760 *\param matchers List of conditions
761 *\param fp Message file
763 *\return gboolean TRUE if succesful match
765 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
770 while (fgets(buf, sizeof(buf), fp) != NULL) {
771 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
772 MatcherProp *matcher = (MatcherProp *) l->data;
777 /* if the criteria is ~body_part or ~message, ZERO lines
778 * must NOT match for the rule to match. */
779 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
780 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
781 if (matcherprop_match_line(matcher, buf)) {
782 matcher->result = TRUE;
784 matcher->result = FALSE;
785 matcher->done = TRUE;
787 /* else, just one line has to match */
788 } else if (matcherprop_criteria_body(matcher) ||
789 matcherprop_criteria_message(matcher)) {
790 if (matcherprop_match_line(matcher, buf)) {
791 matcher->result = TRUE;
792 matcher->done = TRUE;
796 /* if the matchers are OR'ed and the rule matched,
797 * no need to check the others. */
798 if (matcher->result && matcher->done) {
799 if (!matchers->bool_and)
808 *\brief Check if a message file matches criteria
810 *\param matchers Criteria
811 *\param info Message info
812 *\param result Default result
814 *\return gboolean TRUE if matched
816 gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
819 gboolean read_headers;
825 /* file need to be read ? */
827 read_headers = FALSE;
829 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
830 MatcherProp *matcher = (MatcherProp *) l->data;
832 if (matcherprop_criteria_headers(matcher))
834 if (matcherprop_criteria_body(matcher))
836 if (matcherprop_criteria_message(matcher)) {
840 matcher->result = FALSE;
841 matcher->done = FALSE;
844 if (!read_headers && !read_body)
847 file = procmsg_get_message_file_full(info, read_headers, read_body);
851 if ((fp = g_fopen(file, "rb")) == NULL) {
852 FILE_OP_ERROR(file, "fopen");
857 /* read the headers */
860 if (matcherlist_match_headers(matchers, fp))
863 matcherlist_skip_headers(fp);
868 matcherlist_match_body(matchers, fp);
871 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
872 MatcherProp *matcher = (MatcherProp *) l->data;
874 if (matcherprop_criteria_headers(matcher) ||
875 matcherprop_criteria_body(matcher) ||
876 matcherprop_criteria_message(matcher)) {
877 if (matcher->result) {
878 if (!matchers->bool_and) {
884 if (matchers->bool_and) {
900 *\brief Test list of conditions on a message.
902 *\param matchers List of conditions
903 *\param info Message info
905 *\return gboolean TRUE if matched
907 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
912 if (matchers->bool_and)
917 /* test the cached elements */
919 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
920 MatcherProp *matcher = (MatcherProp *) l->data;
922 switch(matcher->criteria) {
923 case MATCHCRITERIA_ALL:
924 case MATCHCRITERIA_UNREAD:
925 case MATCHCRITERIA_NOT_UNREAD:
926 case MATCHCRITERIA_NEW:
927 case MATCHCRITERIA_NOT_NEW:
928 case MATCHCRITERIA_MARKED:
929 case MATCHCRITERIA_NOT_MARKED:
930 case MATCHCRITERIA_DELETED:
931 case MATCHCRITERIA_NOT_DELETED:
932 case MATCHCRITERIA_REPLIED:
933 case MATCHCRITERIA_NOT_REPLIED:
934 case MATCHCRITERIA_FORWARDED:
935 case MATCHCRITERIA_NOT_FORWARDED:
936 case MATCHCRITERIA_LOCKED:
937 case MATCHCRITERIA_NOT_LOCKED:
938 case MATCHCRITERIA_COLORLABEL:
939 case MATCHCRITERIA_NOT_COLORLABEL:
940 case MATCHCRITERIA_IGNORE_THREAD:
941 case MATCHCRITERIA_NOT_IGNORE_THREAD:
942 case MATCHCRITERIA_SUBJECT:
943 case MATCHCRITERIA_NOT_SUBJECT:
944 case MATCHCRITERIA_FROM:
945 case MATCHCRITERIA_NOT_FROM:
946 case MATCHCRITERIA_TO:
947 case MATCHCRITERIA_NOT_TO:
948 case MATCHCRITERIA_CC:
949 case MATCHCRITERIA_NOT_CC:
950 case MATCHCRITERIA_TO_OR_CC:
951 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
952 case MATCHCRITERIA_AGE_GREATER:
953 case MATCHCRITERIA_AGE_LOWER:
954 case MATCHCRITERIA_NEWSGROUPS:
955 case MATCHCRITERIA_NOT_NEWSGROUPS:
956 case MATCHCRITERIA_INREPLYTO:
957 case MATCHCRITERIA_NOT_INREPLYTO:
958 case MATCHCRITERIA_REFERENCES:
959 case MATCHCRITERIA_NOT_REFERENCES:
960 case MATCHCRITERIA_SCORE_GREATER:
961 case MATCHCRITERIA_SCORE_LOWER:
962 case MATCHCRITERIA_SCORE_EQUAL:
963 case MATCHCRITERIA_SIZE_GREATER:
964 case MATCHCRITERIA_SIZE_SMALLER:
965 case MATCHCRITERIA_SIZE_EQUAL:
966 case MATCHCRITERIA_TEST:
967 case MATCHCRITERIA_NOT_TEST:
968 case MATCHCRITERIA_PARTIAL:
969 case MATCHCRITERIA_NOT_PARTIAL:
970 if (matcherprop_match(matcher, info)) {
971 if (!matchers->bool_and) {
976 if (matchers->bool_and) {
983 /* test the condition on the file */
985 if (matcherlist_match_file(matchers, info, result)) {
986 if (!matchers->bool_and)
990 if (matchers->bool_and)
998 static gint quote_filter_str(gchar * result, guint size,
1008 for(p = path ; * p != '\0' ; p ++) {
1010 if ((* p != '\"') && (* p != '\\')) {
1011 if (remaining > 0) {
1017 result[size - 1] = '\0';
1022 if (remaining >= 2) {
1030 result[size - 1] = '\0';
1035 if (remaining > 0) {
1039 result[size - 1] = '\0';
1047 gchar * matcher_quote_str(const gchar * src)
1052 len = strlen(src) * 2 + 1;
1053 res = g_malloc(len);
1054 quote_filter_str(res, len, src);
1060 *\brief Convert a matcher structure to a string
1062 *\param matcher Matcher structure
1064 *\return gchar * Newly allocated string
1066 gchar *matcherprop_to_string(MatcherProp *matcher)
1068 gchar *matcher_str = NULL;
1069 const gchar *criteria_str;
1070 const gchar *matchtype_str;
1072 gchar * quoted_expr;
1074 criteria_str = NULL;
1075 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1076 if (matchparser_tab[i].id == matcher->criteria)
1077 criteria_str = matchparser_tab[i].str;
1079 if (criteria_str == NULL)
1082 switch (matcher->criteria) {
1083 case MATCHCRITERIA_AGE_GREATER:
1084 case MATCHCRITERIA_AGE_LOWER:
1085 case MATCHCRITERIA_SCORE_GREATER:
1086 case MATCHCRITERIA_SCORE_LOWER:
1087 case MATCHCRITERIA_SCORE_EQUAL:
1088 case MATCHCRITERIA_SIZE_GREATER:
1089 case MATCHCRITERIA_SIZE_SMALLER:
1090 case MATCHCRITERIA_SIZE_EQUAL:
1091 case MATCHCRITERIA_COLORLABEL:
1092 case MATCHCRITERIA_NOT_COLORLABEL:
1093 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1094 case MATCHCRITERIA_ALL:
1095 case MATCHCRITERIA_UNREAD:
1096 case MATCHCRITERIA_NOT_UNREAD:
1097 case MATCHCRITERIA_NEW:
1098 case MATCHCRITERIA_NOT_NEW:
1099 case MATCHCRITERIA_MARKED:
1100 case MATCHCRITERIA_NOT_MARKED:
1101 case MATCHCRITERIA_DELETED:
1102 case MATCHCRITERIA_NOT_DELETED:
1103 case MATCHCRITERIA_REPLIED:
1104 case MATCHCRITERIA_NOT_REPLIED:
1105 case MATCHCRITERIA_FORWARDED:
1106 case MATCHCRITERIA_NOT_FORWARDED:
1107 case MATCHCRITERIA_LOCKED:
1108 case MATCHCRITERIA_NOT_LOCKED:
1109 case MATCHCRITERIA_PARTIAL:
1110 case MATCHCRITERIA_NOT_PARTIAL:
1111 case MATCHCRITERIA_IGNORE_THREAD:
1112 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1113 return g_strdup(criteria_str);
1114 case MATCHCRITERIA_TEST:
1115 case MATCHCRITERIA_NOT_TEST:
1116 quoted_expr = matcher_quote_str(matcher->expr);
1117 matcher_str = g_strdup_printf("%s \"%s\"",
1118 criteria_str, quoted_expr);
1119 g_free(quoted_expr);
1123 matchtype_str = NULL;
1124 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1125 if (matchparser_tab[i].id == matcher->matchtype)
1126 matchtype_str = matchparser_tab[i].str;
1129 if (matchtype_str == NULL)
1132 switch (matcher->matchtype) {
1133 case MATCHTYPE_MATCH:
1134 case MATCHTYPE_MATCHCASE:
1135 case MATCHTYPE_REGEXP:
1136 case MATCHTYPE_REGEXPCASE:
1137 case MATCHTYPE_ALL_IN_ADDRESSBOOK:
1138 case MATCHTYPE_ANY_IN_ADDRESSBOOK:
1139 quoted_expr = matcher_quote_str(matcher->expr);
1140 if (matcher->header) {
1141 gchar * quoted_header;
1143 quoted_header = matcher_quote_str(matcher->header);
1144 matcher_str = g_strdup_printf
1145 ("%s \"%s\" %s \"%s\"",
1146 criteria_str, quoted_header,
1147 matchtype_str, quoted_expr);
1148 g_free(quoted_header);
1151 matcher_str = g_strdup_printf
1152 ("%s %s \"%s\"", criteria_str,
1153 matchtype_str, quoted_expr);
1154 g_free(quoted_expr);
1162 *\brief Convert a list of conditions to a string
1164 *\param matchers List of conditions
1166 *\return gchar * Newly allocated string
1168 gchar *matcherlist_to_string(const MatcherList *matchers)
1174 gchar *result = NULL;
1176 count = g_slist_length(matchers->matchers);
1177 vstr = g_new(gchar *, count + 1);
1179 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1180 l = g_slist_next(l), cur_str ++) {
1181 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1182 if (*cur_str == NULL)
1187 if (matchers->bool_and)
1188 result = g_strjoinv(" & ", vstr);
1190 result = g_strjoinv(" | ", vstr);
1192 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1200 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1201 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1203 static void add_str_default(gchar ** dest,
1204 const gchar * s, const gchar * d)
1206 gchar quoted_str[4096];
1214 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1215 strcpy(* dest, quoted_str);
1217 (* dest) += strlen(* dest);
1220 /* matching_build_command() - preferably cmd should be unescaped */
1222 *\brief Build the command line to execute
1224 *\param cmd String with command line specifiers
1225 *\param info Message info to use for command
1227 *\return gchar * Newly allocated string
1229 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1231 const gchar *s = cmd;
1232 gchar *filename = NULL;
1233 gchar *processed_cmd;
1237 const gchar *const no_subject = _("(none)") ;
1238 const gchar *const no_from = _("(none)") ;
1239 const gchar *const no_to = _("(none)") ;
1240 const gchar *const no_cc = _("(none)") ;
1241 const gchar *const no_date = _("(none)") ;
1242 const gchar *const no_msgid = _("(none)") ;
1243 const gchar *const no_newsgroups = _("(none)") ;
1244 const gchar *const no_references = _("(none)") ;
1246 size = STRLEN_ZERO(cmd) + 1;
1247 while (*s != '\0') {
1254 case 's': /* subject */
1255 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1257 case 'f': /* from */
1258 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1261 size += STRLEN_DEFAULT(info->to, no_to) - 2;
1264 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
1266 case 'd': /* date */
1267 size += STRLEN_DEFAULT(info->date, no_date) - 2;
1269 case 'i': /* message-id */
1270 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
1272 case 'n': /* newsgroups */
1273 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
1275 case 'r': /* references */
1276 /* FIXME: using the inreplyto header for reference */
1277 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
1279 case 'F': /* file */
1280 if (filename == NULL)
1281 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1283 if (filename == NULL) {
1284 g_warning("filename is not set");
1288 size += strlen(filename) - 2;
1297 /* as the string can be quoted, we double the result */
1300 processed_cmd = g_new0(gchar, size);
1304 while (*s != '\0') {
1312 case 's': /* subject */
1313 add_str_default(&p, info->subject,
1316 case 'f': /* from */
1317 add_str_default(&p, info->from,
1321 add_str_default(&p, info->to,
1325 add_str_default(&p, info->cc,
1328 case 'd': /* date */
1329 add_str_default(&p, info->date,
1332 case 'i': /* message-id */
1333 add_str_default(&p, info->msgid,
1336 case 'n': /* newsgroups */
1337 add_str_default(&p, info->newsgroups,
1340 case 'r': /* references */
1341 /* FIXME: using the inreplyto header for references */
1342 add_str_default(&p, info->inreplyto, no_references);
1344 case 'F': /* file */
1345 if (filename != NULL)
1346 add_str_default(&p, filename, NULL);
1365 return processed_cmd;
1367 #undef STRLEN_DEFAULT
1370 /* ************************************************************ */
1374 *\brief Write filtering list to file
1377 *\param prefs_filtering List of filtering conditions
1379 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
1383 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
1384 gchar *filtering_str;
1385 gchar *tmp_name = NULL;
1386 FilteringProp *prop;
1388 if (NULL == (prop = (FilteringProp *) cur->data))
1391 if (NULL == (filtering_str = filteringprop_to_string(prop)))
1394 if (fputs("rulename \"", fp) == EOF) {
1395 FILE_OP_ERROR("filtering config", "fputs || fputc");
1396 g_free(filtering_str);
1399 tmp_name = prop->name;
1400 while (tmp_name && *tmp_name != '\0') {
1401 if (*tmp_name != '"') {
1402 if (fputc(*tmp_name, fp) == EOF) {
1403 FILE_OP_ERROR("filtering config", "fputc");
1404 g_free(filtering_str);
1407 } else if (*tmp_name == '"') {
1408 if (fputc('\\', fp) == EOF ||
1409 fputc('"', fp) == EOF) {
1410 FILE_OP_ERROR("filtering config", "fputc");
1411 g_free(filtering_str);
1417 if(fputs("\" ", fp) == EOF ||
1418 fputs(filtering_str, fp) == EOF ||
1419 fputc('\n', fp) == EOF) {
1420 FILE_OP_ERROR("filtering config", "fputs || fputc");
1421 g_free(filtering_str);
1424 g_free(filtering_str);
1429 *\brief Write matchers from a folder item
1431 *\param node Node with folder info
1432 *\param data File pointer
1434 *\return gboolean FALSE
1436 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
1441 GSList *prefs_filtering;
1444 /* prevent warning */
1445 if (item->path == NULL)
1447 id = folder_item_get_identifier(item);
1450 prefs_filtering = item->prefs->processing;
1452 if (prefs_filtering != NULL) {
1453 fprintf(fp, "[%s]\n", id);
1454 prefs_filtering_write(fp, prefs_filtering);
1464 *\brief Save matchers from folder items
1468 static void prefs_matcher_save(FILE *fp)
1472 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1475 folder = (Folder *) cur->data;
1476 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1477 prefs_matcher_write_func, fp);
1480 /* pre global rules */
1481 fprintf(fp, "[preglobal]\n");
1482 prefs_filtering_write(fp, pre_global_processing);
1485 /* post global rules */
1486 fprintf(fp, "[postglobal]\n");
1487 prefs_filtering_write(fp, post_global_processing);
1490 /* filtering rules */
1491 fprintf(fp, "[filtering]\n");
1492 prefs_filtering_write(fp, filtering_rules);
1497 *\brief Write filtering / matcher configuration file
1499 void prefs_matcher_write_config(void)
1504 debug_print("Writing matcher configuration...\n");
1506 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1509 if ((pfile = prefs_write_open(rcpath)) == NULL) {
1510 g_warning("failed to write configuration to file\n");
1516 prefs_matcher_save(pfile->fp);
1520 if (prefs_file_close(pfile) < 0) {
1521 g_warning("failed to write configuration to file\n");
1526 /* ******************************************************************* */
1528 void matcher_add_rulenames(const gchar *rcpath)
1530 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
1531 FILE *src = g_fopen(rcpath, "rb");
1532 FILE *dst = g_fopen(newpath, "wb");
1533 gchar buf[BUFFSIZE];
1541 while (fgets (buf, sizeof(buf), src) != NULL) {
1542 if (strlen(buf) > 2 && buf[0] != '['
1543 && strncmp(buf, "rulename \"", 10)) {
1544 fwrite("rulename \"\" ",
1545 strlen("rulename \"\" "), 1, dst);
1547 fwrite(buf, strlen(buf), 1, dst);
1551 move_file(newpath, rcpath, TRUE);
1556 *\brief Read matcher configuration
1558 void prefs_matcher_read_config(void)
1561 gchar *rc_old_format;
1564 create_matchparser_hashtab();
1565 prefs_filtering_clear();
1567 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
1568 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
1569 ".pre_names", NULL);
1571 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
1572 /* backup file with no rules names, in case
1573 * anything goes wrong */
1574 copy_file(rcpath, rc_old_format, FALSE);
1575 /* now hack the file in order to have it to the new format */
1576 matcher_add_rulenames(rcpath);
1578 f = g_fopen(rcpath, "rb");
1582 matcher_parser_start_parsing(f);
1583 fclose(matcher_parserin);
1586 /* previous version compatibily */
1588 /* printf("reading filtering\n"); */
1589 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1590 FILTERING_RC, NULL);
1591 f = g_fopen(rcpath, "rb");
1595 matcher_parser_start_parsing(f);
1596 fclose(matcher_parserin);
1599 /* printf("reading scoring\n"); */
1600 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1602 f = g_fopen(rcpath, "rb");
1606 matcher_parser_start_parsing(f);
1607 fclose(matcher_parserin);