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_parserin = fp;
305 if (matcher_parserparse() != 0) {
306 printf("%s\n", rcpath);
307 prefs_scoring_clear();
314 void prefs_scoring_read_config(void)
318 gchar buf[PREFSBUFSIZE];
319 GSList * prefs_scoring = NULL;
320 FolderItem * item = NULL;
322 debug_print(_("Reading scoring configuration...\n"));
324 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
325 if ((fp = fopen(rcpath, "r")) == NULL) {
326 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
328 prefs_scoring = NULL;
333 prefs_scoring_clear();
335 while (fgets(buf, sizeof(buf), fp) != NULL) {
336 ScoringProp * scoring;
341 if (g_strcasecmp(buf, "[global]") == 0) {
343 item->prefs->scoring = prefs_scoring;
345 prefs_scoring = global_scoring;
348 else if ((*buf == '[') && buf[strlen(buf) - 1] == ']') {
352 global_scoring = prefs_scoring;
354 item->prefs->scoring = prefs_scoring;
357 id[strlen(id) - 1] = '\0';
359 item = folder_find_item_from_identifier(id);
361 prefs_scoring = global_scoring;
363 prefs_scoring = item->prefs->scoring;
365 else if ((*buf != '#') && (*buf != '\0')) {
367 scoring = scoringprop_parse(&tmp);
369 prefs_scoring = g_slist_append(prefs_scoring,
376 g_warning(_("syntax error : %s\n"), buf);
382 global_scoring = prefs_scoring;
384 item->prefs->scoring = prefs_scoring;
390 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
394 for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
398 prop = (ScoringProp *) cur->data;
399 scoring_str = scoringprop_to_string(prop);
400 if (fputs(scoring_str, fp) == EOF) {
401 FILE_OP_ERROR("scoring config", "fputs");
409 static gboolean prefs_scoring_write_func(GNode *node, gpointer data)
411 FolderItem *item = node->data;
414 if (item->prefs->scoring != NULL) {
415 gchar * id = folder_item_get_identifier(item);
417 fprintf(fp, "[%s]\n", id);
420 prefs_scoring_write(fp, item->prefs->scoring);
428 static void prefs_scoring_save(FILE * fp)
432 fputs("[global]\n", fp);
433 prefs_scoring_write(fp, global_scoring);
436 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
439 folder = (Folder *) cur->data;
440 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
441 prefs_scoring_write_func, fp);
445 void prefs_scoring_write_config(void)
452 debug_print(_("Writing scoring configuration...\n"));
454 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
456 if ((pfile = prefs_write_open(rcpath)) == NULL) {
457 g_warning(_("failed to write configuration to file\n"));
463 prefs_scoring_save(pfile->fp);
467 if (prefs_write_close(pfile) < 0) {
468 g_warning(_("failed to write configuration to file\n"));