/
c3mc-base-send
executable file
·96 lines (71 loc) · 2.01 KB
/
c3mc-base-send
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/Connector/lib
use strict;
use warnings;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
$|++;
use YAML::XS;
use Encode;
use JSON;
use IPC::Open3;
=head1 SYNOPSIS
$0
=cut
local $/ = undef;
my @data = YAML::XS::Load( Encode::encode('utf8', <STDIN> ) );
local $/ = "\n";
my $stat = 0;
sub sendinfo
{
my $data = shift @_;
return unless $data && ref $data eq 'HASH' && $data->{to};
my @to = ref $data->{to} eq 'ARRAY' ? @{$data->{to}} : ( $data->{to} );
my @user;
for my $t ( @to )
{
if( $t =~ /^[a-zA-Z0-9_\.\-\@]+$/ )
{
push @user, $t;
}
else
{
warn "[warn] send info to user: $t fail, username format error";
$stat = 1;
}
}
return unless @user;
if ($data->{title} && $data->{content} )
{
my $pipe = "c3mc-base-sendmail @user";
my $pid = IPC::Open3::open3( my $chld_in, '>&STDOUT', '>&STDERR', $pipe);
my $content = "$data->{title}\n$data->{content}";
Encode::_utf8_off($content);
print $chld_in $content;
$chld_in = undef;
waitpid( $pid, 0 );
( $stat = 1 && warn "send mail fail." ) if $?;
}
if( $data->{mesg} )
{
my $pipe = "c3mc-base-sendmesg @user";
my $pid = IPC::Open3::open3( my $chld_in, '>&STDOUT', '>&STDERR', $pipe);
Encode::_utf8_off($data->{mesg});
print $chld_in $data->{mesg};
$chld_in = undef;
waitpid( $pid, 0 );
( $stat = 1 && warn "send mesg fail." ) if $?;
}
if( $data->{call} )
{
my $pipe = "c3mc-base-sendcall @user";
my $pid = IPC::Open3::open3( my $chld_in, '>&STDOUT', '>&STDERR', $pipe);
Encode::_utf8_off($data->{call});
print $chld_in $data->{call};
$chld_in = undef;
waitpid( $pid, 0 );
( $stat = 1 && warn "send call fail." ) if $?;
}
}
map{ sendinfo($_ ) }@data;
exit $stat;