9 #include "procheader.h"
15 #define PREFSBUFSIZE 1024
18 GSList * global_scoring;
20 ScoringProp * scoringprop_parse(gchar ** str)
24 ScoringProp * scoring;
26 MatcherList * matchers;
30 matchers = matcherlist_parse(&tmp);
36 key = matcher_parse_keyword(&tmp);
39 matcherlist_free(matchers);
44 if (key != MATCHING_SCORE) {
45 matcherlist_free(matchers);
50 score = matcher_parse_number(&tmp);
53 matcherlist_free(matchers);
58 scoring = scoringprop_new(matchers, score);
65 ScoringProp * scoringprop_new(MatcherList * matchers, int score)
67 ScoringProp * scoring;
69 scoring = g_new0(ScoringProp, 1);
70 scoring->matchers = matchers;
71 scoring->score = score;
76 void scoringprop_free(ScoringProp * prop)
78 matcherlist_free(prop->matchers);
82 gint scoringprop_score_message(ScoringProp * scoring, MsgInfo * info)
84 if (matcherlist_match(scoring->matchers, info))
85 return scoring->score;
90 gint score_message(GSList * scoring_list, MsgInfo * info)
96 for(l = scoring_list ; l != NULL ; l = g_slist_next(l)) {
97 ScoringProp * scoring = (ScoringProp *) l->data;
99 add_score = (scoringprop_score_message(scoring, info));
100 if (add_score == MAX_SCORE || add_score == MIN_SCORE) {
110 static void scoringprop_print(ScoringProp * prop)
115 printf("no scoring\n");
119 printf("----- scoring ------\n");
120 for(l = prop->matchers ; l != NULL ; l = g_slist_next(l)) {
121 matcherprop_print((MatcherProp *) l->data);
123 printf("cond: %s\n", prop->bool_and ? "and" : "or");
124 printf("score: %i\n", prop->score);
129 syntax for scoring config
131 file ~/.sylpheed/scoringrc
133 header "x-mailing" match "toto" score -10
134 subject match "regexp" & to regexp "regexp" score 50
135 subject match "regexp" | to regexpcase "regexp" | age_sup 5 score 30
137 if score is = MIN_SCORE (-999), no more match is done in the list
138 if score is = MAX_SCORE (-999), no more match is done in the list
142 void prefs_scoring_read_config(void)
146 gchar buf[PREFSBUFSIZE];
148 debug_print(_("Reading headers configuration...\n"));
150 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
151 if ((fp = fopen(rcpath, "r")) == NULL) {
152 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
154 prefs_scoring = NULL;
159 while (prefs_scoring != NULL) {
160 ScoringProp * scoring = (ScoringProp *) prefs_scoring->data;
161 scoringprop_free(scoring);
162 prefs_scoring = g_slist_remove(prefs_scoring, scoring);
165 while (fgets(buf, sizeof(buf), fp) != NULL) {
166 ScoringProp * scoring;
171 if ((*buf != '#') && (*buf != '\0')) {
173 scoring = scoringprop_parse(&tmp);
175 prefs_scoring = g_slist_append(prefs_scoring,
179 g_warning(_("syntax error : %s\n"), buf);
188 gchar * scoringprop_to_string(ScoringProp * prop)
194 list_str = matcherlist_to_string(prop->matchers);
196 if (list_str == NULL)
199 score_str = itos(prop->score);
200 scoring_str = g_strconcat(list_str, " score ", score_str, NULL);
207 void prefs_scoring_write_config(void)
214 debug_print(_("Writing scoring configuration...\n"));
216 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
218 if ((pfile = prefs_write_open(rcpath)) == NULL) {
219 g_warning(_("failed to write configuration to file\n"));
224 for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
227 prop = (ScoringProp *) cur->data;
228 scoring_str = scoringprop_to_string(prop);
229 if (fputs(scoring_str, pfile->fp) == EOF ||
230 fputc('\n', pfile->fp) == EOF) {
231 FILE_OP_ERROR(rcpath, "fputs || fputc");
232 prefs_write_close_revert(pfile);
242 if (prefs_write_close(pfile) < 0) {
243 g_warning(_("failed to write configuration to file\n"));
249 void prefs_scoring_free(GSList * prefs_scoring)
251 while (prefs_scoring != NULL) {
252 ScoringProp * scoring = (ScoringProp *) prefs_scoring->data;
253 scoringprop_free(scoring);
254 prefs_scoring = g_slist_remove(prefs_scoring, scoring);
258 static gboolean prefs_scoring_free_func(GNode *node, gpointer data)
260 FolderItem *item = node->data;
265 prefs_scoring_free(item->prefs->scoring);
266 item->prefs->scoring = NULL;
271 static void prefs_scoring_clear()
275 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
278 folder = (Folder *) cur->data;
279 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
280 prefs_scoring_free_func, NULL);
283 prefs_scoring_free(global_scoring);
284 global_scoring = NULL;
287 void prefs_scoring_read_config(void)
291 gchar buf[PREFSBUFSIZE];
292 GSList * prefs_scoring = NULL;
293 FolderItem * item = NULL;
295 debug_print(_("Reading headers configuration...\n"));
297 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
298 if ((fp = fopen(rcpath, "r")) == NULL) {
299 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
301 prefs_scoring = NULL;
306 prefs_scoring_clear();
308 while (fgets(buf, sizeof(buf), fp) != NULL) {
309 ScoringProp * scoring;
314 if (g_strcasecmp(buf, "[global]") == 0) {
316 item->prefs->scoring = prefs_scoring;
318 prefs_scoring = global_scoring;
321 else if ((*buf == '[') && buf[strlen(buf) - 1] == ']') {
325 global_scoring = prefs_scoring;
327 item->prefs->scoring = prefs_scoring;
330 id[strlen(id) - 1] = '\0';
332 item = folder_find_item_from_identifier(id);
334 prefs_scoring = global_scoring;
336 prefs_scoring = item->prefs->scoring;
338 else if ((*buf != '#') && (*buf != '\0')) {
340 scoring = scoringprop_parse(&tmp);
342 prefs_scoring = g_slist_append(prefs_scoring,
347 g_warning(_("syntax error : %s\n"), buf);
353 global_scoring = prefs_scoring;
355 item->prefs->scoring = prefs_scoring;
360 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
364 for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
368 prop = (ScoringProp *) cur->data;
369 scoring_str = scoringprop_to_string(prop);
370 if (fputs(scoring_str, fp) == EOF ||
371 fputc('\n', fp) == EOF) {
372 FILE_OP_ERROR("scoring config", "fputs || fputc");
380 static gboolean prefs_scoring_write_func(GNode *node, gpointer data)
382 FolderItem *item = node->data;
385 if (item->prefs->scoring != NULL) {
386 gchar * id = folder_item_get_identifier(item);
388 fprintf(fp, "[%s]\n", id);
391 prefs_scoring_write(fp, item->prefs->scoring);
399 static void prefs_scoring_save(FILE * fp)
403 fputs("[global]\n", fp);
404 prefs_scoring_write(fp, global_scoring);
407 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
410 folder = (Folder *) cur->data;
411 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
412 prefs_scoring_write_func, fp);
416 void prefs_scoring_write_config(void)
423 debug_print(_("Writing scoring configuration...\n"));
425 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
427 if ((pfile = prefs_write_open(rcpath)) == NULL) {
428 g_warning(_("failed to write configuration to file\n"));
434 prefs_scoring_save(pfile->fp);
438 if (prefs_write_close(pfile) < 0) {
439 g_warning(_("failed to write configuration to file\n"));