updated and improved outlook2sylpheed.pl
[claws.git] / src / scoring.c
1 #include <ctype.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include "defs.h"
7 #include "intl.h"
8 #include "utils.h"
9 #include "scoring.h"
10
11 #define PREFSBUFSIZE            1024
12
13
14 GSList * global_scoring = NULL;
15
16 ScoringProp * scoringprop_new(MatcherList * matchers, int score)
17 {
18         ScoringProp * scoring;
19
20         scoring = g_new0(ScoringProp, 1);
21         scoring->matchers = matchers;
22         scoring->score = score;
23
24         return scoring;
25 }
26
27 ScoringProp * scoringprop_copy(ScoringProp *src)
28 {
29         ScoringProp * new;
30         GSList *tmp;
31         
32         new = g_new0(ScoringProp, 1);
33         new->matchers = g_new0(MatcherList, 1);
34         for (tmp = src->matchers->matchers; tmp != NULL && tmp->data != NULL;) {
35                 MatcherProp *matcher = (MatcherProp *)tmp->data;
36                 
37                 new->matchers->matchers = g_slist_append(new->matchers->matchers,
38                                                    matcherprop_copy(matcher));
39                 tmp = tmp->next;
40         }
41         new->matchers->bool_and = src->matchers->bool_and;
42         new->score = src->score;
43
44         return new;
45 }
46
47 void scoringprop_free(ScoringProp * prop)
48 {
49         matcherlist_free(prop->matchers);
50         g_free(prop);
51 }
52
53 gint scoringprop_score_message(ScoringProp * scoring, MsgInfo * info)
54 {
55         if (matcherlist_match(scoring->matchers, info))
56                 return scoring->score;
57         else
58                 return 0;
59 }
60
61 gint score_message(GSList * scoring_list, MsgInfo * info)
62 {
63         gint score = 0;
64         gint add_score;
65         GSList * l;
66
67         for(l = scoring_list ; l != NULL ; l = g_slist_next(l)) {
68                 ScoringProp * scoring = (ScoringProp *) l->data;
69                 
70                 add_score = (scoringprop_score_message(scoring, info));
71                 if (add_score == MAX_SCORE || add_score == MIN_SCORE) {
72                         score = add_score;
73                         break;
74                 }
75                 score += add_score;
76         }
77         return score;
78 }
79
80 /*
81   syntax for scoring config
82
83   file ~/.sylpheed/scoringrc
84
85   header "x-mailing" match "toto" score -10
86   subject match "regexp" & to regexp "regexp" score 50
87   subject match "regexp" | to regexpcase "regexp" | age_sup 5 score 30
88
89   if score is = MIN_SCORE (-999), no more match is done in the list
90   if score is = MAX_SCORE (-999), no more match is done in the list
91  */
92
93
94 gchar * scoringprop_to_string(ScoringProp * prop)
95 {
96         gchar * list_str;
97         gchar * score_str;
98         gchar * scoring_str;
99
100         list_str = matcherlist_to_string(prop->matchers);
101
102         if (list_str == NULL)
103                 return NULL;
104
105         score_str = itos(prop->score);
106         scoring_str = g_strconcat(list_str, " score ", score_str, NULL);
107         g_free(list_str);
108
109         return scoring_str;
110 }
111
112 void prefs_scoring_free(GSList * prefs_scoring)
113 {
114         while (prefs_scoring != NULL) {
115                 ScoringProp * scoring = (ScoringProp *) prefs_scoring->data;
116                 scoringprop_free(scoring);
117                 prefs_scoring = g_slist_remove(prefs_scoring, scoring);
118         }
119 }
120
121 static gboolean prefs_scoring_free_func(GNode *node, gpointer data)
122 {
123         FolderItem *item = node->data;
124
125         if(!item->prefs)
126                 return FALSE;
127
128         prefs_scoring_free(item->prefs->scoring);
129         item->prefs->scoring = NULL;
130
131         return FALSE;
132 }
133
134 void prefs_scoring_clear()
135 {
136         GList * cur;
137
138         for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
139                 Folder *folder;
140
141                 folder = (Folder *) cur->data;
142                 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
143                                 prefs_scoring_free_func, NULL);
144         }
145
146         prefs_scoring_free(global_scoring);
147         global_scoring = NULL;
148 }