#include <stdio.h>

void main() {
  FILE *listfile;
  FILE *mailfile;
  FILE *output;
  char fileinput[256];
  char filename[256]="../maillist";
  char address[256];
  char from[256];
  int x;
  int y;
  int v;
  int done;

  output=fopen("../mailsrc","w");
  listfile=fopen(filename,"r");
  fgets(filename,79,listfile);
  while(filename[0]>31){
    y=0;x=filename[0];
    while(x!=0){
      if(x<32) filename[y]=0;
      y=y+1;x=filename[y];
    }
    /*printf("%s\n",filename);*/
    mailfile=fopen(filename,"r");
    address[0]=0;
    from[0]=0;
    done=0;
    while(!done){
      fgets(fileinput,255,mailfile);
      if(fileinput[0]=='R'&&fileinput[1]=='e'&&fileinput[2]=='c'){
	x=3;v=fileinput[3];
	while(v>31&&!done){
	  if(fileinput[x]=='@'&&(fileinput[x+1]|32)=='a'&&(fileinput[x+2]|32)=='l'&&(fileinput[x+3]|32)=='p'&&(fileinput[x+4]|32)=='h'&&(fileinput[x+5]|32)=='a') {
            while (fileinput[x]>32&&fileinput[x]!='<') x--;
            y=0;x++;
            while(fileinput[x]>32&&fileinput[x]!='@'){
	      address[y]=fileinput[x];
	      x=x+1;y=y+1;
	    }
            address[y]=0;
          }
          x=x+1;v=fileinput[x];
	}
      }
      if(fileinput[0]=='F'&&fileinput[1]=='r'&&fileinput[2]=='o'&&fileinput[4]==':'){
        x=0;y=0;
	while(fileinput[x]!=':'){x=x+1;}
	x=x+1;
	while(fileinput[x]<=32){x=x+1;}
	while(fileinput[x]>32){
	  from[y]=fileinput[x];
          x=x+1;y=y+1;
        }
        from[y]=0;
	x=0; /* look for <address> */
	while(fileinput[x]>31&&fileinput[x]!='<'){x=x+1;}
	if(fileinput[x]=='<'){
    	  y=0;x=x+1;
          while(fileinput[x]>32&&fileinput[x]!='>'){
	    from[y]=fileinput[x];
            x=x+1;y=y+1;
          }
	  from[y]=0;
	}
      }
      if(fileinput[0]<32&&fileinput[0]!=9) {done=1;}
    }
    if(from[0]) {
/*      printf(">>> %s\n",filename);
      printf("-> %s\n",address);
      printf("From: %s\n",from);
*/
/*      if(address[0]=='a'&&address[1]=='l'&&address[2]=='i'&&address[3]=='a'&&address[5]=='!') {
        x=0;
	while(address[x+5]!=0) {
          address[x]=address[x+6];
	  ++x;
	}*/
        /* de-capitalize and remove funky characters */
      x=0;
      while(address[x]!=0){
          if (address[x]>='A'&&address[x]<='Z') {
            address[x]+=32;
          }
          if ((address[x]<'a'||address[x]>'z')&&
              (address[x]<'0'||address[x]>'9')&&address[x]!='-') {
            address[x]='_';
          }
          x++;
        }

/*        printf(" = %s",address);*/
	fprintf(output,"echo `date` >>/home/infinity/ghio/alpha/maillog\n");
/*        fprintf(output,"echo Remail Message From: ");
	fixbang(output,from);
        fprintf(output," >>/home/infinity/ghio/alpha/maillog\n");*/
        fprintf(output,"echo For: %s >>/home/infinity/ghio/alpha/maillog\n",address);
/*        fprintf(output,"echo Received: by mg5n+remailer@andrew.cmu.edu `date` >/home/infinity/alpha/mailtemp\n");*/
        fprintf(output,"cat %s >/home/infinity/ghio/alpha/mailtemp\n",filename);
        fprintf(output,"setenv TO %s\n",address);
        fprintf(output,"source /home/infinity/ghio/alpha/forward\n");
	fprintf(output,"if (-r /home/infinity/ghio/alpha/nyms/$TO.forward || -r /home/infinity/ghio/alpha/nyms/$TO.replyblock) then\n");
	fprintf(output,"rm %s\n",filename);
        fprintf(output,"rm /home/infinity/ghio/alpha/mailtemp\n");
        fprintf(output,"else\n");
        fprintf(output,"mv %s ../dead\n",filename);
	fprintf(output,"endif\n");
        fprintf(output,"echo %c%c >>/home/infinity/ghio/alpha/maillog\n\n",34,34);
      /*printf("\n");*/

/*      if(address[0]=='a'&&address[1]=='l'&&address[2]=='i'&&address[3]=='a'&&address[5]!='!') {
        printf(" = Alias Configuration/Setup",address);
	fprintf(output,"echo `date` >>/home/infinity/alpha/maillog\n");
        fprintf(output,"echo Alias Config/Setup request From: ");
	fixbang(output,from);
        fprintf(output," >>/home/infinity/alpha/maillog\n");
        fprintf(output,"mv %s /home/infinity/alpha/in-raw-msg\n",filename);
        fprintf(output,"source /home/infinity/alpha/anonserv\n");
        fprintf(output,"cd /home/infinity/Mailbox\n");
        fprintf(output,"echo %c%c >>/home/infinity/alpha/maillog\n\n",34,34);
      printf("\n");
      }

      if(address[0]=='r'&&address[1]=='a'&&address[2]=='n'&&address[3]=='d') {
	printf(" = Random Numbers Request..\n");
	fprintf(output,"cd\n");
	fprintf(output,"mv Mailbox/%s randfood\n",filename);
	fprintf(output,"./randomize\n");
	fprintf(output,"finger @unix >randfood\n");
	fprintf(output,"./randomize\n");
	fprintf(output,"rm randfood\n");
	fprintf(output,"echo \"From: mg5n+random@andrew.cmu.edu (Random Number Generator)\" >prog/mailtemp\n");
	fprintf(output,"echo Date: `date` >>prog/mailtemp\n");
	fprintf(output,"echo \"Errors-To: Matthew Ghio <ghio@andrew.cmu.edu>\" >>prog/mailtemp\n");
	fprintf(output,"echo \"\" >>prog/mailtemp\n");
	fprintf(output,"./viewrandom >>prog/mailtemp\n");
	fprintf(output,"cd Mailbox\n");
        fprintf(output,"/usr/andrew/etc/queuemail -f /home/infinity/alpha/mailtemp -a ");
	fixbang(output,from);
        fprintf(output,"\n");
	fprintf(output,"echo `date` >>/home/infinity/alpha/maillog\n");
        fprintf(output,"echo Random Numbers Request: ");
	fixbang(output,from);
        fprintf(output," >>/home/infinity/alpha/maillog\n");
        fprintf(output,"echo %c%c >>/home/infinity/alpha/maillog\n",34,34);
	<fprintf(output,"rm %s\n\n",filename);>
      }
      printf("\n");
*/
    }
    fclose(mailfile);
    filename[0]=0;
    fgets(filename,79,listfile);
  }
  fclose(listfile);
  fclose(output);  
}

fixbang(FILE *output,char string[]) {
  int x=0;
  while(string[x]>0) {
    if (string[x]=='!') fprintf(output,"\\");
    fprintf(output,"%c",string[x]);
    ++x;
  }
}
