NeoMutt  2021-10-29-220-g2b1eec
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 "context.h"
41 #include "init.h"
42 #include "mutt_globals.h"
43 #include "mutt_thread.h"
44 #include "options.h"
45 #include "protos.h"
46 
50 struct Score
51 {
52  char *str;
53  struct PatternList *pat;
54  int val;
55  bool exact;
56  struct Score *next;
57 };
58 
59 static struct Score *ScoreList = NULL;
60 
65 void mutt_check_rescore(struct Mailbox *m)
66 {
67  const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
68  if (OptNeedRescore && c_score)
69  {
70  const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
71  const short c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
72  if (((c_sort & SORT_MASK) == SORT_SCORE) || ((c_sort_aux & SORT_MASK) == SORT_SCORE))
73  {
74  OptNeedResort = true;
75  if (mutt_using_threads())
76  OptSortSubthreads = true;
77  }
78 
79  mutt_debug(LL_NOTIFY, "NT_SCORE: %p\n", m);
80  notify_send(m->notify, NT_SCORE, 0, NULL);
81  }
82  OptNeedRescore = false;
83 }
84 
88 enum CommandResult mutt_parse_score(struct Buffer *buf, struct Buffer *s,
89  intptr_t data, struct Buffer *err)
90 {
91  struct Score *ptr = NULL, *last = NULL;
92  char *pattern = NULL, *pc = NULL;
93 
95  if (!MoreArgs(s))
96  {
97  mutt_buffer_printf(err, _("%s: too few arguments"), "score");
98  return MUTT_CMD_WARNING;
99  }
100  pattern = mutt_buffer_strdup(buf);
102  if (MoreArgs(s))
103  {
104  FREE(&pattern);
105  mutt_buffer_printf(err, _("%s: too many arguments"), "score");
106  return MUTT_CMD_WARNING;
107  }
108 
109  /* look for an existing entry and update the value, else add it to the end
110  * of the list */
111  for (ptr = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
112  if (mutt_str_equal(pattern, ptr->str))
113  break;
114  if (!ptr)
115  {
116  struct Mailbox *m_cur = get_current_mailbox();
117  struct Menu *menu = get_current_menu();
118  struct PatternList *pat =
119  mutt_pattern_comp(m_cur, menu, pattern, MUTT_PC_NO_FLAGS, err);
120  if (!pat)
121  {
122  FREE(&pattern);
123  return MUTT_CMD_ERROR;
124  }
125  ptr = mutt_mem_calloc(1, sizeof(struct Score));
126  if (last)
127  last->next = ptr;
128  else
129  ScoreList = ptr;
130  ptr->pat = pat;
131  ptr->str = pattern;
132  }
133  else
134  {
135  /* 'buf' arg was cleared and 'pattern' holds the only reference;
136  * as here 'ptr' != NULL -> update the value only in which case
137  * ptr->str already has the string, so pattern should be freed. */
138  FREE(&pattern);
139  }
140  pc = buf->data;
141  if (*pc == '=')
142  {
143  ptr->exact = true;
144  pc++;
145  }
146  if (!mutt_str_atoi_full(pc, &ptr->val))
147  {
148  FREE(&pattern);
149  mutt_buffer_strcpy(err, _("Error: score: invalid number"));
150  return MUTT_CMD_ERROR;
151  }
152  OptNeedRescore = true;
153  return MUTT_CMD_SUCCESS;
154 }
155 
162 void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
163 {
164  struct Score *tmp = NULL;
165  struct PatternCache cache = { 0 };
166 
167  e->score = 0; /* in case of re-scoring */
168  for (tmp = ScoreList; tmp; tmp = tmp->next)
169  {
170  if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
171  {
172  if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
173  {
174  e->score = tmp->val;
175  break;
176  }
177  e->score += tmp->val;
178  }
179  }
180  if (e->score < 0)
181  e->score = 0;
182 
183  const short c_score_threshold_delete =
184  cs_subset_number(NeoMutt->sub, "score_threshold_delete");
185  const short c_score_threshold_flag =
186  cs_subset_number(NeoMutt->sub, "score_threshold_flag");
187  const short c_score_threshold_read =
188  cs_subset_number(NeoMutt->sub, "score_threshold_read");
189 
190  if (e->score <= c_score_threshold_delete)
191  mutt_set_flag_update(m, e, MUTT_DELETE, true, upd_mbox);
192  if (e->score <= c_score_threshold_read)
193  mutt_set_flag_update(m, e, MUTT_READ, true, upd_mbox);
194  if (e->score >= c_score_threshold_flag)
195  mutt_set_flag_update(m, e, MUTT_FLAG, true, upd_mbox);
196 }
197 
201 enum 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 }
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:432
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#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:1092
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1038
Convenience wrapper for the config headers.
The "currently-open" mailbox.
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:1108
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:51
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:88
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
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
GUI manage the main index (list of emails)
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:500
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:474
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:399
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: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:171
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:715
Many unsorted constants and some structs.
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:66
@ 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
Hundreds of global variables to back the user variables.
Create/manipulate threading in emails.
#define mutt_using_threads()
Definition: mutt_thread.h:100
@ NT_SCORE
Email scoring has changed.
Definition: notify_type.h:51
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:60
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition: lib.h:95
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:65
static struct Score * ScoreList
Definition: score.c:59
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:162
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:82
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:148
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:106
Scoring rule for email.
Definition: score.c:51
bool exact
if this rule matches, don't evaluate any more
Definition: score.c:55
char * str
Definition: score.c:52
struct PatternList * pat
Definition: score.c:53
int val
Definition: score.c:54
struct Score * next
Definition: score.c:56