NeoMutt  2021-10-29-43-g6b8931
Teaching an old dog new tricks
DOXYGEN
Mail Sorting API

Prototype for an email comparison function. More...

Functions

int nntp_compare_order (const struct Email *a, const struct Email *b, bool reverse)
 Sort to mailbox order - Implements sort_mail_t -. More...
 
static int compare_score (const struct Email *a, const struct Email *b, bool reverse)
 Compare two emails using their scores - Implements sort_mail_t -. More...
 
static int compare_size (const struct Email *a, const struct Email *b, bool reverse)
 Compare the size of two emails - Implements sort_mail_t -. More...
 
static int compare_date_sent (const struct Email *a, const struct Email *b, bool reverse)
 Compare the sent date of two emails - Implements sort_mail_t -. More...
 
static int compare_subject (const struct Email *a, const struct Email *b, bool reverse)
 Compare the subject of two emails - Implements sort_mail_t -. More...
 
static int compare_to (const struct Email *a, const struct Email *b, bool reverse)
 Compare the 'to' fields of two emails - Implements sort_mail_t -. More...
 
static int compare_from (const struct Email *a, const struct Email *b, bool reverse)
 Compare the 'from' fields of two emails - Implements sort_mail_t -. More...
 
static int compare_date_received (const struct Email *a, const struct Email *b, bool reverse)
 Compare the date received of two emails - Implements sort_mail_t -. More...
 
static int compare_order (const struct Email *a, const struct Email *b, bool reverse)
 Restore the 'unsorted' order of emails - Implements sort_mail_t -. More...
 
static int compare_spam (const struct Email *a, const struct Email *b, bool reverse)
 Compare the spam values of two emails - Implements sort_mail_t -. More...
 
static int compare_label (const struct Email *a, const struct Email *b, bool reverse)
 Compare the labels of two emails - Implements sort_mail_t -. More...
 

Detailed Description

Prototype for an email comparison function.

Parameters
aFirst item
bSecond item
reversetrue if this is a reverse sort (smaller b precedes a)
Return values
<0a precedes b
0a and b are identical
>0b precedes a

Function Documentation

◆ nntp_compare_order()

int nntp_compare_order ( const struct Email a,
const struct Email b,
bool  reverse 
)

Sort to mailbox order - Implements sort_mail_t -.

Definition at line 2280 of file nntp.c.

2281 {
2282  anum_t na = nntp_edata_get((struct Email *) a)->article_num;
2283  anum_t nb = nntp_edata_get((struct Email *) b)->article_num;
2284  int result = (na == nb) ? 0 : (na > nb) ? 1 : -1;
2285  return reverse ? -result : result;
2286 }
struct NntpEmailData * nntp_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:58
#define anum_t
Definition: lib.h:60
The envelope/body of an email.
Definition: email.h:37
anum_t article_num
NNTP article number.
Definition: edata.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_score()

static int compare_score ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare two emails using their scores - Implements sort_mail_t -.

Definition at line 81 of file sort.c.

82 {
83  int result = b->score - a->score; /* note that this is reverse */
84  return reverse ? -result : result;
85 }
int score
Message score.
Definition: email.h:113
+ Here is the caller graph for this function:

◆ compare_size()

static int compare_size ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the size of two emails - Implements sort_mail_t -.

Definition at line 90 of file sort.c.

91 {
92  int result = a->body->length - b->body->length;
93  return reverse ? -result : result;
94 }
LOFF_T length
length (in bytes) of attachment
Definition: body.h:52
struct Body * body
List of MIME parts.
Definition: email.h:67
+ Here is the caller graph for this function:

◆ compare_date_sent()

static int compare_date_sent ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the sent date of two emails - Implements sort_mail_t -.

Definition at line 99 of file sort.c.

100 {
101  int result = a->date_sent - b->date_sent;
102  return reverse ? -result : result;
103 }
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:58
+ Here is the caller graph for this function:

◆ compare_subject()

static int compare_subject ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the subject of two emails - Implements sort_mail_t -.

Definition at line 108 of file sort.c.

109 {
110  int rc;
111 
112  if (!a->env->real_subj)
113  {
114  if (!b->env->real_subj)
115  rc = compare_date_sent(a, b, false);
116  else
117  rc = -1;
118  }
119  else if (!b->env->real_subj)
120  rc = 1;
121  else
122  rc = mutt_istr_cmp(a->env->real_subj, b->env->real_subj);
123  return reverse ? -rc : rc;
124 }
static int compare_date_sent(const struct Email *a, const struct Email *b, bool reverse)
Compare the sent date of two emails - Implements sort_mail_t -.
Definition: sort.c:99
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:391
struct Envelope * env
Envelope information.
Definition: email.h:66
char * real_subj
Offset of the real subject.
Definition: envelope.h:69
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_to()

static int compare_to ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the 'to' fields of two emails - Implements sort_mail_t -.

Definition at line 157 of file sort.c.

158 {
159  char fa[128];
160 
161  mutt_str_copy(fa, mutt_get_name(TAILQ_FIRST(&a->env->to)), sizeof(fa));
162  const char *fb = mutt_get_name(TAILQ_FIRST(&b->env->to));
163  int result = mutt_istrn_cmp(fa, fb, sizeof(fa));
164  return reverse ? -result : result;
165 }
int mutt_istrn_cmp(const char *a, const char *b, size_t num)
Compare two strings ignoring case (to a maximum), safely.
Definition: string.c:418
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:560
#define TAILQ_FIRST(head)
Definition: queue.h:723
const char * mutt_get_name(const struct Address *a)
Pick the best name to display from an address.
Definition: sort.c:136
struct AddressList to
Email's 'To' list.
Definition: envelope.h:58
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_from()

static int compare_from ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the 'from' fields of two emails - Implements sort_mail_t -.

Definition at line 170 of file sort.c.

171 {
172  char fa[128];
173 
174  mutt_str_copy(fa, mutt_get_name(TAILQ_FIRST(&a->env->from)), sizeof(fa));
175  const char *fb = mutt_get_name(TAILQ_FIRST(&b->env->from));
176  int result = mutt_istrn_cmp(fa, fb, sizeof(fa));
177  return reverse ? -result : result;
178 }
struct AddressList from
Email's 'From' list.
Definition: envelope.h:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_date_received()

static int compare_date_received ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the date received of two emails - Implements sort_mail_t -.

Definition at line 183 of file sort.c.

184 {
185  int result = a->received - b->received;
186  return reverse ? -result : result;
187 }
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:59
+ Here is the caller graph for this function:

◆ compare_order()

static int compare_order ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Restore the 'unsorted' order of emails - Implements sort_mail_t -.

Definition at line 192 of file sort.c.

193 {
194  int result = a->index - b->index;
195  return reverse ? -result : result;
196 }
int index
The absolute (unsorted) message number.
Definition: email.h:110
+ Here is the caller graph for this function:

◆ compare_spam()

static int compare_spam ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the spam values of two emails - Implements sort_mail_t -.

Definition at line 201 of file sort.c.

202 {
203  char *aptr = NULL, *bptr = NULL;
204  int ahas, bhas;
205  int result = 0;
206  double difference;
207 
208  /* Firstly, require spam attributes for both msgs */
209  /* to compare. Determine which msgs have one. */
210  ahas = a->env && !mutt_buffer_is_empty(&a->env->spam);
211  bhas = b->env && !mutt_buffer_is_empty(&b->env->spam);
212 
213  /* If one msg has spam attr but other does not, sort the one with first. */
214  if (ahas && !bhas)
215  return reverse ? -1 : 1;
216  if (!ahas && bhas)
217  return reverse ? 1 : -1;
218 
219  /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
220  if (!ahas && !bhas)
221  return 0;
222 
223  /* Both have spam attrs. */
224 
225  /* preliminary numeric examination */
226  difference = (strtod(a->env->spam.data, &aptr) - strtod(b->env->spam.data, &bptr));
227 
228  /* map double into comparison (-1, 0, or 1) */
229  result = ((difference < 0.0) ? -1 : (difference > 0.0) ? 1 : 0);
230 
231  /* If either aptr or bptr is equal to data, there is no numeric */
232  /* value for that spam attribute. In this case, compare lexically. */
233  if ((aptr == a->env->spam.data) || (bptr == b->env->spam.data))
234  {
235  result = strcmp(aptr, bptr);
236  return reverse ? -result : result;
237  }
238 
239  /* Otherwise, we have numeric value for both attrs. If these values */
240  /* are equal, then we first fall back upon string comparison, then */
241  /* upon auxiliary sort. */
242  if (result == 0)
243  result = strcmp(aptr, bptr);
244  return reverse ? -result : result;
245 }
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:252
char * data
Pointer to data.
Definition: buffer.h:35
struct Buffer spam
Spam header.
Definition: envelope.h:82
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_label()

static int compare_label ( const struct Email a,
const struct Email b,
bool  reverse 
)
static

Compare the labels of two emails - Implements sort_mail_t -.

Definition at line 250 of file sort.c.

251 {
252  int ahas, bhas, result = 0;
253 
254  /* As with compare_spam, not all messages will have the x-label
255  * property. Blank X-Labels are treated as null in the index
256  * display, so we'll consider them as null for sort, too. */
257  ahas = a->env && a->env->x_label && *(a->env->x_label);
258  bhas = b->env && b->env->x_label && *(b->env->x_label);
259 
260  /* First we bias toward a message with a label, if the other does not. */
261  if (ahas && !bhas)
262  return reverse ? 1 : -1;
263  if (!ahas && bhas)
264  return reverse ? -1 : 1;
265 
266  /* If neither has a label, use aux sort. */
267  if (!ahas && !bhas)
268  return 0;
269 
270  /* If both have a label, we just do a lexical compare. */
271  result = mutt_istr_cmp(a->env->x_label, b->env->x_label);
272  return reverse ? -result : result;
273 }
char * x_label
X-Label.
Definition: envelope.h:74
+ Here is the call graph for this function:
+ Here is the caller graph for this function: