remove unused defines in uip/pick.c
[mmh] / sbr / mts.c
1 /*
2 ** mts.c -- definitions for the mail transport system
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>   /* for snprintf() */
10 #include <h/utils.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <pwd.h>
14 #include <sys/socket.h>
15 #include <netdb.h>
16 #include <unistd.h>
17
18 /*
19 ** static prototypes
20 */
21 static void getuserinfo(void);
22
23 /* Cache the username and fullname of the user */
24 static char username[BUFSIZ];
25 static char fullname[BUFSIZ];
26
27
28 /*
29 ** Get the fully qualified name of the local host.
30 */
31 char *
32 LocalName(void)
33 {
34         static char buffer[BUFSIZ] = "";
35         struct addrinfo hints, *res;
36
37         /* check if we have cached the local name */
38         if (buffer[0])
39                 return buffer;
40
41         memset(buffer, 0, sizeof(buffer));
42         /* first get our local name */
43         gethostname(buffer, sizeof(buffer) - 1);
44
45         /* now fully qualify our name */
46         memset(&hints, 0, sizeof(hints));
47         hints.ai_flags = AI_CANONNAME;
48         hints.ai_family = PF_UNSPEC;
49         if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
50                 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
51                 freeaddrinfo(res);
52         }
53
54         return buffer;
55 }
56
57
58 /*
59 ** Get the username of current user
60 */
61 char *
62 getusername(void)
63 {
64         if (username[0] == '\0')
65                 getuserinfo();
66
67         return username;
68 }
69
70
71 /*
72 ** Get full name of current user (typically from GECOS
73 ** field of password file).
74 */
75 char *
76 getfullname(void)
77 {
78         if (username[0] == '\0')
79                 getuserinfo();
80
81         return fullname;
82 }
83
84
85 /*
86 ** Find the user's username and full name, and cache them.
87 */
88 static void
89 getuserinfo(void)
90 {
91         unsigned char *cp;
92         char *np;
93         struct passwd *pw;
94         int needquotes = 0;
95         char tmp[BUFSIZ];
96         char *tp;
97
98         if (!(pw = getpwuid(getuid())) || !pw->pw_name || !*pw->pw_name) {
99                 strncpy(username, "unknown", sizeof(username));
100                 snprintf(fullname, sizeof(fullname),
101                                 "The Unknown User-ID (%d)", (int)getuid());
102                 return;
103         }
104
105         np = pw->pw_gecos;
106
107         /*
108         ** Get the user's real name from the GECOS field.  Stop once
109         ** we hit a ',', which some OSes use to separate other 'finger'
110         ** information in the GECOS field, like phone number.
111         */
112         for (cp = tmp; *np != '\0' && *np != ',';) {
113                 *cp++ = *np++;
114         }
115         *cp = '\0';
116         strncpy(username, pw->pw_name, sizeof(username));
117
118         /*
119         ** The $SIGNATURE environment variable overrides the GECOS field's
120         ** idea of your real name.
121         */
122         if ((cp = getenv("SIGNATURE")) && *cp)
123                 strncpy(tmp, cp, sizeof(tmp));
124
125         /* quote the fullname as needed */
126         needquotes = 0;
127         for (tp=tmp; *tp; tp++) {
128                 switch (*tp) {
129                 case '(': case ')': case '<': case '>': case '[': case ']':
130                 case ':': case ';': case '@': case '\\': case ',': case '.':
131                 case '"':  /* cf. RFC 5322 */
132                         break;  /* ... the switch */
133                 default:
134                         continue;  /* ... the loop */
135                 }
136                 /* we've found a special char */
137                 needquotes = 1;
138                 break;
139         }
140         cp=fullname;
141         if (needquotes) {
142                 *cp++ = '"';
143         }
144         for (tp=tmp; *tp; *cp++=*tp++) {
145                 if (*tp == '"') {
146                         *cp++ = '\\';  /* prepend backslash */
147                 }
148         }
149         if (needquotes) {
150                 *cp++ = '"';
151         }
152         *cp = '\0';
153
154         return;
155 }