]>
diplodocus.org Git - nmh/blob - sbr/mf.c
3 * mf.c -- mail filter subroutines
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
19 static char *getcpy (char *);
20 static char *add (char *, char *);
21 static void compress (char *, char *);
22 static int isat (char *);
23 static int parse_address (void);
24 static int phrase (char *);
25 static int route_addr (char *);
26 static int local_part (char *);
27 static int domain (char *);
28 static int route (char *);
29 static int my_lex (char *);
38 /* causes compiles to blow up because the symbol _cleanup is undefined
39 where did this ever come from? */
45 if ((p
= malloc ((size_t) (strlen (s
) + 2))))
52 add (char *s1
, char *s2
)
59 if ((p
= malloc ((size_t) (strlen (s1
) + strlen (s2
) + 2))))
60 sprintf (p
, "%s%s", s2
, s1
);
68 return (strncmp (string
, "From ", 5) == 0
69 || strncmp (string
, ">From ", 6) == 0);
74 lequal (char *a
, char *b
)
80 char c1
= islower (*a
) ? toupper (*a
) : *a
;
81 char c2
= islower (*b
) ? toupper (*b
) : *b
;
91 * seekadrx() is tricky. We want to cover both UUCP-style and ARPA-style
92 * addresses, so for each list of addresses we see if we can find some
93 * character to give us a hint.
97 #define CHKADR 0 /* undertermined address style */
98 #define UNIXDR 1 /* UNIX-style address */
99 #define ARPADR 2 /* ARPAnet-style address */
102 static char *punctuators
= ";<>.()[]";
103 static char *vp
= NULL
;
104 static char *tp
= NULL
;
106 static struct adrx adrxs1
;
110 seekadrx (char *addrs
)
112 static int state
= CHKADR
;
114 register struct adrx
*adrxp
;
117 for (state
= UNIXDR
, cp
= addrs
; *cp
; cp
++)
118 if (strchr(punctuators
, *cp
)) {
125 adrxp
= uucpadrx (addrs
);
130 adrxp
= getadrx (addrs
);
142 * uucpadrx() implements a partial UUCP-style address parser. It's based
143 * on the UUCP notion that addresses are separated by spaces or commas.
148 uucpadrx (char *addrs
)
150 register char *cp
, *wp
, *xp
, *yp
, *zp
;
151 register struct adrx
*adrxp
= &adrxs1
;
154 vp
= tp
= getcpy (addrs
);
155 compress (addrs
, vp
);
164 for (cp
= tp
; isspace (*cp
); cp
++)
172 if ((wp
= strchr(cp
, ',')) == NULL
) {
173 if ((wp
= strchr(cp
, ' ')) != NULL
) {
175 while (isspace (*xp
))
177 if (*xp
!= 0 && isat (--xp
)) {
179 while (isspace (*yp
))
182 if ((zp
= strchr(yp
, ' ')) != NULL
)
201 adrxp
->text
= getcpy (cp
);
203 adrxp
->host
= adrxp
->path
= NULL
;
204 if ((wp
= strrchr(cp
, '@')) != NULL
) {
206 adrxp
->host
= *wp
? wp
: NULL
;
209 for (wp
= cp
+ strlen (cp
) - 4; wp
>= cp
; wp
--)
212 adrxp
->host
= wp
+ 3;
215 adrxp
->pers
= adrxp
->grp
= adrxp
->note
= adrxp
->err
= NULL
;
223 compress (char *fp
, char *tp
)
225 register char c
, *cp
;
227 for (c
= ' ', cp
= tp
; (*tp
= *fp
++) != 0;)
235 if (c
== ' ' && cp
< tp
)
243 return (strncmp (p
, " AT ", 4)
244 && strncmp (p
, " At ", 4)
245 && strncmp (p
, " aT ", 4)
246 && strncmp (p
, " at ", 4) ? FALSE
: TRUE
);
252 * getadrx() implements a partial 822-style address parser. The parser
253 * is neither complete nor correct. It does however recognize nearly all
254 * of the 822 address syntax. In addition it handles the majority of the
255 * 733 syntax as well. Most problems arise from trying to accomodate both.
257 * In terms of 822, the route-specification in
259 * "<" [route] local-part "@" domain ">"
261 * is parsed and returned unchanged. Multiple at-signs are compressed
262 * via source-routing. Recursive groups are not allowed as per the
265 * In terms of 733, " at " is recognized as equivalent to "@".
267 * In terms of both the parser will not complain about missing hosts.
271 * We should not allow addresses like
273 * Marshall T. Rose <MRose@UCI>
275 * but should insist on
277 * "Marshall T. Rose" <MRose@UCI>
279 * Unfortunately, a lot of mailers stupidly let people get away with this.
283 * We should not allow addresses like
287 * but should insist on
291 * Unfortunately, a lot of mailers stupidly let people's UAs get away with
296 * We should not allow addresses like
298 * @UCI:MRose@UCI-750a
300 * but should insist on
302 * Marshall Rose <@UCI:MRose@UCI-750a>
304 * Unfortunately, a lot of mailers stupidly do this.
328 static struct specials special
[] = {
345 static int glevel
= 0;
346 static int ingrp
= 0;
347 static int last_lex
= LX_END
;
349 static char *dp
= NULL
;
350 static char *cp
= NULL
;
351 static char *ap
= NULL
;
352 static char *pers
= NULL
;
353 static char *mbox
= NULL
;
354 static char *host
= NULL
;
355 static char *path
= NULL
;
356 static char *grp
= NULL
;
357 static char *note
= NULL
;
358 static char err
[BUFSIZ
];
359 static char adr
[BUFSIZ
];
361 static struct adrx adrxs2
;
365 getadrx (char *addrs
)
368 register struct adrx
*adrxp
= &adrxs2
;
382 pers
= mbox
= host
= path
= grp
= note
= NULL
;
386 dp
= cp
= getcpy (addrs
? addrs
: "");
396 switch (parse_address ()) {
408 default: /* catch trailing comments */
433 while (isspace (*ap
))
436 sprintf (adr
, "%.*s", cp
- ap
, ap
);
439 bp
= adr
+ strlen (adr
) - 1;
440 if (*bp
== ',' || *bp
== ';' || *bp
== '\n')
449 adrxp
->ingrp
= ingrp
;
451 adrxp
->err
= err
[0] ? err
: NULL
;
464 switch (my_lex (buffer
)) {
467 pers
= getcpy (buffer
);
472 strcpy (err
, "extraneous semi-colon");
485 case LX_LBRK
: /* sigh (2) */
488 case LX_AT
: /* sigh (3) */
490 if (route_addr (buffer
) == NOTOK
)
492 return OK
; /* why be choosy? */
495 sprintf (err
, "illegal address construct (%s)", buffer
);
499 switch (my_lex (buffer
)) {
502 pers
= add (buffer
, add (" ", pers
));
503 more_phrase
: ; /* sigh (1) */
504 if (phrase (buffer
) == NOTOK
)
510 if (route_addr (buffer
) == NOTOK
)
512 if (last_lex
== LX_RBRK
)
514 sprintf (err
, "missing right-bracket (%s)", buffer
);
520 sprintf (err
, "nested groups not allowed (%s)", pers
);
523 grp
= add (": ", pers
);
529 switch (my_lex (buffer
)) {
531 case LX_END
: /* tsk, tsk */
540 return parse_address ();
544 case LX_DOT
: /* sigh (1) */
545 pers
= add (".", pers
);
549 sprintf (err
, "no mailbox in address, only a phrase (%s%s)",
561 mbox
= add (buffer
, pers
);
563 if (route_addr (buffer
) == NOTOK
)
571 if (domain (buffer
) == NOTOK
)
577 strcpy (err
, "extraneous semi-colon");
585 sprintf (err
, "junk after local@domain (%s)", buffer
);
589 case LX_SEMI
: /* no host */
593 if (last_lex
== LX_SEMI
&& glevel
-- <= 0) {
594 strcpy (err
, "extraneous semi-colon");
602 sprintf (err
, "missing mailbox (%s)", buffer
);
609 phrase (char *buffer
)
612 switch (my_lex (buffer
)) {
615 pers
= add (buffer
, add (" ", pers
));
625 route_addr (char *buffer
)
627 register char *pp
= cp
;
629 if (my_lex (buffer
) == LX_AT
) {
630 if (route (buffer
) == NOTOK
)
636 if (local_part (buffer
) == NOTOK
)
641 return domain (buffer
);
643 case LX_SEMI
: /* if in group */
644 case LX_RBRK
: /* no host */
650 sprintf (err
, "no at-sign after local-part (%s)", buffer
);
657 local_part (char *buffer
)
662 switch (my_lex (buffer
)) {
665 mbox
= add (buffer
, mbox
);
669 sprintf (err
, "no mailbox in local-part (%s)", buffer
);
673 switch (my_lex (buffer
)) {
675 mbox
= add (buffer
, mbox
);
686 domain (char *buffer
)
689 switch (my_lex (buffer
)) {
692 host
= add (buffer
, host
);
696 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
700 switch (my_lex (buffer
)) {
702 host
= add (buffer
, host
);
705 case LX_AT
: /* sigh (0) */
706 mbox
= add (host
, add ("%", mbox
));
724 switch (my_lex (buffer
)) {
727 path
= add (buffer
, path
);
731 sprintf (err
, "no sub-domain in domain-part of address (%s)", buffer
);
734 switch (my_lex (buffer
)) {
736 path
= add (buffer
, path
);
738 switch (my_lex (buffer
)) {
743 path
= add (buffer
, path
);
747 sprintf (err
, "no at-sign found for next domain in route (%s)",
754 case LX_AT
: /* XXX */
756 path
= add (buffer
, path
);
760 path
= add (buffer
, path
);
764 sprintf (err
, "no colon found to terminate route (%s)", buffer
);
772 my_lex (char *buffer
)
775 register char c
, *bp
;
780 return (last_lex
= LX_END
);
788 return (last_lex
= LX_END
);
792 for (*bp
++ = c
, i
= 0;;)
796 return (last_lex
= LX_ERR
);
799 if ((c
= *cp
++) == 0) {
801 return (last_lex
= LX_ERR
);
814 note
= note
? add (buffer
, add (" ", note
))
816 return my_lex (buffer
);
825 return (last_lex
= LX_ERR
);
828 if ((c
= *cp
++) == 0) {
830 return (last_lex
= LX_ERR
);
838 return (last_lex
= LX_QSTR
);
846 return (last_lex
= LX_ERR
);
849 if ((c
= *cp
++) == 0) {
851 return (last_lex
= LX_ERR
);
859 return (last_lex
= LX_DLIT
);
864 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
865 if (c
== special
[i
].lx_chr
)
866 return (last_lex
= special
[i
].lx_val
);
869 return (last_lex
= LX_ERR
);
872 if ((c
= *cp
++) == 0)
874 for (i
= 0; special
[i
].lx_chr
!= 0; i
++)
875 if (c
== special
[i
].lx_chr
)
877 if (iscntrl (c
) || isspace (c
))
887 last_lex
= !gotat
|| cp
== NULL
|| strchr(cp
, '<') != NULL
894 legal_person (char *p
)
898 static char buffer
[BUFSIZ
];
902 for (cp
= p
; *cp
; cp
++)
903 for (i
= 0; special
[i
].lx_chr
; i
++)
904 if (*cp
== special
[i
].lx_chr
) {
905 sprintf (buffer
, "\"%s\"", p
);
914 mfgets (FILE *in
, char **bp
)
917 register char *cp
, *dp
, *ep
;
919 static char *pp
= NULL
;
922 if (!(pp
= malloc ((size_t) (len
= BUFSIZ
))))
925 for (ep
= (cp
= pp
) + len
- 2;;) {
926 switch (i
= getc (in
)) {
944 if (cp
== pp
) /* end of headers, gobble it */
946 switch (i
= getc (in
)) {
947 default: /* end of line */
948 case '\n': /* end of headers, save for next call */
952 case ' ': /* continue headers */
956 } /* fall into default case */
963 if (!(dp
= realloc (pp
, (size_t) (len
+= BUFSIZ
)))) {
969 cp
+= dp
- pp
, ep
= (pp
= cp
) + len
- 2;