--- trunk/Nos.pm 2005/05/17 17:49:14 36 +++ trunk/Nos.pm 2005/05/17 22:23:40 39 @@ -25,6 +25,7 @@ use Email::Auth::AddressHash; use Email::Simple; use Email::Address; +use Mail::DeliveryStatus::BounceParser; use Data::Dumper; =head1 NAME @@ -55,8 +56,8 @@ hash_len => 8, ); -Parametar C defined length of hash which will be added to each -outgoing e-mail message. +Parametar C defines length of hash which will be added to each +outgoing e-mail message to ensure that replies can be linked with sent e-mails. =cut @@ -86,15 +87,18 @@ =head2 new_list -Create new list +Create new list. Required arguments are name of C and +C address. $nos->new_list( - list => 'My list", + list => 'My list', email => 'my-list@example.com', ); Returns ID of newly created list. +Calls internally L<_add_list>, see details there. + =cut sub new_list { @@ -178,7 +182,7 @@ $nos->add_message_to_list( list => 'My list', message => 'Subject: welcome to list - + This is example message ', ); @@ -291,6 +295,9 @@ $m_obj->header_set('From', $from) || croak "can't set From: header"; $m_obj->header_set('To', $to) || croak "can't set To: header"; + $m_obj->header_set('X-Nos-Version', $VERSION); + $m_obj->header_set('X-Nos-Hash', $hash); + # FIXME do real sending :-) send IO => $m_obj->as_string; @@ -328,6 +335,8 @@ return unless ($arg->{'message'}); croak "need list name" unless ($arg->{'list'}); + my $this_list = $self->_get_list($arg->{'list'}) || croak "can't find list ".$arg->{'list'}."\n"; + my $m = Email::Simple->new($arg->{'message'}) || croak "can't parse message"; my $to = $m->header('To') || die "can't find To: address in incomming message\n"; @@ -352,9 +361,37 @@ my $sent = $self->{'loader'}->find_class('sent'); # will use null if no matching message_id is found - my $message_id = $sent->search( hash => $hash )->first->message_id; + my $sent_msg = $sent->search( hash => $hash )->first; + + my ($message_id, $user_id) = (undef, undef); # init with NULL + + if ($sent_msg) { + $message_id = $sent_msg->message_id || carp "no message_id"; + $user_id = $sent_msg->user_id || carp "no user_id"; + } -print "message_id: $message_id\n"; +print "message_id: ",($message_id || "not found"),"\n"; + + my $is_bounce = 0; + + my $bounce = eval { Mail::DeliveryStatus::BounceParser->new( + $arg->{'message'}, { report_non_bounces=>1 }, + ) }; + carp "can't check if this message is bounce!" if ($@); + + $is_bounce++ if ($bounce && $bounce->is_bounce); + + my $received = $self->{'loader'}->find_class('received'); + + my $this_received = $received->find_or_create({ + user_id => $user_id, + list_id => $this_list->id, + message_id => $message_id, + message => $arg->{'message'}, + bounced => $is_bounce, + }) || croak "can't insert received message"; + + $this_received->dbi_commit; warn "inbox is not yet implemented"; } @@ -375,6 +412,11 @@ Returns C object for created list. +C address can be with domain or without it if your +MTA appends it. There is no checking for validity of your +list e-mail. Flexibility comes with resposibility, so please +feed correct (and configured) return addresses. + =cut sub _add_list { @@ -421,6 +463,105 @@ return $lists->search({ name => $name })->first; } +### +### SOAP +### + +package Nos::SOAP; + +=head1 SOAP methods + +This methods are thin wrappers to provide SOAP calls. They are grouped in +C package which is in same F module file. + +Usually, you want to use named variables in your SOAP calls if at all +possible. + +However, if you have broken SOAP library (like PHP SOAP class from PEAR) +you will want to use positional arguments (in same order as documented for +methods below). + +=cut + +my $nos; + +sub new { + my $class = shift; + my $self = {@_}; + bless($self, $class); + + $nos = new Nos( @_ ) || die "can't create Nos object"; + + $self ? return $self : return undef; +} + + +=head2 NewList + + $message_id = NewList( + list => 'My list', + email => 'my-list@example.com' + ); + +=cut + +sub NewList { + my $self = shift; + + if ($_[0] !~ m/^HASH/) { + return $nos->new_list( + list => $_[0], email => $_[1], + ); + } else { + return $nos->new_list( %{ shift @_ } ); + } +} + +=head2 AddMemberToList + + $member_id = AddMemberToList( + list => "My list", + email => "e-mail@example.com", + name => "Full Name" + ); + +=cut + +sub AddMemberToList { + my $self = shift; + + if ($_[0] !~ m/^HASH/) { + return $nos->add_member_to_list( + list => $_[0], email => $_[1], name => $_[2], + ); + } else { + return $nos->add_member_to_list( %{ shift @_ } ); + } +} + +=head2 AddMessageToList + + $message_id = AddMessageToList( + list => 'My list', + message => 'From: My list...' + ); + +=cut + +sub AddMessageToList { + my $self = shift; + + if ($_[0] !~ m/^HASH/) { + return $nos->add_message_to_list( + list => $_[0], message => $_[1], + ); + } else { + return $nos->add_message_to_list( %{ shift @_ } ); + } +} + + +### =head1 EXPORT @@ -446,3 +587,5 @@ =cut + +1;