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"},
140 {MATCHACTION_IGNORE, "ignore"},
144 *\brief Look up table with keywords defined in \sa matchparser_tab
146 static GHashTable *matchparser_hashtab;
149 *\brief Translate keyword id to keyword string
151 *\param id Id of keyword
153 *\return const gchar * Keyword
155 const gchar *get_matchparser_tab_str(gint id)
159 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
160 if (matchparser_tab[i].id == id)
161 return matchparser_tab[i].str;
167 *\brief Create keyword lookup table
169 static void create_matchparser_hashtab(void)
173 if (matchparser_hashtab) return;
174 matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
175 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
176 g_hash_table_insert(matchparser_hashtab,
177 matchparser_tab[i].str,
178 (gpointer) &matchparser_tab[i]);
182 *\brief Return a keyword id from a keyword string
184 *\param str Keyword string
186 *\return gint Keyword id
188 gint get_matchparser_tab_id(const gchar *str)
192 if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
198 /* **************** data structure allocation **************** */
201 *\brief Allocate a structure for a filtering / scoring
202 * "condition" (a matcher structure)
204 *\param criteria Criteria ID (MATCHCRITERIA_XXXX)
205 *\param header Header string (if criteria is MATCHCRITERIA_HEADER)
206 *\param matchtype Type of action (MATCHTYPE_XXX)
207 *\param expr String value or expression to check
208 *\param value Integer value to check
210 *\return MatcherProp * Pointer to newly allocated structure
212 MatcherProp *matcherprop_new(gint criteria, const gchar *header,
213 gint matchtype, const gchar *expr,
218 prop = g_new0(MatcherProp, 1);
219 prop->criteria = criteria;
220 prop->header = header != NULL ? g_strdup(header) : NULL;
221 prop->expr = expr != NULL ? g_strdup(expr) : NULL;
222 prop->matchtype = matchtype;
231 *\brief Free a matcher structure
233 *\param prop Pointer to matcher structure allocated with
236 void matcherprop_free(MatcherProp *prop)
241 g_free(prop->header);
242 if (prop->preg != NULL) {
250 *\brief Copy a matcher structure
252 *\param src Matcher structure to copy
254 *\return MatcherProp * Pointer to newly allocated matcher structure
256 MatcherProp *matcherprop_copy(const MatcherProp *src)
258 MatcherProp *prop = g_new0(MatcherProp, 1);
260 prop->criteria = src->criteria;
261 prop->header = src->header ? g_strdup(src->header) : NULL;
262 prop->expr = src->expr ? g_strdup(src->expr) : NULL;
263 prop->matchtype = src->matchtype;
265 prop->preg = NULL; /* will be re-evaluated */
266 prop->value = src->value;
267 prop->error = src->error;
271 /* ************** match ******************************/
273 static gboolean match_with_addresses_in_addressbook
274 (MatcherProp *prop, const gchar *str, gint type)
276 GSList *address_list = NULL;
278 gboolean res = FALSE;
280 if (str == NULL || *str == 0)
283 /* XXX: perhaps complete with comments too */
284 address_list = address_list_append(address_list, str);
288 start_address_completion();
290 for (walk = address_list; walk != NULL; walk = walk->next) {
291 gboolean found = complete_address(walk->data) ? TRUE : FALSE;
294 if (!found && type == MATCHTYPE_ALL_IN_ADDRESSBOOK) {
301 g_slist_free(address_list);
303 end_address_completion();
309 *\brief Find out if a string matches a condition
311 *\param prop Matcher structure
312 *\param str String to check
314 *\return gboolean TRUE if str matches the condition in the
317 static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str)
325 switch (prop->matchtype) {
326 case MATCHTYPE_REGEXPCASE:
327 case MATCHTYPE_REGEXP:
328 if (!prop->preg && (prop->error == 0)) {
329 prop->preg = g_new0(regex_t, 1);
330 /* if regexp then don't use the escaped string */
331 if (regcomp(prop->preg, prop->expr,
332 REG_NOSUB | REG_EXTENDED
333 | ((prop->matchtype == MATCHTYPE_REGEXPCASE)
334 ? REG_ICASE : 0)) != 0) {
340 if (prop->preg == NULL)
343 if (regexec(prop->preg, str, 0, NULL, 0) == 0)
348 case MATCHTYPE_ALL_IN_ADDRESSBOOK:
349 case MATCHTYPE_ANY_IN_ADDRESSBOOK:
350 return match_with_addresses_in_addressbook
351 (prop, str, prop->matchtype);
353 case MATCHTYPE_MATCH:
354 return (strstr(str, prop->expr) != NULL);
356 /* FIXME: put upper in unesc_str */
357 case MATCHTYPE_MATCHCASE:
358 str2 = alloca(strlen(prop->expr) + 1);
359 strcpy(str2, prop->expr);
361 str1 = alloca(strlen(str) + 1);
364 return (strstr(str1, str2) != NULL);
372 *\brief Execute a command defined in the matcher structure
374 *\param prop Pointer to matcher structure
375 *\param info Pointer to message info structure
377 *\return gboolean TRUE if command was executed succesfully
379 static gboolean matcherprop_match_test(const MatcherProp *prop,
386 file = procmsg_get_message_file(info);
391 cmd = matching_build_command(prop->expr, info);
395 retval = system(cmd);
396 debug_print("Command exit code: %d\n", retval);
399 return (retval == 0);
403 *\brief Check if a message matches the condition in a matcher
406 *\param prop Pointer to matcher structure
407 *\param info Pointer to message info
409 *\return gboolean TRUE if a match
411 gboolean matcherprop_match(MatcherProp *prop,
416 switch(prop->criteria) {
417 case MATCHCRITERIA_ALL:
419 case MATCHCRITERIA_UNREAD:
420 return MSG_IS_UNREAD(info->flags);
421 case MATCHCRITERIA_NOT_UNREAD:
422 return !MSG_IS_UNREAD(info->flags);
423 case MATCHCRITERIA_NEW:
424 return MSG_IS_NEW(info->flags);
425 case MATCHCRITERIA_NOT_NEW:
426 return !MSG_IS_NEW(info->flags);
427 case MATCHCRITERIA_MARKED:
428 return MSG_IS_MARKED(info->flags);
429 case MATCHCRITERIA_NOT_MARKED:
430 return !MSG_IS_MARKED(info->flags);
431 case MATCHCRITERIA_DELETED:
432 return MSG_IS_DELETED(info->flags);
433 case MATCHCRITERIA_NOT_DELETED:
434 return !MSG_IS_DELETED(info->flags);
435 case MATCHCRITERIA_REPLIED:
436 return MSG_IS_REPLIED(info->flags);
437 case MATCHCRITERIA_NOT_REPLIED:
438 return !MSG_IS_REPLIED(info->flags);
439 case MATCHCRITERIA_FORWARDED:
440 return MSG_IS_FORWARDED(info->flags);
441 case MATCHCRITERIA_NOT_FORWARDED:
442 return !MSG_IS_FORWARDED(info->flags);
443 case MATCHCRITERIA_LOCKED:
444 return MSG_IS_LOCKED(info->flags);
445 case MATCHCRITERIA_NOT_LOCKED:
446 return !MSG_IS_LOCKED(info->flags);
447 case MATCHCRITERIA_COLORLABEL:
448 return MSG_GET_COLORLABEL_VALUE(info->flags) == prop->value;
449 case MATCHCRITERIA_NOT_COLORLABEL:
450 return MSG_GET_COLORLABEL_VALUE(info->flags) != prop->value;
451 case MATCHCRITERIA_IGNORE_THREAD:
452 return MSG_IS_IGNORE_THREAD(info->flags);
453 case MATCHCRITERIA_NOT_IGNORE_THREAD:
454 return !MSG_IS_IGNORE_THREAD(info->flags);
455 case MATCHCRITERIA_SUBJECT:
456 return matcherprop_string_match(prop, info->subject);
457 case MATCHCRITERIA_NOT_SUBJECT:
458 return !matcherprop_string_match(prop, info->subject);
459 case MATCHCRITERIA_FROM:
460 return matcherprop_string_match(prop, info->from);
461 case MATCHCRITERIA_NOT_FROM:
462 return !matcherprop_string_match(prop, info->from);
463 case MATCHCRITERIA_TO:
464 return matcherprop_string_match(prop, info->to);
465 case MATCHCRITERIA_NOT_TO:
466 return !matcherprop_string_match(prop, info->to);
467 case MATCHCRITERIA_CC:
468 return matcherprop_string_match(prop, info->cc);
469 case MATCHCRITERIA_NOT_CC:
470 return !matcherprop_string_match(prop, info->cc);
471 case MATCHCRITERIA_TO_OR_CC:
472 return matcherprop_string_match(prop, info->to)
473 || matcherprop_string_match(prop, info->cc);
474 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
475 return !(matcherprop_string_match(prop, info->to)
476 || matcherprop_string_match(prop, info->cc));
477 case MATCHCRITERIA_AGE_GREATER:
479 return ((t - info->date_t) / (60 * 60 * 24)) >= prop->value;
480 case MATCHCRITERIA_AGE_LOWER:
482 return ((t - info->date_t) / (60 * 60 * 24)) <= prop->value;
483 case MATCHCRITERIA_SCORE_GREATER:
484 return info->score >= prop->value;
485 case MATCHCRITERIA_SCORE_LOWER:
486 return info->score <= prop->value;
487 case MATCHCRITERIA_SCORE_EQUAL:
488 return info->score == prop->value;
489 case MATCHCRITERIA_SIZE_GREATER:
490 /* FIXME: info->size is an off_t */
491 return info->size > (off_t) prop->value;
492 case MATCHCRITERIA_SIZE_EQUAL:
493 /* FIXME: info->size is an off_t */
494 return info->size == (off_t) prop->value;
495 case MATCHCRITERIA_SIZE_SMALLER:
496 /* FIXME: info->size is an off_t */
497 return info->size < (off_t) prop->value;
498 case MATCHCRITERIA_PARTIAL:
499 /* FIXME: info->size is an off_t */
500 return (info->total_size != 0 && info->size != (off_t)info->total_size);
501 case MATCHCRITERIA_NOT_PARTIAL:
502 /* FIXME: info->size is an off_t */
503 return (info->total_size == 0 || info->size == (off_t)info->total_size);
504 case MATCHCRITERIA_NEWSGROUPS:
505 return matcherprop_string_match(prop, info->newsgroups);
506 case MATCHCRITERIA_NOT_NEWSGROUPS:
507 return !matcherprop_string_match(prop, info->newsgroups);
508 case MATCHCRITERIA_INREPLYTO:
509 return matcherprop_string_match(prop, info->inreplyto);
510 case MATCHCRITERIA_NOT_INREPLYTO:
511 return !matcherprop_string_match(prop, info->inreplyto);
512 /* FIXME: Using inreplyto, but matching the (newly implemented)
513 * list of references is better */
514 case MATCHCRITERIA_REFERENCES:
515 return matcherprop_string_match(prop, info->inreplyto);
516 case MATCHCRITERIA_NOT_REFERENCES:
517 return !matcherprop_string_match(prop, info->inreplyto);
518 case MATCHCRITERIA_TEST:
519 return matcherprop_match_test(prop, info);
520 case MATCHCRITERIA_NOT_TEST:
521 return !matcherprop_match_test(prop, info);
527 /* ********************* MatcherList *************************** */
530 *\brief Create a new list of matchers
532 *\param matchers List of matcher structures
533 *\param bool_and Operator
535 *\return MatcherList * New list
537 MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
541 cond = g_new0(MatcherList, 1);
543 cond->matchers = matchers;
544 cond->bool_and = bool_and;
550 *\brief Frees a list of matchers
552 *\param cond List of matchers
554 void matcherlist_free(MatcherList *cond)
558 g_return_if_fail(cond);
559 for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
560 matcherprop_free((MatcherProp *) l->data);
566 *\brief Skip all headers in a message file
568 *\param fp Message file
570 static void matcherlist_skip_headers(FILE *fp)
574 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
579 *\brief Check if a header matches a matcher condition
581 *\param matcher Matcher structure to check header for
582 *\param buf Header name
584 *\return boolean TRUE if matching header
586 static gboolean matcherprop_match_one_header(MatcherProp *matcher,
592 switch (matcher->criteria) {
593 case MATCHCRITERIA_HEADER:
594 case MATCHCRITERIA_NOT_HEADER:
595 header = procheader_parse_header(buf);
598 if (procheader_headername_equal(header->name,
600 if (matcher->criteria == MATCHCRITERIA_HEADER)
601 result = matcherprop_string_match(matcher, header->body);
603 result = !matcherprop_string_match(matcher, header->body);
604 procheader_header_free(header);
608 procheader_header_free(header);
611 case MATCHCRITERIA_HEADERS_PART:
612 case MATCHCRITERIA_MESSAGE:
613 return matcherprop_string_match(matcher, buf);
614 case MATCHCRITERIA_NOT_MESSAGE:
615 case MATCHCRITERIA_NOT_HEADERS_PART:
616 return !matcherprop_string_match(matcher, buf);
622 *\brief Check if the matcher structure wants headers to
625 *\param matcher Matcher structure
627 *\return gboolean TRUE if the matcher structure describes
628 * a header match condition
630 static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
632 switch (matcher->criteria) {
633 case MATCHCRITERIA_HEADER:
634 case MATCHCRITERIA_NOT_HEADER:
635 case MATCHCRITERIA_HEADERS_PART:
636 case MATCHCRITERIA_NOT_HEADERS_PART:
644 *\brief Check if the matcher structure wants the message
645 * to be matched (just perform an action on any
648 *\param matcher Matcher structure
650 *\return gboolean TRUE if matcher condition should match
653 static gboolean matcherprop_criteria_message(MatcherProp *matcher)
655 switch (matcher->criteria) {
656 case MATCHCRITERIA_MESSAGE:
657 case MATCHCRITERIA_NOT_MESSAGE:
665 *\brief Check if a list of conditions matches one header in
668 *\param matchers List of conditions
669 *\param fp Message file
671 *\return gboolean TRUE if one of the headers is matched by
672 * the list of conditions.
674 static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
679 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
680 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
681 MatcherProp *matcher = (MatcherProp *) l->data;
686 /* if the criteria is ~headers_part or ~message, ZERO lines
687 * must NOT match for the rule to match. */
688 if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
689 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
690 if (matcherprop_match_one_header(matcher, buf)) {
691 matcher->result = TRUE;
693 matcher->result = FALSE;
694 matcher->done = TRUE;
696 /* else, just one line matching is enough for the rule to match
698 } else if (matcherprop_criteria_headers(matcher) ||
699 matcherprop_criteria_message(matcher)){
700 if (matcherprop_match_one_header(matcher, buf)) {
701 matcher->result = TRUE;
702 matcher->done = TRUE;
706 /* if the rule matched and the matchers are OR, no need to
707 * check the others */
708 if (matcher->result && matcher->done) {
709 if (!matchers->bool_and)
718 *\brief Check if a matcher wants to check the message body
720 *\param matcher Matcher structure
722 *\return gboolean TRUE if body must be matched.
724 static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
726 switch (matcher->criteria) {
727 case MATCHCRITERIA_BODY_PART:
728 case MATCHCRITERIA_NOT_BODY_PART:
736 *\brief Check if a (line) string matches the criteria
737 * described by a matcher structure
739 *\param matcher Matcher structure
742 *\return gboolean TRUE if string matches criteria
744 static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
746 switch (matcher->criteria) {
747 case MATCHCRITERIA_BODY_PART:
748 case MATCHCRITERIA_MESSAGE:
749 return matcherprop_string_match(matcher, line);
750 case MATCHCRITERIA_NOT_BODY_PART:
751 case MATCHCRITERIA_NOT_MESSAGE:
752 return !matcherprop_string_match(matcher, line);
758 *\brief Check if a line in a message file's body matches
761 *\param matchers List of conditions
762 *\param fp Message file
764 *\return gboolean TRUE if succesful match
766 static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
771 while (fgets(buf, sizeof(buf), fp) != NULL) {
772 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
773 MatcherProp *matcher = (MatcherProp *) l->data;
778 /* if the criteria is ~body_part or ~message, ZERO lines
779 * must NOT match for the rule to match. */
780 if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
781 matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
782 if (matcherprop_match_line(matcher, buf)) {
783 matcher->result = TRUE;
785 matcher->result = FALSE;
786 matcher->done = TRUE;
788 /* else, just one line has to match */
789 } else if (matcherprop_criteria_body(matcher) ||
790 matcherprop_criteria_message(matcher)) {
791 if (matcherprop_match_line(matcher, buf)) {
792 matcher->result = TRUE;
793 matcher->done = TRUE;
797 /* if the matchers are OR'ed and the rule matched,
798 * no need to check the others. */
799 if (matcher->result && matcher->done) {
800 if (!matchers->bool_and)
809 *\brief Check if a message file matches criteria
811 *\param matchers Criteria
812 *\param info Message info
813 *\param result Default result
815 *\return gboolean TRUE if matched
817 gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
820 gboolean read_headers;
826 /* file need to be read ? */
828 read_headers = FALSE;
830 for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
831 MatcherProp *matcher = (MatcherProp *) l->data;
833 if (matcherprop_criteria_headers(matcher))
835 if (matcherprop_criteria_body(matcher))
837 if (matcherprop_criteria_message(matcher)) {
841 matcher->result = FALSE;
842 matcher->done = FALSE;
845 if (!read_headers && !read_body)
848 file = procmsg_get_message_file_full(info, read_headers, read_body);
852 if ((fp = g_fopen(file, "rb")) == NULL) {
853 FILE_OP_ERROR(file, "fopen");
858 /* read the headers */
861 if (matcherlist_match_headers(matchers, fp))
864 matcherlist_skip_headers(fp);
869 matcherlist_match_body(matchers, fp);
872 for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
873 MatcherProp *matcher = (MatcherProp *) l->data;
875 if (matcherprop_criteria_headers(matcher) ||
876 matcherprop_criteria_body(matcher) ||
877 matcherprop_criteria_message(matcher)) {
878 if (matcher->result) {
879 if (!matchers->bool_and) {
885 if (matchers->bool_and) {
901 *\brief Test list of conditions on a message.
903 *\param matchers List of conditions
904 *\param info Message info
906 *\return gboolean TRUE if matched
908 gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
916 if (matchers->bool_and)
921 /* test the cached elements */
923 for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
924 MatcherProp *matcher = (MatcherProp *) l->data;
926 switch(matcher->criteria) {
927 case MATCHCRITERIA_ALL:
928 case MATCHCRITERIA_UNREAD:
929 case MATCHCRITERIA_NOT_UNREAD:
930 case MATCHCRITERIA_NEW:
931 case MATCHCRITERIA_NOT_NEW:
932 case MATCHCRITERIA_MARKED:
933 case MATCHCRITERIA_NOT_MARKED:
934 case MATCHCRITERIA_DELETED:
935 case MATCHCRITERIA_NOT_DELETED:
936 case MATCHCRITERIA_REPLIED:
937 case MATCHCRITERIA_NOT_REPLIED:
938 case MATCHCRITERIA_FORWARDED:
939 case MATCHCRITERIA_NOT_FORWARDED:
940 case MATCHCRITERIA_LOCKED:
941 case MATCHCRITERIA_NOT_LOCKED:
942 case MATCHCRITERIA_COLORLABEL:
943 case MATCHCRITERIA_NOT_COLORLABEL:
944 case MATCHCRITERIA_IGNORE_THREAD:
945 case MATCHCRITERIA_NOT_IGNORE_THREAD:
946 case MATCHCRITERIA_SUBJECT:
947 case MATCHCRITERIA_NOT_SUBJECT:
948 case MATCHCRITERIA_FROM:
949 case MATCHCRITERIA_NOT_FROM:
950 case MATCHCRITERIA_TO:
951 case MATCHCRITERIA_NOT_TO:
952 case MATCHCRITERIA_CC:
953 case MATCHCRITERIA_NOT_CC:
954 case MATCHCRITERIA_TO_OR_CC:
955 case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
956 case MATCHCRITERIA_AGE_GREATER:
957 case MATCHCRITERIA_AGE_LOWER:
958 case MATCHCRITERIA_NEWSGROUPS:
959 case MATCHCRITERIA_NOT_NEWSGROUPS:
960 case MATCHCRITERIA_INREPLYTO:
961 case MATCHCRITERIA_NOT_INREPLYTO:
962 case MATCHCRITERIA_REFERENCES:
963 case MATCHCRITERIA_NOT_REFERENCES:
964 case MATCHCRITERIA_SCORE_GREATER:
965 case MATCHCRITERIA_SCORE_LOWER:
966 case MATCHCRITERIA_SCORE_EQUAL:
967 case MATCHCRITERIA_SIZE_GREATER:
968 case MATCHCRITERIA_SIZE_SMALLER:
969 case MATCHCRITERIA_SIZE_EQUAL:
970 case MATCHCRITERIA_TEST:
971 case MATCHCRITERIA_NOT_TEST:
972 case MATCHCRITERIA_PARTIAL:
973 case MATCHCRITERIA_NOT_PARTIAL:
974 if (matcherprop_match(matcher, info)) {
975 if (!matchers->bool_and) {
980 if (matchers->bool_and) {
987 /* test the condition on the file */
989 if (matcherlist_match_file(matchers, info, result)) {
990 if (!matchers->bool_and)
994 if (matchers->bool_and)
1002 static gint quote_filter_str(gchar * result, guint size,
1012 for(p = path ; * p != '\0' ; p ++) {
1014 if ((* p != '\"') && (* p != '\\')) {
1015 if (remaining > 0) {
1021 result[size - 1] = '\0';
1026 if (remaining >= 2) {
1034 result[size - 1] = '\0';
1039 if (remaining > 0) {
1043 result[size - 1] = '\0';
1051 gchar * matcher_quote_str(const gchar * src)
1056 len = strlen(src) * 2 + 1;
1057 res = g_malloc(len);
1058 quote_filter_str(res, len, src);
1064 *\brief Convert a matcher structure to a string
1066 *\param matcher Matcher structure
1068 *\return gchar * Newly allocated string
1070 gchar *matcherprop_to_string(MatcherProp *matcher)
1072 gchar *matcher_str = NULL;
1073 const gchar *criteria_str;
1074 const gchar *matchtype_str;
1076 gchar * quoted_expr;
1078 criteria_str = NULL;
1079 for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
1080 if (matchparser_tab[i].id == matcher->criteria)
1081 criteria_str = matchparser_tab[i].str;
1083 if (criteria_str == NULL)
1086 switch (matcher->criteria) {
1087 case MATCHCRITERIA_AGE_GREATER:
1088 case MATCHCRITERIA_AGE_LOWER:
1089 case MATCHCRITERIA_SCORE_GREATER:
1090 case MATCHCRITERIA_SCORE_LOWER:
1091 case MATCHCRITERIA_SCORE_EQUAL:
1092 case MATCHCRITERIA_SIZE_GREATER:
1093 case MATCHCRITERIA_SIZE_SMALLER:
1094 case MATCHCRITERIA_SIZE_EQUAL:
1095 case MATCHCRITERIA_COLORLABEL:
1096 case MATCHCRITERIA_NOT_COLORLABEL:
1097 return g_strdup_printf("%s %i", criteria_str, matcher->value);
1098 case MATCHCRITERIA_ALL:
1099 case MATCHCRITERIA_UNREAD:
1100 case MATCHCRITERIA_NOT_UNREAD:
1101 case MATCHCRITERIA_NEW:
1102 case MATCHCRITERIA_NOT_NEW:
1103 case MATCHCRITERIA_MARKED:
1104 case MATCHCRITERIA_NOT_MARKED:
1105 case MATCHCRITERIA_DELETED:
1106 case MATCHCRITERIA_NOT_DELETED:
1107 case MATCHCRITERIA_REPLIED:
1108 case MATCHCRITERIA_NOT_REPLIED:
1109 case MATCHCRITERIA_FORWARDED:
1110 case MATCHCRITERIA_NOT_FORWARDED:
1111 case MATCHCRITERIA_LOCKED:
1112 case MATCHCRITERIA_NOT_LOCKED:
1113 case MATCHCRITERIA_PARTIAL:
1114 case MATCHCRITERIA_NOT_PARTIAL:
1115 case MATCHCRITERIA_IGNORE_THREAD:
1116 case MATCHCRITERIA_NOT_IGNORE_THREAD:
1117 return g_strdup(criteria_str);
1118 case MATCHCRITERIA_TEST:
1119 case MATCHCRITERIA_NOT_TEST:
1120 quoted_expr = matcher_quote_str(matcher->expr);
1121 matcher_str = g_strdup_printf("%s \"%s\"",
1122 criteria_str, quoted_expr);
1123 g_free(quoted_expr);
1127 matchtype_str = NULL;
1128 for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
1129 if (matchparser_tab[i].id == matcher->matchtype)
1130 matchtype_str = matchparser_tab[i].str;
1133 if (matchtype_str == NULL)
1136 switch (matcher->matchtype) {
1137 case MATCHTYPE_MATCH:
1138 case MATCHTYPE_MATCHCASE:
1139 case MATCHTYPE_REGEXP:
1140 case MATCHTYPE_REGEXPCASE:
1141 case MATCHTYPE_ALL_IN_ADDRESSBOOK:
1142 case MATCHTYPE_ANY_IN_ADDRESSBOOK:
1143 quoted_expr = matcher_quote_str(matcher->expr);
1144 if (matcher->header) {
1145 gchar * quoted_header;
1147 quoted_header = matcher_quote_str(matcher->header);
1148 matcher_str = g_strdup_printf
1149 ("%s \"%s\" %s \"%s\"",
1150 criteria_str, quoted_header,
1151 matchtype_str, quoted_expr);
1152 g_free(quoted_header);
1155 matcher_str = g_strdup_printf
1156 ("%s %s \"%s\"", criteria_str,
1157 matchtype_str, quoted_expr);
1158 g_free(quoted_expr);
1166 *\brief Convert a list of conditions to a string
1168 *\param matchers List of conditions
1170 *\return gchar * Newly allocated string
1172 gchar *matcherlist_to_string(const MatcherList *matchers)
1178 gchar *result = NULL;
1180 count = g_slist_length(matchers->matchers);
1181 vstr = g_new(gchar *, count + 1);
1183 for (l = matchers->matchers, cur_str = vstr; l != NULL;
1184 l = g_slist_next(l), cur_str ++) {
1185 *cur_str = matcherprop_to_string((MatcherProp *) l->data);
1186 if (*cur_str == NULL)
1191 if (matchers->bool_and)
1192 result = g_strjoinv(" & ", vstr);
1194 result = g_strjoinv(" | ", vstr);
1196 for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
1204 #define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
1205 #define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
1207 static void add_str_default(gchar ** dest,
1208 const gchar * s, const gchar * d)
1210 gchar quoted_str[4096];
1218 quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
1219 strcpy(* dest, quoted_str);
1221 (* dest) += strlen(* dest);
1224 /* matching_build_command() - preferably cmd should be unescaped */
1226 *\brief Build the command line to execute
1228 *\param cmd String with command line specifiers
1229 *\param info Message info to use for command
1231 *\return gchar * Newly allocated string
1233 gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
1235 const gchar *s = cmd;
1236 gchar *filename = NULL;
1237 gchar *processed_cmd;
1241 const gchar *const no_subject = _("(none)") ;
1242 const gchar *const no_from = _("(none)") ;
1243 const gchar *const no_to = _("(none)") ;
1244 const gchar *const no_cc = _("(none)") ;
1245 const gchar *const no_date = _("(none)") ;
1246 const gchar *const no_msgid = _("(none)") ;
1247 const gchar *const no_newsgroups = _("(none)") ;
1248 const gchar *const no_references = _("(none)") ;
1250 size = STRLEN_ZERO(cmd) + 1;
1251 while (*s != '\0') {
1258 case 's': /* subject */
1259 size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
1261 case 'f': /* from */
1262 size += STRLEN_DEFAULT(info->from, no_from) - 2;
1265 size += STRLEN_DEFAULT(info->to, no_to) - 2;
1268 size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
1270 case 'd': /* date */
1271 size += STRLEN_DEFAULT(info->date, no_date) - 2;
1273 case 'i': /* message-id */
1274 size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
1276 case 'n': /* newsgroups */
1277 size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
1279 case 'r': /* references */
1280 /* FIXME: using the inreplyto header for reference */
1281 size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
1283 case 'F': /* file */
1284 if (filename == NULL)
1285 filename = folder_item_fetch_msg(info->folder, info->msgnum);
1287 if (filename == NULL) {
1288 g_warning("filename is not set");
1292 size += strlen(filename) - 2;
1301 /* as the string can be quoted, we double the result */
1304 processed_cmd = g_new0(gchar, size);
1308 while (*s != '\0') {
1316 case 's': /* subject */
1317 add_str_default(&p, info->subject,
1320 case 'f': /* from */
1321 add_str_default(&p, info->from,
1325 add_str_default(&p, info->to,
1329 add_str_default(&p, info->cc,
1332 case 'd': /* date */
1333 add_str_default(&p, info->date,
1336 case 'i': /* message-id */
1337 add_str_default(&p, info->msgid,
1340 case 'n': /* newsgroups */
1341 add_str_default(&p, info->newsgroups,
1344 case 'r': /* references */
1345 /* FIXME: using the inreplyto header for references */
1346 add_str_default(&p, info->inreplyto, no_references);
1348 case 'F': /* file */
1349 if (filename != NULL)
1350 add_str_default(&p, filename, NULL);
1369 return processed_cmd;
1371 #undef STRLEN_DEFAULT
1374 /* ************************************************************ */
1378 *\brief Write filtering list to file
1381 *\param prefs_filtering List of filtering conditions
1383 static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
1387 for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
1388 gchar *filtering_str;
1389 gchar *tmp_name = NULL;
1390 FilteringProp *prop;
1392 if (NULL == (prop = (FilteringProp *) cur->data))
1395 if (NULL == (filtering_str = filteringprop_to_string(prop)))
1398 if (fputs("rulename \"", fp) == EOF) {
1399 FILE_OP_ERROR("filtering config", "fputs || fputc");
1400 g_free(filtering_str);
1403 tmp_name = prop->name;
1404 while (tmp_name && *tmp_name != '\0') {
1405 if (*tmp_name != '"') {
1406 if (fputc(*tmp_name, fp) == EOF) {
1407 FILE_OP_ERROR("filtering config", "fputc");
1408 g_free(filtering_str);
1411 } else if (*tmp_name == '"') {
1412 if (fputc('\\', fp) == EOF ||
1413 fputc('"', fp) == EOF) {
1414 FILE_OP_ERROR("filtering config", "fputc");
1415 g_free(filtering_str);
1421 if(fputs("\" ", fp) == EOF ||
1422 fputs(filtering_str, fp) == EOF ||
1423 fputc('\n', fp) == EOF) {
1424 FILE_OP_ERROR("filtering config", "fputs || fputc");
1425 g_free(filtering_str);
1428 g_free(filtering_str);
1433 *\brief Write matchers from a folder item
1435 *\param node Node with folder info
1436 *\param data File pointer
1438 *\return gboolean FALSE
1440 static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
1445 GSList *prefs_filtering;
1448 /* prevent warning */
1449 if (item->path == NULL)
1451 id = folder_item_get_identifier(item);
1454 prefs_filtering = item->prefs->processing;
1456 if (prefs_filtering != NULL) {
1457 fprintf(fp, "[%s]\n", id);
1458 prefs_filtering_write(fp, prefs_filtering);
1468 *\brief Save matchers from folder items
1472 static void prefs_matcher_save(FILE *fp)
1476 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
1479 folder = (Folder *) cur->data;
1480 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1481 prefs_matcher_write_func, fp);
1484 /* pre global rules */
1485 fprintf(fp, "[preglobal]\n");
1486 prefs_filtering_write(fp, pre_global_processing);
1489 /* post global rules */
1490 fprintf(fp, "[postglobal]\n");
1491 prefs_filtering_write(fp, post_global_processing);
1494 /* filtering rules */
1495 fprintf(fp, "[filtering]\n");
1496 prefs_filtering_write(fp, filtering_rules);
1501 *\brief Write filtering / matcher configuration file
1503 void prefs_matcher_write_config(void)
1508 debug_print("Writing matcher configuration...\n");
1510 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1513 if ((pfile = prefs_write_open(rcpath)) == NULL) {
1514 g_warning("failed to write configuration to file\n");
1520 prefs_matcher_save(pfile->fp);
1524 if (prefs_file_close(pfile) < 0) {
1525 g_warning("failed to write configuration to file\n");
1530 /* ******************************************************************* */
1532 void matcher_add_rulenames(const gchar *rcpath)
1534 gchar *newpath = g_strconcat(rcpath, ".new", NULL);
1535 FILE *src = g_fopen(rcpath, "rb");
1536 FILE *dst = g_fopen(newpath, "wb");
1537 gchar buf[BUFFSIZE];
1545 while (fgets (buf, sizeof(buf), src) != NULL) {
1546 if (strlen(buf) > 2 && buf[0] != '['
1547 && strncmp(buf, "rulename \"", 10)) {
1548 fwrite("rulename \"\" ",
1549 strlen("rulename \"\" "), 1, dst);
1551 fwrite(buf, strlen(buf), 1, dst);
1555 move_file(newpath, rcpath, TRUE);
1560 *\brief Read matcher configuration
1562 void prefs_matcher_read_config(void)
1565 gchar *rc_old_format;
1568 create_matchparser_hashtab();
1569 prefs_filtering_clear();
1571 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
1572 rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
1573 ".pre_names", NULL);
1575 if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
1576 /* backup file with no rules names, in case
1577 * anything goes wrong */
1578 copy_file(rcpath, rc_old_format, FALSE);
1579 /* now hack the file in order to have it to the new format */
1580 matcher_add_rulenames(rcpath);
1583 g_free(rc_old_format);
1585 f = g_fopen(rcpath, "rb");
1589 matcher_parser_start_parsing(f);
1590 fclose(matcher_parserin);
1593 /* previous version compatibily */
1595 /* printf("reading filtering\n"); */
1596 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1597 FILTERING_RC, NULL);
1598 f = g_fopen(rcpath, "rb");
1602 matcher_parser_start_parsing(f);
1603 fclose(matcher_parserin);
1606 /* printf("reading scoring\n"); */
1607 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1609 f = g_fopen(rcpath, "rb");
1613 matcher_parser_start_parsing(f);
1614 fclose(matcher_parserin);