aboutsummaryrefslogtreecommitdiff
path: root/irc.c
blob: 5af46aec9449ad9c65d696be01d03305cc9d7c2c (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
#include "irc.h"

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

int g_ircsock = -1;

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()
{
	char msg[] = "NICK "NICK"\r\nUSER "USERNAME" 0 * :"REALNAME"\r\nJOIN "CHANNEL"\r\n";
	return irc_sendall(msg, sizeof msg-1);
}



/* **** */

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.

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 -1;
	}

	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;
}