NeoMutt  2025-12-11-980-ge38c27
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
alias.c File Reference

Representation of a single alias to an email address. More...

#include "config.h"
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "alias.h"
#include "lib.h"
#include "browser/lib.h"
#include "editor/lib.h"
#include "history/lib.h"
#include "question/lib.h"
#include "send/lib.h"
#include "alternates.h"
#include "globals.h"
#include "module_data.h"
#include "muttlib.h"
#include "reverse.h"
+ Include dependency graph for alias.c:

Go to the source code of this file.

Functions

static void write_safe_address (FILE *fp, const char *s)
 Defang malicious email addresses.
 
static void expand_aliases_r (struct AddressList *al, struct ListHead *expn)
 Expand aliases, recursively.
 
static void recode_buf (struct Buffer *buf)
 Convert some text between two character sets.
 
static int check_alias_name (const char *s, struct Buffer *dest)
 Sanity-check an alias name.
 
static bool string_is_address (const char *str, const char *user, const char *domain)
 Does an email address match a user and domain?
 
struct AddressList * alias_lookup (const char *name)
 Find an Alias.
 
void mutt_expand_aliases (struct AddressList *al)
 Expand aliases in a List of Addresses.
 
void mutt_expand_aliases_env (struct Envelope *env)
 Expand aliases in all the fields of an Envelope.
 
struct AddressList * mutt_get_address (struct Envelope *env, const char **prefix)
 Get an Address from an Envelope.
 
void alias_create (struct AddressList *al, const struct ConfigSubset *sub)
 Create a new Alias from an Address.
 
bool mutt_addr_is_user (const struct Address *addr)
 Does the address belong to the user.
 
struct Aliasalias_new (void)
 Create a new Alias.
 
void alias_free (struct Alias **ptr)
 Free an Alias.
 
void aliaslist_clear (struct AliasArray *aa)
 Empty a List of Aliases.
 

Detailed Description

Representation of a single alias to an email address.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Federico Kircheis
  • Anna Figueiredo Gomes
  • Dennis Schön

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

Function Documentation

◆ write_safe_address()

static void write_safe_address ( FILE * fp,
const char * s )
static

Defang malicious email addresses.

Parameters
fpFile to write to
sEmail address to defang

if someone has an address like From: John /bin/rm -f ~ Doe john..nosp@m.doe@.nosp@m.examp.nosp@m.le.c.nosp@m.om and the user creates an alias for this, NeoMutt could wind up executing the backticks because it writes aliases like alias me John /bin/rm -f ~ Doe john..nosp@m.doe@.nosp@m.examp.nosp@m.le.c.nosp@m.om To avoid this problem, use a backslash () to quote any backticks. We also need to quote backslashes as well, since you could defeat the above by doing From: John `/bin/rm -f ~` Doe john..nosp@m.doe@.nosp@m.examp.nosp@m.le.c.nosp@m.om since that would get aliased as alias me John \/bin/rm -f ~\\ Doe john..nosp@m.doe@.nosp@m.examp.nosp@m.le.c.nosp@m.om which still gets evaluated because the double backslash is not a quote.

Additionally, we need to quote ' and " characters, otherwise neomutt will interpret them on the wrong parsing step.

$ wants to be quoted since it may indicate the start of an environment variable.

Definition at line 84 of file alias.c.

85{
86 while (*s)
87 {
88 if ((*s == '\\') || (*s == '`') || (*s == '\'') || (*s == '"') || (*s == '$'))
89 fputc('\\', fp);
90 fputc(*s, fp);
91 s++;
92 }
93}
+ Here is the caller graph for this function:

◆ expand_aliases_r()

static void expand_aliases_r ( struct AddressList * al,
struct ListHead * expn )
static

Expand aliases, recursively.

Parameters
[in]alAddress List
[out]expnAlias List

ListHead expn is used as temporary storage for already-expanded aliases.

Definition at line 102 of file alias.c.

103{
104 struct Address *a = TAILQ_FIRST(al);
105 while (a)
106 {
107 if (!a->group && !a->personal && a->mailbox && !buf_find_char(a->mailbox, '@'))
108 {
109 struct AddressList *alias = alias_lookup(buf_string(a->mailbox));
110 if (alias)
111 {
112 bool duplicate = false;
113 struct ListNode *np = NULL;
114 STAILQ_FOREACH(np, expn, entries)
115 {
116 if (mutt_str_equal(buf_string(a->mailbox), np->data)) /* alias already found */
117 {
118 mutt_debug(LL_DEBUG1, "loop in alias found for '%s'\n", buf_string(a->mailbox));
119 duplicate = true;
120 break;
121 }
122 }
123
124 if (duplicate)
125 {
126 // We've already seen this alias, so drop it
127 struct Address *next = TAILQ_NEXT(a, entries);
128 TAILQ_REMOVE(al, a, entries);
129 mutt_addr_free(&a);
130 a = next;
131 continue;
132 }
133
134 // Keep a list of aliases that we've already seen
136
137 /* The alias may expand to several addresses,
138 * some of which may themselves be aliases.
139 * Create a copy and recursively expand any aliases within. */
140 struct AddressList copy = TAILQ_HEAD_INITIALIZER(copy);
141 mutt_addrlist_copy(&copy, alias, false);
142 expand_aliases_r(&copy, expn);
143
144 /* Next, move the expanded addresses
145 * from the copy into the original list (before the alias) */
146 struct Address *a2 = NULL;
147 struct Address *tmp = NULL;
148 TAILQ_FOREACH_SAFE(a2, &copy, entries, tmp)
149 {
150 TAILQ_INSERT_BEFORE(a, a2, entries);
151 }
152 a = TAILQ_PREV(a, AddressList, entries);
153 // Finally, remove the alias itself
154 struct Address *next = TAILQ_NEXT(a, entries);
155 TAILQ_REMOVE(al, next, entries);
156 mutt_addr_free(&next);
157 }
158 else
159 {
160 struct passwd *pw = getpwnam(buf_string(a->mailbox));
161 if (pw)
162 {
163 char namebuf[256] = { 0 };
164
165 mutt_gecos_name(namebuf, sizeof(namebuf), pw);
166 a->personal = buf_new(namebuf);
167 }
168 }
169 }
170 a = TAILQ_NEXT(a, entries);
171 }
172
173 const char *fqdn = NULL;
174 const bool c_use_domain = cs_subset_bool(NeoMutt->sub, "use_domain");
175 if (c_use_domain && (fqdn = mutt_fqdn(true, NeoMutt->sub)))
176 {
177 /* now qualify all local addresses */
178 mutt_addrlist_qualify(al, fqdn);
179 }
180}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition address.c:776
void mutt_addrlist_qualify(struct AddressList *al, const char *host)
Expand local names in an Address list using a hostname.
Definition address.c:687
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition address.c:463
struct AddressList * alias_lookup(const char *name)
Find an Alias.
Definition alias.c:274
static void expand_aliases_r(struct AddressList *al, struct ListHead *expn)
Expand aliases, recursively.
Definition alias.c:102
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition buffer.c:311
const char * buf_find_char(const struct Buffer *buf, const char c)
Return a pointer to a char found in the buffer.
Definition buffer.c:659
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:577
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
struct ListNode * mutt_list_insert_head(struct ListHead *h, char *s)
Insert a string at the beginning of a List.
Definition list.c:46
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:666
char * mutt_gecos_name(char *dest, size_t destlen, struct passwd *pw)
Lookup a user's real name in /etc/passwd.
Definition muttlib.c:321
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:792
#define TAILQ_PREV(elm, headname, field)
Definition queue.h:891
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define TAILQ_FIRST(head)
Definition queue.h:780
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:901
#define TAILQ_NEXT(elm, field)
Definition queue.h:889
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:694
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition queue.h:843
const char * mutt_fqdn(bool may_hide_host, const struct ConfigSubset *sub)
Get the Fully-Qualified Domain Name.
Definition sendlib.c:717
An email address.
Definition address.h:35
struct Buffer * personal
Real name of address.
Definition address.h:36
bool group
Group mailbox?
Definition address.h:38
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ recode_buf()

static void recode_buf ( struct Buffer * buf)
static

Convert some text between two character sets.

Parameters
bufBuffer to convert

The 'from' charset is controlled by the 'charset' config variable. The 'to' charset is controlled by the 'config_charset' config variable.

Definition at line 189 of file alias.c.

190{
191 const char *const c_config_charset = cs_subset_string(NeoMutt->sub, "config_charset");
192 if (!c_config_charset || !cc_charset())
193 return;
194
195 char *s = buf_strdup(buf);
196 if (!s)
197 return;
198 if (mutt_ch_convert_string(&s, cc_charset(), c_config_charset, MUTT_ICONV_NONE) == 0)
199 buf_strcpy(buf, s);
200 FREE(&s);
201}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:401
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
const char * cc_charset(void)
Get the cached value of $charset.
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition charset.c:819
#define MUTT_ICONV_NONE
No flags are set.
Definition charset.h:66
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ check_alias_name()

static int check_alias_name ( const char * s,
struct Buffer * dest )
static

Sanity-check an alias name.

Parameters
sAlias to check
destBuffer for the result
Return values
0Success
-1Error

Only characters which are non-special to both the RFC822 and the neomutt configuration parser are permitted.

Definition at line 213 of file alias.c.

214{
215 wchar_t wc = 0;
216 mbstate_t mbstate = { 0 };
217 size_t l;
218 int rc = 0;
219 bool dry = !dest; // Dry run
220
221 if (!dry)
222 buf_reset(dest);
223 for (; s && *s && (l = mbrtowc(&wc, s, MB_CUR_MAX, &mbstate)) != 0; s += l)
224 {
225 bool bad = (l == ICONV_ILLEGAL_SEQ) || (l == ICONV_BUF_TOO_SMALL); /* conversion error */
226 if (l == 1)
227 bad = bad || (!strchr("-_+=.", *s) && !iswalnum(wc));
228 else
229 bad = bad || !iswalnum(wc);
230 if (bad)
231 {
232 if (dry)
233 return -1;
234 if (l == ICONV_ILLEGAL_SEQ)
235 memset(&mbstate, 0, sizeof(mbstate_t));
236 buf_addch(dest, '_');
237 rc = -1;
238 }
239 else if (!dry)
240 {
241 buf_addstr_n(dest, s, l);
242 }
243 }
244
245 return rc;
246}
size_t buf_addstr_n(struct Buffer *buf, const char *s, size_t len)
Add a string to a Buffer, expanding it if necessary.
Definition buffer.c:109
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:89
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:248
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition charset.h:116
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition charset.h:114
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ string_is_address()

static bool string_is_address ( const char * str,
const char * user,
const char * domain )
static

Does an email address match a user and domain?

Parameters
strAddress string to test
userUser name
domainDomain name
Return values
trueThey match

Definition at line 255 of file alias.c.

256{
257 char buf[1024] = { 0 };
258
259 snprintf(buf, sizeof(buf), "%s@%s", NONULL(user), NONULL(domain));
260 if (mutt_istr_equal(str, buf))
261 return true;
262
263 return false;
264}
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:678
#define NONULL(x)
Definition string2.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_lookup()

struct AddressList * alias_lookup ( const char * name)

Find an Alias.

Parameters
nameAlias name to find
Return values
ptrAddress for the Alias
NULLNo such Alias
Note
The search is case-insensitive

Definition at line 274 of file alias.c.

275{
276 struct Alias **ap = NULL;
277
279 ASSERT(mod_data);
280
281 ARRAY_FOREACH(ap, &mod_data->aliases)
282 {
283 struct Alias *a = *ap;
284
285 if (mutt_istr_equal(name, a->name))
286 return &a->addr;
287 }
288 return NULL;
289}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
@ MODULE_ID_ALIAS
ModuleAlias, Alias
Definition module_api.h:48
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
#define ASSERT(COND)
Definition signal2.h:59
Alias private Module data.
Definition module_data.h:33
struct AliasArray aliases
User's email aliases.
Definition module_data.h:35
A shortcut for an email address or addresses.
Definition alias.h:35
char * name
Short name.
Definition alias.h:36
struct AddressList addr
List of Addresses the Alias expands to.
Definition alias.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_expand_aliases()

void mutt_expand_aliases ( struct AddressList * al)

Expand aliases in a List of Addresses.

Parameters
alAddressList

Duplicate addresses are dropped

Definition at line 297 of file alias.c.

298{
299 // previously expanded aliases to avoid loops
300 struct ListHead expn = STAILQ_HEAD_INITIALIZER(expn);
301
302 expand_aliases_r(al, &expn);
303 mutt_list_free(&expn);
305}
void mutt_addrlist_dedupe(struct AddressList *al)
Remove duplicate addresses.
Definition address.c:1410
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition list.c:123
#define STAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_expand_aliases_env()

void mutt_expand_aliases_env ( struct Envelope * env)

Expand aliases in all the fields of an Envelope.

Parameters
envEnvelope to expand

Definition at line 311 of file alias.c.

312{
314 mutt_expand_aliases(&env->to);
315 mutt_expand_aliases(&env->cc);
319}
void mutt_expand_aliases(struct AddressList *al)
Expand aliases in a List of Addresses.
Definition alias.c:297
struct AddressList to
Email's 'To' list.
Definition envelope.h:60
struct AddressList reply_to
Email's 'reply-to'.
Definition envelope.h:64
struct AddressList mail_followup_to
Email's 'mail-followup-to'.
Definition envelope.h:65
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:

◆ mutt_get_address()

struct AddressList * mutt_get_address ( struct Envelope * env,
const char ** prefix )

Get an Address from an Envelope.

Parameters
[in]envEnvelope to examine
[out]prefixPrefix for the Address, e.g. "To:"
Return values
ptrAddressList in the Envelope
Note
The caller must NOT free the returned AddressList

Definition at line 329 of file alias.c.

330{
331 struct AddressList *al = NULL;
332 const char *pfx = NULL;
333
335 {
336 if (!TAILQ_EMPTY(&env->to) && !mutt_is_mail_list(TAILQ_FIRST(&env->to)))
337 {
338 pfx = "To";
339 al = &env->to;
340 }
341 else
342 {
343 pfx = "Cc";
344 al = &env->cc;
345 }
346 }
347 else if (!TAILQ_EMPTY(&env->reply_to) && !mutt_is_mail_list(TAILQ_FIRST(&env->reply_to)))
348 {
349 pfx = "Reply-To";
350 al = &env->reply_to;
351 }
352 else
353 {
354 al = &env->from;
355 pfx = "From";
356 }
357
358 if (prefix)
359 *prefix = pfx;
360
361 return al;
362}
bool mutt_addr_is_user(const struct Address *addr)
Does the address belong to the user.
Definition alias.c:601
bool mutt_is_mail_list(const struct Address *addr)
Is this the email address of a mailing list?
Definition maillist.c:46
#define TAILQ_EMPTY(head)
Definition queue.h:778
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_create()

void alias_create ( struct AddressList * al,
const struct ConfigSubset * sub )

Create a new Alias from an Address.

Parameters
alAddress to use
subConfig items

Definition at line 369 of file alias.c.

370{
371 struct Buffer *buf = buf_pool_get();
372 struct Buffer *fixed = buf_pool_get();
373 struct Buffer *prompt = NULL;
374 struct Buffer *tmp = buf_pool_get();
375
376 struct Address *addr = NULL;
377 char *pc = NULL;
378 char *err = NULL;
379 FILE *fp_alias = NULL;
380
381 if (al)
382 {
383 addr = TAILQ_FIRST(al);
384 if (addr && addr->mailbox)
385 {
386 buf_copy(tmp, addr->mailbox);
387 pc = strchr(buf_string(tmp), '@');
388 if (pc)
389 *pc = '\0';
390 }
391 }
392
393 /* Don't suggest a bad alias name in the event of a strange local part. */
394 check_alias_name(buf_string(tmp), buf);
395
396retry_name:
397 /* L10N: prompt to add a new alias */
398 if ((mw_get_field(_("Alias as: "), buf, MUTT_COMP_NONE, HC_OTHER, NULL, NULL) != 0) ||
399 buf_is_empty(buf))
400 {
401 goto done;
402 }
403
404 /* check to see if the user already has an alias defined */
405 if (alias_lookup(buf_string(buf)))
406 {
407 mutt_error(_("You already have an alias defined with that name"));
408 goto done;
409 }
410
411 if (check_alias_name(buf_string(buf), fixed))
412 {
413 switch (query_yesorno(_("Warning: This alias name may not work. Fix it?"), MUTT_YES))
414 {
415 case MUTT_YES:
416 buf_copy(buf, fixed);
417 goto retry_name;
418 case MUTT_ABORT:
419 goto done;
420 default:; // do nothing
421 }
422 }
423
424 struct Alias *alias = alias_new();
425 alias->name = buf_strdup(buf);
426
428
429 if (addr && addr->mailbox)
430 buf_copy(buf, addr->mailbox);
431 else
432 buf_reset(buf);
433
434 mutt_addrlist_to_intl(al, NULL);
435
436 do
437 {
438 if ((mw_get_field(_("Address: "), buf, MUTT_COMP_NONE, HC_OTHER, NULL, NULL) != 0) ||
439 buf_is_empty(buf))
440 {
441 alias_free(&alias);
442 goto done;
443 }
444
445 mutt_addrlist_parse(&alias->addr, buf_string(buf));
446 if (TAILQ_EMPTY(&alias->addr))
447 mutt_beep(false);
448 if (mutt_addrlist_to_intl(&alias->addr, &err))
449 {
450 mutt_error(_("Bad IDN: '%s'"), err);
451 FREE(&err);
452 continue;
453 }
454 } while (TAILQ_EMPTY(&alias->addr));
455
456 if (addr && addr->personal && !mutt_is_mail_list(addr))
457 buf_copy(buf, addr->personal);
458 else
459 buf_reset(buf);
460
461 if (mw_get_field(_("Personal name: "), buf, MUTT_COMP_NONE, HC_OTHER, NULL, NULL) != 0)
462 {
463 alias_free(&alias);
464 goto done;
465 }
466
467 TAILQ_FIRST(&alias->addr)->personal = buf_new(buf_string(buf));
468
469 buf_reset(buf);
470 if (mw_get_field(_("Comment: "), buf, MUTT_COMP_NONE, HC_OTHER, NULL, NULL) == 0)
471 {
472 mutt_str_replace(&alias->comment, buf_string(buf));
473 }
474
475 buf_reset(buf);
476 if (mw_get_field(_("Tags (comma-separated): "), buf, MUTT_COMP_NONE, HC_OTHER,
477 NULL, NULL) == 0)
478 {
479 parse_alias_tags(buf_string(buf), &alias->tags);
480 }
481
482 buf_reset(buf);
483 mutt_addrlist_write(&alias->addr, buf, true);
484 prompt = buf_pool_get();
485
486 buf_printf(prompt, "alias %s %s", alias->name, buf_string(buf));
487
488 bool has_tags = STAILQ_FIRST(&alias->tags);
489
490 if (alias->comment || has_tags)
491 buf_addstr(prompt, " #");
492
493 if (alias->comment)
494 buf_add_printf(prompt, " %s", alias->comment);
495
496 if (has_tags)
497 {
498 if (STAILQ_FIRST(&alias->tags))
499 {
500 buf_addstr(prompt, " tags:");
501 alias_tags_to_buffer(&alias->tags, prompt);
502 }
503 }
504
505 buf_add_printf(prompt, "\n%s", _("Accept?"));
506
507 if (query_yesorno(buf_string(prompt), MUTT_YES) != MUTT_YES)
508 {
509 alias_free(&alias);
510 goto done;
511 }
512
514 ASSERT(mod_data);
515
516 alias_reverse_add(alias);
517 ARRAY_ADD(&mod_data->aliases, alias);
518
519 const char *const c_alias_file = cs_subset_path(sub, "alias_file");
520 buf_strcpy(buf, c_alias_file);
521
522 struct FileCompletionData cdata = { false, NULL, NULL, NULL, NULL };
523 if (mw_get_field(_("Save to file: "), buf, MUTT_COMP_CLEAR, HC_FILE,
524 &CompleteFileOps, &cdata) != 0)
525 {
526 goto done;
527 }
528 expand_path(buf, false);
529 fp_alias = mutt_file_fopen(buf_string(buf), "a+");
530 if (!fp_alias)
531 {
532 mutt_perror("%s", buf_string(buf));
533 goto done;
534 }
535
536 /* terminate existing file with \n if necessary */
537 if (ftell(fp_alias) > 0)
538 {
539 if (!mutt_file_seek(fp_alias, -1, SEEK_CUR))
540 {
541 goto done;
542 }
543 if (fread(buf->data, 1, 1, fp_alias) != 1)
544 {
545 mutt_perror(_("Error reading alias file"));
546 goto done;
547 }
548 if (!mutt_file_seek(fp_alias, 0, SEEK_END))
549 {
550 goto done;
551 }
552 if (buf_at(buf, 0) != '\n')
553 fputc('\n', fp_alias);
554 }
555
556 if (check_alias_name(alias->name, NULL))
557 buf_quote_filename(buf, alias->name, true);
558 else
559 buf_strcpy(buf, alias->name);
560
561 recode_buf(buf);
562 fprintf(fp_alias, "alias %s ", buf_string(buf));
563 buf_reset(buf);
564
565 mutt_addrlist_write(&alias->addr, buf, false);
566 recode_buf(buf);
567 write_safe_address(fp_alias, buf_string(buf));
568 if (alias->comment)
569 fprintf(fp_alias, " # %s", alias->comment);
570 if (STAILQ_FIRST(&alias->tags))
571 {
572 fprintf(fp_alias, " tags:");
573
574 struct Tag *tag = NULL;
575 STAILQ_FOREACH(tag, &alias->tags, entries)
576 {
577 fprintf(fp_alias, "%s", tag->name);
578 if (STAILQ_NEXT(tag, entries))
579 fprintf(fp_alias, ",");
580 }
581 }
582 fputc('\n', fp_alias);
583 if (mutt_file_fsync_close(&fp_alias) != 0)
584 mutt_perror(_("Trouble adding alias"));
585 else
586 mutt_message(_("Alias added"));
587
588done:
589 mutt_file_fclose(&fp_alias);
590 buf_pool_release(&buf);
591 buf_pool_release(&fixed);
592 buf_pool_release(&prompt);
593 buf_pool_release(&tmp);
594}
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition address.c:1391
size_t mutt_addrlist_write(const struct AddressList *al, struct Buffer *buf, bool display)
Write an Address to a buffer.
Definition address.c:1219
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition address.c:481
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition address.c:1306
void parse_alias_tags(const char *tags, struct TagList *tl)
Parse a comma-separated list of tags.
Definition commands.c:105
void alias_tags_to_buffer(struct TagList *tl, struct Buffer *buf)
Write a comma-separated list of tags to a Buffer.
Definition commands.c:89
void alias_free(struct Alias **ptr)
Free an Alias.
Definition alias.c:674
static int check_alias_name(const char *s, struct Buffer *dest)
Sanity-check an alias name.
Definition alias.c:213
static void recode_buf(struct Buffer *buf)
Convert some text between two character sets.
Definition alias.c:189
struct Alias * alias_new(void)
Create a new Alias.
Definition alias.c:662
static void write_safe_address(FILE *fp, const char *s)
Defang malicious email addresses.
Definition alias.c:84
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
const struct CompleteOps CompleteFileOps
Auto-Completion of Files.
Definition complete.c:152
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:168
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:211
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:298
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition buffer.c:674
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:233
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition buffer.c:607
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition helpers.c:168
void mutt_beep(bool force)
Irritate the user.
Definition curs_lib.c:68
@ MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition wdata.h:47
@ MUTT_COMP_NONE
No flags are set.
Definition wdata.h:46
void buf_quote_filename(struct Buffer *buf, const char *filename, bool add_outer)
Quote a filename to survive the shell's quoting rules.
Definition file.c:803
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:648
int mutt_file_fsync_close(FILE **fp)
Flush the data, before closing a file (and NULL the pointer)
Definition file.c:128
#define mutt_file_fclose(FP)
Definition file.h:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition window.c:502
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
#define mutt_perror(...)
Definition logging2.h:95
@ HC_FILE
Files.
Definition lib.h:59
@ HC_OTHER
Miscellaneous strings.
Definition lib.h:61
#define _(a)
Definition message.h:28
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
void expand_path(struct Buffer *buf, bool regex)
Create the canonical path.
Definition muttlib.c:122
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition quad.h:37
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:329
#define STAILQ_FIRST(head)
Definition queue.h:388
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
void alias_reverse_add(struct Alias *alias)
Add an email address lookup for an Alias.
Definition reverse.c:62
struct TagList tags
Tags.
Definition alias.h:39
char * comment
Free-form comment string.
Definition alias.h:38
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
Input for the file completion function.
Definition curs_lib.h:39
LinkedList Tag Element.
Definition tags.h:41
char * name
Tag name.
Definition tags.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_addr_is_user()

bool mutt_addr_is_user ( const struct Address * addr)

Does the address belong to the user.

Parameters
addrAddress to check
Return values
trueThe given address belongs to the user

Definition at line 601 of file alias.c.

602{
603 if (!addr)
604 {
605 mutt_debug(LL_DEBUG5, "no, NULL address\n");
606 return false;
607 }
608 if (!addr->mailbox)
609 {
610 mutt_debug(LL_DEBUG5, "no, no mailbox\n");
611 return false;
612 }
613
615 {
616 mutt_debug(LL_DEBUG5, "#1 yes, %s = %s\n", buf_string(addr->mailbox),
618 return true;
619 }
621 {
622 mutt_debug(LL_DEBUG5, "#2 yes, %s = %s @ %s\n", buf_string(addr->mailbox),
624 return true;
625 }
626 const char *fqdn = mutt_fqdn(false, NeoMutt->sub);
628 {
629 mutt_debug(LL_DEBUG5, "#3 yes, %s = %s @ %s\n", buf_string(addr->mailbox),
630 NeoMutt->username, NONULL(fqdn));
631 return true;
632 }
633 fqdn = mutt_fqdn(true, NeoMutt->sub);
635 {
636 mutt_debug(LL_DEBUG5, "#4 yes, %s = %s @ %s\n", buf_string(addr->mailbox),
637 NeoMutt->username, NONULL(fqdn));
638 return true;
639 }
640
641 const struct Address *c_from = cs_subset_address(NeoMutt->sub, "from");
642 if (c_from && mutt_istr_equal(buf_string(c_from->mailbox), buf_string(addr->mailbox)))
643 {
644 mutt_debug(LL_DEBUG5, "#5 yes, %s = %s\n", buf_string(addr->mailbox),
645 buf_string(c_from->mailbox));
646 return true;
647 }
648
650 return true;
651
652 mutt_debug(LL_DEBUG5, "no, all failed\n");
653 return false;
654}
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
static bool string_is_address(const char *str, const char *user, const char *domain)
Does an email address match a user and domain?
Definition alias.c:255
bool mutt_alternates_match(const char *addr)
Compare an Address to the un/alternates lists.
Definition alternates.c:164
char * ShortHostname
Short version of the hostname.
Definition globals.c:36
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
char * username
User's login name.
Definition neomutt.h:56
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_new()

struct Alias * alias_new ( void )

Create a new Alias.

Return values
ptrNewly allocated Alias

Free the result with alias_free()

Definition at line 662 of file alias.c.

663{
664 struct Alias *a = MUTT_MEM_CALLOC(1, struct Alias);
665 TAILQ_INIT(&a->addr);
666 STAILQ_INIT(&a->tags);
667 return a;
668}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
#define STAILQ_INIT(head)
Definition queue.h:410
#define TAILQ_INIT(head)
Definition queue.h:822
+ Here is the caller graph for this function:

◆ alias_free()

void alias_free ( struct Alias ** ptr)

Free an Alias.

Parameters
[out]ptrAlias to free

Definition at line 674 of file alias.c.

675{
676 if (!ptr || !*ptr)
677 return;
678
679 struct Alias *alias = *ptr;
680
681 mutt_debug(LL_NOTIFY, "NT_ALIAS_DELETE: %s\n", alias->name);
682 struct EventAlias ev_a = { alias };
684
685 FREE(&alias->name);
686 FREE(&alias->comment);
689
690 FREE(ptr);
691}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1475
@ NT_ALIAS_DELETE
Alias is about to be deleted.
Definition alias.h:56
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition notify.c:173
@ NT_ALIAS
Alias has changed, NotifyAlias, EventAlias.
Definition notify_type.h:37
An alias-change event.
Definition alias.h:65
struct Alias * alias
Alias that changed.
Definition alias.h:66
struct Notify * notify
Notifications handler.
Definition neomutt.h:45
void driver_tags_free(struct TagList *tl)
Free tags from a header.
Definition tags.c:132
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ aliaslist_clear()

void aliaslist_clear ( struct AliasArray * aa)

Empty a List of Aliases.

Parameters
aaAliasArray to empty

Each Alias will be freed and the AliasArray will be left empty.

Definition at line 699 of file alias.c.

700{
701 if (!aa)
702 return;
703
704 struct Alias **ap = NULL;
705 ARRAY_FOREACH(ap, aa)
706 {
707 alias_free(ap);
708 }
709 ARRAY_FREE(aa);
710}
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
+ Here is the call graph for this function:
+ Here is the caller graph for this function: