2 ** mts.c -- definitions for the mail transport system
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.
9 #include <h/mh.h> /* for snprintf() */
18 #ifdef HAVE_SYS_UTSNAME_H
19 # include <sys/utsname.h>
28 static char *tailor_value(unsigned char *);
29 static void getuserinfo(void);
30 static const char *get_mtsconf_pathname(void);
31 static const char *get_mtsuserconf_pathname(void);
32 static void mts_read_conf_file(FILE *fp);
35 ** nmh mail transport interface customization file
37 static char *mtsconf = NMHETCDIR"/mts.conf";
40 /* Cache the username and fullname of the user */
41 static char username[BUFSIZ];
42 static char fullname[BUFSIZ];
45 ** Global MailDelivery file
47 char *maildelivery = NMHETCDIR"/maildelivery";
50 ** Customize the MTS settings for nmh by adjusting
51 ** the file mts.conf in the nmh etc directory.
59 static struct bind binds[] = {
60 { "maildelivery", &maildelivery },
66 ** Read the configuration file for the nmh interface
67 ** to the mail transport system (MTS).
75 static int inited = 0;
77 if (inited++ || (fp = fopen(get_mtsconf_pathname(), "r")) == NULL)
79 mts_read_conf_file(fp);
82 cp = get_mtsuserconf_pathname();
84 ((fp = fopen(get_mtsuserconf_pathname(), "r")) != NULL)) {
85 mts_read_conf_file(fp);
94 ** Convert escaped values, malloc some new space,
95 ** and copy string to malloc'ed memory.
99 tailor_value(unsigned char *s)
106 for (bp = buffer; *s; bp++, s++) {
111 case 'b': *bp = '\b'; break;
112 case 'f': *bp = '\f'; break;
113 case 'n': *bp = '\n'; break;
114 case 't': *bp = '\t'; break;
126 r = *s != '0' ? 10 : 8;
127 for (i = 0; isdigit(*s); s++)
128 i = i * r + *s - '0';
137 len = strlen(buffer) + 1;
138 bp = mh_xmalloc(len);
139 memcpy(bp, buffer, len);
145 ** Get the fully qualified name of the local host.
151 static char buffer[BUFSIZ] = "";
152 struct addrinfo hints, *res;
157 /* check if we have cached the local name */
163 memset(buffer, 0, sizeof(buffer));
165 /* first get our local name */
167 strncpy(buffer, name.nodename, sizeof(buffer) - 1);
169 /* first get our local name */
170 gethostname(buffer, sizeof(buffer) - 1);
172 /* now fully qualify our name */
174 memset(&hints, 0, sizeof(hints));
175 hints.ai_flags = AI_CANONNAME;
176 hints.ai_family = PF_UNSPEC;
177 if (getaddrinfo(buffer, NULL, &hints, &res) == 0) {
178 strncpy(buffer, res->ai_canonname, sizeof(buffer) - 1);
187 ** This is only for UUCP mail. It gets the hostname
188 ** as part of the UUCP "domain".
194 static char buffer[BUFSIZ] = "";
200 /* check if we have cached the system name */
208 strncpy(buffer, name.nodename, sizeof(buffer));
210 gethostname(buffer, sizeof(buffer));
218 ** Get the username of current user
224 if (username[0] == '\0')
232 ** Get full name of current user (typically from GECOS
233 ** field of password file).
239 if (username[0] == '\0')
247 ** Find the user's username and full name, and cache them.
252 register unsigned char *cp;
254 register struct passwd *pw;
256 if ((pw = getpwuid(getuid())) == NULL
257 || pw->pw_name == NULL
258 || *pw->pw_name == '\0') {
259 strncpy(username, "unknown", sizeof(username));
260 snprintf(fullname, sizeof(fullname), "The Unknown User-ID (%d)",
268 ** Get the user's real name from the GECOS field. Stop once
269 ** we hit a ',', which some OSes use to separate other 'finger'
270 ** information in the GECOS field, like phone number.
272 for (cp = fullname; *np != '\0' && *np != ',';) {
277 ** On BSD(-derived) systems, the system utilities that
278 ** deal with the GECOS field (finger, mail, sendmail,
279 ** etc.) translate any '&' character in it to the login name,
280 ** with the first letter capitalized. So, for instance,
281 ** fingering a user "bob" with the GECOS field "& Jones"
282 ** would reveal him to be "In real life: Bob Jones".
283 ** Surprisingly, though, the OS doesn't do the translation
284 ** for you, so we have to do it manually here.
286 if (*np == '&') { /* blech! */
287 strcpy(cp, pw->pw_name);
298 strncpy(username, pw->pw_name, sizeof(username));
301 ** The $SIGNATURE environment variable overrides the GECOS field's
302 ** idea of your real name.
304 if ((cp = getenv("SIGNATURE")) && *cp)
305 strncpy(fullname, cp, sizeof(fullname));
307 if (strchr(fullname, '.')) { /* quote any .'s */
310 /* should quote "'s too */
311 snprintf(tmp, sizeof(tmp), "\"%s\"", fullname);
312 strncpy(fullname, tmp, sizeof(fullname));
319 get_mtsconf_pathname(void)
321 const char *cp = getenv( "MHMTSCONF ");
322 if (cp != NULL && *cp != '\0') {
329 get_mtsuserconf_pathname(void)
331 const char *cp = getenv( "MHMTSUSERCONF" );
332 if (cp != NULL && *cp != '\0') {
339 mts_read_conf_file(FILE *fp)
342 char *cp, buffer[BUFSIZ];
345 while (fgets(buffer, sizeof(buffer), fp)) {
346 if (!(cp = strchr(buffer, '\n')))
349 if (*buffer == '#' || *buffer == '\0')
351 if (!(bp = strchr(buffer, ':')))
357 for (b = binds; b->keyword; b++)
358 if (strcmp(buffer, b->keyword)==0)
360 if (b->keyword && (cp = tailor_value(bp)))