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