]> git.99rst.org Git - irssi-scripts.irssi.org.git/commitdiff
[lastspoke] #569 and utf8
authorbw1 <redacted>
Sun, 17 Mar 2019 09:24:58 +0000 (10:24 +0100)
committerbw1 <redacted>
Tue, 16 Apr 2019 14:05:24 +0000 (16:05 +0200)
scripts/lastspoke.pl

index 261e66e2aee5edae5834787421d415fd46575bd0..a6b4c8941582cefbe4cd25ad86ddcd24c3389d9d 100644 (file)
 # Triggers on !lastspoke <nick>, !seen <nick> and !lastseen <nick>
 # 
 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();
git clone https://git.99rst.org/PROJECT