/***************************************************************************

   MAIL.c by William Loftus
   Copyright 1988 by William Loftus.   All rights reserved.

   Changes:
           11-Jul-88 by Dan Schein

                   Added .signature routine
                   Changed syntax of mail header
                   Changed beta version from 0.30 to 0.31

***************************************************************************/

# include <time.h>
# include <stdio.h>

char user[128];
char node[128];
char real_name[128];
char to_buf[128];
char subject_buf[128];
char *temp_file_name = "UUCP:spool/tmp.mail";

int seq;

char cmd[128];
char path[128];
char signature[128];

void read_ctl();
void GetTo();
void GetSubject();
void GetMessage();
void BuildMail();
void Signature();

#define TRUE 1
#define FALSE 0

int
is_in_L_sys_file(system_name)

  char *system_name;

{
  char buf[256];
  char system[128];
  FILE *fd;

  if (!(fd = fopen("UUCP:lib/L.sys","r"))) {
    printf("Couldn't open UUCP:lib/L.sys file\n");
    chdir(path);
    exit(1);
  }

  sprintf(system,"%s ", system_name);

  while (NULL != fgets(buf, sizeof buf, fd)) {
    if (buf[0] == '#') continue;
    if (strncmp(buf, system, strlen(system)) == 0) {
      fclose(fd);
      return TRUE;
    }
  }

  fclose(fd);
  return FALSE;

}


void
main(argc, argv)
  int argc;
  char **argv;
{
  getcwd(path,128);
  chdir("uucp:spool");
  read_ctl();
  if (argc > 1) {
    strcpy(to_buf, argv[1]);
  } else {
    GetTo();
  }
  GetSubject();
  GetMessage();
  BuildMail();
  chdir(path);
}

void
GetTo()
{
   printf("To: ");
   fgets(to_buf, sizeof to_buf, stdin);
   to_buf[strlen(to_buf)-1] = '\0';
}

void
GetSubject()
{
   printf("Subject: ");
   fgets(subject_buf, sizeof subject_buf, stdin);
}

void
GetMessage()
{
  FILE *fp;
  char buf[128];
  long t;

  fp = fopen("uucp:lib/seq","r");
  if (fp) {
    fgets(buf, sizeof buf, fp);
    seq = atoi(&buf[0]);
    fclose(fp);
  } else {
    printf("Can't open UUCP:lib/seq for reading.\n");
    perror("UUCP:lib/seq");
    chdir(path);
    exit(1);
  }


  fp = fopen("uucp:lib/seq", "w");

  if (fp) {
    fprintf(fp,"%d", seq+4);
    fclose(fp);
  } else {
    printf("Can't open UUCP:lib/seq for writing.\n");
    perror("UUCP:lib/seq");
    chdir(path);
    exit(1);
  }

  fp = fopen(temp_file_name, "w");

  if (fp) {

    time(&t);
    fprintf(fp,"From %s %24.24s remote from %s\n", user, ctime(&t), node);
    fprintf(fp,"Received: by %s (AmigaUUCP) BETA\n",node);
    fprintf(fp,"\tid AA%04d; %s", seq, ctime(&t));
    fprintf(fp,"Date: %s", ctime(&t));
    fprintf(fp,"From: %s@%s.UUCP (%s)\n", user, node, real_name);
    fprintf(fp,"Message-Id: <%04d.AA%04d@%s>\n",seq,seq,node);
    fprintf(fp,"To: %s\n", to_buf);
    fprintf(fp,"Subject: %s", subject_buf);
    fprintf(fp,"X-AmigaUUCP: version 0.32 BETA\n\n");

    fclose(fp);

  } else {
    printf("Can't open temp file for %s\n",temp_file_name);
    perror(temp_file_name);
    chdir(path);
    exit(1);
  }

  chdir(path);
  sprintf(cmd,"uucp:c/vi %s",temp_file_name);
  system(cmd);

  printf("Append .signature file? [n/y] ");
  fgets(signature, sizeof signature, stdin);
  if ((signature[0] | 0x20) == 'y') {
     Signature();
     printf(".signature file appended.\n");
  }

  chdir("UUCP:spool");

}

void
BuildMail()
{
  char system_name[32];
  int bang;

  bang = (int)strchr(to_buf,'!');
  bang = bang - (int)to_buf;

  strncpy(system_name,to_buf,bang);

  system_name[bang] = '\0';

  if (!is_in_L_sys_file(system_name)) {
    printf ("System \"%s\" not in L.sys file.\n", system_name);
    chdir(path);
    exit(1);
  }


  sprintf(cmd, "uucp:c/uux %s \"%s!rmail %s\"", temp_file_name,
               system_name, (char *)to_buf+bang+1);

  system(cmd);

  remove(temp_file_name);

  printf("Mail sent.\n");

}

/*
 * Read the control file and grab a few parameters.
 */
void
read_ctl()
{
        FILE  *fd;
        char  buf[128];
#define CTL_DELIM " \t\n\r"

        if (! (fd = fopen("uucp:lib/config", "r"))) {
                printf("Can't Find config file");
                perror("config");
                chdir(path);
                exit(3);
        }

        while (NULL != fgets(buf, sizeof buf, fd)) {
                     if (strncmp(buf, "UserName", 8) == 0)
                        strcpy(user, strtok(&buf[9], CTL_DELIM));
                else if (strncmp(buf, "NodeName", 8) == 0)
                        strcpy(node, strtok(&buf[9], CTL_DELIM)) ;
                else if (strncmp(buf, "RealName", 8) == 0)
                        strcpy(real_name,stpblk(strtok(&buf[10],"\t\r\n")));
        }

        fclose(fd);
}

void
Signature()
{
   FILE *fp;
   FILE *sf;
   char buff[128];

   fp = fopen(temp_file_name, "a");
   if (!fp) {
     printf("Can't open %s file\n", temp_file_name);
     perror(temp_file_name);
     chdir(path);
     exit(3);
   }

   sf = fopen("uucp:lib/.signature", "r");
   if (sf) {
      while (NULL != fgets(buff, sizeof buff, sf)) {
         fprintf(fp, "%s", buff);
      }
      fclose(sf);
   } else {
      printf("Can't open UUCP:lib/.signature file\n");
      perror("UUCP:lib/.signature");
      chdir(path);
      exit(3);
   }

   fclose(fp);
}
