NeoMutt  2025-12-11-769-g906513
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 "core/lib.h"
38#include "gui/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 "email.h"
45#include "globals.h"
46#include "module_data.h"
47#include "sort.h"
48
53void score_list_free(struct Score **sp)
54{
55 if (!sp)
56 return;
57
58 struct Score *sc = *sp;
59 while (sc)
60 {
61 struct Score *next = sc->next;
63 FREE(&sc->str);
64 FREE(&sc);
65 sc = next;
66 }
67 *sp = NULL;
68}
69
76enum CommandResult parse_score(const struct Command *cmd, struct Buffer *line,
77 const struct ParseContext *pc, struct ParseError *pe)
78{
79 struct Buffer *err = pe->message;
81
82 if (!MoreArgs(line))
83 {
84 buf_printf(err, _("%s: too few arguments"), cmd->name);
85 return MUTT_CMD_WARNING;
86 }
87
88 struct Score *ptr = NULL, *last = NULL;
89 char *pattern = NULL, *patchar = NULL;
90 struct Buffer *token = buf_pool_get();
92
94 if (!MoreArgs(line))
95 {
96 buf_printf(err, _("%s: too few arguments"), cmd->name);
97 goto done;
98 }
99 pattern = buf_strdup(token);
101 if (MoreArgs(line))
102 {
103 buf_printf(err, _("%s: too many arguments"), cmd->name);
104 goto done;
105 }
106
107 /* look for an existing entry and update the value, else add it to the end
108 * of the list */
109 for (ptr = mod_data->score_list, last = NULL; ptr; last = ptr, ptr = ptr->next)
110 if (mutt_str_equal(pattern, ptr->str))
111 break;
112
113 if (ptr)
114 {
115 /* 'token' arg was cleared and 'pattern' holds the only reference;
116 * as here 'ptr' != NULL -> update the value only in which case
117 * ptr->str already has the string, so pattern should be freed. */
118 FREE(&pattern);
119 }
120 else
121 {
122 struct MailboxView *mv_cur = get_current_mailbox_view();
123 struct PatternList *pat = mutt_pattern_comp(mv_cur, pattern, MUTT_PC_NO_FLAGS, err);
124 if (!pat)
125 {
126 goto done;
127 }
128 ptr = MUTT_MEM_CALLOC(1, struct Score);
129 if (last)
130 last->next = ptr;
131 else
132 mod_data->score_list = ptr;
133 ptr->pat = pat;
134 ptr->str = pattern;
135 pattern = NULL;
136 }
137
138 patchar = token->data;
139 if (*patchar == '=')
140 {
141 ptr->exact = true;
142 patchar++;
143 }
144
145 if (!mutt_str_atoi_full(patchar, &ptr->val))
146 {
147 buf_strcpy(err, _("Error: score: invalid number"));
148 goto done;
149 }
150 OptNeedRescore = true;
151
152 rc = MUTT_CMD_SUCCESS;
153
154done:
155 buf_pool_release(&token);
156 FREE(&pattern);
157 return rc;
158}
159
166enum CommandResult parse_unscore(const struct Command *cmd, struct Buffer *line,
167 const struct ParseContext *pc, struct ParseError *pe)
168{
169 struct Buffer *err = pe->message;
171
172 if (!MoreArgs(line))
173 {
174 buf_printf(err, _("%s: too few arguments"), cmd->name);
175 return MUTT_CMD_WARNING;
176 }
177
178 struct Score *tmp = NULL, *last = NULL;
179 struct Buffer *token = buf_pool_get();
180
181 while (MoreArgs(line))
182 {
184 if (mutt_str_equal("*", buf_string(token)))
185 {
186 score_list_free(&mod_data->score_list);
187 }
188 else
189 {
190 for (tmp = mod_data->score_list; tmp; last = tmp, tmp = tmp->next)
191 {
192 if (mutt_str_equal(buf_string(token), tmp->str))
193 {
194 if (last)
195 last->next = tmp->next;
196 else
197 mod_data->score_list = tmp->next;
198 mutt_pattern_free(&tmp->pat);
199 FREE(&tmp);
200 /* there should only be one score per pattern, so we can stop here */
201 break;
202 }
203 }
204 }
205 }
206
207 OptNeedRescore = true;
208 buf_pool_release(&token);
209 return MUTT_CMD_SUCCESS;
210}
211
217{
218 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
219 if (OptNeedRescore && c_score)
220 {
221 const enum EmailSortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
222 const enum EmailSortType c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
223 if (((c_sort & SORT_MASK) == EMAIL_SORT_SCORE) ||
224 ((c_sort_aux & SORT_MASK) == EMAIL_SORT_SCORE))
225 {
226 OptNeedResort = true;
227 if (mutt_using_threads())
228 OptSortSubthreads = true;
229 }
230
231 mutt_debug(LL_NOTIFY, "NT_SCORE: %p\n", (void *) m);
232 notify_send(m->notify, NT_SCORE, 0, NULL);
233 }
234 OptNeedRescore = false;
235}
236
243void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
244{
246 struct Score *tmp = NULL;
247 struct PatternCache cache = { 0 };
248
249 e->score = 0; /* in case of re-scoring */
250 for (tmp = mod_data->score_list; tmp; tmp = tmp->next)
251 {
252 if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
253 {
254 if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
255 {
256 e->score = tmp->val;
257 break;
258 }
259 e->score += tmp->val;
260 }
261 }
262 if (e->score < 0)
263 e->score = 0;
264
265 const short c_score_threshold_delete = cs_subset_number(NeoMutt->sub, "score_threshold_delete");
266 const short c_score_threshold_flag = cs_subset_number(NeoMutt->sub, "score_threshold_flag");
267 const short c_score_threshold_read = cs_subset_number(NeoMutt->sub, "score_threshold_read");
268
269 if (e->score <= c_score_threshold_delete)
270 mutt_set_flag(m, e, MUTT_DELETE, true, upd_mbox);
271 if (e->score <= c_score_threshold_read)
272 mutt_set_flag(m, e, MUTT_READ, true, upd_mbox);
273 if (e->score >= c_score_threshold_flag)
274 mutt_set_flag(m, e, MUTT_FLAG, true, upd_mbox);
275}
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:37
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition command.h:39
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition compile.c:958
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition compile.c:830
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:39
Convenience wrapper for the core headers.
Email private Module data.
bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition globals.c:51
bool OptNeedResort
(pseudo) used to force a re-sort
Definition globals.c:52
Assorted sorting methods.
EmailSortType
Methods for sorting Emails.
Definition sort.h:53
@ EMAIL_SORT_SCORE
Sort by the email's score.
Definition sort.h:58
Representation of an email.
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:49
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
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:54
bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition globals.c:57
Global variables.
enum CommandResult parse_unscore(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'unscore' command - Implements Command::parse() -.
Definition score.c:166
enum CommandResult parse_score(const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
Parse the 'score' command - Implements Command::parse() -.
Definition score.c:76
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
Convenience wrapper for the gui headers.
#define mutt_using_threads()
Definition thread.h:113
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition index.c:693
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
@ MODULE_ID_EMAIL
ModuleEmail, Email code
Definition module_api.h:64
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:665
Many unsorted constants and some structs.
@ MUTT_READ
Messages that have been read.
Definition mutt.h:92
@ MUTT_FLAG
Flagged messages.
Definition mutt.h:98
@ MUTT_DELETE
Messages to be deleted.
Definition mutt.h:94
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:665
@ NT_SCORE
Email scoring has changed.
Definition notify_type.h:54
Text parsing functions.
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:1151
Match patterns to emails.
#define MUTT_PC_NO_FLAGS
No flags are set.
Definition lib.h:69
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition lib.h:107
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#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:216
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition score.c:243
void score_list_free(struct Score **sp)
Free a list of scoring rules.
Definition score.c:53
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:159
Email private Module data.
Definition module_data.h:32
struct Score * score_list
Linked list of email scoring rules.
Definition module_data.h:44
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:78
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Context for config parsing (history/backtrace)
Definition pcontext.h:34
Detailed error information from config parsing.
Definition perror.h:34
struct Buffer * message
Error message.
Definition perror.h:35
Cache commonly-used patterns.
Definition lib.h:118
Scoring rule for email.
Definition score.h:38
bool exact
If this rule matches, don't evaluate any more.
Definition score.h:42
char * str
String to match.
Definition score.h:39
struct PatternList * pat
Pattern to match.
Definition score.h:40
int val
Score to add.
Definition score.h:41
struct Score * next
Linked list.
Definition score.h:43