forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.c
240 lines (225 loc) · 6.48 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @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/>.
*/
/**
* @page neo_score Routines for adding user scores to emails
*
* Routines for adding user scores to emails
*/
#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "mutt.h"
#include "score.h"
#include "index/lib.h"
#include "pattern/lib.h"
#include "context.h"
#include "init.h"
#include "mutt_globals.h"
#include "mutt_thread.h"
#include "options.h"
#include "protos.h"
/**
* struct Score - Scoring rule for email
*/
struct Score
{
char *str;
struct PatternList *pat;
int val;
bool exact; ///< if this rule matches, don't evaluate any more
struct Score *next;
};
static struct Score *ScoreList = NULL;
/**
* mutt_check_rescore - Do the emails need to have their scores recalculated?
* @param m Mailbox
*/
void mutt_check_rescore(struct Mailbox *m)
{
const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
if (OptNeedRescore && c_score)
{
const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
const short c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
if (((c_sort & SORT_MASK) == SORT_SCORE) || ((c_sort_aux & SORT_MASK) == SORT_SCORE))
{
OptNeedResort = true;
if (mutt_using_threads())
OptSortSubthreads = true;
}
mutt_debug(LL_NOTIFY, "NT_SCORE: %p\n", m);
notify_send(m->notify, NT_SCORE, 0, NULL);
}
OptNeedRescore = false;
}
/**
* mutt_parse_score - Parse the 'score' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult mutt_parse_score(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
struct Score *ptr = NULL, *last = NULL;
char *pattern = NULL, *pc = NULL;
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (!MoreArgs(s))
{
mutt_buffer_printf(err, _("%s: too few arguments"), "score");
return MUTT_CMD_WARNING;
}
pattern = mutt_buffer_strdup(buf);
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (MoreArgs(s))
{
FREE(&pattern);
mutt_buffer_printf(err, _("%s: too many arguments"), "score");
return MUTT_CMD_WARNING;
}
/* look for an existing entry and update the value, else add it to the end
* of the list */
for (ptr = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
if (mutt_str_equal(pattern, ptr->str))
break;
if (!ptr)
{
struct Mailbox *m_cur = get_current_mailbox();
struct Menu *menu = get_current_menu();
struct PatternList *pat =
mutt_pattern_comp(m_cur, menu, pattern, MUTT_PC_NO_FLAGS, err);
if (!pat)
{
FREE(&pattern);
return MUTT_CMD_ERROR;
}
ptr = mutt_mem_calloc(1, sizeof(struct Score));
if (last)
last->next = ptr;
else
ScoreList = 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 = true;
pc++;
}
if (!mutt_str_atoi_full(pc, &ptr->val))
{
FREE(&pattern);
mutt_buffer_strcpy(err, _("Error: score: invalid number"));
return MUTT_CMD_ERROR;
}
OptNeedRescore = true;
return MUTT_CMD_SUCCESS;
}
/**
* mutt_score_message - Apply scoring to an email
* @param m Mailbox
* @param e Email
* @param upd_mbox If true, update the Mailbox too
*/
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
{
struct Score *tmp = NULL;
struct PatternCache cache = { 0 };
e->score = 0; /* in case of re-scoring */
for (tmp = ScoreList; tmp; tmp = tmp->next)
{
if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
{
if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
{
e->score = tmp->val;
break;
}
e->score += tmp->val;
}
}
if (e->score < 0)
e->score = 0;
const short c_score_threshold_delete =
cs_subset_number(NeoMutt->sub, "score_threshold_delete");
const short c_score_threshold_flag =
cs_subset_number(NeoMutt->sub, "score_threshold_flag");
const short c_score_threshold_read =
cs_subset_number(NeoMutt->sub, "score_threshold_read");
if (e->score <= c_score_threshold_delete)
mutt_set_flag_update(m, e, MUTT_DELETE, true, upd_mbox);
if (e->score <= c_score_threshold_read)
mutt_set_flag_update(m, e, MUTT_READ, true, upd_mbox);
if (e->score >= c_score_threshold_flag)
mutt_set_flag_update(m, e, MUTT_FLAG, true, upd_mbox);
}
/**
* mutt_parse_unscore - Parse the 'unscore' command - Implements Command::parse() - @ingroup command_parse
*/
enum CommandResult mutt_parse_unscore(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
struct Score *tmp = NULL, *last = NULL;
while (MoreArgs(s))
{
mutt_extract_token(buf, s, MUTT_TOKEN_NO_FLAGS);
if (mutt_str_equal("*", buf->data))
{
for (tmp = ScoreList; tmp;)
{
last = tmp;
tmp = tmp->next;
mutt_pattern_free(&last->pat);
FREE(&last);
}
ScoreList = NULL;
}
else
{
for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
{
if (mutt_str_equal(buf->data, tmp->str))
{
if (last)
last->next = tmp->next;
else
ScoreList = tmp->next;
mutt_pattern_free(&tmp->pat);
FREE(&tmp);
/* there should only be one score per pattern, so we can stop here */
break;
}
}
}
}
OptNeedRescore = true;
return MUTT_CMD_SUCCESS;
}