1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include "mail.h"
18 #include <pwd.h>
19 
20 static mu_list_t alternate_names = NULL;
21 static char *my_email;
22 static char *my_name;
23 
24 /*
25  * alt[ernates] name...
26  */
27 
28 int
mail_alt(int argc,char ** argv)29 mail_alt (int argc, char **argv)
30 {
31   if (argc == 1)
32     {
33       if (alternate_names)
34 	{
35 	  util_slist_print (alternate_names, 0);
36 	  mu_printf ("\n");
37 	}
38     }
39   else
40     {
41       util_slist_destroy (&alternate_names);
42       while (--argc)
43 	util_slist_add (&alternate_names, *++argv);
44     }
45   return 0;
46 }
47 
48 char *
mail_whoami()49 mail_whoami ()
50 {
51   return my_name;
52 }
53 
54 void
mail_set_my_name(char * name)55 mail_set_my_name (char *name)
56 {
57   if (!name)
58     {
59       struct passwd *pw = getpwuid (getuid ());
60       if (!pw)
61 	{
62 	  mu_error (_("Cannot determine my username"));
63 	  exit (1);
64 	}
65       name = pw->pw_name;
66     }
67   my_name = mu_strdup (name);
68   my_email = mu_get_user_email (name);
69   if (!my_email)
70     {
71       mu_error(_("Cannot determine my email address: %s"),
72 		 mu_strerror (errno));
73       exit (1);
74     }
75 }
76 
77 int
mail_is_my_name(const char * name)78 mail_is_my_name (const char *name)
79 {
80   if (strchr(name, '@') == NULL && mu_c_strcasecmp (name, my_name) == 0)
81     return 1;
82   if (mu_c_strcasecmp (name, my_email) == 0)
83     return 1;
84   return util_slist_lookup (alternate_names, name);
85 }
86