fixed renameAccount function (merging mode)
[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 #include "main.h"
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     if(process_state.running == 0)
226         return; //just ignore the event (shutdown sequence)
227     switch(event->type) {
228     case IOEVENT_CONNECTED:
229         client->flags |= SOCKET_FLAG_CONNECTED;
230         if(client->pass && strcmp(client->pass, ""))
231             putsock(client, "PASS :%s", client->pass);
232         putsock(client, "USER %s 0 0 :%s", client->ident, client->realname);
233         putsock(client, "NICK %s", client->nick);
234         break;
235     case IOEVENT_NOTCONNECTED:
236     case IOEVENT_CLOSED:
237         close_socket(client);
238         if(client->flags & SOCKET_FLAG_RECONNECT) {
239             struct timeval timeout;
240             gettimeofday(&timeout, NULL);
241             timeout.tv_sec += SOCKET_RECONNECT_TIME;
242             client->iofd = iohandler_timer(timeout, socket_callback);
243             client->iofd->data = client;
244         }
245         break;
246     case IOEVENT_TIMEOUT: //reconnect timer
247         connect_socket(client);
248         break;
249     case IOEVENT_RECV:
250         #ifdef HAVE_THREADS
251         tid = (unsigned int) pthread_self_tid();
252         clientsocket_start_of_recv(tid);
253         #endif
254         client->traffic_in += strlen(event->data.recv_str);
255         parse_line(client, event->data.recv_str);
256         #ifdef HAVE_THREADS
257         clientsocket_end_of_recv(tid);
258         #endif
259         break;
260     default:
261         break;
262     }
263 }
264
265 void putsock(struct ClientSocket *client, const char *text, ...) {
266     va_list arg_list;
267     char sendBuf[MAXLEN];
268     int pos;
269     if (!(client && (client->flags & SOCKET_FLAG_CONNECTED))) return;
270     sendBuf[0] = '\0';
271     va_start(arg_list, text);
272     pos = vsnprintf(sendBuf, MAXLEN - 2, text, arg_list);
273     va_end(arg_list);
274     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
275     sendBuf[pos] = '\n';
276     sendBuf[pos+1] = '\0';
277     write_socket(client, sendBuf, pos+1);
278 }
279
280 struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
281     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
282     if(sock == NULL) return NULL;
283     for (; sock; sock = sock->next) {
284         if(!flags || (sock->flags & flags) == flags)
285             return sock;
286     }
287     return NULL;
288 }
289
290 void free_sockets(int close_only) {
291     if(!sockets) return;
292     struct ClientSocket *client, *next;
293     for (client = sockets->data; client; client = next) {
294         next = client->next;
295         if(close_only) {
296             if((client->flags & SOCKET_FLAG_CONNECTED))
297                 iohandler_printf(client->iofd, "QUIT :[NeonServ %s.%d] shutdown requested.\n", NEONSERV_VERSION, patchlevel);
298         } else
299             destroy_socket(client);
300     }
301     if(!close_only) {
302         free(sockets);
303         sockets = NULL;
304     }
305 }