2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
10 #include <lib-lib/lib-lib.h>
16 @import "lib-lua/base.cpkg"
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.
27 int threshold_flag = 9999;
30 ** Messages which have been assigned a score greater than or equal to this
31 ** variable's value are automatically marked ``flagged''.
33 int threshold_delete = -1;
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.
41 int threshold_read = -1;
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.
51 typedef struct score_t {
55 int exact; /* if this rule matches, don't evaluate any more */
58 DO_INIT(score_t, score);
59 static void score_wipe(score_t *sc)
61 pattern_list_wipe(&sc->pat);
64 DO_NEW(score_t, score);
65 DO_DELETE(score_t, score);
66 DO_SLIST(score_t, score, score_delete);
68 static score_t *Score = NULL;
70 void mutt_check_rescore (CONTEXT * ctx)
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);
82 /* must redraw the index since the user might have %N in it */
83 set_option (OPTFORCEREDRAWINDEX);
84 set_option (OPTFORCEREDRAWPAGER);
86 for (i = 0; ctx && i < ctx->msgcount; i++) {
87 mutt_score_message (ctx, ctx->hdrs[i], 1);
88 ctx->hdrs[i]->pair = 0;
91 unset_option (OPTNEEDRESCORE);
94 int mutt_parse_score (BUFFER * buf, BUFFER * s,
95 unsigned long data __attribute__ ((unused)),
99 struct pattern_t *pat;
102 mutt_extract_token (buf, s, 0);
104 m_strcpy(err->data, err->dsize, _("score: too few arguments"));
109 mutt_extract_token (buf, s, 0);
112 m_strcpy(err->data, err->dsize, _("score: too many arguments"));
116 /* look for an existing entry and update the value, else add it to the end
118 for (last = &Score; *last; last = &(*last)->next) {
119 if (m_strcmp(pattern, (*last)->str) == 0)
123 if (!(pat = mutt_pattern_comp(pattern, 0, err))) {
129 (*last)->str = pattern;
132 pc += (*last)->exact = (*pc == '=');
133 (*last)->val = atoi(pc);
134 set_option(OPTNEEDRESCORE);
138 void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx)
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;
149 hdr->score += tmp->val;
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);
163 int mutt_parse_unscore(BUFFER * buf, BUFFER * s,
164 unsigned long data __attribute__ ((unused)),
165 BUFFER * err __attribute__ ((unused)))
167 while (MoreArgs(s)) {
168 mutt_extract_token(buf, s, 0);
169 if (!m_strcmp("*", buf->data)) {
170 score_list_wipe(&Score);
174 for (last = &Score; *last; last = &(*last)->next) {
175 if (!m_strcmp(buf->data, (*last)->str)) {
176 score_t *tmp = score_list_pop(last);
183 set_option (OPTNEEDRESCORE);