16 |
our @EXPORT = qw( |
our @EXPORT = qw( |
17 |
); |
); |
18 |
|
|
19 |
our $VERSION = '0.5'; |
our $VERSION = '0.6'; |
20 |
|
|
21 |
use Class::DBI::Loader; |
use Class::DBI::Loader; |
22 |
use Email::Valid; |
use Email::Valid; |
27 |
use Email::Address; |
use Email::Address; |
28 |
use Mail::DeliveryStatus::BounceParser; |
use Mail::DeliveryStatus::BounceParser; |
29 |
use Class::DBI::AbstractSearch; |
use Class::DBI::AbstractSearch; |
30 |
|
use Mail::Alias; |
31 |
|
use Cwd qw(abs_path); |
32 |
|
|
33 |
|
|
34 |
=head1 NAME |
=head1 NAME |
122 |
|
|
123 |
=head2 new_list |
=head2 new_list |
124 |
|
|
125 |
Create new list. Required arguments are name of C<list> and |
Create new list. Required arguments are name of C<list>, C<email> address |
126 |
C<email> address. |
and path to C<aliases> file. |
127 |
|
|
128 |
$nos->new_list( |
$nos->new_list( |
129 |
list => 'My list', |
list => 'My list', |
130 |
from => 'Outgoing from comment', |
from => 'Outgoing from comment', |
131 |
email => 'my-list@example.com', |
email => 'my-list@example.com', |
132 |
|
aliases => '/etc/mail/mylist', |
133 |
|
archive => '/path/to/mbox/archive', |
134 |
); |
); |
135 |
|
|
136 |
Returns ID of newly created list. |
Returns ID of newly created list. |
691 |
|
|
692 |
Beware of dragons! You shouldn't need to call those methods directly. |
Beware of dragons! You shouldn't need to call those methods directly. |
693 |
|
|
694 |
|
|
695 |
|
=head2 _add_aliases |
696 |
|
|
697 |
|
Add new list to C</etc/aliases> (or equivavlent) file |
698 |
|
|
699 |
|
my $ok = $nos->add_aliases( |
700 |
|
list => 'My list', |
701 |
|
email => 'my-list@example.com', |
702 |
|
aliases => '/etc/mail/mylist', |
703 |
|
archive => '/path/to/mbox/archive', |
704 |
|
|
705 |
|
); |
706 |
|
|
707 |
|
C<archive> parametar is optional. |
708 |
|
|
709 |
|
Return false on failure. |
710 |
|
|
711 |
|
=cut |
712 |
|
|
713 |
|
sub _add_aliases { |
714 |
|
my $self = shift; |
715 |
|
|
716 |
|
my $arg = {@_}; |
717 |
|
|
718 |
|
croak "need list and email options" unless ($arg->{'list'} && $arg->{'email'}); |
719 |
|
|
720 |
|
my $aliases = $arg->{'aliases'} || croak "need aliases"; |
721 |
|
|
722 |
|
unless (-e $aliases) { |
723 |
|
warn "aliases file $aliases doesn't exist, creating empty\n"; |
724 |
|
open(my $fh, '>', $aliases) || croak "can't create $aliases: $!"; |
725 |
|
close($fh); |
726 |
|
chmod 0777, $aliases || warn "can't change permission to 0777"; |
727 |
|
} |
728 |
|
|
729 |
|
my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!"; |
730 |
|
|
731 |
|
my $target = ''; |
732 |
|
|
733 |
|
if (my $archive = $arg->{'archive'}) { |
734 |
|
$target .= "$archive, "; |
735 |
|
|
736 |
|
if (! -e $archive) { |
737 |
|
warn "please make sure that file $archive is writable for your e-mail user (defaulting to bad 777 permission for now)"; |
738 |
|
|
739 |
|
open(my $fh, '>', $archive) || croak "can't create archive file $archive: $!"; |
740 |
|
close($fh); |
741 |
|
chmod 0777, $archive || croak "can't chmod archive file $archive to 0777: $!"; |
742 |
|
} |
743 |
|
} |
744 |
|
|
745 |
|
# resolve my path to absolute one |
746 |
|
my $self_path = abs_path($0); |
747 |
|
$self_path =~ s#/[^/]+$##; |
748 |
|
$self_path =~ s#/t/*$#/#; |
749 |
|
|
750 |
|
$target .= qq#| cd $self_path && ./sender.pl --inbox="$arg->{'list'}"#; |
751 |
|
|
752 |
|
unless ($a->append($arg->{'email'}, $target)) { |
753 |
|
croak "can't add alias ".$a->error_check; |
754 |
|
} |
755 |
|
|
756 |
|
return 1; |
757 |
|
} |
758 |
|
|
759 |
=head2 _add_list |
=head2 _add_list |
760 |
|
|
761 |
Create new list |
Create new list |
764 |
list => 'My list', |
list => 'My list', |
765 |
from => 'Outgoing from comment', |
from => 'Outgoing from comment', |
766 |
email => 'my-list@example.com', |
email => 'my-list@example.com', |
767 |
|
aliases => '/etc/mail/mylist', |
768 |
); |
); |
769 |
|
|
770 |
Returns C<Class::DBI> object for created list. |
Returns C<Class::DBI> object for created list. |
783 |
|
|
784 |
my $name = lc($arg->{'list'}) || confess "can't add list without name"; |
my $name = lc($arg->{'list'}) || confess "can't add list without name"; |
785 |
my $email = lc($arg->{'email'}) || confess "can't add list without e-mail"; |
my $email = lc($arg->{'email'}) || confess "can't add list without e-mail"; |
786 |
|
my $aliases = lc($arg->{'aliases'}) || confess "can't add list without path to aliases file"; |
787 |
|
|
788 |
my $from_addr = $arg->{'from'}; |
my $from_addr = $arg->{'from'}; |
789 |
|
|
790 |
my $lists = $self->{'loader'}->find_class('lists'); |
my $lists = $self->{'loader'}->find_class('lists'); |
791 |
|
|
792 |
|
$self->_add_aliases( |
793 |
|
list => $name, |
794 |
|
email => $email, |
795 |
|
aliases => $aliases, |
796 |
|
) || croak "can't add alias $email for list $name"; |
797 |
|
|
798 |
my $l = $lists->find_or_create({ |
my $l = $lists->find_or_create({ |
799 |
name => $name, |
name => $name, |
800 |
email => $email, |
email => $email, |
814 |
} |
} |
815 |
|
|
816 |
|
|
817 |
|
|
818 |
=head2 _get_list |
=head2 _get_list |
819 |
|
|
820 |
Get list C<Class::DBI> object. |
Get list C<Class::DBI> object. |
859 |
|
|
860 |
my $nos; |
my $nos; |
861 |
|
|
862 |
|
|
863 |
|
=head2 new |
864 |
|
|
865 |
|
Create new SOAP object |
866 |
|
|
867 |
|
my $soap = new Nos::SOAP( |
868 |
|
dsn => 'dbi:Pg:dbname=notices', |
869 |
|
user => 'dpavlin', |
870 |
|
passwd => '', |
871 |
|
debug => 1, |
872 |
|
verbose => 1, |
873 |
|
hash_len => 8, |
874 |
|
aliases => '/etc/aliases', |
875 |
|
); |
876 |
|
|
877 |
|
=cut |
878 |
|
|
879 |
sub new { |
sub new { |
880 |
my $class = shift; |
my $class = shift; |
881 |
my $self = {@_}; |
my $self = {@_}; |
882 |
|
|
883 |
|
croak "need aliases parametar" unless ($self->{'aliases'}); |
884 |
|
|
885 |
bless($self, $class); |
bless($self, $class); |
886 |
|
|
887 |
$nos = new Nos( @_ ) || die "can't create Nos object"; |
$nos = new Nos( @_ ) || die "can't create Nos object"; |
903 |
sub NewList { |
sub NewList { |
904 |
my $self = shift; |
my $self = shift; |
905 |
|
|
906 |
|
croak "self is not Nos::SOAP object" unless (ref($self) eq 'Nos::SOAP'); |
907 |
|
|
908 |
|
my $aliases = $self->{'aliases'} || croak "need 'aliases' argument to new constructor"; |
909 |
|
|
910 |
if ($_[0] !~ m/^HASH/) { |
if ($_[0] !~ m/^HASH/) { |
911 |
return $nos->new_list( |
return $nos->new_list( |
912 |
list => $_[0], from => $_[1], email => $_[2], |
list => $_[0], from => $_[1], email => $_[2], |
913 |
|
aliases => $aliases, |
914 |
); |
); |
915 |
} else { |
} else { |
916 |
return $nos->new_list( %{ shift @_ } ); |
return $nos->new_list( %{ shift @_ }, aliases => $aliases ); |
917 |
} |
} |
918 |
} |
} |
919 |
|
|