whitespace, braces, quotes

This commit is contained in:
Isaac Connor 2017-06-02 11:39:35 -04:00
parent 915450de13
commit be5b93a64e
1 changed files with 751 additions and 898 deletions

View File

@ -77,51 +77,36 @@ use constant EVENT_PATH => ($Config{ZM_DIR_EVENTS}=~m|/|)
logInit();
logSetSignal();
if ( $Config{ZM_OPT_UPLOAD} )
{
if ( $Config{ZM_OPT_UPLOAD} ) {
# Comment these out if you don't have them and don't want to upload
# or don't want to use that format
if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "zip" )
{
if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq 'zip' ) {
require Archive::Zip;
import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
}
else
{
} else {
require Archive::Tar;
}
if ( $Config{ZM_UPLOAD_PROTOCOL} eq "ftp" )
{
if ( $Config{ZM_UPLOAD_PROTOCOL} eq 'ftp' ) {
require Net::FTP;
}
else
{
} else {
require Net::SFTP::Foreign;
}
}
if ( $Config{ZM_OPT_EMAIL} )
{
if ( $Config{ZM_NEW_MAIL_MODULES} )
{
if ( $Config{ZM_OPT_EMAIL} ) {
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
require MIME::Lite;
require Net::SMTP;
}
else
{
} else {
require MIME::Entity;
}
}
if ( $Config{ZM_OPT_MESSAGE} )
{
if ( $Config{ZM_NEW_MAIL_MODULES} )
{
if ( $Config{ZM_OPT_MESSAGE} ) {
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
require MIME::Lite;
require Net::SMTP;
}
else
{
} else {
require MIME::Entity;
}
}
@ -134,7 +119,7 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $delay = $Config{ZM_FILTER_EXECUTE_INTERVAL};
my $event_id = 0;
my $filter_parm = "";
my $filter_parm = '';
my $version = 0;
#
@ -145,7 +130,7 @@ GetOptions(
) or pod2usage(-exitstatus => -1);
if ( $version ) {
print ZoneMinder::Base::ZM_VERSION . "\n";
print ZoneMinder::Base::ZM_VERSION . '\n';
exit(0);
}
@ -158,35 +143,28 @@ chdir( EVENT_PATH );
my $dbh = zmDbConnect();
if ( $filter_parm )
{
if ( $filter_parm ) {
Info( "Scanning for events using filter '$filter_parm'\n" );
}
else
{
} else {
Info( "Scanning for events\n" );
}
if ( !$filter_parm )
{
if ( !$filter_parm ) {
sleep( START_DELAY );
}
my $filters;
my $last_action = 0;
while( 1 )
{
while( 1 ) {
my $now = time;
if ( ($now - $last_action) > $Config{ZM_FILTER_RELOAD_DELAY} )
{
if ( ($now - $last_action) > $Config{ZM_FILTER_RELOAD_DELAY} ) {
Debug( "Reloading filters\n" );
$last_action = $now;
$filters = getFilters( $filter_parm );
}
foreach my $filter ( @$filters )
{
foreach my $filter ( @$filters ) {
checkFilter( $filter );
}
@ -196,43 +174,35 @@ while( 1 )
sleep( $delay );
}
sub getFilters
{
sub getFilters {
my $filter_name = shift;
my @filters;
my $sql = "SELECT * FROM Filters WHERE";
if ( $filter_name )
{
$sql .= " Name = ? and";
my $sql = 'SELECT * FROM Filters WHERE';
if ( $filter_name ) {
$sql .= ' Name = ? and';
} else {
$sql .= ' Background = 1 and';
}
else
{
$sql .= " Background = 1 and";
}
$sql .= "( AutoArchive = 1
$sql .= '( AutoArchive = 1
or AutoVideo = 1
or AutoUpload = 1
or AutoEmail = 1
or AutoMessage = 1
or AutoExecute = 1
or AutoDelete = 1
) ORDER BY Name";
) ORDER BY Name';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res;
if ( $filter_name )
{
if ( $filter_name ) {
$res = $sth->execute( $filter_name )
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
}
else
{
} else {
$res = $sth->execute()
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
}
FILTER: while( my $db_filter = $sth->fetchrow_hashref() )
{
FILTER: while( my $db_filter = $sth->fetchrow_hashref() ) {
my $filter = new ZoneMinder::Filter( $$db_filter{Id}, $db_filter );
Debug( "Found filter '$db_filter->{Name}'\n" );
my $sql = $filter->Sql();
@ -244,95 +214,80 @@ sub getFilters
push( @filters, $filter );
}
$sth->finish();
Debug( "Got " . @filters . " filters" );
Debug( 'Got ' . @filters . ' filters' );
return( \@filters );
}
sub checkFilter
{
sub checkFilter {
my $filter = shift;
Debug( "Checking filter '$filter->{Name}'".
($filter->{AutoDelete}?", delete":"").
($filter->{AutoArchive}?", archive":"").
($filter->{AutoVideo}?", video":"").
($filter->{AutoUpload}?", upload":"").
($filter->{AutoEmail}?", email":"").
($filter->{AutoMessage}?", message":"").
($filter->{AutoExecute}?", execute":"").
"\n"
($filter->{AutoDelete}?', delete':'').
($filter->{AutoArchive}?', archive':'').
($filter->{AutoVideo}?', video':'').
($filter->{AutoUpload}?', upload':'').
($filter->{AutoEmail}?', email':'').
($filter->{AutoMessage}?', message':'').
($filter->{AutoExecute}?', execute':'').
'\n'
);
foreach my $event ( $filter->Execute() ) {
Debug( "Checking event $event->{Id}\n" );
my $delete_ok = !undef;
$dbh->ping();
if ( $filter->{AutoArchive} )
{
if ( $filter->{AutoArchive} ) {
Info( "Archiving event $event->{Id}\n" );
# Do it individually to avoid locking up the table for new events
my $sql = "update Events set Archived = 1 where Id = ?";
my $sql = 'update Events set Archived = 1 where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
or Error( "Can't execute '$sql': ".$sth->errstr() );
}
if ( $Config{ZM_OPT_FFMPEG} && $filter->{AutoVideo} )
{
if ( !$event->{Videoed} )
{
if ( $Config{ZM_OPT_FFMPEG} && $filter->{AutoVideo} ) {
if ( !$event->{Videoed} ) {
$delete_ok = undef if ( !generateVideo( $filter, $event ) );
}
}
if ( $Config{ZM_OPT_EMAIL} && $filter->{AutoEmail} )
{
if ( !$event->{Emailed} )
{
if ( $Config{ZM_OPT_EMAIL} && $filter->{AutoEmail} ) {
if ( !$event->{Emailed} ) {
$delete_ok = undef if ( !sendEmail( $filter, $event ) );
}
}
if ( $Config{ZM_OPT_MESSAGE} && $filter->{AutoMessage} )
{
if ( !$event->{Messaged} )
{
if ( $Config{ZM_OPT_MESSAGE} && $filter->{AutoMessage} ) {
if ( !$event->{Messaged} ) {
$delete_ok = undef if ( !sendMessage( $filter, $event ) );
}
}
if ( $Config{ZM_OPT_UPLOAD} && $filter->{AutoUpload} )
{
if ( !$event->{Uploaded} )
{
if ( $Config{ZM_OPT_UPLOAD} && $filter->{AutoUpload} ) {
if ( !$event->{Uploaded} ) {
$delete_ok = undef if ( !uploadArchFile( $filter, $event ) );
}
}
if ( $filter->{AutoExecute} )
{
if ( !$event->{Executed} )
{
if ( $filter->{AutoExecute} ) {
if ( !$event->{Executed} ) {
$delete_ok = undef if ( !executeCommand( $filter, $event ) );
}
}
if ( $filter->{AutoDelete} )
{
if ( $delete_ok )
{
if ( $filter->{AutoDelete} ) {
if ( $delete_ok ) {
Info( "Deleting event $event->{Id} from Monitor $event->{MonitorId}\n" );
# Do it individually to avoid locking up the table for new events
my $sql = "delete from Events where Id = ?";
my $sql = 'delete from Events where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
if ( ! $Config{ZM_OPT_FAST_DELETE} )
{
my $sql = "delete from Frames where EventId = ?";
if ( ! $Config{ZM_OPT_FAST_DELETE} ) {
my $sql = 'delete from Frames where EventId = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
$sql = "delete from Stats where EventId = ?";
$sql = 'delete from Stats where EventId = ?';
$sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute( $event->{Id} )
@ -340,17 +295,14 @@ $dbh->ping();
deleteEventFiles( $event->{Id}, $event->{MonitorId} );
}
}
else
{
} else {
Error( "Unable to delete event $event->{Id} as previous operations failed\n" );
}
}
}
}
sub generateVideo
{
sub generateVideo {
my $filter = shift;
my $event = shift;
my $phone = shift;
@ -362,58 +314,47 @@ sub generateVideo
my @ffmpeg_formats = split( /\s+/, $Config{ZM_FFMPEG_FORMATS} );
my $default_video_format;
my $default_phone_format;
foreach my $ffmpeg_format( @ffmpeg_formats )
{
if ( $ffmpeg_format =~ /^(.+)\*\*$/ )
{
foreach my $ffmpeg_format( @ffmpeg_formats ) {
if ( $ffmpeg_format =~ /^(.+)\*\*$/ ) {
$default_phone_format = $1;
}
elsif ( $ffmpeg_format =~ /^(.+)\*$/ )
{
} elsif ( $ffmpeg_format =~ /^(.+)\*$/ ) {
$default_video_format = $1;
}
}
if ( $phone && $default_phone_format )
{
if ( $phone && $default_phone_format ) {
$format = $default_phone_format;
}
elsif ( $default_video_format )
{
} elsif ( $default_video_format ) {
$format = $default_video_format;
}
else
{
} else {
$format = $ffmpeg_formats[0];
}
my $command = $Config{ZM_PATH_BIN}."/zmvideo.pl -e "
.$event->{Id}." -r ".$rate." -s ".$scale." -f ".$format;
my $command = join('',
$Config{ZM_PATH_BIN},
'/zmvideo.pl -e ',
.$event->{Id},
' -r ',$rate,' -s ',$scale,' -f ',$format,
);
my $output = qx($command);
chomp( $output );
my $status = $? >> 8;
if ( $status || logDebugging() )
{
if ( $status || logDebugging() ) {
Debug( "Output: $output\n" );
}
if ( $status )
{
if ( $status ) {
Error( "Video generation '$command' failed with status: $status\n" );
if ( wantarray() )
{
if ( wantarray() ) {
return( undef, undef );
}
return( 0 );
}
else
{
my $sql = "update Events set Videoed = 1 where Id = ?";
} else {
my $sql = 'update Events set Videoed = 1 where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
if ( wantarray() )
{
if ( wantarray() ) {
return( $format, $output );
}
}
@ -424,16 +365,15 @@ sub generateVideo
# Optionally an analyse image path may be returned if an analyse image exists
# If neither capture nor analyse image exists it will try to extract a frame from .mp4 file if exists
# An empty string is returned if no one from methods above works
sub generateImage
{
sub generateImage {
my $event = shift;
my $frame = shift;
my $analyse = do { @_ ? shift : 0 }; # don't return analyse image by default
my $capture_image_path = sprintf("%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg", getEventPath($event), $frame->{FrameId});
my $analyse_image_path = sprintf("%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-analyse.jpg", getEventPath($event), $frame->{FrameId}) if $analyse;
my $video_path = sprintf("%s/%d-video.mp4", getEventPath($event), $event->{Id});
my $image_path = "";
my $capture_image_path = sprintf('%s/%0'.$Config{ZM_EVENT_IMAGE_DIGITS}.'d-capture.jpg', getEventPath($event), $frame->{FrameId});
my $analyse_image_path = sprintf('%s/%0'.$Config{ZM_EVENT_IMAGE_DIGITS}.'d-analyse.jpg', getEventPath($event), $frame->{FrameId}) if $analyse;
my $video_path = sprintf('%s/%d-video.mp4', getEventPath($event), $event->{Id});
my $image_path = '';
# check if the image file exists. If the file doesn't exist and we use H264 try to extract it from .mp4 video
if ( $analyse && -r $analyse_image_path ) {
@ -441,7 +381,7 @@ sub generateImage
} elsif ( -r $capture_image_path ) {
$image_path = $capture_image_path;
} elsif ( -e $video_path ) {
if ( !system("ffmpeg -y -v 0 -i ".$video_path." -vf 'select=gte(n\\,".$frame->{FrameId}."),setpts=PTS-STARTPTS' -vframes 1 -f image2 ".$capture_image_path) ) {
if ( !system('ffmpeg -y -v 0 -i '.$video_path." -vf 'select=gte(n\\,".$frame->{FrameId}."),setpts=PTS-STARTPTS' -vframes 1 -f image2 ".$capture_image_path) ) {
$image_path = $capture_image_path;
}
}
@ -449,45 +389,40 @@ sub generateImage
}
sub uploadArchFile
{
sub uploadArchFile {
my $filter = shift;
my $event = shift;
if ( ! $Config{ZM_UPLOAD_HOST} )
{
Error( "Cannot upload archive as no upload host defined" );
if ( ! $Config{ZM_UPLOAD_HOST} ) {
Error( 'Cannot upload archive as no upload host defined' );
return( 0 );
}
my $archFile = $event->{MonitorName}.'-'.$event->{Id};
my $archImagePath = getEventPath( $event )
."/"
.'/'
.(
( $Config{ZM_UPLOAD_ARCH_ANALYSE} )
? '{*analyse,*capture}'
: '*capture'
)
.".jpg"
.'.jpg'
;
my @archImageFiles = glob($archImagePath);
my $archLocPath;
my $archError = 0;
if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "zip" )
{
if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq 'zip' ) {
$archFile .= '.zip';
$archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
my $zip = Archive::Zip->new();
Info( "Creating upload file '$archLocPath', ".int(@archImageFiles)." files\n" );
my $status = &AZ_OK;
foreach my $imageFile ( @archImageFiles )
{
foreach my $imageFile ( @archImageFiles ) {
Debug( "Adding $imageFile\n" );
my $member = $zip->addFile( $imageFile );
if ( !$member )
{
if ( !$member ) {
Error( "Unable to add image file $imageFile to zip archive $archLocPath" );
$archError = 1;
last;
@ -497,28 +432,19 @@ sub uploadArchFile
: &COMPRESSION_STORED
);
}
if ( !$archError )
{
if ( !$archError ) {
$status = $zip->writeToFileNamed( $archLocPath );
if ( $archError = ($status != &AZ_OK) )
{
if ( $archError = ($status != &AZ_OK) ) {
Error( "Zip error: $status\n " );
}
}
else
{
} else {
Error( "Error adding images to zip archive $archLocPath, not writing" );
}
}
elsif ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "tar" )
{
if ( $Config{ZM_UPLOAD_ARCH_COMPRESS} )
{
} elsif ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq 'tar' ) {
if ( $Config{ZM_UPLOAD_ARCH_COMPRESS} ) {
$archFile .= '.tar.gz';
}
else
{
} else {
$archFile .= '.tar';
}
$archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
@ -529,29 +455,23 @@ sub uploadArchFile
$Config{ZM_UPLOAD_ARCH_COMPRESS},
@archImageFiles
)
)
{
Error( "Tar error: ".Archive::Tar->error()."\n " );
) {
Error( 'Tar error: '.Archive::Tar->error()."\n " );
}
}
if ( $archError )
{
if ( $archError ) {
return( 0 );
}
else
{
if ( $Config{ZM_UPLOAD_PROTOCOL} eq "ftp" )
{
Info( "Uploading to ".$Config{ZM_UPLOAD_HOST}." using FTP\n" );
} else {
if ( $Config{ZM_UPLOAD_PROTOCOL} eq 'ftp' ) {
Info( 'Uploading to '.$Config{ZM_UPLOAD_HOST}." using FTP\n" );
my $ftp = Net::FTP->new(
$Config{ZM_UPLOAD_HOST},
Timeout=>$Config{ZM_UPLOAD_TIMEOUT},
Passive=>$Config{ZM_UPLOAD_FTP_PASSIVE},
Debug=>$Config{ZM_UPLOAD_DEBUG}
);
if ( !$ftp )
{
if ( !$ftp ) {
Error( "Can't create FTP connection: $@" );
return( 0 );
}
@ -566,13 +486,11 @@ sub uploadArchFile
or Error( "FTP - Can't upload '$archLocPath'" );
$ftp->quit()
or Error( "FTP - Can't quit" );
}
else
{
} else {
my $host = $Config{ZM_UPLOAD_HOST};
$host .= ":".$Config{ZM_UPLOAD_PORT}
$host .= ':'.$Config{ZM_UPLOAD_PORT}
if $Config{ZM_UPLOAD_PORT};
Info( "Uploading to ".$host." using SFTP\n" );
Info( 'Uploading to '.$host." using SFTP\n" );
my %sftpOptions = ( host=>$Config{ZM_UPLOAD_HOST}, user=>$Config{ZM_UPLOAD_USER} );
$sftpOptions{password} = $Config{ZM_UPLOAD_PASS}
if $Config{ZM_UPLOAD_PASS};
@ -587,8 +505,7 @@ sub uploadArchFile
if $Config{ZM_UPLOAD_DEBUG};
$sftpOptions{more} = [@more_ssh_args];
my $sftp = Net::SFTP::Foreign->new( $Config{ZM_UPLOAD_HOST}, %sftpOptions );
if ( $sftp->error )
{
if ( $sftp->error ) {
Error( "Can't create SFTP connection: ".$sftp->error );
return( 0 );
}
@ -599,7 +516,7 @@ sub uploadArchFile
or Error( "SFTP - Can't upload '$archLocPath': ".$sftp->error );
}
unlink( $archLocPath );
my $sql = "update Events set Uploaded = 1 where Id = ?";
my $sql = 'update Events set Uploaded = 1 where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
@ -608,8 +525,7 @@ sub uploadArchFile
return( 1 );
}
sub substituteTags
{
sub substituteTags {
my $text = shift;
my $filter = shift;
my $event = shift;
@ -621,8 +537,7 @@ sub substituteTags
my $need_monitor = $text =~ /%(?:MET|MEH|MED|MEW|MEN|MEA)%/;
my $monitor = {};
if ( $need_monitor )
{
if ( $need_monitor ) {
my $db_now = strftime( "%Y-%m-%d %H:%M:%S", localtime() );
my $sql = "SELECT
M.Id,
@ -656,8 +571,7 @@ sub substituteTags
my $first_alarm_frame;
my $max_alarm_frame;
my $max_alarm_score = 0;
if ( $need_images )
{
if ( $need_images ) {
my $sql = "SELECT * FROM Frames
WHERE EventId = ? AND Type = 'Alarm'
ORDER BY FrameId"
@ -666,14 +580,11 @@ sub substituteTags
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
or Fatal( "Can't execute '$sql': ".$sth->errstr() );
while( my $frame = $sth->fetchrow_hashref() )
{
if ( !$first_alarm_frame )
{
while( my $frame = $sth->fetchrow_hashref() ) {
if ( !$first_alarm_frame ) {
$first_alarm_frame = $frame;
}
if ( $frame->{Score} > $max_alarm_score )
{
if ( $frame->{Score} > $max_alarm_score ) {
$max_alarm_frame = $frame;
$max_alarm_score = $frame->{Score};
}
@ -707,87 +618,58 @@ sub substituteTags
$text =~ s/%EST%/$event->{TotScore}/g;
$text =~ s/%ESA%/$event->{AvgScore}/g;
$text =~ s/%ESM%/$event->{MaxScore}/g;
if ( $first_alarm_frame )
{
if ( $first_alarm_frame ) {
$text =~ s/%EPI1%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$first_alarm_frame->{FrameId}/g;
$text =~ s/%EPIM%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
if ( $attachments_ref && $text =~ s/%EI1%//g )
{
if ( $attachments_ref && $text =~ s/%EI1%//g ) {
my $path = generateImage( $event, $first_alarm_frame );
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>$path
}
);
push( @$attachments_ref, { type=>'image/jpeg', path=>$path } );
}
}
if ( $attachments_ref && $text =~ s/%EIM%//g )
{
if ( $attachments_ref && $text =~ s/%EIM%//g ) {
# Don't attach the same image twice
if ( !@$attachments_ref
|| ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
)
{
) {
my $path = generateImage( $event, $max_alarm_frame );
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>$path
}
);
push( @$attachments_ref, { type=>'image/jpeg', path=>$path } );
}
}
}
if ( $attachments_ref && $text =~ s/%EI1A%//g )
{
my $path = generateImage( $event, $first_alarm_frame, "analyse" );
if ( $attachments_ref && $text =~ s/%EI1A%//g ) {
my $path = generateImage( $event, $first_alarm_frame, 'analyse' );
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>$path
}
);
push( @$attachments_ref, { type=>'image/jpeg', path=>$path } );
}
}
if ( $attachments_ref && $text =~ s/%EIMA%//g )
{
if ( $attachments_ref && $text =~ s/%EIMA%//g ) {
# Don't attach the same image twice
if ( !@$attachments_ref
|| ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
)
{
my $path = generateImage( $event, $max_alarm_frame, "analyse");
) {
my $path = generateImage( $event, $max_alarm_frame, 'analyse');
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>$path
}
);
push( @$attachments_ref, { type=>'image/jpeg', path=>$path } );
}
}
}
}
if ( $attachments_ref && $Config{ZM_OPT_FFMPEG} )
{
if ( $text =~ s/%EV%//g )
{
} # end if $first_alarm_frame
if ( $attachments_ref && $Config{ZM_OPT_FFMPEG} ) {
if ( $text =~ s/%EV%//g ) {
my ( $format, $path ) = generateVideo( $filter, $event );
if ( !$format )
{
if ( !$format ) {
return( undef );
}
push( @$attachments_ref, { type=>"video/$format", path=>$path } );
}
if ( $text =~ s/%EVM%//g )
{
if ( $text =~ s/%EVM%//g ) {
my ( $format, $path ) = generateVideo( $filter, $event, 1 );
if ( !$format )
{
if ( !$format ) {
return( undef );
}
push( @$attachments_ref, { type=>"video/$format", path=>$path } );
@ -798,21 +680,18 @@ sub substituteTags
$text =~ s/%FP%/$url?view=filter&mid=$event->{MonitorId}&filter_name=$filter_name/g;
return( $text );
}
} # end subsitituteTags
sub sendEmail
{
sub sendEmail {
my $filter = shift;
my $event = shift;
if ( ! $Config{ZM_FROM_EMAIL} )
{
if ( ! $Config{ZM_FROM_EMAIL} ) {
Error( "No 'from' email address defined, not sending email" );
return( 0 );
}
if ( ! $Config{ZM_EMAIL_ADDRESS} )
{
Error( "No email address defined, not sending email" );
if ( ! $Config{ZM_EMAIL_ADDRESS} ) {
Error( 'No email address defined, not sending email' );
return( 0 );
}
@ -826,30 +705,27 @@ sub sendEmail
Info( "Sending notification email '$subject'\n" );
eval
{
if ( $Config{ZM_NEW_MAIL_MODULES} )
{
eval {
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
### Create the multipart container
my $mail = MIME::Lite->new (
From => $Config{ZM_FROM_EMAIL},
To => $Config{ZM_EMAIL_ADDRESS},
Subject => $subject,
Type => "multipart/mixed"
Type => 'multipart/mixed'
);
### Add the text message part
$mail->attach (
Type => "TEXT",
Type => 'TEXT',
Data => $body
);
### Add the attachments
foreach my $attachment ( @attachments )
{
foreach my $attachment ( @attachments ) {
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Disposition => "attachment"
Disposition => 'attachment'
);
}
### Send the Message
@ -863,19 +739,17 @@ sub sendEmail
}
if ( !$ssmtp_location ) {
Debug( "Can't find ssmtp, trying MIME::Lite->send" );
MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
MIME::Lite->send( 'smtp', $Config{ZM_EMAIL_HOST}, Timeout=>60 );
$mail->send();
} else {
### Send using SSMTP
$mail->send( 'sendmail', $ssmtp_location, $Config{ZM_EMAIL_ADDRESS} );
}
} else {
MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
MIME::Lite->send( 'smtp', $Config{ZM_EMAIL_HOST}, Timeout=>60 );
$mail->send();
}
}
else
{
} else {
my $mail = MIME::Entity->build(
From => $Config{ZM_FROM_EMAIL},
To => $Config{ZM_EMAIL_ADDRESS},
@ -884,28 +758,24 @@ sub sendEmail
Data => $body
);
foreach my $attachment ( @attachments )
{
foreach my $attachment ( @attachments ) {
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Encoding => "base64"
Encoding => 'base64'
);
}
$mail->smtpsend( Host => $Config{ZM_EMAIL_HOST}, MailFrom => $Config{ZM_FROM_EMAIL} );
}
};
if ( $@ )
{
if ( $@ ) {
Error( "Can't send email: $@" );
return( 0 );
}
else
{
} else {
Info( "Notification email sent\n" );
}
my $sql = "update Events set Emailed = 1 where Id = ?";
my $sql = 'update Events set Emailed = 1 where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
@ -914,19 +784,16 @@ sub sendEmail
return( 1 );
}
sub sendMessage
{
sub sendMessage {
my $filter = shift;
my $event = shift;
if ( ! $Config{ZM_FROM_EMAIL} )
{
Error( "No 'from' email address defined, not sending message" );
if ( ! $Config{ZM_FROM_EMAIL} ) {
Error( 'No 'from' email address defined, not sending message' );
return( 0 );
}
if ( ! $Config{ZM_MESSAGE_ADDRESS} )
{
Error( "No message address defined, not sending message" );
if ( ! $Config{ZM_MESSAGE_ADDRESS} ) {
Error( 'No message address defined, not sending message' );
return( 0 );
}
@ -940,30 +807,27 @@ sub sendMessage
Info( "Sending notification message '$subject'\n" );
eval
{
if ( $Config{ZM_NEW_MAIL_MODULES} )
{
eval {
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
### Create the multipart container
my $mail = MIME::Lite->new (
From => $Config{ZM_FROM_EMAIL},
To => $Config{ZM_MESSAGE_ADDRESS},
Subject => $subject,
Type => "multipart/mixed"
Type => 'multipart/mixed'
);
### Add the text message part
$mail->attach (
Type => "TEXT",
Type => 'TEXT',
Data => $body
);
### Add the attachments
foreach my $attachment ( @attachments )
{
foreach my $attachment ( @attachments ) {
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Disposition => "attachment"
Disposition => 'attachment'
);
}
### Send the Message
@ -977,19 +841,17 @@ sub sendMessage
}
if ( !$ssmtp_location ) {
Debug( "Can't find ssmtp, trying MIME::Lite->send" );
MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
MIME::Lite->send( 'smtp', $Config{ZM_EMAIL_HOST}, Timeout=>60 );
$mail->send();
} else {
### Send using SSMTP
$mail->send( 'sendmail', $ssmtp_location, $Config{ZM_MESSAGE_ADDRESS} );
}
} else {
MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
MIME::Lite->send( 'smtp', $Config{ZM_EMAIL_HOST}, Timeout=>60 );
$mail->send();
}
}
else
{
} else {
my $mail = MIME::Entity->build(
From => $Config{ZM_FROM_EMAIL},
To => $Config{ZM_MESSAGE_ADDRESS},
@ -998,13 +860,12 @@ sub sendMessage
Data => $body
);
foreach my $attachment ( @attachments )
{
foreach my $attachment ( @attachments ) {
Info( "Attaching '$attachment->{path}\n" );
$mail->attach(
Path => $attachment->{path},
Type => $attachment->{type},
Encoding => "base64"
Encoding => 'base64'
);
}
$mail->smtpsend( Host => $Config{ZM_EMAIL_HOST},
@ -1012,16 +873,13 @@ sub sendMessage
);
}
};
if ( $@ )
{
if ( $@ ) {
Error( "Can't send email: $@" );
return( 0 );
}
else
{
} else {
Info( "Notification message sent\n" );
}
my $sql = "update Events set Messaged = 1 where Id = ?";
my $sql = 'update Events set Messaged = 1 where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )
@ -1030,8 +888,7 @@ sub sendMessage
return( 1 );
}
sub executeCommand
{
sub executeCommand {
my $filter = shift;
my $event = shift;
@ -1044,19 +901,15 @@ sub executeCommand
Info( "Executing '$command'\n" );
my $output = qx($command);
my $status = $? >> 8;
if ( $status || logDebugging() )
{
if ( $status || logDebugging() ) {
chomp( $output );
Debug( "Output: $output\n" );
}
if ( $status )
{
if ( $status ) {
Error( "Command '$command' exited with status: $status\n" );
return( 0 );
}
else
{
my $sql = "update Events set Executed = 1 where Id = ?";
} else {
my $sql = 'update Events set Executed = 1 where Id = ?';
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} )