added free-client event
[NeonServV5.git] / src / ClientSocket.c
1 /* ClientSocket.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  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 #include "ConfigParser.h"
25 #include "version.h"
26 #include "IOHandler.h"
27
28 struct socket_list {
29     struct ClientSocket *data;
30     unsigned count;
31 };
32
33 #ifdef HAVE_THREADS
34 static pthread_mutex_t synchronized;
35 static pthread_mutex_t synchronized_recv;
36
37 struct ParseOrder {
38     unsigned int tid;
39     struct ParseOrder *next;
40 };
41 struct ParseOrder *parse_order = NULL;
42 #endif
43
44 //the magic list :P
45 static struct socket_list *sockets = NULL;
46
47 static IOHANDLER_CALLBACK(socket_callback);
48
49 void init_sockets() {
50     THREAD_MUTEX_INIT(synchronized);
51     THREAD_MUTEX_INIT(synchronized_recv);
52     
53     sockets = malloc(sizeof(*sockets));
54     if (!sockets)
55     {
56         perror("malloc() failed");
57         return;
58     }
59     sockets->data = NULL;
60     sockets->count = 0;
61 }
62
63 struct ClientSocket* create_socket(char *host, int port, char *bindto, char *pass, char *nick, char *ident, char *realname) {
64     if(!sockets)
65         init_sockets();
66     struct ClientSocket *client = malloc(sizeof(*client));
67     if (!client) {
68         return NULL;
69     }
70     client->iofd = NULL;
71     client->host = strdup(host);
72     client->port = port;
73     client->bind = (bindto ? strdup(bindto) : NULL);
74     client->pass = (pass == NULL ? NULL : strdup(pass));
75     client->nick = strdup(nick);
76     client->ident = strdup(ident);
77     client->realname = strdup(realname);
78     client->user = NULL;
79     client->network_name = NULL;
80     client->flags = 0;
81     client->traffic_in = 0;
82     client->traffic_out = 0;
83     client->connection_time = 0;
84         client->botid = 0;
85     client->clientid = 0;
86     client->queue = NULL;
87     client->whoqueue_first = NULL;
88     client->whoqueue_last = NULL;
89     client->handleinfo_first = NULL;
90     client->handleinfo_last = NULL;
91     SYNCHRONIZE(synchronized);
92     client->next = sockets->data;
93     sockets->data = client;
94     DESYNCHRONIZE(synchronized);
95     return client;
96 }
97
98 void connect_socket(struct ClientSocket *client) {
99     client->iofd = iohandler_connect(client->host, client->port, ((client->flags & SOCKET_FLAG_SSL) ? 1 : 0), client->bind, socket_callback);
100     client->iofd->data = client;
101 }
102
103 int close_socket(struct ClientSocket *client) {
104     if(client == NULL) return 0;
105     if((client->flags & SOCKET_FLAG_CONNECTED)) {
106         iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] disconnect requested.\n", NEONSERV_VERSION, patchlevel);
107         
108         bot_disconnect(client);
109         
110         iohandler_close(client->iofd);
111         client->iofd = NULL;
112     }
113     client->flags &= ~(SOCKET_FLAG_READY | SOCKET_FLAG_CONNECTED);
114     if(client->queue)
115         queue_destroy(client);
116     if(client->whoqueue_first)
117         clear_whoqueue(client);
118     if(client->handleinfo_first)
119         clear_handleinfoqueue(client);
120     return 1;
121 }
122
123 int destroy_socket(struct ClientSocket *client) {
124     if(client == NULL) return 0;
125     close_socket(client);
126     SYNCHRONIZE(synchronized);
127     struct ClientSocket *sock, *last_sock = NULL;
128     for (sock = sockets->data; sock; sock = sock->next) {
129         if(sock == client) {
130             if(last_sock)
131                 last_sock->next = sock->next;
132             else
133                 sockets->data = sock->next;
134             sockets->count--;
135             break;
136         } else
137             last_sock = sock;
138     }
139     event_freeclient(client);
140     free(client->host);
141     if(client->bind)
142         free(client->bind);
143     if(client->pass)
144         free(client->pass);
145     if(client->network_name)
146         free(client->network_name);
147     if(client->iofd) //reconnect timer?
148         iohandler_close(client->iofd);
149     free(client);
150     DESYNCHRONIZE(synchronized);
151     return 1;
152 }
153
154 int write_socket_force(struct ClientSocket *client, char* msg, int len) {
155     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
156     SYNCHRONIZE(synchronized);
157     #ifdef HAVE_THREADS
158     putlog(LOGLEVEL_RAW, "[%d send %d] %s", getCurrentThreadID(), len, msg);
159     #else
160     putlog(LOGLEVEL_RAW, "[send %d] %s", len, msg);
161     #endif
162         iohandler_send(client->iofd, msg, len);
163     client->traffic_out += len;
164     DESYNCHRONIZE(synchronized);
165     return 1;
166 }
167
168 int write_socket(struct ClientSocket *client, char* msg, int len) {
169     if(!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return 0;
170     if(client->flags & SOCKET_FLAG_USE_QUEUE)
171         return queue_add(client, msg, len);
172     else
173         return write_socket_force(client, msg, len);
174 }
175
176 #if HAVE_THREADS
177 static void clientsocket_start_of_recv(unsigned int tid) {
178     SYNCHRONIZE(whohandler_sync);
179     struct ParseOrder *entry, *last;
180     for(last = parse_order; last; last = last->next) {
181         if(last->next == NULL)
182             break;
183     }
184     entry = malloc(sizeof(*entry));
185     entry->tid = tid;
186     entry->next = NULL;
187     if(last)
188         last->next = entry;
189     else
190         parse_order = entry;
191     DESYNCHRONIZE(whohandler_sync);
192 }
193
194 static void clientsocket_end_of_recv(unsigned int tid) {
195     SYNCHRONIZE(whohandler_sync);
196     struct ParseOrder *entry, *last = NULL;
197     for(entry = parse_order; entry; entry = entry->next) {
198         if(entry->tid == tid) {
199             if(last)
200                 last->next = entry->next;
201             else
202                 parse_order = entry->next;
203             free(entry);
204             break;
205         } else
206             last = entry;
207     }
208     DESYNCHRONIZE(whohandler_sync);
209 }
210
211 int clientsocket_parseorder_top(unsigned int tid) {
212     if(parse_order && parse_order->tid == tid)
213         return 1;
214     else
215         return 0;
216 }
217 #endif
218
219 static IOHANDLER_CALLBACK(socket_callback) {
220     struct ClientSocket *client = event->iofd->data;
221     #ifdef HAVE_THREADS
222     unsigned int tid;
223     #endif
224     switch(event->type) {
225     case IOEVENT_CONNECTED:
226         client->flags |= SOCKET_FLAG_CONNECTED;
227         if(client->pass && strcmp(client->pass, ""))
228             iohandler_printf(event->iofd, "PASS :%s", client->pass);
229         iohandler_printf(event->iofd, "USER %s 0 0 :%s", client->ident, client->realname);
230         iohandler_printf(event->iofd, "NICK %s", client->nick);
231         break;
232     case IOEVENT_NOTCONNECTED:
233     case IOEVENT_CLOSED:
234         close_socket(client);
235         if(client->flags & SOCKET_FLAG_RECONNECT) {
236             struct timeval timeout;
237             gettimeofday(&timeout, NULL);
238             timeout.tv_sec += SOCKET_RECONNECT_TIME;
239             client->iofd = iohandler_timer(timeout, socket_callback);
240             client->iofd->data = client;
241         }
242         break;
243     case IOEVENT_TIMEOUT: //reconnect timer
244         connect_socket(client);
245         break;
246     case IOEVENT_RECV:
247         #ifdef HAVE_THREADS
248         tid = (unsigned int) pthread_self_tid();
249         clientsocket_start_of_recv(tid);
250         #endif
251         client->traffic_in += strlen(event->data.recv_str);
252         parse_line(client, event->data.recv_str);
253         #ifdef HAVE_THREADS
254         clientsocket_end_of_recv(tid);
255         #endif
256         break;
257     default:
258         break;
259     }
260 }
261
262 void putsock(struct ClientSocket *client, const char *text, ...) {
263     va_list arg_list;
264     char sendBuf[MAXLEN];
265     int pos;
266     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
267     sendBuf[0] = '\0';
268     va_start(arg_list, text);
269     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
270     va_end(arg_list);
271     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
272     sendBuf[pos] = '\n';
273     sendBuf[pos+1] = '\0';
274     write_socket(client, sendBuf, pos+1);
275 }
276
277 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
278     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
279     if(sock == NULL) return NULL;
280     for (; sock; sock = sock->next) {
281         if(!flags || (sock->flags & flags) == flags)
282             return sock;
283     }
284     return NULL;
285 }
286
287 void free_sockets() {
288     if(!sockets) return;
289     struct ClientSocket *client, *next;
290     for (client = sockets->data; client; client = next) {
291         next = client->next;
292         destroy_socket(client);
293     }
294     free(sockets);
295     sockets = NULL;
296 }