/[notice-sender]/jifty-dbi/lib/Nos.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /jifty-dbi/lib/Nos.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 49 by dpavlin, Tue May 24 16:44:34 2005 UTC revision 56 by dpavlin, Tue Jun 21 09:14:54 2005 UTC
# Line 16  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all' Line 16  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'
16  our @EXPORT = qw(  our @EXPORT = qw(
17  );  );
18    
19  our $VERSION = '0.4';  our $VERSION = '0.5';
20    
21  use Class::DBI::Loader;  use Class::DBI::Loader;
22  use Email::Valid;  use Email::Valid;
# Line 108  sub new_list { Line 108  sub new_list {
108          my $arg = {@_};          my $arg = {@_};
109    
110          confess "need list name" unless ($arg->{'list'});          confess "need list name" unless ($arg->{'list'});
111          confess "need list email" unless ($arg->{'list'});          confess "need list email" unless ($arg->{'email'});
112    
113            $arg->{'list'} = lc($arg->{'list'});
114            $arg->{'email'} = lc($arg->{'email'});
115    
116          my $l = $self->_get_list($arg->{'list'}) ||          my $l = $self->_get_list($arg->{'list'}) ||
117                  $self->_add_list( @_ ) ||                  $self->_add_list( @_ ) ||
# Line 126  Add new member to list Line 129  Add new member to list
129          list => "My list",          list => "My list",
130          email => "john.doe@example.com",          email => "john.doe@example.com",
131          name => "John A. Doe",          name => "John A. Doe",
132            ext_id => 42,
133   );   );
134    
135  C<name> parametar is optional.  C<name> and C<ext_id> parametars are optional.
136    
137  Return member ID if user is added.  Return member ID if user is added.
138    
# Line 139  sub add_member_to_list { Line 143  sub add_member_to_list {
143    
144          my $arg = {@_};          my $arg = {@_};
145    
146          my $email = $arg->{'email'} || croak "can't add user without e-mail";          my $email = lc($arg->{'email'}) || croak "can't add user without e-mail";
147          my $name = $arg->{'name'} || '';          my $name = $arg->{'name'} || '';
148          my $list_name = $arg->{'list'} || croak "need list name";          my $list_name = lc($arg->{'list'}) || croak "need list name";
149            my $ext_id = $arg->{'ext_id'};
150    
151          my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist";          my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist";
152    
# Line 164  sub add_member_to_list { Line 169  sub add_member_to_list {
169                  $this_user->update;                  $this_user->update;
170          }          }
171    
172            if (defined($ext_id) && ($this_user->ext_id || '') ne $ext_id) {
173                    $this_user->ext_id($ext_id);
174                    $this_user->update;
175            }
176    
177          my $user_on_list = $user_list->find_or_create({          my $user_on_list = $user_list->find_or_create({
178                  user_id => $this_user->id,                  user_id => $this_user->id,
179                  list_id => $list->id,                  list_id => $list->id,
# Line 191  Returns array of hashes with user inform Line 201  Returns array of hashes with user inform
201          email => 'dpavlin@rot13.org          email => 'dpavlin@rot13.org
202   }   }
203    
204  If list is not found, returns false.  If list is not found, returns false. If there is C<ext_id> in user data,
205    that will also be returned.
206    
207  =cut  =cut
208    
# Line 200  sub list_members { Line 211  sub list_members {
211    
212          my $args = {@_};          my $args = {@_};
213    
214          my $list_name = $args->{'list'} || confess "need list name";          my $list_name = lc($args->{'list'}) || confess "need list name";
215    
216          my $lists = $self->{'loader'}->find_class('lists');          my $lists = $self->{'loader'}->find_class('lists');
217          my $user_list = $self->{'loader'}->find_class('user_list');          my $user_list = $self->{'loader'}->find_class('user_list');
# Line 215  sub list_members { Line 226  sub list_members {
226                          email => $user_on_list->user_id->email,                          email => $user_on_list->user_id->email,
227                  };                  };
228    
229                    my $ext_id = $user_on_list->user_id->ext_id;
230                    $row->{'ext_id'} = $ext_id if (defined($ext_id));
231    
232                  push @results, $row;                  push @results, $row;
233          }          }
234    
# Line 246  sub delete_member { Line 260  sub delete_member {
260    
261          croak "need name or email of user to delete" unless ($args->{'name'} || $args->{'email'});          croak "need name or email of user to delete" unless ($args->{'name'} || $args->{'email'});
262    
263            $args->{'email'} = lc($args->{'email'}) if ($args->{'email'});
264    
265          my $key = 'name';          my $key = 'name';
266          $key = 'email' if ($args->{'email'});          $key = 'email' if ($args->{'email'});
267    
# Line 283  sub add_message_to_list { Line 299  sub add_message_to_list {
299    
300          my $args = {@_};          my $args = {@_};
301    
302          my $list_name = $args->{'list'} || confess "need list name";          my $list_name = lc($args->{'list'}) || confess "need list name";
303          my $message_text = $args->{'message'} || croak "need message";          my $message_text = $args->{'message'} || croak "need message";
304    
305          my $m = Email::Simple->new($message_text) || croak "can't parse message";          my $m = Email::Simple->new($message_text) || croak "can't parse message";
# Line 352  sub send_queued_messages { Line 368  sub send_queued_messages {
368    
369          my $arg = {@_};          my $arg = {@_};
370    
371          my $list_name = $arg->{'list'} || '';          my $list_name = lc($arg->{'list'}) || '';
372          my $driver = $arg->{'driver'} || '';          my $driver = $arg->{'driver'} || '';
373          my $sleep = $arg->{'sleep'};          my $sleep = $arg->{'sleep'};
374          $sleep ||= 3 unless defined($sleep);          $sleep ||= 3 unless defined($sleep);
# Line 363  sub send_queued_messages { Line 379  sub send_queued_messages {
379          if (lc($driver) eq 'smtp') {          if (lc($driver) eq 'smtp') {
380                  $email_send_driver = 'Email::Send::SMTP';                  $email_send_driver = 'Email::Send::SMTP';
381                  @email_send_options = ['127.0.0.1'];                  @email_send_options = ['127.0.0.1'];
382            } else {
383                    warn "dumping all messages to STDERR\n";
384          }          }
         warn "using $driver [$email_send_driver]\n";  
385    
386          my $lists = $self->{'loader'}->find_class('lists');          my $lists = $self->{'loader'}->find_class('lists');
387          my $queue = $self->{'loader'}->find_class('queue');          my $queue = $self->{'loader'}->find_class('queue');
# Line 467  sub inbox_message { Line 484  sub inbox_message {
484          return unless ($arg->{'message'});          return unless ($arg->{'message'});
485          croak "need list name" unless ($arg->{'list'});          croak "need list name" unless ($arg->{'list'});
486    
487            $arg->{'list'} = lc($arg->{'list'});
488    
489          my $this_list = $self->_get_list($arg->{'list'}) || croak "can't find list ".$arg->{'list'}."\n";          my $this_list = $self->_get_list($arg->{'list'}) || croak "can't find list ".$arg->{'list'}."\n";
490    
491          my $m = Email::Simple->new($arg->{'message'}) || croak "can't parse message";          my $m = Email::Simple->new($arg->{'message'}) || croak "can't parse message";
# Line 484  sub inbox_message { Line 503  sub inbox_message {
503          my $hash;          my $hash;
504    
505          foreach my $a (@addrs) {          foreach my $a (@addrs) {
506                  if ($a->address =~ m/\+([a-f0-9]{$hl})@/) {                  if ($a->address =~ m/\+([a-f0-9]{$hl})@/i) {
507                          $hash = $1;                          $hash = $1;
508                          last;                          last;
509                  }                  }
510          }          }
511    
512          warn "can't find hash in e-mail $to\n" unless ($hash);          #warn "can't find hash in e-mail $to\n" unless ($hash);
513    
514          my $sent = $self->{'loader'}->find_class('sent');          my $sent = $self->{'loader'}->find_class('sent');
515    
516          # will use null if no matching message_id is found          # will use null if no matching message_id is found
517          my $sent_msg = $sent->search( hash => $hash )->first;          my $sent_msg;
518            $sent_msg = $sent->search( hash => $hash )->first if ($hash);
519    
520          my ($message_id, $user_id) = (undef, undef);    # init with NULL          my ($message_id, $user_id) = (undef, undef);    # init with NULL
521    
# Line 503  sub inbox_message { Line 523  sub inbox_message {
523                  $message_id = $sent_msg->message_id || carp "no message_id";                  $message_id = $sent_msg->message_id || carp "no message_id";
524                  $user_id = $sent_msg->user_id || carp "no user_id";                  $user_id = $sent_msg->user_id || carp "no user_id";
525          } else {          } else {
526                  warn "can't find sender with hash $hash\n";                  #warn "can't find sender with hash $hash\n";
527                    my $users = $self->{'loader'}->find_class('users');
528                    my $from = $m->header('From');
529                    $from = $1 if ($from =~ m/<(.*)>/);
530                    my $this_user = $users->search( email => lc($from) )->first;
531                    $user_id = $this_user->id if ($this_user);
532          }          }
533    
534    
# Line 514  sub inbox_message { Line 539  sub inbox_message {
539                  my $bounce = eval { Mail::DeliveryStatus::BounceParser->new(                  my $bounce = eval { Mail::DeliveryStatus::BounceParser->new(
540                          $arg->{'message'}, { report_non_bounces=>1 },                          $arg->{'message'}, { report_non_bounces=>1 },
541                  ) };                  ) };
542                  warn "can't check if this message is bounce!" if ($@);                  #warn "can't check if this message is bounce!" if ($@);
543                    
544                  $is_bounce++ if ($bounce && $bounce->is_bounce);                  $is_bounce++ if ($bounce && $bounce->is_bounce);
545          }          }
# Line 563  sub _add_list { Line 588  sub _add_list {
588    
589          my $arg = {@_};          my $arg = {@_};
590    
591          my $name = $arg->{'list'} || confess "can't add list without name";          my $name = lc($arg->{'list'}) || confess "can't add list without name";
592          my $email = $arg->{'email'} || confess "can't add list without e-mail";          my $email = lc($arg->{'email'}) || confess "can't add list without e-mail";
593          my $from_addr = $arg->{'from'};          my $from_addr = $arg->{'from'};
594    
595          my $lists = $self->{'loader'}->find_class('lists');          my $lists = $self->{'loader'}->find_class('lists');
# Line 605  sub _get_list { Line 630  sub _get_list {
630    
631          my $lists = $self->{'loader'}->find_class('lists') || confess "can't find lists class";          my $lists = $self->{'loader'}->find_class('lists') || confess "can't find lists class";
632    
633          return $lists->search({ name => $name })->first;          return $lists->search({ name => lc($name) })->first;
634  }  }
635    
636  ###  ###
# Line 647  sub new { Line 672  sub new {
672    
673   $message_id = NewList(   $message_id = NewList(
674          list => 'My list',          list => 'My list',
675            from => 'Name of my list',
676          email => 'my-list@example.com'          email => 'my-list@example.com'
677   );   );
678    
# Line 657  sub NewList { Line 683  sub NewList {
683    
684          if ($_[0] !~ m/^HASH/) {          if ($_[0] !~ m/^HASH/) {
685                  return $nos->new_list(                  return $nos->new_list(
686                          list => $_[0], email => $_[1],                          list => $_[0], from => $_[1], email => $_[2],
687                  );                  );
688          } else {          } else {
689                  return $nos->new_list( %{ shift @_ } );                  return $nos->new_list( %{ shift @_ } );

Legend:
Removed from v.49  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.26