// Hearsay II Simple Host Program
// Version 0.02

// Set 'local' to 1 to test program locally
int local = 0;
string password = "Hearsay";

void print(string s)
{
  if (local)
  {
    tprints(s);
    pause(5);
  }
  else
    sprints(s);
}

int getc()
{
  int c;

  do
  {
    if (local)
      c = kgetc(0);
    else
      c = sgetc(0);
  }
  while(c == -1);
  return c;
}

string gets(int e)
{
  string s;
  int c;

  while((c = getc()) != '\r')
  {
    e ? print(chars(c)) : print("-");
    s = s + chars(c);
  }
  return s;
}

void catfiles(void)
{
  string name;
  int n = 0;

  print("Catalogue files\r\n");
  while (batchgetname(TX, ++n, 0, name))
  {
  if (!((n-1) % 5))
      print("\r\n");
  print(name + mids("              ", 0, 15 - slen(name)));
  }
  if (n == 1)
    print("No files available to download!");
}

int validname(string f)
{
  string name;
  int n = 0;

  while (batchgetname(TX, ++n, 0, name))
    if (+f == +name)
    {
       batchsetstate(TX, n, 3);
       return 0;
    }
  print("\r\nFilename " + f + " not valid");
  return 1;
}

void setsentstate(void)
{
  string name;
  int n = 0;

  while (batchgetname(TX, ++n, 0, name))
    batchsetstate(TX, n, 1);
}

void download(void)
{
  string filename;

  print("Xmodem download\r\n");
  setsentstate();
  do
  {
    print("\r\nEnter name of file to to download: ");
    if((filename = gets(1)) == "")
      return;
  }
  while(validname(filename));
  {
    print("\r\n\r\nStart your Xmodem downloader now.\r\n");
    sendfiles();
    print("\r\nDownload complete.");
  }
}

void upload()
{
  print("Upload files\r\n");
  print("\r\nEnsure files to upload are in TX batch.");
  print("\r\nStart your Xmodem uploader now.\r\n");
  receivefiles();
  print("\r\nUpload complete.");
}

void setup(void)
{
  if (local)
  {
    setterminal(ANSI);
    print("\f");
    claimkeyboard(1);
    }
  else
  {
    setspeed(300, 300);
    setbits(8, N, 1);
    setline(NC, NC, ANSWER);
  }
  setftp(XMODEM);
}

void answer(void)
{
  if (!local)
  {
    modem_autoanswer(1);
    pause(200);
    modem_countpurge(RX, 0);
    modem_countpurge(TX, 0);
  }
}

void menu(void)
{
  print("\r\n\r\nHearsay Host Main Menu\r\n");
  print("----------------------\r\n");
  print("C]atalogue files\r\n");
  print("D]ownload files\r\n");
  print("U]pload files\r\n");
  print("R]ead messages\r\n");
  print("S]end messages\r\n");
  print("F]inish session\r\n\r\n");
  print("Enter option: ");
}

int welcome()    
{
string s;
int n = 0;

  print("Welcome to the Hearsay Host System\r\n");
  while(n++ < 5)
  {
  print("Enter password: ");
  if(+(s = gets(0)) == +password)
    {
    print("\r\nPassword accepted");
    return(1);
    }
  print("\r\nPassword incorrect.\r\n");
  }
}

int fix(string &s, int h)
{
filereads(s, h);
return fileeof(h);
}

void read()
{
int h;
string s;

  print("Read messages\r\n\r\n");
  getenvs(s = "Hearsay$Path");
  if((h = fileopen(s + ".HostMsgs", "r")) == 0)
  {
    print("No Messages file!");
    return;
  }
  while(!fix(s, h))
  {
    print(s + "\r");
    if(slen(s) == 1)
    {
      print("Press Space for next message.\r\n\r\n");
      while(getc() != 32);
    }
  }
  print("No more messages.");
  fileclose(h);
}

void send()
{
string s = "", t;
int h, c;

  print("Send message\r\n\r\n");
  print("Type in message. Press Return twice to send.\r\n\r\n");

  while(slen(t = gets(1)) > 0)
  {
    s = s + t + "\n";
    print("\r\n");
  }
  print("\r\nSend message (Y/N)?: ");
  if(+chars(getc()) == "Y")
  {
    print("Yes");
    getenvs(t = "Hearsay$Path");
    h = fileopen(t + ".HostMsgs", "a");
    filewrites(s + "\n", h);
    fileclose(h);
  }
  else
    print("No");
}

void options(void)
{
  string k;

  do
  {
    menu();
    while(1)
    {
      k = +chars(getc());
      if (k == "C")  { catfiles(); break; }
      if (k == "D")  { download(); break; }
      if (k == "U")  { upload(); break; }
      if (k == "R")  { read(); break; }
      if (k == "S")  { send(); break; }
      if (k == "F")  { print("Finish\r\n"); break; }
      if (k == "\r") { break; }
    }
  }
  while (k != "F");
}

void goodbye()
{
  print("\r\nGoodbye from the Hearsay Host. Thanks for calling.\r\n");
  print("Please disconnect now.\r\n\r\n");
  if (!local)
    disconnect();
}

void main(void)
{
  setup();
  while(1)
  {
    answer();
  if(welcome())
    options();
  goodbye();
  }
}
