NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
score.c
Go to the documentation of this file.
1
25
31
32#include "config.h"
33#include <stdbool.h>
34#include <stdlib.h>
35#include "mutt/lib.h"
36#include "config/lib.h"
37#include "email/lib.h"
38#include "core/lib.h"
39#include "mutt.h"
40#include "score.h"
41#include "index/lib.h"
42#include "parse/lib.h"
43#include "pattern/lib.h"
44#include "globals.h"
45#include "mutt_thread.h"
46#include "protos.h"
47
51struct Score
52{
53 char *str;
54 struct PatternList *pat;
55 int val;
56 bool exact;
57 struct Score *next;
58};
59
61static struct Score *ScoreList = NULL;
62
68{
69 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
70 if (OptNeedRescore && c_score)
71 {
72 const enum EmailSortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
73 const enum EmailSortType c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
74 if (((c_sort & SORT_MASK) == EMAIL_SORT_SCORE) ||
75 ((c_sort_aux & SORT_MASK) == EMAIL_SORT_SCORE))
76 {
77 OptNeedResort = true;
79 OptSortSubthreads = true;
80 }
81
82 mutt_debug(LL_NOTIFY, "NT_SCORE: %p\n", (void *) m);
83 notify_send(m->notify, NT_SCORE, 0, NULL);
84 }
85 OptNeedRescore = false;
86}
87
94enum CommandResult parse_score(const struct Command *cmd, struct Buffer *line,
95 struct Buffer *err)
96{
97 if (!MoreArgs(line))
98 {
99 buf_printf(err, _("%s: too few arguments"), cmd->name);
100 return MUTT_CMD_WARNING;
101 }
102
103 struct Score *ptr = NULL, *last = NULL;
104 char *pattern = NULL, *pc = NULL;
105 struct Buffer *token = buf_pool_get();
107
109 if (!MoreArgs(line))
110 {
111 buf_printf(err, _("%s: too few arguments"), cmd->name);
112 goto done;
113 }
114 pattern = buf_strdup(token);
116 if (MoreArgs(line))
117 {
118 buf_printf(err, _("%s: too many arguments"), cmd->name);
119 goto done;
120 }
121
122 /* look for an existing entry and update the value, else add it to the end
123 * of the list */
124 for (ptr = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
125 if (mutt_str_equal(pattern, ptr->str))
126 break;
127
128 if (ptr)
129 {
130 /* 'token' arg was cleared and 'pattern' holds the only reference;
131 * as here 'ptr' != NULL -> update the value only in which case
132 * ptr->str already has the string, so pattern should be freed. */
133 FREE(&pattern);
134 }
135 else
136 {
137 struct MailboxView *mv_cur = get_current_mailbox_view();
138 struct PatternList *pat = mutt_pattern_comp(mv_cur, pattern, MUTT_PC_NO_FLAGS, err);
139 if (!pat)
140 {
141 goto done;
142 }
143 ptr = MUTT_MEM_CALLOC(1, struct Score);
144 if (last)
145 last->next = ptr;
146 else
147 ScoreList = ptr;
148 ptr->pat = pat;
149 ptr->str = pattern;
150 pattern = NULL;
151 }
152
153 pc = token->data;
154 if (*pc == '=')
155 {
156 ptr->exact = true;
157 pc++;
158 }
159
160 if (!mutt_str_atoi_full(pc, &ptr->val))
161 {
162 buf_strcpy(err, _("Error: score: invalid number"));
163 goto done;
164 }
165 OptNeedRescore = true;
166
167 rc = MUTT_CMD_SUCCESS;
168
169done:
170 buf_pool_release(&token);
171 FREE(&pattern);
172 return rc;
173}
174
181void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
182{
183 struct Score *tmp = NULL;
184 struct PatternCache cache = { 0 };
185
186 e->score = 0; /* in case of re-scoring */
187 for (tmp = ScoreList; tmp; tmp = tmp->next)
188 {
189 if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
190 {
191 if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
192 {
193 e->score = tmp->val;
194 break;
195 }
196 e->score += tmp->val;
197 }
198 }
199 if (e->score < 0)
200 e->score = 0;
201
202 const short c_score_threshold_delete = cs_subset_number(NeoMutt->sub, "score_threshold_delete");
203 const short c_score_threshold_flag = cs_subset_number(NeoMutt->sub, "score_threshold_flag");
204 const short c_score_threshold_read = cs_subset_number(NeoMutt->sub, "score_threshold_read");
205
206 if (e->score <= c_score_threshold_delete)
207 mutt_set_flag(m, e, MUTT_DELETE, true, upd_mbox);
208 if (e->score <= c_score_threshold_read)
209 mutt_set_flag(m, e, MUTT_READ, true, upd_mbox);
210 if (e->score >= c_score_threshold_flag)
211 mutt_set_flag(m, e, MUTT_FLAG, true, upd_mbox);
212}
213
220enum CommandResult parse_unscore(const struct Command *cmd, struct Buffer *line,
221 struct Buffer *err)
222{
223 if (!MoreArgs(line))
224 {
225 buf_printf(err, _("%s: too few arguments"), cmd->name);
226 return MUTT_CMD_WARNING;
227 }
228
229 struct Score *tmp = NULL, *last = NULL;
230 struct Buffer *token = buf_pool_get();
231
232 while (MoreArgs(line))
233 {
235 if (mutt_str_equal("*", buf_string(token)))
236 {
237 for (tmp = ScoreList; tmp;)
238 {
239 last = tmp;
240 tmp = tmp->next;
241 mutt_pattern_free(&last->pat);
242 FREE(&last);
243 }
244 ScoreList = NULL;
245 }
246 else
247 {
248 for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
249 {
250 if (mutt_str_equal(buf_string(token), tmp->str))
251 {
252 if (last)
253 last->next = tmp->next;
254 else
255 ScoreList = tmp->next;
256 mutt_pattern_free(&tmp->pat);
257 FREE(&tmp);
258 /* there should only be one score per pattern, so we can stop here */
259 break;
260 }
261 }
262 }
263 }
264
265 OptNeedRescore = true;
266 buf_pool_release(&token);
267 return MUTT_CMD_SUCCESS;
268}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
CommandResult
Error codes for command_t parse functions.
Definition command.h:35
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:38
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:36
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:37
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition compile.c:902
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:774
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition helpers.c:266
Convenience wrapper for the config headers.
#define SORT_MASK
Mask for the sort id.
Definition sort.h:38
Convenience wrapper for the core headers.
Structs that make up an email.
bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition globals.c:62
bool OptNeedResort
(pseudo) used to force a re-sort
Definition globals.c:63
EmailSortType
Methods for sorting Emails.
Definition sort.h:53
@ EMAIL_SORT_SCORE
Sort by the email's score.
Definition sort.h:58
bool mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition exec.c:1148
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:48
#define MoreArgs(buf)
Definition extract.h:30
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:44
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition flags.c:56
bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition globals.c:68
Global variables.
enum CommandResult parse_unscore(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse the 'unscore' command - Implements Command::parse() -.
Definition score.c:220
enum CommandResult parse_score(const struct Command *cmd, struct Buffer *line, struct Buffer *err)
Parse the 'score' command - Implements Command::parse() -.
Definition score.c:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition index.c:689
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:49
#define FREE(x)
Definition memory.h:62
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition notify.c:173
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:660
Many unsorted constants and some structs.
@ MUTT_READ
Messages that have been read.
Definition mutt.h:73
@ MUTT_FLAG
Flagged messages.
Definition mutt.h:79
@ MUTT_DELETE
Messages to be deleted.
Definition mutt.h:75
Create/manipulate threading in emails.
#define mutt_using_threads()
@ NT_SCORE
Email scoring has changed.
Definition notify_type.h:54
Text parsing functions.
Match patterns to emails.
#define MUTT_PC_NO_FLAGS
No flags are set.
Definition lib.h:68
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition lib.h:106
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
Prototypes for many functions.
#define SLIST_FIRST(head)
Definition queue.h:227
void mutt_check_rescore(struct Mailbox *m)
Do the emails need to have their scores recalculated?
Definition score.c:67
static struct Score * ScoreList
Linked list of email scoring rules.
Definition score.c:61
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition score.c:181
Routines for adding user scores to emails.
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
const char * name
Name of the command.
Definition command.h:59
The envelope/body of an email.
Definition email.h:39
int score
Message score.
Definition email.h:113
View of a Mailbox.
Definition mview.h:40
A mailbox.
Definition mailbox.h:79
Container for Accounts, Notifications.
Definition neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
Cache commonly-used patterns.
Definition lib.h:117
Scoring rule for email.
Definition score.c:52
bool exact
If this rule matches, don't evaluate any more.
Definition score.c:56
char * str
Definition score.c:53
struct PatternList * pat
Definition score.c:54
int val
Definition score.c:55
struct Score * next
Linked list.
Definition score.c:57