9 #include "procheader.h"
14 #include "matcher_parser.h"
16 #define PREFSBUFSIZE 1024
19 GSList * global_scoring;
22 ScoringProp * scoringprop_parse(gchar ** str)
26 ScoringProp * scoring;
28 MatcherList * matchers;
32 matchers = matcherlist_parse(&tmp);
38 key = matcher_parse_keyword(&tmp);
41 matcherlist_free(matchers);
46 if (key != MATCHING_SCORE) {
47 matcherlist_free(matchers);
52 score = matcher_parse_number(&tmp);
55 matcherlist_free(matchers);
60 scoring = scoringprop_new(matchers, score);
67 ScoringProp * scoringprop_new(MatcherList * matchers, int score)
69 ScoringProp * scoring;
71 scoring = g_new0(ScoringProp, 1);
72 scoring->matchers = matchers;
73 scoring->score = score;
78 void scoringprop_free(ScoringProp * prop)
80 matcherlist_free(prop->matchers);
84 gint scoringprop_score_message(ScoringProp * scoring, MsgInfo * info)
86 if (matcherlist_match(scoring->matchers, info))
87 return scoring->score;
92 gint score_message(GSList * scoring_list, MsgInfo * info)
98 for(l = scoring_list ; l != NULL ; l = g_slist_next(l)) {
99 ScoringProp * scoring = (ScoringProp *) l->data;
101 add_score = (scoringprop_score_message(scoring, info));
102 if (add_score == MAX_SCORE || add_score == MIN_SCORE) {
112 static void scoringprop_print(ScoringProp * prop)
117 printf("no scoring\n");
121 printf("----- scoring ------\n");
122 for(l = prop->matchers ; l != NULL ; l = g_slist_next(l)) {
123 matcherprop_print((MatcherProp *) l->data);
125 printf("cond: %s\n", prop->bool_and ? "and" : "or");
126 printf("score: %i\n", prop->score);
131 syntax for scoring config
133 file ~/.sylpheed/scoringrc
135 header "x-mailing" match "toto" score -10
136 subject match "regexp" & to regexp "regexp" score 50
137 subject match "regexp" | to regexpcase "regexp" | age_sup 5 score 30
139 if score is = MIN_SCORE (-999), no more match is done in the list
140 if score is = MAX_SCORE (-999), no more match is done in the list
144 void prefs_scoring_read_config(void)
148 gchar buf[PREFSBUFSIZE];
150 debug_print(_("Reading headers configuration...\n"));
152 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
153 if ((fp = fopen(rcpath, "r")) == NULL) {
154 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
156 prefs_scoring = NULL;
161 while (prefs_scoring != NULL) {
162 ScoringProp * scoring = (ScoringProp *) prefs_scoring->data;
163 scoringprop_free(scoring);
164 prefs_scoring = g_slist_remove(prefs_scoring, scoring);
167 while (fgets(buf, sizeof(buf), fp) != NULL) {
168 ScoringProp * scoring;
173 if ((*buf != '#') && (*buf != '\0')) {
175 scoring = scoringprop_parse(&tmp);
177 prefs_scoring = g_slist_append(prefs_scoring,
181 g_warning(_("syntax error : %s\n"), buf);
190 gchar * scoringprop_to_string(ScoringProp * prop)
196 list_str = matcherlist_to_string(prop->matchers);
198 if (list_str == NULL)
201 score_str = itos(prop->score);
202 scoring_str = g_strconcat(list_str, " score ", score_str, "\n", NULL);
209 void prefs_scoring_write_config(void)
216 debug_print(_("Writing scoring configuration...\n"));
218 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
220 if ((pfile = prefs_write_open(rcpath)) == NULL) {
221 g_warning(_("failed to write configuration to file\n"));
226 for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
229 prop = (ScoringProp *) cur->data;
230 scoring_str = scoringprop_to_string(prop);
231 if (fputs(scoring_str, pfile->fp) == EOF ||
232 fputc('\n', pfile->fp) == EOF) {
233 FILE_OP_ERROR(rcpath, "fputs || fputc");
234 prefs_write_close_revert(pfile);
244 if (prefs_write_close(pfile) < 0) {
245 g_warning(_("failed to write configuration to file\n"));
251 void prefs_scoring_free(GSList * prefs_scoring)
253 while (prefs_scoring != NULL) {
254 ScoringProp * scoring = (ScoringProp *) prefs_scoring->data;
255 scoringprop_free(scoring);
256 prefs_scoring = g_slist_remove(prefs_scoring, scoring);
260 static gboolean prefs_scoring_free_func(GNode *node, gpointer data)
262 FolderItem *item = node->data;
264 prefs_scoring_free(item->prefs->scoring);
265 item->prefs->scoring = NULL;
270 void prefs_scoring_clear()
274 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
277 folder = (Folder *) cur->data;
278 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
279 prefs_scoring_free_func, NULL);
282 prefs_scoring_free(global_scoring);
283 global_scoring = NULL;
286 void prefs_scoring_read_config(void)
291 prefs_scoring_clear();
293 debug_print(_("Reading scoring configuration...\n"));
295 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
297 if ((fp = fopen(rcpath, "r")) == NULL) {
298 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
303 matcher_parserlineno = 1;
304 matcher_parserrestart(fp);
305 if (matcher_parserparse() != 0) {
306 prefs_scoring_clear();
313 void prefs_scoring_read_config(void)
317 gchar buf[PREFSBUFSIZE];
318 GSList * prefs_scoring = NULL;
319 FolderItem * item = NULL;
321 debug_print(_("Reading scoring configuration...\n"));
323 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
324 if ((fp = fopen(rcpath, "r")) == NULL) {
325 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
327 prefs_scoring = NULL;
332 prefs_scoring_clear();
334 while (fgets(buf, sizeof(buf), fp) != NULL) {
335 ScoringProp * scoring;
340 if (g_strcasecmp(buf, "[global]") == 0) {
342 item->prefs->scoring = prefs_scoring;
344 prefs_scoring = global_scoring;
347 else if ((*buf == '[') && buf[strlen(buf) - 1] == ']') {
351 global_scoring = prefs_scoring;
353 item->prefs->scoring = prefs_scoring;
356 id[strlen(id) - 1] = '\0';
358 item = folder_find_item_from_identifier(id);
360 prefs_scoring = global_scoring;
362 prefs_scoring = item->prefs->scoring;
364 else if ((*buf != '#') && (*buf != '\0')) {
366 scoring = scoringprop_parse(&tmp);
368 prefs_scoring = g_slist_append(prefs_scoring,
375 g_warning(_("syntax error : %s\n"), buf);
381 global_scoring = prefs_scoring;
383 item->prefs->scoring = prefs_scoring;
389 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
393 for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
397 prop = (ScoringProp *) cur->data;
398 scoring_str = scoringprop_to_string(prop);
399 if (fputs(scoring_str, fp) == EOF) {
400 FILE_OP_ERROR("scoring config", "fputs");
408 static gboolean prefs_scoring_write_func(GNode *node, gpointer data)
410 FolderItem *item = node->data;
413 if (item->prefs->scoring != NULL) {
414 gchar * id = folder_item_get_identifier(item);
416 fprintf(fp, "[%s]\n", id);
419 prefs_scoring_write(fp, item->prefs->scoring);
427 static void prefs_scoring_save(FILE * fp)
431 fputs("[global]\n", fp);
432 prefs_scoring_write(fp, global_scoring);
435 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
438 folder = (Folder *) cur->data;
439 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
440 prefs_scoring_write_func, fp);
444 void prefs_scoring_write_config(void)
451 debug_print(_("Writing scoring configuration...\n"));
453 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
455 if ((pfile = prefs_write_open(rcpath)) == NULL) {
456 g_warning(_("failed to write configuration to file\n"));
462 prefs_scoring_save(pfile->fp);
466 if (prefs_write_close(pfile) < 0) {
467 g_warning(_("failed to write configuration to file\n"));