/[notice-sender]/trunk/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 /trunk/Nos.pm

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

revision 29 by dpavlin, Mon May 16 20:58:44 2005 UTC revision 33 by dpavlin, Tue May 17 11:09:08 2005 UTC
# Line 76  sub new { Line 76  sub new {
76          $self ? return $self : return undef;          $self ? return $self : return undef;
77  }  }
78    
79    
80    =head2 new_list
81    
82    Create new list
83    
84     $nos->new_list(
85            list => 'My list",
86            email => 'my-list@example.com',
87     );
88    
89    Returns ID of newly created list.
90    
91    =cut
92    
93    sub new_list {
94            my $self = shift;
95    
96            my $arg = {@_};
97    
98            confess "need list name" unless ($arg->{'list'});
99            confess "need list email" unless ($arg->{'list'});
100    
101            my $l = $self->_get_list($arg->{'list'}) ||
102                    $self->_add_list( @_ ) ||
103                    return undef;
104    
105            return $l->id;
106    }
107    
108    
109  =head2 add_member_to_list  =head2 add_member_to_list
110    
111  Add new member to list  Add new member to list
# Line 97  sub add_member_to_list { Line 127  sub add_member_to_list {
127    
128          my $arg = {@_};          my $arg = {@_};
129    
130          my $email = $arg->{'email'} || confess "can't add user without e-mail";          my $email = $arg->{'email'} || croak "can't add user without e-mail";
131          my $name = $arg->{'name'} || '';          my $name = $arg->{'name'} || '';
132          confess "need list name" unless ($arg->{'list'});          my $list_name = $arg->{'list'} || croak "need list name";
133    
134            my $list = $self->_get_list($list_name) || croak "list $list_name doesn't exist";
135    
136          if (! Email::Valid->address($email)) {          if (! Email::Valid->address($email)) {
137                  carp "SKIPPING $name <$email>\n" if ($self->{'verbose'});                  carp "SKIPPING $name <$email>\n";
138                  return 0;                  return 0;
139          }          }
140    
141          carp "# $name <$email>\n" if ($self->{'verbose'});          carp "# $name <$email>\n" if ($self->{'verbose'});
142    
         my $lists = $self->{'loader'}->find_class('lists');  
143          my $users = $self->{'loader'}->find_class('users');          my $users = $self->{'loader'}->find_class('users');
144          my $user_list = $self->{'loader'}->find_class('user_list');          my $user_list = $self->{'loader'}->find_class('user_list');
145    
         my $list = $lists->find_or_create({  
                 name => $arg->{'list'},  
         }) || croak "can't add list ",$arg->{'list'},"\n";  
           
146          my $this_user = $users->find_or_create({          my $this_user = $users->find_or_create({
147                  email => $email,                  email => $email,
                 full_name => $name,  
148          }) || croak "can't find or create member\n";          }) || croak "can't find or create member\n";
149    
150            if ($name && $this_user->full_name ne $name) {
151                    $this_user->full_name($name || '');
152                    $this_user->update;
153            }
154    
155          my $user_on_list = $user_list->find_or_create({          my $user_on_list = $user_list->find_or_create({
156                  user_id => $this_user->id,                  user_id => $this_user->id,
157                  list_id => $list->id,                  list_id => $list->id,
# Line 158  sub add_message_to_list { Line 189  sub add_message_to_list {
189          my $list_name = $args->{'list'} || confess "need list name";          my $list_name = $args->{'list'} || confess "need list name";
190          my $message_text = $args->{'message'} || croak "need message";          my $message_text = $args->{'message'} || croak "need message";
191    
         warn Dumper($message_text);  
   
192          my $m = Email::Simple->new($message_text) || croak "can't parse message";          my $m = Email::Simple->new($message_text) || croak "can't parse message";
193    
194          croak "message doesn't have Subject header\n" unless( $m->header('Subject') );          unless( $m->header('Subject') ) {
195                    warn "message doesn't have Subject header\n";
196                    return;
197            }
198    
199          my $lists = $self->{'loader'}->find_class('lists');          my $lists = $self->{'loader'}->find_class('lists');
200    
# Line 225  sub send_queued_messages { Line 257  sub send_queued_messages {
257                  print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n";                  print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n";
258                  my $msg = $m->message_id->message;                  my $msg = $m->message_id->message;
259    
                 my $auth = Email::Auth::AddressHash->new(  
                         $m->list_id->name,      # secret  
                         10,                     # hashlen  
                 );  
   
260                  foreach my $u ($user_list->search(list_id => $m->list_id)) {                  foreach my $u ($user_list->search(list_id => $m->list_id)) {
261    
262                          my $to_email = $u->user_id->email;                          my $to_email = $u->user_id->email;
263    
264                            my ($from,$domain) = split(/@/, $u->list_id->email, 2);
265    
266                          if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {                          if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {
267                                  print "SKIP $to_email message allready sent\n";                                  print "SKIP $to_email message allready sent\n";
268                          } else {                          } else {
269                                  print "\t$to_email\n";                                  print "=> $to_email\n";
270    
271                                    my $secret = $m->list_id->name . " " . $u->user_id->email . " " . $m->message_id;
272                                    my $auth = Email::Auth::AddressHash->new( $secret, 10 );
273    
274                                  my $hash = $auth->generate_hash( $to_email );                                  my $hash = $auth->generate_hash( $to_email );
275    
276                                  my $from = $u->list_id->name . " <" . $u->list_id->email . "+" . $hash . ">";                                  my $from = $u->list_id->name . " <" . $from . "+" . $hash . ( $domain ? "@" . $domain : '' ). ">";
277                                  my $to = $u->user_id->full_name . " <$to_email>";                                  my $to = $u->user_id->full_name . " <$to_email>";
278    
279                                  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);  
280    
281                                  $m->header_set('From', $from) || croak "can't set From: header";                                  $m_obj->header_set('From', $from) || croak "can't set From: header";
282                                  $m->header_set('To', $to) || croak "can't set To: header";                                  $m_obj->header_set('To', $to) || croak "can't set To: header";
283    
284                                  # FIXME do real sending :-)                                  # FIXME do real sending :-)
285                                  send IO => $m->as_string;                                  send IO => $m_obj->as_string;
286    
287                                  $sent->create({                                  $sent->create({
288                                          message_id => $m->message_id,                                          message_id => $m->message_id,
# Line 286  sub inbox_message { Line 316  sub inbox_message {
316  }  }
317    
318    
319    =head1 INTERNAL METHODS
320    
321    Beware of dragons! You shouldn't need to call those methods directly.
322    
323    =head2 _add_list
324    
325    Create new list
326    
327     my $list_obj = $nos->_add_list(
328            list => 'My list',
329            email => 'my-list@example.com',
330     );
331    
332    Returns C<Class::DBI> object for created list.
333    
334    =cut
335    
336    sub _add_list {
337            my $self = shift;
338    
339            my $arg = {@_};
340    
341            my $name = $arg->{'list'} || confess "can't add list without name";
342            my $email = $arg->{'email'} || confess "can't add list without e-mail";
343    
344            my $lists = $self->{'loader'}->find_class('lists');
345    
346            my $l = $lists->find_or_create({
347                    name => $name,
348                    email => $email,
349            });
350            
351            croak "can't add list $name\n" unless ($l);
352    
353            $l->dbi_commit;
354    
355            return $l;
356    
357    }
358    
359    
360    =head2 _get_list
361    
362    Get list C<Class::DBI> object.
363    
364     my $list_obj = $nos->check_list('My list');
365    
366    Returns false on failure.
367    
368    =cut
369    
370    sub _get_list {
371            my $self = shift;
372    
373            my $name = shift || return;
374    
375            my $lists = $self->{'loader'}->find_class('lists') || confess "can't find lists class";
376    
377            return $lists->search({ name => $name })->first;
378    }
379    
380    
381  =head1 EXPORT  =head1 EXPORT
382    
383  Nothing.  Nothing.

Legend:
Removed from v.29  
changed lines
  Added in v.33

  ViewVC Help
Powered by ViewVC 1.1.26