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

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

revision 15 by dpavlin, Sun May 15 16:25:01 2005 UTC revision 24 by dpavlin, Sun May 15 22:30:54 2005 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use Class::DBI::Loader::Pg;  use blib;
5    use Nos;
6  use Getopt::Long;  use Getopt::Long;
 use Mail::CheckUser qw(check_email);  
 use Email::Valid;  
 use Email::Send;  
7    
8  =head1 NAME  =head1 NAME
9    
# Line 30  my $list_opt; Line 28  my $list_opt;
28  my $add_opt;  my $add_opt;
29  my $queue_opt;  my $queue_opt;
30  my $send_opt;  my $send_opt;
31    my $email_opt;
32    
33  my $result = GetOptions(  my $result = GetOptions(
34          "list:s" => \$list_opt,          "list:s" => \$list_opt,
# Line 38  my $result = GetOptions( Line 37  my $result = GetOptions(
37          "send:s" => \$send_opt,          "send:s" => \$send_opt,
38          "debug" => \$debug,          "debug" => \$debug,
39          "verbose" => \$verbose,          "verbose" => \$verbose,
40            "email=s" => \$email_opt,
41  );  );
42    
43    my $nos = new Nos(
44  my $loader = Class::DBI::Loader::Pg->new(          dsn => 'dbi:Pg:dbname=notices',
45          debug           => $debug,          user => 'dpavlin',
46          dsn             => "dbi:Pg:dbname=notices",          passwd => '',
47          user            => "dpavlin",          debug => $debug,
48          password        => "",          verbose => $verbose,
         namespace       => "Noticer",  
 #       additional_classes      => qw/Class::DBI::AbstractSearch/,  
 #       additional_base_classes => qw/My::Stuff/,  
         relationships   => 1,  
49  );  );
50    
51    my $loader = $nos->{'loader'} || die "can't find loader?";
52    
53  my $lists = $loader->find_class('lists');  my $lists = $loader->find_class('lists');
54  my $users = $loader->find_class('users');  my $users = $loader->find_class('users');
55  my $user_list = $loader->find_class('user_list');  my $user_list = $loader->find_class('user_list');
# Line 85  if (defined($list_opt)) { Line 83  if (defined($list_opt)) {
83          }          }
84    
85          foreach my $list (@lists) {          foreach my $list (@lists) {
86                  print $list->name,"\n";                  print $list->name," <",$list->email,">\n";
87                  foreach my $user_on_list ($user_list->search(list_id => $list->id)) {                  foreach my $user_on_list ($user_list->search(list_id => $list->id)) {
88                          my $user = $users->retrieve( id => $user_on_list->user_id );                          my $user = $users->retrieve( id => $user_on_list->user_id );
89                          print "\t",$user->full_name," <", $user->email, ">\n";                          print "\t",$user->full_name," <", $user->email, ">\n";
# Line 100  argument) or read from C<STDIN>. List sh Line 98  argument) or read from C<STDIN>. List sh
98   email@example.com      Optional full name of person   email@example.com      Optional full name of person
99   dpavlin@rot13.org      Dobrica Pavlinusic   dpavlin@rot13.org      Dobrica Pavlinusic
100    
101    You may use C<--email> parametar at any time to set From: e-mail address for list.
102    B<This seems somewhat cludgy, and it will probably change in future>.
103    
104  =cut  =cut
105    
106  } elsif ($add_opt) {  } elsif ($add_opt) {
         #my $noticer = $loader->find_class('Noticer') || die "can't find my class!";  
107          my $list = $lists->find_or_create({          my $list = $lists->find_or_create({
108                  name => $add_opt,                  name => $add_opt,
109          }) || die "can't add list $add_opt\n";          }) || die "can't add list $add_opt\n";
110    
111            if ($email_opt && $list->email ne $email_opt) {
112                    $list->email($email_opt);
113                    $list->update;
114                    $list->dbi_commit;
115            }
116    
117          my $added = 0;          my $added = 0;
118    
119          while(<>) {          while(<>) {
120                  chomp;                  chomp;
121                  next if (/^#/ || /^\s*$/);                  next if (/^#/ || /^\s*$/);
122                  my ($email, $name) = split(/\s+/,$_, 2);                  my ($email, $name) = split(/\s+/,$_, 2);
123                  $name ||= '';                  $added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $add_opt ));
                 if (! Email::Valid->address($email)) {  
                         print "SKIPPING $name <$email>\n";  
                         next;  
                 }  
                 print "# $name <$email>\n";  
                 my $this_user = $users->find_or_create({  
                         email => $email,  
                         full_name => $name,  
                 }) || die "can't find or create member\n";  
                 my $user_on_list = $user_list->find_or_create({  
                         user_id => $this_user->id,  
                         list_id => $list->id,  
                 }) || die "can't add user to list";  
                 $added++;  
         }  
   
         foreach my $c_name ($loader->tables) {  
                 my $c = $loader->find_class($c_name)|| die "can't find $c_name";  
                 $c->dbi_commit();  
124          }          }
125    
126          print "list ",$list->name," has $added users\n";          print "list ",$list->name," has $added users\n";
# Line 143  argument) or read from C<STDIN>. List sh Line 130  argument) or read from C<STDIN>. List sh
130  Queue message for later delivery. Message can be read from file (specified as  Queue message for later delivery. Message can be read from file (specified as
131  argument) or read from C<STDIN>.  argument) or read from C<STDIN>.
132    
133  This options without optional parametars it will display current queue.  This option without optional parametar will display pending queue. If you
134    add C<--verbose> flag, it will display all messages in queue.
135    
136  =cut  =cut
137    
# Line 152  This options without optional parametars Line 140  This options without optional parametars
140          if ($queue_opt ne '') {          if ($queue_opt ne '') {
141                  # add message to list queue                  # add message to list queue
142    
                 my $this_list = $lists->search(  
                         name => $queue_opt,  
                 )->first || die "can't find list $queue_opt";  
   
143                  my $message_text;                  my $message_text;
144                  while(<>) {                  while(<>) {
145                          $message_text .= $_;                          $message_text .= $_;
146                  }                  }
147    
148                  die "no message" unless ($message_text);                  my $id = $nos->add_message_to_queue(
149                            list => $queue_opt,
150                  my $this_message = $messages->find_or_create({                          message => $message_text,
151                          message => $message_text                  );
                 }) || die "can't insert message";  
   
                 $this_message->dbi_commit() || die "can't add message";  
   
                 $queue->find_or_create({  
                         message_id => $this_message->id,  
                         list_id => $this_list->id,  
                 }) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name;  
152    
153                  $queue->dbi_commit || die "can't add message to list ",$this_list->name;                  print "added message $id to list $queue_opt\n";
   
                 print "added message ",$this_message->id, " to list ",$this_list->name,"\n";  
154    
155          } else {          } else {
156                  # list messages in queue                          # list messages in queue        
# Line 201  This options without optional parametars Line 175  This options without optional parametars
175    
176  =item --send[=list_name]  =item --send[=list_name]
177    
178  Send e-mail waiting in queue for all lists, or with optional argument for  Send e-mails waiting in queue, or with optional argument, just send messages
179  just single list.  for single list.
180    
181  =cut  =cut
182    
183  } elsif (defined($send_opt)) {  } elsif (defined($send_opt)) {
184    
185          my $my_q;          $nos->send_queued_messages($send_opt);
         if ($send_opt ne '') {  
                 my $l_id = $lists->search_like( name => $send_opt )->first ||  
                         die "can't find list $send_opt";  
                 $my_q = $queue->search_like( list_id => $l_id ) ||  
                         die "can't find list $send_opt";  
         } else {  
                 $my_q = $queue->retrieve_all;  
         }  
   
         while (my $m = $my_q->next) {  
                 next if ($m->all_sent);  
   
                 print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n";  
                 my $msg = $m->message_id->message;  
   
                 foreach my $u ($user_list->search(list_id => $m->list_id)) {  
   
                         my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n";  
   
                         if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {  
                                 print "SKIP ",$u->user_id->email," message allready sent\n";  
                         } else {  
                                 print "\t",$u->user_id->email,"\n";  
                                 send IO => "$hdr\n$msg";  
                                 $sent->create({  
                                         message_id => $m->message_id,  
                                         user_id => $u->user_id,  
                                 });  
                                 $sent->dbi_commit;  
                         }  
                 }  
                 $m->all_sent(1);  
                 $m->update;  
                 $m->dbi_commit;  
         }  
186    
187  } else  {  } else  {
188          die "see perldoc $0 for help";          die "see perldoc $0 for help";
# Line 265  Turn on debugging output from C<Class::D Line 204  Turn on debugging output from C<Class::D
204    
205  Dump more info on screen.  Dump more info on screen.
206    
207    =item --email
208    
209    Used to specify e-mail address where needed.
210    
211  =back  =back
212    
213    

Legend:
Removed from v.15  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26