1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
* Copyright (c) 2003 by FlashCode <flashcode@flashtux.org>
* Bounga <bounga@altern.org>
* Xahlexx <xahlexx@tuxisland.org>
* See README for License detail.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* irc-server.c: (dis)connection and communication with irc server */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "../weechat.h"
#include "irc.h"
#include "../gui/gui.h"
t_irc_server *irc_servers = NULL;
t_irc_server *last_irc_server = NULL;
t_irc_server *current_irc_server = NULL;
t_irc_message *recv_msgq, *msgq_last_msg;
/* buffer containing beginning of message if not ending with \r\n */
char *unterminated_message = NULL;
/*
* server_init: init server struct with default values
*/
void
server_init (t_irc_server *server)
{
server->name = NULL;
server->autoconnect = 0;
server->address = NULL;
server->port = -1;
server->password = NULL;
server->nick1 = NULL;
server->nick2 = NULL;
server->nick3 = NULL;
server->username = NULL;
server->realname = NULL;
server->nick = NULL;
server->is_connected = 0;
server->sock4 = -1;
server->is_away = 0;
server->server_read = -1;
server->server_write = -1;
server->window = NULL;
server->channels = NULL;
server->last_channel = NULL;
}
/*
* server_alloc: allocate a new server and add it to the servers queue
*/
t_irc_server *
server_alloc ()
{
t_irc_server *new_server;
#if DEBUG >= 1
log_printf ("allocating new server\n");
#endif
/* alloc memory for new server */
if ((new_server = (t_irc_server *) malloc (sizeof (t_irc_server))) == NULL)
{
fprintf (stderr, _("%s cannot allocate new server"), WEECHAT_ERROR);
return NULL;
}
/* initialize new server */
server_init (new_server);
/* add new server to queue */
new_server->prev_server = last_irc_server;
new_server->next_server = NULL;
if (irc_servers)
last_irc_server->next_server = new_server;
else
irc_servers = new_server;
last_irc_server = new_server;
/* all is ok, return address of new server */
return new_server;
}
/*
* server_create_window: create windows for a server
*/
void
server_create_window (t_irc_server *server)
{
if (!SERVER(gui_windows))
{
server->window = gui_windows;
SERVER(gui_windows) = server;
}
else
gui_window_new (server, NULL);
}
/*
* server_destroy: free server data (not struct himself)
*/
void
server_destroy (t_irc_server *server)
{
/* free data */
if (server->name)
free (server->name);
if (server->address)
free (server->address);
if (server->password)
free (server->password);
if (server->nick1)
free (server->nick1);
if (server->nick2)
free (server->nick2);
if (server->nick3)
free (server->nick3);
if (server->username)
free (server->username);
if (server->realname)
free (server->realname);
if (server->nick)
free (server->nick);
if (server->channels)
channel_free_all (server);
}
/*
* server_free: free a server and remove it from servers queue
*/
void
server_free (t_irc_server *server)
{
t_irc_server *new_irc_servers;
/* remove server from queue */
if (server->prev_server)
{
(server->prev_server)->next_server = server->next_server;
new_irc_servers = irc_servers;
}
else
new_irc_servers = server->next_server;
if (server->next_server)
(server->next_server)->prev_server = server->prev_server;
server_destroy (server);
free (server);
irc_servers = new_irc_servers;
}
/*
* server_free_all: free all allocated servers
*/
void
server_free_all ()
{
/* for each server in memory, remove it */
while (irc_servers)
server_free (irc_servers);
}
/*
* server_new: creates a new server, and initialize it
*/
t_irc_server *
server_new (char *name, int autoconnect, char *address, int port,
char *password, char *nick1, char *nick2, char *nick3,
char *username, char *realname)
{
t_irc_server *new_server;
if (!name || !address || (port < 0))
return NULL;
#if DEBUG >= 1
log_printf ("creating new server (name:%s, address:%s, port:%d, pwd:%s, "
"nick1:%s, nick2:%s, nick3:%s, username:%s, realname:%s)\n",
name, address, port, (password) ? password : "",
(nick1) ? nick1 : "", (nick2) ? nick2 : "", (nick3) ? nick3 : "",
(username) ? username : "", (realname) ? realname : "");
#endif
if ((new_server = server_alloc ()))
{
new_server->name = strdup (name);
new_server->autoconnect = autoconnect;
new_server->address = strdup (address);
new_server->port = port;
new_server->password = (password) ? strdup (password) : strdup ("");
new_server->nick1 = (nick1) ? strdup (nick1) : strdup ("weechat_user");
new_server->nick2 = (nick2) ? strdup (nick2) : strdup ("weechat2");
new_server->nick3 = (nick3) ? strdup (nick3) : strdup ("weechat3");
new_server->username =
(username) ? strdup (username) : strdup ("weechat");
new_server->realname =
(realname) ? strdup (realname) : strdup ("realname");
new_server->nick = strdup (new_server->nick1);
}
else
return NULL;
return new_server;
}
/*
* server_send: send data to irc server
*/
int
server_send (t_irc_server * server, char *buffer, int size_buf)
{
if (!server)
return -1;
return send (server->sock4, buffer, size_buf, 0);
}
/*
* server_sendf: send formatted data to irc server
*/
int
server_sendf (t_irc_server * server, char *fmt, ...)
{
va_list args;
static char buffer[1024];
int size_buf;
if (!server)
return -1;
va_start (args, fmt);
size_buf = vsnprintf (buffer, sizeof (buffer) - 1, fmt, args);
va_end (args);
if ((size_buf == 0) || (strcmp (buffer, "\r\n") == 0))
return 0;
buffer[sizeof (buffer) - 1] = '\0';
if ((size_buf < 0) || (size_buf > (int) (sizeof (buffer) - 1)))
size_buf = strlen (buffer);
buffer[size_buf - 2] = '\0';
#if DEBUG >= 2
gui_printf (server->window, "[DEBUG] Sending to server >>> %s\n", buffer);
#endif
buffer[size_buf - 2] = '\r';
return server_send (server, buffer, size_buf);
}
/*
* server_msgq_add_msg: add a message to received messages queue (at the end)
*/
void
server_msgq_add_msg (t_irc_server * server, char *msg)
{
t_irc_message *message;
message = (t_irc_message *) malloc (sizeof (t_irc_message));
message->server = server;
if (unterminated_message)
{
message->data = (char *) malloc (strlen (unterminated_message) +
strlen (msg) + 1);
strcpy (message->data, unterminated_message);
strcat (message->data, msg);
free (unterminated_message);
unterminated_message = NULL;
}
else
message->data = strdup (msg);
message->next_message = NULL;
if (msgq_last_msg)
{
msgq_last_msg->next_message = message;
msgq_last_msg = message;
}
else
{
recv_msgq = message;
msgq_last_msg = message;
}
}
/*
* server_msgq_add_buffer: explode received buffer, creating queued messages
*/
void
server_msgq_add_buffer (t_irc_server * server, char *buffer)
{
char *pos;
while (buffer[0])
{
pos = strstr (buffer, "\r\n");
if (pos)
{
pos[0] = '\0';
server_msgq_add_msg (server, buffer);
buffer = pos + 2;
}
else
{
pos = strchr (buffer, '\0');
if (pos)
{
unterminated_message =
(char *) realloc (unterminated_message,
strlen (buffer) + 1);
strcpy (unterminated_message, buffer);
return;
}
gui_printf (server->window,
_("%s unable to explode received buffer\n"),
WEECHAT_ERROR);
}
}
}
/*
* server_msgq_flush: flush message queue
*/
void
server_msgq_flush ()
{
t_irc_message *next;
/*char **argv;
int argc;*/
char *ptr_data, *pos, *pos2;
char *host, *command, *args;
/* TODO: optimize this function, parse only a few messages (for low CPU time!) */
while (recv_msgq)
{
#if DEBUG >= 2
gui_printf (gui_current_window, "[DEBUG] %s\n", recv_msgq->data);
#endif
ptr_data = recv_msgq->data;
while (ptr_data[0] == ' ')
ptr_data++;
if (ptr_data)
{
#if DEBUG >= 2
gui_printf (NULL, "[DEBUG] data received from server: %s\n", ptr_data);
#endif
host = NULL;
command = NULL;
args = ptr_data;
if (ptr_data[0] == ':')
{
pos = strchr(ptr_data, ' ');
pos[0] = '\0';
host = ptr_data+1;
pos++;
}
else
pos = ptr_data;
if (pos != NULL)
{
while (pos[0] == ' ')
pos++;
pos2 = strchr(pos, ' ');
if (pos2 != NULL)
{
pos2[0] = '\0';
command = strdup(pos);
pos2++;
while (pos2[0] == ' ')
pos2++;
args = (pos2[0] == ':') ? pos2+1 : pos2;
}
}
switch (irc_recv_command (recv_msgq->server, host, command, args))
{
case -1:
gui_printf (recv_msgq->server->window,
_("Command '%s' failed!\n"), command);
break;
case -2:
gui_printf (recv_msgq->server->window,
_("No command to execute!\n"));
break;
case -3:
gui_printf (recv_msgq->server->window,
_("Unknown command: cmd=%s, args=%s\n"),
command, args);
break;
}
}
free (recv_msgq->data);
next = recv_msgq->next_message;
free (recv_msgq);
recv_msgq = next;
if (recv_msgq == NULL)
msgq_last_msg = NULL;
}
}
/*
* server_recv: receive data from an irc server
*/
void
server_recv (t_irc_server *server)
{
static char buffer[4096 + 2];
int num_read;
num_read = recv (server->sock4, buffer, sizeof (buffer) - 2, 0);
if (num_read > 0)
{
buffer[num_read] = '\0';
server_msgq_add_buffer (server, buffer);
server_msgq_flush ();
}
}
/*
* server_connect: connect to an irc server
*/
int
server_connect (t_irc_server *server)
{
int set;
struct hostent *ip4_hostent;
struct sockaddr_in addr;
char *ip_address;
int error;
int server_pipe[2];
gui_printf (server->window,
_(WEECHAT_NAME ": connecting to %s:%d...\n"),
server->address, server->port);
log_printf ("connecting to server %s:%d...\n",
server->address, server->port);
server->is_connected = 0;
/* create pipe */
if (pipe (server_pipe) < 0)
{
gui_printf (server->window,
_("%s cannot create pipe\n"), WEECHAT_ERROR);
server_free (server);
return 0;
}
server->server_read = server_pipe[0];
server->server_write = server_pipe[1];
/* create socket and set options */
server->sock4 = socket (AF_INET, SOCK_STREAM, 0);
set = 1;
if (setsockopt
(server->sock4, SOL_SOCKET, SO_REUSEADDR, (char *) &set,
sizeof (set)) == -1)
gui_printf (server->window,
_("%s cannot set socket option 'SO_REUSEADDR'\n"),
WEECHAT_ERROR);
set = 1;
if (setsockopt
(server->sock4, SOL_SOCKET, SO_KEEPALIVE, (char *) &set,
sizeof (set)) == -1)
gui_printf (server->window,
_("%s cannot set socket option \"SO_KEEPALIVE\"\n"),
WEECHAT_ERROR);
/* bind to hostname */
ip4_hostent = gethostbyname (server->address);
if (!ip4_hostent)
{
gui_printf (server->window,
_("%s address \"%s\" not found\n"),
WEECHAT_ERROR, server->address);
close (server->server_read);
close (server->server_write);
close (server->sock4);
server->sock4 = -1;
return 0;
}
memset (&addr, 0, sizeof (addr));
memcpy (&addr.sin_addr, ip4_hostent->h_addr, ip4_hostent->h_length);
addr.sin_port = htons (server->port);
addr.sin_family = AF_INET;
/*error = bind(server->sock4, (struct sockaddr *)(&addr), sizeof(addr));
if (error != 0)
{
gui_printf (server->window,
WEECHAT_ERORR "server_connect: can't bind to hostname\n");
return 0;
} */
ip_address = inet_ntoa (addr.sin_addr);
if (!ip_address)
{
gui_printf (server->window,
_("%s IP address not found\n"), WEECHAT_ERROR);
close (server->server_read);
close (server->server_write);
close (server->sock4);
server->sock4 = -1;
return 0;
}
/* connection to server */
gui_printf (server->window,
_(WEECHAT_NAME ": server IP is: %s\n"), ip_address);
error = connect (server->sock4, (struct sockaddr *) &addr, sizeof (addr));
if (error != 0)
{
gui_printf (server->window,
_("%s cannot connect to irc server\n"), WEECHAT_ERROR);
close (server->server_read);
close (server->server_write);
close (server->sock4);
server->sock4 = -1;
return 0;
}
current_irc_server = server;
return 1;
}
/*
* server_disconnect: disconnect from an irc server
*/
void
server_disconnect (t_irc_server *server)
{
t_irc_channel *ptr_channel;
if (server->is_connected)
{
/* write disconnection message on each channel/private window */
for (ptr_channel = server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
irc_display_prefix (ptr_channel->window, PREFIX_INFO);
gui_printf (ptr_channel->window, "Disconnected from server!\n");
}
/* close communication with server */
close (server->server_read);
close (server->server_write);
close (server->sock4);
server->is_connected = 0;
server->sock4 = -1;
}
}
/*
* server_disconnect_all: disconnect from all irc servers
*/
void
server_disconnect_all ()
{
t_irc_server *ptr_server;
for (ptr_server = irc_servers; ptr_server; ptr_server = ptr_server->next_server)
server_disconnect (ptr_server);
}
/*
* server_search: return pointer on a server with a name
*/
t_irc_server *
server_search (char *servername)
{
t_irc_server *ptr_server;
for (ptr_server = irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (strcmp (ptr_server->name, servername) == 0)
return ptr_server;
}
return NULL;
}
/*
* server_get_number_connected: returns number of connected server
*/
int
server_get_number_connected ()
{
t_irc_server *ptr_server;
int number;
number = 0;
for (ptr_server = irc_servers; ptr_server; ptr_server = ptr_server->next_server)
{
if (ptr_server->is_connected)
number++;
}
return number;
}
/*
* server_name_already_exists: return 1 if server name already exists
* otherwise return 0
*/
int
server_name_already_exists (char *name)
{
t_irc_server *ptr_server;
for (ptr_server = irc_servers; ptr_server; ptr_server = ptr_server->next_server)
{
if (strcmp (ptr_server->name, name) == 0)
return 1;
}
return 0;
}
|