File Coverage

blib/lib/App/MonM/Notifier/Channel.pm
Criterion Covered Total %
statement 33 100 33.0
branch 0 22 0.0
condition 0 18 0.0
subroutine 11 21 52.3
pod 10 10 100.0
total 54 171 31.5


line stmt bran cond sub pod time code
1             package App::MonM::Notifier::Channel; # $Id: Channel.pm 59 2019-07-14 09:14:38Z abalama $
2 1     1   5 use strict;
  1         2  
  1         23  
3 1     1   4 use utf8;
  1         1  
  1         4  
4              
5             =encoding utf-8
6              
7             =head1 NAME
8              
9             App::MonM::Notifier::Channel - monotifier channel base class
10              
11             =head1 VERSION
12              
13             Version 1.01
14              
15             =head1 SYNOPSIS
16              
17             use App::MonM::Notifier::Channel;
18              
19             my $channel = new App::MonM::Notifier::Channel;
20              
21             my $data = {
22             id => 1,
23             to => "recipient",
24             subject => "Test message",
25             message => "Content of the message",
26             };
27              
28             my $ch_conf = {
29             'fri' => '18:01-19',
30             'from' => 'root@example.com',
31             'host' => 'mail.example.com',
32             'mon' => '7:35-17:45',
33             'period' => '7:30-16:30',
34             'port' => '25',
35             'set' => [
36             'User TeStUser',
37             'Password MyPassword'
38             ],
39             'sun' => '-',
40             'thu' => '16-18:01',
41             'to' => 'test@example.com',
42             'tue' => '15-19',
43             'type' => 'Email',
44             'wed' => '-'
45             };
46              
47             my $status = $channel->process($data, $ch_conf);
48             die($channel->error) unless $channel->status;
49              
50             =head1 DESCRIPTION
51              
52             This module provides channel base methods
53              
54             =head2 new
55              
56             my $channel = new App::MonM::Notifier::Channel;
57              
58             Returns the channel object
59              
60             =head2 cleanup
61              
62             my $self = $channel->cleanup;
63              
64             Cleaning up of working variables
65              
66             =head2 config
67              
68             my $conf_hash = $channel->config;
69              
70             Returns the channel configuration ($ch_conf)
71              
72             =head2 data
73              
74             my $data = $channel->data;
75             my $data = $channel->data( { ... } );
76              
77             Sets/gets data structure
78              
79             =head2 error
80              
81             my $error = $channel->error;
82             my $error = $channel->error( "New error" );
83              
84             Sets/gets error message
85              
86             =head2 genId
87              
88             my $message_id = $self->genId(
89             $self->data->{id} || 0,
90             $self->data->{pubdate} || 0,
91             $self->data->{to} || "anonymous",
92             );
93              
94             Return ID of message
95              
96             =head2 message
97              
98             my $email = $channel->message;
99             my $email = $channel->message( new Email::MIME );
100              
101             Gets/sets the Email::MIME object
102              
103             =head2 process
104              
105             my $status = $channel->process( $data, $ch_conf )
106             or die($channel->error);
107              
108             This method runs process of sending message to channel and returns
109             operation status.
110              
111             See L and L for details
112              
113             =head2 status
114              
115             my $status = $channel->status;
116             my $status = $channel->status( 1 ); # Sets the status value and returns it
117              
118             Get/set BOOL status of the operation
119              
120             =head2 type
121              
122             my $type = $channel->type;
123             my $type = $channel->type( "File" );
124              
125             Gets/sets the type value
126              
127             =head1 DATA
128              
129             It is a structure (hash), that can contain the following fields:
130              
131             'data' => {
132             'channel' => "MyEmail",
133             'comment' => "Comment",
134             'errcode' => 0,
135             'errmsg' => 'Ok',
136             'expires' => 1565599719,
137             'id' => 31,
138             'message' => "Message body",
139             'pubdate' => 1563007719,
140             'status' => 'NEW',
141             'subject' => "My message",
142             'to' => 'testuser'
143             }
144              
145             =over 4
146              
147             =item B
148              
149             Channel name
150              
151             =item B
152              
153             Comment string
154              
155             =item B
156              
157             Error code
158              
159             =item B
160              
161             Error message
162              
163             =item B
164              
165             Expires time value
166              
167             =item B
168              
169             Contains internal ID of the message. This ID is converted to an X-Id header
170              
171             =item B
172              
173             Body of the message
174              
175             =item B
176              
177             The time of message publication
178              
179             =item B
180              
181             Status of record (text formst). See L
182              
183             =item B
184              
185             Subject of the message
186              
187             =item B
188              
189             Recipient address or name
190              
191             =back
192              
193             =head2 DIRECTIVES
194              
195             It is a structure (hash), that can contain the following fields:
196              
197             =over 4
198              
199             =item B
200              
201             Sets the charset
202              
203             Default: utf-8
204              
205             See also L
206              
207             =item B
208              
209             Sets the content type
210              
211             Default: text/plain
212              
213             See also L
214              
215             =item B
216              
217             Sets encoding (8bit, base64, quoted-printable)
218              
219             Default: 8bit
220              
221             See also L
222              
223             =item B
224              
225             Container for MIME headers definitions
226              
227             =item B
228              
229             Defines type of channel
230              
231             Allowed types: File, Command, Email
232              
233             =back
234              
235             =head1 HISTORY
236              
237             See C file
238              
239             =head1 DEPENDENCIES
240              
241             L, L, L
242              
243             =head1 TO DO
244              
245             See C file
246              
247             =head1 BUGS
248              
249             * none noted
250              
251             =head1 SEE ALSO
252              
253             L
254              
255             =head1 AUTHOR
256              
257             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
258              
259             =head1 COPYRIGHT
260              
261             Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
262              
263             =head1 LICENSE
264              
265             This program is free software; you can redistribute it and/or
266             modify it under the same terms as Perl itself.
267              
268             See C file and L
269              
270             =cut
271              
272 1     1   51 use vars qw/$VERSION/;
  1         1  
  1         38  
273             $VERSION = '1.01';
274              
275 1     1   383 use Class::C3::Adopt::NEXT;
  1         4870  
  1         4  
276 1     1   475 use Email::MIME;
  1         34082  
  1         31  
277 1     1   507 use Compress::Raw::Zlib qw//; # CRC32
  1         3579  
  1         24  
278              
279 1     1   6 use CTK::Util;
  1         2  
  1         37  
280 1     1   5 use CTK::ConfGenUtil;
  1         2  
  1         61  
281 1     1   6 use CTK::TFVals qw/ :ALL /;
  1         1  
  1         212  
282              
283 1         439 use base qw/
284             App::MonM::Notifier::Channel::File
285             App::MonM::Notifier::Channel::Email
286             App::MonM::Notifier::Channel::Command
287 1     1   8 /;
  1         2  
288              
289             use constant {
290 1         758 TIMEOUT => 300, # 5 min timeout
291             CONTENT_TYPE=> "text/plain",
292             CHARSET => "utf-8",
293             ENCODING => "8bit", # "base64"
294             USERNAME => "anonymous",
295 1     1   6 };
  1         2  
296              
297             sub new {
298 0     0 1   my $class = shift;
299 0           my %args = @_;
300 0           my $self = bless {%args}, $class;
301 0           return $self->cleanup;
302             }
303             sub cleanup {
304 0     0 1   my $self = shift;
305 0           $self->{config} = {}; # Channel config
306 0           $self->{status} = 0; # 1 - Ok; 0 - Error
307 0           $self->{error} = ''; # Error message
308 0           $self->{type} = ''; # email/file/command
309 0           $self->{message} = undef; # Message
310 0           $self->{data} = {};
311              
312 0           return $self;
313             }
314             sub config {
315 0     0 1   my $self = shift;
316 0           return $self->{config};
317             }
318             sub status {
319 0     0 1   my $self = shift;
320 0           my $v = shift;
321 0 0         $self->{status} = $v if defined $v;
322 0           return $self->{status};
323             }
324             sub error {
325 0     0 1   my $self = shift;
326 0           my $v = shift;
327 0 0         $self->{error} = $v if defined $v;
328 0           return $self->{error};
329             }
330             sub type {
331 0     0 1   my $self = shift;
332 0           my $v = shift;
333 0 0         $self->{type} = $v if defined $v;
334 0           return $self->{type};
335             }
336             sub message {
337 0     0 1   my $self = shift;
338 0           my $v = shift;
339 0 0         $self->{message} = $v if defined $v;
340 0           return $self->{message};
341             }
342             sub data {
343 0     0 1   my $self = shift;
344 0           my $v = shift;
345 0 0         $self->{data} = $v if defined $v;
346 0           return $self->{data};
347             }
348             sub process {
349 0     0 1   my $self = shift;
350 0           my $data = shift;
351 0           my $conf = shift;
352 0           $self->cleanup;
353 0 0         $self->{config} = $conf if ref($conf) eq 'HASH';
354 0 0         $self->data($data) if ref($data) eq 'HASH';
355 0           $self->type(lc(uv2null(value($conf, 'type'))));
356              
357             # Create message
358 0           my $headers = hash($conf => "headers");
359 0   0       my $from = value($conf, "from") // '';
360 0 0 0       my %hset = (
      0        
361             To => value($conf, "to") || value($data, "to") || USERNAME,
362             $from ? (From => $from) : (),
363             Subject => value($data, "subject") || '',
364             );
365 0 0 0       if ($headers && is_hash($headers) && keys(%$headers)) {
      0        
366 0           while (my ($k,$v) = each %$headers) {
367 0 0         next unless defined $v;
368 0 0         if (grep {lc($k) eq lc($_)} (qw/To From Subject/)) {
  0            
369 0           $hset{ucfirst($k)} = $v;
370             } else {
371 0           $hset{$k} = $v;
372             }
373             }
374             }
375              
376             # Create message object
377 0           my $email = Email::MIME->create(
378             header_str => [%hset],
379             );
380 0   0       $email->content_type_set( value($conf => "contenttype") // CONTENT_TYPE );
381 0   0       $email->charset_set( value($conf => "charset") // CHARSET );
382 0   0       $email->encoding_set( value($conf => "encoding") // ENCODING );
383              
384             # Add message content
385 0           my $message = uv2null(value($data => "message"));
386 0           $email->body_str_set($message);
387 0           $self->message($email);
388              
389             # Go!
390 0           $self->maybe::next::method();
391 0           return $self->status;
392             }
393             sub genId {
394 0     0 1   my $self = shift;
395 0           my @arr = @_;
396 0           unshift @arr, $$;
397 0           my $text = join("|", @arr);
398 0           my $short = time & 0x7FFFFF;
399 0           my $crc8 = Compress::Raw::Zlib::crc32($text) & 0xFF;
400 0           return hex(sprintf("%x%x",$short, $crc8));
401             }
402              
403             1;
404              
405             __END__