2006-07-04 [colin] 2.3.1cvs55
[claws.git] / src / matcher_parser_parse.y
1
2 %{
3
4 /*
5  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
6  * Copyright (c) 2001-2002 by Hiroyuki Yamamoto & The Sylpheed Claws Team.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #include "defs.h"
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27
28 #include "utils.h"
29 #include "filtering.h"
30 #include "matcher.h"
31 #include "matcher_parser.h"
32 #include "matcher_parser_lex.h"
33 #include "procmsg.h"
34
35 #define MAX_COLORLABELS (MSG_CLABEL_7 - MSG_CLABEL_NONE)
36
37 static gint error = 0;
38 static gint bool_op = 0;
39 static gint match_type = 0;
40 static gchar *header = NULL;
41
42 static MatcherProp *prop;
43
44 static GSList *matchers_list = NULL;
45
46 static gboolean enabled = TRUE;
47 static gchar *name = NULL;
48 static MatcherList *cond;
49 static GSList *action_list = NULL;
50 static FilteringAction *action = NULL;
51
52 static FilteringProp *filtering;
53
54 static GSList **prefs_filtering = NULL;
55 static int enable_compatibility = 0;
56
57 enum {
58         MATCHER_PARSE_FILE,
59         MATCHER_PARSE_NO_EOL,
60         MATCHER_PARSE_ENABLED,
61         MATCHER_PARSE_NAME,
62         MATCHER_PARSE_CONDITION,
63         MATCHER_PARSE_FILTERING_ACTION,
64 };
65
66 static int matcher_parse_op = MATCHER_PARSE_FILE;
67
68
69 /* ******************************************************************** */
70 /* redeclarations to avoid warnings */
71 void matcher_parserrestart(FILE *input_file);
72 void matcher_parser_init(void);
73 void matcher_parser_switch_to_buffer(void * new_buffer);
74 void matcher_parser_delete_buffer(void * b);
75 int matcher_parserlex(void);
76
77 void matcher_parser_start_parsing(FILE *f)
78 {
79         matcher_parserrestart(f);
80         matcher_parserparse();
81 }
82
83  
84 void * matcher_parser_scan_string(const char * str);
85  
86 FilteringProp *matcher_parser_get_filtering(gchar *str)
87 {
88         void *bufstate;
89         void *tmp_str = NULL;
90         
91         /* little hack to allow passing rules with no names */
92         if (!strncmp(str, "rulename ", 9))
93                 tmp_str = g_strdup(str);
94         else 
95                 tmp_str = g_strconcat("rulename \"\" ", str, NULL);
96
97         /* bad coding to enable the sub-grammar matching
98            in yacc */
99         matcher_parserlineno = 1;
100         matcher_parse_op = MATCHER_PARSE_NO_EOL;
101         matcher_parserrestart(NULL);
102         matcher_parser_init();
103         bufstate = matcher_parser_scan_string((const char *) tmp_str);
104         matcher_parser_switch_to_buffer(bufstate);
105         if (matcher_parserparse() != 0)
106                 filtering = NULL;
107         matcher_parse_op = MATCHER_PARSE_FILE;
108         matcher_parser_delete_buffer(bufstate);
109         g_free(tmp_str);
110         return filtering;
111 }
112
113 static gboolean check_quote_symetry(gchar *str)
114 {
115         const gchar *walk;
116         int ret = 0;
117         
118         if (str == NULL)
119                 return TRUE; /* heh, that's symetric */
120         if (*str == '\0')
121                 return TRUE;
122         for (walk = str; *walk; walk++) {
123                 if (*walk == '\"') {
124                         if (walk == str         /* first char */
125                         || *(walk - 1) != '\\') /* not escaped */
126                                 ret ++;
127                 }
128         }
129         return !(ret % 2);
130 }
131
132 MatcherList *matcher_parser_get_name(gchar *str)
133 {
134         void *bufstate;
135
136         if (!check_quote_symetry(str)) {
137                 cond = NULL;
138                 return cond;
139         }
140         
141         /* bad coding to enable the sub-grammar matching
142            in yacc */
143         matcher_parserlineno = 1;
144         matcher_parse_op = MATCHER_PARSE_NAME;
145         matcher_parserrestart(NULL);
146         matcher_parser_init();
147         bufstate = matcher_parser_scan_string(str);
148         matcher_parserparse();
149         matcher_parse_op = MATCHER_PARSE_FILE;
150         matcher_parser_delete_buffer(bufstate);
151         return cond;
152 }
153
154 MatcherList *matcher_parser_get_cond(gchar *str)
155 {
156         void *bufstate;
157
158         if (!check_quote_symetry(str)) {
159                 cond = NULL;
160                 return cond;
161         }
162         
163         /* bad coding to enable the sub-grammar matching
164            in yacc */
165         matcher_parserlineno = 1;
166         matcher_parse_op = MATCHER_PARSE_CONDITION;
167         matcher_parserrestart(NULL);
168         matcher_parser_init();
169         bufstate = matcher_parser_scan_string(str);
170         matcher_parserparse();
171         matcher_parse_op = MATCHER_PARSE_FILE;
172         matcher_parser_delete_buffer(bufstate);
173         return cond;
174 }
175
176 GSList *matcher_parser_get_action_list(gchar *str)
177 {
178         void *bufstate;
179
180         if (!check_quote_symetry(str)) {
181                 action_list = NULL;
182                 return action_list;
183         }
184         
185         /* bad coding to enable the sub-grammar matching
186            in yacc */
187         matcher_parserlineno = 1;
188         matcher_parse_op = MATCHER_PARSE_FILTERING_ACTION;
189         matcher_parserrestart(NULL);
190         matcher_parser_init();
191         bufstate = matcher_parser_scan_string(str);
192         matcher_parserparse();
193         matcher_parse_op = MATCHER_PARSE_FILE;
194         matcher_parser_delete_buffer(bufstate);
195         return action_list;
196 }
197
198 MatcherProp *matcher_parser_get_prop(gchar *str)
199 {
200         MatcherList *list;
201         MatcherProp *prop;
202
203         matcher_parserlineno = 1;
204         list = matcher_parser_get_cond(str);
205         if (list == NULL)
206                 return NULL;
207
208         if (list->matchers == NULL)
209                 return NULL;
210
211         if (list->matchers->next != NULL)
212                 return NULL;
213
214         prop = list->matchers->data;
215
216         g_slist_free(list->matchers);
217         g_free(list);
218
219         return prop;
220 }
221
222 void matcher_parsererror(char *str)
223 {
224         GSList *l;
225
226         if (matchers_list) {
227                 for (l = matchers_list; l != NULL; l = g_slist_next(l)) {
228                         matcherprop_free((MatcherProp *)
229                                          l->data);
230                         l->data = NULL;
231                 }
232                 g_slist_free(matchers_list);
233                 matchers_list = NULL;
234         }
235         cond = NULL;
236         g_warning("filtering parsing: %i: %s\n",
237                   matcher_parserlineno, str);
238         error = 1;
239 }
240
241 int matcher_parserwrap(void)
242 {
243         return 1;
244 }
245 %}
246
247 %union {
248         char *str;
249         int value;
250 }
251 %token MATCHER_ALL MATCHER_UNREAD  MATCHER_NOT_UNREAD 
252 %token MATCHER_NEW  MATCHER_NOT_NEW  MATCHER_MARKED
253 %token MATCHER_NOT_MARKED  MATCHER_DELETED  MATCHER_NOT_DELETED
254 %token MATCHER_REPLIED  MATCHER_NOT_REPLIED  MATCHER_FORWARDED
255 %token MATCHER_NOT_FORWARDED  MATCHER_SUBJECT  MATCHER_NOT_SUBJECT
256 %token MATCHER_FROM  MATCHER_NOT_FROM  MATCHER_TO  MATCHER_NOT_TO
257 %token MATCHER_CC  MATCHER_NOT_CC  MATCHER_TO_OR_CC  MATCHER_NOT_TO_AND_NOT_CC
258 %token MATCHER_AGE_GREATER  MATCHER_AGE_LOWER  MATCHER_NEWSGROUPS
259 %token MATCHER_NOT_NEWSGROUPS  MATCHER_INREPLYTO  MATCHER_NOT_INREPLYTO
260 %token MATCHER_REFERENCES  MATCHER_NOT_REFERENCES  MATCHER_SCORE_GREATER
261 %token MATCHER_SCORE_LOWER  MATCHER_HEADER  MATCHER_NOT_HEADER
262 %token MATCHER_HEADERS_PART  MATCHER_NOT_HEADERS_PART  MATCHER_MESSAGE
263 %token MATCHER_NOT_MESSAGE  MATCHER_BODY_PART  MATCHER_NOT_BODY_PART
264 %token MATCHER_TEST  MATCHER_NOT_TEST  MATCHER_MATCHCASE  MATCHER_MATCH
265 %token MATCHER_REGEXPCASE  MATCHER_REGEXP  MATCHER_SCORE  MATCHER_MOVE
266 %token MATCHER_ANY_IN_ADDRESSBOOK MATCHER_ALL_IN_ADDRESSBOOK
267 %token MATCHER_COPY  MATCHER_DELETE  MATCHER_MARK  MATCHER_UNMARK
268 %token MATCHER_LOCK MATCHER_UNLOCK
269 %token MATCHER_EXECUTE
270 %token MATCHER_MARK_AS_READ  MATCHER_MARK_AS_UNREAD  MATCHER_FORWARD
271 %token MATCHER_FORWARD_AS_ATTACHMENT  MATCHER_EOL  MATCHER_STRING  
272 %token MATCHER_OR MATCHER_AND  
273 %token MATCHER_COLOR MATCHER_SCORE_EQUAL MATCHER_REDIRECT 
274 %token MATCHER_SIZE_GREATER MATCHER_SIZE_SMALLER MATCHER_SIZE_EQUAL
275 %token MATCHER_LOCKED MATCHER_NOT_LOCKED
276 %token MATCHER_PARTIAL MATCHER_NOT_PARTIAL
277 %token MATCHER_COLORLABEL MATCHER_NOT_COLORLABEL
278 %token MATCHER_IGNORE_THREAD MATCHER_NOT_IGNORE_THREAD
279 %token MATCHER_CHANGE_SCORE MATCHER_SET_SCORE
280 %token MATCHER_STOP MATCHER_HIDE MATCHER_IGNORE
281
282 %start file
283
284 %token MATCHER_ENABLED MATCHER_DISABLED
285 %token MATCHER_RULENAME
286 %token <str> MATCHER_STRING
287 %token <str> MATCHER_SECTION
288 %token <str> MATCHER_INTEGER
289
290 %%
291
292 file:
293 {
294         if (matcher_parse_op == MATCHER_PARSE_FILE) {
295                 prefs_filtering = &pre_global_processing;
296         }
297 }
298 file_line_list;
299
300 file_line_list:
301 file_line
302 file_line_list
303 | file_line
304 ;
305
306 file_line:
307 section_notification
308
309 { action_list = NULL; }
310 instruction
311 | error MATCHER_EOL
312 {
313         yyerrok;
314 };
315
316 section_notification:
317 MATCHER_SECTION MATCHER_EOL
318 {
319         gchar *folder = $1;
320         FolderItem *item = NULL;
321
322         if (matcher_parse_op == MATCHER_PARSE_FILE) {
323                 enable_compatibility = 0;
324                 if (!strcmp(folder, "global")) {
325                         /* backward compatibility */
326                         enable_compatibility = 1;
327                 }
328                 else if (!strcmp(folder, "preglobal")) {
329                         prefs_filtering = &pre_global_processing;
330                 }
331                 else if (!strcmp(folder, "postglobal")) {
332                         prefs_filtering = &post_global_processing;
333                 }
334                 else if (!strcmp(folder, "filtering")) {
335                         prefs_filtering = &filtering_rules;
336                 }
337                 else {
338                         item = folder_find_item_from_identifier(folder);
339                         if (item != NULL) {
340                                 prefs_filtering = &item->prefs->processing;
341                         } else {
342                                 prefs_filtering = NULL;
343                         }
344                 }
345         }
346 }
347 ;
348
349 instruction:
350 enabled name condition filtering MATCHER_EOL
351 | enabled name condition filtering
352 | name condition filtering MATCHER_EOL
353 | name condition filtering
354 {
355         if (matcher_parse_op == MATCHER_PARSE_NO_EOL)
356                 YYACCEPT;
357         else {
358                 matcher_parsererror("parse error a");
359                 YYERROR;
360         }
361 }
362 | enabled
363 {
364         if (matcher_parse_op == MATCHER_PARSE_ENABLED)
365                 YYACCEPT;
366         else {
367                 matcher_parsererror("parse error enabled");
368                 YYERROR;
369         }
370 }
371 | name
372 {
373         if (matcher_parse_op == MATCHER_PARSE_NAME)
374                 YYACCEPT;
375         else {
376                 matcher_parsererror("parse error b");
377                 YYERROR;
378         }
379 }
380 | condition
381 {
382         if (matcher_parse_op == MATCHER_PARSE_CONDITION)
383                 YYACCEPT;
384         else {
385                 matcher_parsererror("parse error c");
386                 YYERROR;
387         }
388 }
389 | filtering_action_list
390 {
391         if (matcher_parse_op == MATCHER_PARSE_FILTERING_ACTION)
392                 YYACCEPT;
393         else {
394                 matcher_parsererror("parse error d");
395                 YYERROR;
396         }
397 }
398 | MATCHER_EOL
399 ;
400
401 enabled:
402 MATCHER_ENABLED
403 {
404         enabled = TRUE;
405 }
406 | MATCHER_DISABLED
407 {
408         enabled = FALSE;
409 }
410 ;
411
412 name:
413 MATCHER_RULENAME MATCHER_STRING
414 {
415         name = g_strdup($2);
416 }
417 ;
418
419 filtering:
420 filtering_action_list
421 {
422         filtering = filteringprop_new(enabled, name, cond, action_list);
423         enabled = TRUE;
424         g_free(name);
425         name = NULL;
426         if (enable_compatibility) {
427                 prefs_filtering = &filtering_rules;
428                 if (action_list != NULL) {
429                         FilteringAction * first_action;
430                         
431                         first_action = action_list->data;
432                         
433                         if (first_action->type == MATCHACTION_CHANGE_SCORE)
434                                 prefs_filtering = &pre_global_processing;
435                 }
436         }
437         
438         cond = NULL;
439         action_list = NULL;
440         
441         if ((matcher_parse_op == MATCHER_PARSE_FILE) &&
442             (prefs_filtering != NULL)) {
443                 *prefs_filtering = g_slist_append(*prefs_filtering,
444                                                   filtering);
445                 filtering = NULL;
446         }
447 }
448 ;
449
450 filtering_action_list:
451 filtering_action_b filtering_action_list
452 | filtering_action_b
453 ;
454
455 filtering_action_b:
456 filtering_action
457 {
458         action_list = g_slist_append(action_list, action);
459         action = NULL;
460 }
461 ;
462
463 match_type:
464 MATCHER_MATCHCASE
465 {
466         match_type = MATCHTYPE_MATCHCASE;
467 }
468 | MATCHER_MATCH
469 {
470         match_type = MATCHTYPE_MATCH;
471 }
472 | MATCHER_REGEXPCASE
473 {
474         match_type = MATCHTYPE_REGEXPCASE;
475 }
476 | MATCHER_REGEXP
477 {
478         match_type = MATCHTYPE_REGEXP;
479 }
480 | MATCHER_ANY_IN_ADDRESSBOOK
481 {
482         match_type = MATCHTYPE_ANY_IN_ADDRESSBOOK;
483 }
484 | MATCHER_ALL_IN_ADDRESSBOOK
485 {
486         match_type = MATCHTYPE_ALL_IN_ADDRESSBOOK;
487 }
488 ;
489
490 condition:
491 condition_list
492 {
493         cond = matcherlist_new(matchers_list, (bool_op == MATCHERBOOL_AND));
494         matchers_list = NULL;
495 }
496 ;
497
498 condition_list:
499 condition_list bool_op one_condition
500 {
501         matchers_list = g_slist_append(matchers_list, prop);
502 }
503 | one_condition
504 {
505         matchers_list = NULL;
506         matchers_list = g_slist_append(matchers_list, prop);
507 }
508 ;
509
510 bool_op:
511 MATCHER_AND
512 {
513         bool_op = MATCHERBOOL_AND;
514 }
515 | MATCHER_OR
516 {
517         bool_op = MATCHERBOOL_OR;
518 }
519 ;
520
521 one_condition:
522 MATCHER_ALL
523 {
524         gint criteria = 0;
525
526         criteria = MATCHCRITERIA_ALL;
527         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
528 }
529 | MATCHER_UNREAD
530 {
531         gint criteria = 0;
532
533         criteria = MATCHCRITERIA_UNREAD;
534         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
535 }
536 | MATCHER_NOT_UNREAD 
537 {
538         gint criteria = 0;
539
540         criteria = MATCHCRITERIA_NOT_UNREAD;
541         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
542 }
543 | MATCHER_NEW
544 {
545         gint criteria = 0;
546
547         criteria = MATCHCRITERIA_NEW;
548         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
549 }
550 | MATCHER_NOT_NEW
551 {
552         gint criteria = 0;
553
554         criteria = MATCHCRITERIA_NOT_NEW;
555         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
556 }
557 | MATCHER_MARKED
558 {
559         gint criteria = 0;
560
561         criteria = MATCHCRITERIA_MARKED;
562         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
563 }
564 | MATCHER_NOT_MARKED
565 {
566         gint criteria = 0;
567
568         criteria = MATCHCRITERIA_NOT_MARKED;
569         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
570 }
571 | MATCHER_DELETED
572 {
573         gint criteria = 0;
574
575         criteria = MATCHCRITERIA_DELETED;
576         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
577 }
578 | MATCHER_NOT_DELETED
579 {
580         gint criteria = 0;
581
582         criteria = MATCHCRITERIA_NOT_DELETED;
583         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
584 }
585 | MATCHER_REPLIED
586 {
587         gint criteria = 0;
588
589         criteria = MATCHCRITERIA_REPLIED;
590         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
591 }
592 | MATCHER_NOT_REPLIED
593 {
594         gint criteria = 0;
595
596         criteria = MATCHCRITERIA_NOT_REPLIED;
597         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
598 }
599 | MATCHER_FORWARDED
600 {
601         gint criteria = 0;
602
603         criteria = MATCHCRITERIA_FORWARDED;
604         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
605 }
606 | MATCHER_NOT_FORWARDED
607 {
608         gint criteria = 0;
609
610         criteria = MATCHCRITERIA_NOT_FORWARDED;
611         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
612 }
613 | MATCHER_LOCKED
614 {
615         gint criteria = 0;
616
617         criteria = MATCHCRITERIA_LOCKED;
618         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
619 }
620 | MATCHER_NOT_LOCKED
621 {
622         gint criteria = 0;
623
624         criteria = MATCHCRITERIA_NOT_LOCKED;
625         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
626 }
627 | MATCHER_PARTIAL
628 {
629         gint criteria = 0;
630
631         criteria = MATCHCRITERIA_PARTIAL;
632         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
633 }
634 | MATCHER_NOT_PARTIAL
635 {
636         gint criteria = 0;
637
638         criteria = MATCHCRITERIA_NOT_PARTIAL;
639         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
640 }
641 | MATCHER_COLORLABEL MATCHER_INTEGER
642 {
643         gint criteria = 0;
644         gint value = 0;
645
646         criteria = MATCHCRITERIA_COLORLABEL;
647         value = strtol($2, NULL, 10);
648         if (value < 0) value = 0;
649         else if (value > MAX_COLORLABELS) value = MAX_COLORLABELS;
650         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
651 }
652 | MATCHER_NOT_COLORLABEL MATCHER_INTEGER
653 {
654         gint criteria = 0;
655         gint value = 0;
656
657         criteria = MATCHCRITERIA_NOT_COLORLABEL;
658         value = strtol($2, NULL, 0);
659         if (value < 0) value = 0;
660         else if (value > MAX_COLORLABELS) value = MAX_COLORLABELS;
661         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
662 }
663 | MATCHER_IGNORE_THREAD
664 {
665         gint criteria = 0;
666
667         criteria = MATCHCRITERIA_IGNORE_THREAD;
668         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
669 }
670 | MATCHER_NOT_IGNORE_THREAD
671 {
672         gint criteria = 0;
673
674         criteria = MATCHCRITERIA_NOT_IGNORE_THREAD;
675         prop = matcherprop_new(criteria, NULL, 0, NULL, 0);
676 }
677 | MATCHER_SUBJECT match_type MATCHER_STRING
678 {
679         gint criteria = 0;
680         gchar *expr = NULL;
681
682         criteria = MATCHCRITERIA_SUBJECT;
683         expr = $3;
684         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
685 }
686 | MATCHER_NOT_SUBJECT match_type MATCHER_STRING
687 {
688         gint criteria = 0;
689         gchar *expr = NULL;
690
691         criteria = MATCHCRITERIA_NOT_SUBJECT;
692         expr = $3;
693         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
694 }
695 | MATCHER_FROM match_type MATCHER_STRING
696 {
697         gint criteria = 0;
698         gchar *expr = NULL;
699
700         criteria = MATCHCRITERIA_FROM;
701         expr = $3;
702         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
703 }
704 | MATCHER_NOT_FROM match_type MATCHER_STRING
705 {
706         gint criteria = 0;
707         gchar *expr = NULL;
708
709         criteria = MATCHCRITERIA_NOT_FROM;
710         expr = $3;
711         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
712 }
713 | MATCHER_TO match_type MATCHER_STRING
714 {
715         gint criteria = 0;
716         gchar *expr = NULL;
717
718         criteria = MATCHCRITERIA_TO;
719         expr = $3;
720         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
721 }
722 | MATCHER_NOT_TO match_type MATCHER_STRING
723 {
724         gint criteria = 0;
725         gchar *expr = NULL;
726
727         criteria = MATCHCRITERIA_NOT_TO;
728         expr = $3;
729         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
730 }
731 | MATCHER_CC match_type MATCHER_STRING
732 {
733         gint criteria = 0;
734         gchar *expr = NULL;
735
736         criteria = MATCHCRITERIA_CC;
737         expr = $3;
738         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
739 }
740 | MATCHER_NOT_CC match_type MATCHER_STRING
741 {
742         gint criteria = 0;
743         gchar *expr = NULL;
744
745         criteria = MATCHCRITERIA_NOT_CC;
746         expr = $3;
747         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
748 }
749 | MATCHER_TO_OR_CC match_type MATCHER_STRING
750 {
751         gint criteria = 0;
752         gchar *expr = NULL;
753
754         criteria = MATCHCRITERIA_TO_OR_CC;
755         expr = $3;
756         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
757 }
758 | MATCHER_NOT_TO_AND_NOT_CC match_type MATCHER_STRING
759 {
760         gint criteria = 0;
761         gchar *expr = NULL;
762
763         criteria = MATCHCRITERIA_NOT_TO_AND_NOT_CC;
764         expr = $3;
765         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
766 }
767 | MATCHER_AGE_GREATER MATCHER_INTEGER
768 {
769         gint criteria = 0;
770         gint value = 0;
771
772         criteria = MATCHCRITERIA_AGE_GREATER;
773         value = strtol($2, NULL, 0);
774         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
775 }
776 | MATCHER_AGE_LOWER MATCHER_INTEGER
777 {
778         gint criteria = 0;
779         gint value = 0;
780
781         criteria = MATCHCRITERIA_AGE_LOWER;
782         value = strtol($2, NULL, 0);
783         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
784 }
785 | MATCHER_NEWSGROUPS match_type MATCHER_STRING
786 {
787         gint criteria = 0;
788         gchar *expr = NULL;
789
790         criteria = MATCHCRITERIA_NEWSGROUPS;
791         expr = $3;
792         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
793 }
794 | MATCHER_NOT_NEWSGROUPS match_type MATCHER_STRING
795 {
796         gint criteria = 0;
797         gchar *expr = NULL;
798
799         criteria = MATCHCRITERIA_NOT_NEWSGROUPS;
800         expr = $3;
801         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
802 }
803 | MATCHER_INREPLYTO match_type MATCHER_STRING
804 {
805         gint criteria = 0;
806         gchar *expr = NULL;
807
808         criteria = MATCHCRITERIA_INREPLYTO;
809         expr = $3;
810         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
811 }
812 | MATCHER_NOT_INREPLYTO match_type MATCHER_STRING
813 {
814         gint criteria = 0;
815         gchar *expr = NULL;
816
817         criteria = MATCHCRITERIA_NOT_INREPLYTO;
818         expr = $3;
819         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
820 }
821 | MATCHER_REFERENCES match_type MATCHER_STRING
822 {
823         gint criteria = 0;
824         gchar *expr = NULL;
825
826         criteria = MATCHCRITERIA_REFERENCES;
827         expr = $3;
828         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
829 }
830 | MATCHER_NOT_REFERENCES match_type MATCHER_STRING
831 {
832         gint criteria = 0;
833         gchar *expr = NULL;
834
835         criteria = MATCHCRITERIA_NOT_REFERENCES;
836         expr = $3;
837         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
838 }
839 | MATCHER_SCORE_GREATER MATCHER_INTEGER
840 {
841         gint criteria = 0;
842         gint value = 0;
843
844         criteria = MATCHCRITERIA_SCORE_GREATER;
845         value = strtol($2, NULL, 0);
846         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
847 }
848 | MATCHER_SCORE_LOWER MATCHER_INTEGER
849 {
850         gint criteria = 0;
851         gint value = 0;
852
853         criteria = MATCHCRITERIA_SCORE_LOWER;
854         value = strtol($2, NULL, 0);
855         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
856 }
857 | MATCHER_SCORE_EQUAL MATCHER_INTEGER
858 {
859         gint criteria = 0;
860         gint value = 0;
861
862         criteria = MATCHCRITERIA_SCORE_EQUAL;
863         value = strtol($2, NULL, 0);
864         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
865 }
866 | MATCHER_SIZE_GREATER MATCHER_INTEGER 
867 {
868         gint criteria = 0;
869         gint value    = 0;
870         criteria = MATCHCRITERIA_SIZE_GREATER;
871         value = strtol($2, NULL, 0);
872         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
873 }
874 | MATCHER_SIZE_SMALLER MATCHER_INTEGER
875 {
876         gint criteria = 0;
877         gint value    = 0;
878         criteria = MATCHCRITERIA_SIZE_SMALLER;
879         value = strtol($2, NULL, 0);
880         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
881 }
882 | MATCHER_SIZE_EQUAL MATCHER_INTEGER
883 {
884         gint criteria = 0;
885         gint value    = 0;
886         criteria = MATCHCRITERIA_SIZE_EQUAL;
887         value = strtol($2, NULL, 0);
888         prop = matcherprop_new(criteria, NULL, 0, NULL, value);
889 }
890 | MATCHER_HEADER MATCHER_STRING
891 {
892         header = g_strdup($2);
893 } match_type MATCHER_STRING
894 {
895         gint criteria = 0;
896         gchar *expr = NULL;
897
898         criteria = MATCHCRITERIA_HEADER;
899         expr = $2;
900         prop = matcherprop_new(criteria, header, match_type, expr, 0);
901         g_free(header);
902 }
903 | MATCHER_NOT_HEADER MATCHER_STRING
904 {
905         header = g_strdup($2);
906 } match_type MATCHER_STRING
907 {
908         gint criteria = 0;
909         gchar *expr = NULL;
910
911         criteria = MATCHCRITERIA_NOT_HEADER;
912         expr = $2;
913         prop = matcherprop_new(criteria, header, match_type, expr, 0);
914         g_free(header);
915 }
916 | MATCHER_HEADERS_PART match_type MATCHER_STRING
917 {
918         gint criteria = 0;
919         gchar *expr = NULL;
920
921         criteria = MATCHCRITERIA_HEADERS_PART;
922         expr = $3;
923         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
924 }
925 | MATCHER_NOT_HEADERS_PART match_type MATCHER_STRING
926 {
927         gint criteria = 0;
928         gchar *expr = NULL;
929
930         criteria = MATCHCRITERIA_NOT_HEADERS_PART;
931         expr = $3;
932         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
933 }
934 | MATCHER_MESSAGE match_type MATCHER_STRING
935 {
936         gint criteria = 0;
937         gchar *expr = NULL;
938
939         criteria = MATCHCRITERIA_MESSAGE;
940         expr = $3;
941         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
942 }
943 | MATCHER_NOT_MESSAGE match_type MATCHER_STRING
944 {
945         gint criteria = 0;
946         gchar *expr = NULL;
947
948         criteria = MATCHCRITERIA_NOT_MESSAGE;
949         expr = $3;
950         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
951 }
952 | MATCHER_BODY_PART match_type MATCHER_STRING
953 {
954         gint criteria = 0;
955         gchar *expr = NULL;
956
957         criteria = MATCHCRITERIA_BODY_PART;
958         expr = $3;
959         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
960 }
961 | MATCHER_NOT_BODY_PART match_type MATCHER_STRING
962 {
963         gint criteria = 0;
964         gchar *expr = NULL;
965
966         criteria = MATCHCRITERIA_NOT_BODY_PART;
967         expr = $3;
968         prop = matcherprop_new(criteria, NULL, match_type, expr, 0);
969 }
970 | MATCHER_TEST MATCHER_STRING
971 {
972         gint criteria = 0;
973         gchar *expr = NULL;
974
975         criteria = MATCHCRITERIA_TEST;
976         expr = $2;
977         prop = matcherprop_new(criteria, NULL, 0, expr, 0);
978 }
979 | MATCHER_NOT_TEST MATCHER_STRING
980 {
981         gint criteria = 0;
982         gchar *expr = NULL;
983
984         criteria = MATCHCRITERIA_NOT_TEST;
985         expr = $2;
986         prop = matcherprop_new(criteria, NULL, 0, expr, 0);
987 }
988 ;
989
990 filtering_action:
991 MATCHER_EXECUTE MATCHER_STRING
992 {
993         gchar *cmd = NULL;
994         gint action_type = 0;
995
996         action_type = MATCHACTION_EXECUTE;
997         cmd = $2;
998         action = filteringaction_new(action_type, 0, cmd, 0, 0);
999 }
1000 | MATCHER_MOVE MATCHER_STRING
1001 {
1002         gchar *destination = NULL;
1003         gint action_type = 0;
1004
1005         action_type = MATCHACTION_MOVE;
1006         destination = $2;
1007         action = filteringaction_new(action_type, 0, destination, 0, 0);
1008 }
1009 | MATCHER_COPY MATCHER_STRING
1010 {
1011         gchar *destination = NULL;
1012         gint action_type = 0;
1013
1014         action_type = MATCHACTION_COPY;
1015         destination = $2;
1016         action = filteringaction_new(action_type, 0, destination, 0, 0);
1017 }
1018 | MATCHER_DELETE
1019 {
1020         gint action_type = 0;
1021
1022         action_type = MATCHACTION_DELETE;
1023         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1024 }
1025 | MATCHER_MARK
1026 {
1027         gint action_type = 0;
1028
1029         action_type = MATCHACTION_MARK;
1030         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1031 }
1032 | MATCHER_UNMARK
1033 {
1034         gint action_type = 0;
1035
1036         action_type = MATCHACTION_UNMARK;
1037         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1038 }
1039 | MATCHER_LOCK
1040 {
1041         gint action_type = 0;
1042
1043         action_type = MATCHACTION_LOCK;
1044         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1045 }
1046 | MATCHER_UNLOCK
1047 {
1048         gint action_type = 0;
1049
1050         action_type = MATCHACTION_UNLOCK;
1051         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1052 }
1053 | MATCHER_MARK_AS_READ
1054 {
1055         gint action_type = 0;
1056
1057         action_type = MATCHACTION_MARK_AS_READ;
1058         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1059 }
1060 | MATCHER_MARK_AS_UNREAD
1061 {
1062         gint action_type = 0;
1063
1064         action_type = MATCHACTION_MARK_AS_UNREAD;
1065         action = filteringaction_new(action_type, 0, NULL, 0, 0);
1066 }
1067 | MATCHER_FORWARD MATCHER_INTEGER MATCHER_STRING
1068 {
1069         gchar *destination = NULL;
1070         gint action_type = 0;
1071         gint account_id = 0;
1072
1073         action_type = MATCHACTION_FORWARD;
1074         account_id = strtol($2, NULL, 10);
1075         destination = $3;
1076         action = filteringaction_new(action_type,
1077             account_id, destination, 0, 0);
1078 }
1079 | MATCHER_FORWARD_AS_ATTACHMENT MATCHER_INTEGER MATCHER_STRING
1080 {
1081         gchar *destination = NULL;
1082         gint action_type = 0;
1083         gint account_id = 0;
1084
1085         action_type = MATCHACTION_FORWARD_AS_ATTACHMENT;
1086         account_id = strtol($2, NULL, 10);
1087         destination = $3;
1088         action = filteringaction_new(action_type,
1089             account_id, destination, 0, 0);
1090 }
1091 | MATCHER_REDIRECT MATCHER_INTEGER MATCHER_STRING
1092 {
1093         gchar *destination = NULL;
1094         gint action_type = 0;
1095         gint account_id = 0;
1096
1097         action_type = MATCHACTION_REDIRECT;
1098         account_id = strtol($2, NULL, 10);
1099         destination = $3;
1100         action = filteringaction_new(action_type,
1101             account_id, destination, 0, 0);
1102 }
1103 | MATCHER_COLOR MATCHER_INTEGER
1104 {
1105         gint action_type = 0;
1106         gint color = 0;
1107
1108         action_type = MATCHACTION_COLOR;
1109         color = strtol($2, NULL, 10);
1110         action = filteringaction_new(action_type, 0, NULL, color, 0);
1111 }
1112 | MATCHER_CHANGE_SCORE MATCHER_INTEGER
1113 {
1114         gint score = 0;
1115         
1116         score = strtol($2, NULL, 10);
1117         action = filteringaction_new(MATCHACTION_CHANGE_SCORE, 0,
1118                                      NULL, 0, score);
1119 }
1120 /* backward compatibility */
1121 | MATCHER_SCORE MATCHER_INTEGER
1122 {
1123         gint score = 0;
1124         
1125         score = strtol($2, NULL, 10);
1126         action = filteringaction_new(MATCHACTION_CHANGE_SCORE, 0,
1127                                      NULL, 0, score);
1128 }
1129 | MATCHER_SET_SCORE MATCHER_INTEGER
1130 {
1131         gint score = 0;
1132         
1133         score = strtol($2, NULL, 10);
1134         action = filteringaction_new(MATCHACTION_SET_SCORE, 0,
1135                                      NULL, 0, score);
1136 }
1137 | MATCHER_HIDE
1138 {
1139         action = filteringaction_new(MATCHACTION_HIDE, 0, NULL, 0, 0);
1140 }
1141 | MATCHER_IGNORE
1142 {
1143         action = filteringaction_new(MATCHACTION_IGNORE, 0, NULL, 0, 0);
1144 }
1145 | MATCHER_STOP
1146 {
1147         action = filteringaction_new(MATCHACTION_STOP, 0, NULL, 0, 0);
1148 }
1149 ;