--- trunk/Nos.pm 2005/05/25 15:03:10 52 +++ trunk/Nos.pm 2005/06/21 21:24:10 60 @@ -16,7 +16,7 @@ our @EXPORT = qw( ); -our $VERSION = '0.4'; +our $VERSION = '0.5'; use Class::DBI::Loader; use Email::Valid; @@ -26,6 +26,7 @@ use Email::Simple; use Email::Address; use Mail::DeliveryStatus::BounceParser; +use Class::DBI::AbstractSearch; =head1 NAME @@ -39,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 @@ -74,7 +107,7 @@ user => $self->{'user'}, password => $self->{'passwd'}, namespace => "Nos", -# additional_classes => qw/Class::DBI::AbstractSearch/, + additional_classes => qw/Class::DBI::AbstractSearch/, # additional_base_classes => qw/My::Stuff/, relationships => 1, ) || croak "can't init Class::DBI::Loader"; @@ -98,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 @@ -129,9 +162,10 @@ list => "My list", email => "john.doe@example.com", name => "John A. Doe", + ext_id => 42, ); -C parametar is optional. +C and C parametars are optional. Return member ID if user is added. @@ -145,6 +179,7 @@ my $email = lc($arg->{'email'}) || croak "can't add user without e-mail"; my $name = $arg->{'name'} || ''; my $list_name = lc($arg->{'list'}) || croak "need list name"; + my $ext_id = $arg->{'ext_id'}; my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist"; @@ -167,6 +202,11 @@ $this_user->update; } + if (defined($ext_id) && ($this_user->ext_id || '') ne $ext_id) { + $this_user->ext_id($ext_id); + $this_user->update; + } + my $user_on_list = $user_list->find_or_create({ user_id => $this_user->id, list_id => $list->id, @@ -194,7 +234,8 @@ email => 'dpavlin@rot13.org } -If list is not found, returns false. +If list is not found, returns false. If there is C in user data, +it will also be returned. =cut @@ -218,6 +259,9 @@ email => $user_on_list->user_id->email, }; + my $ext_id = $user_on_list->user_id->ext_id; + $row->{'ext_id'} = $ext_id if (defined($ext_id)); + push @results, $row; } @@ -240,6 +284,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 { @@ -263,6 +310,46 @@ return $users->dbi_commit || croak "can't commit"; } +=head2 delete_member_from_list + +Delete member from particular list. + + my $ok = delete_member_from_list( + list => 'My list', + email => 'dpavlin@rot13.org', + ); + +Returns false if user doesn't exist on that particular list. + +It will die if list or user doesn't exist. You have been warned (you might +want to eval this functon to prevent it from croaking). + +=cut + +sub delete_member_from_list { + my $self = shift; + + my $args = {@_}; + + croak "need list name and email of user to delete" unless ($args->{'list'} && $args->{'email'}); + + $args->{'list'} = lc($args->{'list'}); + $args->{'email'} = lc($args->{'email'}); + + my $user = $self->{'loader'}->find_class('users'); + my $list = $self->{'loader'}->find_class('lists'); + my $user_list = $self->{'loader'}->find_class('user_list'); + + 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; + + $this_user_list->delete || croak "can't delete user from list\n"; + + return $user_list->dbi_commit || croak "can't commit"; +} + =head2 add_message_to_list Adds message to one list's queue for later sending. @@ -463,6 +550,8 @@ message => $message, ); +This method is used by C when receiving e-mail messages. + =cut sub inbox_message { @@ -661,6 +750,7 @@ $message_id = NewList( list => 'My list', + from => 'Name of my list', email => 'my-list@example.com' ); @@ -671,7 +761,7 @@ if ($_[0] !~ m/^HASH/) { return $nos->new_list( - list => $_[0], email => $_[1], + list => $_[0], from => $_[1], email => $_[2], ); } else { return $nos->new_list( %{ shift @_ } ); @@ -684,7 +774,8 @@ $member_id = AddMemberToList( list => 'My list', email => 'e-mail@example.com', - name => 'Full Name' + name => 'Full Name', + ext_id => 42, ); =cut @@ -694,7 +785,7 @@ if ($_[0] !~ m/^HASH/) { return $nos->add_member_to_list( - list => $_[0], email => $_[1], name => $_[2], + list => $_[0], email => $_[1], name => $_[2], ext_id => $_[4], ); } else { return $nos->add_member_to_list( %{ shift @_ } );