NeoMutt  2022-04-29-81-g9c5a59
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_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, enum ColorId 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 77 of file hdrline.c.

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

◆ 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 95 of file hdrline.c.

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

◆ 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 109 of file hdrline.c.

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

Function Documentation

◆ add_index_color()

static size_t add_index_color ( char *  buf,
size_t  buflen,
MuttFormatFlags  flags,
enum ColorId  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 129 of file hdrline.c.

130 {
131  /* only add color markers if we are operating on main index entries. */
132  if (!(flags & MUTT_FORMAT_INDEX))
133  return 0;
134 
135  /* this item is going to be passed to an external filter */
136  if (flags & MUTT_FORMAT_NOFILTER)
137  return 0;
138 
139  if (color == MT_COLOR_INDEX)
140  { /* buf might be uninitialized other cases */
141  const size_t len = mutt_str_len(buf);
142  buf += len;
143  buflen -= len;
144  }
145 
146  if (buflen <= 2)
147  return 0;
148 
149  buf[0] = MUTT_SPECIAL_INDEX;
150  buf[1] = color;
151  buf[2] = '\0';
152 
153  return 2;
154 }
@ 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:544
@ 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 166 of file hdrline.c.

167 {
168  if (!table || !table->chars || (index < 0) || (index >= table->len))
169  return " ";
170 
171  if (table->chars[index][0] == '\r')
172  return "";
173 
174  return table->chars[index];
175 }
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 185 of file hdrline.c.

186 {
187  /* need 2 bytes at the end, one for the space, another for NUL */
188  static char padded[8];
189  static const char *long_prefixes[DISP_MAX] = {
190  [DISP_TO] = "To ", [DISP_CC] = "Cc ", [DISP_BCC] = "Bcc ",
191  [DISP_FROM] = "", [DISP_PLAIN] = "",
192  };
193 
194  const struct MbTable *c_from_chars = cs_subset_mbtable(NeoMutt->sub, "from_chars");
195 
196  if (!c_from_chars || !c_from_chars->chars || (c_from_chars->len == 0))
197  return long_prefixes[disp];
198 
199  const char *pchar = get_nth_wchar(c_from_chars, disp);
200  if (mutt_str_len(pchar) == 0)
201  return "";
202 
203  snprintf(padded, sizeof(padded), "%s ", pchar);
204  return padded;
205 }
static const char * get_nth_wchar(const struct MbTable *table, int index)
Extract one char from a multi-byte table.
Definition: hdrline.c:166
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 221 of file hdrline.c.

223 {
224  if (!env || !buf)
225  return;
226 
227  bool me;
228  enum FieldType disp;
229  struct AddressList *name = NULL;
230 
231  me = mutt_addr_is_user(TAILQ_FIRST(&env->from));
232 
233  if (do_lists || me)
234  {
235  if (check_for_mailing_list(&env->to, make_from_prefix(DISP_TO), buf, buflen))
236  return;
237  if (check_for_mailing_list(&env->cc, make_from_prefix(DISP_CC), buf, buflen))
238  return;
239  }
240 
241  if (me && !TAILQ_EMPTY(&env->to))
242  {
243  disp = (flags & MUTT_FORMAT_PLAIN) ? DISP_PLAIN : DISP_TO;
244  name = &env->to;
245  }
246  else if (me && !TAILQ_EMPTY(&env->cc))
247  {
248  disp = DISP_CC;
249  name = &env->cc;
250  }
251  else if (me && !TAILQ_EMPTY(&env->bcc))
252  {
253  disp = DISP_BCC;
254  name = &env->bcc;
255  }
256  else if (!TAILQ_EMPTY(&env->from))
257  {
258  disp = DISP_FROM;
259  name = &env->from;
260  }
261  else
262  {
263  *buf = '\0';
264  return;
265  }
266 
267  snprintf(buf, buflen, "%s%s", make_from_prefix(disp), mutt_get_name(TAILQ_FIRST(name)));
268 }
bool mutt_addr_is_user(const struct Address *addr)
Does the address belong to the user.
Definition: alias.c:573
#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:185
FieldType
Header types.
Definition: hdrline.c:110
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:60
struct AddressList cc
Email's 'Cc' list.
Definition: envelope.h:61
struct AddressList bcc
Email's 'Bcc' list.
Definition: envelope.h:62
struct AddressList from
Email's 'From' list.
Definition: envelope.h:59
+ 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 277 of file hdrline.c.

278 {
279  if (!env || !buf)
280  return;
281 
282  bool me = mutt_addr_is_user(TAILQ_FIRST(&env->from));
283 
284  if (do_lists || me)
285  {
286  if (check_for_mailing_list_addr(&env->to, buf, buflen))
287  return;
288  if (check_for_mailing_list_addr(&env->cc, buf, buflen))
289  return;
290  }
291 
292  if (me && !TAILQ_EMPTY(&env->to))
293  snprintf(buf, buflen, "%s", TAILQ_FIRST(&env->to)->mailbox);
294  else if (me && !TAILQ_EMPTY(&env->cc))
295  snprintf(buf, buflen, "%s", TAILQ_FIRST(&env->cc)->mailbox);
296  else if (!TAILQ_EMPTY(&env->from))
297  mutt_str_copy(buf, TAILQ_FIRST(&env->from)->mailbox, buflen);
298  else
299  *buf = '\0';
300 }
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:629
+ 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 307 of file hdrline.c.

308 {
309  struct Address *a = NULL;
310  TAILQ_FOREACH(a, al, entries)
311  if (mutt_addr_is_user(a))
312  return true;
313  return false;
314 }
#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 327 of file hdrline.c.

328 {
329  if (!e || !e->env)
330  return 0;
331 
332  struct Envelope *env = e->env;
333 
334  if (!e->recip_valid)
335  {
336  e->recip_valid = true;
337 
338  if (mutt_addr_is_user(TAILQ_FIRST(&env->from)))
339  e->recipient = 4;
340  else if (user_in_addr(&env->to))
341  {
342  if (TAILQ_NEXT(TAILQ_FIRST(&env->to), entries) || !TAILQ_EMPTY(&env->cc))
343  e->recipient = 2; /* non-unique recipient */
344  else
345  e->recipient = 1; /* unique recipient */
346  }
347  else if (user_in_addr(&env->cc))
348  e->recipient = 3;
349  else if (check_for_mailing_list(&env->to, NULL, NULL, 0))
350  e->recipient = 5;
351  else if (check_for_mailing_list(&env->cc, NULL, NULL, 0))
352  e->recipient = 5;
353  else if (user_in_addr(&env->reply_to))
354  e->recipient = 6;
355  else
356  e->recipient = 0;
357  }
358 
359  return e->recipient;
360 }
static bool user_in_addr(struct AddressList *al)
Do any of the addresses refer to the user?
Definition: hdrline.c:307
#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:57
struct AddressList reply_to
Email's 'reply-to'.
Definition: envelope.h:64
+ 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 367 of file hdrline.c.

368 {
369  return e->collapsed && (e->num_hidden > 1) && (mutt_thread_contains_unread(e) == 1);
370 }
#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 377 of file hdrline.c.

378 {
379  return e->collapsed && (e->num_hidden > 1) && (mutt_thread_contains_unread(e) == 2);
380 }
+ 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
See also
index_format_str()

Definition at line 1405 of file hdrline.c.

1408 {
1409  struct HdrFormatInfo hfi = { 0 };
1410 
1411  hfi.email = e;
1412  hfi.mailbox = m;
1413  hfi.msg_in_pager = inpgr;
1414  hfi.pager_progress = progress;
1415 
1416  mutt_expando_format(buf, buflen, 0, cols, s, index_format_str, (intptr_t) &hfi, flags);
1417 }
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:776
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:439
Data passed to index_format_str()
Definition: hdrline.c:67
struct Email * email
Current Email.
Definition: hdrline.c:70
int msg_in_pager
Index of Email displayed in the Pager.
Definition: hdrline.c:69
struct Mailbox * mailbox
Current Mailbox.
Definition: hdrline.c:68
const char * pager_progress
String representing Pager position through Email.
Definition: hdrline.c:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function: