NeoMutt  2021-10-22-8-g9cb437
Teaching an old dog new tricks
DOXYGEN
hdrline.c File Reference

String processing routines to generate the mail index. More...

#include "config.h"
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "gui/lib.h"
#include "hdrline.h"
#include "attach/lib.h"
#include "color/lib.h"
#include "ncrypt/lib.h"
#include "format_flags.h"
#include "hook.h"
#include "maillist.h"
#include "mutt_globals.h"
#include "mutt_thread.h"
#include "muttlib.h"
#include "mx.h"
#include "sort.h"
#include "subjectrx.h"
#include "notmuch/lib.h"
+ Include dependency graph for hdrline.c:

Go to the source code of this file.

Data Structures

struct  HdrFormatInfo
 Data passed to index_format_str() More...
 

Enumerations

enum  FlagChars {
  FLAG_CHAR_TAGGED , FLAG_CHAR_IMPORTANT , FLAG_CHAR_DELETED , FLAG_CHAR_DELETED_ATTACH ,
  FLAG_CHAR_REPLIED , FLAG_CHAR_OLD , FLAG_CHAR_NEW , FLAG_CHAR_OLD_THREAD ,
  FLAG_CHAR_NEW_THREAD , FLAG_CHAR_SEMPTY , FLAG_CHAR_ZEMPTY
}
 Index into the $flag_chars variable ($flag_chars) More...
 
enum  CryptChars {
  FLAG_CHAR_CRYPT_GOOD_SIGN , FLAG_CHAR_CRYPT_ENCRYPTED , FLAG_CHAR_CRYPT_SIGNED , FLAG_CHAR_CRYPT_CONTAINS_KEY ,
  FLAG_CHAR_CRYPT_NO_CRYPTO
}
 Index into the $crypt_chars variable ($crypt_chars) More...
 
enum  FieldType {
  DISP_TO , DISP_CC , DISP_BCC , DISP_FROM ,
  DISP_PLAIN , DISP_MAX
}
 Header types. More...
 

Functions

static size_t add_index_color (char *buf, size_t buflen, MuttFormatFlags flags, char color)
 Insert a color marker into a string. More...
 
static const char * get_nth_wchar (const struct MbTable *table, int index)
 Extract one char from a multi-byte table. More...
 
static const char * make_from_prefix (enum FieldType disp)
 Create a prefix for an author field. More...
 
static void make_from (struct Envelope *env, char *buf, size_t buflen, bool do_lists, MuttFormatFlags flags)
 Generate a From: field (with optional prefix) More...
 
static void make_from_addr (struct Envelope *env, char *buf, size_t buflen, bool do_lists)
 Create a 'from' address for a reply email. More...
 
static bool user_in_addr (struct AddressList *al)
 Do any of the addresses refer to the user? More...
 
static int user_is_recipient (struct Email *e)
 Is the user a recipient of the message. More...
 
static bool thread_is_new (struct Email *e)
 Does the email thread contain any new emails? More...
 
static bool thread_is_old (struct Email *e)
 Does the email thread contain any unread emails? More...
 
static const char * index_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Format a string for the index list - Implements format_t -. More...
 
void mutt_make_string (char *buf, size_t buflen, int cols, const char *s, struct Mailbox *m, int inpgr, struct Email *e, MuttFormatFlags flags, const char *progress)
 Create formatted strings using mailbox expandos. More...
 

Detailed Description

String processing routines to generate the mail index.

Authors
  • Michael R. Elkins
  • Richard Russon
  • Ian Zimmerman
  • Pietro Cerutti

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 hdrline.c.

Enumeration Type Documentation

◆ FlagChars

enum FlagChars

Index into the $flag_chars variable ($flag_chars)

Enumerator
FLAG_CHAR_TAGGED 

Character denoting a tagged email.

FLAG_CHAR_IMPORTANT 

Character denoting a important (flagged) email.

FLAG_CHAR_DELETED 

Character denoting a deleted email.

FLAG_CHAR_DELETED_ATTACH 

Character denoting a deleted attachment.

FLAG_CHAR_REPLIED 

Character denoting an email that has been replied to.

FLAG_CHAR_OLD 

Character denoting an email that has been read.

FLAG_CHAR_NEW 

Character denoting an unread email.

FLAG_CHAR_OLD_THREAD 

Character denoting a thread of emails that has been read.

FLAG_CHAR_NEW_THREAD 

Character denoting a thread containing at least one new email.

FLAG_CHAR_SEMPTY 

Character denoting a read email, $index_format S expando.

FLAG_CHAR_ZEMPTY 

Character denoting a read email, $index_format Z expando.

Definition at line 78 of file hdrline.c.

79 {
91 };
@ FLAG_CHAR_OLD
Character denoting an email that has been read.
Definition: hdrline.c:85
@ FLAG_CHAR_REPLIED
Character denoting an email that has been replied to.
Definition: hdrline.c:84
@ FLAG_CHAR_OLD_THREAD
Character denoting a thread of emails that has been read.
Definition: hdrline.c:87
@ FLAG_CHAR_ZEMPTY
Character denoting a read email, $index_format Z expando.
Definition: hdrline.c:90
@ FLAG_CHAR_TAGGED
Character denoting a tagged email.
Definition: hdrline.c:80
@ FLAG_CHAR_NEW
Character denoting an unread email.
Definition: hdrline.c:86
@ FLAG_CHAR_DELETED
Character denoting a deleted email.
Definition: hdrline.c:82
@ FLAG_CHAR_NEW_THREAD
Character denoting a thread containing at least one new email.
Definition: hdrline.c:88
@ FLAG_CHAR_DELETED_ATTACH
Character denoting a deleted attachment.
Definition: hdrline.c:83
@ FLAG_CHAR_SEMPTY
Character denoting a read email, $index_format S expando.
Definition: hdrline.c:89
@ FLAG_CHAR_IMPORTANT
Character denoting a important (flagged) email.
Definition: hdrline.c:81

◆ CryptChars

enum CryptChars

Index into the $crypt_chars variable ($crypt_chars)

Enumerator
FLAG_CHAR_CRYPT_GOOD_SIGN 

Character denoting a message signed with a verified key.

FLAG_CHAR_CRYPT_ENCRYPTED 

Character denoting a message is PGP-encrypted.

FLAG_CHAR_CRYPT_SIGNED 

Character denoting a message is signed.

FLAG_CHAR_CRYPT_CONTAINS_KEY 

Character denoting a message contains a PGP key.

FLAG_CHAR_CRYPT_NO_CRYPTO 

Character denoting a message has no cryptography information.

Definition at line 96 of file hdrline.c.

97 {
103 };
@ FLAG_CHAR_CRYPT_CONTAINS_KEY
Character denoting a message contains a PGP key.
Definition: hdrline.c:101
@ FLAG_CHAR_CRYPT_SIGNED
Character denoting a message is signed.
Definition: hdrline.c:100
@ FLAG_CHAR_CRYPT_NO_CRYPTO
Character denoting a message has no cryptography information.
Definition: hdrline.c:102
@ FLAG_CHAR_CRYPT_GOOD_SIGN
Character denoting a message signed with a verified key.
Definition: hdrline.c:98
@ FLAG_CHAR_CRYPT_ENCRYPTED
Character denoting a message is PGP-encrypted.
Definition: hdrline.c:99

◆ FieldType

enum FieldType

Header types.

Strings for printing headers

Enumerator
DISP_TO 

To: string.

DISP_CC 

Cc: string.

DISP_BCC 

Bcc: string.

DISP_FROM 

From: string.

DISP_PLAIN 

Empty string.

DISP_MAX 

Definition at line 110 of file hdrline.c.

111 {
112  DISP_TO,
113  DISP_CC,
114  DISP_BCC,
115  DISP_FROM,
116  DISP_PLAIN,
117  DISP_MAX,
118 };
@ DISP_PLAIN
Empty string.
Definition: hdrline.c:116
@ DISP_TO
To: string.
Definition: hdrline.c:112
@ DISP_CC
Cc: string.
Definition: hdrline.c:113
@ DISP_BCC
Bcc: string.
Definition: hdrline.c:114
@ DISP_MAX
Definition: hdrline.c:117
@ DISP_FROM
From: string.
Definition: hdrline.c:115

Function Documentation

◆ add_index_color()

static size_t add_index_color ( char *  buf,
size_t  buflen,
MuttFormatFlags  flags,
char  color 
)
static

Insert a color marker into a string.

Parameters
bufBuffer to store marker
buflenBuffer length
flagsFlags, see MuttFormatFlags
colorColor, e.g. MT_COLOR_MESSAGE
Return values
numCharacters written

The colors are stored as "magic" strings embedded in the text.

Definition at line 130 of file hdrline.c.

131 {
132  /* only add color markers if we are operating on main index entries. */
133  if (!(flags & MUTT_FORMAT_INDEX))
134  return 0;
135 
136  /* this item is going to be passed to an external filter */
137  if (flags & MUTT_FORMAT_NOFILTER)
138  return 0;
139 
140  if (color == MT_COLOR_INDEX)
141  { /* buf might be uninitialized other cases */
142  const size_t len = mutt_str_len(buf);
143  buf += len;
144  buflen -= len;
145  }
146 
147  if (buflen <= 2)
148  return 0;
149 
150  buf[0] = MUTT_SPECIAL_INDEX;
151  buf[1] = color;
152  buf[2] = '\0';
153 
154  return 2;
155 }
@ MT_COLOR_INDEX
Index: default colour (takes a pattern)
Definition: color.h:76
#define MUTT_FORMAT_NOFILTER
Do not allow filtering on this pass.
Definition: format_flags.h:37
#define MUTT_FORMAT_INDEX
This is a main index entry.
Definition: format_flags.h:36
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition: mutt_thread.h:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_nth_wchar()

static const char* get_nth_wchar ( const struct MbTable table,
int  index 
)
static

Extract one char from a multi-byte table.

Parameters
tableMulti-byte table
indexSelect this character
Return values
ptrString pointer to the character

Extract one multi-byte character from a string table. If the index is invalid, then a space character will be returned. If the character selected is '
' (Ctrl-M), then "" will be returned.

Definition at line 167 of file hdrline.c.

168 {
169  if (!table || !table->chars || (index < 0) || (index >= table->len))
170  return " ";
171 
172  if (table->chars[index][0] == '\r')
173  return "";
174 
175  return table->chars[index];
176 }
int len
Number of characters.
Definition: mbtable.h:36
char ** chars
The array of multibyte character strings.
Definition: mbtable.h:37
+ Here is the caller graph for this function:

◆ make_from_prefix()

static const char* make_from_prefix ( enum FieldType  disp)
static

Create a prefix for an author field.

Parameters
dispType of field
Return values
ptrPrefix string (do not free it)

If $from_chars is set, pick an appropriate character from it. If not, use the default prefix: "To", "Cc", etc

Definition at line 186 of file hdrline.c.

187 {
188  /* need 2 bytes at the end, one for the space, another for NUL */
189  static char padded[8];
190  static const char *long_prefixes[DISP_MAX] = {
191  [DISP_TO] = "To ", [DISP_CC] = "Cc ", [DISP_BCC] = "Bcc ",
192  [DISP_FROM] = "", [DISP_PLAIN] = "",
193  };
194 
195  const struct MbTable *c_from_chars =
196  cs_subset_mbtable(NeoMutt->sub, "from_chars");
197 
198  if (!c_from_chars || !c_from_chars->chars || (c_from_chars->len == 0))
199  return long_prefixes[disp];
200 
201  const char *pchar = get_nth_wchar(c_from_chars, disp);
202  if (mutt_str_len(pchar) == 0)
203  return "";
204 
205  snprintf(padded, sizeof(padded), "%s ", pchar);
206  return padded;
207 }
static const char * get_nth_wchar(const struct MbTable *table, int index)
Extract one char from a multi-byte table.
Definition: hdrline.c:167
struct MbTable * cs_subset_mbtable(const struct ConfigSubset *sub, const char *name)
Get a Multibyte table config item by name.
Definition: helpers.c:145
Multibyte character table.
Definition: mbtable.h:34
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ make_from()

static void make_from ( struct Envelope env,
char *  buf,
size_t  buflen,
bool  do_lists,
MuttFormatFlags  flags 
)
static

Generate a From: field (with optional prefix)

Parameters
envEnvelope of the email
bufBuffer to store the result
buflenSize of the buffer
do_listsShould we check for mailing lists?
flagsFormat flags, see MuttFormatFlags

Generate the F or L field in $index_format. This is the author, or recipient of the email.

The field can optionally be prefixed by a character from $from_chars. If $from_chars is not set, the prefix will be, "To", "Cc", etc

Definition at line 223 of file hdrline.c.

225 {
226  if (!env || !buf)
227  return;
228 
229  bool me;
230  enum FieldType disp;
231  struct AddressList *name = NULL;
232 
233  me = mutt_addr_is_user(TAILQ_FIRST(&env->from));
234 
235  if (do_lists || me)
236  {
237  if (check_for_mailing_list(&env->to, make_from_prefix(DISP_TO), buf, buflen))
238  return;
239  if (check_for_mailing_list(&env->cc, make_from_prefix(DISP_CC), buf, buflen))
240  return;
241  }
242 
243  if (me && !TAILQ_EMPTY(&env->to))
244  {
245  disp = (flags & MUTT_FORMAT_PLAIN) ? DISP_PLAIN : DISP_TO;
246  name = &env->to;
247  }
248  else if (me && !TAILQ_EMPTY(&env->cc))
249  {
250  disp = DISP_CC;
251  name = &env->cc;
252  }
253  else if (me && !TAILQ_EMPTY(&env->bcc))
254  {
255  disp = DISP_BCC;
256  name = &env->bcc;
257  }
258  else if (!TAILQ_EMPTY(&env->from))
259  {
260  disp = DISP_FROM;
261  name = &env->from;
262  }
263  else
264  {
265  *buf = '\0';
266  return;
267  }
268 
269  snprintf(buf, buflen, "%s%s", make_from_prefix(disp), mutt_get_name(TAILQ_FIRST(name)));
270 }
bool mutt_addr_is_user(const struct Address *addr)
Does the address belong to the user.
Definition: alias.c:562
#define MUTT_FORMAT_PLAIN
Do not prepend DISP_TO, DISP_CC ...
Definition: format_flags.h:38
static const char * make_from_prefix(enum FieldType disp)
Create a prefix for an author field.
Definition: hdrline.c:186
FieldType
Header types.
Definition: hdrline.c:111
bool check_for_mailing_list(struct AddressList *al, const char *pfx, char *buf, int buflen)
Search list of addresses for a mailing list.
Definition: maillist.c:78
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_EMPTY(head)
Definition: queue.h:721
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
struct AddressList cc
Email's 'Cc' list.
Definition: envelope.h:59
struct AddressList bcc
Email's 'Bcc' list.
Definition: envelope.h:60
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:

◆ make_from_addr()

static void make_from_addr ( struct Envelope env,
char *  buf,
size_t  buflen,
bool  do_lists 
)
static

Create a 'from' address for a reply email.

Parameters
envEnvelope of current email
bufBuffer for the result
buflenLength of buffer
do_listsIf true, check for mailing lists

Definition at line 279 of file hdrline.c.

280 {
281  if (!env || !buf)
282  return;
283 
284  bool me = mutt_addr_is_user(TAILQ_FIRST(&env->from));
285 
286  if (do_lists || me)
287  {
288  if (check_for_mailing_list_addr(&env->to, buf, buflen))
289  return;
290  if (check_for_mailing_list_addr(&env->cc, buf, buflen))
291  return;
292  }
293 
294  if (me && !TAILQ_EMPTY(&env->to))
295  snprintf(buf, buflen, "%s", TAILQ_FIRST(&env->to)->mailbox);
296  else if (me && !TAILQ_EMPTY(&env->cc))
297  snprintf(buf, buflen, "%s", TAILQ_FIRST(&env->cc)->mailbox);
298  else if (!TAILQ_EMPTY(&env->from))
299  mutt_str_copy(buf, TAILQ_FIRST(&env->from)->mailbox, buflen);
300  else
301  *buf = '\0';
302 }
bool check_for_mailing_list_addr(struct AddressList *al, char *buf, int buflen)
Check an address list for a mailing list.
Definition: maillist.c:102
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:749
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ user_in_addr()

static bool user_in_addr ( struct AddressList *  al)
static

Do any of the addresses refer to the user?

Parameters
alAddressList
Return values
trueAny of the addresses match one of the user's addresses

Definition at line 309 of file hdrline.c.

310 {
311  struct Address *a = NULL;
312  TAILQ_FOREACH(a, al, entries)
313  if (mutt_addr_is_user(a))
314  return true;
315  return false;
316 }
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
An email address.
Definition: address.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ user_is_recipient()

static int user_is_recipient ( struct Email e)
static

Is the user a recipient of the message.

Parameters
eEmail to test
Return values
0User is not in list
1User is unique recipient
2User is in the TO list
3User is in the CC list
4User is originator
5Sent to a subscribed mailinglist
6User is in the Reply-To list

Definition at line 329 of file hdrline.c.

330 {
331  if (!e || !e->env)
332  return 0;
333 
334  struct Envelope *env = e->env;
335 
336  if (!e->recip_valid)
337  {
338  e->recip_valid = true;
339 
340  if (mutt_addr_is_user(TAILQ_FIRST(&env->from)))
341  e->recipient = 4;
342  else if (user_in_addr(&env->to))
343  {
344  if (TAILQ_NEXT(TAILQ_FIRST(&env->to), entries) || !TAILQ_EMPTY(&env->cc))
345  e->recipient = 2; /* non-unique recipient */
346  else
347  e->recipient = 1; /* unique recipient */
348  }
349  else if (user_in_addr(&env->cc))
350  e->recipient = 3;
351  else if (check_for_mailing_list(&env->to, NULL, NULL, 0))
352  e->recipient = 5;
353  else if (check_for_mailing_list(&env->cc, NULL, NULL, 0))
354  e->recipient = 5;
355  else if (user_in_addr(&env->reply_to))
356  e->recipient = 6;
357  else
358  e->recipient = 0;
359  }
360 
361  return e->recipient;
362 }
static bool user_in_addr(struct AddressList *al)
Do any of the addresses refer to the user?
Definition: hdrline.c:309
#define TAILQ_NEXT(elm, field)
Definition: queue.h:832
bool recip_valid
Is_recipient is valid.
Definition: email.h:104
struct Envelope * env
Envelope information.
Definition: email.h:66
short recipient
User_is_recipient()'s return value, cached.
Definition: email.h:116
The header of an Email.
Definition: envelope.h:55
struct AddressList reply_to
Email's 'reply-to'.
Definition: envelope.h:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ thread_is_new()

static bool thread_is_new ( struct Email e)
static

Does the email thread contain any new emails?

Parameters
eEmail
Return values
trueThread contains new mail

Definition at line 369 of file hdrline.c.

370 {
371  return e->collapsed && (e->num_hidden > 1) && (mutt_thread_contains_unread(e) == 1);
372 }
#define mutt_thread_contains_unread(e)
Definition: mutt_thread.h:95
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:120
size_t num_hidden
Number of hidden messages in this view (only valid when collapsed is set)
Definition: email.h:122
+ Here is the caller graph for this function:

◆ thread_is_old()

static bool thread_is_old ( struct Email e)
static

Does the email thread contain any unread emails?

Parameters
eEmail
Return values
trueThread contains unread mail

Definition at line 379 of file hdrline.c.

380 {
381  return e->collapsed && (e->num_hidden > 1) && (mutt_thread_contains_unread(e) == 2);
382 }
+ Here is the caller graph for this function:

◆ mutt_make_string()

void mutt_make_string ( char *  buf,
size_t  buflen,
int  cols,
const char *  s,
struct Mailbox m,
int  inpgr,
struct Email e,
MuttFormatFlags  flags,
const char *  progress 
)

Create formatted strings using mailbox expandos.

Parameters
bufBuffer for the result
buflenBuffer length
colsNumber of screen columns (OPTIONAL)
sprintf-line format string
mMailbox
inpgrMessage shown in the pager
eEmail
flagsFlags, see MuttFormatFlags
progressPager progress string

Definition at line 1410 of file hdrline.c.

1413 {
1414  struct HdrFormatInfo hfi = { 0 };
1415 
1416  hfi.email = e;
1417  hfi.mailbox = m;
1418  hfi.msg_in_pager = inpgr;
1419  hfi.pager_progress = progress;
1420 
1421  mutt_expando_format(buf, buflen, 0, cols, s, index_format_str, (intptr_t) &hfi, flags);
1422 }
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:780
static const char * index_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Format a string for the index list - Implements format_t -.
Definition: hdrline.c:441
Data passed to index_format_str()
Definition: hdrline.c:68
struct Email * email
Current Email.
Definition: hdrline.c:71
int msg_in_pager
Index of Email displayed in the Pager.
Definition: hdrline.c:70
struct Mailbox * mailbox
Current Mailbox.
Definition: hdrline.c:69
const char * pager_progress
String representing Pager postiion through Email.
Definition: hdrline.c:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function: