--- trunk/Nos.pm 2005/05/16 22:32:58 32 +++ trunk/Nos.pm 2005/05/17 19:15:27 37 @@ -24,6 +24,8 @@ use Carp; use Email::Auth::AddressHash; use Email::Simple; +use Email::Address; +use Mail::DeliveryStatus::BounceParser; use Data::Dumper; =head1 NAME @@ -51,8 +53,12 @@ passwd => '', debug => 1, verbose => 1, + hash_len => 8, ); +Parametar C defined length of hash which will be added to each +outgoing e-mail message. + =cut sub new { @@ -73,10 +79,41 @@ relationships => 1, ) || croak "can't init Class::DBI::Loader"; + $self->{'hash_len'} ||= 8; + $self ? return $self : return undef; } +=head2 new_list + +Create new list + + $nos->new_list( + list => 'My list", + email => 'my-list@example.com', + ); + +Returns ID of newly created list. + +=cut + +sub new_list { + my $self = shift; + + my $arg = {@_}; + + confess "need list name" unless ($arg->{'list'}); + confess "need list email" unless ($arg->{'list'}); + + my $l = $self->_get_list($arg->{'list'}) || + $self->_add_list( @_ ) || + return undef; + + return $l->id; +} + + =head2 add_member_to_list Add new member to list @@ -105,7 +142,7 @@ my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist"; if (! Email::Valid->address($email)) { - carp "SKIPPING $name <$email>\n" if ($self->{'verbose'}); + carp "SKIPPING $name <$email>\n"; return 0; } @@ -116,9 +153,13 @@ my $this_user = $users->find_or_create({ email => $email, - full_name => $name, }) || croak "can't find or create member\n"; + if ($name && $this_user->full_name ne $name) { + $this_user->full_name($name || ''); + $this_user->update; + } + my $user_on_list = $user_list->find_or_create({ user_id => $this_user->id, list_id => $list->id, @@ -137,8 +178,7 @@ $nos->add_message_to_list( list => 'My list', - message => 'From: My list - To: John A. Doe + message => 'Subject: welcome to list This is example message ', @@ -146,6 +186,10 @@ On success returns ID of newly created (or existing) message. +Only required header in e-mail is C. C and C headers +will be automatically generated, but if you want to use own headers, just +include them in messages. + =cut sub add_message_to_list { @@ -236,7 +280,7 @@ print "=> $to_email\n"; my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id; - my $auth = Email::Auth::AddressHash->new( $secret, 10 ); + my $auth = Email::Auth::AddressHash->new( $secret, $self->{'hash_len'} ); my $hash = $auth->generate_hash( $to_email ); @@ -254,6 +298,7 @@ $sent->create({ message_id => $m->message_id, user_id => $u->user_id, + hash => $hash, }); $sent->dbi_commit; } @@ -269,17 +314,80 @@ Receive single message for list's inbox. - my $ok = $nos->inbox_message($message); + my $ok = $nos->inbox_message( + list => 'My list', + message => $message, + ); =cut sub inbox_message { my $self = shift; - my $message = shift || return; + my $arg = {@_}; + + 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"; + + my @addrs = Email::Address->parse( $to ); + + die "can't parse To: $to address\n" unless (@addrs); + + my $hl = $self->{'hash_len'} || confess "no hash_len?"; + + my $hash; + + foreach my $a (@addrs) { + if ($a->address =~ m/\+([a-f0-9]{$hl})@/) { + $hash = $1; + last; + } + } + + croak "can't find hash in e-mail $to\n" unless ($hash); + + my $sent = $self->{'loader'}->find_class('sent'); + + # will use null if no matching message_id is found + 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 || "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"; - my $m = new Email::Simple->new($message); + $this_received->dbi_commit; + warn "inbox is not yet implemented"; }