home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / lan / pmail301.zip / UDG.ZIP / FILTER.C next >
Text File  |  1992-04-14  |  9KB  |  303 lines

  1. /*
  2.  * filter.c
  3.  * a program to take the output produced by Pegasus Mail/PC in standalone
  4.  * mode, and place it appropriately. with associated
  5.  * support .cmd and .xqt files for mail processing using the Waffle BBS uucico
  6.  * and uuxqt programs.
  7.  *
  8.  * Pegasus Mail/PC (C) Copyright 1990, 1991, David Harris, Dunedin, New Zealand
  9.  * WAFFLE  (C) Copyright 1991 by Darkside International of Mountain View CA.
  10.  *
  11.  * Author: Brendan Murray, Dunedin, New Zealand
  12.  * Permission is granted to do whatever you like with this code. Just about
  13.  * anyone ought to be able to improve on it. No warranty whatsoever is granted
  14.  * or implied.
  15.  *
  16.  * V1.1
  17.  */
  18.  
  19. /*
  20.  *    Command line parameters.
  21.  *    0:    the command
  22.  *    1:    username
  23.  *    2:    container file name -- full path
  24.  *    3:    To: field for mail
  25.  *    4:    The time in RFC 822 format
  26.  *
  27.  *    Actions
  28.  *    1. Take the RFC 822 message produced by pmail and prepend a
  29.  *        uucp acceptible From line
  30.  *    2. Create a .cmd file to tell UUCICO what to do
  31.  *    3. Create a .xqt file to tell UUXQT what to do at the other end
  32.  */
  33.  
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <dir.h>
  38. #include <ctype.h>
  39.  
  40. void main (int argc, char * argv[] )
  41.    {
  42.    FILE *outfile;
  43.    FILE *infile;
  44.  
  45.    char *dat_FileName;
  46.    char *cmd_FileName;
  47.    char *xqt_FileName;
  48.  
  49.    char *HostName;    /* this host */
  50.    char *SmartHost;     /* who sends things on for us */
  51.    char *Spool;            /* where to put things */
  52.    char *TimeZone;      /* for the header */
  53.  
  54.    /* for fnsplit and fnmerge */
  55.    char *drive;
  56.    char *dir;
  57.    char *name;
  58.    char *ext;
  59.  
  60.    char *wafdir;
  61.    char *tmpstring;
  62.  
  63.    int cntr;
  64.    int cntr2;
  65.    char ch;
  66.  
  67.    if (argc != 5)
  68.       {
  69.       /* even though Pmail probably won't display this */
  70.       fprintf(stderr,"Usage: filter username container-file to-line date\n");
  71. /*      exit(1); */
  72.       }
  73.  
  74.  
  75. /*
  76.  *    Waffle uses an environment variable (WAFFLE) to point at the
  77.  *    static parameters file
  78.  */
  79.    wafdir = (char *) malloc(MAXPATH);
  80.    if (( wafdir = (char *) getenv ("WAFFLE")) == NULL)
  81.       {
  82.       fprintf (stderr, "ERROR: WAFFLE has not been defined\n");
  83.       exit(1);
  84.       }
  85.  
  86.    if (( infile = fopen(wafdir,"r")) == NULL)
  87.       {
  88.       fprintf(stderr,"Unable to open %s",wafdir);
  89.       exit(1);
  90.       }
  91.  
  92.    tmpstring = (char *) malloc (132);
  93.    while ((fgets(tmpstring, 132, infile)) != NULL)
  94.       {
  95.       if ((strnicmp(tmpstring, "uucpname", 8)) == 0)
  96.      {
  97.      cntr = 8; /* might as well start here */
  98.      /* find an alpha character, the start of the uucpname */
  99.      while ((!isalpha(tmpstring[cntr])) && (tmpstring[cntr] != '\0'))
  100.         cntr++;
  101.      if (tmpstring[cntr] == '\0')
  102.         {
  103.         fprintf(stderr,"No UUCPNAME specified in %s\n",wafdir);
  104.         exit(1);
  105.         }
  106.      HostName = (char *) malloc (132); /* it can't be bigger than this? */
  107.      cntr2 = 0;
  108.      while ((isalpha(tmpstring[cntr])) && (tmpstring[cntr] != '\0'))
  109.         HostName[cntr2++] = tmpstring[cntr++];
  110.      HostName[cntr2] = '\0';
  111.      }
  112.       else
  113.      if (( strnicmp(tmpstring, "smarthost", 9)) == 0)
  114.         {
  115.         cntr = 9; /* might as well start here */
  116.         /* find an alpha character, the start of the Smart Host name */
  117.         while ((!isalpha(tmpstring[cntr])) && (tmpstring[cntr] != '\0'))
  118.            cntr++;
  119.         if (tmpstring[cntr] == '\0')
  120.            {
  121.            fprintf(stderr,"No SMARTHOST specified in %s\n",wafdir);
  122.            exit(1);
  123.            }
  124.         SmartHost = (char *) malloc (132); /* it can't be bigger than this? */
  125.         cntr2 = 0;
  126.         while ((isalpha(tmpstring[cntr])) && (tmpstring[cntr] != '\0'))
  127.            SmartHost[cntr2++] = tmpstring[cntr++];
  128.         SmartHost[cntr2] = '\0';
  129.         }
  130.      else
  131.         if (( strnicmp(tmpstring, "spool", 5)) == 0)
  132.            {
  133.            cntr = 5; /* might as well start here */
  134.            /* find an alpha character, the start of the spool directory name */
  135.            while ((!isalpha(tmpstring[cntr])) && (tmpstring[cntr] != '\0'))
  136.           cntr++;
  137.            if (tmpstring[cntr] == '\0')
  138.           {
  139.           fprintf(stderr,"No SPOOLDIR specified in %s\n",wafdir);
  140.           exit(1);
  141.           }
  142.            Spool = (char *) malloc (132); /* it can't be bigger than this? */
  143.            cntr2 = 0;
  144.            while ((tmpstring[cntr] != '\0') && (tmpstring[cntr] != '\n'))
  145.           if (tmpstring[cntr] == '/')
  146.              {
  147.              Spool[cntr2++] = '\\';
  148.              cntr++;}
  149.           else
  150.              Spool[cntr2++] = tmpstring[cntr++];
  151.            Spool[cntr2] = '\0';
  152.            }
  153.         else
  154.            if ((strnicmp(tmpstring,"timezone", 8)) == 0)
  155.           {
  156.           cntr = 8;
  157.           while ((tmpstring[cntr] == ':') || (tmpstring[cntr] == ' '))
  158.              cntr++;
  159.           TimeZone = (char *) malloc (132); /* a number! */
  160.           cntr2 = 0;
  161.           while ((tmpstring[cntr] != '\0') && (tmpstring[cntr] != '\n'))
  162.              TimeZone[cntr2++] = tmpstring[cntr++];
  163.           TimeZone[cntr2] = '\0';
  164.           }
  165.       }
  166.       fclose(infile);
  167.  
  168.  
  169. /*
  170.  * allocate space for file name bits
  171.  */
  172.    drive = (char *) malloc(MAXDRIVE);
  173.    dir = (char *) malloc(MAXDIR);
  174.    name = (char*) malloc(MAXFILE);
  175.    ext = (char *) malloc(MAXEXT);
  176.  
  177.  
  178.    dat_FileName = (char *) malloc (MAXPATH);
  179.    cmd_FileName = (char *) malloc (MAXPATH);
  180.    xqt_FileName = (char *) malloc (MAXPATH);
  181.  
  182.    Spool = strcat(Spool,"\\");
  183.    Spool = strcat(Spool,SmartHost);
  184.    Spool = strcat(Spool,"\\");
  185.  
  186.    /* drive and directory from Spool */
  187.    fnsplit(Spool, drive, dir, NULL, NULL);
  188.    /* file name from input arguments */
  189.    fnsplit(argv[2], NULL, NULL, name, NULL);
  190.    /* put 'em together and what do you get? */
  191.    fnmerge(dat_FileName, drive, dir, name, ".DAT");
  192.  
  193. /*
  194.  *    create the data file for mailing
  195.  */
  196.    if ((outfile = fopen(dat_FileName,"w")) == NULL)
  197.       {
  198.       fprintf(stderr,"Open of DAT file %s failed\n", dat_FileName);
  199.       exit(1);
  200.       }
  201.    if ((infile = fopen(argv[2],"r"))== NULL)
  202.       {
  203.       fprintf(stderr,"Error opening container file %s\n", argv[2]);
  204.       exit(1);
  205.       }
  206.  
  207.    fprintf(outfile,"From %s, %s %s remote from %s\n",
  208.        argv[1], argv[4], TimeZone, HostName);
  209.  
  210.    while (( ch = fgetc(infile)) != EOF )
  211.        fputc(ch, outfile);
  212.  
  213.    fclose(infile);
  214.    fclose(outfile);
  215.  
  216. /*
  217.  * create the ".CMD" file - commands to UUCICO (?)
  218.  *    Format:
  219.  *    S 0051.DAT D.home0051 brendan - 0051.DAT 0666
  220.  *    S 0051.XQT X.home0051 brendan - 0051.XQT 0666
  221.  *
  222.  *  (roughly)
  223.  *    SEND local-filename as-filename from - ????? unix-file-mode
  224.  */
  225.    fnsplit (dat_FileName, drive, dir, name, ext);
  226.    fnmerge(cmd_FileName, drive, dir, name, ".CMD");
  227.    if ((outfile = fopen (cmd_FileName,"w")) == NULL)
  228.       {
  229.       fprintf(stderr,"Open of CMD file %s failed\n", cmd_FileName);
  230.       exit(1);
  231.       }
  232.  
  233.    fnmerge(dat_FileName, NULL, NULL, name, ".DAT");
  234.    fnmerge(xqt_FileName, NULL, NULL, name, ".XQT");
  235.  
  236.    fprintf(outfile, "S %s D.%s%s %s - %s 0666\n", dat_FileName, HostName,
  237.        name, argv[1], dat_FileName);
  238.  
  239.    fprintf(outfile, "S %s X.%s%s %s - %s 0666\n", xqt_FileName, HostName,
  240.        name, argv[1], xqt_FileName);
  241.    fclose(outfile);
  242.  
  243.  /*
  244.   *    Create the ".XQT" file --  commands to uuxqt at the other end!
  245.   *
  246.   *
  247.   *    Format:
  248.   *    U brendan home
  249.   *    Z
  250.   *    F  D.home0051
  251.   *    I D.home0051
  252.   *    C rmail brendan
  253.   *
  254.   *    where the commands defined in the uuxqt file are (as stated by
  255.   *     Ian Taylor (Ian@airs.com, uunet!airs!ian) in a newsitem posted
  256.   *    to comp.unix.internals 4 Apr 1992)
  257.   *
  258.   *    "Here are the commands defined in uuxqt files:
  259.   *
  260.   *     C command-line
  261.   *     I standard-input
  262.   *     O standard-output [ system ]
  263.   *     F required-file filename-to-use
  264.   *     R requestor-address
  265.   *     U user system
  266.   *     Z (acknowledge if command failed; default)
  267.   *     N (no acknowledgement on failure)
  268.   *     n (acknowledge if command succeeded)
  269.   *     B (return command input on error)
  270.   *     e (process with sh)
  271.   *     E (process with exec)
  272.   *     M status-file
  273.   *     # comment                    "
  274.   *
  275.   */
  276.    fnmerge(xqt_FileName,drive, dir, name, ".XQT");
  277.    if ((outfile = fopen (xqt_FileName,"w")) == NULL)
  278.       {
  279.       fprintf(stderr,"Open of XQT file %s.XQT failed\n", xqt_FileName);
  280.       exit(1);
  281.       }
  282.    fprintf(outfile,"U %s %s\n", argv[1], HostName);
  283.    fprintf(outfile,"Z\n");
  284.    fprintf(outfile,"F D.%s%s\n", HostName, name);
  285.    fprintf(outfile,"I D.%s%s\n", HostName, name);
  286.    fprintf(outfile,"C rmail %s\n", argv[3]);
  287.    fclose(outfile);
  288.  
  289. /*
  290.  *    Now delete the file created by pmail, 'cos all this worked,
  291.  *    didn't it! You might want to stop this from happening, as a
  292.  *    log, since uucico deletes the files when finished, or just
  293.  *    in case you're paranoid and might want to re-process it all
  294.  *    later.
  295.  */
  296.  
  297. /*   if ( (unlink(argv[2])) != 0)
  298.       {
  299.       fprintf(stderr,"Failed to delete PMAIL temporary file %s\n", argv[3]);
  300.       exit(1);
  301.       }
  302. */
  303.    }