-
Notifications
You must be signed in to change notification settings - Fork 1
/
matterircd_complete.pl
1496 lines (1267 loc) · 55.7 KB
/
matterircd_complete.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
#
# For the full matterircd_complete experience, your matterircd.toml
# should have SuffixContext=true, ThreadContext="mattermost", and
# Unicode=true.
#
# Add it to ~/.irssi/scripts/autorun, or:
#
# /script load ~/.irssi/scripts/matterircd_complete.pl
# /set matterircd_complete_networks <...>
#
# NOTE: It is important to set which networks to enable plugin for per
# above ^.
#
# Bind message/thread ID completion to a key to make it easier to
# reply to threads:
#
# /bind ^G /message_thread_id_search
#
# Also bind to insert nicknames:
#
# /bind ^F /nicknames_search
#
# (Or pick your own shortcut keys to bind to).
#
# Then:
#
# Ctrl+g - Insert latest message/thread ID.
# Ctrl+c - Abort inserting message/thread ID. Also clears existing.
#
# @@+TAB to tab auto-complete message/thread ID.
# @ +TAB to tab auto-complete IRC nick. Active users appear first.
#
# By default, message/thread IDs are shortened from 26 characters to
# first few (default 5). It is also grayed out to try reduce noise and
# make it easier to read conversations. To disable this use:
#
# /set matterircd_complete_shorten_message_thread_id 0
#
# Use the dump commands to show the contents of the cache:
#
# /matterircd_complete_msgthreadid_cache_dump
# /matterircd_complete_nick_cache_dump
#
# (You can bind these to keys).
#
# To increase or decrease the size of the cache, use:
#
# /set matterircd_complete_message_thread_id_cache_size 50
# /set matterircd_complete_nick_cache_size 20
#
# To ignore specific nicks in autocomplete:
#
# /set matterircd_complete_nick_ignore somebot anotherbot
use strict;
use warnings;
use experimental 'smartmatch';
require Irssi::TextUI;
require Irssi;
# Enable for debugging purposes only.
# use Data::Dumper;
our $VERSION = '2.00-dev'; # 0db0537f7a35c998
our %IRSSI = (
name => 'Matterircd Tab Auto Complete',
description => 'Adds tab completion for Matterircd message threads',
authors => 'Haw Loeung',
contact => 'hloeung/Freenode',
license => 'GPL',
);
my $KEY_CTRL_C = 3;
my $KEY_CTRL_U = 21;
my $KEY_ESC = 27;
my $KEY_RET = 13;
my $KEY_SPC = 32;
my $KEY_B = 66;
my $KEY_O = 79;
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_networks', '');
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_nick_ignore', '');
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_channel_dont_ignore', '');
sub _wi_print {
my ($wi, $msg) = @_;
if ($wi) {
$wi->print($msg);
} else {
Irssi::print($msg);
}
}
#==============================================================================
my %color_config;
# Taken from nickcolor_expando irssi script
# These are all the colors, sorted by main color class
# To display and select colors you want/want to avoid based on your background, use /cubes_text from cubes.pl
my @all_colors = (
qw[20 30 40 50 04 66 0C 61 60 67 6L], # RED
qw[37 3D 36 4C 46 5C 56 6C 6J 47 5D 6K 6D 57 6E 5E 4E 4K 4J 5J 4D 5K 6R], # ORANGE
qw[3C 4I 5I 6O 6I 06 4O 5O 3U 0E 5U 6U 6V 6P 6Q 6W 5P 4P 4V 4W 5W 4Q 5Q 5R 6Y 6X], # YELLOW
qw[26 2D 2C 3I 3O 4U 5V 2J 3V 3P 3J 5X], # YELLOW-GREEN
qw[16 1C 2I 2U 2O 1I 1O 1V 1P 02 0A 1U 2V 4X], # GREEN
qw[1D 1J 1Q 1W 1X 2Y 2S 2R 3Y 3Z 3S 3R 2K 3K 4S 5Z 5Y 4R 3Q 2Q 2X 2W 3X 3W 2P 4Y], # GREEN-TURQUOIS
qw[17 1E 1L 1K 1R 1S 03 1M 1N 1T 0B 1Y 1Z 2Z 4Z], # TURQUOIS
qw[28 2E 18 1F 19 1G 1A 1B 1H 2N 2H 09 3H 3N 2T 3T 2M 2G 2A 2F 2L 3L 3F 4M 3M 3G 29 4T 5T], # LIGHT-BLUE
qw[22 33 44 0D 45 5B 6A 5A 5H 3B 4H 3A 4G 39 4F 6S 6T 5L 5N], # VIOLET
qw[21 32 42 53 63 52 43 34 35 55 65 6B 4B 4A 48 5G 6H 5M 6M 6N], # PINK
qw[38 31 05 64 54 41 51 62 69 68 59 5F 6F 58 49 6G], # ROSE
qw[11 12 23 25 24 13 14 01 15 2B 4N], # DARK-BLUE
qw[7A 00 10 7B 7C 7D 7E 7G 7F], # DARK-GRAY
qw[7H 7I 27 7K 7J 08 7L 3E 7O 7Q 7N 7M 7P], # GRAY
qw[7S 7T 7R 4L 7W 7U 7V 5S 07 7X 6Z 0F], # LIGHT-GRAY
);
# These are the colors unwanted with a dark theme
my @dark_theme_unwanted = (
qw[11 12 23 25 24 13 14 01 15 2B 4N], # DARK-BLUE
qw[7A 00 10 7B 7C 7D 7E 7G 7F], # DARK-GRAY
qw[7H 7I 27 7K 7J 08 7L 3E 7O 7Q 7N 7M 7P], # GRAY
qw[7S 7T 7R 4L 7W 7U 7V 5S 07 7X 6Z 0F], # LIGHT-GRAY
);
# These are the colors unwanted with a light theme
my @solarized_light_theme_unwanted = (
qw[4U 4V 4W 4X 4Y 4Z 5U 5V 5W 5X 5Y 5Z 6U 6V 6W 6X 6Y 6Z 6O 6P 6Q 6R 6S 6T], # too light flashy colors
qw[7T 7U 7V 7W 7X 07 7R 7S 7T 7U 7V 7W 7X 7B 7C 7D 7E 7F 7H 7I 7J 7K 7M 7N 7O 7P], # too light grayscales
qw[5S 4L 7Q 5T 4T 4M 5M 6M 6N 1Z 2Z 1Y 2X 2W 3X 3W 1U 2V 1X 2Y 3V 3Z 3Y 2U], # hand picked too light + redundant
);
Irssi::settings_add_int('matterircd_complete', 'matterircd_complete_thread_id_color', -1);
# Default color theme to none, so we use all available colors.
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_thread_id_color_theme', '');
Irssi::settings_add_bool('matterircd_complete', 'matterircd_complete_thread_id_allow_bold', 0);
Irssi::settings_add_bool('matterircd_complete', 'matterircd_complete_thread_id_allow_italic', 0);
Irssi::settings_add_bool('matterircd_complete', 'matterircd_complete_thread_id_allow_underline', 0);
# Allowed colors will be applied first
# These can be a list of 20 30 40 50 5F colors, or without spaces 203040505F
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_thread_id_allowed_colors', '');
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_thread_id_unwanted_colors', '');
$color_config{'color_theme'} = '';
$color_config{'allowed_colors'} = '';
$color_config{'unwanted_colors'} = '';
# Initialize
my @thread_id_selected_colors = ();
# Rely on message/thread IDs stored in message cache so we can shorten
# to save on screen real-estate.
Irssi::settings_add_int('matterircd_complete', 'matterircd_complete_shorten_message_thread_id', 5);
Irssi::settings_add_bool('matterircd_complete', 'matterircd_complete_shorten_message_thread_id_hide_prefix', 1);
Irssi::settings_add_str('matterircd_complete', 'matterircd_complete_override_reply_prefix', '↪');
# Taken from nickcolor_expando irssi script and adapted for our use
sub xcolor_to_irssi {
# Set to foreground xcolor
my $c = "X".$_[0];
my @ext_colour_off = (
'.', '-', ',',
'+', "'", '&',
);
if ($c =~ /^(X)(?:0([[:xdigit:]])|([1-6])(?:([0-9])|([a-z]))|7([a-x]))$/i) {
my $bg = $1 eq 'x';
my $col = defined $2 ? hex $2
: defined $6 ? 232 + (ord lc $6) - (ord 'a')
: 16 + 36 * ($3 - 1) + (defined $4 ? $4 : 10 + (ord lc $5) - (ord 'a'));
if ($col < 0x10) {
my $chr = chr $col + ord '0';
return "\cD" . ($bg ? "/$chr" : "$chr/");
}
else {
return "\cD" . $ext_colour_off[($col - 0x10) / 0x50 + $bg * 3] . chr (($col - 0x10) % 0x50 - 1 + ord '0');
}
} else {
return $c;
}
}
sub get_thread_format {
my ($str) = @_;
my @nums = (0..9,'a'..'z');
my $chr=join('',@nums);
my %nums = map { $nums[$_] => $_ } 0..$#nums;
my $n = 0;
$str = lc $str;
foreach ($str =~ /[$chr]/g) {
$n += $nums{$_} * 36;
}
my @colors = @thread_id_selected_colors;
my $color_count = @colors;
# We have normal, bold, italic, underline
my $allow_bold = Irssi::settings_get_bool('matterircd_complete_thread_id_allow_bold');
my $allow_italic = Irssi::settings_get_bool('matterircd_complete_thread_id_allow_italic');
my $allow_underline = Irssi::settings_get_bool('matterircd_complete_thread_id_allow_underline');
my @classes_prepend;
push @classes_prepend, "\x02" if $allow_bold;
push @classes_prepend, "\x1d" if $allow_italic;
push @classes_prepend, "\x1f" if $allow_underline;
my $classes = 1 + @classes_prepend;
$n = $n % $color_count*$classes;
my $random = $n;
my $prepend = "";
if ($classes == 4 and $n >= $color_count*3) {
$n -= $color_count*3;
$prepend = $classes_prepend[2];
} elsif ($classes ge 3 and $n >= $color_count*2) {
$n -= $color_count*2;
$prepend = $classes_prepend[1];
} elsif ($classes ge 2 and $n >= $color_count) {
$n -= $color_count;
$prepend = $classes_prepend[0];
}
$n = $colors[$n-1];
return $n, $prepend;
}
sub thread_color {
my ($str) = @_;
my ($n, $prepend) = get_thread_format($str);
# Pick the color in the allowed_color list.
# n should be comprised between 1 and the array length.
$n = xcolor_to_irssi($n);
$n = "$prepend\x03$n";
return $n;
}
sub cmd_matterircd_complete_thread_id_get_color {
my ($data, $server, $wi) = @_;
my ($color, $prepend) = get_thread_format($_[0]);
my $n = xcolor_to_irssi($color);
_wi_print($wi, "Thread color for $prepend\x03$n$_[0]\x0f is $color");
}
Irssi::command_bind('matterircd_complete_thread_id_get_color', 'cmd_matterircd_complete_thread_id_get_color');
sub update_msgthreadid {
my($server, $msg, $nick, $address, $target) = @_;
return unless Irssi::settings_get_int('matterircd_complete_shorten_message_thread_id');
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
# Replace tabs with spaces.
# https://github.com/irssi/irssi/issues/1499
$msg =~ s/\t/ /g;
my $prefix = '';
my $msgthreadid = '';
my $msgpostid = '';
my $reply_prefix = Irssi::settings_get_str('matterircd_complete_override_reply_prefix');
my $thread_m_style = 0;
if ($msg =~ s/\[(->|↪)?\@\@([0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43})(?:,\@\@([0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}))?\]/\@\@PLACEHOLDER\@\@/) {
$prefix = $reply_prefix ? $reply_prefix : $1 if $1;
$msgthreadid = $2;
$msgpostid = $3 ? $3 : '';
} elsif ($msg =~ s/\[(->|↪)?([0-9a-f]{3})(?:,([0-9a-f]{3}))?\]/\@\@PLACEHOLDER\@\@/) {
$prefix = $reply_prefix ? $reply_prefix : $1 if $1;
$msgthreadid = $2;
$msgpostid = $3 ? $3 : '';
$thread_m_style = 1;
}
if (not $msgthreadid) {
Irssi::signal_continue($server, $msg, $nick, $address, $target);
return
}
my $thread_color = Irssi::settings_get_int('matterircd_complete_thread_id_color');
my $post_color = '';
if ($thread_color == -1) {
$thread_color = thread_color($msgthreadid);
if ($msgpostid ne '') {
$post_color = thread_color($msgpostid);
}
} else {
$thread_color = "\x03${thread_color}";
}
# Show that message is reply to a thread. (backwards compatibility
# when matterircd doesn't show reply)
if ((not $prefix) && ($msg =~ /\(re \@.*\)/)) {
$prefix = $reply_prefix;
}
if (not Irssi::settings_get_bool('matterircd_complete_shorten_message_thread_id_hide_prefix')) {
$prefix = "${prefix}\@\@";
}
my $len = Irssi::settings_get_int('matterircd_complete_shorten_message_thread_id');
if (($len < 25) && ($nick ne 'mattermost') && ($thread_m_style != 1)) {
# Shorten to length configured. We use unicode ellipsis (...)
# here to both allow word selection to just select parts of
# the message/thread ID when copying & pasting and save on
# screen real estate.
$msgthreadid = substr($msgthreadid, 0, $len) . '…';
if ($msgpostid ne '') {
$msgpostid = substr($msgpostid, 0, $len) . '…';
}
}
if ($msgpostid eq '') {
$msg =~ s/\@\@PLACEHOLDER\@\@/${thread_color}[${prefix}${msgthreadid}]\x0f/;
} elsif ($post_color ne '') {
$msg =~ s/\@\@PLACEHOLDER\@\@/${thread_color}[${prefix}${msgthreadid},${post_color}${msgpostid}${thread_color}]\x0f/;
} else {
$msg =~ s/\@\@PLACEHOLDER\@\@/${thread_color}[${prefix}${msgthreadid},${msgpostid}]\x0f/;
}
Irssi::signal_continue($server, $msg, $nick, $address, $target);
}
Irssi::signal_add_last('message irc action', 'update_msgthreadid');
Irssi::signal_add_last('message irc notice', 'update_msgthreadid');
Irssi::signal_add_last('message private', 'update_msgthreadid');
Irssi::signal_add_last('message public', 'update_msgthreadid');
sub cache_store {
my ($cache_ref, $item, $cache_size) = @_;
return unless $item ne '';
my $changed = 0;
if ((@$cache_ref[0]) && (@$cache_ref[0] eq $item)) {
return $changed;
}
$changed = 1;
# Insert item in cache early to ensure there isn't a read then
# update/modify race where the item doesn't currently exist in the
# cache.
unshift(@$cache_ref, $item);
# We want to reduce duplicates by removing them currently in the
# per-channel cache. But as a trade off in favor of
# speed/performance, rather than traverse the entire per-channel
# cache, we cap/limit it.
my $limit = 16;
my $max = ($#$cache_ref < $limit)? $#$cache_ref : $limit;
for my $i (1 .. $max) {
if ((@$cache_ref[$i]) && (@$cache_ref[$i] eq $item)) {
splice(@$cache_ref, $i, 1);
}
}
if (($cache_size > 0) && (scalar(@$cache_ref) > $cache_size)) {
pop(@$cache_ref);
}
return $changed;
}
#==============================================================================
# Adds tab-complete or keybinding insertion of messages/threads
# seen. This makes it easier for replying directly to threads in
# Mattermost or creating new threads.
my %MSGTHREADID_CACHE;
Irssi::settings_add_int('matterircd_complete', 'matterircd_complete_message_thread_id_cache_size', 32);
sub cmd_matterircd_complete_msgthreadid_cache_dump {
my ($data, $server, $wi) = @_;
if (not $data) {
return unless ref $wi and ($wi->{type} eq 'CHANNEL' or $wi->{type} eq 'QUERY');
}
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
my $channel = $data ? $data : $wi->{name};
# Remove leading and trailing whitespace.
$channel =~ tr/ //d;
_wi_print($wi, "${channel}: Message/Thread ID cache");
if ((not exists($MSGTHREADID_CACHE{$channel})) || (scalar @{$MSGTHREADID_CACHE{$channel}} == 0)) {
_wi_print($wi, "${channel}: Empty");
return;
}
foreach my $msgthread_id (@{$MSGTHREADID_CACHE{$channel}}) {
_wi_print($wi, "${channel}: ${msgthread_id}");
}
_wi_print($wi, "${channel}: Total: " . scalar @{$MSGTHREADID_CACHE{$channel}});
};
Irssi::command_bind('matterircd_complete_msgthreadid_cache_dump', 'cmd_matterircd_complete_msgthreadid_cache_dump');
my $MSGTHREADID_CACHE_SEARCH_ENABLED = 0;
my $MSGTHREADID_CACHE_INDEX = 0;
sub cmd_message_thread_id_search {
my ($data, $server, $wi) = @_;
return unless Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
return unless ref $wi and ($wi->{type} eq 'CHANNEL' or $wi->{type} eq 'QUERY');
return unless exists($MSGTHREADID_CACHE{$wi->{name}});
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
$MSGTHREADID_CACHE_SEARCH_ENABLED = 1;
my $msgthreadid = $MSGTHREADID_CACHE{$wi->{name}}[$MSGTHREADID_CACHE_INDEX];
$MSGTHREADID_CACHE_INDEX += 1;
if ($MSGTHREADID_CACHE_INDEX > $#{$MSGTHREADID_CACHE{$wi->{name}}}) {
# Cycle back to the start.
$MSGTHREADID_CACHE_INDEX = 0;
}
if ($msgthreadid) {
# Save input text.
my $input = Irssi::parse_special('$L');
# Remove existing thread.
$input =~ s/^@@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}|[0-9a-f]{3}) //;
# Insert message/thread ID from cache.
Irssi::gui_input_set_pos(0);
Irssi::gui_input_set("\@\@${msgthreadid} ${input}");
}
};
Irssi::command_bind('message_thread_id_search', 'cmd_message_thread_id_search');
my $ESC_PRESSED = 0;
my $O_PRESSED = 0;
sub signal_gui_key_pressed_msgthreadid {
my ($key) = @_;
return unless $MSGTHREADID_CACHE_SEARCH_ENABLED;
my $server = Irssi::active_server();
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
if (($key == $KEY_RET) || ($key == $KEY_CTRL_U)) {
$MSGTHREADID_CACHE_INDEX = 0;
$MSGTHREADID_CACHE_SEARCH_ENABLED = 0;
$ESC_PRESSED = 0;
$O_PRESSED = 0;
}
# Cancel/abort, so remove thread stuff.
elsif ($key == $KEY_CTRL_C) {
my $input = Irssi::parse_special('$L');
# Remove the Ctrl+C character.
$input =~ tr///d;
my $pos = 0;
if ($input =~ s/^(@@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}|[0-9a-f]{3}) )//) {
$pos = Irssi::gui_input_get_pos() - length($1);
}
# We also want to move the input position back one for Ctrl+C
# char.
$pos = $pos > 0 ? $pos - 1 : 0;
# Replace the text in the input box with our modified version,
# then move cursor positon to where it was without the
# message/thread ID.
Irssi::gui_input_set($input);
Irssi::gui_input_set_pos($pos);
$MSGTHREADID_CACHE_INDEX = 0;
$MSGTHREADID_CACHE_SEARCH_ENABLED = 0;
$ESC_PRESSED = 0;
$O_PRESSED = 0;
}
# For 'down arrow', it's a sequence of ESC + O + B.
elsif ($key == $KEY_ESC) {
$ESC_PRESSED = 1;
}
elsif ($key == $KEY_O) {
$O_PRESSED = 1;
}
elsif ($key == $KEY_B && $O_PRESSED && $ESC_PRESSED) {
$MSGTHREADID_CACHE_INDEX = 0;
$MSGTHREADID_CACHE_SEARCH_ENABLED = 0;
$ESC_PRESSED = 0;
$O_PRESSED = 0;
}
# Reset sequence on any other keys pressed.
elsif ($O_PRESSED || $ESC_PRESSED) {
$ESC_PRESSED = 0;
$O_PRESSED = 0;
}
};
Irssi::signal_add_last('gui key pressed', 'signal_gui_key_pressed_msgthreadid');
# We keep the posts to threads IDs in a separate cache so it's added
# to the end for autocompletion so it's less likely that we reply to
# the wrong thread.
my %MSGTHREAD_POST_ID_CACHE;
sub signal_complete_word_msgthread_id {
my ($complist, $window, $word, $linestart, $want_space) = @_;
return unless Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
return if (substr($word, 0, 1) eq '@' and substr($word, 0, 2) ne '@@');
return unless $window->{active} and ($window->{active}->{type} eq 'CHANNEL' || $window->{active}->{type} eq 'QUERY');
return unless exists($MSGTHREADID_CACHE{$window->{active}->{name}});
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$window->{active_server}->{chatnet}};
if (substr($word, 0, 2) eq '@@') {
$word = substr($word, 2);
}
# Main msg post and thread IDs in the main cache.
foreach my $msgthread_id (@{$MSGTHREADID_CACHE{$window->{active}->{name}}}) {
if ($msgthread_id =~ /^\Q$word\E/) {
push(@$complist, "\@\@${msgthread_id}");
}
}
if (exists($MSGTHREAD_POST_ID_CACHE{$window->{active}->{name}})) {
# Store posts to threads IDs in a separate cache.
foreach my $msgthread_id (@{$MSGTHREAD_POST_ID_CACHE{$window->{active}->{name}}}) {
if ($msgthread_id =~ /^\Q$word\E/) {
push(@$complist, "\@\@${msgthread_id}");
}
}
}
};
Irssi::signal_add_last('complete word', 'signal_complete_word_msgthread_id');
my $MSGTHREADID_CACHE_STATS = 0;
my $MSGTHREAD_POST_ID_CACHE_STATS = 0;
sub cache_msgthreadid {
my($server, $msg, $nick, $address, $target) = @_;
return unless Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
my @msgids = ();
my @msgpost_ids = ();
my @ignore_nicks = split(/\s+/, Irssi::settings_get_str('matterircd_complete_nick_ignore'));
# Ignore nicks configured to be ignored such as bots.
if (grep(/^$nick$/, @ignore_nicks)) {
# But not if the channel is in matterircd_complete_channel_dont_ignore.
my @channel_dont_ignore = split(/\s+/, Irssi::settings_get_str('matterircd_complete_channel_dont_ignore'));
if ($target !~ @channel_dont_ignore) {
return;
}
}
# Mattermost message/thread IDs.
if ($msg =~ /\[(?:->|↪)?\@\@([0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43})(?:,\@\@([0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}))?\]/) {
my $msgthreadid = $1;
my $msgpostid = $2 ? $2 : '';
if ($msgpostid ne '') {
push(@msgpost_ids, $msgpostid);
}
push(@msgids, $msgthreadid);
}
# matterircd generated 3-letter hexadecimal.
elsif ($msg =~ /(?:^\[(?:->|↪)?([0-9a-f]{3})\])|(?:\[(?:->|↪)?([0-9a-f]{3})\]\s*$)/) {
push(@msgids, $1 ? $1 : $2);
}
# matterircd generated 3-letter hexadecimal replying to threads.
elsif ($msg =~ /\[(?:->|↪)?([0-9a-f]{3})(?:,([0-9a-f]{3}))?\]/) {
my $msgthreadid = $1;
my $msgpostid = $2 ? $2 : '';
if ($msgpostid ne '') {
push(@msgpost_ids, $msgpostid);
}
push(@msgids, $msgthreadid);
}
# matterircd generated 3-letter hexadecimal replying to threads (legacy).
elsif ($msg =~ /(?:^\[[0-9a-f]{3}->([0-9a-f]{3})\])|(?:\[[0-9a-f]{3}->([0-9a-f]{3})\]\s*$)/) {
push(@msgids, $1 ? $1 : $2);
}
else {
return;
}
my $key;
if (substr($target, 0, 1) eq '#') {
# It's a channel, so use $target
$key = $target;
} else {
# It's a private query so use $nick
$key = $nick
}
my $cache_size = Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
for my $msgid (@msgids) {
if (cache_store(\@{$MSGTHREADID_CACHE{$key}}, $msgid, $cache_size)) {
$MSGTHREADID_CACHE_INDEX = 0;
stats_increment(\$MSGTHREADID_CACHE_STATS);
}
}
# Search to supplementary cache to see if replies to posts IDs
# match. This is mainly for reactions and such.
for my $msgpostid (@msgpost_ids) {
if (cache_store(\@{$MSGTHREAD_POST_ID_CACHE{$key}}, $msgpostid, $cache_size)) {
stats_increment(\$MSGTHREAD_POST_ID_CACHE_STATS);
}
}
}
Irssi::signal_add('message irc action', 'cache_msgthreadid');
Irssi::signal_add('message irc notice', 'cache_msgthreadid');
Irssi::signal_add('message private', 'cache_msgthreadid');
Irssi::signal_add('message public', 'cache_msgthreadid');
Irssi::settings_add_bool('matterircd_complete', 'matterircd_complete_reply_msg_thread_id_at_start', 1);
sub signal_message_own_public_msgthreadid {
my($server, $msg, $target) = @_;
return unless Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
if ($msg !~ /^@@((?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43})|(?:[0-9a-f]{3}))/) {
return;
}
my $msgthreadid = $1;
# matterircd generated 3-letter hexadecimal.
my $thread_m_style = 0;
if ($msg =~ /^@@(?:[0-9a-f]{3}) /) {
$thread_m_style = 1;
}
my $cache_size = Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
if (cache_store(\@{$MSGTHREADID_CACHE{$target}}, $msgthreadid, $cache_size)) {
$MSGTHREADID_CACHE_INDEX = 0;
stats_increment(\$MSGTHREADID_CACHE_STATS);
}
my $thread_color = Irssi::settings_get_int('matterircd_complete_thread_id_color');
if ($thread_color == -1) {
$thread_color = thread_color($msgthreadid);
} else {
$thread_color = "\x03${thread_color}";
}
my $len = Irssi::settings_get_int('matterircd_complete_shorten_message_thread_id');
if (($len < 25) && ($thread_m_style != 1)) {
# Shorten to length configured. We use unicode ellipsis (...)
# here to both allow word selection to just select parts of
# the message/thread ID when copying & pasting and save on
# screen real estate.
$msgthreadid = substr($msgthreadid, 0, $len) . "…";
}
my $reply_prefix = Irssi::settings_get_str('matterircd_complete_override_reply_prefix');
if (Irssi::settings_get_bool('matterircd_complete_reply_msg_thread_id_at_start')) {
$msg =~ s/^@@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}) /${thread_color}[${reply_prefix}${msgthreadid}]\x0f /;
$msg =~ s/^@@[0-9a-f]{3} /${thread_color}[${reply_prefix}${msgthreadid}]\x0f /;
} else {
$msg =~ s/^@@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}) //;
$msg =~ s/^@@[0-9a-f]{3} //;
$msg =~ s/$/ ${thread_color}[${reply_prefix}${msgthreadid}]\x0f/;
}
Irssi::signal_continue($server, $msg, $target);
};
Irssi::signal_add_last('message own_public', 'signal_message_own_public_msgthreadid');
sub signal_message_own_private {
my($server, $msg, $target, $orig_target) = @_;
return unless Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
if ($msg !~ /^@@([0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}|[0-9a-f]{3})/) {
return;
}
my $msgthreadid = $1;
# matterircd generated 3-letter hexadecimal.
my $thread_m_style = 0;
if ($msg =~ /^@@(?:[0-9a-f]{3}) /) {
$thread_m_style = 1;
}
my $cache_size = Irssi::settings_get_int('matterircd_complete_message_thread_id_cache_size');
if (cache_store(\@{$MSGTHREADID_CACHE{$target}}, $msgthreadid, $cache_size)) {
$MSGTHREADID_CACHE_INDEX = 0;
stats_increment(\$MSGTHREADID_CACHE_STATS);
}
my $thread_color = Irssi::settings_get_int('matterircd_complete_thread_id_color');
if ($thread_color == -1) {
$thread_color = thread_color($msgthreadid);
} else {
$thread_color = "\x03${thread_color}";
}
my $len = Irssi::settings_get_int('matterircd_complete_shorten_message_thread_id');
if (($len < 25) && ($thread_m_style != 1)) {
# Shorten to length configured. We use unicode ellipsis (...)
# here to both allow word selection to just select parts of
# the message/thread ID when copying & pasting and save on
# screen real estate.
$msgthreadid = substr($msgthreadid, 0, $len) . "…";
}
my $reply_prefix = Irssi::settings_get_str('matterircd_complete_override_reply_prefix');
if (Irssi::settings_get_bool('matterircd_complete_reply_msg_thread_id_at_start')) {
$msg =~ s/^@@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}) /${thread_color}[${reply_prefix}${msgthreadid}]\x0f /;
$msg =~ s/^@@[0-9a-f]{3} /${thread_color}[${reply_prefix}${msgthreadid}]\x0f /;
} else {
$msg =~ s/^@@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}) //;
$msg =~ s/^@@[0-9a-f]{3} //;
$msg =~ s/$/ ${thread_color}[${reply_prefix}${msgthreadid}]\x0f/;
}
Irssi::signal_continue($server, $msg, $target, $orig_target);
};
Irssi::signal_add_last('message own_private', 'signal_message_own_private');
#==============================================================================
# Adds tab-complete or keybinding insertion of nicknames for users in
# the current channel. Similar to irssi's builtin, recently active
# users/nicks will be first in the completion list.
my %NICKNAMES_CACHE;
Irssi::settings_add_int('matterircd_complete', 'matterircd_complete_nick_cache_size', 16);
sub cmd_matterircd_complete_nick_cache_dump {
my ($data, $server, $wi) = @_;
if (not $data) {
return unless ref $wi and ($wi->{type} eq 'CHANNEL' or $wi->{type} eq 'QUERY');
}
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
my $channel = $data ? $data : $wi->{name};
# Remove leading and trailing whitespace.
$channel =~ tr/ //d;
_wi_print($wi, "${channel}: Nicknames cache");
if ((not exists($NICKNAMES_CACHE{$channel})) || (scalar @{$NICKNAMES_CACHE{$channel}} == 0)) {
_wi_print($wi,"${channel}: Empty");
return;
}
foreach my $nick (@{$NICKNAMES_CACHE{$channel}}) {
_wi_print($wi, "${channel}: ${nick}");
}
_wi_print($wi, "${channel}: Total: " . scalar @{$NICKNAMES_CACHE{$channel}});
};
Irssi::command_bind('matterircd_complete_nick_cache_dump', 'cmd_matterircd_complete_nick_cache_dump');
sub signal_complete_word_nicks {
my ($complist, $window, $word, $linestart, $want_space) = @_;
return if substr($word, 0, 2) eq '@@';
return unless $window->{active} and $window->{active}->{type} eq 'CHANNEL';
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$window->{active_server}->{chatnet}};
if (substr($word, 0, 1) eq '@') {
$word = substr($word, 1);
}
my $compl_char = Irssi::settings_get_str('completion_char');
my $own_nick = $window->{active}->{ownnick}->{nick};
my @ignore_nicks = split(/\s+/, Irssi::settings_get_str('matterircd_complete_nick_ignore'));
# We need to store the results in a temporary array so we can
# sort.
my @tmp;
foreach my $cur ($window->{active}->nicks()) {
my $nick = $cur->{nick};
# Ignore our own nick.
if ($nick eq $own_nick) {
next;
}
# Ignore nicks configured to be ignored such as bots.
elsif (grep(/^$nick$/, @ignore_nicks)) {
next;
}
# Only those matching partial word.
elsif ($nick =~ /^\Q$word\E/i) {
push(@tmp, $nick);
}
}
@tmp = sort @tmp;
foreach my $nick (@tmp) {
# Only add completion character on line start.
if (not $linestart) {
push(@$complist, "\@${nick}${compl_char}");
} else {
push(@$complist, "\@${nick}");
}
}
return unless exists($NICKNAMES_CACHE{$window->{active}->{name}});
# We use the populated cache so frequent and active users in
# channel come before those idling there. e.g. In a channel where
# @barryp talks more often, it will come before @barry-m. We also
# want to make sure users are still in channel for those still in
# the cache.
foreach my $nick (reverse @{$NICKNAMES_CACHE{$window->{active}->{name}}}) {
my $nick_compl;
# Only add completion character on line start.
if (not $linestart) {
$nick_compl = "\@${nick}${compl_char}";
} else {
$nick_compl = "\@${nick}";
}
# Skip over if nick is already first in completion list.
if ((scalar(@{$complist}) > 0) and ($nick_compl eq @{$complist}[0])) {
next;
}
# Only add to completion list if user/nick is online and in channel.
elsif (grep(/^$nick$/, @tmp)) {
# Only add completion character on line start.
if (not $linestart) {
unshift(@$complist, "\@${nick}${compl_char}");
} else {
unshift(@$complist, "\@${nick}");
}
}
}
};
Irssi::signal_add('complete word', 'signal_complete_word_nicks');
my $NICKNAMES_CACHE_STATS = 0;
sub cache_ircnick {
my($server, $msg, $nick, $address, $target) = @_;
return unless Irssi::settings_get_int('matterircd_complete_nick_cache_size');
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
my $cache_size = Irssi::settings_get_int('matterircd_complete_nick_cache_size');
my @ignore_nicks = split(/\s+/, Irssi::settings_get_str('matterircd_complete_nick_ignore'));
# Ignore nicks configured to be ignored such as bots.
if ($nick !~ @ignore_nicks) {
if (cache_store(\@{$NICKNAMES_CACHE{$target}}, $nick, $cache_size)) {
stats_increment(\$NICKNAMES_CACHE_STATS);
}
}
}
Irssi::signal_add('message irc action', 'cache_ircnick');
Irssi::signal_add('message irc notice', 'cache_ircnick');
Irssi::signal_add('message public', 'cache_ircnick');
sub signal_message_own_public_nicks {
my($server, $msg, $target) = @_;
return unless Irssi::settings_get_int('matterircd_complete_nick_cache_size');
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
if ($msg !~ /^@([^@ \t:,\)]+)/) {
return;
}
my $nick = $1;
my $cache_size = Irssi::settings_get_int('matterircd_complete_nick_cache_size');
# We want to make sure that the nick or user is still online and
# in the channel.
my $wi = $server->window_item_find($target);
if (not defined $wi) {
return;
}
foreach my $cur ($wi->nicks()) {
if ($nick eq $cur->{nick}) {
if (cache_store(\@{$NICKNAMES_CACHE{$target}}, $nick, $cache_size, 1)) {
stats_increment(\$NICKNAMES_CACHE_STATS);
}
last;
}
}
};
Irssi::signal_add_last('message own_public', 'signal_message_own_public_nicks');
my @NICKNAMES_CACHE_SEARCH;
my $NICKNAMES_CACHE_SEARCH_ENABLED = 0;
my $NICKNAMES_CACHE_INDEX = 0;
sub cmd_nicknames_search {
my ($data, $server, $wi) = @_;
return unless ref $wi and $wi->{type} eq 'CHANNEL';
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
my $own_nick = $wi->{ownnick}->{nick};
my @ignore_nicks = split(/\s+/, Irssi::settings_get_str('matterircd_complete_nick_ignore'));
@NICKNAMES_CACHE_SEARCH = ();
foreach my $cur ($wi->nicks()) {
my $nick = $cur->{nick};
# Ignore our own nick.
if ($nick eq $own_nick) {
next;
}
# Ignore nicks configured to be ignored such as bots.
elsif (grep(/^$nick$/, @ignore_nicks)) {
next;
}
push(@NICKNAMES_CACHE_SEARCH, $nick);
}
@NICKNAMES_CACHE_SEARCH = sort @NICKNAMES_CACHE_SEARCH;
if (exists($NICKNAMES_CACHE{$wi->{name}})) {
# We use the populated cache so frequent and active users in
# channel come before those idling there. e.g. In a channel
# where @barryp talks more often, it will come before
# @barry-m. We also want to make sure users are still in
# channel for those still in the cache.
foreach my $nick (reverse @{$NICKNAMES_CACHE{$wi->{name}}}) {
# Skip over if nick is already first in completion list.
if ((scalar(@NICKNAMES_CACHE_SEARCH) > 0) and ($nick eq $NICKNAMES_CACHE_SEARCH[0])) {
next;
}
# Only add to completion list if user/nick is online and
# in channel.
elsif (grep(/^$nick$/, @NICKNAMES_CACHE_SEARCH)) {
unshift(@NICKNAMES_CACHE_SEARCH, $nick);
}
}
}
$NICKNAMES_CACHE_SEARCH_ENABLED = 1;
my $nickname = $NICKNAMES_CACHE_SEARCH[$NICKNAMES_CACHE_INDEX];
$NICKNAMES_CACHE_INDEX += 1;
if ($NICKNAMES_CACHE_INDEX > $#NICKNAMES_CACHE_SEARCH) {
# Cycle back to the start.
$NICKNAMES_CACHE_INDEX = 0;
}
if ($nickname) {
# Save input text.
my $input = Irssi::parse_special('$L');
my $compl_char = Irssi::settings_get_str('completion_char');
# Remove any existing nickname and insert one from the cache.
my $msgid = "";
if ($input =~ s/^(\@\@(?:[0-9a-z]{26}|\$[0-9A-Za-z\-_\.]{43}|[0-9a-f]{3}) )//) {
$msgid = $1;
}
$input =~ s/^\@[^${compl_char}]+$compl_char //;
Irssi::gui_input_set_pos(0);
Irssi::gui_input_set("${msgid}\@${nickname}${compl_char} ${input}");
}
};
Irssi::command_bind('nicknames_search', 'cmd_nicknames_search');
sub signal_gui_key_pressed_nicks {
my ($key) = @_;
return unless $NICKNAMES_CACHE_SEARCH_ENABLED;
my $server = Irssi::active_server();
my %chatnets = map { $_ => 1 } split(/\s+/, Irssi::settings_get_str('matterircd_complete_networks'));
return unless exists $chatnets{'*'} || exists $chatnets{$server->{chatnet}};
if (($key == $KEY_RET) || ($key == $KEY_CTRL_U)) {
$NICKNAMES_CACHE_INDEX = 0;
$NICKNAMES_CACHE_SEARCH_ENABLED = 0;
@NICKNAMES_CACHE_SEARCH = ();
}
# Cancel/abort, so remove current nickname.
elsif ($key == $KEY_CTRL_C) {
my $input = Irssi::parse_special('$L');
# Remove the Ctrl+C character.
$input =~ tr///d;
my $compl_char = Irssi::settings_get_str('completion_char');
my $pos = 0;
if ($input =~ s/^(\@[^${compl_char}]+$compl_char )//) {
$pos = Irssi::gui_input_get_pos() - length($1);
}
# We also want to move the input position back one for Ctrl+C
# char.
$pos = $pos > 0 ? $pos - 1 : 0;
# Replace the text in the input box with our modified version,
# then move cursor positon to where it was without the
# current nickname.
Irssi::gui_input_set($input);
Irssi::gui_input_set_pos($pos);
$NICKNAMES_CACHE_INDEX = 0;
$NICKNAMES_CACHE_SEARCH_ENABLED = 0;
@NICKNAMES_CACHE_SEARCH = ();
}