2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 typedef struct score_t
33 int exact; /* if this rule matches, don't evaluate any more */
37 static SCORE *Score = NULL;
39 void mutt_check_rescore (CONTEXT *ctx)
43 if (option (OPTNEEDRESCORE) && option (OPTSCORE))
45 if ((Sort & SORT_MASK) == SORT_SCORE ||
46 (SortAux & SORT_MASK) == SORT_SCORE)
48 set_option (OPTNEEDRESORT);
49 if ((Sort & SORT_MASK) == SORT_THREADS)
50 set_option (OPTSORTSUBTHREADS);
53 /* must redraw the index since the user might have %N in it */
54 set_option (OPTFORCEREDRAWINDEX);
55 set_option (OPTFORCEREDRAWPAGER);
57 for (i = 0; ctx && i < ctx->msgcount; i++)
59 mutt_score_message (ctx, ctx->hdrs[i], 1);
60 ctx->hdrs[i]->pair = 0;
63 unset_option (OPTNEEDRESCORE);
66 int mutt_parse_score (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
70 struct pattern_t *pat;
72 mutt_extract_token (buf, s, 0);
75 strfcpy (err->data, _("score: too few arguments"), err->dsize);
79 memset (buf, 0, sizeof (BUFFER));
80 mutt_extract_token (buf, s, 0);
84 strfcpy (err->data, _("score: too many arguments"), err->dsize);
88 /* look for an existing entry and update the value, else add it to the end
90 for (ptr = Score, last = NULL; ptr; last = ptr, ptr = ptr->next)
91 if (mutt_strcmp (pattern, ptr->str) == 0)
95 if ((pat = mutt_pattern_comp (pattern, 0, err)) == NULL)
100 ptr = safe_calloc (1, sizeof (SCORE));
108 /* 'buf' arg was cleared and 'pattern' holds the only reference;
109 * as here 'ptr' != NULL -> update the value only in which case
110 * ptr->str already has the string, so pattern should be freed.
119 ptr->val = atoi (pc);
120 set_option (OPTNEEDRESCORE);
124 void mutt_score_message (CONTEXT *ctx, HEADER *hdr, int upd_ctx)
128 hdr->score = 0; /* in case of re-scoring */
129 for (tmp = Score; tmp; tmp = tmp->next)
131 if (mutt_pattern_exec (tmp->pat, 0, NULL, hdr) > 0)
133 if (tmp->exact || tmp->val == 9999 || tmp->val == -9999)
135 hdr->score = tmp->val;
138 hdr->score += tmp->val;
144 if (hdr->score <= ScoreThresholdDelete)
145 _mutt_set_flag (ctx, hdr, M_DELETE, 1, upd_ctx);
146 if (hdr->score <= ScoreThresholdRead)
147 _mutt_set_flag (ctx, hdr, M_READ, 1, upd_ctx);
148 if (hdr->score >= ScoreThresholdFlag)
149 _mutt_set_flag (ctx, hdr, M_FLAG, 1, upd_ctx);
152 int mutt_parse_unscore (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
154 SCORE *tmp, *last = NULL;
158 mutt_extract_token (buf, s, 0);
159 if (!mutt_strcmp ("*", buf->data))
161 for (tmp = Score; tmp; )
165 mutt_pattern_free (&last->pat);
172 for (tmp = Score; tmp; last = tmp, tmp = tmp->next)
174 if (!mutt_strcmp (buf->data, tmp->str))
177 last->next = tmp->next;
180 mutt_pattern_free (&tmp->pat);
182 /* there should only be one score per pattern, so we can stop here */
188 set_option (OPTNEEDRESCORE);