|
#!/usr/bin/perl |
|
|
|
# Copyright (c) 2014 Sunil Nimmagadda <[email protected]> |
|
# |
|
# Permission to use, copy, modify, and distribute this software for any |
|
# purpose with or without fee is hereby granted, provided that the above |
|
# copyright notice and this permission notice appear in all copies. |
|
# |
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
|
|
use 5.20.1; |
|
use warnings; |
|
use experimental 'signatures'; |
|
|
|
use HTTP::Tiny; |
|
|
|
my $hdrs_ref = {}; |
|
|
|
sub on_data ($id) { $hdrs_ref->{$id} = {}; } |
|
|
|
sub on_dataline ($id, $line) { |
|
smtpd::filter_api_writeln($id, $line); |
|
my $h = $hdrs_ref->{$id}; |
|
|
|
$h->{is_done} = 1 if $line =~ /^$/; # end of headers |
|
return if $h->{is_done}; |
|
|
|
$h->{From} = $line if $line =~ /^From:/; |
|
$h->{To} = $line if $line =~ /^To:/; |
|
$h->{Cc} = $line if $line =~ /^Cc:/; |
|
$h->{Subject} = $line if $line =~ /^Subject:/; |
|
} |
|
|
|
sub on_commit ($id) { |
|
my $mailaddr = '[email protected]'; |
|
my $h = $hdrs_ref->{$id}; |
|
my $subject = $h->{Subject}; |
|
my $from = $h->{From}; |
|
|
|
pushover( $from, $subject ) if grep { /$mailaddr/ } ( $h->{To}, $h->{Cc} ); |
|
delete $hdrs_ref->{$id}; |
|
} |
|
|
|
sub on_disconnect ($id) { delete $hdrs_ref->{$id}; } |
|
|
|
sub on_reset ($id) { delete $hdrs_ref->{$id}; } |
|
|
|
sub on_rollback ($id) { delete $hdrs_ref->{$id}; } |
|
|
|
sub pushover ($from, $subject) { |
|
my $token = 'xxxxxx'; |
|
my $user = 'yyyyyy'; |
|
my $url = 'https://api.pushover.net/1/messages.json'; |
|
|
|
my $pid = fork; |
|
if ( !defined $pid ) { |
|
say 'Pushover: fork failed'; |
|
return; |
|
} |
|
return if $pid; # parent exits |
|
|
|
my $res = HTTP::Tiny->new->post_form( |
|
$url, |
|
{ |
|
token => $token, |
|
user => $user, |
|
message => $subject, |
|
title => $from |
|
} |
|
); |
|
|
|
say "Pushover: ", $res->{status}, " ", $res->{reason}; |
|
} |