slightly less hackish way to generate files, I'm still not very pleased with it.
[apps/madmutt.git] / score.cpkg
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 #include "score.h"
16 @import  "lib-lua/base.cpkg"
17
18 @package mod_score {
19     bool enable = 1;
20     /*
21      ** .pp
22      ** When this variable is \fIunset\fP, scoring is turned off.  This can
23      ** be useful to selectively disable scoring for certain folders when the
24      ** ``$$score_threshold_delete'' variable and friends are used.
25      **
26      */
27     int threshold_flag = 9999;
28     /*
29      ** .pp
30      ** Messages which have been assigned a score greater than or equal to this
31      ** variable's value are automatically marked ``flagged''.
32      */
33     int threshold_delete = -1;
34     /*
35      ** .pp
36      ** Messages which have been assigned a score equal to or lower than the value
37      ** of this variable are automatically marked for deletion by Madmutt.  Since
38      ** Madmutt scores are always greater than or equal to zero, the default setting
39      ** of this variable will never mark a message for deletion.
40      */
41     int threshold_read   = -1;
42     /*
43      ** .pp
44      ** Messages which have been assigned a score equal to or lower than the value
45      ** of this variable are automatically marked as read by Madmutt.  Since
46      ** Madmutt scores are always greater than or equal to zero, the default setting
47      ** of this variable will never mark a message read.
48      */
49 };
50
51 typedef struct score_t {
52   char *str;
53   pattern_t *pat;
54   int val;
55   int exact;                    /* if this rule matches, don't evaluate any more */
56   struct score_t *next;
57 } score_t;
58 DO_INIT(score_t, score);
59 static void score_wipe(score_t *sc)
60 {
61     pattern_list_wipe(&sc->pat);
62     p_delete(&sc->str);
63 }
64 DO_NEW(score_t, score);
65 DO_DELETE(score_t, score);
66 DO_SLIST(score_t, score, score_delete);
67
68 static score_t *Score = NULL;
69
70 void mutt_check_rescore (CONTEXT * ctx)
71 {
72   int i;
73
74   if (option (OPTNEEDRESCORE) && mod_score.enable) {
75     if ((Sort & SORT_MASK) == SORT_SCORE ||
76         (SortAux & SORT_MASK) == SORT_SCORE) {
77       set_option (OPTNEEDRESORT);
78       if ((Sort & SORT_MASK) == SORT_THREADS)
79         set_option (OPTSORTSUBTHREADS);
80     }
81
82     /* must redraw the index since the user might have %N in it */
83     set_option (OPTFORCEREDRAWINDEX);
84     set_option (OPTFORCEREDRAWPAGER);
85
86     for (i = 0; ctx && i < ctx->msgcount; i++) {
87       mutt_score_message (ctx, ctx->hdrs[i], 1);
88       ctx->hdrs[i]->pair = 0;
89     }
90   }
91   unset_option (OPTNEEDRESCORE);
92 }
93
94 int mutt_parse_score (BUFFER * buf, BUFFER * s,
95                       unsigned long data __attribute__ ((unused)),
96                       BUFFER * err)
97 {
98   char *pattern, *pc;
99   struct pattern_t *pat;
100   score_t **last;
101
102   mutt_extract_token (buf, s, 0);
103   if (!MoreArgs (s)) {
104     m_strcpy(err->data, err->dsize, _("score: too few arguments"));
105     return (-1);
106   }
107   pattern = buf->data;
108   p_clear(buf, 1);
109   mutt_extract_token (buf, s, 0);
110   if (MoreArgs (s)) {
111     p_delete(&pattern);
112     m_strcpy(err->data, err->dsize, _("score: too many arguments"));
113     return (-1);
114   }
115
116   /* look for an existing entry and update the value, else add it to the end
117      of the list */
118   for (last = &Score; *last; last = &(*last)->next) {
119     if (m_strcmp(pattern, (*last)->str) == 0)
120       break;
121   }
122   if (!*last) {
123     if (!(pat = mutt_pattern_comp(pattern, 0, err))) {
124       p_delete(&pattern);
125       return (-1);
126     }
127     *last = score_new();
128     (*last)->pat = pat;
129     (*last)->str = pattern;
130   }
131   pc = buf->data;
132   pc += (*last)->exact = (*pc == '=');
133   (*last)->val = atoi(pc);
134   set_option(OPTNEEDRESCORE);
135   return 0;
136 }
137
138 void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx)
139 {
140   score_t *tmp;
141
142   hdr->score = 0;               /* in case of re-scoring */
143   for (tmp = Score; tmp; tmp = tmp->next) {
144     if (mutt_pattern_exec (tmp->pat, M_MATCH_FULL_ADDRESS, NULL, hdr) > 0) {
145       if (tmp->exact || tmp->val == 9999 || tmp->val == -9999) {
146         hdr->score = tmp->val;
147         break;
148       }
149       hdr->score += tmp->val;
150     }
151   }
152   if (hdr->score < 0)
153     hdr->score = 0;
154
155   if (hdr->score <= mod_score.threshold_delete)
156     _mutt_set_flag (ctx, hdr, M_DELETE, 1, upd_ctx);
157   if (hdr->score <= mod_score.threshold_flag)
158     _mutt_set_flag (ctx, hdr, M_READ, 1, upd_ctx);
159   if (hdr->score >= mod_score.threshold_flag)
160     _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx);
161 }
162
163 int mutt_parse_unscore(BUFFER * buf, BUFFER * s,
164                        unsigned long data __attribute__ ((unused)),
165                        BUFFER * err __attribute__ ((unused)))
166 {
167     while (MoreArgs(s)) {
168         mutt_extract_token(buf, s, 0);
169         if (!m_strcmp("*", buf->data)) {
170             score_list_wipe(&Score);
171         } else {
172             score_t **last;
173
174             for (last = &Score; *last; last = &(*last)->next) {
175                 if (!m_strcmp(buf->data, (*last)->str)) {
176                     score_t *tmp = score_list_pop(last);
177                     score_delete(&tmp);
178                     break;
179                 }
180             }
181         }
182     }
183     set_option (OPTNEEDRESCORE);
184     return 0;
185 }
186
187 /* vim:set ft=c: */