# 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',
# 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 {
# 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 =~ /^#/);
$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]);
}
}
}
+# 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");
# Add setting
Irssi::settings_add_str("lastspoke", "lastspoke_channels", '%s');
+
+load();