File Coverage

blib/lib/WebService/MIAB.pm
Criterion Covered Total %
statement 29 112 25.8
branch 0 30 0.0
condition 0 24 0.0
subroutine 10 24 41.6
pod 10 12 83.3
total 49 202 24.2


line stmt bran cond sub pod time code
1             package WebService::MIAB;
2              
3 1     1   69760 use 5.006;
  1         4  
4 1     1   6 use strict;
  1         2  
  1         23  
5 1     1   5 use warnings;
  1         2  
  1         45  
6              
7             =head1 NAME
8              
9             WebService :: MIAB - MIAB (Mail in a Box) helps you manage user and aliasaddressen
10              
11             =head1 VERSION
12              
13             version 0.01
14              
15             =cut
16              
17 1     1   491 use version;
  1         2004  
  1         5  
18             our $VERSION = '0.01';
19              
20 1     1   656 use Moo;
  1         12240  
  1         6  
21             with 'WebService::Client';
22              
23 1     1   2295 use LWP::UserAgent;
  1         50806  
  1         46  
24 1     1   725 use MIME::Base64;
  1         687  
  1         98  
25 1     1   8 use Carp qw(croak);
  1         3  
  1         53  
26 1     1   6 use re qw(is_regexp);
  1         8  
  1         165  
27              
28 1     1   693 use JSON;
  1         10617  
  1         8  
29             my $json = JSON->new->allow_nonref;
30              
31             #Alias_Address
32             has alias_get_uri => ( is => 'ro', default => '/mail/aliases?format=json' );
33             has alias_add_uri => ( is => 'ro', default => '/mail/aliases/add' );
34             has alias_remove_uri => ( is => 'ro', default => '/mail/aliases/remove' );
35              
36             #User_Address
37             has user_get_uri => ( is => 'ro', default => '/mail/users?format=json');
38             has user_add_uri => ( is => 'ro', default => '/mail/users/add' );
39             has user_remove_uri => ( is => 'ro', default => '/mail/users/remove' );
40              
41             #General
42             has host => (is => 'ro', required => 1);
43             has pass => ( is => 'ro', required => 1 );
44             has username => ( is => 'ro', required => 1 );
45             has '+base_url' => (
46             is => 'ro',
47             lazy => 1,
48             builder => sub {
49 0     0     my ($self) = @_;
50 0           return 'https://'.$self->host.'/admin';
51             },
52             );
53              
54             # custom deserializer needed as the MIAB webservice doesn't return JSON for post requests
55             has '+deserializer' => (
56             is => 'ro',
57             lazy => 1,
58             default => sub {
59             my $self = shift;
60             my $usejson = $self->json;
61             sub {
62             my ($res, %args) = @_;
63             my $decoded = $res;
64            
65             if ($res->request->uri =~ /json/) {
66             $decoded = $json->decode($res->content);
67             }
68             # else {
69             # # no json request - nothing to do
70             # }
71             return $decoded;
72             }
73             },
74             );
75              
76             ################ Constructor ################
77             sub BUILD(){
78 0     0 0   my ($self) = @_;
79 0           my $empty = q{};
80 0           my $colon = q{:};
81 0           return $self->ua->default_header('Authorization' => 'Basic '.encode_base64($self->username.$colon.$self->pass , $empty));
82             }
83              
84             =head1 SYNOPSIS
85              
86             use WebService::MIAB;
87              
88             my $MIAB = WebService::MIAB->new(
89             username => 'Email@domain.com',
90             pass => 'ThisIsThePassword',
91             host => 'example.com',
92             );
93              
94             my $users = $MIAB->get_users();
95             my $result = $MIAB->create_user(
96             {
97             email => 'bart.simpson@springfield.com',
98             password => 'EatMyShorts',
99             });
100              
101             =head1 DESCRIPTION
102              
103             WebService::MIAB is a Perl module that manages user and alias addresses from the mail server MIAB (Mail in a Box).
104             The module is based on the WebService::Client module that is written from Naveed Massjouni.
105             You can list, create and remove user and alias addresses.
106              
107             =head1 METHODES
108              
109             =head2 User methods
110              
111             =head3 get_user()
112              
113             The get_user function returns all users which are defined in JSON format.
114             The function only need the Uri for gets the Users.
115             MIAB->get_user()
116              
117             sub get_users{
118             my ($self) = @_;
119             my $result = $self->get($self->user_get_uri);
120             return $result;
121             }
122              
123             =cut
124              
125             sub get_users{
126 0     0 0   my ($self) = @_;
127 0           my $result = $self->get($self->user_get_uri);
128 0           return $result;
129             }
130              
131             =head3 create_user($hashWithParam)
132              
133             The create_user function create a new User.
134             The function receives the parameters email and password in form of a hash reference for creating an user.
135             The create_user function consists only a post command that create the user.
136             $MIAB->create_user(email => 'bart.simpson@springfield.com',password => 'EatMyShorts')
137              
138             sub create_user{
139             my ($self,$param) = @_;
140             my $result = $self->post($self->user_add_uri,
141             {
142             email => $param->{email},
143             password => $param->{password},
144             },
145             headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
146             );
147             return $result;
148             }
149              
150             =cut
151              
152             sub create_user{
153 0     0 1   my ($self,$param) = @_;
154             my $result = $self->post($self->user_add_uri,
155             {
156             email => $param->{email},
157             password => $param->{password},
158             },
159 0           headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
160             );
161 0           return $result;
162             }
163              
164             =head3 remove_user($hashWithParam)
165              
166             The remove_user function delete a User.
167             The function receives the parameters email in form of a hash reference for deleting an user.
168             The remove_user function consists only a post command that delete an user.
169             $MIAB->remove_user(email => bart.simpson@springfield.com)
170              
171             sub remove_user{
172             my ($self, $param) = @_;
173             my $result = $self->post($self->user_remove_uri,
174             {
175             email => $param->{email},
176             },
177             headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
178             );
179             return $result;
180             }
181              
182             =cut
183              
184             sub remove_user{
185 0     0 1   my ($self, $param) = @_;
186             my $result = $self->post($self->user_remove_uri,
187             {
188             email => $param->{email},
189             },
190 0           headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
191             );
192 0           return $result;
193             }
194              
195             =head3 find_user($email_pattern, $domain_pattern)
196              
197             The find_user function search an user.
198             The function receives the parameters E-Mail and Domain to search the user.
199             The function is_regexp checks if the given parameter are a regular expression.
200             The function gets with get_users () all users that are present and then filters them according to the passed parameters.
201             $MIAB->remove_user(email => bart.simpson@springfield.com)
202              
203             sub find_user {
204             my ($self, $email_pattern, $domain_pattern) = @_;
205             my @result;
206             if ( !is_regexp($email_pattern) ) {
207             $email_pattern = qr/$email_pattern/mxs;
208             }
209             if ($domain_pattern && !is_regexp($domain_pattern)) {
210             $domain_pattern = qr/$domain_pattern/mxs;
211             }
212              
213             my $users = $self->get_users();
214             foreach ( @{$users} ) {
215             if (!$domain_pattern || $_->{'domain'} =~ $domain_pattern) {
216             foreach (@{$_->{'users'}}) {
217             my $value = $_->{'email'};
218              
219             # filter list
220             push( @result,
221             grep {
222             $_ && $_ =~ $email_pattern
223             } ($value && 'ARRAY' eq ref $value)
224             ? @{$value}
225             : ( $value )
226             );
227             }
228             }
229             }
230             return @result;
231             }
232              
233             =cut
234              
235             sub find_user {
236 0     0 1   my ($self, $email_pattern, $domain_pattern) = @_;
237 0           my @result;
238 0 0         if ( !is_regexp($email_pattern) ) {
239 0           $email_pattern = qr/$email_pattern/mxs;
240             }
241 0 0 0       if ($domain_pattern && !is_regexp($domain_pattern)) {
242 0           $domain_pattern = qr/$domain_pattern/mxs;
243             }
244              
245 0           my $users = $self->get_users();
246 0           foreach ( @{$users} ) {
  0            
247 0 0 0       if (!$domain_pattern || $_->{'domain'} =~ $domain_pattern) {
248 0           foreach (@{$_->{'users'}}) {
  0            
249 0           my $value = $_->{'email'};
250              
251             # filter list
252             push( @result,
253             grep {
254 0 0         $_ && $_ =~ $email_pattern
255             } ($value && 'ARRAY' eq ref $value)
256 0 0 0       ? @{$value}
  0            
257             : ( $value )
258             );
259             }
260             }
261             }
262 0           return @result;
263              
264             }
265              
266             =head2 Alias mehtods
267              
268             =head3 get_aliases()
269              
270             The get_aliases function returns all Aliasaddresses for all domains which exist in Json format.
271             $MIAB->get_aliases()
272              
273             sub get_aliases{
274             my ($self) = @_;
275             my $result = $self->get($self->alias_get_uri);
276             return $result;
277             }
278              
279             =cut
280              
281             sub get_aliases{
282 0     0 1   my ($self) = @_;
283 0           my $result = $self->get($self->alias_get_uri);
284 0           return $result;
285             }
286              
287             =head3 get_domains()
288              
289             The get_domains function returns all Domains which are defined.
290             $MIAB->get_domains()
291              
292             sub get_domains{
293             my ($self) = @_;
294             my $aliases = $self->get_aliases();
295             my @result;
296             map {
297             push( @result, $_->{'domain'} );
298             } @{$aliases};
299             return @result;
300             }
301              
302             =cut
303              
304             sub get_domains{
305 0     0 1   my ($self) = @_;
306 0           my $aliases = $self->get_aliases();
307 0           my @result;
308             map {
309 0           push( @result, $_->{'domain'} );
310 0           } @{$aliases};
  0            
311 0           return @result;
312             }
313              
314             =head3 find_forward_to_aliases($alias_pattern, $domain_pattern)
315              
316             The function receives the parameters alias_pattern and domain_pattern.
317             The find_forward_to_aliases function contains only one function call and a transfer of parameters
318             MIAB->find_forward_to_aliases($alias_pattern, $domain_pattern)
319              
320             sub find_forward_to_aliases {
321             my ($self, $alias_pattern, $domain_pattern) = @_;
322             return $self->_find_aliases_generic('forwards_to', $alias_pattern, $domain_pattern);
323             }
324              
325             =cut
326              
327             sub find_forward_to_aliases {
328 0     0 1   my ($self, $alias_pattern, $domain_pattern) = @_;
329              
330 0           return $self->_find_aliases_generic('forwards_to', $alias_pattern, $domain_pattern);
331             }
332              
333             =head3 find_permitted_sender_aliases($alias_pattern, $domain_pattern)
334              
335             The function receives the parameters alias_pattern and domain_pattern.
336             The find_permitted_sender_aliases function contains only one function call and a transfer of parameters
337             MIAB->find_permitted_sender_aliases($alias_pattern, $domain_pattern)
338              
339             sub find_permitted_sender_aliases {
340             my ($self, $alias_pattern, $domain_pattern) = @_;
341             return $self->_find_aliases_generic('permitted_senders', $alias_pattern, $domain_pattern);
342             }
343              
344             =cut
345              
346             sub find_permitted_sender_aliases {
347 0     0 1   my ($self, $alias_pattern, $domain_pattern) = @_;
348              
349 0           return $self->_find_aliases_generic('permitted_senders', $alias_pattern, $domain_pattern);
350             }
351              
352             =head3 find_aliases($alias_pattern, $domain_pattern)
353              
354             The function receives the parameters alias_pattern and domain_pattern.
355             The find_aliases function contains only one function call and a transfer of parameters
356             MIAB->find_aliases($alias_pattern, $domain_pattern)
357              
358             sub find_aliases {
359             my ($self, $alias_pattern, $domain_pattern) = @_;
360             return $self->_find_aliases_generic('address', $alias_pattern, $domain_pattern);
361             }
362              
363             =cut
364              
365             sub find_aliases {
366 0     0 1   my ($self, $alias_pattern, $domain_pattern) = @_;
367              
368 0           return $self->_find_aliases_generic('address', $alias_pattern, $domain_pattern);
369             }
370              
371             =head3 create_alias($param)
372              
373             The create_alias function create a new Aliasaddress.
374             The function receives the parameters address and forwards_to or permitted_senders in form of a hash reference for create a Aliasaddress.
375             The create_alias function consists a post command that create the Aliasaddress.
376             $param = {
377             address => 'bart.simpson@springfield.com',
378             forwards_to => ['lisa.simpson@springfield.com','Maggie.simpson@springfield.com'], # One of
379             permitted_senders => ['Homer.simpson@springfield.com','Marge.simpson@springfield.com'], # these
380             };
381             $MIAB->create_alias($param);
382              
383             sub create_alias{
384             my ($self,$param) = @_;
385             my $empty = q{};
386             my $point = q{,};
387             # Parameters normalizee
388             if (defined $param->{forwards_to} && ref $param->{forwards_to} ne 'ARRAY')
389             {
390             $param->{forwards_to} = [$param->{forwards_to}];
391             }
392             if (defined $param->{permitted_senders} && ref $param->{permitted_senders} ne 'ARRAY')
393             {
394             $param->{permitted_senders} = [$param->{permitted_senders}];
395             }
396             # Rest-Interface Paramter generieren
397             my $forward_address_for_request = defined $param->{forwards_to}
398             ? join ($point, @{$param->{forwards_to}})
399             : $empty;
400             my $permittet_senders_for_request = defined $param->{permitted_senders}
401             ? join ($point, @{$param->{permitted_senders}})
402             : $empty;
403              
404             # At least one alias parameter must exist
405             if(!length($forward_address_for_request.$permittet_senders_for_request))
406             {
407             croak 'forwardsaddress and permitted_sender are required';
408             }
409              
410             my $result = $self->post($self->alias_add_uri,
411             {
412             address => $param->{address},
413             forwards_to => $forward_address_for_request,
414             permitted_senders => $permittet_senders_for_request,
415             },
416             headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
417             );
418             return $result;
419             }
420              
421             =cut
422              
423             sub create_alias{
424 0     0 1   my ($self,$param) = @_;
425 0           my $empty = q{};
426 0           my $point = q{,};
427             # parameters normalize
428 0 0 0       if (defined $param->{forwards_to} && ref $param->{forwards_to} ne 'ARRAY'){
429 0           $param->{forwards_to} = [$param->{forwards_to}];
430             }
431 0 0 0       if (defined $param->{permitted_senders} && ref $param->{permitted_senders} ne 'ARRAY'){
432 0           $param->{permitted_senders} = [$param->{permitted_senders}];
433             }
434             # Rest-Interface Paramter generieren
435             my $forward_address_for_request = defined $param->{forwards_to}
436 0 0         ? join ($point, @{$param->{forwards_to}})
  0            
437             : $empty;
438             my $permittet_senders_for_request = defined $param->{permitted_senders}
439 0 0         ? join ($point, @{$param->{permitted_senders}})
  0            
440             : $empty;
441              
442             # One alias parameter must exist
443 0 0         if(!length($forward_address_for_request.$permittet_senders_for_request)){
444 0           croak 'forwardsaddress and permitted_sender are required';
445             }
446              
447             my $result = $self->post($self->alias_add_uri,
448             {
449             address => $param->{address},
450 0           forwards_to => $forward_address_for_request,
451             permitted_senders => $permittet_senders_for_request,
452             },
453             headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
454             );
455 0           return $result;
456             }
457              
458             =head3 remove_alias($param)
459              
460             The remove_alias function delete a Aliasaddress.
461             The function gets in form of a hash reference the parameter address for delete a Aliasaddress.
462             The remove_alias function consists only a post command that delete a Aliasaddress.
463             $MIAB->remove_alias(address => bart.simpson@springfield.com)
464              
465             sub remove_alias{
466             my ($self, $param) = @_;
467             my $result = $self->post($self->alias_remove_uri,
468             {
469             address => $param->{address},
470             },
471             headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }
472             );
473             return $result;
474             }
475              
476             =cut
477              
478             sub remove_alias{
479 0     0 1   my ($self, $param) = @_;
480             my $result = $self->post($self->alias_remove_uri,
481             {
482             address => $param->{address},
483             },
484 0           headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }
485             );
486 0           return $result;
487             }
488              
489             sub _find_aliases_generic {
490 0     0     my ($self, $param_name, $alias_pattern, $domain_pattern) = @_;
491 0           my @result;
492              
493             # force patterns to be a regex
494 0 0         if (!is_regexp($alias_pattern)) {
495 0           $alias_pattern = qr/$alias_pattern/mxs;
496             }
497 0 0 0       if ($domain_pattern && !is_regexp($domain_pattern)) {
498 0           $domain_pattern = qr/$domain_pattern/mxs;
499             }
500              
501             # get all aliases
502 0           my $aliases = $self->get_aliases();
503              
504             # filter
505 0           foreach (@{$aliases}) {
  0            
506 0 0 0       if (!$domain_pattern || $_->{'domain'} =~ $domain_pattern)
507             {
508             # either $domain_pattern is undefined (param not given)
509             # or the current domain matched the given $domain_pattern
510              
511 0           foreach (@{$_->{'aliases'}}) {
  0            
512 0           my $value = $_->{$param_name};
513              
514             # filter list
515             push( @result,
516             grep {
517 0 0         $_ && $_ =~ $alias_pattern
518             } ($value && 'ARRAY' eq ref $value)
519 0 0 0       ? @{$value}
  0            
520             : ( $value )
521             );
522             }
523             }
524             }
525 0           return @result;
526             }
527              
528             =head1 AUTHOR
529              
530             Alexander Schneider, C<< >>
531              
532             =head1 BUGS
533              
534             Please report any bugs or feature requests to C, or through
535             the web interface at L. I will be notified, and then you'll
536             automatically be notified of progress on your bug as I make changes.
537              
538              
539             =head1 SUPPORT
540              
541             You can find documentation for this module with the perldoc command.
542              
543             perldoc WebService::MIAB
544              
545              
546             You can also look for information at:
547              
548             =over 4
549              
550             =item * RT: CPAN's request tracker (report bugs here)
551              
552             L
553              
554             =item * AnnoCPAN: Annotated CPAN documentation
555              
556             L
557              
558             =item * CPAN Ratings
559              
560             L
561              
562             =item * Search CPAN
563              
564             L
565              
566             =back
567              
568              
569             =head1 ACKNOWLEDGEMENTS
570              
571              
572             =head1 LICENSE AND COPYRIGHT
573              
574             Copyright 2018 Alexander Schneider.
575              
576             This program is free software; you can redistribute it and/or modify it
577             under the terms of the the Artistic License (2.0). You may obtain a
578             copy of the full license at:
579              
580             L
581              
582             Any use, modification, and distribution of the Standard or Modified
583             Versions is governed by this Artistic License. By using, modifying or
584             distributing the Package, you accept this license. Do not use, modify,
585             or distribute the Package, if you do not accept this license.
586              
587             If your Modified Version has been derived from a Modified Version made
588             by someone other than you, you are nevertheless required to ensure that
589             your Modified Version complies with the requirements of this license.
590              
591             This license does not grant you the right to use any trademark, service
592             mark, tradename, or logo of the Copyright Holder.
593              
594             This license includes the non-exclusive, worldwide, free-of-charge
595             patent license to make, have made, use, offer to sell, sell, import and
596             otherwise transfer the Package with respect to any patent claims
597             licensable by the Copyright Holder that are necessarily infringed by the
598             Package. If you institute patent litigation (including a cross-claim or
599             counterclaim) against any party alleging that the Package constitutes
600             direct or contributory patent infringement, then this Artistic License
601             to you shall terminate on the date that such litigation is filed.
602              
603             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
604             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
605             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
606             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
607             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
608             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
609             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
610             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
611              
612              
613             =cut
614              
615             1; # End of WebService::MIAB
616