2 #include "ClientSocket.h"
7 struct ClientSocket *data;
12 static struct socket_list *sockets = NULL;
13 static char buffer[BUF_SIZ];
15 static void init_sockets() {
16 sockets = malloc(sizeof(*sockets));
19 perror("malloc() failed");
26 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
27 if(sockets == NULL) init_sockets();
28 struct ClientSocket *client = malloc(sizeof(*client));
31 perror("malloc() failed");
34 client->host = strdup(host);
36 client->pass = (pass == NULL ? NULL : strdup(pass));
39 client->bufferpos = 0;
41 client->next = sockets->data;
42 sockets->data = client;
46 int connect_socket(struct ClientSocket *client) {
47 if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
49 struct sockaddr_in addr;
51 if (!inet_aton(client->host, &addr.sin_addr))
53 host = gethostbyname(client->host);
56 herror("gethostbyname() failed");
59 addr.sin_addr = *(struct in_addr*)host->h_addr;
61 sock = socket(PF_INET, SOCK_STREAM, 0);
64 perror("socket() failed");
68 addr.sin_port = htons(client->port);
69 addr.sin_family = AF_INET;
71 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
73 perror("connect() failed");
78 client->flags |= SOCKET_FLAG_CONNECTED;
80 //send the IRC Headers
85 len = sprintf(sendBuf, "PASS :%s\n", client->pass);
86 write_socket(client, sendBuf, len);
88 len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
89 write_socket(client, sendBuf, len);
90 len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
91 write_socket(client, sendBuf, len);
96 int close_socket(struct ClientSocket *client) {
97 if(client == NULL) return 0;
98 if((client->flags & SOCKET_FLAG_CONNECTED))
100 struct ClientSocket *sock, *last_sock = NULL;
101 for (sock = sockets->data; sock; sock = sock->next) {
104 last_sock->next = sock->next;
106 sockets->data = sock->next;
117 int write_socket(struct ClientSocket *client, char* msg, int len) {
118 if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
119 printf("[send %d] %s", len, msg);
120 write(client->sock, msg, len);
124 void socket_loop(int timeout_seconds) {
125 if(sockets == NULL) return;
127 struct timeval timeout;
128 struct ClientSocket *sock;
129 int ret = 0, bytes, i;
132 for (sock = sockets->data; sock; sock = sock->next) {
133 if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
134 FD_SET(sock->sock, &fds);
138 timeout.tv_sec = timeout_seconds;
140 ret = select(ret + 1, &fds, NULL, NULL, &timeout);
142 for (sock = sockets->data; sock; sock = sock->next) {
143 if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
144 if(sock->bufferpos != 0) {
145 bytes = read(sock->sock, buffer, sizeof(buffer));
147 for(i = 0; i < bytes; i++) {
148 if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
149 sock->buffer[sock->bufferpos + i] = buffer[i];
151 sock->bufferpos += i;
154 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
156 sock->bufferpos = bytes;
160 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
161 bot_disconnect(sock);
163 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
164 if(used == sock->bufferpos + 1) {
165 //used all bytes so just reset the bufferpos
168 for(i = 0; i < sock->bufferpos - used; i++) {
169 sock->buffer[i] = sock->buffer[i+used];
171 sock->bufferpos -= used;
179 putsock(struct ClientSocket *client, const char *text, ...)
182 char sendBuf[MAXLEN];
184 if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
186 va_start(arg_list, text);
187 pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
189 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
191 sendBuf[pos+1] = '\0';
192 write_socket(client, sendBuf, pos+1);
195 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
196 struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
197 if(sock == NULL) return NULL;
198 for (; sock; sock = sock->next) {
199 if((sock->flags & flags) == flags)
205 void free_sockets() {
207 struct ClientSocket *client, *next;
208 for (client = sockets->data; client; client = next) {
210 if((client->flags & SOCKET_FLAG_CONNECTED))