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