NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
commands.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdint.h>
31 #include <stdio.h>
32 #include "mutt/lib.h"
33 #include "address/lib.h"
34 #include "core/lib.h"
35 #include "mutt.h"
36 #include "lib.h"
37 #include "alias.h"
38 #include "command_parse.h"
39 #include "init.h"
40 #include "mutt_commands.h"
41 #include "mutt_logging.h"
42 #include "reverse.h"
43 
49 enum CommandResult parse_alias(struct Buffer *buf, struct Buffer *s,
50  intptr_t data, struct Buffer *err)
51 {
52  struct Alias *tmp = NULL;
53  struct GroupList gl = STAILQ_HEAD_INITIALIZER(gl);
54  enum NotifyAlias event;
55 
56  if (!MoreArgs(s))
57  {
58  mutt_buffer_strcpy(err, _("alias: no address"));
59  return MUTT_CMD_WARNING;
60  }
61 
62  /* name */
64  mutt_debug(LL_DEBUG5, "First token is '%s'\n", buf->data);
65  if (parse_grouplist(&gl, buf, s, err) == -1)
66  {
67  return MUTT_CMD_ERROR;
68  }
69  char *name = mutt_str_dup(buf->data);
70 
71  /* address list */
73  mutt_debug(LL_DEBUG5, "Second token is '%s'\n", buf->data);
74  struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
75  int parsed = mutt_addrlist_parse2(&al, buf->data);
76  if (parsed == 0)
77  {
78  mutt_buffer_printf(err, _("Warning: Bad address '%s' in alias '%s'"), buf->data, name);
79  FREE(&name);
80  goto bail;
81  }
82 
83  /* IDN */
84  char *estr = NULL;
85  if (mutt_addrlist_to_intl(&al, &estr))
86  {
87  mutt_buffer_printf(err, _("Warning: Bad IDN '%s' in alias '%s'"), estr, name);
88  FREE(&name);
89  FREE(&estr);
90  goto bail;
91  }
92 
93  /* check to see if an alias with this name already exists */
94  TAILQ_FOREACH(tmp, &Aliases, entries)
95  {
96  if (mutt_istr_equal(tmp->name, name))
97  break;
98  }
99 
100  if (tmp)
101  {
102  FREE(&name);
104  /* override the previous value */
105  mutt_addrlist_clear(&tmp->addr);
106  FREE(&tmp->comment);
107  event = NT_ALIAS_CHANGED;
108  }
109  else
110  {
111  /* create a new alias */
112  tmp = alias_new();
113  tmp->name = name;
114  TAILQ_INSERT_TAIL(&Aliases, tmp, entries);
115  event = NT_ALIAS_NEW;
116  }
117  tmp->addr = al;
118 
119  mutt_grouplist_add_addrlist(&gl, &tmp->addr);
120 
121  if (C_DebugLevel > LL_DEBUG4)
122  {
123  /* A group is terminated with an empty address, so check a->mailbox */
124  struct Address *a = NULL;
125  TAILQ_FOREACH(a, &tmp->addr, entries)
126  {
127  if (!a->mailbox)
128  break;
129 
130  if (a->group)
131  mutt_debug(LL_DEBUG5, " Group %s\n", a->mailbox);
132  else
133  mutt_debug(LL_DEBUG5, " %s\n", a->mailbox);
134  }
135  }
137  if (!MoreArgs(s) && (s->dptr[0] == '#'))
138  {
139  char *comment = s->dptr + 1;
140  SKIPWS(comment);
141  tmp->comment = mutt_str_dup(comment);
142  }
143 
144  alias_reverse_add(tmp);
145 
146  struct EventAlias ea = { tmp };
147  notify_send(NeoMutt->notify, NT_ALIAS, event, &ea);
148 
149  return MUTT_CMD_SUCCESS;
150 
151 bail:
153  return MUTT_CMD_ERROR;
154 }
155 
159 enum CommandResult parse_unalias(struct Buffer *buf, struct Buffer *s,
160  intptr_t data, struct Buffer *err)
161 {
162  do
163  {
165 
166  struct Alias *np = NULL;
167  if (mutt_str_equal("*", buf->data))
168  {
169  TAILQ_FOREACH(np, &Aliases, entries)
170  {
172  }
173 
175  return MUTT_CMD_SUCCESS;
176  }
177 
178  TAILQ_FOREACH(np, &Aliases, entries)
179  {
180  if (!mutt_istr_equal(buf->data, np->name))
181  continue;
182 
183  TAILQ_REMOVE(&Aliases, np, entries);
185  alias_free(&np);
186  break;
187  }
188  } while (MoreArgs(s));
189  return MUTT_CMD_SUCCESS;
190 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
A shortcut for an email address or addresses.
Definition: alias.h:33
int parse_grouplist(struct GroupList *gl, struct Buffer *buf, struct Buffer *s, struct Buffer *err)
Parse a group context.
Log at debug level 4.
Definition: logging.h:43
char * name
Short name.
Definition: alias.h:35
CommandResult
Error codes for command_t parse functions.
Definition: mutt_commands.h:34
Config/command parsing.
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
Error: Can&#39;t help the user.
Definition: mutt_commands.h:36
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1468
NeoMutt Logging.
Alias has changed, NotifyAlias, EventAlias.
Definition: notify_type.h:44
String manipulation buffer.
Definition: buffer.h:33
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define _(a)
Definition: message.h:28
An alias-change event.
Definition: alias.h:59
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
void mutt_grouplist_add_addrlist(struct GroupList *gl, struct AddressList *al)
Add Address list to a GroupList.
Definition: group.c:226
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
int mutt_addrlist_parse2(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:616
Container for Accounts, Notifications.
Definition: neomutt.h:36
Representation of a single alias to an email address.
Email Address Handling.
#define MoreArgs(buf)
Definition: buffer.h:43
An Alias has been changed.
Definition: alias.h:52
Many unsorted constants and some structs.
Convenience wrapper for the core headers.
#define SKIPWS(ch)
Definition: string2.h:46
struct Alias * alias_new(void)
Create a new Alias.
Definition: alias.c:608
void alias_reverse_add(struct Alias *alias)
Add an email address lookup for an Alias.
Definition: reverse.c:61
enum CommandResult parse_alias(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;alias&#39; command - Implements Command::parse()
Definition: commands.c:49
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:883
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:834
#define MUTT_TOKEN_QUOTE
Don&#39;t interpret quotes.
Definition: mutt.h:74
struct AliasList Aliases
List of all the user&#39;s email aliases.
Definition: alias.c:55
short C_DebugLevel
Config: Logging level for debug logs.
Definition: mutt_logging.c:48
Functions to parse commands in a config file.
int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: init.c:394
A new Alias has been created.
Definition: alias.h:51
char * dptr
Current read/write position.
Definition: buffer.h:36
#define MUTT_TOKEN_SPACE
Don&#39;t treat whitespace as a term.
Definition: mutt.h:73
char * data
Pointer to data.
Definition: buffer.h:35
Definitions of NeoMutt commands.
char * comment
Free-form comment string.
Definition: alias.h:37
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:802
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
void alias_free(struct Alias **ptr)
Free an Alias.
Definition: alias.c:619
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
#define MUTT_TOKEN_SEMICOLON
Don&#39;t treat ; as special.
Definition: mutt.h:77
void aliaslist_free(struct AliasList *al)
Free a List of Aliases.
Definition: alias.c:639
Success: Command worked.
Definition: mutt_commands.h:38
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1304
bool group
Group mailbox?
Definition: address.h:38
Warning: Help given to the user.
Definition: mutt_commands.h:37
enum CommandResult parse_unalias(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;unalias&#39; command - Implements Command::parse()
Definition: commands.c:159
NotifyAlias
Alias notification types.
Definition: alias.h:49
void mutt_grouplist_destroy(struct GroupList *gl)
Free a GroupList.
Definition: group.c:157
#define FREE(x)
Definition: memory.h:40
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:321
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:36
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
void alias_reverse_delete(struct Alias *alias)
Remove an email address lookup for an Alias.
Definition: reverse.c:83
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:630
Manage alias reverse lookups.
#define MUTT_TOKEN_NO_FLAGS
No flags are set.
Definition: mutt.h:70
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:152