Skip to content

Instantly share code, notes, and snippets.

@zeha
Last active August 29, 2015 14:00
Show Gist options
  • Save zeha/11387703 to your computer and use it in GitHub Desktop.
Save zeha/11387703 to your computer and use it in GitHub Desktop.
irssi script to forward messages while away to mqtt
use Irssi;
use POSIX;
use JSON;
use vars qw(%IRSSI);
%IRSSI = (
name => "awayforward",
license => "Public Domain",
);
sub sig_printtext {
my ($dest, $text, $stripped) = @_;
my $opt = MSGLEVEL_HILIGHT;
$opt = MSGLEVEL_HILIGHT|MSGLEVEL_MSGS;
if ($dest->{server} && $dest->{server}->{usermode_away}) {
if(
($dest->{level} & ($opt)) &&
($dest->{level} & MSGLEVEL_NOHILIGHT) == 0
) {
my %json_hash = ('message' => $stripped, 'target' => $dest->{target});
my $json_message = encode_json \%json_hash;
my $pid = fork();
unless (defined($pid)) {
Irssi::print("awayforward: fork failed");
return 0;
}
if ($pid > 0) {
# parent
Irssi::print("forked to $pid");
Irssi::pidwait_add($pid);
} else {
open(MQTT, "| mosquitto_pub -t irssi/away -h mqttbroker -s");
print MQTT $json_message."\n";
close(MQTT);
POSIX::_exit(0);
}
}
}
}
Irssi::signal_add_last('print text', 'sig_printtext');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment