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;
262 prefs_scoring_free(item->prefs->scoring);
263 item->prefs->scoring = NULL;
268 static void prefs_scoring_clear()
272 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
275 folder = (Folder *) cur->data;
276 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
277 prefs_scoring_free_func, NULL);
280 prefs_scoring_free(global_scoring);
281 global_scoring = NULL;
284 void prefs_scoring_read_config(void)
288 gchar buf[PREFSBUFSIZE];
289 GSList * prefs_scoring = NULL;
290 FolderItem * item = NULL;
292 debug_print(_("Reading headers configuration...\n"));
294 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
295 if ((fp = fopen(rcpath, "r")) == NULL) {
296 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
298 prefs_scoring = NULL;
303 prefs_scoring_clear();
305 while (fgets(buf, sizeof(buf), fp) != NULL) {
306 ScoringProp * scoring;
311 if (g_strcasecmp(buf, "[global]") == 0) {
313 item->prefs->scoring = prefs_scoring;
315 prefs_scoring = global_scoring;
318 else if ((*buf == '[') && buf[strlen(buf) - 1] == ']') {
322 global_scoring = prefs_scoring;
324 item->prefs->scoring = prefs_scoring;
327 id[strlen(id) - 1] = '\0';
329 item = folder_find_item_from_identifier(id);
331 prefs_scoring = global_scoring;
333 prefs_scoring = item->prefs->scoring;
335 else if ((*buf != '#') && (*buf != '\0')) {
337 scoring = scoringprop_parse(&tmp);
339 prefs_scoring = g_slist_append(prefs_scoring,
344 g_warning(_("syntax error : %s\n"), buf);
350 global_scoring = prefs_scoring;
352 item->prefs->scoring = prefs_scoring;
357 static void prefs_scoring_write(FILE * fp, GSList * prefs_scoring)
361 for (cur = prefs_scoring; cur != NULL; cur = cur->next) {
365 prop = (ScoringProp *) cur->data;
366 scoring_str = scoringprop_to_string(prop);
367 if (fputs(scoring_str, fp) == EOF ||
368 fputc('\n', fp) == EOF) {
369 FILE_OP_ERROR("scoring config", "fputs || fputc");
377 static gboolean prefs_scoring_write_func(GNode *node, gpointer data)
379 FolderItem *item = node->data;
382 if (item->prefs->scoring != NULL) {
383 gchar * id = folder_item_get_identifier(item);
385 fprintf(fp, "[%s]\n", id);
388 prefs_scoring_write(fp, item->prefs->scoring);
396 static void prefs_scoring_save(FILE * fp)
400 fputs("[global]\n", fp);
401 prefs_scoring_write(fp, global_scoring);
404 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
407 folder = (Folder *) cur->data;
408 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
409 prefs_scoring_write_func, fp);
413 void prefs_scoring_write_config(void)
420 debug_print(_("Writing scoring configuration...\n"));
422 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, SCORING_RC, NULL);
424 if ((pfile = prefs_write_open(rcpath)) == NULL) {
425 g_warning(_("failed to write configuration to file\n"));
431 prefs_scoring_save(pfile->fp);
435 if (prefs_write_close(pfile) < 0) {
436 g_warning(_("failed to write configuration to file\n"));