forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.c
212 lines (197 loc) · 5.25 KB
/
score.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* @file
* Routines for adding user scores to emails
*
* @authors
* Copyright (C) 1996-2000 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "mutt.h"
#include "context.h"
#include "globals.h"
#include "header.h"
#include "keymap.h"
#include "lib/lib.h"
#include "mutt_menu.h"
#include "options.h"
#include "pattern.h"
#include "protos.h"
#include "sort.h"
/**
* struct Score - Scoring rule for email
*/
struct Score
{
char *str;
struct Pattern *pat;
int val;
int exact; /**< if this rule matches, don't evaluate any more */
struct Score *next;
};
static struct Score *Score = NULL;
void mutt_check_rescore(struct Context *ctx)
{
int i;
if (option(OPT_NEED_RESCORE) && option(OPT_SCORE))
{
if ((Sort & SORT_MASK) == SORT_SCORE || (SortAux & SORT_MASK) == SORT_SCORE)
{
set_option(OPT_NEED_RESORT);
if ((Sort & SORT_MASK) == SORT_THREADS)
set_option(OPT_SORT_SUBTHREADS);
}
/* must redraw the index since the user might have %N in it */
mutt_set_menu_redraw_full(MENU_MAIN);
mutt_set_menu_redraw_full(MENU_PAGER);
for (i = 0; ctx && i < ctx->msgcount; i++)
{
mutt_score_message(ctx, ctx->hdrs[i], 1);
ctx->hdrs[i]->pair = 0;
}
}
unset_option(OPT_NEED_RESCORE);
}
int mutt_parse_score(struct Buffer *buf, struct Buffer *s, unsigned long data,
struct Buffer *err)
{
struct Score *ptr = NULL, *last = NULL;
char *pattern = NULL, *pc = NULL;
struct Pattern *pat = NULL;
mutt_extract_token(buf, s, 0);
if (!MoreArgs(s))
{
strfcpy(err->data, _("score: too few arguments"), err->dsize);
return -1;
}
pattern = buf->data;
mutt_buffer_init(buf);
mutt_extract_token(buf, s, 0);
if (MoreArgs(s))
{
FREE(&pattern);
strfcpy(err->data, _("score: too many arguments"), err->dsize);
return -1;
}
/* look for an existing entry and update the value, else add it to the end
of the list */
for (ptr = Score, last = NULL; ptr; last = ptr, ptr = ptr->next)
if (mutt_strcmp(pattern, ptr->str) == 0)
break;
if (!ptr)
{
if ((pat = mutt_pattern_comp(pattern, 0, err)) == NULL)
{
FREE(&pattern);
return -1;
}
ptr = safe_calloc(1, sizeof(struct Score));
if (last)
last->next = ptr;
else
Score = ptr;
ptr->pat = pat;
ptr->str = pattern;
}
else
/* 'buf' arg was cleared and 'pattern' holds the only reference;
* as here 'ptr' != NULL -> update the value only in which case
* ptr->str already has the string, so pattern should be freed.
*/
FREE(&pattern);
pc = buf->data;
if (*pc == '=')
{
ptr->exact = 1;
pc++;
}
if (mutt_atoi(pc, &ptr->val) < 0)
{
FREE(&pattern);
strfcpy(err->data, _("Error: score: invalid number"), err->dsize);
return -1;
}
set_option(OPT_NEED_RESCORE);
return 0;
}
void mutt_score_message(struct Context *ctx, struct Header *hdr, int upd_ctx)
{
struct Score *tmp = NULL;
struct PatternCache cache;
memset(&cache, 0, sizeof(cache));
hdr->score = 0; /* in case of re-scoring */
for (tmp = Score; tmp; tmp = tmp->next)
{
if (mutt_pattern_exec(tmp->pat, MUTT_MATCH_FULL_ADDRESS, NULL, hdr, &cache) > 0)
{
if (tmp->exact || tmp->val == 9999 || tmp->val == -9999)
{
hdr->score = tmp->val;
break;
}
hdr->score += tmp->val;
}
}
if (hdr->score < 0)
hdr->score = 0;
if (hdr->score <= ScoreThresholdDelete)
_mutt_set_flag(ctx, hdr, MUTT_DELETE, 1, upd_ctx);
if (hdr->score <= ScoreThresholdRead)
_mutt_set_flag(ctx, hdr, MUTT_READ, 1, upd_ctx);
if (hdr->score >= ScoreThresholdFlag)
_mutt_set_flag(ctx, hdr, MUTT_FLAG, 1, upd_ctx);
}
int mutt_parse_unscore(struct Buffer *buf, struct Buffer *s, unsigned long data,
struct Buffer *err)
{
struct Score *tmp = NULL, *last = NULL;
while (MoreArgs(s))
{
mutt_extract_token(buf, s, 0);
if (mutt_strcmp("*", buf->data) == 0)
{
for (tmp = Score; tmp;)
{
last = tmp;
tmp = tmp->next;
mutt_pattern_free(&last->pat);
FREE(&last);
}
Score = NULL;
}
else
{
for (tmp = Score; tmp; last = tmp, tmp = tmp->next)
{
if (mutt_strcmp(buf->data, tmp->str) == 0)
{
if (last)
last->next = tmp->next;
else
Score = tmp->next;
mutt_pattern_free(&tmp->pat);
FREE(&tmp);
/* there should only be one score per pattern, so we can stop here */
break;
}
}
}
}
set_option(OPT_NEED_RESCORE);
return 0;
}