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