/*
    Pirate Bulletin Board System
    Copyright (C) 1990, Edward Luke

    This program 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 1, or (at your option)
    any later version.

    This program 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; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    Email address:
    lush@Athena.EE.MsState.EDU
*/
#include "bbs.h"
#include "io.h"

#include <sys/types.h>
#ifdef lint
#include <sys/uio.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>

extern struct user_info uinfo ;

t_chat()
{
	char chatid[STRLEN] ;
	struct hostent *h ;
	char hostname[STRLEN] ;
	struct sockaddr_in sin ;
	int a, n ;
	int currline, currchar ;
	char inbuf[80] ;
    int pline,echatwin ;
    extern int t_lines, dumb_term ;

	report("Chat") ;
	currline = 0 ;
	currchar = 0 ;
	move(2,0) ;
	prints("<Enter Chat alias (8 char max)>\n") ;
	getdata(1,0,"Enter Chat id: ", chatid,9,DOECHO,NULL) ;
	if(chatid[0] == '\0')
	  strcpy(chatid,currentuser.userid) ;
	chatid[8] = '\0' ;
	strcat(chatid,":                      ") ;
	chatid[10] = '\0' ;
	strcpy(inbuf,chatid) ;
	gethostname(hostname,STRLEN) ;
	if(!(h = gethostbyname(hostname))) {
		perror("gethostbyname") ;
		return -1 ;
	}
	bzero(&sin,sizeof sin) ;
	sin.sin_family = h->h_addrtype ;
	bcopy(h->h_addr, &sin.sin_addr, h->h_length) ;
	sin.sin_port = 4000 ;
	a = socket(sin.sin_family, SOCK_STREAM, 0) ;
	if((connect(a, &sin, sizeof sin))) {
		move(2,0) ;
		prints("Creating Chat Daemon\n") ;
		refresh() ;
		close(a) ;
		do_exec("/bin/bbs.chatd 4000",NULL) ;
		sleep(2) ;
		a = socket(sin.sin_family, SOCK_STREAM, 0) ;
		if((connect(a,&sin, sizeof sin))) {
			perror("connect failed") ;
			return -1 ;
		}
	}
	uinfo.mode = CHAT ;
	substitute_record(ULIST,&uinfo, sizeof(uinfo), utmpent) ;
	sprintf(inbuf,"%sWelcome!",chatid) ;
	write(a,inbuf,strlen(inbuf)+1) ;
	strcpy(inbuf,chatid) ;
	clear() ;
    pline = t_lines - 1 ;
    echatwin = t_lines - 2 ;
	move(echatwin,0) ;
    prints("-------------------------------------------------------------------------------") ;
	move(pline,0) ;
    if(!dumb_term)
      prints("%s",inbuf) ;
	currchar = strlen(inbuf) ;
    add_io(a,0) ;
	while(YEA) {
        int ch = igetch() ;

		if(ch == I_OTHERDATA) {
			static int cnt  = 0;
            int cc ;
			static char buf[512] ;
			
			if((cc = read(a,buf+cnt,sizeof buf)) > 0) {
                int processed = 0 ;

                while(cc > 0) {
                    register int i ;
                    for(i=processed;buf[i] != '\0' && i != sizeof buf;i++)
                      /* NULL STATEMENT */ ;
                    if(i==sizeof buf && buf[i] != '\0') {
                        bcopy(buf+processed,buf,processed-sizeof(buf)) ;
                        cnt = processed - sizeof(buf) ;
                        break ;
                    }
                    cnt = 0 ;
                    move(currline++,0) ;
                    clrtoeol() ;
                    prints("%s\n",buf+processed) ;
                    if(currline == echatwin)
                      currline = 0 ;
                    move(currline,0) ;
                    clrtoeol() ;
                    standout() ;
                    if(!dumb_term)
                      prints("-->") ;
                    standend() ;
                    i++ ;
                    cc -= (i - processed) ;
                    processed = i ;
                    if(i == sizeof buf)
                      break ;
                }
            }
            move(pline,currchar) ;
            continue ;
		}
        if(isprint(ch)) {
            if(currchar == 78) {
                bell() ;
                continue ;
            }
            inbuf[currchar] = ch ;
            currchar++ ;
            inbuf[currchar] = '\0' ;
            move(pline,currchar-1) ;
            prints("%c",ch) ;
            continue ;
        }
        if(ch == '\n' || ch == '\r') {
            char buf[STRLEN] ;
				int i,j ;
            
            if(dumb_term)
              prints("\n") ;
            for(i=10,j=0;i<STRLEN;i++) {
                if(inbuf[i] == '\0') {
                    buf[j] = '\0' ;
                    break ;
                }
                if(inbuf[i] != ' ')
                  buf[j++] = inbuf[i] ;
            }
            if(buf[0] == '\0')
              continue ;
            if(!strcmp(buf,"Goodbye!")) {
                report("Chat user typed Goodbye!") ;
                sprintf(inbuf,"%sGoodbye!",chatid) ;
                write(a,inbuf,strlen(inbuf)+1) ;
                break ;
            }
            write(a,inbuf,strlen(inbuf)+1) ;
            move(pline,0) ;
            strcpy(inbuf,chatid) ;
            currchar = strlen(inbuf) ;
            if(!dumb_term)
              prints("%s",inbuf) ;
            clrtoeol() ;
            move(pline,currchar) ;
            continue ;
        }
        if(ch == 0x08 || ch == '\177') {
            if(currchar == 10) {
                bell() ;
                continue ;
            }
            currchar-- ;
            move(pline,currchar) ;
            if(dumb_term)
              ochar(0x08) ;
            else
              prints(" ") ;
            move(pline,currchar) ;
            inbuf[currchar] = '\0' ;
            continue ;
        }
        if(ch == 0x03 || ch == 0x04) {
            sprintf(inbuf,"%sGoodbye!",chatid) ;
            write(a,inbuf,strlen(inbuf)+1) ;
            break ;
        }
    }
    add_io(0,0) ;
	close(a) ;
	uinfo.mode = IDLE ;
	substitute_record(ULIST, &uinfo, sizeof(uinfo), utmpent) ;
	clear() ;
	return  0;
}
