16 |
our @EXPORT = qw( |
our @EXPORT = qw( |
17 |
); |
); |
18 |
|
|
19 |
our $VERSION = '0.5'; |
our $VERSION = '0.7'; |
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 |
120 |
} |
} |
121 |
|
|
122 |
|
|
123 |
=head2 new_list |
=head2 create_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->create_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. |
139 |
|
|
140 |
=cut |
=cut |
141 |
|
|
142 |
sub new_list { |
sub create_list { |
143 |
my $self = shift; |
my $self = shift; |
144 |
|
|
145 |
my $arg = {@_}; |
my $arg = {@_}; |
158 |
} |
} |
159 |
|
|
160 |
|
|
161 |
=head2 delete_list |
=head2 drop_list |
162 |
|
|
163 |
Delete list from database. |
Delete list from database. |
164 |
|
|
165 |
my $ok = delete_list( |
my $ok = drop_list( |
166 |
list => 'My list' |
list => 'My list' |
167 |
|
aliases => '/etc/mail/mylist', |
168 |
); |
); |
169 |
|
|
170 |
Returns false if list doesn't exist. |
Returns false if list doesn't exist. |
171 |
|
|
172 |
=cut |
=cut |
173 |
|
|
174 |
sub delete_list { |
sub drop_list { |
175 |
my $self = shift; |
my $self = shift; |
176 |
|
|
177 |
my $args = {@_}; |
my $args = {@_}; |
180 |
|
|
181 |
$args->{'list'} = lc($args->{'list'}); |
$args->{'list'} = lc($args->{'list'}); |
182 |
|
|
183 |
|
my $aliases = $args->{'aliases'} || croak "need path to aliases file"; |
184 |
|
|
185 |
my $lists = $self->{'loader'}->find_class('lists'); |
my $lists = $self->{'loader'}->find_class('lists'); |
186 |
|
|
187 |
my $this_list = $lists->search( name => $args->{'list'} )->first || return; |
my $this_list = $lists->search( name => $args->{'list'} )->first || return; |
188 |
|
|
189 |
|
$self->_remove_alias( email => $this_list->email, aliases => $aliases); |
190 |
|
|
191 |
$this_list->delete || croak "can't delete list\n"; |
$this_list->delete || croak "can't delete list\n"; |
192 |
|
|
193 |
return $lists->dbi_commit || croak "can't commit"; |
return $lists->dbi_commit || croak "can't commit"; |
529 |
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
530 |
print "SKIP $to_email message allready sent\n"; |
print "SKIP $to_email message allready sent\n"; |
531 |
} else { |
} else { |
532 |
print "=> $to_email\n"; |
print "=> $to_email "; |
533 |
|
|
534 |
my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id; |
my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id; |
535 |
my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} ); |
my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} ); |
555 |
$m_obj->header_set('X-Nos-Hash', $hash); |
$m_obj->header_set('X-Nos-Hash', $hash); |
556 |
|
|
557 |
# really send e-mail |
# really send e-mail |
558 |
|
my $sent_status; |
559 |
|
|
560 |
if (@email_send_options) { |
if (@email_send_options) { |
561 |
send $email_send_driver => $m_obj->as_string, @email_send_options; |
$sent_status = send $email_send_driver => $m_obj->as_string, @email_send_options; |
562 |
} else { |
} else { |
563 |
send $email_send_driver => $m_obj->as_string; |
$sent_status = send $email_send_driver => $m_obj->as_string; |
564 |
} |
} |
565 |
|
|
566 |
$sent->create({ |
croak "can't send e-mail: $sent_status\n\nOriginal e-mail follows:\n".$m_obj->as_string unless ($sent_status); |
567 |
message_id => $m->message_id, |
my @bad = @{ $sent_status->prop('bad') }; |
568 |
user_id => $u->user_id, |
croak "failed sending to ",join(",",@bad) if (@bad); |
569 |
hash => $hash, |
|
570 |
}); |
if ($sent_status) { |
571 |
$sent->dbi_commit; |
|
572 |
|
$sent->create({ |
573 |
|
message_id => $m->message_id, |
574 |
|
user_id => $u->user_id, |
575 |
|
hash => $hash, |
576 |
|
}); |
577 |
|
$sent->dbi_commit; |
578 |
|
|
579 |
|
print " - $sent_status\n"; |
580 |
|
|
581 |
|
} else { |
582 |
|
warn "ERROR: $sent_status\n"; |
583 |
|
} |
584 |
|
|
585 |
if ($sleep) { |
if ($sleep) { |
586 |
warn "sleeping $sleep seconds\n"; |
warn "sleeping $sleep seconds\n"; |
696 |
|
|
697 |
Beware of dragons! You shouldn't need to call those methods directly. |
Beware of dragons! You shouldn't need to call those methods directly. |
698 |
|
|
699 |
|
|
700 |
|
=head2 _add_aliases |
701 |
|
|
702 |
|
Add or update alias in C</etc/aliases> (or equivalent) file for selected list |
703 |
|
|
704 |
|
my $ok = $nos->add_aliases( |
705 |
|
list => 'My list', |
706 |
|
email => 'my-list@example.com', |
707 |
|
aliases => '/etc/mail/mylist', |
708 |
|
archive => '/path/to/mbox/archive', |
709 |
|
|
710 |
|
); |
711 |
|
|
712 |
|
C<archive> parametar is optional. |
713 |
|
|
714 |
|
Return false on failure. |
715 |
|
|
716 |
|
=cut |
717 |
|
|
718 |
|
sub _add_aliases { |
719 |
|
my $self = shift; |
720 |
|
|
721 |
|
my $arg = {@_}; |
722 |
|
|
723 |
|
foreach my $o (qw/list email aliases/) { |
724 |
|
croak "need $o option" unless ($arg->{$o}); |
725 |
|
} |
726 |
|
|
727 |
|
my $aliases = $arg->{'aliases'}; |
728 |
|
my $email = $arg->{'email'}; |
729 |
|
my $list = $arg->{'list'}; |
730 |
|
|
731 |
|
unless (-e $aliases) { |
732 |
|
warn "aliases file $aliases doesn't exist, creating empty\n"; |
733 |
|
open(my $fh, '>', $aliases) || croak "can't create $aliases: $!"; |
734 |
|
close($fh); |
735 |
|
chmod 0777, $aliases || warn "can't change permission to 0777"; |
736 |
|
} |
737 |
|
|
738 |
|
die "FATAL: aliases file $aliases is not writable\n" unless (-w $aliases); |
739 |
|
|
740 |
|
my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!"; |
741 |
|
|
742 |
|
my $target = ''; |
743 |
|
|
744 |
|
if (my $archive = $arg->{'archive'}) { |
745 |
|
$target .= "$archive, "; |
746 |
|
|
747 |
|
if (! -e $archive) { |
748 |
|
warn "please make sure that file $archive is writable for your e-mail user (defaulting to bad 777 permission for now)"; |
749 |
|
|
750 |
|
open(my $fh, '>', $archive) || croak "can't create archive file $archive: $!"; |
751 |
|
close($fh); |
752 |
|
chmod 0777, $archive || croak "can't chmod archive file $archive to 0777: $!"; |
753 |
|
} |
754 |
|
} |
755 |
|
|
756 |
|
# resolve my path to absolute one |
757 |
|
my $self_path = abs_path($0); |
758 |
|
$self_path =~ s#/[^/]+$##; |
759 |
|
$self_path =~ s#/t/*$#/#; |
760 |
|
|
761 |
|
$target .= qq#| cd $self_path && ./sender.pl --inbox="$list"#; |
762 |
|
|
763 |
|
if ($a->exists($email)) { |
764 |
|
$a->update($email, $target) or croak "can't update alias ".$a->error_check; |
765 |
|
} else { |
766 |
|
$a->append($email, $target) or croak "can't add alias ".$a->error_check; |
767 |
|
} |
768 |
|
|
769 |
|
#$a->write($aliases) or croak "can't save aliases $aliases ".$a->error_check; |
770 |
|
|
771 |
|
return 1; |
772 |
|
} |
773 |
|
|
774 |
=head2 _add_list |
=head2 _add_list |
775 |
|
|
776 |
Create new list |
Create new list |
779 |
list => 'My list', |
list => 'My list', |
780 |
from => 'Outgoing from comment', |
from => 'Outgoing from comment', |
781 |
email => 'my-list@example.com', |
email => 'my-list@example.com', |
782 |
|
aliases => '/etc/mail/mylist', |
783 |
); |
); |
784 |
|
|
785 |
Returns C<Class::DBI> object for created list. |
Returns C<Class::DBI> object for created list. |
798 |
|
|
799 |
my $name = lc($arg->{'list'}) || confess "can't add list without name"; |
my $name = lc($arg->{'list'}) || confess "can't add list without name"; |
800 |
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"; |
801 |
|
my $aliases = lc($arg->{'aliases'}) || confess "can't add list without path to aliases file"; |
802 |
|
|
803 |
my $from_addr = $arg->{'from'}; |
my $from_addr = $arg->{'from'}; |
804 |
|
|
805 |
my $lists = $self->{'loader'}->find_class('lists'); |
my $lists = $self->{'loader'}->find_class('lists'); |
806 |
|
|
807 |
|
$self->_add_aliases( |
808 |
|
list => $name, |
809 |
|
email => $email, |
810 |
|
aliases => $aliases, |
811 |
|
) || warn "can't add alias $email for list $name"; |
812 |
|
|
813 |
my $l = $lists->find_or_create({ |
my $l = $lists->find_or_create({ |
814 |
name => $name, |
name => $name, |
815 |
email => $email, |
email => $email, |
829 |
} |
} |
830 |
|
|
831 |
|
|
832 |
|
|
833 |
=head2 _get_list |
=head2 _get_list |
834 |
|
|
835 |
Get list C<Class::DBI> object. |
Get list C<Class::DBI> object. |
850 |
return $lists->search({ name => lc($name) })->first; |
return $lists->search({ name => lc($name) })->first; |
851 |
} |
} |
852 |
|
|
853 |
|
|
854 |
|
=head2 _remove_alias |
855 |
|
|
856 |
|
Remove list alias |
857 |
|
|
858 |
|
my $ok = $nos->_remove_alias( |
859 |
|
email => 'mylist@example.com', |
860 |
|
aliases => '/etc/mail/mylist', |
861 |
|
); |
862 |
|
|
863 |
|
Returns true if list is removed or false if list doesn't exist. Dies in case of error. |
864 |
|
|
865 |
|
=cut |
866 |
|
|
867 |
|
sub _remove_alias { |
868 |
|
my $self = shift; |
869 |
|
|
870 |
|
my $arg = {@_}; |
871 |
|
|
872 |
|
my $email = lc($arg->{'email'}) || confess "can't remove alias without email"; |
873 |
|
my $aliases = lc($arg->{'aliases'}) || confess "can't remove alias without list"; |
874 |
|
|
875 |
|
my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!"; |
876 |
|
|
877 |
|
if ($a->exists($email)) { |
878 |
|
$a->delete($email) || croak "can't remove alias $email"; |
879 |
|
} else { |
880 |
|
return 0; |
881 |
|
} |
882 |
|
|
883 |
|
return 1; |
884 |
|
|
885 |
|
} |
886 |
|
|
887 |
### |
### |
888 |
### SOAP |
### SOAP |
889 |
### |
### |
908 |
|
|
909 |
my $nos; |
my $nos; |
910 |
|
|
911 |
|
|
912 |
|
=head2 new |
913 |
|
|
914 |
|
Create new SOAP object |
915 |
|
|
916 |
|
my $soap = new Nos::SOAP( |
917 |
|
dsn => 'dbi:Pg:dbname=notices', |
918 |
|
user => 'dpavlin', |
919 |
|
passwd => '', |
920 |
|
debug => 1, |
921 |
|
verbose => 1, |
922 |
|
hash_len => 8, |
923 |
|
aliases => '/etc/aliases', |
924 |
|
); |
925 |
|
|
926 |
|
=cut |
927 |
|
|
928 |
sub new { |
sub new { |
929 |
my $class = shift; |
my $class = shift; |
930 |
my $self = {@_}; |
my $self = {@_}; |
931 |
|
|
932 |
|
croak "need aliases parametar" unless ($self->{'aliases'}); |
933 |
|
|
934 |
bless($self, $class); |
bless($self, $class); |
935 |
|
|
936 |
$nos = new Nos( @_ ) || die "can't create Nos object"; |
$nos = new Nos( @_ ) || die "can't create Nos object"; |
939 |
} |
} |
940 |
|
|
941 |
|
|
942 |
=head2 NewList |
=head2 CreateList |
943 |
|
|
944 |
$message_id = NewList( |
$message_id = CreateList( |
945 |
list => 'My list', |
list => 'My list', |
946 |
from => 'Name of my list', |
from => 'Name of my list', |
947 |
email => 'my-list@example.com' |
email => 'my-list@example.com' |
949 |
|
|
950 |
=cut |
=cut |
951 |
|
|
952 |
sub NewList { |
sub CreateList { |
953 |
my $self = shift; |
my $self = shift; |
954 |
|
|
955 |
|
my $aliases = $nos->{'aliases'} || croak "need 'aliases' argument to new constructor"; |
956 |
|
|
957 |
if ($_[0] !~ m/^HASH/) { |
if ($_[0] !~ m/^HASH/) { |
958 |
return $nos->new_list( |
return $nos->create_list( |
959 |
list => $_[0], from => $_[1], email => $_[2], |
list => $_[0], from => $_[1], email => $_[2], |
960 |
|
aliases => $aliases, |
961 |
); |
); |
962 |
} else { |
} else { |
963 |
return $nos->new_list( %{ shift @_ } ); |
return $nos->create_list( %{ shift @_ }, aliases => $aliases ); |
964 |
} |
} |
965 |
} |
} |
966 |
|
|
967 |
|
|
968 |
=head2 DeleteList |
=head2 DropList |
969 |
|
|
970 |
$ok = DeleteList( |
$ok = DropList( |
971 |
list => 'My list', |
list => 'My list', |
972 |
); |
); |
973 |
|
|
974 |
=cut |
=cut |
975 |
|
|
976 |
sub DeleteList { |
sub DropList { |
977 |
my $self = shift; |
my $self = shift; |
978 |
|
|
979 |
|
my $aliases = $nos->{'aliases'} || croak "need 'aliases' argument to new constructor"; |
980 |
|
|
981 |
if ($_[0] !~ m/^HASH/) { |
if ($_[0] !~ m/^HASH/) { |
982 |
return $nos->delete_list( |
return $nos->drop_list( |
983 |
list => $_[0], |
list => $_[0], |
984 |
|
aliases => $aliases, |
985 |
); |
); |
986 |
} else { |
} else { |
987 |
return $nos->delete_list( %{ shift @_ } ); |
return $nos->drop_list( %{ shift @_ }, aliases => $aliases ); |
988 |
} |
} |
989 |
} |
} |
990 |
|
|