NeoMutt  2018-07-16 +952-a2da0a
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/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "mutt.h"
#include "score.h"
#include "context.h"
#include "globals.h"
#include "keymap.h"
#include "mailbox.h"
#include "menu.h"
#include "options.h"
#include "pattern.h"
#include "protos.h"
#include "sort.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...
 
int mutt_parse_score (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
 Parse the 'score' command - Implements command_t. More...
 
void mutt_score_message (struct Mailbox *m, struct Email *e, bool upd_mbox)
 Apply scoring to an email. More...
 
int mutt_parse_unscore (struct Buffer *buf, struct Buffer *s, unsigned long data, struct Buffer *err)
 Parse the 'unscore' command - Implements command_t. More...
 

Variables

short ScoreThresholdDelete
 Config: Messages with a lower score will be automatically deleted. More...
 
short ScoreThresholdFlag
 Config: Messages with a greater score will be automatically flagged. More...
 
short ScoreThresholdRead
 Config: Messages with a lower score will be automatically marked read. More...
 
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

void mutt_check_rescore ( struct Mailbox m)

Do the emails need to have their scores recalculated?

Parameters
mMailbox

Definition at line 70 of file score.c.

71 {
72  if (OptNeedRescore && Score)
73  {
74  if ((Sort & SORT_MASK) == SORT_SCORE || (SortAux & SORT_MASK) == SORT_SCORE)
75  {
76  OptNeedResort = true;
77  if ((Sort & SORT_MASK) == SORT_THREADS)
78  OptSortSubthreads = true;
79  }
80 
81  /* must redraw the index since the user might have %N in it */
84 
85  for (int i = 0; m && i < m->msg_count; i++)
86  {
87  mutt_score_message(m, m->hdrs[i], true);
88  m->hdrs[i]->pair = 0;
89  }
90  }
91  OptNeedRescore = false;
92 }
int msg_count
total number of messages
Definition: mailbox.h:86
WHERE bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:40
#define SORT_SCORE
Sort by the email&#39;s score.
Definition: sort.h:54
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:171
WHERE bool OptNeedRescore
(pseudo) set when the `score&#39; command is used
Definition: options.h:39
struct Email ** hdrs
Definition: mailbox.h:93
#define SORT_THREADS
Sort by email threads.
Definition: sort.h:51
Scoring rule for email.
Definition: score.c:55
WHERE short Sort
Config: Sort method for the index.
Definition: sort.h:58
WHERE bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:51
WHERE short SortAux
Config: Secondary sort method for the index.
Definition: sort.h:59
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:77
Pager pager (email viewer)
Definition: keymap.h:74
int pair
color-pair to use when displaying in the index
Definition: email.h:81
Index panel (list of emails)
Definition: keymap.h:73

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_parse_score ( struct Buffer buf,
struct Buffer s,
unsigned long  data,
struct Buffer err 
)

Parse the 'score' command - Implements command_t.

Definition at line 97 of file score.c.

99 {
100  struct Score *ptr = NULL, *last = NULL;
101  char *pattern = NULL, *pc = NULL;
102  struct Pattern *pat = NULL;
103 
104  mutt_extract_token(buf, s, 0);
105  if (!MoreArgs(s))
106  {
107  mutt_buffer_printf(err, _("%s: too few arguments"), "score");
108  return -1;
109  }
110  pattern = buf->data;
111  mutt_buffer_init(buf);
112  mutt_extract_token(buf, s, 0);
113  if (MoreArgs(s))
114  {
115  FREE(&pattern);
116  mutt_buffer_printf(err, _("%s: too many arguments"), "score");
117  return -1;
118  }
119 
120  /* look for an existing entry and update the value, else add it to the end
121  of the list */
122  for (ptr = ScoreList, last = NULL; ptr; last = ptr, ptr = ptr->next)
123  if (mutt_str_strcmp(pattern, ptr->str) == 0)
124  break;
125  if (!ptr)
126  {
127  pat = mutt_pattern_comp(pattern, 0, err);
128  if (!pat)
129  {
130  FREE(&pattern);
131  return -1;
132  }
133  ptr = mutt_mem_calloc(1, sizeof(struct Score));
134  if (last)
135  last->next = ptr;
136  else
137  ScoreList = ptr;
138  ptr->pat = pat;
139  ptr->str = pattern;
140  }
141  else
142  {
143  /* 'buf' arg was cleared and 'pattern' holds the only reference;
144  * as here 'ptr' != NULL -> update the value only in which case
145  * ptr->str already has the string, so pattern should be freed.
146  */
147  FREE(&pattern);
148  }
149  pc = buf->data;
150  if (*pc == '=')
151  {
152  ptr->exact = 1;
153  pc++;
154  }
155  if (mutt_str_atoi(pc, &ptr->val) < 0)
156  {
157  FREE(&pattern);
158  mutt_buffer_strcpy(err, _("Error: score: invalid number"));
159  return -1;
160  }
161  OptNeedRescore = true;
162  return 0;
163 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:262
#define _(a)
Definition: message.h:28
static struct Score * ScoreList
Definition: score.c:64
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:200
WHERE bool OptNeedRescore
(pseudo) set when the `score&#39; command is used
Definition: options.h:39
A simple (non-regex) pattern.
Definition: pattern.h:44
#define MoreArgs(buf)
Definition: buffer.h:44
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, int flags)
Extract one token from a string.
Definition: init.c:2520
void mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:316
char * data
pointer to data
Definition: buffer.h:35
Scoring rule for email.
Definition: score.c:55
struct Pattern * mutt_pattern_comp(char *s, int flags, struct Buffer *err)
Create a Pattern.
Definition: pattern.c:1289
char * str
Definition: pattern.h:60
#define FREE(x)
Definition: memory.h:46
struct Score * next
Definition: score.c:61
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:66
struct Pattern * pat
Definition: score.c:58
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:612

+ Here is the call graph for this function:

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 Context too

Definition at line 171 of file score.c.

172 {
173  struct Score *tmp = NULL;
174  struct PatternCache cache = { 0 };
175 
176  e->score = 0; /* in case of re-scoring */
177  for (tmp = ScoreList; tmp; tmp = tmp->next)
178  {
179  if (mutt_pattern_exec(tmp->pat, MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
180  {
181  if (tmp->exact || tmp->val == 9999 || tmp->val == -9999)
182  {
183  e->score = tmp->val;
184  break;
185  }
186  e->score += tmp->val;
187  }
188  }
189  if (e->score < 0)
190  e->score = 0;
191 
192  if (e->score <= ScoreThresholdDelete)
193  mutt_set_flag_update(m, e, MUTT_DELETE, true, upd_mbox);
194  if (e->score <= ScoreThresholdRead)
195  mutt_set_flag_update(m, e, MUTT_READ, true, upd_mbox);
196  if (e->score >= ScoreThresholdFlag)
197  mutt_set_flag_update(m, e, MUTT_FLAG, true, upd_mbox);
198 }
Messages to be deleted.
Definition: mutt.h:118
Match the full address.
Definition: pattern.h:69
static struct Score * ScoreList
Definition: score.c:64
short ScoreThresholdRead
Config: Messages with a lower score will be automatically marked read.
Definition: score.c:50
int val
Definition: score.c:59
int score
Definition: email.h:90
short ScoreThresholdDelete
Config: Messages with a lower score will be automatically deleted.
Definition: score.c:48
Scoring rule for email.
Definition: score.c:55
Messages that have been read.
Definition: mutt.h:116
int exact
if this rule matches, don&#39;t evaluate any more
Definition: score.c:60
struct Score * next
Definition: score.c:61
Flagged messages.
Definition: mutt.h:122
Cache commonly-used patterns.
Definition: pattern.h:80
int mutt_pattern_exec(struct Pattern *pat, enum PatternExecFlag flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition: pattern.c:1837
struct Pattern * pat
Definition: score.c:58
void mutt_set_flag_update(struct Mailbox *m, struct Email *e, int flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition: flags.c:56
short ScoreThresholdFlag
Config: Messages with a greater score will be automatically flagged.
Definition: score.c:49

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_parse_unscore ( struct Buffer buf,
struct Buffer s,
unsigned long  data,
struct Buffer err 
)

Parse the 'unscore' command - Implements command_t.

Definition at line 203 of file score.c.

205 {
206  struct Score *tmp = NULL, *last = NULL;
207 
208  while (MoreArgs(s))
209  {
210  mutt_extract_token(buf, s, 0);
211  if (mutt_str_strcmp("*", buf->data) == 0)
212  {
213  for (tmp = ScoreList; tmp;)
214  {
215  last = tmp;
216  tmp = tmp->next;
217  mutt_pattern_free(&last->pat);
218  FREE(&last);
219  }
220  ScoreList = NULL;
221  }
222  else
223  {
224  for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
225  {
226  if (mutt_str_strcmp(buf->data, tmp->str) == 0)
227  {
228  if (last)
229  last->next = tmp->next;
230  else
231  ScoreList = tmp->next;
232  mutt_pattern_free(&tmp->pat);
233  FREE(&tmp);
234  /* there should only be one score per pattern, so we can stop here */
235  break;
236  }
237  }
238  }
239  }
240  OptNeedRescore = true;
241  return 0;
242 }
void mutt_pattern_free(struct Pattern **pat)
Free a Pattern.
Definition: pattern.c:1248
static struct Score * ScoreList
Definition: score.c:64
WHERE bool OptNeedRescore
(pseudo) set when the `score&#39; command is used
Definition: options.h:39
#define MoreArgs(buf)
Definition: buffer.h:44
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, int flags)
Extract one token from a string.
Definition: init.c:2520
char * data
pointer to data
Definition: buffer.h:35
Scoring rule for email.
Definition: score.c:55
#define FREE(x)
Definition: memory.h:46
struct Score * next
Definition: score.c:61
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:612

+ Here is the call graph for this function:

Variable Documentation

short ScoreThresholdDelete

Config: Messages with a lower score will be automatically deleted.

Definition at line 48 of file score.c.

short ScoreThresholdFlag

Config: Messages with a greater score will be automatically flagged.

Definition at line 49 of file score.c.

short ScoreThresholdRead

Config: Messages with a lower score will be automatically marked read.

Definition at line 50 of file score.c.

struct Score* ScoreList = NULL
static

Definition at line 64 of file score.c.