NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
score.c
Go to the documentation of this file.
1
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
91enum CommandResult mutt_parse_score(struct Buffer *buf, struct Buffer *s,
92 intptr_t data, struct Buffer *err)
93{
94 struct Score *ptr = NULL, *last = NULL;
95 char *pattern = NULL, *pc = NULL;
96
98 if (!MoreArgs(s))
99 {
100 buf_printf(err, _("%s: too few arguments"), "score");
101 return MUTT_CMD_WARNING;
102 }
103 pattern = buf_strdup(buf);
105 if (MoreArgs(s))
106 {
107 FREE(&pattern);
108 buf_printf(err, _("%s: too many arguments"), "score");
109 return MUTT_CMD_WARNING;
110 }
111
112 /* look for an existing entry and update the value, else add it to the end
113 * of the list */
114 for (ptr = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
115 if (mutt_str_equal(pattern, ptr->str))
116 break;
117
118 if (ptr)
119 {
120 /* 'buf' arg was cleared and 'pattern' holds the only reference;
121 * as here 'ptr' != NULL -> update the value only in which case
122 * ptr->str already has the string, so pattern should be freed. */
123 FREE(&pattern);
124 }
125 else
126 {
127 struct MailboxView *mv_cur = get_current_mailbox_view();
128 struct Menu *menu = get_current_menu();
129 struct PatternList *pat = mutt_pattern_comp(mv_cur, menu, pattern, MUTT_PC_NO_FLAGS, err);
130 if (!pat)
131 {
132 FREE(&pattern);
133 return MUTT_CMD_ERROR;
134 }
135 ptr = MUTT_MEM_CALLOC(1, struct Score);
136 if (last)
137 last->next = ptr;
138 else
139 ScoreList = ptr;
140 ptr->pat = pat;
141 ptr->str = pattern;
142 }
143 pc = buf->data;
144 if (*pc == '=')
145 {
146 ptr->exact = true;
147 pc++;
148 }
149 if (!mutt_str_atoi_full(pc, &ptr->val))
150 {
151 FREE(&pattern);
152 buf_strcpy(err, _("Error: score: invalid number"));
153 return MUTT_CMD_ERROR;
154 }
155 OptNeedRescore = true;
156 return MUTT_CMD_SUCCESS;
157}
158
165void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
166{
167 struct Score *tmp = NULL;
168 struct PatternCache cache = { 0 };
169
170 e->score = 0; /* in case of re-scoring */
171 for (tmp = ScoreList; tmp; tmp = tmp->next)
172 {
173 if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
174 {
175 if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
176 {
177 e->score = tmp->val;
178 break;
179 }
180 e->score += tmp->val;
181 }
182 }
183 if (e->score < 0)
184 e->score = 0;
185
186 const short c_score_threshold_delete = cs_subset_number(NeoMutt->sub, "score_threshold_delete");
187 const short c_score_threshold_flag = cs_subset_number(NeoMutt->sub, "score_threshold_flag");
188 const short c_score_threshold_read = cs_subset_number(NeoMutt->sub, "score_threshold_read");
189
190 if (e->score <= c_score_threshold_delete)
191 mutt_set_flag(m, e, MUTT_DELETE, true, upd_mbox);
192 if (e->score <= c_score_threshold_read)
193 mutt_set_flag(m, e, MUTT_READ, true, upd_mbox);
194 if (e->score >= c_score_threshold_flag)
195 mutt_set_flag(m, e, MUTT_FLAG, true, upd_mbox);
196}
197
201enum CommandResult mutt_parse_unscore(struct Buffer *buf, struct Buffer *s,
202 intptr_t data, struct Buffer *err)
203{
204 struct Score *tmp = NULL, *last = NULL;
205
206 while (MoreArgs(s))
207 {
209 if (mutt_str_equal("*", buf->data))
210 {
211 for (tmp = ScoreList; tmp;)
212 {
213 last = tmp;
214 tmp = tmp->next;
215 mutt_pattern_free(&last->pat);
216 FREE(&last);
217 }
218 ScoreList = NULL;
219 }
220 else
221 {
222 for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
223 {
224 if (mutt_str_equal(buf->data, tmp->str))
225 {
226 if (last)
227 last->next = tmp->next;
228 else
229 ScoreList = tmp->next;
230 mutt_pattern_free(&tmp->pat);
231 FREE(&tmp);
232 /* there should only be one score per pattern, so we can stop here */
233 break;
234 }
235 }
236 }
237 }
238 OptNeedRescore = true;
239 return MUTT_CMD_SUCCESS;
240}
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
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, struct Menu *menu, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition: compile.c:906
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:778
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:65
bool OptNeedResort
(pseudo) used to force a re-sort
Definition: globals.c:66
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:1147
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:50
#define MoreArgs(buf)
Definition: extract.h:32
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
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:57
bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: globals.c:72
Global variables.
enum CommandResult mutt_parse_unscore(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'unscore' command - Implements Command::parse() -.
Definition: score.c:201
enum CommandResult mutt_parse_score(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'score' command - Implements Command::parse() -.
Definition: score.c:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition: index.c:683
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:731
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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()
Definition: mutt_thread.h:114
@ 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
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:165
Routines for adding user scores to emails.
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
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
Definition: lib.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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