
/*
 *  UUEcho.C
 *
 *  12/09/90	Andrew Kopp
 *
 *  Handles stdin messages with MAGIC COMMANDS
 *
 *  16/09/90	ak
 *		ADD minidoc
 *		FIX sendmail -r bug
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "version.h"

IDENT(".03");

#define PROGNAME "uuecho"
#define PROGVERS "0.3"

char buf[1024];
char path[128];
char user[128];
char host[128];
char domain[128];

char field_from[128];
char field_msgid[128];
char field_subject[128];
char field_to[128];
char field_date[128];

int  f_subject = 0;

char command[128];

int debug;

#define TRUE 1
#define FALSE 0
#define DELIM " \t\n\r"

int	brk();

int
brk()
{
    return(0);
}

int
main(argc, argv)
int argc;
char **argv;
{
    int error;
    char *up = PROGNAME;
    char *hp = FindConfig(NODENAME);
    char *dp = FindConfig(DOMAINNAME);
    FILE *fd;

    onbreak(brk);

    strcpy(user, up);
    strcpy(host, hp);
    strcpy(domain, dp);

    getcwd(path,128);
    chdir(GetConfigDir(UUSPOOL));

    while( (fgets(buf, sizeof buf, stdin)) && !f_subject ) {

	/*  MESSAGE HANDLER  */

	if ( strncmp(buf,"From:",5) == 0) {
	    strcpy(field_from, strtok(&buf[5],DELIM));
	    continue;
	}
	else if ( strncmp(buf,"Message-Id:",11) == 0) {
	    strcpy(field_msgid, strtok(&buf[11],DELIM));
	    continue;
	}
	else if ( strncmp(buf,"Subject:",8) == 0) {
	    strcpy(field_subject, strtok(&buf[8],DELIM));
	    f_subject = 1;
	    continue;
	}
	else if ( strncmp(buf,"To:",3) == 0) {
	    strcpy(field_to, strtok(&buf[3],DELIM));
	    continue;
	}
	else if ( strncmp(buf,"Date:",5) == 0) {
	    strcpy(field_date, strtok(&buf[5],DELIM));
	    continue;
	}
	else {

	}
    };

    /* handle the message text	    */

    /* generate status from text    */

    fd = fopen("T:uuecho","w");

    if (fd) {

      fprintf(fd,"From:    %s\n", field_to);
      fprintf(fd,"To:      %s\n", field_from);
      fprintf(fd,"Subject: RE: %s\n", field_subject);
      fprintf(fd,"\n");

      fprintf(fd,"jobid  %s\n", field_msgid);
      fprintf(fd,"record \n\n");

      fprintf(fd,"<------------------ S T A R T ------------------>\n");

      while(fgets(buf, sizeof buf, stdin)) {
	    fputs(buf, fd);
      }

      fprintf(fd,"<-------------------- E N D -------------------->\n");
      fclose(fd); fd = NULL;
    }

    /* send reponse from this job   */

    sprintf(command,"sendmail < T:uuecho -R %s-%s\0", host, field_to);
    error = system(command);

    chdir(path);
    return(0);
}

