/
friends_shasta.pl
2719 lines (2382 loc) · 84.8 KB
/
friends_shasta.pl
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
#!/usr/bin/perl -w
#
# This script may not work with irssi older than 0.8.5!
#
# Historical author of this script is Erkki Seppala <flux@inside.org>
# Now it's maintained by me, so i'm listed as an author.
#
# $Id: friends.pl,v 1.3 2003/11/09 21:11:45 shasta Exp $
use strict;
use vars qw($VERSION %IRSSI);
$VERSION = "2.4.9";
%IRSSI = (
authors => 'Jakub Jankowski',
contact => 'shasta@toxcorp.com',
name => 'Friends',
description => 'Maintains list of people you know.',
license => 'GNU GPLv2 or later',
url => 'http://toxcorp.com/irc/irssi/friends/',
changed => 'Sun Oct 9 22:12:43 2003'
);
use Irssi 20011201.0100 ();
use Irssi::Irc;
# friends.pl
my $friends_version = $VERSION . " (20031109)";
# release note, if any
my $release_note = "Please read http://toxcorp.com/irc/irssi/friends/current/README\n";
##############################################
# These variables are adjustable with /set
# but here are some 'safe' defaults:
# do you want to process CTCP queries?
my $default_friends_use_ctcp = 1;
# space-separated list of allowed (implemented ;) CTCP commands
my $default_friends_ctcp_commands = "OP VOICE LIMIT KEY INVITE PASS IDENT UNBAN";
# do you want to learn new users?
my $default_friends_learn = 1;
# do you want to autovoice already opped nicks?
my $default_friends_voice_opped = 0;
# do you want to show additional info with /whois?
my $default_friends_show_whois_extra = 1;
# which flags do you want to add automatically with /addfriend? (case *sensitive*)
my $default_friends_default_flags = "";
# default path to friendlist
my $default_friends_file = Irssi::get_irssi_dir() . "/friends";
# do you want to save friendlist every time irssi's setup is saved
my $default_friends_autosave = 0;
# do you want to backup your friendlist upon a save
my $default_friends_backup_friendlist = 1;
# backup suffix to use (unixtime if empty)
my $default_friends_backup_suffix = ".backup";
# do you want to show friend's flags while he joins a channel?
my $default_friends_show_flags_on_join = 1;
# do you want to revenge?
my $default_friends_revenge = 1;
# revenge mode:
# 0 Deop the user.
# 1 Deop the user and give them the +D flag for the channel.
# 2 Deop the user, give them the +D flag for the channel, and kick them.
# 3 Deop the user, give them the +D flag for the channel, kick, and ban them.
my $default_friends_revenge_mode = 0;
# do you want /findfriends to print info in separate windows for separate chans?
my $default_friends_findfriends_to_windows = 0;
# maximum size of operationQueue
my $default_friends_max_queue_size = 20;
# min delaytime
my $default_delay_min = 10;
# max delaytime
my $default_delay_max = 60;
###############################################################
# registering themes
Irssi::theme_register([
'friends_empty', 'Your friendlist is empty. Add items with /ADDFRIEND',
'friends_notenoughargs', 'Not enough arguments. Usage: $0',
'friends_badargs', 'Bad arguments. Usage: $0',
'friends_nosuch', 'No such friend %R$0%n',
'friends_notonchan', 'Not on channel {hilight $0}',
'friends_endof', 'End of $0 $1',
'friends_badhandle', 'Wrong handle: %R$0%n. $1',
'friends_notuniqhandle', 'Handle %R$0%n already exists, choose another one',
'friends_version', 'friends.pl\'s version: {hilight $0} [$1]',
'friends_file_written', 'friendlist written on: {hilight $0}',
'friends_file_version', 'friendlist written with: {hilight $0} [$1]',
'friends_filetooold', 'Friendfile too old, loading aborted',
'friends_loaded', 'Loaded {hilight $0} friends from $1',
'friends_saved', 'Saved {hilight $0} friends to $1',
'friends_duplicate', 'Skipping %R$0%n [duplicate?]',
'friends_checking', 'Checking {hilight $0} took {hilight $1} secs [on $2]',
'friends_line_head', '[$[!-3]0] Handle: %R$1%n, flags: %C$2%n [password: $3]',
'friends_line_hosts', '$[-6]9 Hosts: $0',
'friends_line_chan', '$[-6]9 Channel {hilight $0}: Flags: %c$1%n, Delay: $2',
'friends_line_comment', '$[-6]9 Comment: $0',
'friends_line_currentnick', '$[-6]9 [$1] Current nick: {nick $0}',
'friends_line_channelson', '$[-6]9 [$1] Currently sharing with you: $0',
'friends_joined', '{nick $0} is a friend, handle: %R$1%n, global flags: %C$2%n, flags for {hilight $3}: %C$4%n',
'friends_whois', '{whois friend handle: {hilight $0}, global flags: $1}',
'friends_queue_empty', 'Operation queue is empty',
'friends_queue_line1', '[$[!-2]0] Operation: %R$1%n secs left before {hilight $2}',
'friends_queue_line2', ' (Server: {hilight $0}, Channel: {hilight $1}, Nicklist: $2)',
'friends_queue_nosuch', 'No such entry in operation queue ($0)',
'friends_queue_removed', '$0 queues: {hilight $1} [$2]',
'friends_friendlist', '{hilight Friendlist} [$0]:',
'friends_friendlist_count', 'Listed {hilight $0} friend$1',
'friends_findfriends', 'Looking for %R$2%n on channel {hilight $0} [on $1]:',
'friends_already_added', 'Nick {hilight $0} matches one of %R$1%n\'s hosts',
'friends_added', 'Added %R$0%n to friendlist',
'friends_removed', 'Removed %R$0%n from friendlist',
'friends_comment_added', 'Added comment line to %R$0%n ($1)',
'friends_comment_removed', 'Removed comment line from %R$0%n',
'friends_host_added', 'Added {hilight $1} to %R$0%n',
'friends_host_removed', 'Removed {hilight $1} from %R$0%n',
'friends_host_exists', 'Hostmask {hilight $1} overlaps with one of the already added to %R$0%n',
'friends_host_notexists', '%R$0%n does not have {hilight $1} in hostlist',
'friends_chanrec_removed', 'Removed {hilight $1} record from %R$0%n',
'friends_chanrec_notexists', '%R$0%n does not have {hilight $1} record',
'friends_changed_handle', 'Changed {hilight $0} to %R$1%n',
'friends_changed_delay', 'Changed %R$0%n\'s delay value on {hilight $1} to %c$2%n',
'friends_chflagexec', 'Executing %c$0%n for %R$1%n ($2)',
'friends_currentflags', 'Current {channel $2} flags for %R$1%n are: %c$0%n',
'friends_chpassexec', 'Altered password for %R$0%n',
'friends_ctcprequest', '%R$0%n asks for {hilight $1} on {hilight $2}',
'friends_ctcppass', 'Password for %R$0%n altered by $1',
'friends_ctcpident', 'CTCP IDENT for %R$0%n from {hilight $1} succeeded',
'friends_ctcpfail', 'Failed CTCP {hilight $0} from %R$1%n. $2',
'friends_optree_header', 'Opping tree:',
'friends_optree_line1', '%R$0%n has opped these:',
'friends_optree_line2', '{hilight $[!-4]0} times: $1',
'friends_general', '$0',
'friends_notice', '[%RN%n] $0'
]);
my @friends = ();
my $all_regexp_hosts = {};
my $all_hosts = {};
my $all_handles = {};
my @operationQueue = ();
my $timerHandle = undef;
my $friends_file_version;
my $friends_file_written;
my $friends_PLAIN_HOSTS = 0;
my $friends_REGEXP_HOSTS = 1;
# Idea of moving userhost to a regexp and
# the subroutine userhost_to_regexp were adapted from people.pl,
# an userlist script made by Marcin 'Qrczak' Kowalczyk.
# You can get that script from http://qrnik.knm.org.pl/~qrczak/irssi/people.pl
# or from http://scripts.irssi.org/
# HostToRegexp
my %htr = ();
# fill the hash
foreach my $i (0..255) {
my $ch = chr($i);
$htr{$ch} = "\Q$ch\E";
}
# wildcards to regexp
$htr{'?'} = '.';
$htr{'*'} = '.*';
# str userhost_to_regexp($userhost)
# translates userhost to a regexp
# lowercases host-part
sub userhost_to_regexp($) {
my ($mask) = @_;
$mask = lowercase_hostpart($mask);
$mask =~ s/(.)/$htr{$1}/g;
return $mask;
}
# str lowercase_hostpart($userhost)
# returns userhost with host-part loweracased
sub lowercase_hostpart($) {
my ($host) = @_;
$host =~ s/(.+)\@(.+)/sprintf("%s@%s", $1, lc($2));/eg;
return $host;
}
# void print_version($what)
# print's version of script/userlist
sub print_version($) {
my ($what) = @_;
$what = lc($what);
if ($what eq "filever") {
if ($friends_file_version) {
my ($verbal, $numeric) = $friends_file_version =~ /^(.+)\ \(([0-9]+)\)$/;
Irssi::printformat(MSGLEVEL_CRAP, 'friends_file_version', $verbal, $numeric);
} else {
Irssi::printformat(MSGLEVEL_CRAP, 'friends_empty');
}
} elsif ($what eq "filewritten" && $friends_file_written) {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($friends_file_written);
my $written = sprintf("%4d%02d%02d %02d:%02d:%02d", ($year+1900), ($mon+1), $mday, $hour, $min, $sec);
Irssi::printformat(MSGLEVEL_CRAP, 'friends_file_written', $written);
} else {
my ($verbal, $numerical) = $friends_version =~ /^(.+)\ \(([0-9]+)\)$/;
Irssi::printformat(MSGLEVEL_CRAP, 'friends_version', $verbal, $numerical);
}
}
# void print_releasenote()
# suprisingly, prints a release note ;^)
sub print_releasenote {
foreach my $line (split(/\n/, $release_note)) {
Irssi::printformat(MSGLEVEL_CRAP, 'friends_notice', $line);
}
}
# str friends_crypt($plain)
# returns crypt()ed $plain, using random salt;
# or "" if $plain is empty
sub friends_crypt {
return if ($_[0] eq "");
return crypt("$_[0]", (join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
}
# bool friend_passwdok($idx, $pwd)
# returns 1 if password is ok, 0 if isn't
sub friends_passwdok {
my ($idx, $pwd) = @_;
return 1 if (crypt("$pwd", $friends[$idx]->{password}) eq $friends[$idx]->{password});
return 0;
}
# arr get_friends_channels($idx)
# returns list of $friends[$idx] channels
sub get_friends_channels {
return keys(%{$friends[$_[0]]->{channels}});
}
# arr get_friends_hosts($idx, $type)
# returns list of $friends[$idx] regexp-hostmask if $type=$friends_REGEXP_HOSTS
# returns list of plain-hostmasks if $type=$friends_PLAIN_HOSTS
sub get_friends_hosts($$) {
if ($_[1] == $friends_REGEXP_HOSTS) {
return keys(%{$friends[$_[0]]->{regexp_hosts}});
} elsif ($_[1] == $friends_PLAIN_HOSTS) {
return keys(%{$friends[$_[0]]->{hosts}});
}
return undef;
}
# str get_friends_flags($idx[, $chan])
# returns list of $chan flags for $idx
# $chan can be also 'global' or undef
# case insensitive about the $chan
sub get_friends_flags {
my ($idx, $chan) = @_;
$chan = lc($chan);
if ($chan eq "" || $chan eq "global") {
return $friends[$idx]->{globflags};
} else {
foreach my $friendschan (get_friends_channels($idx)) {
if ($chan eq lc($friendschan)) {
return $friends[$idx]->{channels}->{$friendschan}->{flags};
}
}
}
return;
}
# str get_friends_delay($idx[, $chan])
# returns $chan delay for $idx
# returns "" if $chan is 'global' or undef
# case insensitive about the $chan
sub get_friends_delay {
my ($idx, $chan) = @_;
$chan = lc($chan);
if ($chan && $chan ne "global") {
foreach my $friendschan (get_friends_channels($idx)) {
if ($chan eq lc($friendschan)) {
return undef if ($friends[$idx]->{channels}->{$friendschan}->{delay} eq '');
return $friends[$idx]->{channels}->{$friendschan}->{delay};
}
}
}
return;
}
# struct friend new_friend($handle, $hoststr, $globflags, $chanflagstr, $password, $comment)
# hoststr is: *!foo@host1 *!bar@host2 *!?baz@host3
# chanstr is: #chan1,flags,delay #chan2,flags,delay
sub new_friend {
my $friend = {};
my $idx = scalar(@friends);
$friend->{handle} = $_[0];
$all_handles->{lc($_[0])} = $idx;
$friend->{globflags} = $_[2];
$friend->{password} = $_[4];
$friend->{comment} = $_[5];
$friend->{friends} = [];
foreach my $host (split(/ +/, $_[1])) {
my $regexp_host = userhost_to_regexp($host);
my ($firstalpha) = $host =~ /\@(.)/;
$firstalpha = lc($firstalpha);
$friend->{hosts}->{$host} = $regexp_host;
$friend->{regexp_hosts}->{$regexp_host} = $host;
$all_regexp_hosts->{allhosts}->{$regexp_host} = lc($_[0]);
$all_regexp_hosts->{$firstalpha}->{$regexp_host} = lc($_[0]);
$all_hosts->{$host} = lc($_[0]);
}
foreach my $cfd (split(/ +/, $_[3])) {
# $cfd format: #foobar,oikl,15 (channelname,flags,delay)
my ($channel, $flags, $delay) = split(",", $cfd, 3);
$friend->{channels}->{$channel}->{exist} = 1;
$friend->{channels}->{$channel}->{flags} = $flags;
$friend->{channels}->{$channel}->{delay} = $delay;
}
return $friend;
}
# get_regexp_hosts_by_letter($letter)
# returns those regexp masks whose host part begins with $letter, '?' or '*'
sub get_regexp_hosts_by_letter($) {
my $l = lc(substr($_[0], 0, 1));
my @tmphosts = ();
push(@tmphosts, keys(%{$all_regexp_hosts->{$l}}));
push(@tmphosts, keys(%{$all_regexp_hosts->{'?'}}));
push(@tmphosts, keys(%{$all_regexp_hosts->{'*'}}));
return @tmphosts;
}
# bool is_allowed_flag($flag)
# will be obsolete, soon.
sub is_allowed_flag { return 1; }
# bool is_ctcp_command($command)
# check if $command is one of the implemented ctcp commands
sub is_ctcp_command {
my ($command) = @_;
$command = uc($command);
foreach my $allowed (split(/[,\ \|]+/, uc(Irssi::settings_get_str('friends_ctcp_commands')))) {
return 1 if ($command eq $allowed);
}
return 0;
}
# int get_idx($nick, $userhost)
# returns idx of the friend or -1 if not a friend
# The New Approach (TM) :)
sub get_idx($$) {
my ($nick, $userhost) = @_;
$userhost = lowercase_hostpart($nick.'!'.$userhost);
my ($letter) = $userhost =~ /\@(.)/;
my $idx = -1;
foreach my $regexp_host (get_regexp_hosts_by_letter($letter)) {
if ($userhost =~ /^$regexp_host$/) {
return get_idxbyhand($all_regexp_hosts->{allhosts}->{$regexp_host});
}
}
return -1;
}
# int get_idxbyhand($handle)
# returns $idx of friend with $handle or -1 if no such handle
# case insensitive
sub get_idxbyhand($) {
my $handle = lc($_[0]);
if (exists $all_handles->{$handle}) {
return $all_handles->{$handle};
}
return -1;
}
# int get_handbyidx($idx)
# returns $handle of friend with $idx or undef if no such $idx
# case sensitive
sub get_handbyidx($) {
my ($idx) = @_;
return undef unless ($idx > -1 && $idx < scalar(@friends));
return $friends[$idx]->{handle};
}
# bool friend_has_host($idx, $host)
# checks wheter $host matches any of $friend[$idx]'s hostmasks
# The New Approach (TM)
sub friend_has_host($$) {
my ($idx, $host) = @_;
$host = lowercase_hostpart($host);
foreach my $regexp_host (keys (%{$friends[$idx]->{regexp_hosts}})) {
return 1 if ($host =~ /^$regexp_host$/);
}
return 0;
}
# void add_host($idx, $host)
# adds $host wherever it's needed
# $friends[$idx]->{handle} is A MUST for add_host() to work properly.
sub add_host($$) {
my ($idx, $host) = @_;
my $regexp_host = userhost_to_regexp($host);
my ($firstalpha) = $host =~ /\@(.)/;
$firstalpha = lc($firstalpha);
$friends[$idx]->{hosts}->{$host} = $regexp_host;
$friends[$idx]->{regexp_hosts}->{$regexp_host} = $host;
$all_regexp_hosts->{allhosts}->{$regexp_host} = lc($friends[$idx]->{handle});
$all_regexp_hosts->{$firstalpha}->{$regexp_host} = lc($friends[$idx]->{handle});
$all_hosts->{$host} = lc($friends[$idx]->{handle});
}
# int del_host($idx, $host)
# deletes $host from wherever it is
# if given $host arg is '*', removes all hosts of this friend
sub del_host($$) {
my ($idx, $host) = @_;
my $deleted = 0;
foreach my $regexp_host (keys (%{$friends[$idx]->{regexp_hosts}})) {
if ($host eq '*' || $host =~ /^$regexp_host$/) {
my $plain_host = $friends[$idx]->{regexp_hosts}->{$regexp_host};
my ($l) = $plain_host =~ /\@(.)/;
delete $friends[$idx]->{hosts}->{$plain_host};
delete $friends[$idx]->{regexp_hosts}->{$regexp_host};
delete $all_regexp_hosts->{allhosts}->{$regexp_host};
delete $all_regexp_hosts->{$l}->{$regexp_host};
delete $all_hosts->{$plain_host};
$deleted++;
}
}
return $deleted;
}
# bool friend_has_chanrec($idx, $chan)
# checks wheter $friend[$idx] has a $chan record
# case insensitive
sub friend_has_chanrec {
my ($idx, $chan) = @_;
$chan = lc($chan);
foreach my $friendschan (get_friends_channels($idx)) {
return 1 if ($chan eq lc($friendschan));
}
return 0;
}
# bool add_chanrec($idx, $chan)
# adds an empty $chan record to $friends[$idx]
# case sensitive
sub add_chanrec {
my ($idx, $chan) = @_;
return 0 unless ($idx > -1 && $idx < scalar(@friends));
$friends[$idx]->{channels}->{$chan}->{exist} = 1;
return 1;
}
# bool del_chanrec($idx, $chan)
# deletes $chan record from $friends[$idx]
# case *in*sensitive
sub del_chanrec {
my ($idx, $chan) = @_;
my $deleted = 0;
foreach my $friendschan (get_friends_channels($idx)) {
if (lc($chan) eq lc($friendschan)) {
delete $friends[$idx]->{channels}->{$friendschan};
$deleted = 1;
}
}
return $deleted;
}
# arr del_friend($idxs)
# removes friends
# removes all hosts corresponding to this friend
# returns array of removed friends
sub del_friend($) {
my ($idxlist) = @_;
my @idxs = split(/ /, $idxlist);
return -1 unless (scalar(@idxs) > 0);
my @tmp = ();
my @result = ();
my @todelete = ();
foreach my $idx (@idxs) {
my $handle = get_handbyidx($idx);
if (!(!defined $handle || grep(/^\Q$handle\E$/i, @todelete))) {
push(@todelete, $handle);
del_host($idx, '*');
}
}
for (my $idx = 0; $idx < @friends; $idx++) {
if (grep(/^\Q$friends[$idx]->{handle}\E$/i, @todelete)) {
push(@result, $friends[$idx]);
} else {
push(@tmp, $friends[$idx]);
}
}
@friends = @tmp;
update_allhandles();
return @result;
}
# void update_all_handles()
# updates $all_handles
sub update_allhandles {
$all_handles = {};
for (my $idx = 0; $idx < @friends; $idx++) {
$all_handles->{lc($friends[$idx]->{handle})} = $idx
}
}
# bool is_unique_handle($handle)
# checks if the $handle is unique for the whole friendlist
# returns 1 if there's no such $handle
# returns 0 if there is one.
sub is_unique_handle($) {
return !exists $all_handles->{lc($_[0])};
}
# str choose_handle($proposed)
# tries to choose a handle, closest to the $proposed one
sub choose_handle {
my ($proposed) = @_;
my $counter = 0;
my $handle = $proposed;
# do this until we have an unique handle
while (!is_unique_handle($handle)) {
if (($handle !~ /([0-9]+)$/) && !$counter) {
# first, if handle doesn't end with a digit, append '2'
# (but only in first step)
$handle .= "2";
} elsif ($counter < 85) {
# later, increase the trailing number by one
# do that 84 times
my ($number) = $handle =~ /([0-9]+)$/;
++$number;
$handle =~ s/([0-9]+)$/$number/;
} elsif ($counter == 85) {
# then, if it didn't helped, make $handle = $proposed."_"
$handle = $proposed . "_";
} elsif ($counter < 90) {
# if still unsuccessful, append "_" to the handle
# do that 4 times
$handle .= "_";
} else {
# if THAT didn't help -- make some silly handle
# and exit the loop
$handle = $proposed.'_'.(join '', (0..9, 'a'..'z')[rand 36, rand 36, rand 36, rand 36]);
last;
}
++$counter;
}
# return our glorious handle ;-)
return $handle;
}
# bool friend_has_flag($idx, $flag[, $chan])
# returns true if $friends[$idx] has $flag for $chan
# (checks global flags, if $chan is 'global' or undef)
# returns false if hasn't
# case sensitive about the FLAG
# case insensitive about the chan.
sub friend_has_flag {
my ($idx, $flag, $chan) = @_;
$chan = "global" unless ($chan ne '');
return 1 if (get_friends_flags($idx, $chan) =~ /\Q$flag\E/);
return 0;
}
# bool friend_is_wrapper($idx, $chan, $goodflag, $badflag)
# something to replace friend_is_* subs
# true on: ($channel +$goodflag OR global +$goodflag) AND ($badflag == "" OR NOT $channel +$badflag))
sub friend_is_wrapper($$$$) {
my ($idx, $chan, $goodflag, $badflag) = @_;
return 0 unless ($idx > -1);
if ((friend_has_flag($idx, $goodflag, $chan) ||
friend_has_flag($idx, $goodflag, undef)) &&
($badflag eq "" || !friend_has_flag($idx, $badflag, $chan))) {
return 1;
}
return 0;
}
# bool add_flag($idx, $flag[, $chan])
# adds $flag to $idx's $chan flags
# $chan can be 'global' or undef
# case insensitive about the $chan -- chooses the proper case.
# returns 1 on success
sub add_flag {
my ($idx, $flag, $chan) = @_;
$chan = lc($chan);
if ($chan eq "" || $chan eq "global") {
$friends[$idx]->{globflags} .= $flag;
return 1;
} else {
foreach my $friendschan (get_friends_channels($idx)) {
if ($chan eq lc($friendschan)) {
$friends[$idx]->{channels}->{$friendschan}->{flags} .= $flag;
return 1;
}
}
}
return 0;
}
# bool del_flag($idx, $flag[, $chan])
# removes $flag from $idx's $chan flags
# $chan can be 'global' or undef
# case insensitive about the $chan -- chooses the proper case.
sub del_flag {
my ($idx, $flag, $chan) = @_;
$chan = lc($chan);
if ($chan eq "" || $chan eq "global") {
$friends[$idx]->{globflags} =~ s/\Q$flag\E//g;
return 1;
} else {
foreach my $friendschan (get_friends_channels($idx)) {
if ($chan eq lc($friendschan)) {
$friends[$idx]->{channels}->{$friendschan}->{flags} =~ s/\Q$flag\E//i;
return 1;
}
}
}
return 0;
}
# bool change_delay($idx, $delay, $chan)
# alters $idx's delay time for $chan
# fails if $chan is 'global' or undef
sub change_delay {
my ($idx, $delay, $chan) = @_;
$chan = lc($chan);
if ($chan && $chan ne "global") {
foreach my $friendschan (get_friends_channels($idx)) {
if ($chan eq lc($friendschan)) {
$friends[$idx]->{channels}->{$friendschan}->{delay} = $delay;
return 1;
}
}
}
return 0;
}
# void list_friend($window, $who, @data)
# prints an info line about certain friend.
# $who may be handle or idx
# if you want to improve the look of the script, you should
# change /format friends_*, probably.
sub list_friend {
my ($win, $who, @data) = @_;
my $idx = $who;
$idx = get_idxbyhand($who) unless ($who =~ /^[0-9]+$/);
return unless ($idx > -1 && $idx < scalar(@friends));
my $globflags = get_friends_flags($idx, undef);
$win = Irssi::active_win() unless ($win);
$win->printformat(MSGLEVEL_CRAP, 'friends_line_head',
$idx,
get_handbyidx($idx),
(($globflags) ? "$globflags" : "[none]"),
(($friends[$idx]->{password}) ? "yes" : "no"));
$win->printformat(MSGLEVEL_CRAP, 'friends_line_hosts',
join(", ", get_friends_hosts($idx, $friends_PLAIN_HOSTS)) );
foreach my $chan (get_friends_channels($idx)) {
my $flags = get_friends_flags($idx, $chan);
my $delay = get_friends_delay($idx, $chan);
$win->printformat(MSGLEVEL_CRAP, 'friends_line_chan',
$chan,
(($flags) ? "$flags" : "[none]"),
(defined($delay) ? "$delay" : "random"));
}
if ($friends[$idx]->{comment}) {
$win->printformat(MSGLEVEL_CRAP, 'friends_line_comment', $friends[$idx]->{comment});
}
for my $item (@data) {
my ($ircnet, $nick, $chanstr) = split(" ", $item);
next unless (defined $ircnet);
$win->printformat(MSGLEVEL_CRAP, 'friends_line_currentnick', $nick, $ircnet) if ($nick ne '');;
$win->printformat(MSGLEVEL_CRAP, 'friends_line_channelson', join(", ", split(/,/, $chanstr)), $ircnet) if ($chanstr ne '');
}
}
# void add_operation($server, "#channel", "op|voice|deop|devoice|kick|kickban", timeout, "nick1", "nick2", ...)
# adds a delayed (or not) operation
sub add_operation {
my ($server, $channel, $operation, $timeout, @nicks) = @_;
# my dear queue, don't grow too big, mmkay? ;^)
my $maxsize = Irssi::settings_get_int('friends_max_queue_size');
$maxsize = $default_friends_max_queue_size unless ($maxsize > 0);
return if (@operationQueue >= $maxsize);
push(@operationQueue,
{
server=>$server, # server object
left=>$timeout, # seconds left
nicks=>[ @nicks ], # array of nicks
channel=>$channel, # channel name
operation=>$operation # operation ("op", "voice" and so on)
});
$timerHandle = Irssi::timeout_add(1000, 'timer_handler', 0) unless (defined $timerHandle);
}
# void timer_handler()
# handles delay timer
sub timer_handler {
my @ops = ();
# splice out expired timeouts. if they are expired, move them to
# local ops-queue. this allows creating new operations to the queue
# in the operation. (we're not (yet) doing that)
for (my $c = 0; $c < @operationQueue;) {
if ($operationQueue[$c]->{left} <= 0) {
push(@ops, splice(@operationQueue, $c, 1));
} else {
++$c;
}
}
for (my $c = 0; $c < @ops; ++$c) {
my $op = $ops[$c];
my $channel = $op->{server}->channel_find($op->{channel});
# check if $channel is still active (you might've parted)
if ($channel) {
my @operationNicks = ();
foreach my $nickStr (@{$op->{nicks}}) {
my $nick = $channel->nick_find($nickStr);
# check if there's still such nick (it might've quit/parted)
if ($nick) {
if ($op->{operation} eq "op" && !$nick->{op}) {
push(@operationNicks, $nick->{nick});
}
if ($op->{operation} eq "voice" && !$nick->{voice} &&
(!$nick->{op} || Irssi::settings_get_bool('friends_voice_opped'))) {
push(@operationNicks, $nick->{nick});
}
if ($op->{operation} eq "deop" && $nick->{op}) {
push(@operationNicks, $nick->{nick});
}
if ($op->{operation} eq "devoice" && $nick->{voice}) {
push(@operationNicks, $nick->{nick});
}
if ($op->{operation} eq "kick") {
push(@operationNicks, $nick->{nick});
}
if ($op->{operation} eq "kickban") {
push(@operationNicks, $nick->{nick});
}
}
}
# final stage: issue desired command if we're a chanop
$channel->command($op->{operation}." ".join(" ", @operationNicks)) if ($channel->{chanop});
}
}
# decrement timeouts.
for (my $c = 0; $c < @operationQueue; ++$c) {
--$operationQueue[$c]->{left};
}
# if operation queue is empty, remove timer.
if (!@operationQueue && $timerHandle) {
Irssi::timeout_remove($timerHandle);
$timerHandle = undef;
}
}
# str replace_home($string)
# replaces '~' with current $ENV{HOME}
sub replace_home($) {
my ($string) = @_;
my $home = $ENV{HOME};
return undef unless ($string);
$string =~ s/^\~/$home/;
return $string;
}
# void load_friends($inputfile)
# loads friends from file. uses $inputfile if supplied.
# if not, uses friends_file setting. if this setting is empty,
# uses default -- $friends_file
sub load_friends {
my ($inputfile) = @_;
my $friendfile = undef;
if (defined($inputfile)) {
$friendfile = replace_home($inputfile);
} else {
$friendfile = replace_home(Irssi::settings_get_str('friends_file'));
}
$friendfile = $default_friends_file unless (defined $friendfile);
if (-e $friendfile && -r $friendfile) {
@friends = ();
$all_hosts = {};
$all_regexp_hosts = {};
$all_handles = {};
local *F;
open(F, "<", $friendfile) or return -1;
local $/ = "\n";
while (<F>) {
my ($handle, $hosts, $globflags, $chanstr, $password, $comment);
chop;
# dealing with empty lines
next if (/^[\w]*$/);
# dealing with comments
if (/^\#/) {
# script version
if (/^\# version = (.+)/) { $friends_file_version = $1; }
# timestamp
if (/^\# written = ([0-9]+)/) { $friends_file_written = $1; }
next;
}
# split by '%'
my @fields = split("%", $_);
foreach my $field (@fields) {
if ($field =~ /^handle=(.*)$/) { $handle = $1; }
elsif ($field =~ /^hosts=(.*)$/) { $hosts = $1; }
elsif ($field =~ /^globflags=(.*)$/) { $globflags = $1; }
elsif ($field =~ /^chanflags=(.*)$/) { $chanstr = $1; }
elsif ($field =~ /^password=(.*)$/) { $password = $1; }
elsif ($field =~ /^comment=(.*)$/) { $comment = $1; }
}
# handle cannot start with a digit
# skip friend if it does
next if ($handle =~ /^[0-9]/);
# if all fields were processed, and $handle is unique,
# make a friend and add it to $friends
if (is_unique_handle($handle)) {
push(@friends, new_friend($handle, $hosts, $globflags, $chanstr, $password, $comment));
} else {
Irssi::printformat(MSGLEVEL_CRAP, 'friends_duplicate', $handle);
}
}
close(F);
# if everything's ok -- print a message
Irssi::printformat(MSGLEVEL_CRAP, 'friends_loaded', scalar(@friends), $friendfile);
} else {
# whoops, bail out, but do not clear the friendlist.
Irssi::print("Cannot load $friendfile");
}
}
# void cmd_loadfriends($data, $server, $channel)
# handles /loadfriends [file]
sub cmd_loadfriends {
my ($file) = split(/ +/, $_[0]);
load_friends($file);
}
# void save_friends($auto)
# saving friends to file
sub save_friends {
my ($auto, $inputfile) = @_;
local *F;
my $friendfile = undef;
my $backup_suffix = Irssi::settings_get_str('friends_backup_suffix');
$backup_suffix = "." . time if ($backup_suffix eq '');
if (defined $inputfile) {
$friendfile = replace_home($inputfile);
} else {
$friendfile = replace_home(Irssi::settings_get_str('friends_file'));
}
$friendfile = $default_friends_file unless (defined $friendfile);
my $backupfile = $friendfile . $backup_suffix;
my $tmpfile = $friendfile . ".tmp" . time;
# be sane
my $old_umask = umask(077);
if (!defined open(F, ">", $tmpfile)) {
Irssi::print("Couldn't open $tmpfile for writing");
return 0;
}
# write script's version and update corresponding variable
$friends_file_version = $friends_version;
print(F "# version = $friends_file_version\n");
# write current unixtime and update corresponding variable
$friends_file_written = time;
print(F "# written = $friends_file_written\n");
# go through all entries
for (my $idx = 0; $idx < @friends; ++$idx) {
# get friend's channels, corresponding flags and delay values
# then put them as c,f,d fields into @chanstr
my @chanstr = ();
foreach my $chan (get_friends_channels($idx)) {
$chan =~ s/\%//g;
push(@chanstr, $chan.",".(get_friends_flags($idx, $chan)).",".
(get_friends_delay($idx, $chan)));
}
# write the actual line
print(F join("%",
"handle=".get_handbyidx($idx),
"hosts=".(join(" ", get_friends_hosts($idx, $friends_PLAIN_HOSTS))),
"globflags=".(get_friends_flags($idx, undef)),
"chanflags=".(join(" ", @chanstr)),
"password=".$friends[$idx]->{password},
"comment=".$friends[$idx]->{comment},
"\n"));
}
# done.
close(F);
rename($friendfile, $backupfile) if (Irssi::settings_get_bool('friends_backup_friendlist'));
rename($tmpfile, $friendfile);
Irssi::printformat(MSGLEVEL_CRAP, 'friends_saved', scalar(@friends), $friendfile) unless ($auto);
# restore umask
umask($old_umask);
}
# void cmd_savefriends($data, $server, $channel)
# handles /savefriends [filename]
sub cmd_savefriends {
my ($file) = split(/ +/, $_[0]);
eval {
save_friends(0, $file);
};
Irssi::print("Saving friendlist failed: $?") if ($?);
}
# void event_setup_saved($config, $auto)
# calls save_friends to save friendslist while saving irssi's setup
# (if friends_autosave is turned on)
sub event_setup_saved {
my ($config, $auto) = @_;
return unless (Irssi::settings_get_bool('friends_autosave'));
eval {
save_friends($auto);
};
Irssi::print("Saving friendlist failed: $?") if ($?);
}
# void event_setup_reread($config)
# calls load_friends() while setup is re-readed
# (if friends_autosave is turned on)
sub event_setup_reread {
load_friends() if (Irssi::settings_get_bool('friends_autosave'));
}
# int calculate_delay($idx, $chan)
# calculates delay
sub calculate_delay {
my ($idx, $chan) = @_;
my $delay = get_friends_delay($idx, $chan);
my $min = Irssi::settings_get_int('friends_delay_min');
my $max = Irssi::settings_get_int('friends_delay_max');
# lazy man's sanity checks :-P
$min = $default_delay_min if $min < 0;