3 * addrsbr.c -- parse addresses 822-style
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
15 /* High level parsing of addresses:
17 The routines in sbr/mf.c parse the syntactic representations of
18 addresses. The routines in sbr/addrsbr.c associate semantics with those
21 The comments below are left in for historical purposes; DUMB and
22 REALLYDUMB are now the default in the code.
24 If #ifdef DUMB is in effect, a full 822-style parser is called
25 for syntax recongition. This breaks each address into its components.
26 Note however that no semantics are assumed about the parts or their
27 totality. This means that implicit hostnames aren't made explicit,
28 and explicit hostnames aren't expanded to their "official" represenations.
30 If DUMB is not in effect, then this module does some
31 high-level thinking about what the addresses are.
35 string%<uucp>@<local> -> string
37 2. for non-MMDF systems:
39 string@host.<uucp> -> host!string
41 3. for any system, an address interpreted relative to the local host:
43 string@<uucp> -> string
45 For cases (1) and (3) above, the leftmost host is extracted. If it's not
46 present, the local host is used. If the tests above fail, the address is
47 considered to be a real 822-style address.
49 If an explicit host is not present, then MH checks for a bang to indicate
50 an explicit UUCP-style address. If so, this is noted. If not, the host is
51 defaulted, typically to the local host. The lack of an explict host is
54 If an explicit 822-style host is present, then MH checks to see if it
55 can expand this to the official name for the host. If the hostname is
56 unknown, the address is so typed.
58 To summarize, when we're all done, here's what MH knows about the address:
60 DUMB - type: local, uucp, or network
61 host: not locally defaulted, not explicitly expanded
64 other - type: local, uucp, network, unknown
70 static char *pers = NULL;
71 static char *mbox = NULL;
72 static char *host = NULL;
73 static char *route = NULL;
74 static char *grp = NULL;
75 static char *note = NULL;
76 static char err[BUFSIZ];
77 static char adr[BUFSIZ];
85 pers = mbox = host = route = grp = note = NULL;
88 if ((ap = getadrx (addrs ? addrs : "")) == NULL)
91 strncpy (adr, ap->text, sizeof(adr));
99 if (ap->err && *ap->err)
100 strncpy (err, ap->err, sizeof(err));
107 getm (char *str, char *dfhost, int dftype, int wanthost, char *eresult)
114 strcpy (eresult, err);
116 if (wanthost == AD_HOST)
117 admonish (NULL, "bad address '%s' - %s", str, err);
121 && mbox == NULL && host == NULL && route == NULL
124 strcpy (eresult, "null address");
126 if (wanthost == AD_HOST)
127 admonish (NULL, "null address '%s'", str);
130 if (mbox == NULL && grp == NULL) {
132 strcpy (eresult, "no mailbox in address");
134 if (wanthost == AD_HOST)
135 admonish (NULL, "no mailbox in address '%s'", str);
139 if (dfhost == NULL) {
140 dfhost = LocalName (0);
144 mp = (struct mailname *) calloc ((size_t) 1, sizeof(*mp));
147 strcpy (eresult, "insufficient memory to represent address");
149 if (wanthost == AD_HOST)
150 adios (NULL, "insufficient memory to represent address");
155 mp->m_text = getcpy (str);
157 mp->m_pers = getcpy (pers);
160 mp->m_type = BADHOST;
163 mp->m_gname = getcpy (grp);
165 mp->m_note = getcpy (note);
170 mp->m_mbox = getcpy (mbox);
171 mp->m_host = getcpy (host);
174 if ((pp = strchr(mbox, '!'))) {
176 mp->m_mbox = getcpy (pp);
177 mp->m_host = getcpy (mbox);
178 mp->m_type = UUCPHOST;
182 mp->m_mbox = getcpy (mbox);
183 if (route == NULL && dftype == LOCALHOST) {
189 mp->m_host = route ? NULL : getcpy (dfhost);
190 mp->m_type = route ? NETHOST : dftype;
196 /* For alternate mailboxes, m_type gets overwritten in ismymbox ()
197 to support wild-card matching. */
198 if (wanthost == AD_NHST)
199 mp->m_type = !mh_strcasecmp (LocalName (0), mp->m_host)
200 ? LOCALHOST : NETHOST;
202 mp->m_type = mh_strcasecmp (LocalName(0), mp->m_host) ? NETHOST : LOCALHOST;
206 mp->m_path = getcpy (route);
209 mp->m_gname = getcpy (grp);
211 mp->m_note = getcpy (note);
218 mnfree (struct mailname *mp)
242 #define empty(s) ((s) ? (s) : "")
245 auxformat (struct mailname *mp, int extras)
247 static char addr[BUFSIZ];
248 static char buffer[BUFSIZ];
251 strncpy (addr, mp->m_mbox ? mp->m_mbox : "", sizeof(addr));
254 if (mp->m_type != UUCPHOST)
255 snprintf (addr, sizeof(addr), mp->m_host ? "%s%s@%s" : "%s%s",
256 empty(mp->m_path), empty(mp->m_mbox), mp->m_host);
258 snprintf (addr, sizeof(addr), "%s!%s", mp->m_host, mp->m_mbox);
263 if (mp->m_pers || mp->m_path) {
265 snprintf (buffer, sizeof(buffer), "%s %s <%s>",
266 legal_person (mp->m_pers ? mp->m_pers : mp->m_mbox),
269 snprintf (buffer, sizeof(buffer), "%s <%s>",
270 legal_person (mp->m_pers ? mp->m_pers : mp->m_mbox),
275 snprintf (buffer, sizeof(buffer), "%s %s", addr, mp->m_note);
277 strncpy (buffer, addr, sizeof(buffer));
284 * This used to be adrsprintf() (where it would format an address for you
285 * given a username and a domain). But somewhere we got to the point where
286 * the only caller was post, and it only called it with both arguments NULL.
287 * So the function was renamed with a more sensible name.
295 username = getusername();
302 #define W_MBEG 0x0001
303 #define W_MEND 0x0002
304 #define W_MBOX (W_MBEG | W_MEND)
305 #define W_HBEG 0x0004
306 #define W_HEND 0x0008
307 #define W_HOST (W_HBEG | W_HEND)
308 #define WBITS "\020\01MBEG\02MEND\03HBEG\04HEND"
311 * Check if this is my address
315 ismymbox (struct mailname *np)
323 static char *am = NULL;
324 static struct mailname mq;
325 static int localmailbox = 0;
328 * If this is the first call, initialize
329 * list of alternate mailboxes.
333 mq.m_mbox = getusername ();
335 if ((am = context_find ("local-mailbox"))) {
339 if ((cp = getname(am)) == NULL) {
340 admonish (NULL, "Unable to find address in local-mailbox");
344 if ((mq.m_next = getm (cp, NULL, 0, AD_NAME, NULL)) == NULL) {
345 admonish (NULL, "invalid entry in local-mailbox: %s", cp);
350 * Sigh, it turns out that the address parser gets messed up
351 * if you don't call getname() until it returns NULL.
354 while ((cp = getname(am)) != NULL)
358 if ((am = context_find ("alternate-mailboxes")) == NULL)
361 mp = mq.m_next ? mq.m_next : &mq;
363 while ((cp = getname (am))) {
364 if ((mp->m_next = getm (cp, NULL, 0, AD_NAME, NULL)) == NULL) {
365 admonish (NULL, "illegal address: %s", cp);
370 if (*mp->m_mbox == '*') {
371 mp->m_type |= W_MBEG;
374 if (*(cp = mp->m_mbox + strlen (mp->m_mbox) - 1) == '*') {
375 mp->m_type |= W_MEND;
379 if (*mp->m_host == '*') {
380 mp->m_type |= W_HBEG;
383 if (*(cp = mp->m_host + strlen (mp->m_host) - 1) == '*') {
384 mp->m_type |= W_HEND;
391 advise (NULL, "please fix the %s: entry in your %s file",
392 "alternate-mailboxes", mh_profile);
395 if ((cp = getenv ("MHWDEBUG")) && *cp) {
396 for (mp = &mq; mp; mp = mp->m_next) {
397 fprintf (stderr, "Local- or Alternate-Mailbox: text=\"%s\" "
398 "mbox=\"%s\" host=\"%s\" %s\n",
399 mp->m_text ? mp->m_text : "", mp->m_mbox,
400 mp->m_host ? mp->m_host : "",
401 snprintb (buffer, sizeof(buffer), (unsigned) mp->m_type,
407 if (np == NULL) /* XXX */
411 * Don't perform this "local" test if we have a Local-Mailbox set
415 switch (np->m_type) {
417 len = strlen (cp = LocalName (0));
418 if (!uprf (np->m_host, cp) || np->m_host[len] != '.')
423 if (mh_strcasecmp (np->m_host, SystemName()))
427 if (!mh_strcasecmp (np->m_mbox, mq.m_mbox))
436 * Now scan through list of alternate
437 * mailboxes, and check for a match.
439 for (mp = &mq; mp->m_next;) {
442 continue; if ((len = strlen (cp = np->m_mbox))
443 < (i = strlen (pp = mp->m_mbox)))
445 switch (mp->m_type & W_MBOX) {
447 if (mh_strcasecmp (cp, pp))
451 if (mh_strcasecmp (cp + len - i, pp))
458 case W_MBEG | W_MEND:
459 if (stringdex (pp, cp) < 0)
466 if (np->m_host == NULL)
468 if ((len = strlen (cp = np->m_host))
469 < (i = strlen (pp = mp->m_host)))
471 switch (mp->m_type & W_HOST) {
473 if (mh_strcasecmp (cp, pp))
477 if (mh_strcasecmp (cp + len - i, pp))
484 case W_HBEG | W_HEND:
485 if (stringdex (pp, cp) < 0)