completely rebuild WHOHander ticket management
[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
24 struct socket_list {
25     struct ClientSocket *data;
26     unsigned count;
27 };
28
29 //the magic list :P
30 static struct socket_list *sockets = NULL;
31 static char buffer[BUF_SIZ];
32
33 static void init_sockets() {
34     sockets = malloc(sizeof(*sockets));
35     if (!sockets)
36     {
37         perror("malloc() failed");
38         return;
39     }
40     sockets->data = NULL;
41     sockets->count = 0;
42 }
43
44 struct ClientSocket* create_socket(char *host, int port, char *pass, struct UserNode *user) {
45     if(sockets == NULL) init_sockets();
46     struct ClientSocket *client = malloc(sizeof(*client));
47     if (!client)
48     {
49         perror("malloc() failed");
50         return NULL;
51     }
52     client->host = strdup(host);
53     client->port = port;
54     printf("Connect: %s:%d\n", client->host, client->port);
55     client->pass = (pass == NULL ? NULL : strdup(pass));
56     client->user = user;
57     client->flags = 0;
58     client->bufferpos = 0;
59     client->traffic_in = 0;
60     client->traffic_out = 0;
61     client->connection_time = 0;
62         client->botid = 0;
63     client->clientid = 0;
64     client->queue = NULL;
65     client->whoqueue_first = NULL;
66     client->whoqueue_last = NULL;
67     client->next = sockets->data;
68     sockets->data = client;
69     return client;
70 }
71
72 #ifdef WIN32
73
74 int connect_socket(struct ClientSocket *client) {
75     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
76     struct hostent *host;
77     struct sockaddr_in addr;
78     int sock;
79     addr.sin_addr.s_addr = inet_addr(client->host);
80     if (addr.sin_addr.s_addr == INADDR_NONE) {
81         host = gethostbyname(client->host);
82         if(!host) {
83             return SOCKET_ERROR;
84         }
85         memcpy(&(addr.sin_addr), host->h_addr_list[0], 4);
86     }
87     sock = socket(PF_INET, SOCK_STREAM, 0);
88     if (sock == -1)
89     {
90         perror("socket() failed");
91         return 0;
92     }
93
94     addr.sin_port = htons(client->port);
95     addr.sin_family = AF_INET;
96
97     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
98     {
99         perror("connect() failed");
100         return 0;
101     }
102
103     client->sock = sock;
104     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
105     client->connection_time = time(0);
106
107     //send the IRC Headers
108     char sendBuf[512];
109     int len;
110
111     if(client->pass && strcmp(client->pass, "")) {
112         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
113         write_socket(client, sendBuf, len);
114     }
115     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
116     write_socket(client, sendBuf, len);
117     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
118     write_socket(client, sendBuf, len);
119
120     return 1;
121 }
122
123 #else
124
125 int connect_socket(struct ClientSocket *client) {
126     if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
127     struct hostent *host;
128     struct sockaddr_in addr;
129     int sock;
130     if (!inet_aton(client->host, &addr.sin_addr))
131     {
132         host = gethostbyname(client->host);
133         if (!host)
134         {
135             perror("gethostbyname() failed");
136             return 0;
137         }
138         addr.sin_addr = *(struct in_addr*)host->h_addr;
139     }
140     sock = socket(PF_INET, SOCK_STREAM, 0);
141     if (sock == -1)
142     {
143         perror("socket() failed");
144         return 0;
145     }
146
147     addr.sin_port = htons(client->port);
148     addr.sin_family = AF_INET;
149
150     if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
151     {
152         perror("connect() failed");
153         return 0;
154     }
155
156     client->sock = sock;
157     client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
158     client->connection_time = time(0);
159
160     //send the IRC Headers
161     char sendBuf[512];
162     int len;
163
164     if(client->pass && strcmp(client->pass, "")) {
165         len = sprintf(sendBuf, "PASS :%s\n", client->pass);
166         write_socket(client, sendBuf, len);
167     }
168     len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->user->ident, client->user->realname);
169     write_socket(client, sendBuf, len);
170     len = sprintf(sendBuf, "NICK %s\n", client->user->nick);
171     write_socket(client, sendBuf, len);
172
173     return 1;
174 }
175
176 #endif
177
178 int close_socket(struct ClientSocket *client) {
179     if(client == NULL) return 0;
180     if((client->flags & SOCKET_FLAG_CONNECTED))
181         close(client->sock);
182     struct ClientSocket *sock, *last_sock = NULL;
183     for (sock = sockets->data; sock; sock = sock->next) {
184         if(sock == client) {
185             if(last_sock)
186                 last_sock->next = sock->next;
187             else
188                 sockets->data = sock->next;
189             sockets->count--;
190         } else
191             last_sock = sock;
192     }
193     if(client->queue)
194         queue_destroy(client);
195     if(client->whoqueue_first)
196         clear_whoqueue(client);
197     free(client->host);
198     free(client->pass);
199     free(client);
200     return 1;
201 }
202
203 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
204     printf("[send %d] %s", len, msg);
205     #ifdef WIN32
206     send(client->sock, msg, len, 0);
207     #else
208     write(client->sock, msg, len);
209     #endif
210     client->traffic_out += len;
211     return 1;
212 }
213
214 int write_socket(struct ClientSocket *client, char* msg, int len) {
215     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
216     if(client->flags & SOCKET_FLAG_USE_QUEUE)
217         return queue_add(client, msg, len);
218     else
219         return write_socket_force(client, msg, len);
220 }
221
222 void socket_loop(int timeout_seconds) {
223     if(sockets == NULL) return;
224     fd_set fds;
225     struct timeval timeout;
226     struct ClientSocket *sock;
227     int ret = 0, bytes, i;
228     
229     FD_ZERO(&fds);
230     for (sock = sockets->data; sock; sock = sock->next) {
231         if(!(sock->flags & SOCKET_FLAG_CONNECTED)) continue; //skip disconnected sockets
232         FD_SET(sock->sock, &fds);
233         if(sock->sock > ret)
234             ret = sock->sock;
235     }
236     timeout.tv_sec = timeout_seconds;
237     timeout.tv_usec = 0;
238     ret = select(ret + 1, &fds, NULL, NULL, &timeout);
239     if(ret == 0) return;
240     for (sock = sockets->data; sock; sock = sock->next) {
241         if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) {
242             if(sock->bufferpos != 0) {
243                 #ifdef WIN32
244                 bytes = recv(sock->sock, buffer, sizeof(buffer), 0);
245                 #else
246                 bytes = read(sock->sock, buffer, sizeof(buffer));
247                 #endif
248                 if(bytes > 0) {
249                     for(i = 0; i < bytes; i++) {
250                         if(sock->bufferpos + i == BUF_SIZ*2) break; //buffer overflow
251                         sock->buffer[sock->bufferpos + i] = buffer[i];
252                     }
253                     sock->bufferpos += i;
254                 }
255             } else {
256                 #ifdef WIN32
257                 bytes = recv(sock->sock, sock->buffer, sizeof(sock->buffer), 0);
258                 #else
259                 bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer));
260                 #endif
261                 if(bytes > 0)
262                     sock->bufferpos = bytes;
263             }
264             if(bytes <= 0) {
265                 //error
266                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
267                 bot_disconnect(sock);
268                 if(sock->queue)
269                     queue_destroy(sock);
270             } else {
271                 sock->traffic_in += bytes;
272                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
273                 if(used == sock->bufferpos + 1) {
274                     //used all bytes so just reset the bufferpos
275                     sock->bufferpos = 0;
276                 } else {
277                     for(i = 0; i < sock->bufferpos - used; i++) {
278                         sock->buffer[i] = sock->buffer[i+used];
279                     }
280                     sock->bufferpos -= used;
281                 }
282             }
283         } else if(!(sock->flags & SOCKET_FLAG_CONNECTED) && (sock->flags & SOCKET_FLAG_RECONNECT)) {
284             if(time(0) - sock->connection_time >= SOCKET_RECONNECT_TIME) {
285                 connect_socket(sock);
286             }
287         }
288     }
289 }
290
291 void
292 putsock(struct ClientSocket *client, const char *text, ...)
293 {
294     va_list arg_list;
295     char sendBuf[MAXLEN];
296     int pos;
297     if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
298     sendBuf[0] = '\0';
299     va_start(arg_list, text);
300     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
301     va_end(arg_list);
302     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
303     sendBuf[pos] = '\n';
304     sendBuf[pos+1] = '\0';
305     write_socket(client, sendBuf, pos+1);
306 }
307
308 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
309     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
310     if(sock == NULL) return NULL;
311     for (; sock; sock = sock->next) {
312         if(!flags || (sock->flags & flags) == flags)
313             return sock;
314     }
315     return NULL;
316 }
317
318 void free_sockets() {
319     if(!sockets) return;
320     struct ClientSocket *client, *next;
321     for (client = sockets->data; client; client = next) {
322         next = client->next;
323         if((client->flags & SOCKET_FLAG_CONNECTED))
324             close(client->sock);
325         if(client->queue)
326             queue_destroy(client);
327         free(client->host);
328         free(client->pass);
329         free(client);
330     }
331     free(sockets);
332     sockets = NULL;
333 }