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