From efee87342f5c9700860c2a075966b70df2fc0724 Mon Sep 17 00:00:00 2001 From: Arne Baeumler Date: Mon, 14 Dec 2015 22:11:10 +0100 Subject: [PATCH] added configuration file parser --- notify/xmpp/xmpp-send-img.pl | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 notify/xmpp/xmpp-send-img.pl diff --git a/notify/xmpp/xmpp-send-img.pl b/notify/xmpp/xmpp-send-img.pl new file mode 100755 index 0000000..5b8e422 --- /dev/null +++ b/notify/xmpp/xmpp-send-img.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl + +use Config::General; +use Net::XMPP; +use LWP::UserAgent; +use File::Basename; +use Data::Dumper; +use strict; + +my ($tojid,$filename) = @ARGV; + +my $conf = Config::General->new("$ENV{HOME}/.xmpprc"); +my %config = $conf->getall; + +my $client = new Net::XMPP::Client(); +$client->SetCallBacks( iq => \&iq_cb ); +$client->AddNamespace( + ns => 'urn:xmpp:http:upload', + tag => 'slot', + xpath => { + Get => { path => 'get/text()' }, + Put => { path => 'put/text()' }, + } +); + +my $status = $client->Connect( + hostname => $config{server}, + port => $config{port}, + tls => 0, +); +die("EE - connect($config{server}:$config{port}): failed or connection not allowed\n") unless($status); + + +my ($auth_status,$auth_error) = eval {$client->AuthSend( + username => $config{username}, + password => $config{password}, + resource => $config{resource} +)}; +die("EE - Authorization failed: $auth_status - $auth_error\n") unless($auth_status eq 'ok'); + + +my $fileName = basename($filename); +my $fileSize = -s $filename; + +my $query = qq{ + + + $fileName + $fileSize + + +}; + +my $id = $client->SendWithID($query); +$client->Process(1); + +sub iq_cb { + my ($id,$iq) = @_; + my $query = $iq->GetQuery(); + + my $imageData; + open(IMG,"<$filename"); + binmode IMG; + read(IMG,$imageData,$fileSize); + close(IMG); + + my $ua = LWP::UserAgent->new(ssl_opts => { + verify_hostname => 0, + }); + $ua->timeout(600); + my $putRequest = HTTP::Request->new(PUT => $query->GetPut()); + $putRequest->content($imageData); + my $response = $ua->request($putRequest); + + if($response->is_success) { + $client->MessageSend( + to => $tojid, + body => $query->GetGet() + ); + } else { + print $response->decoded_content . "\n"; + } +} + +$client->Disconnect();