Annotation of researchv8dc/cmd/upas/rfc822/from822.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <ctype.h>
        !             3: #include "mail.h"
        !             4: #include "header.h"
        !             5: #include "string.h"
        !             6: 
        !             7: /*
        !             8:  *     Convert the rfc822 message on standard input into `UNIX' format
        !             9:  *     and write it onto the passed FILE. 
        !            10:  */
        !            11: 
        !            12: /* header tags */
        !            13: header hdrs[] = {
        !            14:        HEADER("Date:"),
        !            15:        HEADER("From:"),
        !            16:        HEADER("Sender:"),
        !            17:        HEADER("Received:"),
        !            18:        HEADER("Message-Id:"),
        !            19:        HEADER("Status:"),
        !            20:        HEADER("")
        !            21: };
        !            22: #define datehdr hdrs[0]
        !            23: #define fromhdr hdrs[1]
        !            24: #define senderhdr hdrs[2]
        !            25: #define receivedhdr hdrs[3]
        !            26: #define idhdr hdrs[4]
        !            27: #define statushdr hdrs[5]
        !            28: #define hideoushdr hdrs[6]
        !            29: 
        !            30: #ifdef SILLY
        !            31: /* silly fields */
        !            32: header silly[] = {
        !            33: #include "silly.name"
        !            34: };
        !            35: #define NSILLY sizeof(silly)/sizeof(header)
        !            36: #endif SILLY
        !            37: 
        !            38: /* imported */
        !            39: extern void getheader();
        !            40: extern void printheaders();
        !            41: extern void addheader();
        !            42: extern char *gets();
        !            43: extern void putrfunix();
        !            44: 
        !            45: /* predeclared */
        !            46: static int getfrom();
        !            47: static void getdate();
        !            48: extern char *convertaddr();
        !            49: 
        !            50: from822(netname, fp)
        !            51: char *netname;
        !            52: FILE *fp;
        !            53: {
        !            54:        register int rv;
        !            55:        char from[FROMLINESIZE];
        !            56:        char date[FROMLINESIZE];
        !            57:        char line[2*FROMLINESIZE];
        !            58: 
        !            59:        getheader();
        !            60:        from[0] = date[0] = '\0';
        !            61: 
        !            62:        /*  Get sender's address.  If anything else is on the from line,
        !            63:         *  keep it under another name.  If no `From:' lineis found, use
        !            64:         *  `Sender:' line.
        !            65:         */
        !            66:        if (fromhdr.line != NULL) {
        !            67:                rv = getfrom(fromhdr.line, from);
        !            68:        } else if (senderhdr.line != NULL)
        !            69:                getfrom(senderhdr.line, from);
        !            70:        
        !            71:        /*  Get date line */
        !            72:        if (datehdr.line != NULL)
        !            73:                getdate(datehdr.line, date);
        !            74: 
        !            75:        /* output UNIX header */
        !            76:        if (*from != '\0' && *date != '\0')
        !            77:                putrfunix(convertaddr(from), date, netname, fp);
        !            78: 
        !            79:        /* output the rest */
        !            80:        if (rv > 0) {
        !            81: #ifdef SILLY
        !            82:                srand(time((long *)0));
        !            83:                fprintf(fp, "%s%s", silly[nrand(NSILLY)].name, fromhdr.line);
        !            84: #else
        !            85:                fprintf(fp, "%s%s", "Original-From: ", fromhdr.line);
        !            86: #endif
        !            87:        }
        !            88:        if (senderhdr.line != NULL)
        !            89:                fprintf(fp, "%s%s", senderhdr.name, senderhdr.line);
        !            90:        printheaders(fp);
        !            91:        while (gets(line) != NULL)
        !            92:                fprintf(fp, "%s\n", line);
        !            93: }
        !            94: 
        !            95: /*
        !            96:  *  The sender is either the next first whitespace delimited token or
        !            97:  *  the first thing enclosed in "<" ">".
        !            98:  *
        !            99:  *  Returns:  0                if a from line with only the address
        !           100:  *          >0         if a from line with other cruft in it
        !           101:  */
        !           102: static int
        !           103: getfrom(line, sender)
        !           104: char *line, *sender;
        !           105: {
        !           106:        register char *lp, *sp;
        !           107:        register int comment = 0;
        !           108:        register int anticomment = 0;
        !           109:        register int inquote = 0;
        !           110:        int rv = 0;
        !           111: 
        !           112:        lp = line;
        !           113:        for (sp = sender; *lp; lp++) {
        !           114:                if (comment) {
        !           115:                        if (*lp==')')
        !           116:                                comment = 0;
        !           117:                        continue;
        !           118:                }
        !           119:                if (anticomment) {
        !           120:                        if (*lp=='>')
        !           121:                                break;
        !           122:                }
        !           123:                if (inquote) {
        !           124:                        if (*lp=='"')
        !           125:                                inquote = 0;
        !           126:                        *sp++ = *lp;
        !           127:                        continue;
        !           128:                }
        !           129:                switch (*lp) {
        !           130:                case '\t':
        !           131:                case '\n':
        !           132:                        break;
        !           133:                case ' ':
        !           134:                        if (strncmp(lp, " at ", sizeof(" at ")-1)==0) {
        !           135:                                *sp++ = '@';
        !           136:                                lp += sizeof(" at ")-2;
        !           137:                        }
        !           138:                        break;
        !           139:                case '<':
        !           140:                        rv++;
        !           141:                        anticomment = 1;
        !           142:                        sp = sender;
        !           143:                        break;
        !           144:                case '(':
        !           145:                        rv++;
        !           146:                        comment = 1;
        !           147:                        break;
        !           148:                case ',':
        !           149:                        sp = sender;
        !           150:                        break;
        !           151:                case '"':
        !           152:                        inquote = 1;
        !           153:                        /* fall through */
        !           154:                default:
        !           155:                        *sp++ = *lp;
        !           156:                        break;
        !           157:                }
        !           158:        }
        !           159:        *sp = '\0';
        !           160:        return rv;
        !           161: }
        !           162: 
        !           163: /*
        !           164:  *  Get a date line.  Convert to `UNIX' format.
        !           165:  *
        !           166:  */
        !           167: static void
        !           168: getdate(line, date)
        !           169: char *line, *date;
        !           170: {
        !           171:        register char *sp;
        !           172: 
        !           173:        sp = line + datehdr.size;
        !           174:        while (isspace(*sp) || *sp == ',' || *sp == '-')
        !           175:                sp++;
        !           176:        while (*sp != '\0' && *sp != '\n')
        !           177:                *date++ = *sp++;
        !           178:        *date = '\0';
        !           179: }
        !           180: 
        !           181: /*
        !           182:  *     Convert from to `bang' format. 
        !           183:  */
        !           184: extern char *
        !           185: convertaddr(from)
        !           186: char *from;
        !           187: {
        !           188:        static char buf[FROMLINESIZE];
        !           189:        char *sp;
        !           190: 
        !           191:        buf[0] = '\0';
        !           192:        for (sp = from + strlen(from); sp >= from; sp--) {
        !           193:                if (*sp == '.') {
        !           194:                        strcat(buf, sp+1);
        !           195:                        strcat(buf, "!");
        !           196:                        *sp = '\0';
        !           197:                } else if (*sp == '@' || *sp == '%') {
        !           198:                        strcat(buf, sp+1);
        !           199:                        strcat(buf, "!");
        !           200:                        *sp = '\0';
        !           201:                        break;
        !           202:                }
        !           203:        }
        !           204:        if (strchr(from, '!') != NULL)
        !           205:                strcat(buf, "uucp!");
        !           206:        strcat(buf, from);
        !           207: 
        !           208:        return buf;
        !           209: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.