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 Data::Dumper; |
|
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
9 |
|
|
13 |
|
|
14 |
sender.pl --add=mylist members.txt |
sender.pl --add=mylist members.txt |
15 |
sender.pl --list[=mylist] |
sender.pl --list[=mylist] |
16 |
sender.pl --queue=mylist message.txt |
sender.pl --queue[=mylist message.txt] |
17 |
sender.pl --send=mylist |
sender.pl --send=mylist |
18 |
|
|
19 |
=head2 All options |
=head2 Command options |
20 |
|
|
21 |
=over 20 |
=over 20 |
22 |
|
|
|
=item --debug |
|
|
|
|
|
Turn on debugging output from C<Class::DBI> |
|
|
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
my ($list_opt,$debug) = (0,0); |
my $debug = 0; |
26 |
|
my $verbose = 0; |
27 |
|
my $list_opt; |
28 |
my $add_opt; |
my $add_opt; |
29 |
my $queue_opt; |
my $queue_opt; |
30 |
|
my $send_opt; |
31 |
|
my $email_opt; |
32 |
|
|
33 |
my $result = GetOptions( |
my $result = GetOptions( |
34 |
"list:s" => \$list_opt, |
"list:s" => \$list_opt, |
35 |
"add=s" => \$add_opt, |
"add=s" => \$add_opt, |
36 |
"queue=s" => \$queue_opt, |
"queue:s" => \$queue_opt, |
37 |
|
"send:s" => \$send_opt, |
38 |
"debug" => \$debug, |
"debug" => \$debug, |
39 |
|
"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'); |
56 |
my $messages = $loader->find_class('messages'); |
my $messages = $loader->find_class('messages'); |
57 |
my $message_list = $loader->find_class('message_list'); |
my $queue = $loader->find_class('queue'); |
58 |
|
my $sent = $loader->find_class('sent'); |
59 |
|
|
60 |
|
$queue->set_sql( list_queue => qq{ |
61 |
|
SELECT messages.message, messages.date AS date, lists.name AS list |
62 |
|
FROM queue |
63 |
|
JOIN messages on message_id = messages.id |
64 |
|
JOIN lists on list_id = lists.id |
65 |
|
} ); |
66 |
|
|
67 |
|
|
68 |
=item --list[=list_name] |
=item --list[=list_name] |
69 |
|
|
70 |
List all available lists and users on them. Optional value to list |
List all available lists and users on them. |
71 |
(like C<--list=list_name>) will produce users just on that list. |
|
72 |
|
Optional value is name of list. With it, this option will produce just users |
73 |
|
on that list. |
74 |
|
|
75 |
=cut |
=cut |
76 |
|
|
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"; |
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) { |
108 |
my $list = $lists->find_or_create({ |
my $list = $lists->find_or_create({ |
109 |
name => $add_opt, |
name => $add_opt, |
110 |
}) || die "can't add list $add_opt\n"; |
}) || die "can't add list $add_opt\n"; |
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 |
|
|
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 ||= ''; |
124 |
|
if (! Email::Valid->address($email)) { |
125 |
|
print "SKIPPING $name <$email>\n"; |
126 |
|
next; |
127 |
|
} |
128 |
print "# $name <$email>\n"; |
print "# $name <$email>\n"; |
129 |
my $this_user = $users->find_or_create({ |
my $this_user = $users->find_or_create({ |
130 |
email => $email, |
email => $email, |
144 |
|
|
145 |
print "list ",$list->name," has $added users\n"; |
print "list ",$list->name," has $added users\n"; |
146 |
|
|
147 |
=item --queue=list_name |
=item --queue[=list_name] |
148 |
|
|
149 |
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 |
150 |
argument) or read from C<STDIN>. |
argument) or read from C<STDIN>. |
151 |
|
|
152 |
|
This option without optional parametar will display pending queue. If you |
153 |
|
add C<--verbose> flag, it will display all messages in queue. |
154 |
|
|
155 |
=cut |
=cut |
156 |
|
|
157 |
} elsif ($queue_opt) { |
} elsif (defined($queue_opt)) { |
158 |
my $this_list = $lists->search( |
|
159 |
name => $queue_opt, |
if ($queue_opt ne '') { |
160 |
)->first || die "can't find list $queue_opt"; |
# add message to list queue |
161 |
|
|
162 |
|
my $this_list = $lists->search( |
163 |
|
name => $queue_opt, |
164 |
|
)->first || die "can't find list $queue_opt"; |
165 |
|
|
166 |
|
my $message_text; |
167 |
|
while(<>) { |
168 |
|
$message_text .= $_; |
169 |
|
} |
170 |
|
|
171 |
|
die "no message" unless ($message_text); |
172 |
|
|
173 |
|
my $this_message = $messages->find_or_create({ |
174 |
|
message => $message_text |
175 |
|
}) || die "can't insert message"; |
176 |
|
|
177 |
|
$this_message->dbi_commit() || die "can't add message"; |
178 |
|
|
179 |
|
$queue->find_or_create({ |
180 |
|
message_id => $this_message->id, |
181 |
|
list_id => $this_list->id, |
182 |
|
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
183 |
|
|
184 |
|
$queue->dbi_commit || die "can't add message to list ",$this_list->name; |
185 |
|
|
186 |
|
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
187 |
|
|
188 |
|
} else { |
189 |
|
# list messages in queue |
190 |
|
|
191 |
|
foreach my $m ($queue->retrieve_all) { |
192 |
|
next if ($m->all_sent && ! $verbose); |
193 |
|
|
194 |
|
my $l = $m->all_sent ? 'S' : 'Q'; |
195 |
|
|
196 |
|
my $date = $m->message_id->date; |
197 |
|
$date =~ s/\..+$//; |
198 |
|
my $msg = $m->message_id->message; |
199 |
|
$msg =~ s/\s+/ /gs; |
200 |
|
|
201 |
|
$l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); |
202 |
|
$l .= substr($msg, 0, 79 - length($l)); |
203 |
|
|
204 |
|
print "$l\n"; |
205 |
|
} |
206 |
|
|
|
my $message_text; |
|
|
while(<>) { |
|
|
$message_text .= $_; |
|
207 |
} |
} |
208 |
|
|
209 |
die "no message" unless ($message_text); |
=item --send[=list_name] |
210 |
|
|
211 |
|
Send e-mails waiting in queue, or with optional argument, just send messages |
212 |
|
for single list. |
213 |
|
|
214 |
|
=cut |
215 |
|
|
216 |
|
} elsif (defined($send_opt)) { |
217 |
|
|
218 |
|
my $my_q; |
219 |
|
if ($send_opt ne '') { |
220 |
|
my $l_id = $lists->search_like( name => $send_opt )->first || |
221 |
|
die "can't find list $send_opt"; |
222 |
|
$my_q = $queue->search_like( list_id => $l_id ) || |
223 |
|
die "can't find list $send_opt"; |
224 |
|
} else { |
225 |
|
$my_q = $queue->retrieve_all; |
226 |
|
} |
227 |
|
|
228 |
my $this_message = $messages->find_or_create({ |
while (my $m = $my_q->next) { |
229 |
message => $message_text |
next if ($m->all_sent); |
|
}) || die "can't insert message"; |
|
230 |
|
|
231 |
$this_message->dbi_commit(); |
print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; |
232 |
|
my $msg = $m->message_id->message; |
233 |
|
|
234 |
$message_list->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; |
|
235 |
|
|
236 |
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 )) { |
237 |
|
print "SKIP ",$u->user_id->email," message allready sent\n"; |
238 |
|
} else { |
239 |
|
print "\t",$u->user_id->email,"\n"; |
240 |
|
|
241 |
|
my $hdr = "From: " . $u->list_id->name . " <" . $u->list_id->email . ">\n" . |
242 |
|
"To: " . $u->user_id->full_name . " <". $u->user_id->email. ">\n"; |
243 |
|
|
244 |
|
# FIXME do real sending :-) |
245 |
|
$nos->send_email("$hdr\n$msg"); |
246 |
|
|
247 |
|
$sent->create({ |
248 |
|
message_id => $m->message_id, |
249 |
|
user_id => $u->user_id, |
250 |
|
}); |
251 |
|
$sent->dbi_commit; |
252 |
|
} |
253 |
|
} |
254 |
|
$m->all_sent(1); |
255 |
|
$m->update; |
256 |
|
$m->dbi_commit; |
257 |
|
} |
258 |
|
|
259 |
} else { |
} else { |
260 |
die "see perldoc $0 for help"; |
die "see perldoc $0 for help"; |
262 |
|
|
263 |
=back |
=back |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
=head2 Helper options |
268 |
|
|
269 |
|
=over 20 |
270 |
|
|
271 |
|
=item --debug |
272 |
|
|
273 |
|
Turn on debugging output from C<Class::DBI> |
274 |
|
|
275 |
|
=item --verbose |
276 |
|
|
277 |
|
Dump more info on screen. |
278 |
|
|
279 |
|
=item --email |
280 |
|
|
281 |
|
Used to specify e-mail address where needed. |
282 |
|
|
283 |
|
=back |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
=head1 AUTHOR |
=head1 AUTHOR |
288 |
|
|
289 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |