-void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
-{
- ALIAS *new, *t;
- char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
- char *err = NULL;
- char fixed[LONG_STRING];
- FILE *rc;
- ADDRESS *adr = NULL;
-
- if (cur) {
- adr = mutt_get_address (cur, NULL);
- }
- else if (iadr) {
- adr = iadr;
- }
-
- if (adr && adr->mailbox) {
- strfcpy (buf, adr->mailbox, sizeof (buf));
- if ((pc = strchr (buf, '@')))
- *pc = 0;
- }
- else
- buf[0] = '\0';
-
- /* Don't suggest a bad alias name in the event of a strange local part. */
- mutt_check_alias_name (buf, buf);
-
-retry_name:
- /* add a new alias */
- if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
- return;
-
- /* check to see if the user already has an alias defined */
- if (mutt_lookup_alias (buf)) {
- mutt_error _("You already have an alias defined with that name!");
-
- return;
- }
-
- if (mutt_check_alias_name (buf, fixed)) {
- switch (mutt_yesorno
- (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
- case M_YES:
- strfcpy (buf, fixed, sizeof (buf));
- goto retry_name;
- case -1:
- return;
- }
- }
-
- new = mem_calloc (1, sizeof (ALIAS));
- new->self = new;
- new->name = str_dup (buf);
-
- mutt_addrlist_to_local (adr);
-
- if (adr)
- strfcpy (buf, adr->mailbox, sizeof (buf));
- else
- buf[0] = 0;
-
- mutt_addrlist_to_idna (adr, NULL);
-
- do {
- if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
- mutt_free_alias (&new);
- return;
- }
-
- if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
- BEEP ();
- if (mutt_addrlist_to_idna (new->addr, &err)) {
- mutt_error (_("Error: '%s' is a bad IDN."), err);
- mutt_sleep (2);
- continue;
- }
- }
- while (new->addr == NULL);
-
- if (adr && adr->personal && !mutt_is_mail_list (adr))
- strfcpy (buf, adr->personal, sizeof (buf));
- else
- buf[0] = 0;
-
- if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
- mutt_free_alias (&new);
- return;
- }
- new->addr->personal = str_dup (buf);
-
- buf[0] = 0;
- rfc822_write_address (buf, sizeof (buf), new->addr, 1);
- snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
- if (mutt_yesorno (prompt, M_YES) != M_YES) {
- mutt_free_alias (&new);
- return;
- }
-
- if ((t = Aliases)) {
- while (t->next)
- t = t->next;
- t->next = new;
- }
- else
- Aliases = new;
-
- strfcpy (buf, NONULL (AliasFile), sizeof (buf));
- if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
- return;
- mutt_expand_path (buf, sizeof (buf));
- if ((rc = safe_fopen (buf, "a"))) {
- if (mutt_check_alias_name (new->name, NULL))
- mutt_quote_filename (buf, sizeof (buf), new->name);
- else
- strfcpy (buf, new->name, sizeof (buf));
- fprintf (rc, "alias %s ", buf);
- buf[0] = 0;
- rfc822_write_address (buf, sizeof (buf), new->addr, 0);
- write_safe_address (rc, buf);
- fputc ('\n', rc);
- fclose (rc);
- mutt_message _("Alias added.");
- }
- else
- mutt_perror (buf);
-}
-
-/*
- * Sanity-check an alias name: Only characters which are non-special to both
- * the RFC 822 and the mutt configuration parser are permitted.
- */
-
-static int check_alias_name_char (char c)
-{
- return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
- isalnum ((unsigned char) c));
-}
-
-int mutt_check_alias_name (const char *s, char *d)
-{
- int rv = 0;
-
- for (; *s; s++) {
- if (!check_alias_name_char (*s)) {
- if (!d)
- return -1;
- else {
- *d++ = '_';
- rv = -1;
- }
- }
- else if (d)
- *d++ = *s;
- }
- if (d)
- *d++ = *s;
- return rv;
-}
-