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