From 7ade0017c73e3d31e06a7ee1940487c2cbc4f9bd Mon Sep 17 00:00:00 2001 From: knilch Date: Sun, 18 Mar 2018 18:31:20 +0100 Subject: [PATCH] Per-channel modifiers and follow-ups Unify follow-up and modifier handling by providing channel-specific modifiers. Now, can subscribe to updates of more than one channel. To get all updates of a channel, send #channel,all To get the next lines, send #channel, If no modifier is used, default to "numfollowups" config option. Signed-off-by: knilch --- telegram.cfg | 2 +- telegram.pl | 57 +++++++++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/telegram.cfg b/telegram.cfg index a6e51eb..979e2c1 100644 --- a/telegram.cfg +++ b/telegram.cfg @@ -13,7 +13,7 @@ debug: 0 matchPattern: . #number of backlog lines sent when pattern matches in channel: backlog: 3 -#number of follow-up lines sent after sending a message +#default modifier after sending to a channel (all or number of follow-ups) numfollowup: 0 #base-url for posted images #baseURL: http://hostname/~user/i2t diff --git a/telegram.pl b/telegram.pl index 77b5a2c..459b2b6 100644 --- a/telegram.pl +++ b/telegram.pl @@ -51,14 +51,13 @@ my $last_poll = 0; my $last_ts = 0; -my $followup = 0; my $offset = -1; my %servers; # maps channels to servers +my %chanmod; # maps modifiers to channels my $last_target; my $last_server; my $last_msg_target; my $last_msg_server; -my $sendchan = undef; my $log; sub telegram_getupdates($); @@ -73,18 +72,18 @@ ($;$) (my $chan, my $text) = split(/ /, $text, 2); $chan =~ s/^\@//; - my $modifier; if ($chan =~ m/^(#.+),(.+)$/) { $chan = $1; - $modifier = $2; + $chanmod{$chan} = $2; } + my $query = ($chan !~ m/^#/); my $cmd = "msg ${chan} ".$text; print $cmd if ($debug); my $srv = $servers{$chan}; if (!defined($srv)) { my @targets = (); - if ($chan =~ m/^#/) { + if (!$query) { @targets = Irssi::channels(); } else { @targets = Irssi::queries(); @@ -100,19 +99,15 @@ ($;$) if (defined $srv) { if (length($text)) { $srv->command($cmd); - if ($chan =~ m/^#/) { - $followup = $numfollowup; - } else { - $followup = 0; + my $fupstr = ""; + if ((!$query) && ($chanmod{$chan} ne 'all')) { + $chanmod{$chan} = $numfollowup; + $fupstr = " (f'up: ".$numfollowup.")"; } - telegram_send_message($user, "->${chan}".($followup?" (f'up: ".$followup.")":"")); + telegram_send_message($user, "->${chan}".$fupstr); } else { - telegram_send_message($user, "${chan} on $srv->{tag} ".(defined($modifier)?"(${modifier}) ":"")."selected as new target."); - if ($modifier eq "all") { - $sendchan = $chan; - } else { - $sendchan = undef; - } + my $modstr = defined($chanmod{$chan}) ? " (f'up: ".$chanmod{$chan}.")" : ""; + telegram_send_message($user, "${chan} on $srv->{tag}".$modstr." selected as new target."); } $last_target = $chan; $last_server = $srv; @@ -161,12 +156,12 @@ ($;$) my $cmd = "msg ${target} ".$text; print $cmd if ($debug); $server->command($cmd); + my $fupstr = ""; if ($target =~ m/^#/) { - $followup = $numfollowup; - } else { - $followup = 0; + $chanmod{$target} = $numfollowup; + $fupstr = " (f'up: ".$numfollowup.")"; } - telegram_send_message($user, "->${target}".($followup?" (f'up: ".$followup.")":"")); + telegram_send_message($user, "->${target}".$fupstr); } } @@ -400,6 +395,20 @@ ($) telegram_https("/bot${token}/getUpdates?offset=".($offset + 1)."&timeout=${longpoll}", undef, 1, undef); } +sub telegram_wantfup($) { + my $target = shift; + my $mod = $chanmod{$target}; + if (defined($mod)) { + if ($mod eq 'all') { + return 1; + } elsif ($mod > 0) { + $chanmod{$target} = $mod - 1; + return $mod; + } + } + return 0; +} + sub telegram_signal { my ($server, $msg, $nick, $address, $target) = @_; @@ -422,8 +431,7 @@ sub telegram_signal { if ( !$query && !grep(/$matchPattern/, $msg) - && ((!defined($sendchan)) || $sendchan ne $target) - && (!$followup || (!defined($last_target)) || $target ne $last_target) + && !telegram_wantfup($target) ) { if ($backlog) { push @{$log->{$target}}, $text; @@ -432,9 +440,8 @@ sub telegram_signal { return; } - if ($followup && defined($last_target) && $target eq $last_target) { - $text = '('.$followup.') '.$text unless $query; - $followup--; + if (defined($chanmod{$target})) { + $text = '('.$chanmod{$target}.') '.$text; } my $quiet = undef;