Annotation of 43BSDReno/contrib/mh/uip/inc.c, revision 1.1.1.1

1.1       root        1: /* inc.c - incorporate messages from a maildrop into a folder */
                      2: 
                      3: #include "../h/mh.h"
                      4: #include "../h/formatsbr.h"
                      5: #include "../h/scansbr.h"
                      6: #include "../zotnet/tws.h"
                      7: #include <stdio.h>
                      8: #include "../zotnet/mts.h"
                      9: #include <errno.h>
                     10: #include <signal.h>
                     11: #include <sys/types.h>
                     12: #include <sys/stat.h>
                     13: 
                     14: /*  */
                     15: 
                     16: static struct swit switches[] = {
                     17: #define        AUDSW   0
                     18:     "audit audit-file", 0,
                     19: #define        NAUDSW  1
                     20:     "noaudit", 0,
                     21: 
                     22: #define        CHGSW   2
                     23:     "changecur", 0,
                     24: #define        NCHGSW  3
                     25:     "nochangecur", 0,
                     26: 
                     27: #define        DECRSW  4
                     28:     "decrypt",
                     29:        -7,
                     30: #define        NDECRSW 5
                     31:     "nodecrypt",
                     32:        -9,
                     33: 
                     34: #define        MSW     6
                     35:     "file name", 0,
                     36: 
                     37: #define        FORMSW  7
                     38:     "form formatfile", 0,
                     39: #define        FMTSW   8
                     40:     "format string", 5,
                     41: 
                     42: #define        HOSTSW  9
                     43:     "host host",
                     44:        -4,
                     45: #define        USERSW  10
                     46:     "user user",
                     47:        -4,
                     48: #define        PACKSW  11
                     49:     "pack file",
                     50:        -4,
                     51: #define        NPACKSW 12
                     52:     "nopack",
                     53:        -6,
                     54: #define        RPOPSW  13
                     55:     "rpop",
                     56: #ifndef        RPOP
                     57:        -4,
                     58: #else  RPOP
                     59:        0,
                     60: #endif RPOP
                     61: #define        NRPOPSW 14
                     62:     "norpop",
                     63: #ifndef        RPOP
                     64:        -6,
                     65: #else  RPOP
                     66:        0,
                     67: #endif RPOP
                     68: 
                     69: #define        SILSW   15
                     70:     "silent", 0,
                     71: #define        NSILSW  16
                     72:     "nosilent", 0,
                     73: 
                     74: #define        TRNCSW  17
                     75:     "truncate", 0,
                     76: #define        NTRNCSW 18
                     77:     "notruncate", 0,
                     78: 
                     79: #define        UUCPSW  19
                     80:     "uucp",
                     81: #ifndef        MF
                     82:        -4,
                     83: #else  MF
                     84:        0,
                     85: #endif MF
                     86: #define        NUUCPSW 20
                     87:     "nouucp",
                     88: #ifndef        MF
                     89:        -6,
                     90: #else  MF
                     91:        0,
                     92: #endif MF
                     93: 
                     94: #define        WIDSW   21
                     95:     "width columns", 0,
                     96: 
                     97: #define        HELPSW  22
                     98:     "help", 4,
                     99: 
                    100:     NULL, NULL
                    101: };
                    102: 
                    103: /*  */
                    104: 
                    105: extern int  errno;
                    106: extern struct msgs *fmt_current_folder;
                    107: 
                    108: 
                    109: /*  * /
                    110: 
                    111: /* ARGSUSED */
                    112: 
                    113: main(argc, argv)
                    114:        int argc;
                    115:        char *argv[];
                    116: {
                    117:     int     chgflag = 1,
                    118:            trnflag = 1,
                    119:            decflag = 1,
                    120:             noisy = 1,
                    121:            width = 0,
                    122:            uucp = 1,
                    123:            locked = 0,
                    124:            rpop = 1,
                    125:             i,
                    126:            hghnum,
                    127:             msgnum;
                    128:     char   *cp,
                    129:            *maildir,
                    130:            *folder = NULL,
                    131:           *form = NULL,
                    132:           *format = NULL,
                    133:            *audfile = NULL,
                    134:            *from = NULL,
                    135:           *host = NULL,
                    136:           *user = NULL,
                    137:            *newmail,
                    138:             buf[100],
                    139:           **ap,
                    140:           **argp,
                    141:            *nfs,
                    142:            *arguments[MAXARGS];
                    143:     struct msgs *mp;
                    144:     struct stat st,
                    145:                 s1;
                    146:     FILE *in, *aud = NULL;
                    147: #ifdef MHE
                    148:     FILE *mhe = NULL;
                    149: #endif MHE
                    150: 
                    151:     invo_name = r1bindex (argv[0], '/');
                    152:     mts_init (invo_name);
                    153:     if ((cp = m_find (invo_name)) != NULL) {
                    154:        ap = brkstring (cp = getcpy (cp), " ", "\n");
                    155:        ap = copyip (ap, arguments);
                    156:     }
                    157:     else
                    158:        ap = arguments;
                    159:     (void) copyip (argv + 1, ap);
                    160:     argp = arguments;
                    161: 
                    162: /*  */
                    163: 
                    164:     while (cp = *argp++) {
                    165:        if (*cp == '-')
                    166:            switch (smatch (++cp, switches)) {
                    167:                case AMBIGSW:
                    168:                    ambigsw (cp, switches);
                    169:                    done (1);
                    170:                case UNKWNSW:
                    171:                    adios (NULLCP, "-%s unknown", cp);
                    172:                case HELPSW:
                    173:                    (void) sprintf (buf, "%s [+folder] [switches]", invo_name);
                    174:                    help (buf, switches);
                    175:                    done (1);
                    176: 
                    177:                case AUDSW:
                    178:                    if (!(cp = *argp++) || *cp == '-')
                    179:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    180:                    audfile = getcpy (m_maildir (cp));
                    181:                    continue;
                    182:                case NAUDSW:
                    183:                    audfile = NULL;
                    184:                    continue;
                    185: 
                    186:                case CHGSW:
                    187:                    chgflag++;
                    188:                    continue;
                    189:                case NCHGSW:
                    190:                    chgflag = 0;
                    191:                    continue;
                    192: 
                    193:                case TRNCSW:
                    194:                    trnflag++;
                    195:                    continue;
                    196:                case MSW:
                    197:                    if (!(cp = *argp++) || *cp == '-')
                    198:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    199:                    from = path (cp, TFILE);/* fall */
                    200:                case NTRNCSW:
                    201:                    trnflag = 0;
                    202:                    continue;
                    203: 
                    204:                case SILSW:
                    205:                    noisy = 0;
                    206:                    continue;
                    207:                case NSILSW:
                    208:                    noisy++;
                    209:                    continue;
                    210: 
                    211:                case FORMSW:
                    212:                    if (!(form = *argp++) || *form == '-')
                    213:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    214:                    format = NULL;
                    215:                    continue;
                    216:                case FMTSW:
                    217:                    if (!(format = *argp++) || *format == '-')
                    218:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    219:                    form = NULL;
                    220:                    continue;
                    221: 
                    222:                case WIDSW:
                    223:                    if (!(cp = *argp++) || *cp == '-')
                    224:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    225:                    width = atoi (cp);
                    226:                    continue;
                    227: 
                    228:                case DECRSW:
                    229:                    decflag++;
                    230:                    continue;
                    231:                case NDECRSW:
                    232:                    decflag = 0;
                    233:                    continue;
                    234: 
                    235:                case UUCPSW:
                    236:                    uucp++;
                    237:                    continue;
                    238:                case NUUCPSW:
                    239:                    uucp = 0;
                    240:                    continue;
                    241: 
                    242:                case HOSTSW:
                    243:                    if (!(host = *argp++) || *host == '-')
                    244:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    245:                    continue;
                    246:                case USERSW:
                    247:                    if (!(user = *argp++) || *user == '-')
                    248:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    249:                    continue;
                    250:                case PACKSW:
                    251:                    if (!(cp = *argp++) || *cp == '-')
                    252:                        adios (NULLCP, "missing argument to %s", argp[-2]);
                    253:                    continue;
                    254:                case NPACKSW:
                    255:                    continue;
                    256:                case RPOPSW:
                    257:                    rpop++;
                    258:                    continue;
                    259:                case NRPOPSW:
                    260:                    rpop = 0;
                    261:                    continue;
                    262:            }
                    263:        if (*cp == '+' || *cp == '@') {
                    264:            if (folder)
                    265:                adios (NULLCP, "only one folder at a time!");
                    266:            else
                    267:                folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
                    268:        }
                    269:        else
                    270:            adios (NULLCP, "usage: %s [+folder] [switches]", invo_name);
                    271:     }
                    272: 
                    273: /*  */
                    274: 
                    275:     if (from) {
                    276:        newmail = from;
                    277:        if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
                    278:            adios (NULLCP, "no mail to incorporate");
                    279:     }
                    280:     else {
                    281:        if (((newmail = getenv ("MAILDROP")) && *newmail)
                    282:                || ((newmail = m_find ("maildrop")) && *newmail))
                    283:            newmail = m_mailpath (newmail);
                    284:        else {
                    285:            newmail = concat (MAILDIR, "/", MAILFIL, NULLCP);
                    286:        }
                    287:        if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
                    288:            adios (NULLCP, "no mail to incorporate");
                    289:     }
                    290: 
                    291:     if (!m_find ("path"))
                    292:        free (path ("./", TFOLDER));
                    293:     if (!folder)
                    294:        folder = defalt;
                    295:     maildir = m_maildir (folder);
                    296: 
                    297:     if (stat (maildir, &st) == NOTOK) {
                    298:        if (errno != ENOENT)
                    299:            adios (maildir, "error on folder");
                    300:        cp = concat ("Create folder \"", maildir, "\"? ", NULLCP);
                    301:        if (noisy && !getanswer (cp))
                    302:            done (1);
                    303:        free (cp);
                    304:        if (!makedir (maildir))
                    305:            adios (NULLCP, "unable to create folder %s", maildir);
                    306:     }
                    307: 
                    308:     if (chdir (maildir) == NOTOK)
                    309:        adios (maildir, "unable to change directory to");
                    310:     if (!(mp = m_gmsg (folder)))
                    311:        adios (NULLCP, "unable to read folder %s", folder);
                    312: 
                    313:     if (access (newmail, 02) == NOTOK) {
                    314:        trnflag = 0;
                    315:        if ((in = fopen (newmail, "r")) == NULL)
                    316:            adios (newmail, "unable to read");
                    317:     }
                    318:     else {
                    319:        locked++;
                    320:        if (trnflag) {
                    321:            (void) signal (SIGHUP, SIG_IGN);
                    322:            (void) signal (SIGINT, SIG_IGN);
                    323:            (void) signal (SIGQUIT, SIG_IGN);
                    324:            (void) signal (SIGTERM, SIG_IGN);
                    325:        }
                    326:        if ((in = lkfopen (newmail, "r")) == NULL)
                    327:            adios (NULLCP, "unable to lock and fopen %s", newmail);
                    328:        (void) fstat (fileno(in), &s1);
                    329:     }
                    330: 
                    331:     if (audfile) {
                    332:        if ((i = stat (audfile, &st)) == NOTOK)
                    333:            advise (NULLCP, "Creating Receive-Audit: %s", audfile);
                    334:        if ((aud = fopen (audfile, "a")) == NULL)
                    335:            adios (audfile, "unable to append to");
                    336:        else
                    337:            if (i == NOTOK)
                    338:                (void) chmod (audfile, m_gmprot ());
                    339:        fprintf (aud, from ? "<<inc>> %s  -ms %s\n" : "<<inc>> %s\n",
                    340:                dtimenow (), from);
                    341:     }
                    342: 
                    343: #ifdef MHE
                    344:     if (m_find ("mhe")) {
                    345:        cp = concat (maildir, "/++", NULLCP);
                    346:        i = stat (cp, &st);
                    347:        if ((mhe = fopen (cp, "a")) == NULL)
                    348:            admonish (cp, "unable to append to");
                    349:        else
                    350:            if (i == NOTOK)
                    351:                (void) chmod (cp, m_gmprot ());
                    352:        free (cp);
                    353:     }
                    354: #endif MHE
                    355: 
                    356:     nfs = new_fs (form, format, FORMAT);
                    357: 
                    358:     if (noisy) {
                    359:        printf ("Incorporating new mail into %s...\n\n", folder);
                    360:        (void) fflush (stdout);
                    361:     }
                    362: 
                    363: /*  */
                    364: 
                    365:     fmt_current_folder = mp;
                    366:     m_unknown (in);            /* the MAGIC invocation... */
                    367:     hghnum = msgnum = mp -> hghmsg;
                    368:     for (;;) {
                    369:        if (msgnum >= mp -> hghoff) {
                    370:            if ((mp = m_remsg (mp, 0, mp -> hghoff + MAXFOLDER)) == NULL)
                    371:                adios (NULLCP, "unable to allocate folder storage");
                    372:             fmt_current_folder = mp;
                    373:        }
                    374:        mp->msgstats[msgnum+1] = 0;
                    375:        switch (i = scan(in, msgnum + 1, msgnum + 1, nfs, width,
                    376:            msgnum == hghnum && chgflag, 0, 0L, noisy, aud)) {
                    377:            case SCNEOF:
                    378:                break;
                    379: 
                    380:            case SCNERR:
                    381:                if (aud)
                    382:                    fputs ("inc aborted!\n", aud);
                    383:                adios (NULLCP, "aborted!");
                    384: 
                    385:            case SCNNUM:
                    386:                adios (NULLCP, "more than %d messages in folder %s, %s not zero'd",
                    387:                        MAXFOLDER, folder, newmail);
                    388: 
                    389:            default:
                    390:                adios (NULLCP, "scan() botch (%d)", i);
                    391: 
                    392:            case SCNMSG:
                    393:            case SCNENC:
                    394: #ifdef MHE
                    395:                if (mhe)
                    396:                    fputs (scanl, mhe);
                    397: #endif MHE
                    398:                if (noisy)
                    399:                    (void) fflush (stdout);
                    400: 
                    401:                ++msgnum; ++mp->hghmsg;
                    402:                mp -> msgstats[msgnum] |= (EXISTS|UNSEEN);
                    403:                mp -> msgflags |= SEQMOD;
                    404:                continue;
                    405:        }
                    406:        break;
                    407:     }
                    408: 
                    409:     if (aud)
                    410:        (void) fclose (aud);
                    411: #ifdef MHE
                    412:     if (mhe)
                    413:        (void) fclose (mhe);
                    414: #endif MHE
                    415:     if (noisy)
                    416:        (void) fflush (stdout);
                    417: 
                    418:     if (trnflag) {
                    419:        if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
                    420:            advise (NULLCP, "new messages have arrived!\007");
                    421:        else {
                    422:            if ((i = creat (newmail, 0600)) != NOTOK)
                    423:                (void) close (i);
                    424:            else
                    425:                admonish (newmail, "error zero'ing");
                    426:            (void) unlink (map_name (newmail));
                    427:        }
                    428:     }
                    429:     else
                    430:        if (noisy)
                    431:            printf ("%s not zero'd\n", newmail);
                    432: 
                    433:     if (msgnum == hghnum)
                    434:        admonish (NULLCP, "no messages incorporated");
                    435:     else {
                    436:        m_replace (pfolder, folder);
                    437:        if (chgflag)
                    438:            mp -> curmsg = hghnum + 1;
                    439:        mp -> hghmsg = msgnum;
                    440:        if (mp -> lowmsg == 0)
                    441:            mp -> lowmsg = 1;
                    442:        if (chgflag)            /* sigh... */
                    443:            m_setcur (mp, mp -> curmsg);
                    444:     }
                    445: 
                    446:     if (locked)
                    447:        (void) lkfclose (in, newmail);
                    448:     else
                    449:        (void) fclose (in);
                    450: 
                    451:     m_setvis (mp, 0);
                    452:     m_sync (mp);
                    453:     m_update ();
                    454:     done (0);
                    455: }

unix.superglobalmegacorp.com

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