# /* get_from.c * * Gets the "From:" field from the header of an Arpanet mail file, * or the "Reply-to:" field if one is specifed. * This routine is isolated in a separate file because it is the * only routine in the mail daemon that knows anything about the * format of the mail files. */ #include "rqfile.h" #include "extern.h" #include char *from[] = { /* string to look for */ "From: ", 0 }; char *repto[] = { "Reply-to: ", 0 }; get_from (file, name) /* Get the "Reply-to:" or "From:" field from an Arpanet mail file. * Returns TRUE on success and FALSE on failure. * * Arguments: */ char *file; /* name of mail file */ char *name; /* string to hold from name */ { FILE *mfile; /* mail file */ char linbuf[BIGBUF]; /* buffer for lines */ char fromlin[BIGBUF]; /* contains fromlin */ register char *pname; /* pointer to name in linebuf */ int ix; /* temp index */ register char *lp; /* line ptr */ int start; /* start pos. of name in line */ if ((mfile = fopen (file, "r")) == NULL) return (FALSE); fromlin[0] = 0; for (;;) { /* first look for reply-to */ lp = fgets (linbuf, BIGBUF, mfile); if (lp == NULL || linbuf[0] == '\n') break; if ((pname = min_ix_in_str (linbuf, repto, &ix)) == linbuf) { start = strlen (repto[0]); strcpy (fromlin, &linbuf[start]); break; } else if ((pname = min_ix_in_str (linbuf, from, &ix)) == linbuf) { start = strlen (from[0]); strcpy (fromlin, &linbuf[start]); } } if (fromlin[0] == 0) return (FALSE); canon (fromlin, name); fclose (mfile); return (TRUE); } canon(from, to) /* Canonicalize the user name pointed to by from into the standard format. * This involves removing comments, quotes, and angle brackets, and converting * 'at' into '@', etc. * Leave the result in to. */ register char *from; char *to; { register char *name; register char c; int angles, quoted; c = *from++; name = to; for (;;) { while (c == ' ' || sepchar(c)) c = *from++; if (c == '\0') { *name = '\0'; for (from = (name - 1); from >= to && *from == ' '; from--) *from = '\0'; /* remove trailing blanks */ return; } if (c == '"') { while ((c = *from++) != '\0' && c != '"') *name++ = c; if (c == '"') c = *from++; } while (c != '\0' && !sepchar(c)) { if (c == '(') { while ((c = *from++) != ')' && c != '\0') ; if (c == '\0') break; } else if (c == '<') { name = to; angles = TRUE; } else if (c == '>' && angles) { angles = FALSE; while ((c = *from++) != '\0' && !sepchar(c)) ; break; } else if (c == ' ' && *from == 'a' && *(from+1) == 't' && *(from+2) == ' ') { *name++ = '@'; from += 3; } else *name++ = c; c = *from++; } } } sepchar(ch) register char ch; { if ((ch > '\0' && ch < ' ') || ch == ',') return (TRUE); return (FALSE); }