rewrote HandleInfoHandler.c (WHOHandler style)
[NeonServV5.git] / src / ClientSocket.c
1 /* ClientSocket.c - NeonServ v5.2
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "ClientSocket.h"
19 #include "IRCParser.h"
20 #include "UserNode.h"
21 #include "IRCQueue.h"
22 #include "WHOHandler.h"
23 #include "HandleInfoHandler.h"
24
25 struct socket_list {
26     struct ClientSocket *data;
27     unsigned count;
28 };
29
30 //the magic list :P
31 static struct socket_list *sockets = NULL;
32 static char buffer[BUF_SIZ];
33
34 static void init_sockets() {
35     sockets = malloc(sizeof(*sockets));
36     if (!sockets)
37     {
38         perror("malloc() failed");
39         return;
40     }
41     sockets->data = NULL;
42     sockets->count = 0;
43 }
44
45 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
46     if(sockets == NULL) init_sockets();
47     struct ClientSocket *client = malloc(sizeof(*client));
48     if (!client)
49     {
50         perror("malloc() failed");
51         return NULL;
52     }
53     client->host = strdup(host);
54     client->port = port;
55     printf("Connect: %s:%d\n", client->host, client->port);
56     client->pass = (pass == NULL ? NULL : strdup(pass));
57     client->user = user;
58     client->flags = 0;
59     client->bufferpos = 0;
60     client->traffic_in = 0;
61     client->traffic_out = 0;
62     client->connection_time = 0;
63         client->botid = 0;
64     client->clientid = 0;
65     client->queue = NULL;
66     client->whoqueue_first = NULL;
67     client->whoqueue_last = NULL;
68     client->handleinfo_first = NULL;
69     client->handleinfo_last = NULL;
70     client->next = sockets->data;
71     sockets->data = client;
72     return client;
73 }
74
75 #ifdef WIN32
76
77 int connect_socket(struct ClientSocket *client) {
78     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
79     struct hostent *host;
80     struct sockaddr_in addr;
81     int sock;
82     addr.sin_addr.s_addr = inet_addr(client->host);
83     if (addr.sin_addr.s_addr == INADDR_NONE) {
84         host = gethostbyname(client->host);
85         if(!host) {
86             return SOCKET_ERROR;
87         }
88         memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
89     }
90     sock = socket(PF_INET, SOCK_STREAM, 0);
91     if (sock == -1)
92     {
93         perror("socket() failed");
94         return 0;
95     }
96
97     addr.sin_port = htons(client->port);
98     addr.sin_family = AF_INET;
99
100     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
101     {
102         perror("connect() failed");
103         return 0;
104     }
105
106     client->sock = sock;
107     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
108     client->connection_time = time(0);
109
110     //send the IRC Headers
111     char sendBuf[512];
112     int len;
113
114     if(client->pass && strcmp(client->pass, "")) {
115         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
116         write_socket(client, sendBuf, len);
117     }
118     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
119     write_socket(client, sendBuf, len);
120     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
121     write_socket(client, sendBuf, len);
122
123     return 1;
124 }
125
126 #else
127
128 int connect_socket(struct ClientSocket *client) {
129     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
130     struct hostent *host;
131     struct sockaddr_in addr;
132     int sock;
133     if (!inet_aton(client->host, &addr.sin_addr))
134     {
135         host = gethostbyname(client->host);
136         if (!host)
137         {
138             perror("gethostbyname() failed");
139             return 0;
140         }
141         addr.sin_addr = *(struct in_addr*)host->h_addr;
142     }
143     sock = socket(PF_INET, SOCK_STREAM, 0);
144     if (sock == -1)
145     {
146         perror("socket() failed");
147         return 0;
148     }
149
150     addr.sin_port = htons(client->port);
151     addr.sin_family = AF_INET;
152
153     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
154     {
155         perror("connect() failed");
156         return 0;
157     }
158
159     client->sock = sock;
160     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
161     client->connection_time = time(0);
162
163     //send the IRC Headers
164     char sendBuf[512];
165     int len;
166
167     if(client->pass && strcmp(client->pass, "")) {
168         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
169         write_socket(client, sendBuf, len);
170     }
171     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
172     write_socket(client, sendBuf, len);
173     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
174     write_socket(client, sendBuf, len);
175
176     return 1;
177 }
178
179 #endif
180
181 int close_socket(struct ClientSocket *client) {
182     if(client == NULL) return 0;
183     if((client->flags & SOCKET_FLAG_CONNECTED))
184         close(client->sock);
185     struct ClientSocket *sock, *last_sock = NULL;
186     for (sock = sockets->data; sock; sock = sock->next) {
187         if(sock == client) {
188             if(last_sock)
189                 last_sock->next = sock->next;
190             else
191                 sockets->data = sock->next;
192             sockets->count--;
193         } else
194             last_sock = sock;
195     }
196     if(client->queue)
197         queue_destroy(client);
198     if(client->whoqueue_first)
199         clear_whoqueue(client);
200     if(client->handleinfo_first)
201         clear_handleinfoqueue(client);
202     free(client->host);
203     free(client->pass);
204     free(client);
205     return 1;
206 }
207
208 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
209     printf("[send %d] %s", len, msg);
210     #ifdef WIN32
211     send(client->sock, msg, len, 0);
212     #else
213     write(client->sock, msg, len);
214     #endif
215     client->traffic_out += len;
216     return 1;
217 }
218
219 int write_socket(struct ClientSocket *client, char* msg, int len) {
220     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
221     if(client->flags & SOCKET_FLAG_USE_QUEUE)
222         return queue_add(client, msg, len);
223     else
224         return write_socket_force(client, msg, len);
225 }
226
227 void socket_loop(int timeout_seconds) {
228     if(sockets == NULL) return;
229     fd_set fds;
230     struct timeval timeout;
231     struct ClientSocket *sock;
232     int ret = 0, bytes, i;
233     
234     FD_ZERO(&fds);
235     for (sock = sockets->data; sock; sock = sock->next) {
236         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
237         FD_SET(sock->sock, &fds);
238         if(sock->sock > ret)
239             ret = sock->sock;
240     }
241     timeout.tv_sec = timeout_seconds;
242     timeout.tv_usec = 0;
243     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
244     if(ret == 0) return;
245     for (sock = sockets->data; sock; sock = sock->next) {
246         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
247             if(sock->bufferpos != 0) {
248                 #ifdef WIN32
249                 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
250                 #else
251                 bytes = read(sock->sock, buffer, sizeof(buffer));
252                 #endif
253                 if(bytes > 0) {
254                     for(i = 0; i < bytes; i++) {
255                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
256                         sock->buffer[sock->bufferpos + i] = buffer[i];
257                     }
258                     sock->bufferpos += i;
259                 }
260             } else {
261                 #ifdef WIN32
262                 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
263                 #else
264                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
265                 #endif
266                 if(bytes > 0)
267                     sock->bufferpos = bytes;
268             }
269             if(bytes <= 0) {
270                 //error
271                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
272                 bot_disconnect(sock);
273                 if(sock->queue)
274                     queue_destroy(sock);
275             } else {
276                 sock->traffic_in += bytes;
277                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
278                 if(used == sock->bufferpos + 1) {
279                     //used all bytes so just reset the bufferpos
280                     sock->bufferpos = 0;
281                 } else {
282                     for(i = 0; i < sock->bufferpos - used; i++) {
283                         sock->buffer[i] = sock->buffer[i+used];
284                     }
285                     sock->bufferpos -= used;
286                 }
287             }
288         } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
289             if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
290                 connect_socket(sock);
291             }
292         }
293     }
294 }
295
296 void
297 putsock(struct ClientSocket *client, const char *text, ...)
298 {
299     va_list arg_list;
300     char sendBuf[MAXLEN];
301     int pos;
302     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
303     sendBuf[0] = '\0';
304     va_start(arg_list, text);
305     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
306     va_end(arg_list);
307     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
308     sendBuf[pos] = '\n';
309     sendBuf[pos+1] = '\0';
310     write_socket(client, sendBuf, pos+1);
311 }
312
313 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
314     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
315     if(sock == NULL) return NULL;
316     for (; sock; sock = sock->next) {
317         if(!flags || (sock->flags & flags) == flags)
318             return sock;
319     }
320     return NULL;
321 }
322
323 void free_sockets() {
324     if(!sockets) return;
325     struct ClientSocket *client, *next;
326     for (client = sockets->data; client; client = next) {
327         next = client->next;
328         if((client->flags & SOCKET_FLAG_CONNECTED))
329             close(client->sock);
330         if(client->queue)
331             queue_destroy(client);
332         free(client->host);
333         free(client->pass);
334         free(client);
335     }
336     free(sockets);
337     sockets = NULL;
338 }