aboutsummaryrefslogtreecommitdiff
path: root/irc.c
blob: b30af48cb59021cfa056ca6c90dae02378b865c9 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "irc.h"

#include "debug.h"
#include "conf.h"
#include "util.h"

int g_ircsock = -1;

char *g_chanlist[] = {
	"a",
	"b",
	"g",
	"cg",
	"secret-channel",
};
const int g_nchannels = sizeof(g_chanlist)/sizeof(g_chanlist[0]);


int
irc_connect()
{
	struct addrinfo hints;
	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	printf("connecting to %s:%s....\n",HOST,PORT);

	int status;
	struct addrinfo *res;
	if ((status = getaddrinfo(HOST, PORT, &hints, &res)) != 0) {
		fprintf(stderr, "gettaddrinfo fail: %s\n", gai_strerror(status));
		return -1;
	}

	int sockfd;
	for (; res != NULL; res = res->ai_next) {
		if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
			perror("socket");
			continue;
		}
		if (connect(sockfd, res->ai_addr, res->ai_addrlen) != 0) {
			perror("connect");
			continue;
		}
		goto success;
	}
	return -1;
success:
	g_ircsock = sockfd;
	return 0;
}

int 
irc_sendall(char msg[], size_t len)
{
	return sendall(g_ircsock,  msg, len);
}

int
irc_handshake()
{	
	#define E(x) do { if (x==-1) { perror("irc_sendall"); return -1; } } while(0)
	char msg1[] = "NICK "NICK"\r\nUSER "USERNAME" 0 * :"REALNAME"\r\n";
	E(irc_sendall(msg1, sizeof msg1-1));
	for (int i = 0; i<g_nchannels; i++) {
		if (strlen(g_chanlist[i]) > 16) {
			fprintf(stderr,"channel name too long (%s)\n",g_chanlist[i]);
			return -1;
		}
		E(irc_sendall("JOIN #",6));
		E(irc_sendall(g_chanlist[i],strlen(g_chanlist[i])));
		E(irc_sendall("\r\n",2));
	}
	#undef E
	return 0;
}



/* **** */

static void
handle(char msg[], size_t len)
{
	printf("HANDLE {");
	int i;
	for (i=0; i<len; i++) {
		switch (msg[i]) {
		case '\r':
			printf("<CR>");
			break;
		case '\n':
			printf("<LF>");
			break;
		default:
			putchar(msg[i]);
			break;
		}
	}
	printf("}\n");

	if (len >=4 && strncmp(msg, "PING", 4) == 0) {
		DP("ponging");
		// this modifies buffer in place. which is presumably fine
		msg[1] = 'O';
		irc_sendall(msg,len);
	}
}


#define RECVBUF_SIZE 1024
char recv_buf[RECVBUF_SIZE];
char *recv_cur = &recv_buf[0];
// one past the end
char *recv_end = &recv_buf[RECVBUF_SIZE];

// i can't be bothered to implement a proper circular buffer
// (it'd be more effort than this anyway, for very little gain)
// the start of the buffer (index 0) is always the start of the buffer
// then cur points to one past the most recently recieved byte.
// after processing a message, if there's more stuff in the buffer, memmove it back.

// returns -2 on eof, -1 on other error, 0 on success
int
irc_recv()
{
	ssize_t amt;
	if ((amt = recv(g_ircsock, recv_cur, recv_end-recv_cur, 0)) == -1) {
		// socket error
		perror("recv");
		return -1;
	} else if (amt == 0) {
		// eof
		fprintf(stderr,"irc eof\n");
		return -2;
	}

	DPf("recieved %ld ",amt);

	// find the end of a message
	char *c;
	char *end = recv_cur+amt;
	char *msg_start = recv_buf;


	DPn("BUF",recv_buf,end-recv_buf);
	
	for (c=recv_buf; c<end; c++) {
		if (*c == '\r' && *(c+1) == '\n') {
			handle(msg_start, (c+2)-msg_start);
			msg_start = c+2;
			c++;
		}
	}

	if (msg_start == end) 
		recv_cur = recv_buf;
	else {
		size_t remlen = end - msg_start;
		if (remlen > 512) {
			fprintf(stderr,"my receive buffer is filled with NONSENSE!!!\n");
			return -1;
		}
		if (recv_buf != msg_start) {
			DPf("mem moving %ld\n",remlen);
			memmove(recv_buf, msg_start, remlen);
		}
		recv_cur = recv_buf + remlen;
	}

	return 0;
}