From a33b597377dfc921aa29f033617c099bbeb4a0ac Mon Sep 17 00:00:00 2001 From: jbosecker <> Date: Tue, 26 Nov 2013 10:17:10 +0000 Subject: [PATCH] Added Pushover module to contrib. git-svn-id: https://svn.fhem.de/fhem/trunk@4290 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/contrib/70_Pushover.pm | 179 ++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 fhem/contrib/70_Pushover.pm diff --git a/fhem/contrib/70_Pushover.pm b/fhem/contrib/70_Pushover.pm new file mode 100644 index 000000000..dc2d3b6b7 --- /dev/null +++ b/fhem/contrib/70_Pushover.pm @@ -0,0 +1,179 @@ +############################################## +# +# A module to send notifications to Pushover. +# +# written 2013 by Johannes B +# +############################################## +# +# Definition: +# define Pushover +# +# Example: +# define Pushover1 Pushover 12345 6789 +# +# +# You can send messages via the following command: +# set msg <msg> <device> <priority> <sound> +# +# Example: +# set Pushover1 msg 'Titel' 'This is a text.' '' 0 '' +# +# Explantation: +# If device is empty, the message will be sent to all devices. +# If sound is empty, the default setting in the app will be used. +# +# +# For further documentation of these parameters: +# https://pushover.net/api + + +package main; + +use HTTP::Request; +use LWP::UserAgent; +use IO::Socket::SSL; +use utf8; + +sub Pushover_Initialize($$) +{ + my ($hash) = @_; + $hash->{DefFn} = "Pushover_Define"; + $hash->{SetFn} = "Pushover_Set"; +} + +sub Pushover_Define($$) +{ + my ($hash, $def) = @_; + + my @args = split("[ \t]+", $def); + + if (int(@args) < 2) + { + return "Invalid number of arguments: define <name> Pushover <token> <user>"; + } + + my ($name, $type, $token, $user) = @args; + + $hash->{STATE} = 'Initialized'; + + if(defined($token) && defined($user)) + { + $hash->{Token} = $token; + $hash->{User} = $user; + return undef; + } + else + { + return "Token and/or user missing."; + } +} + +sub Pushover_Set($@) +{ + my ($hash, $name, $cmd, @args) = @_; + + if($cmd eq 'msg') + { + return Pushover_Set_Message($hash, @args); + } +} + +sub Pushover_Set_Message +{ + my $hash = shift; + + my $attr = join(" ", @_); + $attr =~ /(".*"|'.*')\s*(".*"|'.*')\s*(".*"|'.*')\s*(\d+)\s*(".*"|'.*')\s*$/s; + + my $title = $1; + my $message = $2; + my $device = $3; + my $priority = $4; + my $sound = $5; + + if($title =~ /^['"](.*)['"]$/s) + { + $title = $1; + } + + if($message =~ /^['"](.*)['"]$/s) + { + $message = $1; + } + + if($device =~ /^['"](.*)['"]$/s) + { + $device = $1; + } + + if($priority =~ /^['"](.*)['"]$/) + { + $priority = $1; + } + + if($sound =~ /^['"](.*)['"]$/s) + { + $sound = $1; + } + + if(($title ne "") && ($message ne "")) + { + my $body = "token=" . $hash->{Token} . "&" . + "user=" . $hash->{User} . "&" . + "title=" . $title . "&" . + "message=" . $message; + + if ($device ne "") + { + $body = $body . "&" . "device=" . $device; + } + + if ($priority ne "") + { + $body = $body . "&" . "priority=" . $priority; + } + + if ($sound ne "") + { + $body = $body . "&" . "sound=" . $sound; + } + + return Pushover_HTTP_Call($hash, $body); + } + else + { + return "Syntax: set <Pushover_device> msg <title> <msg> <device> <priority> <sound>"; + } +} + +sub Pushover_HTTP_Call($$) +{ + my ($hash,$body) = @_; + + my $client = LWP::UserAgent->new(); + + my $req = HTTP::Request->new(POST => "https://api.pushover.net/1/messages.json"); + $req->header('Content-Type' => 'application/x-www-form-urlencoded'); + $req->content($body); + + my $response = $client->request($req); + + if($response) + { + if ($response->is_error) + { + return "Error: " . $response->status_line; + } + else + { + return "OK"; + } + } + else + { + return "Status: " . $response->status_line; + } +} + +1;