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