/[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 6 by dpavlin, Sat May 14 12:31:27 2005 UTC revision 14 by dpavlin, Sun May 15 14:20:08 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 Mail::CheckUser qw(check_email);
7    use Email::Valid;
8    use Email::Send;
9    
10  =head1 NAME  =head1 NAME
11    
12  sender.pl - command line notify sender utility  sender.pl - command line notify sender utility
13    
14    =head1 SYNOPSYS
15    
16     sender.pl --add=mylist members.txt
17     sender.pl --list[=mylist]
18     sender.pl --queue[=mylist message.txt]
19     sender.pl --send=mylist
20    
21    =head2 All options
22    
23    =over 20
24    
25    =item --debug
26    
27    Turn on debugging output from C<Class::DBI>
28    
29  =cut  =cut
30    
31  my ($list_opt,$debug) = (0,0);  my $debug = 0;
32    my $list_opt;
33  my $add_opt;  my $add_opt;
34  my $queue_opt;  my $queue_opt;
35    my $send_opt;
36    
37  my $result = GetOptions(  my $result = GetOptions(
38          "list"  => \$list_opt,          "list:s" => \$list_opt,
39          "add=s" => \$add_opt,          "add=s" => \$add_opt,
40          "queue=s" => \$queue_opt,          "queue:s" => \$queue_opt,
41            "send:s" => \$send_opt,
42          "debug" => \$debug,          "debug" => \$debug,
43  );  );
44    
# Line 38  my $lists = $loader->find_class('lists') Line 58  my $lists = $loader->find_class('lists')
58  my $users = $loader->find_class('users');  my $users = $loader->find_class('users');
59  my $user_list = $loader->find_class('user_list');  my $user_list = $loader->find_class('user_list');
60  my $messages = $loader->find_class('messages');  my $messages = $loader->find_class('messages');
61  my $message_list = $loader->find_class('message_list');  my $queue = $loader->find_class('queue');
62    
63    $queue->set_sql( list_queue => qq{
64            SELECT messages.message, messages.date AS message_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]
72    
73    List all available lists and users on them.
74    
75    Optional value is name of list. With it, this option will produce just users
76    on that list.
77    
78    =cut
79    
80  if ($list_opt) {  if (defined($list_opt)) {
81          foreach my $list ($lists->retrieve_all) {          my @lists;
82            if ($list_opt ne '') {
83                    @lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt";
84            } else {
85                    @lists = $lists->retrieve_all;
86            }
87    
88            foreach my $list (@lists) {
89                  print $list->name,"\n";                  print $list->name,"\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";
93                  }                  }
94          }          }
95    
96    =item --add=list_name
97    
98    Add users to list. Users are stored in file (which can be supplied as
99    argument) or read from C<STDIN>. List should be in following format:
100    
101     email@example.com      Optional full name of person
102     dpavlin@rot13.org      Dobrica Pavlinusic
103    
104    =cut
105    
106  } elsif ($add_opt) {  } elsif ($add_opt) {
107          #my $noticer = $loader->find_class('Noticer') || die "can't find my class!";          #my $noticer = $loader->find_class('Noticer') || die "can't find my class!";
108          my $list = $lists->find_or_create({          my $list = $lists->find_or_create({
# Line 60  if ($list_opt) { Line 115  if ($list_opt) {
115                  chomp;                  chomp;
116                  next if (/^#/ || /^\s*$/);                  next if (/^#/ || /^\s*$/);
117                  my ($email, $name) = split(/\s+/,$_, 2);                  my ($email, $name) = split(/\s+/,$_, 2);
118                    if (! Email::Valid->address($email)) {
119                            print "SKIPPING $name <$email>\n";
120                            next;
121                    }
122                  print "# $name <$email>\n";                  print "# $name <$email>\n";
123                  my $this_user = $users->find_or_create({                  my $this_user = $users->find_or_create({
124                          email => $email,                          email => $email,
# Line 79  if ($list_opt) { Line 138  if ($list_opt) {
138    
139          print "list ",$list->name," has $added users\n";          print "list ",$list->name," has $added users\n";
140    
141  } elsif ($queue_opt) {  =item --queue[=list_name]
142          my $this_list = $lists->search(  
143                  name => $queue_opt,  Queue message for later delivery. Message can be read from file (specified as
144          )->first || die "can't find list $queue_opt";  argument) or read from C<STDIN>.
145    
146    This options without optional parametars it will display current queue.
147    
148    =cut
149    
150    } elsif (defined($queue_opt)) {
151    
152            if ($queue_opt ne '') {
153                    # add message to list queue
154    
155                    my $this_list = $lists->search(
156                            name => $queue_opt,
157                    )->first || die "can't find list $queue_opt";
158    
159                    my $message_text;
160                    while(<>) {
161                            $message_text .= $_;
162                    }
163    
164                    die "no message" unless ($message_text);
165    
166                    my $this_message = $messages->find_or_create({
167                            message => $message_text
168                    }) || die "can't insert message";
169    
170                    $this_message->dbi_commit() || die "can't add message";
171    
172                    $queue->find_or_create({
173                            message_id => $this_message->id,
174                            list_id => $this_list->id,
175                    }) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name;
176    
177                    $queue->dbi_commit || die "can't add message to list ",$this_list->name;
178    
179                    print "added message ",$this_message->id, " to list ",$this_list->name,"\n";
180    
181            } else {
182                    # list messages in queue        
183    
184                    foreach my $m ($queue->retrieve_all) {
185                            my $l = sprintf("%-10s %15s : ", $m->list_id->name, $m->message_id->date);
186                            $l .= substr($m->message_id->message, 0, 79 - length($l));
187                            $l =~ s/[\n\r]/ /gs;
188                            print "$l\n";
189                    }
190    
         my $message_text;  
         while(<>) {  
                 $message_text .= $_;  
191          }          }
192    
193          die "no message" unless ($message_text);  =item --send[=list_name]
194    
195    Send e-mail waiting in queue for all lists, or with optional argument for
196    just single list.
197    
198          my $this_message = $messages->find_or_create({  =cut
199                  message => $message_text  
200          }) || die "can't insert message";  } elsif (defined($send_opt)) {
201    
         $this_message->dbi_commit();  
202    
203          $message_list->find_or_create({          die "send option not yet implemented";
                 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;  
204    
205          print "added message ",$this_message->id, " to list ",$this_list->name,"\n";          my @q;
206            if ($send_opt ne '') {
207    #               @q => $queue->search( name => 'foo' );
208            }
209            foreach my $q (@q) {
210                    
211            }
212    
213  } else  {  } else  {
214          die $0.'          die "see perldoc $0 for help";
         --list                          show all lists and users  
         --add=name_of_list < users.txt  add users (email@example.com full name)  
         --queue=name_of_list < message  queue message for sending to list  
         --debug  
 ';  
215  }  }
216    
217    =back
218    
219    =head1 AUTHOR
220    
221    Dobrica Pavlinusic <dpavlin@rot13.org>
222    
223    =cut
224    

Legend:
Removed from v.6  
changed lines
  Added in v.14

  ViewVC Help
Powered by ViewVC 1.1.26