NeoMutt  2018-07-16 +2225-8687db
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 "core/lib.h"
#include "globals.h"
#include "keymap.h"
#include "mutt_commands.h"
#include "mutt_menu.h"
#include "options.h"
#include "pattern.h"
#include "protos.h"
#include "sort.h"

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, 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...
 
enum CommandResult 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 C_ScoreThresholdDelete
 Config: Messages with a lower score will be automatically deleted. More...
 
short C_ScoreThresholdFlag
 Config: Messages with a greater score will be automatically flagged. More...
 
short C_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 71 of file score.c.

72 {
73  if (OptNeedRescore && C_Score)
74  {
75  if (((C_Sort & SORT_MASK) == SORT_SCORE) || ((C_SortAux & SORT_MASK) == SORT_SCORE))
76  {
77  OptNeedResort = true;
78  if ((C_Sort & SORT_MASK) == SORT_THREADS)
79  OptSortSubthreads = true;
80  }
81 
82  /* must redraw the index since the user might have %N in it */
85 
86  for (int i = 0; m && i < m->msg_count; i++)
87  {
88  mutt_score_message(m, m->emails[i], true);
89  m->emails[i]->pair = 0;
90  }
91  }
92  OptNeedRescore = false;
93 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:111
int msg_count
Total number of messages.
Definition: mailbox.h:103
WHERE bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:40
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:170
Index panel (list of emails)
Definition: keymap.h:76
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:39
Pager pager (email viewer)
Definition: keymap.h:77
WHERE short C_Sort
Config: Sort method for the index.
Definition: sort.h:59
WHERE short C_SortAux
Config: Secondary sort method for the index.
Definition: sort.h:60
WHERE bool C_Score
Config: Use message scoring.
Definition: globals.h:248
WHERE bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:51
Sort by email threads.
Definition: sort.h:56
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:85
int pair
Color-pair to use when displaying in the index.
Definition: email.h:79
Sort by the email&#39;s score.
Definition: sort.h:59
enum CommandResult 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 98 of file score.c.

100 {
101  struct Score *ptr = NULL, *last = NULL;
102  char *pattern = NULL, *pc = NULL;
103 
105  if (!MoreArgs(s))
106  {
107  mutt_buffer_printf(err, _("%s: too few arguments"), "score");
108  return MUTT_CMD_WARNING;
109  }
110  pattern = buf->data;
111  mutt_buffer_init(buf);
113  if (MoreArgs(s))
114  {
115  FREE(&pattern);
116  mutt_buffer_printf(err, _("%s: too many arguments"), "score");
117  return MUTT_CMD_WARNING;
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  struct PatternHead *pat = mutt_pattern_comp(pattern, 0, err);
128  if (!pat)
129  {
130  FREE(&pattern);
131  return MUTT_CMD_ERROR;
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  FREE(&pattern);
147  }
148  pc = buf->data;
149  if (*pc == '=')
150  {
151  ptr->exact = true;
152  pc++;
153  }
154  if (mutt_str_atoi(pc, &ptr->val) < 0)
155  {
156  FREE(&pattern);
157  mutt_buffer_strcpy(err, _("Error: score: invalid number"));
158  return MUTT_CMD_ERROR;
159  }
160  OptNeedRescore = true;
161  return MUTT_CMD_SUCCESS;
162 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct PatternHead * pat
Definition: score.c:59
int mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: string.c:262
Error: Can&#39;t help the user.
Definition: mutt_commands.h:33
struct PatternHead * mutt_pattern_comp(const char *s, int flags, struct Buffer *err)
Create a Pattern.
Definition: pattern.c:1389
#define _(a)
Definition: message.h:28
static struct Score * ScoreList
Definition: score.c:65
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:191
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:39
#define MoreArgs(buf)
Definition: buffer.h:44
void mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:314
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:2614
char * data
Pointer to data.
Definition: buffer.h:35
Scoring rule for email.
Definition: score.c:56
Success: Command worked.
Definition: mutt_commands.h:35
Warning: Help given to the user.
Definition: mutt_commands.h:34
#define FREE(x)
Definition: memory.h:40
struct Score * next
Definition: score.c:62
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:61
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:76
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 170 of file score.c.

171 {
172  struct Score *tmp = NULL;
173  struct PatternCache cache = { 0 };
174 
175  e->score = 0; /* in case of re-scoring */
176  for (tmp = ScoreList; tmp; tmp = tmp->next)
177  {
178  if (mutt_pattern_exec(SLIST_FIRST(tmp->pat), MUTT_MATCH_FULL_ADDRESS, NULL, e, &cache) > 0)
179  {
180  if (tmp->exact || (tmp->val == 9999) || (tmp->val == -9999))
181  {
182  e->score = tmp->val;
183  break;
184  }
185  e->score += tmp->val;
186  }
187  }
188  if (e->score < 0)
189  e->score = 0;
190 
191  if (e->score <= C_ScoreThresholdDelete)
192  mutt_set_flag_update(m, e, MUTT_DELETE, true, upd_mbox);
193  if (e->score <= C_ScoreThresholdRead)
194  mutt_set_flag_update(m, e, MUTT_READ, true, upd_mbox);
195  if (e->score >= C_ScoreThresholdFlag)
196  mutt_set_flag_update(m, e, MUTT_FLAG, true, upd_mbox);
197 }
struct PatternHead * pat
Definition: score.c:59
short C_ScoreThresholdFlag
Config: Messages with a greater score will be automatically flagged.
Definition: score.c:50
Flagged messages.
Definition: mutt.h:106
static struct Score * ScoreList
Definition: score.c:65
int val
Definition: score.c:60
int score
Message score.
Definition: email.h:88
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: pattern.c:2005
#define SLIST_FIRST(head)
Definition: queue.h:229
Messages to be deleted.
Definition: mutt.h:102
Messages that have been read.
Definition: mutt.h:100
Scoring rule for email.
Definition: score.c:56
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition: pattern.h:73
short C_ScoreThresholdDelete
Config: Messages with a lower score will be automatically deleted.
Definition: score.c:49
struct Score * next
Definition: score.c:62
short C_ScoreThresholdRead
Config: Messages with a lower score will be automatically marked read.
Definition: score.c:51
Cache commonly-used patterns.
Definition: pattern.h:83
bool exact
if this rule matches, don&#39;t evaluate any more
Definition: score.c:61
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
enum CommandResult 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 202 of file score.c.

204 {
205  struct Score *tmp = NULL, *last = NULL;
206 
207  while (MoreArgs(s))
208  {
210  if (mutt_str_strcmp("*", buf->data) == 0)
211  {
212  for (tmp = ScoreList; tmp;)
213  {
214  last = tmp;
215  tmp = tmp->next;
216  mutt_pattern_free(&last->pat);
217  FREE(&last);
218  }
219  ScoreList = NULL;
220  }
221  else
222  {
223  for (tmp = ScoreList; tmp; last = tmp, tmp = tmp->next)
224  {
225  if (mutt_str_strcmp(buf->data, tmp->str) == 0)
226  {
227  if (last)
228  last->next = tmp->next;
229  else
230  ScoreList = tmp->next;
231  mutt_pattern_free(&tmp->pat);
232  FREE(&tmp);
233  /* there should only be one score per pattern, so we can stop here */
234  break;
235  }
236  }
237  }
238  }
239  OptNeedRescore = true;
240  return MUTT_CMD_SUCCESS;
241 }
static struct Score * ScoreList
Definition: score.c:65
WHERE bool OptNeedRescore
(pseudo) set when the &#39;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, TokenFlags flags)
Extract one token from a string.
Definition: init.c:2614
char * data
Pointer to data.
Definition: buffer.h:35
Scoring rule for email.
Definition: score.c:56
Success: Command worked.
Definition: mutt_commands.h:35
#define FREE(x)
Definition: memory.h:40
struct Score * next
Definition: score.c:62
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:76
void mutt_pattern_free(struct PatternHead **pat)
Free a Pattern.
Definition: pattern.c:1337

Variable Documentation

short C_ScoreThresholdDelete

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

Definition at line 49 of file score.c.

short C_ScoreThresholdFlag

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

Definition at line 50 of file score.c.

short C_ScoreThresholdRead

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

Definition at line 51 of file score.c.

struct Score* ScoreList = NULL
static

Definition at line 65 of file score.c.