/
irc.c
2309 lines (1906 loc) · 69.3 KB
/
irc.c
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* (C) Copyright 2004-2005 Michal 'GiM' Spadlinski <gim at skrzynka dot pl>
* Jakub 'darkjames' Zawadzki <darkjames@darkjames.ath.cx>
* Wies³aw Ochmiñski <wiechu@wiechu.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "ekg2.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef NO_POSIX_SYSTEM
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#endif
#include <sys/types.h>
#ifndef NO_POSIX_SYSTEM
#include <sys/socket.h>
#endif
#include <sys/stat.h>
#ifndef NO_POSIX_SYSTEM
#include <netdb.h>
#endif
#include <sys/time.h>
#ifndef NO_POSIX_SYSTEM
#include <sys/utsname.h>
#include <pwd.h>
#endif
#ifdef __sun
/* Solaris, thanks to Beeth */
#include <sys/filio.h>
#endif
#include <ekg/net.h>
#include "irc.h"
#include "people.h"
#include "input.h"
#include "autoacts.h"
#include "IRCVERSION.h"
#define DEFPORT 6667
#define DEFPARTMSG "EKG2 bejbi! http://ekg2.org/"
#define DEFQUITMSG "EKG2 - It's better than sex!"
#define DEFKICKMSG "EKG2 - Y0U 57iNK2 50 MUCH!"
#define SGPARTMSG(x) session_get(x, "PART_MSG")
#define SGQUITMSG(x) session_get(x, "QUIT_MSG")
#define SGKICKMSG(x) session_get(x, "KICK_MSG")
#define PARTMSG(x,r) (r?r: SGPARTMSG(x)?SGPARTMSG(x):DEFPARTMSG)
#define QUITMSG(x) (SGQUITMSG(x)?SGQUITMSG(x):DEFQUITMSG)
#define KICKMSG(x,r) (r?r: SGKICKMSG(x)?SGKICKMSG(x):DEFKICKMSG)
/* ************************ KNOWN BUGS ***********************
* OTHER LESS IMPORTANT BUGS
* -> somewhere with altnick sending
* G->dj: still not as I would like it to be
* !BUGS (?) TODO->check
* -> buggy auto_find. if smb type smth on the channel.
* * 10:58:27 ::: Nieprawidowe parametry. Sprobuj help find *
*************************************************************
*/
/* *************************** TODO **************************
*
* -> split mode.
* -> disconnection detection
* why not just sending simple PING to SERVER and deal with it's
* pong reply...
* PING konstantynopolitanczykiewikowna lublin.irc.pl
* :prefix PONG lublin.irc.pl konstantynopolitanczykiewikowna
* *************************************************************
*/
/*
* IDEAS:
* -> maybe some params in /connect command ?
* for example if we have in /session server krakow.irc.pl,poznan.irc.pl
* and we want connect to poznan.irc.pl (150.254.64.64) nor krakow... we type
* /connect poznan.irc.pl
* /connect #2
* /connect 150.254.64.64
* next, if we want to connect instead of default 6667 port to i.e 6665 we type
* /connect :6665
* or we can use both.
* /connect poznan.irc.pl:6665 || /connect 150.254.64.64 6665.
*/
/* *
* ======================================== STARTUP AND STANDARD FUNCS - *
* */
static int irc_theme_init();
static COMMAND(irc_command_disconnect);
static int irc_really_connect(session_t *session, gboolean quiet);
static char *irc_getchan_int(session_t *s, const char *name, int checkchan);
static char *irc_getchan(session_t *s, const char **params, const char *name,
char ***v, int pr, int checkchan);
static char *irc_config_default_access_groups;
int irc_config_allow_fake_contacts = 0;
int irc_config_clean_channel_name;
const gchar fillchars[] = "\xC2\xA0";
const gint fillchars_len = 2;
PLUGIN_DEFINE(irc, PLUGIN_PROTOCOL, irc_theme_init);
/**
* irc_session_init()
*
* Handler for: <i>SESSION_ADDED</i><br>
* Init private session struct irc_private_t if @a session is irc one.<br>
* Read saved userlist with userlist_read()
*
* @param ap 1st param: <i>(char *) </i><b>session</b> - uid of session
* @param data NULL
*
* @return 0 if @a session is irc one, and we init memory<br>
* 1 if we don't found such session, or it wasn't irc session <b>[most probable]</b>, or we already init memory.
*/
static QUERY(irc_session_init) {
char *session = *(va_arg(ap, char**));
session_t *s = session_find(session);
irc_private_t *j;
if (!s || s->priv || (s->plugin != &irc_plugin))
return 1;
userlist_read(s);
ekg_recode_utf8_inc();
j = xmalloc(sizeof(irc_private_t));
j->nick_modes = j->nick_signs = NULL;
j->conv = NULL;
s->priv = j;
return 0;
}
static LIST_FREE_ITEM(list_irc_awaylog_free, irc_awaylog_t *) {
xfree(data->channame);
xfree(data->uid);
xfree(data->msg);
xfree(data);
}
/**
* irc_session_deinit()
*
* Handler for: <i>SESSION_REMOVED</i><br>
* Free memory allocated by irc_private_t if @a session is irc one.<br>
* Save userlist with userlist_write()
*
* @param ap 1st param: <i>(char *) </i><b>session</b> - uid of session
* @param data NULL
*
* @todo Check if userlist_write() here is good.
* @todo
*
* @return 0 if @a session is irc one, and memory allocated where xfree()'d.<br>
* 1 if not such session, or it wasn't irc session <b>[most probable]</b>, or we already free memory.
*/
static QUERY(irc_session_deinit) {
char *session = *(va_arg(ap, char**));
session_t *s = session_find(session);
irc_private_t *j;
int i;
if (!s || !(j = s->priv) || (s->plugin != &irc_plugin))
return 1;
userlist_write(s);
config_commit();
s->priv = NULL;
xfree(j->host_ident);
xfree(j->nick);
g_free(j->conv);
g_strfreev(j->auto_guess_encoding);
/* XXX, hilights list_t */
/* NOTE: j->awaylog shouldn't exists here */
LIST_DESTROY(j->awaylog, list_irc_awaylog_free);
irc_free_people(s, j);
for (i = 0; i<SERVOPTS; i++)
xfree(j->sopt[i]);
xfree(j->nick_modes);
xfree(j);
return 0;
}
static char *irc_make_banmask(session_t *session, const char *nick, const char *ident, const char *hostname) {
/*
* 1 (Nick) - nick!*@*
* 2 (User) - *!*ident@*
* 4 (Host) - *!*@host.*
* 4 (IP) - *!*@*.168.11.11 - buggy, it bans @*.11
* 8 (Domain) - *!*@*.domain.net
* 8 (IP) - *!*@192.168.11.*
*/
char *host = xstrdup(hostname);
const char *tmp[4];
char *temp = NULL;
int family = 0;
char ind = '.';
int bantype = session_int_get(session, "ban_type");
#ifdef HAVE_INET_PTON
char buf[33];
if (xstrchr(host, ':')) {
/* to protect againt iwil var in ircd.conf (ircd-hybrid)
* dot_in_ip6_addr = yes;
*/
if (host[xstrlen(host)-1] == '.')
host[xstrlen(host)-1] = 0;
if (inet_pton(AF_INET6, host, &buf) > 0) {
family = AF_INET6;
ind = ':';
}
}
else if (inet_pton(AF_INET, host, &buf) > 0)
family = AF_INET;
#else
/* TODO */
print("generic_error", "It seems you don't have inet_pton(). The current version of irc_make_banmask won't work without this function. If you want to get it work faster, contact developers ;>");
#endif
if (host && !family && (temp=xstrchr(host, ind)))
*temp = '\0';
if (host && family && (temp=xstrrchr(host, ind)))
*temp = '\0';
if (bantype > 15) bantype = 10;
memset(tmp, 0, sizeof(tmp));
#define getit(x) tmp[x]?tmp[x]:"*"
if (bantype & 1) tmp[0] = nick;
if (bantype & 2 && (ident[0] != '~' || session_int_get(session, "dont_ban_user_on_noident") == 0 )) tmp[1] = ident;
if (family) {
if (bantype & 8) tmp[2] = host;
if (bantype & 4) tmp[3] = hostname ? temp?temp+1:NULL : NULL;
} else {
if (bantype & 4) tmp[2] = host;
if (bantype & 8) tmp[3] = hostname ? temp?temp+1:NULL : NULL;
}
/* temp = saprintf("%s!*%s@%s%c%s", getit(0), getit(1), getit(2), ind, getit(3)); */
temp = saprintf("%s!%s@%s%c%s", getit(0), getit(1), getit(2), ind, getit(3));
xfree(host);
return temp;
#undef getit
}
/**
* irc_print_version()
*
* handler for <i>PLUGIN_PRINT_VERSION</i> query requests<br>
* print info about this plugin [Copyright note+version]
*
* @note what the heck this can be ? ;) (c GiM)
*
* @return 0
*/
static QUERY(irc_print_version) {
print("generic", "IRC plugin by Michal 'GiM' Spadlinski, Jakub 'darkjames' Zawadzki v. "IRCVERSION);
return 0;
}
static QUERY(irc_setvar_default) {
xfree(irc_config_default_access_groups);
irc_config_default_access_groups = xstrdup("__ison");
return 0;
}
/**
* irc_validate_uid()
*
* handler for <i>PROTOCOL_VALIDATE_UID</i><br>
* checks, if @a uid is <i>proper for irc plugin</i>.
*
* @note <i>Proper for irc plugin</i> means if @a uid starts with "irc:" and uid len > 4
*
* @param ap 1st param: <i>(char *) </i><b>uid</b> - of user/session/command/whatever
* @param ap 2nd param: <i>(int) </i><b>valid</b> - place to put 1 if uid is valid for irc plugin.
* @param data NULL
*
* @return -1 if it's valid uid for irc plugin<br>
* 0 if not
*/
static QUERY(irc_validate_uid) {
char *uid = *(va_arg(ap, char **));
int *valid = va_arg(ap, int *);
if (!uid)
return 0;
if (!xstrncasecmp(uid, IRC4, 4) && uid[4]) {
(*valid)++;
return -1; /* if it's correct uid for irc we don't need to send to others... */
}
return 0;
}
out_recodes_t *irc_find_out_recode(list_t rl, char *encname) {
out_recodes_t *recode;
if (!(encname && rl))
return NULL;
for ( ; rl; rl = rl->next) {
recode = (out_recodes_t *)(rl->data);
if ( recode->name && !xstrcasecmp(recode->name, encname) )
return recode;
}
return NULL;
}
static char *irc_convert_out(irc_private_t *j, char *recipient, const char *line) {
if ((j->recoded_channels)) {
/* channel/nick recode */
char *channame = (!xstrncasecmp(recipient, IRC4, 4)) ? recipient+4 : recipient;
gchar *enc = g_datalist_get_data(&j->recoded_channels, channame);
if (enc)
return ekg_recode_to(enc, line);
}
/* default recode */
return j->conv ? ekg_recode_to(j->conv, line) : xstrdup(line);
}
static void irc_changed_recode_list(session_t *s, const char *var) {
const char *val;
irc_private_t *j;
char **list1, **list2, *nicks, *encoding;
int i,i2;
g_assert(s);
if (!(j = s->priv))
return;
g_datalist_clear(&j->recoded_channels);
if (!(val = session_get(s, var)) || !*val)
return;
/* Parse list */
// Syntax: encoding1:nick1,nick2,#chan1,nick3;encoding2:nick4,#chan5,chan6
list1 = array_make(val, ";", 0, 1, 0);
for (i=0; list1[i]; i++) {
if (!(nicks = xstrchr(list1[i], ':'))) {
debug_error("[irc] recode_list parse error: no colon. Skipped. '%s'\n", list1[i]);
continue;
}
*nicks++ = '\0';
encoding = list1[i];
if (!*nicks) {
debug_error("[irc] recode_list parse error: no nick or channel. Skipped. '%s:'\n", encoding);
continue;
}
if (!*encoding) {
debug_error("[irc] recode_list parse error: no encoding name. Skipped. ':%s'\n", nicks);
continue;
}
list2 = array_make(nicks, ",", 0, 1, 0);
for(i2=0; list2[i2]; i2++) {
if (g_datalist_get_data(&j->recoded_channels, list2[i2])) {
debug_error("[irc] recode_list. Duplicated channel/nick '%s'. Skipped.'\n", list2[i2]);
continue;
}
g_datalist_set_data_full(&j->recoded_channels, list2[i2],
g_strdup(encoding), g_free);
}
g_strfreev(list2);
}
g_strfreev(list1);
}
static void irc_changed_recode(session_t *s, const char *var) {
const char *val;
irc_private_t *j;
if (!s || !(j = s->priv))
return;
g_free(j->conv);
if (!(val = session_get(s, var)) || !*val) {
j->conv = NULL;
return;
}
j->conv = g_strdup(val);
}
static void irc_changed_auto_guess_encoding(session_t *s, const char *var) {
const char *val;
irc_private_t *j;
g_assert(s);
j = s->priv;
if (!j)
return;
if (j->auto_guess_encoding) {
g_strfreev(j->auto_guess_encoding);
j->auto_guess_encoding = NULL;
}
if (!(val = session_get(s, var)) || !*val)
return;
j->auto_guess_encoding = array_make(val, ",", 0, 1, 0);
}
/* *
* ======================================== HANDLERS ------------------- *
* */
void irc_handle_disconnect(session_t *s, const char *reason, int type)
{
/*
* EKG_DISCONNECT_NETWORK @ irc_handle_stream type == 1
* EKG_DISCONNECT_FAILURE @ irc_handle_connect when we got timeouted or connection refused or other..
* EKG_DISCONNECT_FAILURE @ irc_c_error(misc.c) when we recv ERROR message @ connecting
* EKG_DISCONNECT_FAILURE @ irc_command_connect when smth goes wrong.
* EKG_DISCONNECT_STOPPED @ irc_command_disconnect when we do /disconnect when we are not connected and we try to connect.
* EKG_DISCONNECT_USER @ irc_command_disconnect when we do /disconnect when we are connected.
*/
irc_private_t *j = irc_private(s);
char *__reason;
g_assert(j);
j->disconnecting = FALSE;
irc_free_people(s, j);
switch (type) {
case EKG_DISCONNECT_FAILURE:
break;
case EKG_DISCONNECT_STOPPED:
/* this is just in case somebody would add some servers that have given
* port disabled so answer comes very fast and plug would think
* he is 'disconnected' while in fact he would try many times to
* reconnect to given host
*/
j->autoreconnecting = 0;
/* if ,,reconnect'' timer exists we should stop doing */
if (timer_remove_session(s, "reconnect") == 0)
print("auto_reconnect_removed", session_name(s));
break;
/*
* default:
* debug("[irc_handle_disconnect] unknow || !handled type = %d %s", type, reason);
*/
}
__reason = xstrdup(format_find(reason));
if (!xstrcmp(__reason, "")) {
xfree(__reason);
__reason = xstrdup(reason);
}
protocol_disconnected_emit(s, __reason, type);
xfree(__reason);
}
static void irc_handle_line(GDataInputStream *f, gpointer data) {
session_t *s = data;
gchar *l;
l = g_data_input_stream_read_line(f, NULL, NULL, NULL);
/* XXX: get rid of that fd arg */
if (l)
irc_parse_line(s, l, -1);
}
static void irc_handle_failure(GDataInputStream *f, GError *err, gpointer data) {
session_t *s = data;
irc_private_t *j = irc_private(s);
j->send_stream = NULL; /* XXX: needed? */
if (j->disconnecting &&
g_error_matches(err, EKG_CONNECTION_ERROR, EKG_CONNECTION_ERROR_EOF))
irc_handle_disconnect(s, NULL, EKG_DISCONNECT_USER);
else
irc_handle_disconnect(s, err->message, EKG_DISCONNECT_NETWORK);
}
static void irc_handle_connect(
GSocketConnection *conn,
GInputStream *instream,
GOutputStream *outstream,
gpointer data)
{
session_t *s = data;
irc_private_t *j = irc_private(s);
j->send_stream = ekg_connection_add(
conn,
instream,
outstream,
EKG_INPUT_LINE,
irc_handle_line,
irc_handle_failure,
s);
{
const gchar *real = session_get(s, "realname");
const gchar *hostname = session_get(s, "hostname");
const gchar *pass = session_password_get(s); /* XXX: we used to strip_spaces() here?! */
if (pass && *pass)
ekg_fprintf(G_OUTPUT_STREAM(j->send_stream), "PASS %s\r\n", pass);
ekg_connection_write(j->send_stream,
"USER %s %s unused_field :%s\r\nNICK %s\r\n",
j->nick, hostname ? hostname : "12", real, j->nick);
}
}
static void irc_handle_connect_failure(GError *err, gpointer data) {
session_t *s = data;
irc_handle_disconnect(s, err->message, EKG_DISCONNECT_FAILURE);
}
/* *
* ======================================== COMMANDS ------------------- *
* */
static int irc_really_connect(session_t *session, gboolean quiet) {
irc_private_t *j = irc_private(session);
GSocketClient *s;
const int defport = session_int_get(session, "port");
const gchar *bindhost = session_get(session, "hostname");
ekg_connection_starter_t cs;
session->connecting = 1;
j->autoreconnecting = 1; /* XXX? */
printq("connecting", session_name(session));
cs = ekg_connection_starter_new(defport > 0 ? defport : DEFPORT);
ekg_connection_starter_set_servers(cs, session_get(session, "server"));
ekg_connection_starter_set_use_tls(cs, !!session_int_get(session, "use_tls"));
if (bindhost)
ekg_connection_starter_bind(cs, bindhost);
s = g_socket_client_new();
ekg_connection_starter_run(cs, s, irc_handle_connect,
irc_handle_connect_failure, session);
if (session_status_get(session) == EKG_STATUS_NA)
session_status_set(session, EKG_STATUS_AVAIL);
/* XXX: timeout */
return -1;
}
static COMMAND(irc_command_connect) {
irc_private_t *j = irc_private(session);
const char *newnick;
if (!session_get(session, "server")) {
printq("generic_error", "gdzie lecimy ziom ?! [/session server]");
return -1;
}
if (session->connecting) {
printq("during_connect", session_name(session));
return -1;
}
if (session_connected_get(session)) {
printq("already_connected", session_name(session));
return -1;
}
if ((newnick = session_get(session, "nickname"))) {
xfree(j->nick);
j->nick = xstrdup(newnick);
} else {
printq("generic_error", "gdzie lecimy ziom ?! [/session nickname]");
return -1;
}
return irc_really_connect(session, quiet);
}
static COMMAND(irc_command_disconnect) {
irc_private_t *j = irc_private(session);
const char *reason = params[0]?params[0]:QUITMSG(session);
debug_function("[irc] comm_disconnect() !!!\n");
if (!session->connecting && !session_connected_get(session) && !j->autoreconnecting) {
printq("not_connected", session_name(session));
return -1;
}
j->disconnecting = TRUE;
if (reason && session_connected_get(session))
ekg_connection_write(j->send_stream, "QUIT :%s\r\n", reason);
if (session->connecting) {
g_cancellable_cancel(j->connect_cancellable);
/* XXX: how about the 'connection processing' part? */
}
#if 0
if (session->connecting || j->autoreconnecting)
irc_handle_disconnect(session, reason, EKG_DISCONNECT_STOPPED);
else
irc_handle_disconnect(session, reason, EKG_DISCONNECT_USER);
#endif
return 0;
}
static COMMAND(irc_command_reconnect) {
if (session->connecting || session_connected_get(session))
irc_command_disconnect(name, params, session, target, quiet);
return irc_command_connect(name, params, session, target, quiet);
}
/*****************************************************************************/
static COMMAND(irc_command_msg) {
irc_private_t *j = irc_private(session);
people_chan_t *perchn = NULL;
people_t *person;
window_t *w;
int prv = 0; /* NOTICE | PRIVMSG */
char *mline[2];
char *tmpbuf;
int ischn; /* IS CHANNEL ? */
char prefix[2] = {' ', '\0'};
char *head, *coloured;
const char *frname; /* formatname */
int secure = 0;
char **rcpts;
char *uid;
if (!xstrncmp(target, IRC4, 4)) {
uid = xstrdup(target);
} else {
uid = irc_uid(target);
}
w = window_find_s(session, uid);
prv = xstrcmp(name, ("notice"));
ischn = !!xstrchr(SOP(_005_CHANTYPES), uid[4]);
/* PREFIX */
/* ok new irc-find-person checked */
if ((ischn && (person = irc_find_person(j, j->people, j->nick)) && (perchn = irc_find_person_chan(person->channels, (char *)uid))))
prefix[0] = *(perchn->sign);
if (!ischn || (!session_int_get(session, "SHOW_NICKMODE_EMPTY") && *prefix==' '))
*prefix='\0';
frname = format_find(prv?
ischn?"irc_msg_sent_chan":w?"irc_msg_sent_n":"irc_msg_sent":
ischn?"irc_not_sent_chan":w?"irc_not_sent_n":"irc_not_sent");
rcpts = xmalloc(sizeof(char *) * 2);
rcpts[0] = xstrdup(!!w?w->target:uid);
rcpts[1] = NULL;
debug ("%s - %s\n", uid, rcpts[0]);
tmpbuf = (mline[0] = xstrdup(params[1]));
while ((mline[1] = split_line(&(mline[0])))) {
char *line, *recoded;
char *__mtmp, *padding = NULL;
int len_limit, msg_len;
line = xstrdup(mline[1]);
{ /* one-shot variables */
int __isour = 1;
int __to_us = 0;
int __priv = !ischn;
query_emit(NULL, "irc-protocol-message", &(session->uid), &(j->nick), &line, &__isour,
&__to_us, &__priv, &uid);
}
if (perchn)
padding = nickpad_string_apply(perchn->chanp, j->nick);
head = format_string(frname, session_name(session),
prefix, j->nick, j->nick, uid+4, line, padding);
if (perchn)
nickpad_string_restore(perchn->chanp);
recoded = irc_convert_out(j, uid, line);
coloured = irc_ircoldcolstr_to_ekgcolstr(session, head, 1);
query_emit(NULL, "message-encrypt", &(session->uid), &uid, &recoded, &secure);
protocol_message_emit(session, session->uid, rcpts, coloured, NULL, time(NULL), (EKG_MSGCLASS_SENT | EKG_NO_THEMEBIT), NULL, EKG_NO_BEEP, secure);
debug ("%s ! %s\n", j->nick, j->host_ident);
/* "Thus, there are 510 characters maximum allowed for the command and its parameters." [rfc2812]
* yes, I know it's a nasty variable reusing ;)
*/
len_limit = 510 - (prv?7:6) - 6 - xstrlen(uid+4) - xstrlen(j->host_ident) - xstrlen(j->nick);
/* 6 = 3xspace + '!' + 2xsemicolon; -> [:nick!ident@hostident PRIVMSG dest :mesg] */
msg_len = xstrlen(recoded);
__mtmp = recoded;
while ( msg_len > len_limit )
{
char saved = __mtmp[len_limit];
__mtmp[len_limit] = '\0'; /* XXX danger: cut unicode chars */
ekg_connection_write(j->send_stream, "%s %s :%s\r\n", (prv) ? "PRIVMSG" : "NOTICE", uid+4, __mtmp);
__mtmp[len_limit] = saved;
__mtmp += len_limit;
msg_len -= len_limit;
}
ekg_connection_write(j->send_stream, "%s %s :%s\r\n", (prv) ? "PRIVMSG" : "NOTICE", uid+4, __mtmp);
xfree(line);
xfree(recoded);
xfree(coloured);
xfree(head);
}
xfree(rcpts[0]);
xfree(rcpts);
xfree(uid);
xfree(tmpbuf);
if (!quiet)
session_unidle(session);
return 0;
}
static COMMAND(irc_command_inline_msg) {
const char *p[2] = { NULL, params[0] };
if (!target || !params[0])
return -1;
return irc_command_msg(("msg"), p, session, target, quiet);
}
static COMMAND(irc_command_quote) {
ekg_connection_write(irc_private(session)->send_stream, "%s\r\n", params[0]);
return 0;
}
static COMMAND(irc_command_pipl) {
irc_private_t *j = irc_private(session);
list_t t1, t2;
people_t *per;
people_chan_t *chan;
debug_white("[irc] this is a secret command ;-)\n");
for (t1 = j->people; t1; t1=t1->next) {
per = (people_t *)t1->data;
debug("(%s)![%s]@{%s} ", per->nick, per->ident, per->host);
for (t2 = per->channels; t2; t2=t2->next)
{
chan = (people_chan_t *)t2->data;
debug ("%s:%d, ", chan->chanp->name, chan->mode);
}
debug("\n");
}
return 0;
}
static COMMAND(irc_command_alist) {
irc_private_t *j = irc_private(session);
int isshow = 0;
#if 0
for (l = j->people; l; l = l->next) {
people_t *per = l->data;
printq("irc_access_known", session_name(session), per->nick+4, per->ident, per->host);
}
#endif
if (!params[0] || match_arg(params[0], 'l', "list", 2) || (isshow = match_arg(params[0], 's', "show", 2))) { /* list, list, show */
#if 0
list_t l;
for (l = session->userlist; l; l = l->next) {
userlist_t *u = l->data;
/* u->resources; */
}
#endif
return 0;
}
if (match_arg(params[0], 'a', "add", 2)) {
/* params[1] - nick!ident@hostname or nickname (in form irc:nick)
* params[2] - options + channels like: +ison +autoop:#linux,#linux.pl +autounban:* +autovoice:*
* ZZZ params[2] - channels: #chan1,#chan2 or '*'
* ZZZ params[3] - options.
*/
/* /irc:access -a irc:nickname #chan1,#chan2 +autoop +autounban +revenge +ison */
char *mask = NULL;
const char *channel;
char *groupstr;
userlist_t *u;
if (!params[1] || !(channel = params[2])) {
printq("not_enough_params", name);
return -1;
}
if (!xstrncmp(params[1], "irc:", 4)) { /* nickname */
list_t l;
for (l = j->people; l; l = l->next) {
people_t *per = l->data;
if (!xstrcmp(per->nick+4, params[1]+4)) {
/* XXX, here generate mask */
mask = saprintf("%s!%s@%s", per->nick+4, per->ident, per->host);
break;
}
}
if (!mask) {
printq("user_not_found", params[1]);
return -1;
}
} else {
/* XXX verify if mask in params[1] is ok */
mask = xstrdup(params[1]);
}
{
char *tmp = saprintf("irc:%s:%s", mask, channel);
u = userlist_add(session, tmp, params[1]);
if (params[3]) {
char **arr = array_make(params[3], " ", 0, 1, 1);
int i;
for (i=0; arr[i]; i++) {
const char *value = arr[i];
if (arr[i][0] == '+')
value++;
if (!xstrcmp(value, "autoop")) ekg_group_add(u, "__autoop"); /* +o */
else if (!xstrcmp(value, "autovoice")) ekg_group_add(u, "__autovoice"); /* +v */
else if (!xstrcmp(value, "autounban")) ekg_group_add(u, "__autounban"); /* -b */
else if (!xstrcmp(value, "autoban")) ekg_group_add(u, "__autoban"); /* +b */
else if (!xstrcmp(value, "autodevop")) ekg_group_add(u, "__autodevop"); /* -o, -h, -v */
else if (!xstrcmp(value, "revenge")) ekg_group_add(u, "__revenge"); /* + */
else if (!xstrcmp(value, "ison")) ekg_group_add(u, "__ison"); /* + */
else printq("irc_access_invalid_flag", value);
}
g_strfreev(arr);
} else u->groups = group_init(irc_config_default_access_groups);
xfree(tmp);
}
groupstr = group_to_string(u->groups, 1, 1);
printq("irc_access_added", session_name(session), "0" /* XXX # */, mask, channel, groupstr);
xfree(groupstr);
xfree(mask);
/* XXX sync if wanted */
return 0;
}
if (match_arg(params[0], 'd', "delete", 2)) {
printq("generic_error", "stub function use /del");
return -1;
}
if (match_arg(params[0], 'e', "edit", 2)) {
printq("generic_error", "stub function");
return -1;
}
if (match_arg(params[0], 'S', "sync", 2)) {
printq("generic_error", "stub function");
return -1;
}
printq("invalid_params", name, params[0]);
return -1;
}
static COMMAND(irc_command_add) {
printq("generic_error", "/irc:add do nothing. if you want friendlists use /irc:access");
/* XXX, wrapper do /irc:access --add $UID +ison ? */
return -1;
}
static void irc_display_awaylog(session_t *session) {
irc_private_t *j = irc_private(session);
if (j->awaylog) {
list_t l;
const char *awaylog_timestampf = format_find("irc_awaylog_timestamp");
print_status("irc_awaylog_begin", session_name(session));
for (l = j->awaylog; l; l = l->next) {
irc_awaylog_t *e = l->data;
if (e->channame)
print_status("irc_awaylog_msg_chan", session_name(session),
timestamp_time(awaylog_timestampf, e->t), (e->channame)+4, (e->uid)+4, e->msg);
else
print_status("irc_awaylog_msg", session_name(session),
timestamp_time(awaylog_timestampf, e->t), "", (e->uid)+4, e->msg);
xfree(e->channame);
xfree(e->uid);
xfree(e->msg);
}
print_status("irc_awaylog_end", session_name(session));
list_destroy(j->awaylog, 1);
j->awaylog = NULL;
}
}
static COMMAND(irc_command_away) {
irc_private_t *j = irc_private(session);
int isaway = 0;
if (!xstrcmp(name, ("back"))) {
session_descr_set(session, NULL);
session_status_set(session, EKG_STATUS_AVAIL);
session_unidle(session);
} else if (!xstrcmp(name, ("away"))) {
session_descr_set(session, params[0]);
session_status_set(session, EKG_STATUS_AWAY);
session_unidle(session);
isaway = 1;
} else if (!xstrcmp(name, ("_autoaway"))) {
session_status_set(session, EKG_STATUS_AUTOAWAY);
isaway = 1;
} else if (!xstrcmp(name, ("_autoback"))) {
session_status_set(session, EKG_STATUS_AUTOBACK);
session_unidle(session);
} else {
printq("generic_error", "Ale o so chozi?");