// Filename:	bbsint.C
// Contents:	the bbs interface object methods
// Author:		Greg Shaw
// Created:		7/12/93

/*
This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file with other programs, and to distribute
those programs without any restriction coming from the use of this
file.  (The General Public License restrictions do apply in other
respects; for example, they cover modification of the file, and
distribution when not linked into another program.)

This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifndef _BBSINT_C_
#define _BBSINT_C_

#include "bbshdr.h"

#undef DEBUG
// monitor connection object

extern moncon mon_obj;

// Method:	constructor
// Purpose:	initialize all variables and attempt to connect to sysop process
// Input:	none
// Output:	none
// Author:	Greg Shaw
// Created:	7/12/93

bbsint::bbsint()
{

	cur_colors[0] = cur_colors[1] = 0;		// no color .. yet
	cur_pos[0] = cur_pos[1] = 0;		// current position 0,0
	if (!mon_obj.connected())
		mon_obj.connect_mon();		// try to connect to monitor
	tcgetattr(0, &rbuf);	// get regular mode termio stuff
	term_mode(1);		// turn on nonblocking i/o
};

// Method:	char_avail
// Purpose:	return true if a character is available (depending on port)
// Input:	who - 0 for IPC (from monitor) 1 for from user
// Output:	1 for character available, 0 for no char available
// Author:	Greg Shaw
// Created:	7/12/93

int bbsint::char_avail(int who, int timeout)
{
	struct fd_set fds[2];
	struct timeval waittime;

	waittime.tv_sec = 0;
	waittime.tv_usec = 100;
	if (!who)		// ipc.
	{
#ifdef DEBUG
printf("ipc check\r\n");
fflush(stdout);
#endif
		if (mon_obj.connected())
			if (mon_obj.msg_avail(0))
				return(1);
	}
	else	// normal user
	{
#ifdef DEBUG
printf("serial check\r\n");
fflush(stdout);
#endif
		FD_SET(fileno(stdin), fds);
		if (select(1,fds,NULL,NULL,&waittime)) 
			return(1);
	}
	return(0);
}

// Method:	clear
// Purpose:	attempt to clear the user's screen
// Input:	none
// Output:	a ctrl-l is sent to user.  This should be changed to do
// 			curses/text screen clearing when full screen possible
// Author:	Greg Shaw
// Created:	7/25/93

int bbsint::clear(void)
{
	char	clrstr[] = {0xc,0};	// ctrl-l (new page)

	sstr(clrstr);
	return(0);
};

// Method:	cr
// Purpose:	send a carriage return to the user (and sysop if applicable)
// Input:	none
// Output:	see purpose.
// Author:	Greg Shaw
// Created:	7/12/93

void bbsint::cr(void)
{
	char crmsg[] = "\r\n";

	mon_obj.send_monitor(crmsg);
	printf(crmsg);
};

// Method:	display_file
// Purpose:	display a file to the user using the system pager
// Input:	path - path and file name (absolute path only)
//			page - use paging 
// Output:	file is displayed or error returned.
// Author:	Greg Shaw
// Created:	7/25/93

int bbsint::display_file(char *path, char page)
{
	char	pagername[30];
	char	tmpstr[255];
	char	c;
	int	clen;
	FILE	*infile;


	clear();
	if (page)
	{
		sprintf(tmpstr,"Viewing %s\n",path);		// add file name
		mon_obj.send_monitor(tmpstr);
		sstrcr(QTOQUIT);
		if (strcpy(pagername,sys_pager()), pagername == NULL)
			return(-1);
		strcpy(tmpstr,pagername);	// get command name
		strcat(tmpstr," ");			// add space
		strcat(tmpstr,path);		// add file name
		if (sysint(tmpstr,0,0) < 0)
		{
			ap_log("display_file: unable to execute sysint() call.");
			return(-1);
		}
		waitcr();
	}
	else
	{
		if (infile = bopen(path,"r"), infile == NULL)
			return(-1);
		else
			clen = 0;
			while (!feof(infile))
			{
				c = fgetc(infile);	
				if (clen % 50 == 0 && clen > 0)
				{
					tmpstr[clen] = 0;
					sstr(tmpstr);
					clen = 0;
				}
				if (!feof(infile))
					tmpstr[clen++] = c;
			}
			if (clen > 0)
			{
				tmpstr[clen] = 0;
				sstr(tmpstr);
			}
		bclose(infile);
		waitcr();
	}
	return(0);
};

// Method:	gstr 
// Purpose:	get a string from the user
// Input:	maxlen - the maximum length of a string to enter
// Output:	str - the string (as entered by user)
// Author:	Greg Shaw
// Created:	7/12/93

int bbsint::gstr(char *str, int maxlen)
{
	char tstr[255];
	char	c;
	int	offset;		// offset into string
	char	bsstr[] = {0x8,0x20,0x8};	// backspace
	char	bestr[] = {0x7,0};	// beep 

	offset = 0;
	while (c = gch(1), c != '\r' && c != '\n')
	{
		if (c != 0)
		{
			if (c == '\b' || c == 0x7f)
			{
				offset--;
				sstr(bsstr);
				sstr(bsstr);
				sstr(bsstr);
			}
			else if (offset < maxlen)
				tstr[offset++] = c;
			else
			{
				sstr(bsstr);
				sstr(bestr);
			}
		}
	}
	tstr[offset] = 0;
	strcpy(str,tstr);
	return(0);
};

// Method:	gch
// Purpose:	get a character from the user/sysop (if available)
// Input:	none
// Output:	a character, if available, or 0 if one not available
// Author:	Greg Shaw
// Created:	7/12/93

char bbsint::gch(int wait)
{
	struct fd_set fds[2];
	char 	msg[255];		// room for message
	char 	c;			// room for message
	struct timeval waittime;

	waittime.tv_sec = 0;
	waittime.tv_usec = 100;
	if (char_avail(0,1))	// has monitor sent something to me?
	{
#ifdef DEBUG
printf("monitor avail\r\n");
fflush(stdout);
#endif
		if (c = mon_obj.get_char(), c != -1)	// get message
			return(c);
	} else 
	if (char_avail(1,1))	// something available at serial port?
	{
#ifdef DEBUG
printf("serial avail\r\n");
fflush(stdout);
#endif
		c = fgetc(stdin);
		if (c > 0)
		{
			msg[0] = c;
			msg[1] = 0;
			mon_obj.send_monitor(msg);
			return(c);
		}
	}
//	fds[0].fd = fileno(stdin);
//	poll(fds,0,50*wait);	// poll (wait 50msec)
	FD_SET(fileno(stdin), fds);
	select(1,fds,NULL,NULL,&waittime);
	return(0);
};


// Method:	sch
// Purpose:	send a char to the user
// Input:	ch - character to send to user
// Output:	(to user)
// Author:	Greg Shaw
// Created:	7/12/93

void bbsint::sch(char ch)
{
	char newstr[2] = {ch,0};

	mon_obj.send_monitor(newstr);
	fwrite(newstr,2,1,stdout);
};

// Method:	sstr
// Purpose:	send a string to the user
// Input:	str - the string to send
// Output:	(to user)
// Author:	Greg Shaw
// Created:	7/12/93

int bbsint::sstr(char *msg)
{
	if (!mon_obj.connected())
		mon_obj.connect_mon();	// try to connect (if not already connected)
	mon_obj.send_monitor(msg);
	fwrite(msg,strlen(msg),1,stdout);
	fflush(stdout);
	return(0);
};

// Method:	sstr
// Purpose:	send a string to the user
// Input:	str - the string to send
// Output:	(to user)
// Author:	Greg Shaw
// Created:	7/12/93

int bbsint::sstrcr(char *msg)
{

	if (!mon_obj.connected())
		mon_obj.connect_mon();	// try to connect (if not already connected)
	mon_obj.send_monitor(msg);
	fwrite(msg,strlen(msg),1,stdout);
	cr();
	fflush(stdout);
	return(0);
};

// Method:	sstr
// Purpose:	send a string to the user
// Input:	str - the string to send
// Output:	(to user)
// Author:	Greg Shaw
// Created:	7/12/93

int bbsint::sysopstrcr(char *msg)
{
	char crmsg[] = {'\r','\n', 0};

	mon_obj.send_monitor(msg);
	mon_obj.send_monitor(crmsg);
	return(0);
};

// Function:	term_mode
// Purpose:	set the terminal mode to timeout on a read
// Input:	none
// Output:	none (terminal mode is changed)
// Author:	Greg Shaw
// Created:	7/19/93

void bbsint::term_mode(int type)
{
	struct termios tbuf;

	if (!type)	// go to regular mode
	{
		if (tstate != 0)
			tcsetattr(0, TCSANOW, &rbuf);
		tstate = 0;	
	}
	else
	{
		if (tstate != 1)	// regular mode
		{
			tcgetattr(0, &tbuf);

			tbuf.c_cc[4] = 1;		/* VMIN */
			tbuf.c_cc[5] = 0;		/* VTIME */
			tbuf.c_lflag &= ~(ICANON);

			tcsetattr(0, TCSANOW, &tbuf);
		}
		tstate = 1;	
	}
	return;
}

// Method:	wait
// Purpose:	prompt the user to hit 'return' to continue
// Input:	none
// Output:	none
// Author:	Greg Shaw
// Created:	7/25/93

void bbsint::waitcr(void)
{
	time_t	now,then;
	int	inactivity;
	char 	c;

	time(&then);
	inactivity = inactivity_timeout();
	sstr(CONTINUE);
	while (c = gch(1), c != '\r' && c != '\n')
	{
		time(&now);
		if ((now -then)/60 > inactivity)
			return;
	}
	cr();
};

// Method:	yesno
// Purpose:	get 'y' or 'n' from user.  
// Input:	none
// Output:	true for yes, false for no
// Author:	Greg Shaw
// Created:	7/13/93

int bbsint::yesno(void)
{
	struct fd_set fds[2];
	char 	found=0;
	char	c;
	struct timeval waittime;

	waittime.tv_sec = 0;
	waittime.tv_usec = 100;
	FD_SET(fileno(stdin), fds);
	while (!found)
	{
		c = toupper(gch(1));
		if (c != 0)
		{
			if (c == 'Y')
				found = 1;
			if (c == 'N')
				found = 2;
		}
		select(1,fds,NULL,NULL,&waittime);
	}
	fflush(stdin);
	cr();
	return(found==1);
};

#endif // _BBSINT_C_

