Nico Golde:
[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 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "sort.h"
16 #include <string.h>
17 #include <stdlib.h>
18
19 typedef struct score_t {
20   char *str;
21   pattern_t *pat;
22   int val;
23   int exact;                    /* if this rule matches, don't evaluate any more */
24   struct score_t *next;
25 } SCORE;
26
27 SCORE *Score = NULL;
28
29 void mutt_check_rescore (CONTEXT * ctx)
30 {
31   int i;
32
33   if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
34     if ((Sort & SORT_MASK) == SORT_SCORE ||
35         (SortAux & SORT_MASK) == SORT_SCORE) {
36       set_option (OPTNEEDRESORT);
37       if ((Sort & SORT_MASK) == SORT_THREADS)
38         set_option (OPTSORTSUBTHREADS);
39     }
40
41     /* must redraw the index since the user might have %N in it */
42     set_option (OPTFORCEREDRAWINDEX);
43     set_option (OPTFORCEREDRAWPAGER);
44
45     for (i = 0; ctx && i < ctx->msgcount; i++) {
46       mutt_score_message (ctx, ctx->hdrs[i], 1);
47       ctx->hdrs[i]->pair = 0;
48     }
49   }
50   unset_option (OPTNEEDRESCORE);
51 }
52
53 int mutt_parse_score (BUFFER * buf, BUFFER * s, unsigned long data,
54                       BUFFER * err)
55 {
56   SCORE *ptr, *last;
57   char *pattern, *pc;
58   struct pattern_t *pat;
59
60   mutt_extract_token (buf, s, 0);
61   if (!MoreArgs (s)) {
62     strfcpy (err->data, _("score: too few arguments"), err->dsize);
63     return (-1);
64   }
65   pattern = buf->data;
66   memset (buf, 0, sizeof (BUFFER));
67   mutt_extract_token (buf, s, 0);
68   if (MoreArgs (s)) {
69     FREE (&pattern);
70     strfcpy (err->data, _("score: too many arguments"), err->dsize);
71     return (-1);
72   }
73
74   /* look for an existing entry and update the value, else add it to the end
75      of the list */
76   for (ptr = Score, last = NULL; ptr; last = ptr, ptr = ptr->next)
77     if (mutt_strcmp (pattern, ptr->str) == 0)
78       break;
79   if (!ptr) {
80     if ((pat = mutt_pattern_comp (pattern, 0, err)) == NULL) {
81       FREE (&pattern);
82       return (-1);
83     }
84     ptr = safe_calloc (1, sizeof (SCORE));
85     if (last)
86       last->next = ptr;
87     else
88       Score = ptr;
89     ptr->pat = pat;
90     ptr->str = pattern;
91   }
92   pc = buf->data;
93   if (*pc == '=') {
94     ptr->exact = 1;
95     pc++;
96   }
97   ptr->val = atoi (pc);
98   set_option (OPTNEEDRESCORE);
99   return 0;
100 }
101
102 void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx)
103 {
104   SCORE *tmp;
105
106   hdr->score = 0;               /* in case of re-scoring */
107   for (tmp = Score; tmp; tmp = tmp->next) {
108     if (mutt_pattern_exec (tmp->pat, 0, NULL, hdr) > 0) {
109       if (tmp->exact || tmp->val == 9999 || tmp->val == -9999) {
110         hdr->score = tmp->val;
111         break;
112       }
113       hdr->score += tmp->val;
114     }
115   }
116   if (hdr->score < 0)
117     hdr->score = 0;
118
119   if (hdr->score <= ScoreThresholdDelete)
120     _mutt_set_flag (ctx, hdr, M_DELETE, 1, upd_ctx);
121   if (hdr->score <= ScoreThresholdRead)
122     _mutt_set_flag (ctx, hdr, M_READ, 1, upd_ctx);
123   if (hdr->score >= ScoreThresholdFlag)
124     _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx);
125 }
126
127 int mutt_parse_unscore (BUFFER * buf, BUFFER * s, unsigned long data,
128                         BUFFER * err)
129 {
130   SCORE *tmp, *last = NULL;
131
132   while (MoreArgs (s)) {
133     mutt_extract_token (buf, s, 0);
134     if (!mutt_strcmp ("*", buf->data)) {
135       for (tmp = Score; tmp;) {
136         last = tmp;
137         tmp = tmp->next;
138         mutt_pattern_free (&last->pat);
139         FREE (&last);
140       }
141       Score = NULL;
142     }
143     else {
144       for (tmp = Score; tmp; last = tmp, tmp = tmp->next) {
145         if (!mutt_strcmp (buf->data, tmp->str)) {
146           if (last)
147             last->next = tmp->next;
148           else
149             Score = tmp->next;
150           mutt_pattern_free (&tmp->pat);
151           FREE (&tmp);
152           /* there should only be one score per pattern, so we can stop here */
153           break;
154         }
155       }
156     }
157   }
158   set_option (OPTNEEDRESCORE);
159   return 0;
160 }