--- trunk/Nos.pm 2005/05/16 20:58:44 29 +++ trunk/Nos.pm 2005/05/16 22:32:58 32 @@ -76,6 +76,7 @@ $self ? return $self : return undef; } + =head2 add_member_to_list Add new member to list @@ -97,9 +98,11 @@ my $arg = {@_}; - my $email = $arg->{'email'} || confess "can't add user without e-mail"; + my $email = $arg->{'email'} || croak "can't add user without e-mail"; my $name = $arg->{'name'} || ''; - confess "need list name" unless ($arg->{'list'}); + my $list_name = $arg->{'list'} || croak "need list name"; + + 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'}); @@ -108,14 +111,9 @@ carp "# $name <$email>\n" if ($self->{'verbose'}); - my $lists = $self->{'loader'}->find_class('lists'); my $users = $self->{'loader'}->find_class('users'); my $user_list = $self->{'loader'}->find_class('user_list'); - my $list = $lists->find_or_create({ - name => $arg->{'list'}, - }) || croak "can't add list ",$arg->{'list'},"\n"; - my $this_user = $users->find_or_create({ email => $email, full_name => $name, @@ -158,11 +156,12 @@ my $list_name = $args->{'list'} || confess "need list name"; my $message_text = $args->{'message'} || croak "need message"; - warn Dumper($message_text); - my $m = Email::Simple->new($message_text) || croak "can't parse message"; - croak "message doesn't have Subject header\n" unless( $m->header('Subject') ); + unless( $m->header('Subject') ) { + warn "message doesn't have Subject header\n"; + return; + } my $lists = $self->{'loader'}->find_class('lists'); @@ -225,34 +224,32 @@ print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; my $msg = $m->message_id->message; - my $auth = Email::Auth::AddressHash->new( - $m->list_id->name, # secret - 10, # hashlen - ); - foreach my $u ($user_list->search(list_id => $m->list_id)) { my $to_email = $u->user_id->email; + my ($from,$domain) = split(/@/, $u->list_id->email, 2); + if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { print "SKIP $to_email message allready sent\n"; } else { - print "\t$to_email\n"; + 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 $hash = $auth->generate_hash( $to_email ); - my $from = $u->list_id->name . " <" . $u->list_id->email . "+" . $hash . ">"; + my $from = $u->list_id->name . " <" . $from . "+" . $hash . ( $domain ? "@" . $domain : '' ). ">"; my $to = $u->user_id->full_name . " <$to_email>"; - my $m = Email::Simple->new($msg) || croak "can't parse message"; + my $m_obj = Email::Simple->new($msg) || croak "can't parse message"; - print Dumper($m); - - $m->header_set('From', $from) || croak "can't set From: header"; - $m->header_set('To', $to) || croak "can't set To: header"; + $m_obj->header_set('From', $from) || croak "can't set From: header"; + $m_obj->header_set('To', $to) || croak "can't set To: header"; # FIXME do real sending :-) - send IO => $m->as_string; + send IO => $m_obj->as_string; $sent->create({ message_id => $m->message_id, @@ -286,6 +283,68 @@ } +=head1 INTERNAL METHODS + +Beware of dragons! You shouldn't need to call those methods directly. + +=head2 _add_list + +Create new list + + my $list_obj = $nos->_add_list( + list => 'My list', + email => 'my-list@example.com', + ); + +Returns C object for created list. + +=cut + +sub _add_list { + my $self = shift; + + my $arg = {@_}; + + my $name = $arg->{'list'} || confess "can't add list without name"; + my $email = $arg->{'email'} || confess "can't add list without e-mail"; + + my $lists = $self->{'loader'}->find_class('lists'); + + my $l = $lists->find_or_create({ + name => $name, + email => $email, + }); + + croak "can't add list $name\n" unless ($l); + + $l->dbi_commit; + + return $l; + +} + + +=head2 _get_list + +Get list C object. + + my $list_obj = $nos->check_list('My list'); + +Returns false on failure. + +=cut + +sub _get_list { + my $self = shift; + + my $name = shift || return; + + my $lists = $self->{'loader'}->find_class('lists') || confess "can't find lists class"; + + return $lists->search({ name => $name })->first; +} + + =head1 EXPORT Nothing.