/[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 11 by dpavlin, Sat May 14 18:20:50 2005 UTC revision 19 by dpavlin, Sun May 15 17:04:18 2005 UTC
# Line 3  Line 3 
3  use strict;  use strict;
4  use Class::DBI::Loader::Pg;  use Class::DBI::Loader::Pg;
5  use Getopt::Long;  use Getopt::Long;
6  use Data::Dumper;  use Email::Valid;
7    use Email::Send;
8    
9  =head1 NAME  =head1 NAME
10    
# Line 13  sender.pl - command line notify sender u Line 14  sender.pl - command line notify sender u
14    
15   sender.pl --add=mylist members.txt   sender.pl --add=mylist members.txt
16   sender.pl --list[=mylist]   sender.pl --list[=mylist]
17   sender.pl --queue=mylist message.txt   sender.pl --queue[=mylist message.txt]
18   sender.pl --send=mylist   sender.pl --send=mylist
19    
20  =head2 All options  =head2 Command options
21    
22  =over 20  =over 20
23    
 =item --debug  
   
 Turn on debugging output from C<Class::DBI>  
   
24  =cut  =cut
25    
26  my ($list_opt,$debug) = (0,0);  my $debug = 0;
27    my $verbose = 0;
28    my $list_opt;
29  my $add_opt;  my $add_opt;
30  my $queue_opt;  my $queue_opt;
31    my $send_opt;
32    my $email_opt;
33    
34  my $result = GetOptions(  my $result = GetOptions(
35          "list:s" => \$list_opt,          "list:s" => \$list_opt,
36          "add=s" => \$add_opt,          "add=s" => \$add_opt,
37          "queue=s" => \$queue_opt,          "queue:s" => \$queue_opt,
38            "send:s" => \$send_opt,
39          "debug" => \$debug,          "debug" => \$debug,
40            "verbose" => \$verbose,
41            "email=s" => \$email_opt,
42  );  );
43    
44    
# Line 54  my $users = $loader->find_class('users') Line 58  my $users = $loader->find_class('users')
58  my $user_list = $loader->find_class('user_list');  my $user_list = $loader->find_class('user_list');
59  my $messages = $loader->find_class('messages');  my $messages = $loader->find_class('messages');
60  my $queue = $loader->find_class('queue');  my $queue = $loader->find_class('queue');
61    my $sent = $loader->find_class('sent');
62    
63    $queue->set_sql( list_queue => qq{
64            SELECT messages.message, messages.date AS date, lists.name AS list
65            FROM queue
66            JOIN messages on message_id = messages.id
67            JOIN lists on list_id = lists.id
68    } );
69    
70    
71  =item --list[=list_name]  =item --list[=list_name]
72    
73  List all available lists and users on them. Optional value is name of list  List all available lists and users on them.
74  and it will produce users just on that list.  
75    Optional value is name of list. With it, this option will produce just users
76    on that list.
77    
78  =cut  =cut
79    
# Line 71  if (defined($list_opt)) { Line 86  if (defined($list_opt)) {
86          }          }
87    
88          foreach my $list (@lists) {          foreach my $list (@lists) {
89                  print $list->name,"\n";                  print $list->name," <",$list->email,">\n";
90                  foreach my $user_on_list ($user_list->search(list_id => $list->id)) {                  foreach my $user_on_list ($user_list->search(list_id => $list->id)) {
91                          my $user = $users->retrieve( id => $user_on_list->user_id );                          my $user = $users->retrieve( id => $user_on_list->user_id );
92                          print "\t",$user->full_name," <", $user->email, ">\n";                          print "\t",$user->full_name," <", $user->email, ">\n";
# Line 86  argument) or read from C<STDIN>. List sh Line 101  argument) or read from C<STDIN>. List sh
101   email@example.com      Optional full name of person   email@example.com      Optional full name of person
102   dpavlin@rot13.org      Dobrica Pavlinusic   dpavlin@rot13.org      Dobrica Pavlinusic
103    
104    You may use C<--email> parametar at any time to set From: e-mail address for list.
105    B<This seems somewhat cludgy, and it will probably change in future>.
106    
107  =cut  =cut
108    
109  } elsif ($add_opt) {  } elsif ($add_opt) {
# Line 93  argument) or read from C<STDIN>. List sh Line 111  argument) or read from C<STDIN>. List sh
111          my $list = $lists->find_or_create({          my $list = $lists->find_or_create({
112                  name => $add_opt,                  name => $add_opt,
113          }) || die "can't add list $add_opt\n";          }) || die "can't add list $add_opt\n";
114            if ($email_opt && $list->email ne $email_opt) {
115                    $list->email($email_opt);
116                    $list->update;
117                    $list->dbi_commit;
118            }
119    
120          my $added = 0;          my $added = 0;
121    
# Line 100  argument) or read from C<STDIN>. List sh Line 123  argument) or read from C<STDIN>. List sh
123                  chomp;                  chomp;
124                  next if (/^#/ || /^\s*$/);                  next if (/^#/ || /^\s*$/);
125                  my ($email, $name) = split(/\s+/,$_, 2);                  my ($email, $name) = split(/\s+/,$_, 2);
126                    $name ||= '';
127                    if (! Email::Valid->address($email)) {
128                            print "SKIPPING $name <$email>\n";
129                            next;
130                    }
131                  print "# $name <$email>\n";                  print "# $name <$email>\n";
132                  my $this_user = $users->find_or_create({                  my $this_user = $users->find_or_create({
133                          email => $email,                          email => $email,
# Line 119  argument) or read from C<STDIN>. List sh Line 147  argument) or read from C<STDIN>. List sh
147    
148          print "list ",$list->name," has $added users\n";          print "list ",$list->name," has $added users\n";
149    
150  =item --queue=list_name  =item --queue[=list_name]
151    
152  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
153  argument) or read from C<STDIN>.  argument) or read from C<STDIN>.
154    
155    This option without optional parametar will display pending queue. If you
156    add C<--verbose> flag, it will display all messages in queue.
157    
158  =cut  =cut
159    
160  } elsif ($queue_opt) {  } elsif (defined($queue_opt)) {
161          my $this_list = $lists->search(  
162                  name => $queue_opt,          if ($queue_opt ne '') {
163          )->first || die "can't find list $queue_opt";                  # add message to list queue
164    
165                    my $this_list = $lists->search(
166                            name => $queue_opt,
167                    )->first || die "can't find list $queue_opt";
168    
169                    my $message_text;
170                    while(<>) {
171                            $message_text .= $_;
172                    }
173    
174                    die "no message" unless ($message_text);
175    
176                    my $this_message = $messages->find_or_create({
177                            message => $message_text
178                    }) || die "can't insert message";
179    
180                    $this_message->dbi_commit() || die "can't add message";
181    
182                    $queue->find_or_create({
183                            message_id => $this_message->id,
184                            list_id => $this_list->id,
185                    }) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name;
186    
187                    $queue->dbi_commit || die "can't add message to list ",$this_list->name;
188    
189                    print "added message ",$this_message->id, " to list ",$this_list->name,"\n";
190    
191            } else {
192                    # list messages in queue        
193    
194                    foreach my $m ($queue->retrieve_all) {
195                            next if ($m->all_sent && ! $verbose);
196    
197                            my $l = $m->all_sent ? 'S' : 'Q';
198    
199                            my $date = $m->message_id->date;
200                            $date =~ s/\..+$//;
201                            my $msg = $m->message_id->message;
202                            $msg =~ s/\s+/ /gs;
203    
204                            $l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date);
205                            $l .= substr($msg, 0, 79 - length($l));
206    
207                            print "$l\n";
208                    }
209    
         my $message_text;  
         while(<>) {  
                 $message_text .= $_;  
210          }          }
211    
212          die "no message" unless ($message_text);  =item --send[=list_name]
213    
214    Send e-mails waiting in queue, or with optional argument, just send messages
215    for single list.
216    
217    =cut
218    
219    } elsif (defined($send_opt)) {
220    
221            my $my_q;
222            if ($send_opt ne '') {
223                    my $l_id = $lists->search_like( name => $send_opt )->first ||
224                            die "can't find list $send_opt";
225                    $my_q = $queue->search_like( list_id => $l_id ) ||
226                            die "can't find list $send_opt";
227            } else {
228                    $my_q = $queue->retrieve_all;
229            }
230    
231          my $this_message = $messages->find_or_create({          while (my $m = $my_q->next) {
232                  message => $message_text                  next if ($m->all_sent);
         }) || die "can't insert message";  
233    
234          $this_message->dbi_commit();                  print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n";
235                    my $msg = $m->message_id->message;
236    
237          $queue->find_or_create({                  foreach my $u ($user_list->search(list_id => $m->list_id)) {
                 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;  
238    
239          print "added message ",$this_message->id, " to list ",$this_list->name,"\n";                          if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {
240                                    print "SKIP ",$u->user_id->email," message allready sent\n";
241                            } else {
242                                    print "\t",$u->user_id->email,"\n";
243    
244                                    my $hdr = "From: " . $u->list_id->name . " <" . $u->list_id->email . ">\n" .
245                                            "To: " . $u->user_id->full_name . " <". $u->user_id->email. ">\n";
246    
247                                    # FIXME do real sending :-)
248                                    send IO => "$hdr\n$msg";
249    
250                                    $sent->create({
251                                            message_id => $m->message_id,
252                                            user_id => $u->user_id,
253                                    });
254                                    $sent->dbi_commit;
255                            }
256                    }
257                    $m->all_sent(1);
258                    $m->update;
259                    $m->dbi_commit;
260            }
261    
262  } else  {  } else  {
263          die "see perldoc $0 for help";          die "see perldoc $0 for help";
# Line 157  argument) or read from C<STDIN>. Line 265  argument) or read from C<STDIN>.
265    
266  =back  =back
267    
268    
269    
270    =head2 Helper options
271    
272    =over 20
273    
274    =item --debug
275    
276    Turn on debugging output from C<Class::DBI>
277    
278    =item --verbose
279    
280    Dump more info on screen.
281    
282    =item --email
283    
284    Used to specify e-mail address where needed.
285    
286    =back
287    
288    
289    
290  =head1 AUTHOR  =head1 AUTHOR
291    
292  Dobrica Pavlinusic <dpavlin@rot13.org>  Dobrica Pavlinusic <dpavlin@rot13.org>

Legend:
Removed from v.11  
changed lines
  Added in v.19

  ViewVC Help
Powered by ViewVC 1.1.26