/*
 *  REQFIL.C - Server for the W0RLI MailBox V10.xx
 *
 *  Copyright (C) 1989
 *  Peter Meiring
 *  13 Venn Crescent,
 *  Hartley, Plymouth.
 *  PL3 5PJ
 *
 *  This code may be freely used and copied for non-commercial uses.
 *
 *  s reqfil reqfil.exe reqfil.out H8 reqfil.in H8
 *
 *  See SERVER.DOC for further details.
 *
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>

/*
 *  Copyright storage:
 */

char *copyright = "REQFIL 1.02  Copyright (c) Peter Meiring, G0BSX, 1989.\n";

/*
 *  Some buffer space.
 */

#define ln_buf 512

char buf[ln_buf];

/*
 *  End-of-message mark.
 */

char *mark    = "/EX\n";
char base[64];  /* storage for the root path of the files section */

/*
 *  File used by this program to read initialisation Data
 */

char *datfile = "\\reqxxx.dat";
/*
 *  File the MailBox will create, and this server will read.
 */

char *infile  = "reqfil.out";

/*
 *  File this server will create, and the MailBox will read.
 */

char *outfile = "reqfil.in";

/* List directory to file */

copyfile(filename,fp)
FILE *fp;
char *filename;
{
    char tmp[64];
    char *st;
    FILE *datafile;

    strcpy( tmp, base ) ;
    strcat(tmp,filename);
    if ((datafile = fopen(tmp, "r")) == NULL) {
       fprintf(fp,"File: %s not found \n", tmp);
       return 1;
    }
    st = fgets(buf, ln_buf, datafile) ;
    while (st != NULL)
    {
       fputs(buf, fp);
       st = fgets(buf, ln_buf, datafile);
    }
    fclose(datafile);
    return 0;
}

stripcr( line )
char *line;
{
  int i;
  for(i=1;i<strlen(line);i++)
    if(line[i]=='\n')
        line[i]='\0' ;
}

main(argc, argv)
int argc;
char *argv[];
{
  FILE *in, *out;
  char *st, *tail, *fname, *ptr ;

/*
 *   Open and read the initialisation file: reqxxx.dat in the root directory
 */

  puts(copyright);

  if((in = fopen(datfile, "r")) == NULL) exit(1);

  st = fgets(buf, ln_buf, in) ;

  while (st != NULL) {

/*
 *  skip all comment and blank lines
 */

    while( ((*buf == ';') || (*buf == '\n')) && (st != NULL))
      st = fgets(buf, ln_buf, in) ;

/*
 *  Extract parameters
 */
    if (!strncmp(buf, "textsdir", 8)) {
        ptr = strchr(buf, ' ');
	while( *ptr == ' ' ) *ptr++ = '\0';
        strcpy( base, ptr);
        stripcr( base );
    }
    st = fgets(buf, ln_buf, in) ;
  }

  fclose(in);

/*
 *  Open the input file.
 *  If not possible, exit with return code 1 to signal
 *  the MailBox that the server failed.
 */

  if ((in = fopen(infile, "r")) == NULL) exit(1);

/*
 *  Open the output file.
 *  If not possible, exit with return code 1 to signal
 *  the MailBox that the server failed.
 */

  if ((out = fopen(outfile, "w")) == NULL) exit(1);

  st = fgets(buf, ln_buf, in);
  while (st != NULL)
  {

/*
 *  Read the rfc-822 header placed into the message by the MailBox.
 *  The end of header is marked by a blank line.
 */

    while(*buf != '\n')
    {

/*
 *  Find end of keyword.
 *  Terminate string at end of keyword.
 */

      tail = strchr(buf, ' ');
      *tail++ = '\0';

/*
 *  Make the new rfc-822 header.
 */

      if      (!strcmp(buf, "To:"))      fprintf(out, "From: %s", tail);
      else if (!strcmp(buf, "From:"))    fprintf(out, "To: %s", tail);
      else if (!strcmp(buf, "Subject:")) {
		fname = tail ;
		if( (tail = strchr( fname, ' ')) != NULL)
		   *tail = '\0';
		fprintf(out, "Subject: File: %s", fname);
      }
      st = fgets(buf, ln_buf, in);
    }

/*
 *  Now that the rfc-822 header has been read, and a new one created,
 *  copy the text of the message from the input to the output.
 */
    fputs(buf,out) ;
    fprintf(out, "File: %s", fname);
    stripcr(fname) ;
    copyfile(fname,out) ;
    while ((st != NULL) && stricmp(buf, mark))
    {
      st = fgets(buf, ln_buf, in);
    }

/*
 *  Mark the end of this message.
 *  Go on to the next message.
 */

    fputs(mark, out);
    if (st != NULL) st = fgets(buf, ln_buf, in);
  }

/*
 *  All the incoming messages have been processed,
 *  and an outgoing message created for each one.
 */

  fclose(in);
  fclose(out);

/*
 *  Delete the input file.
 *  The MailBox appends messages to this file, this server must delete it.
 */

  unlink(infile);

/*
 *  Exit with return code 0 to signal the MailBox that
 *  the server completed it's processing normally.
 *  The MailBox will then read our output file,
 *  and create messages from it.
 */

  exit(0);
}
