--- trunk/Nos.pm 2005/06/21 20:49:27 59 +++ trunk/Nos.pm 2005/06/29 17:05:30 65 @@ -40,7 +40,39 @@ =head1 DESCRIPTION -Core module for notice sender's functionality. +Notice sender is mail handler. It is not MTA, since it doesn't know how to +receive e-mails or send them directly to other hosts. It is not mail list +manager because it requires programming to add list members and send +messages. You can think of it as mechanisam for off-loading your e-mail +sending to remote server using SOAP service. + +It's concept is based around B. Each list can have zero or more +B. Each list can have zero or more B. + +Here comes a twist: each outgoing message will have unique e-mail generated, +so Notice Sender will be able to link received replies (or bounces) with +outgoing messages. + +It doesn't do much more than that. It B create MIME encoded e-mail, +send attachments, handle 8-bit characters in headers (which have to be +encoded) or anything else. + +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. + +It is best used to send smaller 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 +which can be used to track some unique identifier from remote system for +particular user. + +It comes with command-line utility C which can be used to perform +all available operation from scripts (see C). +This command is also useful for debugging while writing client SOAP +application. =head1 METHODS @@ -99,7 +131,7 @@ Returns ID of newly created list. -Calls internally L<_add_list>, see details there. +Calls internally C<_add_list>, see details there. =cut @@ -122,6 +154,37 @@ } +=head2 delete_list + +Delete list from database. + + my $ok = delete_list( + list => 'My list' + ); + +Returns false if list doesn't exist. + +=cut + +sub delete_list { + my $self = shift; + + my $args = {@_}; + + croak "need list to delete" unless ($args->{'list'}); + + $args->{'list'} = lc($args->{'list'}); + + my $lists = $self->{'loader'}->find_class('lists'); + + my $this_list = $lists->search( name => $args->{'list'} )->first || return; + + $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 @@ -203,7 +266,7 @@ } If list is not found, returns false. If there is C in user data, -that will also be returned. +it will also be returned. =cut @@ -252,6 +315,9 @@ Returns false if user doesn't exist. +This function will delete member from all lists (by cascading delete), so it +shouldn't be used lightly. + =cut sub delete_member { @@ -308,7 +374,7 @@ my $this_user = $user->search( email => $args->{'email'} )->first || croak "can't find user: ".$args->{'email'}; my $this_list = $list->search( name => $args->{'list'} )->first || croak "can't find list: ".$args->{'list'}; - my $this_user_list = $user_list->search_where( list_id => $this_list->id, user_id => $this_list->id )->first || return; + my $this_user_list = $user_list->search_where( list_id => $this_list->id, user_id => $this_user->id )->first || return; $this_user_list->delete || croak "can't delete user from list\n"; @@ -454,7 +520,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'} ); @@ -480,18 +546,32 @@ $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 = @{ $sent_status->prop('bad') }; + 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"; + + } else { + warn "ERROR: $sent_status\n"; + } if ($sleep) { warn "sleeping $sleep seconds\n"; @@ -515,6 +595,8 @@ message => $message, ); +This method is used by C when receiving e-mail messages. + =cut sub inbox_message { @@ -732,6 +814,26 @@ } +=head2 DeleteList + + $ok = DeleteList( + list => 'My list', + ); + +=cut + +sub DeleteList { + my $self = shift; + + if ($_[0] !~ m/^HASH/) { + return $nos->delete_list( + list => $_[0], + ); + } else { + return $nos->delete_list( %{ shift @_ } ); + } +} + =head2 AddMemberToList $member_id = AddMemberToList( @@ -764,6 +866,10 @@ 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 { @@ -777,9 +883,32 @@ $list_name = $_[0]->{'list'}; } - return $nos->list_members( list => $list_name ); + return [ $nos->list_members( list => $list_name ) ]; +} + + +=head2 DeleteMemberFromList + + $member_id = DeleteMemberFromList( + list => 'My list', + email => 'e-mail@example.com', + ); + +=cut + +sub DeleteMemberFromList { + my $self = shift; + + if ($_[0] !~ m/^HASH/) { + return $nos->delete_member_from_list( + list => $_[0], email => $_[1], + ); + } else { + return $nos->delete_member_from_list( %{ shift @_ } ); + } } + =head2 AddMessageToList $message_id = AddMessageToList(