6f58ba16d553e906124099155d8b29ff2005771c
[apps/madmutt.git] / score.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-lib/lib-lib.h>
11
12 #include "mutt.h"
13 #include "sort.h"
14 #include "pattern.h"
15
16 typedef struct score_t {
17   char *str;
18   pattern_t *pat;
19   int val;
20   int exact;                    /* if this rule matches, don't evaluate any more */
21   struct score_t *next;
22 } score_t;
23
24 score_t *Score = NULL;
25
26 DO_INIT(score_t, score);
27 static void score_wipe(score_t *sc)
28 {
29     pattern_list_wipe(&sc->pat);
30     p_delete(&sc->str);
31 }
32 DO_NEW(score_t, score);
33 DO_DELETE(score_t, score);
34
35 DO_SLIST(score_t, score, score_delete);
36
37 void mutt_check_rescore (CONTEXT * ctx)
38 {
39   int i;
40
41   if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
42     if ((Sort & SORT_MASK) == SORT_SCORE ||
43         (SortAux & SORT_MASK) == SORT_SCORE) {
44       set_option (OPTNEEDRESORT);
45       if ((Sort & SORT_MASK) == SORT_THREADS)
46         set_option (OPTSORTSUBTHREADS);
47     }
48
49     /* must redraw the index since the user might have %N in it */
50     set_option (OPTFORCEREDRAWINDEX);
51     set_option (OPTFORCEREDRAWPAGER);
52
53     for (i = 0; ctx && i < ctx->msgcount; i++) {
54       mutt_score_message (ctx, ctx->hdrs[i], 1);
55       ctx->hdrs[i]->pair = 0;
56     }
57   }
58   unset_option (OPTNEEDRESCORE);
59 }
60
61 int mutt_parse_score (BUFFER * buf, BUFFER * s,
62                       unsigned long data __attribute__ ((unused)),
63                       BUFFER * err)
64 {
65   char *pattern, *pc;
66   struct pattern_t *pat;
67   score_t **last;
68
69   mutt_extract_token (buf, s, 0);
70   if (!MoreArgs (s)) {
71     m_strcpy(err->data, err->dsize, _("score: too few arguments"));
72     return (-1);
73   }
74   pattern = buf->data;
75   p_clear(buf, 1);
76   mutt_extract_token (buf, s, 0);
77   if (MoreArgs (s)) {
78     p_delete(&pattern);
79     m_strcpy(err->data, err->dsize, _("score: too many arguments"));
80     return (-1);
81   }
82
83   /* look for an existing entry and update the value, else add it to the end
84      of the list */
85   for (last = &Score; *last; last = &(*last)->next) {
86     if (m_strcmp(pattern, (*last)->str) == 0)
87       break;
88   }
89   if (!*last) {
90     if (!(pat = mutt_pattern_comp(pattern, 0, err))) {
91       p_delete(&pattern);
92       return (-1);
93     }
94     *last = score_new();
95     (*last)->pat = pat;
96     (*last)->str = pattern;
97   }
98   pc = buf->data;
99   if (*pc == '=') {
100     (*last)->exact = 1;
101     pc++;
102   }
103   (*last)->val = atoi(pc);
104   set_option(OPTNEEDRESCORE);
105   return 0;
106 }
107
108 void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx)
109 {
110   score_t *tmp;
111
112   hdr->score = 0;               /* in case of re-scoring */
113   for (tmp = Score; tmp; tmp = tmp->next) {
114     if (mutt_pattern_exec (tmp->pat, M_MATCH_FULL_ADDRESS, NULL, hdr) > 0) {
115       if (tmp->exact || tmp->val == 9999 || tmp->val == -9999) {
116         hdr->score = tmp->val;
117         break;
118       }
119       hdr->score += tmp->val;
120     }
121   }
122   if (hdr->score < 0)
123     hdr->score = 0;
124
125   if (hdr->score <= ScoreThresholdDelete)
126     _mutt_set_flag (ctx, hdr, M_DELETE, 1, upd_ctx);
127   if (hdr->score <= ScoreThresholdRead)
128     _mutt_set_flag (ctx, hdr, M_READ, 1, upd_ctx);
129   if (hdr->score >= ScoreThresholdFlag)
130     _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx);
131 }
132
133 int mutt_parse_unscore(BUFFER * buf, BUFFER * s,
134                        unsigned long data __attribute__ ((unused)),
135                        BUFFER * err __attribute__ ((unused)))
136 {
137     while (MoreArgs(s)) {
138         mutt_extract_token(buf, s, 0);
139         if (!m_strcmp("*", buf->data)) {
140             score_list_wipe(&Score);
141         } else {
142             score_t **last;
143
144             for (last = &Score; *last; last = &(*last)->next) {
145                 if (!m_strcmp(buf->data, (*last)->str)) {
146                     score_t *tmp = score_list_pop(last);
147                     score_delete(&tmp);
148                     break;
149                 }
150             }
151         }
152     }
153     set_option (OPTNEEDRESCORE);
154     return 0;
155 }