/*
**	Misc for MB33J (RFwd etc)
**
**	Copyright (C) 1988
**	Akira Yamada(JA3JWI)
**	401, 1-18 UchiHiranomachi
**	Higashi-ku   Osaka   City
**
**	Any part of this program may be  reproduced,  transmitted,
**	transcribed, stored in a retrieval system,  or translated
**	into any language or computer language, in any form or by
**	any  means,   electonic,  mecahnical,  magnetic,  optical,
**	chemical, manual or otherwise,  without  the prior witten
**	permission of the author.
*/

#include "mb.h"
#include "mbexp.h"
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#if !SWAP_NO			/* AY-880921 */
#include "mbswap.h"
#endif

extern int	trace_tnc;	/* AY-880619 */

#if !DOMAIN_NO
int	domainok, extfwdok;	/* AY-880903 */
extern char domain[];	/* AY-880903 */
#endif
/* extern int  showexp;   /* AY-871102 AY-880830 */

extern short  deep;
extern FILE   *ifl[];
extern short skipit;
extern char *nxtin();
extern char *strpbrk();
extern char *getenv();

extern char fwdpath[] ;  /* feb11,90 */

#define maxscript 10

/*
 * Next variables are decreaed in mbfwd.c
 */

extern char  *script;
extern char  *fwdfile;
extern char  fcall[];
extern short onebbs;
extern char  igtime;

int    rfwd, rfwdok;
int    sprompt;

char	rliversion[80], mblmode[80];
char  *can_t_do;		   /* AY-880228 */
char  shell_name[20];	   /* AY-880228 */
char  *meantime;           /* AY-880108 */
char  *abortby_usr;        /* AY-871218 */

char  *rfwdfile;
char  edname[EDNAME];
char  mean_time[ln_call];

editenv()       /* ES Command - X */
{
char *strchr();

    do {
        printenv();
        getdat();
        pparse();
        switch(port->opt1) {
                case '1':
			editenvs(edname); 
			break;
                case '2':
			editenvs2(&trace_tnc); 
			break;
                case '3':
			editenvs(mean_time); 
			break;
                case '4':
                /*MAY2,91
			editenvs(shell_name); 
			break;
                case '5':
                */
                        editenvs2(&check_kanji_title); 
			break;
#if (XENIX|UNIX|BSD)
                case '5':
                        editenvs(shell_name);
                        break ;
                case '6':
			editenvs2(&kill_after_m); 
			break;
                case 'S':
			outstr(rliversion);
                        outstr("Modify to?:");
			getdat();
                        outchar('\7');
                        unbl(tmp->scr, port->line, 10);
                        strcpy(port->line, strchr(rliversion, '-'));
                        sprintf(rliversion, "[%s%s", tmp->scr, port->line);
                        /* End of AY-890101 */
                        outstr("\7Modified SID: ");
			outstr(rliversion);
			wait(2);
			outchar('\7');
			break;
#endif
                default:
			return;
		}
	} while(port->line[0]);
}

editenvs(ptr)
char ptr[];
{
	char buf[BUFSIZ];

	sprintf(buf, "[%s] change to:", ptr);
	outstr(buf);

	getdat();
	remnl(port->line);
	strcpy(ptr, port->line);
}

editenvs2(parm)
int *parm;
{
	char buf[BUFSIZ];

	sprintf(buf, "[%s] change to [y/n]:", (*parm & 1) ? "Yes" : "No");
	outstr(buf);
	getdat();

	switch(*(port->line)) {
	case 'Y':
	case 'y': 
		*parm setbit 1; 
		break;
	case 'N':
	case 'n': 
		*parm clrbit 1; 
		break;
	default: 
        outstr("What? Y[es]|N[o] only\n");
	}
}

printenv()
{
    sprintf(tmp->scr, "\n[1] Editor is: \"%s\"\n", edname); /* CHANGED SMC */
    outstr(tmp->scr);
    sprintf(tmp->scr, "[2] Traceing TNC : \"%s\"\n",
	    (trace_tnc & 1) ? "Yes" : "No");
    outstr(tmp->scr);
    sprintf(tmp->scr, "[3] Mean time is: \"%s\"\n", mean_time);
    outstr(tmp->scr);
/*
    sprintf(tmp->scr, "[4] Shell name is: \"%s\"\n", shell_name);
    outstr(tmp->scr);
*/
    sprintf(tmp->scr, "[4] List Kanji Titled messages: \"%s\"\n",
	    (check_kanji_title & 1) ? "Yes" : "No");
    outstr(tmp->scr);
#if	(XENIX|UNIX|BSD)
    sprintf(tmp->src, "[5] Shell name is: \"%s\"\n", shell_name);
    outstr(tmp->src);
    sprintf(tmp->scr, "[6] Rm after fwd to xenix: \"%s\"\n",
	    (kill_after_m & 1) ? "Yes" : "No");
    outstr(tmp->scr);
    sprintf(tmp->scr, "[S] Modify SID : %s", rliversion);
    outstr(tmp->scr);
#endif
    outstr("\nSelect number(CR for end).\n");
}

setupsid()
{
#if (XENIX|BSD|UNIX)
	strcpy(rliversion, "[Net-UN*X-H$]\n");
	strcpy(mblmode, "[Net-UN*X-H$]\n");
#else
        strcpy(rliversion, "[MBX-46.06-H$]\n");
        strcpy(mblmode, "[MBX-DOS-H$]\n");
#endif
}

#if !SWAP_NO	/* AY-880921 */
info_swap()
{
extern AT_SWAP	*at_swaproot;
extern TO_SWAP	*to_swaproot;
extern DISARP	*disarproot;
AT_SWAP *at_tmp;
TO_SWAP *to_tmp;
DISARP	*dis_tmp;
char call1[ln_call + 1], call2[ln_call + 1], filenam[80];

        fprintf(stderr, "Swaping To call's\n");
	for (to_tmp = to_swaproot; to_tmp; to_tmp = to_tmp->next) {
		unbl(call1, to_tmp->to, ln_call);
		unbl(call2, to_tmp->new_to, ln_call);
                fprintf(stderr, "%s to %s\n", call1, call2);
	}
        fprintf(stderr, "------    ------\n");
        fprintf(stderr, "Swaping At call's\n");
	for (at_tmp = at_swaproot; at_tmp; at_tmp = at_tmp->next) {
		unbl(call1, at_tmp->at, ln_call);
		unbl(call2, at_tmp->new_at, ln_call);
                fprintf(stderr, "%s to %s\n", call1, call2);
	}
        fprintf(stderr, "----------------\n");
        fprintf(stderr, "Exchange Distirbution files\n");
	for (dis_tmp = disarproot; dis_tmp; dis_tmp = dis_tmp->next) {
		unbl(call1, dis_tmp->to, ln_call);
		unbl(call2, dis_tmp->at, ln_call);
		unbl(filenam, dis_tmp->disfile, sizeof(filenam));
                fprintf(stderr, "%s & %s use %s\n", call1, call2, filenam);
	}
}
#endif

initenv()
{
char *getenv();
char *ptr;

	setupsid();

        if (*meantime) {
		strncpy(mean_time, meantime, 3);	/* AY-880525 */
                mean_time[3] = '\0';                    /* AY-881215 */
        } else {
            ptr = getenv("TZ");
            if (ptr != NULL) {
                    strncpy(mean_time, ptr, 3);
                    mean_time[3] = '\0';            /* AY-881215 */
            } else
                    *mean_time = '\0';
	}

	ptr = getenv("SHELL");
	if (ptr != NULL)
		(void)strcpy(shell_name,ptr);
	else
		*shell_name = '\0';
        /* fprintf(stderr, "SHELL=\"%s\"\n", ptr);    DEL Jun23,90 */
        fprintf(stderr, "EDITOR=");
        if (ptr = getenv("EDITOR")) {
                fprintf(stderr, "\"%s\"", ptr);
		strcpy(edname, ptr);
        }                           /* CHANGED SMC Feb. EDITOR-->"EDITOR" */

       fprintf(stderr, "\nFWD HEADER=%s\n", mm[4]);

    msgbid = xalloc(MSG_BID, 1);
    msgbid = (MSG_BID *) malloc (sizeof (MSG_BID)); /* add from V46.xx 3/11*/
}

#if !O_NO
shell()   /* add subcommand for MS-DOS  Feb.12,90 */
{
        char subcmd[80] ;
        sprintf (subcmd, "%s%c%c %s", "sh", port->opt2, port->id, port->fld[1]) ;
        system (subcmd) ;
}
#endif

refwd()
{
register PORTS *saveport;
int rvsdone ;
	if (!sprompt) { /* allow reforward only host mode AY-970911 */
		outstr("*** Done.\n");
		return;
	}
        rvsdone = 0 ;  /* May18,91   RVS-done end  */
	deep = 0;

	if ((ifl[deep] = fopen(rfwdfile, "r")) is NULL)
	if ((ifl[deep] = fopen(fwdfile, "r")) is NULL) {
		outstr("*** Done.\n");
		return;
	}

	saveport = port;
	igtime = ' ';
	while(nxtin(cport->line) isnt NULL) {
		if (saveport->mode & idle) {
			while(deep >= 0) {
				fclose(ifl[deep]);
				deep--;
			}
			ioport(saveport);
			return;
		}

		ioport(cport);
		uc(cport->line);
		pparse();
		switch(cport->opt1) {
		case 'P':
			passlst(); 
			break;
		case 'C':
		case 'R':
		case 'S':
		case 'Q':
		case 'D':
		case '>':
		case '<':
		case '!':
			break;
		case 'F':
		case 'G':
                case 'H':    /* check Time + callsign + port NO  5/13    */
                        pcall(fcall,cport->fld[1]) ;  /* Fwd.mbx call */
			if (chktime())
                            if ((wcm(fcall, saveport->user->call)) and
                                   ( saveport->id is cport->opt2))   {
                                sprintf (fwdpath,"F> %s",cport->fld[1] ) ;
                                ioport(saveport);
				dorefwd();

                                if (!(port->mode & idle))
                                        outstr ("*** Done.\n") ;
                                rvsdone = 1 ;
                                ioport (saveport) ;
                            } else  passlst();     /* add "else" 3/11 jh1nai */
			break;
		default:  
			passlst(); 
			break;
		}
	}
	ioport(saveport);
	port->flags clrbit p_dofwd;
	port->flags clrbit p_fwdok;
        if (!(port->mode & idle) and (rvsdone == 0) )
		outstr("*** Done.\n");
}

/*
 *  Reforward whatever messages go to this MailBox.
 */

dorefwd()
{
	register char *st;

	st = nxtin(port->line);
	while (!iseof(port->line) and (st isnt NULL)) {
		pparse();
		pcall (tcall, port->fld[0]);
		if (findfwd(tcall, bfwd, bfwdn))
			if (fmsg())
			if (port->mode & idle) {
				passlst();
				rfwdok = false;
				return;
			}

		st = nxtin(port->line);
	}
}

isbreak()
{
        if (port->dev is p_console) return false ; /* May5,1990 */
	if (instat())
	if (getdat()) {
		if (isdis(port->line)) { 
			cmdtnc(); 
			port->mode = idle; 
			return true; 
		} else if (port->mode & timeout) { 
			cmdtnc(); 
			port->mode = idle; 
			return true; 
		}

		if ((port->line[0] is 'a') or (port->line[0] is 'A')) {
			if (port->line[1] isnt '\n')	/* AY-880515 */
				return false;

			port->msg = abortby_usr;	/* AY-881006 */
			return true;
		}
	}
	return false;
}

#if	!(XENIX|UNIX|BSD)
editor(tname)
char *tname;
{
	char buf[BUFSIZ];
	FILE *fp;

        if (*edname) {
                if (!isatty(0)) return false;   /* AY-880515 */

                sprintf(buf, "%s %s", edname, EDTMP);
		if(system(buf)) { 
                    /*MAY2,91
                        outstr(mcant);
			outstr(buf); 
			outstr("\n");
                    */
			return false; 
		}
		copy(EDTMP, tname);
		unlink(EDTMP);
		return(true);
	}
	return (false);
}
#endif

/*
 *  "Copy, no blanks".
 */

cpnb(p, n, buf)
char *p;
short n;
char *buf;
{
	int i;

	i = 0;
	while (n--) {
		switch (*p) {
		case ' ' :
		case '\n' :
		case '\0' : 
			return i;
		default: 
			*buf++ = *p++; 
			i++;
			break;
		}
	}
	return i;
}

debugmsg(msg)
char *msg;
{
	fprintf(stderr, "%s", msg);
}

/*
**	syserr: printout error message & die
*/

#if	(XENIX|UNIX|BSD)
syserr(msg, code)
char *msg;
int code;
{
	fprintf(stderr, "MB: %s(code=%d)\n Stop.\n", msg, code);
	die(0);
}
#else
syserr(msg, code)
char *msg;
int code;
{
    fprintf(stderr, "MBX: %s(code=%d)\n Stop.\n", msg, code);
	exit(1);
}
#endif
