NeoMutt  2021-02-05-329-g9e03b7
Teaching an old dog new tricks
DOXYGEN
score.c File Reference

Routines for adding user scores to emails. More...

#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "mutt.h"
#include "score.h"
#include "pattern/lib.h"
#include "context.h"
#include "init.h"
#include "mutt_commands.h"
#include "mutt_globals.h"
#include "options.h"
#include "protos.h"
+ Include dependency graph for score.c:

Go to the source code of this file.

Data Structures

struct  Score
 Scoring rule for email. More...
 

Functions

void mutt_check_rescore (struct Mailbox *m)
 Do the emails need to have their scores recalculated? More...
 
enum CommandResult mutt_parse_score (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'score' command - Implements Command::parse() More...
 
void mutt_score_message (struct Mailbox *m, struct Email *e, bool upd_mbox)
 Apply scoring to an email. More...
 
enum CommandResult mutt_parse_unscore (struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
 Parse the 'unscore' command - Implements Command::parse() More...
 

Variables

static struct ScoreScoreList = NULL
 

Detailed Description

Routines for adding user scores to emails.

Authors
  • Michael R. Elkins

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file score.c.

Function Documentation

◆ mutt_check_rescore()

void mutt_check_rescore ( struct Mailbox m)

Do the emails need to have their scores recalculated?

Parameters
mMailbox

Definition at line 64 of file score.c.

65 {
66  const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
67  if (OptNeedRescore && c_score)
68  {
69  const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
70  const short c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
71  if (((c_sort & SORT_MASK) == SORT_SCORE) || ((c_sort_aux & SORT_MASK) == SORT_SCORE))
72  {
73  OptNeedResort = true;
74  if ((c_sort & SORT_MASK) == SORT_THREADS)
75  OptSortSubthreads = true;
76  }
77 
78  notify_send(m->notify, NT_SCORE, 0, NULL);
79  }
80  OptNeedRescore = false;
81 }
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:71
WHERE bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:43
Sort by the email&#39;s score.
Definition: sort2.h:52
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:42
Container for Accounts, Notifications.
Definition: neomutt.h:36
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:272
Sort by email threads.
Definition: sort2.h:49
Email scoring has changed.
Definition: notify_type.h:49
WHERE bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:54
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:156
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_parse_score()

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 at line 86 of file score.c.

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 PatternList *pat =
116  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(pc, &ptr->val) < 0)
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 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
The "current" mailbox.
Definition: context.h:37
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:252
struct Mailbox * ctx_mailbox(struct Context *ctx)
wrapper to get the mailbox in a Context, or NULL
Definition: context.c:440
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
struct PatternList * pat
Definition: score.c:52
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer&#39;s string.
Definition: buffer.c:432
#define _(a)
Definition: message.h:28
static struct Score * ScoreList
Definition: score.c:58
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:42
#define MoreArgs(buf)
Definition: buffer.h:40
struct Menu * menu
Needed for pattern compilation.
Definition: context.h:45
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
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:395
#define MUTT_PC_NO_FLAGS
No flags are set.
Definition: lib.h:60
char * data
Pointer to data.
Definition: buffer.h:35
Scoring rule for email.
Definition: score.c:49
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
Success: Command worked.
Definition: mutt_commands.h:38
Warning: Help given to the user.
Definition: mutt_commands.h:37
#define FREE(x)
Definition: memory.h:40
struct Score * next
Definition: score.c:55
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:66
+ Here is the call graph for this function:

◆ mutt_score_message()

void mutt_score_message ( struct Mailbox m,
struct Email e,
bool  upd_mbox 
)

Apply scoring to an email.

Parameters
mMailbox
eEmail
upd_mboxIf true, update the Mailbox too

Definition at line 159 of file score.c.

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 =
181  cs_subset_number(NeoMutt->sub, "score_threshold_delete");
182  const short c_score_threshold_flag =
183  cs_subset_number(NeoMutt->sub, "score_threshold_flag");
184  const short c_score_threshold_read =
185  cs_subset_number(NeoMutt->sub, "score_threshold_read");
186 
187  if (e->score <= c_score_threshold_delete)
188  mutt_set_flag_update(m, e, MUTT_DELETE, true, upd_mbox);
189  if (e->score <= c_score_threshold_read)
190  mutt_set_flag_update(m, e, MUTT_READ, true, upd_mbox);
191  if (e->score >= c_score_threshold_flag)
192  mutt_set_flag_update(m, e, MUTT_FLAG, true, upd_mbox);
193 }
struct PatternList * pat
Definition: score.c:52
Flagged messages.
Definition: mutt.h:98
static struct Score * ScoreList
Definition: score.c:58
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:159
Container for Accounts, Notifications.
Definition: neomutt.h:36
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:50
int val
Definition: score.c:53
int score
Message score.
Definition: email.h:89
#define SLIST_FIRST(head)
Definition: queue.h:228
Messages to be deleted.
Definition: mutt.h:94
Messages that have been read.
Definition: mutt.h:92
Scoring rule for email.
Definition: score.c:49
int 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:1089
struct Score * next
Definition: score.c:55
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Cache commonly-used patterns.
Definition: lib.h:105
bool exact
if this rule matches, don&#39;t evaluate any more
Definition: score.c:54
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition: lib.h:95
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_parse_unscore()

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 at line 198 of file score.c.

200 {
201  struct Score *tmp = NULL, *last = NULL;
202 
203  while (MoreArgs(s))
204  {
206  if (mutt_str_equal("*", buf->data))
207  {
208  for (tmp = ScoreList; tmp;)
209  {
210  last = tmp;
211  tmp = tmp->next;
212  mutt_pattern_free(&last->pat);
213  FREE(&last);
214  }
215  ScoreList = NULL;
216  }
217  else
218  {
219  for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
220  {
221  if (mutt_str_equal(buf->data, tmp->str))
222  {
223  if (last)
224  last->next = tmp->next;
225  else
226  ScoreList = tmp->next;
227  mutt_pattern_free(&tmp->pat);
228  FREE(&tmp);
229  /* there should only be one score per pattern, so we can stop here */
230  break;
231  }
232  }
233  }
234  }
235  OptNeedRescore = true;
236  return MUTT_CMD_SUCCESS;
237 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
static struct Score * ScoreList
Definition: score.c:58
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:42
#define MoreArgs(buf)
Definition: buffer.h:40
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:395
char * data
Pointer to data.
Definition: buffer.h:35
Scoring rule for email.
Definition: score.c:49
Success: Command worked.
Definition: mutt_commands.h:38
#define FREE(x)
Definition: memory.h:40
struct Score * next
Definition: score.c:55
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:1038
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:66
+ Here is the call graph for this function:

Variable Documentation

◆ ScoreList

struct Score* ScoreList = NULL
static

Definition at line 58 of file score.c.