aboutsummaryrefslogtreecommitdiff
path: root/irc/c_bsd.c
blob: 25bbd83e71abfdd769a0679efd82f9858e701c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/************************************************************************
 *   IRC - Internet Relay Chat, irc/c_bsd.c
 *   Copyright (C) 1990, Jarkko Oikarinen and
 *                       University of Oulu, Computing Center
 *
 *   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.
 */

#ifndef lint
static  char rcsid[] = "@(#)$Id: c_bsd.c,v 1.5 1998/12/13 00:02:35 kalt Exp $";
#endif

#include "os.h"
#include "c_defines.h"
#define C_BSD_C
#include "c_externs.h"
#undef C_BSD_C

#ifdef AUTOMATON
#ifdef DOCURSES
#undef DOCURSES
#endif
#ifdef DOTERMCAP
#undef DOTERMCAP
#endif
#endif /* AUTOMATON */

#define	STDINBUFSIZE (0x80)

int	client_init(host, portnum, cptr)
char	*host;
int	portnum;
aClient	*cptr;
{
	int	sock;
	static	struct	hostent *hp;
	static	struct	SOCKADDR_IN server;

	sock = socket(AFINET, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("opening stream socket");
		exit(1);
	}
	server.SIN_FAMILY = AFINET;
 
	if (isdigit(*host))
#ifdef INET6
	    {
		if(!inet_pton(AF_INET6, host, server.sin6_addr.s6_addr))
			bcopy(minus_one, server.sin6_addr.s6_addr, IN6ADDRSZ);
	    }
#else
		server.sin_addr.s_addr = inetaddr(host);
#endif
	else { 
#ifdef INET6
		res_init();
		_res.options|=RES_USE_INET6;
#endif
		hp = gethostbyname(host);
		if (hp == 0) {
			fprintf(stderr, "%s: unknown host\n", host);
			exit(2);
		}
		bcopy(hp->h_addr, (char *)&server.SIN_ADDR, hp->h_length);
	}
	server.SIN_PORT = htons(portnum);
	if (connect(sock, (SAP)&server, sizeof(server)) == -1) {
		perror("irc");
	 	exit(1);
	}

	cptr->acpt = cptr;
	cptr->port = server.SIN_PORT;
#ifdef INET6
	bcopy(server.sin6_addr.s6_addr, cptr->ip.s6_addr, IN6ADDRSZ);
#else
	cptr->ip.s_addr = server.sin_addr.s_addr;
#endif
	return(sock);
}

void client_loop(sock)
int	sock;
{
	int	i = 0, size, pos;
	char	apubuf[STDINBUFSIZE+1];
	fd_set	ready;

	do {
		if (sock < 0 || QuitFlag)
			return;
		FD_ZERO(&ready);
		FD_SET(sock, &ready);
		FD_SET(0, &ready);
#ifdef DOCURSES
		if (termtype == CURSES_TERM)
			move(LINES-1,i); refresh();
#endif
#ifdef DOTERMCAP
		if (termtype == TERMCAP_TERM)
			tcap_move (-1, i);
#endif
		if (select(32, (SELECT_FDSET_TYPE *)&ready, 0, 0, NULL) < 0) {
/*      perror("select"); */
			continue;
		}
		if (FD_ISSET(sock, &ready)) {
			if ((size = read(sock, apubuf, STDINBUFSIZE)) < 0)
				perror("receiving stream packet");
			if (size <= 0) {
				close(sock);
				return;
			}
			dopacket(&me, apubuf, size);
		}
#ifndef AUTOMATON
		if (FD_ISSET(0, &ready)) {
			if ((size = read(0, apubuf, STDINBUFSIZE)) < 0) {
				putline("FATAL ERROR: End of stdin file !");
				return;
			}
			for (pos = 0; pos < size; pos++) {
				i=do_char(apubuf[pos]);
#ifdef DOCURSES
				if (termtype == CURSES_TERM)
					move(LINES-1, i);
#endif
#ifdef DOTERMCAP
				if (termtype == CURSES_TERM)
					tcap_move(-1, i);
#endif
			}
		}
#endif /* AUTOMATON */
	} while (1);
}