NeoMutt  2024-10-02-37-gfa9146
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 SortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
73 const enum SortType c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
74 if (((c_sort & SORT_MASK) == SORT_SCORE) || ((c_sort_aux & SORT_MASK) == SORT_SCORE))
75 {
76 OptNeedResort = true;
78 OptSortSubthreads = true;
79 }
80
81 mutt_debug(LL_NOTIFY, "NT_SCORE: %p\n", (void *) m);
82 notify_send(m->notify, NT_SCORE, 0, NULL);
83 }
84 OptNeedRescore = false;
85}
86
90enum CommandResult mutt_parse_score(struct Buffer *buf, struct Buffer *s,
91 intptr_t data, struct Buffer *err)
92{
93 struct Score *ptr = NULL, *last = NULL;
94 char *pattern = NULL, *pc = NULL;
95
97 if (!MoreArgs(s))
98 {
99 buf_printf(err, _("%s: too few arguments"), "score");
100 return MUTT_CMD_WARNING;
101 }
102 pattern = buf_strdup(buf);
104 if (MoreArgs(s))
105 {
106 FREE(&pattern);
107 buf_printf(err, _("%s: too many arguments"), "score");
108 return MUTT_CMD_WARNING;
109 }
110
111 /* look for an existing entry and update the value, else add it to the end
112 * of the list */
113 for (ptr = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
114 if (mutt_str_equal(pattern, ptr->str))
115 break;
116
117 if (ptr)
118 {
119 /* 'buf' arg was cleared and 'pattern' holds the only reference;
120 * as here 'ptr' != NULL -> update the value only in which case
121 * ptr->str already has the string, so pattern should be freed. */
122 FREE(&pattern);
123 }
124 else
125 {
126 struct MailboxView *mv_cur = get_current_mailbox_view();
127 struct Menu *menu = get_current_menu();
128 struct PatternList *pat = mutt_pattern_comp(mv_cur, menu, pattern, MUTT_PC_NO_FLAGS, err);
129 if (!pat)
130 {
131 FREE(&pattern);
132 return MUTT_CMD_ERROR;
133 }
134 ptr = mutt_mem_calloc(1, sizeof(struct Score));
135 if (last)
136 last->next = ptr;
137 else
138 ScoreList = ptr;
139 ptr->pat = pat;
140 ptr->str = pattern;
141 }
142 pc = buf->data;
143 if (*pc == '=')
144 {
145 ptr->exact = true;
146 pc++;
147 }
148 if (!mutt_str_atoi_full(pc, &ptr->val))
149 {
150 FREE(&pattern);
151 buf_strcpy(err, _("Error: score: invalid number"));
152 return MUTT_CMD_ERROR;
153 }
154 OptNeedRescore = true;
155 return MUTT_CMD_SUCCESS;
156}
157
164void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
165{
166 struct Score *tmp = NULL;
167 struct PatternCache cache = { 0 };
168
169 e->score = 0; /* in case of re-scoring */
170 for (tmp = ScoreList; tmp; tmp = tmp->next)
171 {
172 if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
173 {
174 if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
175 {
176 e->score = tmp->val;
177 break;
178 }
179 e->score += tmp->val;
180 }
181 }
182 if (e->score < 0)
183 e->score = 0;
184
185 const short c_score_threshold_delete = cs_subset_number(NeoMutt->sub, "score_threshold_delete");
186 const short c_score_threshold_flag = cs_subset_number(NeoMutt->sub, "score_threshold_flag");
187 const short c_score_threshold_read = cs_subset_number(NeoMutt->sub, "score_threshold_read");
188
189 if (e->score <= c_score_threshold_delete)
190 mutt_set_flag(m, e, MUTT_DELETE, true, upd_mbox);
191 if (e->score <= c_score_threshold_read)
192 mutt_set_flag(m, e, MUTT_READ, true, upd_mbox);
193 if (e->score >= c_score_threshold_flag)
194 mutt_set_flag(m, e, MUTT_FLAG, true, upd_mbox);
195}
196
200enum CommandResult mutt_parse_unscore(struct Buffer *buf, struct Buffer *s,
201 intptr_t data, struct Buffer *err)
202{
203 struct Score *tmp = NULL, *last = NULL;
204
205 while (MoreArgs(s))
206 {
208 if (mutt_str_equal("*", buf->data))
209 {
210 for (tmp = ScoreList; tmp;)
211 {
212 last = tmp;
213 tmp = tmp->next;
214 mutt_pattern_free(&last->pat);
215 FREE(&last);
216 }
217 ScoreList = NULL;
218 }
219 else
220 {
221 for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
222 {
223 if (mutt_str_equal(buf->data, tmp->str))
224 {
225 if (last)
226 last->next = tmp->next;
227 else
228 ScoreList = tmp->next;
229 mutt_pattern_free(&tmp->pat);
230 FREE(&tmp);
231 /* there should only be one score per pattern, so we can stop here */
232 break;
233 }
234 }
235 }
236 }
237 OptNeedRescore = true;
238 return MUTT_CMD_SUCCESS;
239}
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.
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:1149
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 OptNeedRescore
(pseudo) set when the 'score' command is used
Definition: globals.c:65
bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: globals.c:72
bool OptNeedResort
(pseudo) used to force a re-sort
Definition: globals.c:66
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:200
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:90
#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
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
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:67
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition: lib.h:105
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: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:164
Routines for adding user scores to emails.
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:70
SortType
Methods for sorting.
Definition: sort2.h:34
@ SORT_SCORE
Sort by the email's score.
Definition: sort2.h:44
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:116
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