Last active
November 17, 2019 13:23
-
-
Save zarya/dbaf3a611ac4a7ad14077c935641fe69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Irssi; | |
use Irssi::Irc; | |
use vars qw($VERSION %IRSSI); | |
use LWP::UserAgent; | |
use IO::Socket::SSL qw(); | |
use HTTP::Request::Common; | |
use URI::Escape; | |
use JSON; | |
$VERSION = "0.2"; | |
my $last_gui_action = time(); | |
%IRSSI = ( | |
authors => "zarya", | |
contact => "zarya\@gigafreak.net", | |
name => "hass", | |
description => "Sends notifications when user has been idle long enough", | |
license => "GPLv2", | |
url => "" | |
); | |
#my $api = new LWP::UserAgent; | |
my $api = LWP::UserAgent->new( | |
ssl_opts => { | |
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, | |
verify_hostname => 0 | |
} | |
); | |
my $response; | |
# some niceties | |
$api->agent("irssi-hass/$VERSION"); | |
$api->env_proxy; | |
$api->timeout(5); # too long timeouts would foul up IRC experience | |
# Configuration settings and default values. | |
Irssi::settings_add_bool("hass", "hass_enable", 0); | |
Irssi::settings_add_int("hass", "hass_idletime", 1); | |
Irssi::settings_add_str('hass', 'hass_webhook', ''); | |
Irssi::settings_add_bool('hass', 'hass_away_only', '1'); | |
# create a title for the hass message | |
sub create_title { | |
my ($type, $target) = @_; | |
if ($type eq 'private') { | |
return "irssi: private message from $target"; | |
} elsif ($type eq 'public') { | |
return "irssi: public hilight in $target"; | |
} else { | |
die "Received unknown message type \"$type\" in create_title\n"; | |
} | |
} | |
# check if we're actually allowed to send a notification | |
sub allowed_to_send { | |
my $server = shift; | |
# filter to check for away status | |
if (Irssi::settings_get_bool('hass_away_only')) { | |
return 0 unless ($server->{usermode_away}); | |
} | |
return 1; | |
} | |
# check if api token and target user are set to something | |
sub sanity_check { | |
if (Irssi::settings_get_str('hass_webhook') =~ m/^\s*$/) { | |
Irssi::print("hass_webhook has not been set, disabling hass."); | |
Irssi::settings_set_bool('hass_enabled', 0); | |
return 0; | |
} else { | |
return 1; | |
} | |
} | |
# send the request to hass | |
sub send_to_hass { | |
my ($type, $target, $message) = @_; | |
# drop message if empty line | |
return if ($message =~ m/^\s*$/); | |
my %request = ( | |
'title' => &create_title($type, $target), | |
'target' => $target, | |
'type' => $type, | |
'message' => $message, | |
); | |
my $req = HTTP::Request->new( 'POST', Irssi::settings_get_str('hass_webhook') ); | |
$req->content_type('application/json'); | |
$req->content( to_json( \%request ) ); | |
$response = $api->request( $req ); | |
if ($response->is_error()) { | |
Irssi::print("Hass returned an error: " . $response->status_line()); | |
} | |
} | |
sub event_handler { | |
my ($server, $msg, $nick, $address, $target) = @_; | |
return unless (Irssi::settings_get_bool('hass_enable')); | |
return unless (&allowed_to_send($server)); | |
return unless (&sanity_check); | |
if(is_idle() == 1 || !Irssi::settings_get_bool('hass_away_only') ){ | |
if ($target) { | |
# public message | |
send_to_hass('public', $target, "<$nick> $msg") if ($msg =~ m/$server->{'nick'}/); | |
} else { | |
# private message | |
send_to_hass('private', $nick, $msg); | |
} | |
} | |
} | |
sub key_pressed { | |
$last_gui_action = time(); | |
} | |
sub is_idle { | |
if ($last_gui_action + Irssi::settings_get_int("hass_idletime") * 60 < time()) { | |
return 1; | |
} | |
return 0; | |
} | |
Irssi::signal_add_last('message public', 'event_handler'); | |
Irssi::signal_add_last('message private', 'event_handler'); | |
Irssi::signal_add_last("gui key pressed", 'key_pressed'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment