|
| 1 | +use strict; |
| 2 | + |
| 3 | +use Irssi qw(signal_add_last); |
| 4 | + |
| 5 | +our $VERSION = '1.00'; |
| 6 | +our %IRSSI = ( |
| 7 | + name => 'Matterircd Message Thread Tab Complete', |
| 8 | + description => 'Adds tab complettion for Matterircd message threads', |
| 9 | + authors => 'Haw Loeung', |
| 10 | + contact => 'haw.loeung@canonical.com', |
| 11 | + license => 'GPL', |
| 12 | +); |
| 13 | + |
| 14 | +our %CACHE_MSG_THREAD_ID; |
| 15 | + |
| 16 | +signal_add_last 'complete word' => sub { |
| 17 | + my ($complist, $window, $word, $linestart, $want_space) = @_; |
| 18 | + |
| 19 | + my $wi = Irssi::active_win()->{active}; |
| 20 | + return unless ref $wi and $wi->{type} eq 'CHANNEL'; |
| 21 | + |
| 22 | + # Only message/thread IDs at the start. |
| 23 | + if ($word !~ /^@@/) { |
| 24 | + return; |
| 25 | + } |
| 26 | + $word =~ s/^@@//; |
| 27 | + |
| 28 | + if (not exists($CACHE_MSG_THREAD_ID{$wi->{name}})) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + foreach my $msgthread_id (@{$CACHE_MSG_THREAD_ID{$wi->{name}}}) { |
| 33 | + if ($msgthread_id =~ /^\Q$word\E/) { |
| 34 | + push(@$complist, "\@\@$msgthread_id"); |
| 35 | + } |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +signal_add_last 'message public' => sub { |
| 40 | + my($server, $msg, $nick, $address, $target) = @_; |
| 41 | + |
| 42 | + if ($msg !~ '@@([0-9a-z]{26})') { |
| 43 | + return; |
| 44 | + } |
| 45 | + my $msgid = $1; |
| 46 | + |
| 47 | + if (not exists($CACHE_MSG_THREAD_ID{$target})) { |
| 48 | + $CACHE_MSG_THREAD_ID{$target} = (); |
| 49 | + my $cache_ref = \@{$CACHE_MSG_THREAD_ID{$target}}; |
| 50 | + unshift(@$cache_ref, $msgid); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + my $cache_ref = \@{$CACHE_MSG_THREAD_ID{$target}}; |
| 55 | + |
| 56 | + # Message / thread IDs are added at the start of the array so most |
| 57 | + # recent would be first. We also want to avoid duplicates. |
| 58 | + if (@$cache_ref[0] ne $msgid) { |
| 59 | + unshift(@$cache_ref, $msgid); |
| 60 | + } |
| 61 | + |
| 62 | + # Maximum cache elements to store per channel. |
| 63 | + # XXX: Make this value configurable. |
| 64 | + if (scalar(@$cache_ref) > 20) { |
| 65 | + pop(@$cache_ref); |
| 66 | + } |
| 67 | +}; |
0 commit comments