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 typedef struct score_t {
20 int exact; /* if this rule matches, don't evaluate any more */
23 DO_INIT(score_t, score);
24 static void score_wipe(score_t *sc)
26 pattern_list_wipe(&sc->pat);
29 DO_NEW(score_t, score);
30 DO_DELETE(score_t, score);
31 DO_SLIST(score_t, score, score_delete);
33 static score_t *Score = NULL;
35 void mutt_check_rescore (CONTEXT * ctx)
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);
47 /* must redraw the index since the user might have %N in it */
48 set_option (OPTFORCEREDRAWINDEX);
49 set_option (OPTFORCEREDRAWPAGER);
51 for (i = 0; ctx && i < ctx->msgcount; i++) {
52 mutt_score_message (ctx, ctx->hdrs[i], 1);
53 ctx->hdrs[i]->pair = 0;
56 unset_option (OPTNEEDRESCORE);
59 int mutt_parse_score (BUFFER * buf, BUFFER * s,
60 unsigned long data __attribute__ ((unused)),
64 struct pattern_t *pat;
67 mutt_extract_token (buf, s, 0);
69 m_strcpy(err->data, err->dsize, _("score: too few arguments"));
74 mutt_extract_token (buf, s, 0);
77 m_strcpy(err->data, err->dsize, _("score: too many arguments"));
81 /* look for an existing entry and update the value, else add it to the end
83 for (last = &Score; *last; last = &(*last)->next) {
84 if (m_strcmp(pattern, (*last)->str) == 0)
88 if (!(pat = mutt_pattern_comp(pattern, 0, err))) {
94 (*last)->str = pattern;
97 pc += (*last)->exact = (*pc == '=');
98 (*last)->val = atoi(pc);
99 set_option(OPTNEEDRESCORE);
103 void mutt_score_message (CONTEXT * ctx, HEADER * hdr, int upd_ctx)
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;
114 hdr->score += tmp->val;
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);
128 int mutt_parse_unscore(BUFFER * buf, BUFFER * s,
129 unsigned long data __attribute__ ((unused)),
130 BUFFER * err __attribute__ ((unused)))
132 while (MoreArgs(s)) {
133 mutt_extract_token(buf, s, 0);
134 if (!m_strcmp("*", buf->data)) {
135 score_list_wipe(&Score);
139 for (last = &Score; *last; last = &(*last)->next) {
140 if (!m_strcmp(buf->data, (*last)->str)) {
141 score_t *tmp = score_list_pop(last);
148 set_option (OPTNEEDRESCORE);