NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
score.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stdlib.h>
32#include "mutt/lib.h"
33#include "config/lib.h"
34#include "email/lib.h"
35#include "core/lib.h"
36#include "mutt.h"
37#include "score.h"
38#include "index/lib.h"
39#include "pattern/lib.h"
40#include "init.h"
41#include "mutt_thread.h"
42#include "options.h"
43#include "protos.h"
44
48struct Score
49{
50 char *str;
51 struct PatternList *pat;
52 int val;
53 bool exact;
54 struct Score *next;
55};
56
57static struct Score *ScoreList = NULL;
58
64{
65 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
66 if (OptNeedRescore && c_score)
67 {
68 const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
69 const short c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
70 if (((c_sort & SORT_MASK) == SORT_SCORE) || ((c_sort_aux & SORT_MASK) == SORT_SCORE))
71 {
72 OptNeedResort = true;
74 OptSortSubthreads = true;
75 }
76
77 mutt_debug(LL_NOTIFY, "NT_SCORE: %p\n", m);
78 notify_send(m->notify, NT_SCORE, 0, NULL);
79 }
80 OptNeedRescore = false;
81}
82
86enum CommandResult mutt_parse_score(struct Buffer *buf, struct Buffer *s,
87 intptr_t data, struct Buffer *err)
88{
89 struct Score *ptr = NULL, *last = NULL;
90 char *pattern = NULL, *pc = NULL;
91
93 if (!MoreArgs(s))
94 {
95 mutt_buffer_printf(err, _("%s: too few arguments"), "score");
96 return MUTT_CMD_WARNING;
97 }
98 pattern = mutt_buffer_strdup(buf);
100 if (MoreArgs(s))
101 {
102 FREE(&pattern);
103 mutt_buffer_printf(err, _("%s: too many arguments"), "score");
104 return MUTT_CMD_WARNING;
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 = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
110 if (mutt_str_equal(pattern, ptr->str))
111 break;
112 if (!ptr)
113 {
114 struct Mailbox *m_cur = get_current_mailbox();
115 struct Menu *menu = get_current_menu();
116 struct PatternList *pat = mutt_pattern_comp(m_cur, menu, pattern, MUTT_PC_NO_FLAGS, err);
117 if (!pat)
118 {
119 FREE(&pattern);
120 return MUTT_CMD_ERROR;
121 }
122 ptr = mutt_mem_calloc(1, sizeof(struct Score));
123 if (last)
124 last->next = ptr;
125 else
126 ScoreList = ptr;
127 ptr->pat = pat;
128 ptr->str = pattern;
129 }
130 else
131 {
132 /* 'buf' arg was cleared and 'pattern' holds the only reference;
133 * as here 'ptr' != NULL -> update the value only in which case
134 * ptr->str already has the string, so pattern should be freed. */
135 FREE(&pattern);
136 }
137 pc = buf->data;
138 if (*pc == '=')
139 {
140 ptr->exact = true;
141 pc++;
142 }
143 if (!mutt_str_atoi_full(pc, &ptr->val))
144 {
145 FREE(&pattern);
146 mutt_buffer_strcpy(err, _("Error: score: invalid number"));
147 return MUTT_CMD_ERROR;
148 }
149 OptNeedRescore = true;
150 return MUTT_CMD_SUCCESS;
151}
152
159void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
160{
161 struct Score *tmp = NULL;
162 struct PatternCache cache = { 0 };
163
164 e->score = 0; /* in case of re-scoring */
165 for (tmp = ScoreList; tmp; tmp = tmp->next)
166 {
167 if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
168 {
169 if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
170 {
171 e->score = tmp->val;
172 break;
173 }
174 e->score += tmp->val;
175 }
176 }
177 if (e->score < 0)
178 e->score = 0;
179
180 const short c_score_threshold_delete = cs_subset_number(NeoMutt->sub, "score_threshold_delete");
181 const short c_score_threshold_flag = cs_subset_number(NeoMutt->sub, "score_threshold_flag");
182 const short c_score_threshold_read = cs_subset_number(NeoMutt->sub, "score_threshold_read");
183
184 if (e->score <= c_score_threshold_delete)
185 mutt_set_flag_update(m, e, MUTT_DELETE, true, upd_mbox);
186 if (e->score <= c_score_threshold_read)
187 mutt_set_flag_update(m, e, MUTT_READ, true, upd_mbox);
188 if (e->score >= c_score_threshold_flag)
189 mutt_set_flag_update(m, e, MUTT_FLAG, true, upd_mbox);
190}
191
195enum CommandResult mutt_parse_unscore(struct Buffer *buf, struct Buffer *s,
196 intptr_t data, struct Buffer *err)
197{
198 struct Score *tmp = NULL, *last = NULL;
199
200 while (MoreArgs(s))
201 {
203 if (mutt_str_equal("*", buf->data))
204 {
205 for (tmp = ScoreList; tmp;)
206 {
207 last = tmp;
208 tmp = tmp->next;
209 mutt_pattern_free(&last->pat);
210 FREE(&last);
211 }
212 ScoreList = NULL;
213 }
214 else
215 {
216 for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
217 {
218 if (mutt_str_equal(buf->data, tmp->str))
219 {
220 if (last)
221 last->next = tmp->next;
222 else
223 ScoreList = tmp->next;
224 mutt_pattern_free(&tmp->pat);
225 FREE(&tmp);
226 /* there should only be one score per pattern, so we can stop here */
227 break;
228 }
229 }
230 }
231 }
232 OptNeedRescore = true;
233 return MUTT_CMD_SUCCESS;
234}
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:327
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:447
#define MoreArgs(buf)
Definition: buffer.h:40
CommandResult
Error codes for command_t parse functions.
Definition: command.h:34
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:37
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:35
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:36
struct PatternList * mutt_pattern_comp(struct Mailbox *m, struct Menu *menu, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition: compile.c:1160
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1038
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
Structs that make up an email.
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:1107
void mutt_set_flag_update(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition: flags.c:52
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:195
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:86
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
GUI manage the main index (list of emails)
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:618
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:644
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:273
Config/command parsing.
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
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:171
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
Many unsorted constants and some structs.
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:67
@ MUTT_READ
Messages that have been read.
Definition: mutt.h:93
@ MUTT_FLAG
Flagged messages.
Definition: mutt.h:99
@ MUTT_DELETE
Messages to be deleted.
Definition: mutt.h:95
Create/manipulate threading in emails.
#define mutt_using_threads()
Definition: mutt_thread.h:100
@ NT_SCORE
Email scoring has changed.
Definition: notify_type.h:53
Handling of global boolean variables.
bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition: options.h:47
bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:59
bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:48
Match patterns to emails.
#define MUTT_PC_NO_FLAGS
No flags are set.
Definition: lib.h:61
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition: lib.h:99
Prototypes for many functions.
#define SLIST_FIRST(head)
Definition: queue.h:229
void mutt_check_rescore(struct Mailbox *m)
Do the emails need to have their scores recalculated?
Definition: score.c:63
static struct Score * ScoreList
Definition: score.c:57
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:159
Routines for adding user scores to emails.
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
@ SORT_SCORE
Sort by the email's score.
Definition: sort2.h:52
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
The envelope/body of an email.
Definition: email.h:37
int score
Message score.
Definition: email.h:113
A mailbox.
Definition: mailbox.h:79
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
Definition: lib.h:69
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Cache commonly-used patterns.
Definition: lib.h:110
Scoring rule for email.
Definition: score.c:49
bool exact
if this rule matches, don't evaluate any more
Definition: score.c:53
char * str
Definition: score.c:50
struct PatternList * pat
Definition: score.c:51
int val
Definition: score.c:52
struct Score * next
Definition: score.c:54