/[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

Annotation of /trunk/sender.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (hide annotations)
Sun May 15 17:01:19 2005 UTC (18 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 6561 byte(s)
lists now have From: e-mail address

1 dpavlin 1 #!/usr/bin/perl -w
2    
3     use strict;
4     use Class::DBI::Loader::Pg;
5     use Getopt::Long;
6 dpavlin 13 use Email::Valid;
7 dpavlin 14 use Email::Send;
8 dpavlin 1
9 dpavlin 6 =head1 NAME
10    
11     sender.pl - command line notify sender utility
12    
13 dpavlin 8 =head1 SYNOPSYS
14    
15     sender.pl --add=mylist members.txt
16 dpavlin 9 sender.pl --list[=mylist]
17 dpavlin 14 sender.pl --queue[=mylist message.txt]
18 dpavlin 8 sender.pl --send=mylist
19    
20 dpavlin 15 =head2 Command options
21 dpavlin 8
22     =over 20
23    
24 dpavlin 6 =cut
25    
26 dpavlin 12 my $debug = 0;
27 dpavlin 15 my $verbose = 0;
28 dpavlin 12 my $list_opt;
29 dpavlin 2 my $add_opt;
30 dpavlin 6 my $queue_opt;
31 dpavlin 14 my $send_opt;
32 dpavlin 18 my $email_opt;
33 dpavlin 1
34     my $result = GetOptions(
35 dpavlin 9 "list:s" => \$list_opt,
36 dpavlin 2 "add=s" => \$add_opt,
37 dpavlin 14 "queue:s" => \$queue_opt,
38     "send:s" => \$send_opt,
39 dpavlin 1 "debug" => \$debug,
40 dpavlin 15 "verbose" => \$verbose,
41 dpavlin 18 "email=s" => \$email_opt,
42 dpavlin 1 );
43    
44    
45     my $loader = Class::DBI::Loader::Pg->new(
46     debug => $debug,
47     dsn => "dbi:Pg:dbname=notices",
48     user => "dpavlin",
49     password => "",
50     namespace => "Noticer",
51     # additional_classes => qw/Class::DBI::AbstractSearch/,
52     # additional_base_classes => qw/My::Stuff/,
53 dpavlin 3 relationships => 1,
54 dpavlin 1 );
55    
56 dpavlin 2 my $lists = $loader->find_class('lists');
57     my $users = $loader->find_class('users');
58     my $user_list = $loader->find_class('user_list');
59 dpavlin 6 my $messages = $loader->find_class('messages');
60 dpavlin 11 my $queue = $loader->find_class('queue');
61 dpavlin 15 my $sent = $loader->find_class('sent');
62 dpavlin 2
63 dpavlin 14 $queue->set_sql( list_queue => qq{
64 dpavlin 15 SELECT messages.message, messages.date AS date, lists.name AS list
65 dpavlin 14 FROM queue
66     JOIN messages on message_id = messages.id
67     JOIN lists on list_id = lists.id
68     } );
69    
70    
71 dpavlin 9 =item --list[=list_name]
72 dpavlin 8
73 dpavlin 14 List all available lists and users on them.
74 dpavlin 8
75 dpavlin 14 Optional value is name of list. With it, this option will produce just users
76     on that list.
77    
78 dpavlin 8 =cut
79    
80 dpavlin 9 if (defined($list_opt)) {
81     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 dpavlin 17 print $list->name," <",$list->email,">\n";
90 dpavlin 1 foreach my $user_on_list ($user_list->search(list_id => $list->id)) {
91     my $user = $users->retrieve( id => $user_on_list->user_id );
92     print "\t",$user->full_name," <", $user->email, ">\n";
93     }
94     }
95 dpavlin 8
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 dpavlin 18 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 dpavlin 8 =cut
108    
109 dpavlin 2 } elsif ($add_opt) {
110     #my $noticer = $loader->find_class('Noticer') || die "can't find my class!";
111     my $list = $lists->find_or_create({
112     name => $add_opt,
113     }) || die "can't add list $add_opt\n";
114 dpavlin 18 if ($email_opt && $list->email ne $email_opt) {
115     $list->email($email_opt);
116     $list->update;
117     $list->dbi_commit;
118     }
119 dpavlin 6
120     my $added = 0;
121    
122 dpavlin 2 while(<>) {
123     chomp;
124     next if (/^#/ || /^\s*$/);
125     my ($email, $name) = split(/\s+/,$_, 2);
126 dpavlin 15 $name ||= '';
127 dpavlin 13 if (! Email::Valid->address($email)) {
128     print "SKIPPING $name <$email>\n";
129     next;
130     }
131 dpavlin 2 print "# $name <$email>\n";
132     my $this_user = $users->find_or_create({
133     email => $email,
134     full_name => $name,
135     }) || die "can't find or create member\n";
136     my $user_on_list = $user_list->find_or_create({
137     user_id => $this_user->id,
138     list_id => $list->id,
139     }) || die "can't add user to list";
140 dpavlin 6 $added++;
141 dpavlin 2 }
142 dpavlin 3
143     foreach my $c_name ($loader->tables) {
144     my $c = $loader->find_class($c_name)|| die "can't find $c_name";
145     $c->dbi_commit();
146     }
147    
148 dpavlin 6 print "list ",$list->name," has $added users\n";
149    
150 dpavlin 14 =item --queue[=list_name]
151 dpavlin 8
152     Queue message for later delivery. Message can be read from file (specified as
153     argument) or read from C<STDIN>.
154    
155 dpavlin 16 This option without optional parametar will display pending queue. If you
156     add C<--verbose> flag, it will display all messages in queue.
157 dpavlin 14
158 dpavlin 8 =cut
159    
160 dpavlin 14 } elsif (defined($queue_opt)) {
161 dpavlin 6
162 dpavlin 14 if ($queue_opt ne '') {
163     # 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 dpavlin 15 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 dpavlin 14 print "$l\n";
208     }
209    
210 dpavlin 6 }
211    
212 dpavlin 14 =item --send[=list_name]
213 dpavlin 6
214 dpavlin 16 Send e-mails waiting in queue, or with optional argument, just send messages
215     for single list.
216 dpavlin 6
217 dpavlin 14 =cut
218 dpavlin 6
219 dpavlin 14 } elsif (defined($send_opt)) {
220 dpavlin 6
221 dpavlin 15 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 dpavlin 6
231 dpavlin 15 while (my $m = $my_q->next) {
232     next if ($m->all_sent);
233 dpavlin 14
234 dpavlin 15 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     foreach my $u ($user_list->search(list_id => $m->list_id)) {
238    
239     my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n";
240    
241     if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) {
242     print "SKIP ",$u->user_id->email," message allready sent\n";
243     } else {
244     print "\t",$u->user_id->email,"\n";
245 dpavlin 18
246     # FIXME do real sending :-)
247 dpavlin 15 send IO => "$hdr\n$msg";
248 dpavlin 18
249 dpavlin 15 $sent->create({
250     message_id => $m->message_id,
251     user_id => $u->user_id,
252     });
253     $sent->dbi_commit;
254     }
255     }
256     $m->all_sent(1);
257     $m->update;
258     $m->dbi_commit;
259 dpavlin 14 }
260    
261 dpavlin 1 } else {
262 dpavlin 8 die "see perldoc $0 for help";
263 dpavlin 1 }
264    
265 dpavlin 8 =back
266    
267 dpavlin 15
268    
269     =head2 Helper options
270    
271     =over 20
272    
273     =item --debug
274    
275     Turn on debugging output from C<Class::DBI>
276    
277     =item --verbose
278    
279     Dump more info on screen.
280    
281 dpavlin 18 =item --email
282    
283     Used to specify e-mail address where needed.
284    
285 dpavlin 15 =back
286    
287    
288    
289 dpavlin 8 =head1 AUTHOR
290    
291     Dobrica Pavlinusic <dpavlin@rot13.org>
292    
293     =cut
294    

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26