--- trunk/Nos.pm 2005/06/22 12:31:45 62 +++ trunk/Nos.pm 2005/08/25 00:56:06 78 @@ -16,7 +16,7 @@ our @EXPORT = qw( ); -our $VERSION = '0.5'; +our $VERSION = '0.8'; use Class::DBI::Loader; use Email::Valid; @@ -27,6 +27,9 @@ use Email::Address; use Mail::DeliveryStatus::BounceParser; use Class::DBI::AbstractSearch; +use SQL::Abstract; +use Mail::Alias; +use Cwd qw(abs_path); =head1 NAME @@ -60,9 +63,9 @@ It will just queue your e-mail message to particular list (sending it to possibly remote Notice Sender SOAP server just once), send it out at reasonable rate (so that it doesn't flood your e-mail infrastructure) and -track replies. +keep track replies. -It is best used to send smaller number of messages to more-or-less fixed +It is best used to send small number of messages to more-or-less fixed list of recipients while allowing individual responses to be examined. Tipical use include replacing php e-mail sending code with SOAP call to Notice Sender. It does support additional C field for each member @@ -70,7 +73,7 @@ particular user. It comes with command-line utility C which can be used to perform -all available operation from scripts (see C). +all available operation from scripts (see C). This command is also useful for debugging while writing client SOAP application. @@ -118,15 +121,17 @@ } -=head2 new_list +=head2 create_list -Create new list. Required arguments are name of C and -C address. +Create new list. Required arguments are name of C, C address +and path to C file. - $nos->new_list( + $nos->create_list( list => 'My list', from => 'Outgoing from comment', email => 'my-list@example.com', + aliases => '/etc/mail/mylist', + archive => '/path/to/mbox/archive', ); Returns ID of newly created list. @@ -135,7 +140,7 @@ =cut -sub new_list { +sub create_list { my $self = shift; my $arg = {@_}; @@ -154,6 +159,42 @@ } +=head2 drop_list + +Delete list from database. + + my $ok = drop_list( + list => 'My list' + aliases => '/etc/mail/mylist', + ); + +Returns false if list doesn't exist. + +=cut + +sub drop_list { + my $self = shift; + + my $args = {@_}; + + croak "need list to delete" unless ($args->{'list'}); + + $args->{'list'} = lc($args->{'list'}); + + my $aliases = $args->{'aliases'} || croak "need path to aliases file"; + + my $lists = $self->{'loader'}->find_class('lists'); + + my $this_list = $lists->search( name => $args->{'list'} )->first || return; + + $self->_remove_alias( email => $this_list->email, aliases => $aliases); + + $this_list->delete || croak "can't delete list\n"; + + return $lists->dbi_commit || croak "can't commit"; +} + + =head2 add_member_to_list Add new member to list @@ -227,7 +268,7 @@ list => 'My list', ); -Returns array of hashes with user informations like this: +Returns array of hashes with user information like this: $member = { name => 'Dobrica Pavlinusic', @@ -435,8 +476,12 @@ =back +Any other driver name will try to use C module. + Default sleep wait between two messages is 3 seconds. +This method will return number of succesfully sent messages. + =cut sub send_queued_messages { @@ -449,12 +494,17 @@ my $sleep = $arg->{'sleep'}; $sleep ||= 3 unless defined($sleep); + # number of messages sent o.k. + my $ok = 0; + my $email_send_driver = 'Email::Send::IO'; my @email_send_options; if (lc($driver) eq 'smtp') { $email_send_driver = 'Email::Send::SMTP'; @email_send_options = ['127.0.0.1']; + } elsif ($driver && $driver ne '') { + $email_send_driver = 'Email::Send::' . $driver; } else { warn "dumping all messages to STDERR\n"; } @@ -489,7 +539,7 @@ if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { print "SKIP $to_email message allready sent\n"; } else { - print "=> $to_email\n"; + print "=> $to_email "; my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id; my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} ); @@ -515,18 +565,34 @@ $m_obj->header_set('X-Nos-Hash', $hash); # really send e-mail + my $sent_status; + if (@email_send_options) { - send $email_send_driver => $m_obj->as_string, @email_send_options; + $sent_status = send $email_send_driver => $m_obj->as_string, @email_send_options; } else { - send $email_send_driver => $m_obj->as_string; + $sent_status = send $email_send_driver => $m_obj->as_string; } - $sent->create({ - message_id => $m->message_id, - user_id => $u->user_id, - hash => $hash, - }); - $sent->dbi_commit; + croak "can't send e-mail: $sent_status\n\nOriginal e-mail follows:\n".$m_obj->as_string unless ($sent_status); + my @bad; + @bad = @{ $sent_status->prop('bad') } if (eval { $sent_status->can('prop') }); + croak "failed sending to ",join(",",@bad) if (@bad); + + if ($sent_status) { + + $sent->create({ + message_id => $m->message_id, + user_id => $u->user_id, + hash => $hash, + }); + $sent->dbi_commit; + + print " - $sent_status\n"; + + $ok++; + } else { + warn "ERROR: $sent_status\n"; + } if ($sleep) { warn "sleeping $sleep seconds\n"; @@ -539,6 +605,8 @@ $m->dbi_commit; } + return $ok; + } =head2 inbox_message @@ -637,11 +705,143 @@ # print "message_id: ",($message_id || "not found")," -- $is_bounce\n"; } +=head2 received_messages + +Returns all received messages for given list or user. + + my @received = $nos->received_message( + list => 'My list', + email => "john.doe@example.com", + ); + +Each element in returned array will have following structure: + + { + id => 42, # unique ID of received message + list => 'My list', # useful if filtering by email + ext_id => 9999, # ext_id from message sender + email => 'jdoe@example.com', # e-mail of message sender + bounced => 0, # true if message is bounce + date => '2005-08-24 18:57:24', # date of receival in ISO format + } + + +=cut + +sub received_messages { + my $self = shift; + + my $arg = {@_} if (@_); + +# croak "need list name or email" unless ($arg->{'list'} || $arg->{'email'}); + + my $sql = qq{ + select + received.id as id, + lists.name as list, + users.ext_id as ext_id, + users.email as email, + bounced,received.date as date + from received + join lists on lists.id = list_id + join users on users.id = user_id + }; + + my $where; + + $where->{'lists.name'} = lc($arg->{'list'}) if ($arg->{'list'}); + $where->{'users.email'} = lc($arg->{'email'}) if ($arg->{'email'}); + + # hum, yammy one-liner + my($stmt, @bind) = SQL::Abstract->new->where($where); + + my $dbh = $self->{'loader'}->find_class('received')->db_Main; + + my $sth = $dbh->prepare($sql . $stmt); + $sth->execute(@bind); + return $sth->fetchall_hash; +} + =head1 INTERNAL METHODS Beware of dragons! You shouldn't need to call those methods directly. + +=head2 _add_aliases + +Add or update alias in C (or equivalent) file for selected list + + my $ok = $nos->add_aliases( + list => 'My list', + email => 'my-list@example.com', + aliases => '/etc/mail/mylist', + archive => '/path/to/mbox/archive', + + ); + +C parametar is optional. + +Return false on failure. + +=cut + +sub _add_aliases { + my $self = shift; + + my $arg = {@_}; + + foreach my $o (qw/list email aliases/) { + croak "need $o option" unless ($arg->{$o}); + } + + my $aliases = $arg->{'aliases'}; + my $email = $arg->{'email'}; + my $list = $arg->{'list'}; + + unless (-e $aliases) { + warn "aliases file $aliases doesn't exist, creating empty\n"; + open(my $fh, '>', $aliases) || croak "can't create $aliases: $!"; + close($fh); + chmod 0777, $aliases || warn "can't change permission to 0777"; + } + + die "FATAL: aliases file $aliases is not writable\n" unless (-w $aliases); + + my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!"; + + my $target = ''; + + if (my $archive = $arg->{'archive'}) { + $target .= "$archive, "; + + if (! -e $archive) { + warn "please make sure that file $archive is writable for your e-mail user (defaulting to bad 777 permission for now)"; + + open(my $fh, '>', $archive) || croak "can't create archive file $archive: $!"; + close($fh); + chmod 0777, $archive || croak "can't chmod archive file $archive to 0777: $!"; + } + } + + # resolve my path to absolute one + my $self_path = abs_path($0); + $self_path =~ s#/[^/]+$##; + $self_path =~ s#/t/*$#/#; + + $target .= qq#| cd $self_path && ./sender.pl --inbox="$list"#; + + if ($a->exists($email)) { + $a->update($email, $target) or croak "can't update alias ".$a->error_check; + } else { + $a->append($email, $target) or croak "can't add alias ".$a->error_check; + } + + #$a->write($aliases) or croak "can't save aliases $aliases ".$a->error_check; + + return 1; +} + =head2 _add_list Create new list @@ -650,6 +850,7 @@ list => 'My list', from => 'Outgoing from comment', email => 'my-list@example.com', + aliases => '/etc/mail/mylist', ); Returns C object for created list. @@ -668,10 +869,18 @@ my $name = lc($arg->{'list'}) || confess "can't add list without name"; my $email = lc($arg->{'email'}) || confess "can't add list without e-mail"; + my $aliases = lc($arg->{'aliases'}) || confess "can't add list without path to aliases file"; + my $from_addr = $arg->{'from'}; my $lists = $self->{'loader'}->find_class('lists'); + $self->_add_aliases( + list => $name, + email => $email, + aliases => $aliases, + ) || warn "can't add alias $email for list $name"; + my $l = $lists->find_or_create({ name => $name, email => $email, @@ -691,6 +900,7 @@ } + =head2 _get_list Get list C object. @@ -711,6 +921,40 @@ return $lists->search({ name => lc($name) })->first; } + +=head2 _remove_alias + +Remove list alias + + my $ok = $nos->_remove_alias( + email => 'mylist@example.com', + aliases => '/etc/mail/mylist', + ); + +Returns true if list is removed or false if list doesn't exist. Dies in case of error. + +=cut + +sub _remove_alias { + my $self = shift; + + my $arg = {@_}; + + my $email = lc($arg->{'email'}) || confess "can't remove alias without email"; + my $aliases = lc($arg->{'aliases'}) || confess "can't remove alias without list"; + + my $a = new Mail::Alias($aliases) || croak "can't open aliases file $aliases: $!"; + + if ($a->exists($email)) { + $a->delete($email) || croak "can't remove alias $email"; + } else { + return 0; + } + + return 1; + +} + ### ### SOAP ### @@ -735,9 +979,33 @@ my $nos; + +=head2 new + +Create new SOAP object + + my $soap = new Nos::SOAP( + dsn => 'dbi:Pg:dbname=notices', + user => 'dpavlin', + passwd => '', + debug => 1, + verbose => 1, + hash_len => 8, + aliases => '/etc/aliases', + ); + +If you are writing SOAP server (like C example), you will need to +call this method once to make new instance of Nos::SOAP and specify C +and options for it. + +=cut + sub new { my $class = shift; my $self = {@_}; + + croak "need aliases parametar" unless ($self->{'aliases'}); + bless($self, $class); $nos = new Nos( @_ ) || die "can't create Nos object"; @@ -746,9 +1014,9 @@ } -=head2 NewList +=head2 CreateList - $message_id = NewList( + $message_id = CreateList( list => 'My list', from => 'Name of my list', email => 'my-list@example.com' @@ -756,19 +1024,45 @@ =cut -sub NewList { +sub CreateList { my $self = shift; + my $aliases = $nos->{'aliases'} || croak "need 'aliases' argument to new constructor"; + if ($_[0] !~ m/^HASH/) { - return $nos->new_list( + return $nos->create_list( list => $_[0], from => $_[1], email => $_[2], + aliases => $aliases, ); } else { - return $nos->new_list( %{ shift @_ } ); + return $nos->create_list( %{ shift @_ }, aliases => $aliases ); } } +=head2 DropList + + $ok = DropList( + list => 'My list', + ); + +=cut + +sub DropList { + my $self = shift; + + my $aliases = $nos->{'aliases'} || croak "need 'aliases' argument to new constructor"; + + if ($_[0] !~ m/^HASH/) { + return $nos->drop_list( + list => $_[0], + aliases => $aliases, + ); + } else { + return $nos->drop_list( %{ shift @_ }, aliases => $aliases ); + } +} + =head2 AddMemberToList $member_id = AddMemberToList( @@ -801,10 +1095,6 @@ Returns array of hashes with user informations, see C. -Returning arrays from SOAP calls is somewhat fuzzy (at least to me). It -seems that SOAP::Lite client thinks that it has array with one element which -is array of hashes with data. - =cut sub ListMembers { @@ -865,9 +1155,61 @@ } } +=head2 MessagesReceived + +Return statistics about received messages. + + my @result = MessagesReceived( + list => 'My list', + email => 'jdoe@example.com', + ); + +You must specify C or C or any combination of those. + +For format of returned array element see C. + +=cut + +sub MessagesReceived { + my $self = shift; + + if ($_[0] !~ m/^HASH/) { + die "need both list and email" unless (scalar @_ < 2); + return $nos->received_messages( + list => $_[0], email => $_[1], + ); + } else { + my $arg = {@_}; + die "need both list and email" unless ($arg->{'list'} && $arg->{'email'}); + return $nos->received_messages( $arg ); + } +} ### +=head1 UNIMPLEMENTED SOAP FUNCTIONS + +This is a stub for documentation of unimplemented functions. + +=head2 MessagesReceivedByDate + +=head2 MessagesReceivedByDateWithContent + +=head2 ReceivedMessageContent + +Return content of received message. + + my $mail_body = ReceivedMessageContent( id => 42 ); + + + + +=head1 NOTE ON ARRAYS IN SOAP + +Returning arrays from SOAP calls is somewhat fuzzy (at least to me). It +seems that SOAP::Lite client thinks that it has array with one element which +is array of hashes with data. + =head1 EXPORT Nothing.