From: bw1 Date: Sun, 17 Mar 2019 09:24:58 +0000 (+0100) Subject: [lastspoke] #569 and utf8 X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=0377cb17f3dd30e9ed411c848322a20eaf6ba4b8;p=irssi-scripts.irssi.org.git [lastspoke] #569 and utf8 --- diff --git a/scripts/lastspoke.pl b/scripts/lastspoke.pl index 261e66e..a6b4c89 100644 --- a/scripts/lastspoke.pl +++ b/scripts/lastspoke.pl @@ -20,11 +20,14 @@ # Triggers on !lastspoke , !seen and !lastseen # use strict; +use utf8; +use Encode qw/decode encode/; use Irssi; use Irssi::Irc; +use JSON::PP; use vars qw($VERSION %IRSSI); -$VERSION = "0.3"; +$VERSION = "0.4"; %IRSSI = ( authors => 'Sander Smeenk', contact => 'irssi@freshdot.net', @@ -37,6 +40,8 @@ $VERSION = "0.3"; # Storage for the data. my %lasthash; +my $filename=Irssi::get_irssi_dir().'/lastspoke.json'; + # Calculates the difference between two unix times and returns # a string like '15d 23h 42m 15s ago.' sub calcDiff { @@ -119,6 +124,7 @@ sub on_part { # Only act on channels we are supposed to act on (settings_get_str) sub on_public { my ($server, $msg, $nick, $addr, $target) = @_; + utf8::decode($msg); $target = $nick if ( ! $target ); $nick = $server->{'nick'} if ($nick =~ /^#/); @@ -142,7 +148,9 @@ sub on_public { $lasthash{lc($nick)}{'words'} = "$nick last queried information about " . $parts[1] . " on $target"; if (exists $lasthash{lc($parts[1])}) { - $server->command("MSG $target " . $lasthash{lc($parts[1])}{'words'} . " " . calcDiff($lasthash{lc($parts[1])}{'last'})); + $server->command("MSG $target " . + to_term_enc($lasthash{lc($parts[1])}{'words'}) . + " " . calcDiff($lasthash{lc($parts[1])}{'last'})); } else { $server->command("MSG $target I don't know anything about " . $parts[1]); } @@ -153,6 +161,37 @@ sub on_public { } } +# encode the words to term_charset +sub to_term_enc { + my ($words)= @_; + my $charset= Irssi::settings_get_str('term_charset'); + return encode($charset, $words); +} + +# write the memory to disk +sub save { + my $fa; + open($fa, '>', $filename); + print $fa encode_json( \%lasthash ); + close($fa); +} + +# read form the disk to memory +sub load { + my $fi; + if (-e $filename) { + local $/; + open($fi, '<', $filename); + %lasthash = %{ decode_json <$fi> }; + close($fi); + } +} + +# hook for unload, /quit +sub UNLOAD { + save(); +} + # Put hooks on events Irssi::signal_add_last("message public", "on_public"); Irssi::signal_add_last("message own_public", "on_public"); @@ -163,3 +202,5 @@ Irssi::signal_add_last("message nick", "on_nick"); # Add setting Irssi::settings_add_str("lastspoke", "lastspoke_channels", '%s'); + +load();