1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use Class::DBI::Loader::Pg; |
5 |
use Getopt::Long; |
6 |
use Mail::CheckUser qw(check_email); |
7 |
use Email::Valid; |
8 |
use Email::Send; |
9 |
|
10 |
=head1 NAME |
11 |
|
12 |
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 Command options |
22 |
|
23 |
=over 20 |
24 |
|
25 |
=cut |
26 |
|
27 |
my $debug = 0; |
28 |
my $verbose = 0; |
29 |
my $list_opt; |
30 |
my $add_opt; |
31 |
my $queue_opt; |
32 |
my $send_opt; |
33 |
|
34 |
my $result = GetOptions( |
35 |
"list:s" => \$list_opt, |
36 |
"add=s" => \$add_opt, |
37 |
"queue:s" => \$queue_opt, |
38 |
"send:s" => \$send_opt, |
39 |
"debug" => \$debug, |
40 |
"verbose" => \$verbose, |
41 |
); |
42 |
|
43 |
|
44 |
my $loader = Class::DBI::Loader::Pg->new( |
45 |
debug => $debug, |
46 |
dsn => "dbi:Pg:dbname=notices", |
47 |
user => "dpavlin", |
48 |
password => "", |
49 |
namespace => "Noticer", |
50 |
# additional_classes => qw/Class::DBI::AbstractSearch/, |
51 |
# additional_base_classes => qw/My::Stuff/, |
52 |
relationships => 1, |
53 |
); |
54 |
|
55 |
my $lists = $loader->find_class('lists'); |
56 |
my $users = $loader->find_class('users'); |
57 |
my $user_list = $loader->find_class('user_list'); |
58 |
my $messages = $loader->find_class('messages'); |
59 |
my $queue = $loader->find_class('queue'); |
60 |
my $sent = $loader->find_class('sent'); |
61 |
|
62 |
$queue->set_sql( list_queue => qq{ |
63 |
SELECT messages.message, messages.date AS date, lists.name AS list |
64 |
FROM queue |
65 |
JOIN messages on message_id = messages.id |
66 |
JOIN lists on list_id = lists.id |
67 |
} ); |
68 |
|
69 |
|
70 |
=item --list[=list_name] |
71 |
|
72 |
List all available lists and users on them. |
73 |
|
74 |
Optional value is name of list. With it, this option will produce just users |
75 |
on that list. |
76 |
|
77 |
=cut |
78 |
|
79 |
if (defined($list_opt)) { |
80 |
my @lists; |
81 |
if ($list_opt ne '') { |
82 |
@lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt"; |
83 |
} else { |
84 |
@lists = $lists->retrieve_all; |
85 |
} |
86 |
|
87 |
foreach my $list (@lists) { |
88 |
print $list->name,"\n"; |
89 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
90 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
91 |
print "\t",$user->full_name," <", $user->email, ">\n"; |
92 |
} |
93 |
} |
94 |
|
95 |
=item --add=list_name |
96 |
|
97 |
Add users to list. Users are stored in file (which can be supplied as |
98 |
argument) or read from C<STDIN>. List should be in following format: |
99 |
|
100 |
email@example.com Optional full name of person |
101 |
dpavlin@rot13.org Dobrica Pavlinusic |
102 |
|
103 |
=cut |
104 |
|
105 |
} elsif ($add_opt) { |
106 |
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
107 |
my $list = $lists->find_or_create({ |
108 |
name => $add_opt, |
109 |
}) || die "can't add list $add_opt\n"; |
110 |
|
111 |
my $added = 0; |
112 |
|
113 |
while(<>) { |
114 |
chomp; |
115 |
next if (/^#/ || /^\s*$/); |
116 |
my ($email, $name) = split(/\s+/,$_, 2); |
117 |
$name ||= ''; |
118 |
if (! Email::Valid->address($email)) { |
119 |
print "SKIPPING $name <$email>\n"; |
120 |
next; |
121 |
} |
122 |
print "# $name <$email>\n"; |
123 |
my $this_user = $users->find_or_create({ |
124 |
email => $email, |
125 |
full_name => $name, |
126 |
}) || die "can't find or create member\n"; |
127 |
my $user_on_list = $user_list->find_or_create({ |
128 |
user_id => $this_user->id, |
129 |
list_id => $list->id, |
130 |
}) || die "can't add user to list"; |
131 |
$added++; |
132 |
} |
133 |
|
134 |
foreach my $c_name ($loader->tables) { |
135 |
my $c = $loader->find_class($c_name)|| die "can't find $c_name"; |
136 |
$c->dbi_commit(); |
137 |
} |
138 |
|
139 |
print "list ",$list->name," has $added users\n"; |
140 |
|
141 |
=item --queue[=list_name] |
142 |
|
143 |
Queue message for later delivery. Message can be read from file (specified as |
144 |
argument) or read from C<STDIN>. |
145 |
|
146 |
This option without optional parametar will display pending queue. If you |
147 |
add C<--verbose> flag, it will display all messages in queue. |
148 |
|
149 |
=cut |
150 |
|
151 |
} elsif (defined($queue_opt)) { |
152 |
|
153 |
if ($queue_opt ne '') { |
154 |
# add message to list queue |
155 |
|
156 |
my $this_list = $lists->search( |
157 |
name => $queue_opt, |
158 |
)->first || die "can't find list $queue_opt"; |
159 |
|
160 |
my $message_text; |
161 |
while(<>) { |
162 |
$message_text .= $_; |
163 |
} |
164 |
|
165 |
die "no message" unless ($message_text); |
166 |
|
167 |
my $this_message = $messages->find_or_create({ |
168 |
message => $message_text |
169 |
}) || die "can't insert message"; |
170 |
|
171 |
$this_message->dbi_commit() || die "can't add message"; |
172 |
|
173 |
$queue->find_or_create({ |
174 |
message_id => $this_message->id, |
175 |
list_id => $this_list->id, |
176 |
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
177 |
|
178 |
$queue->dbi_commit || die "can't add message to list ",$this_list->name; |
179 |
|
180 |
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
181 |
|
182 |
} else { |
183 |
# list messages in queue |
184 |
|
185 |
foreach my $m ($queue->retrieve_all) { |
186 |
next if ($m->all_sent && ! $verbose); |
187 |
|
188 |
my $l = $m->all_sent ? 'S' : 'Q'; |
189 |
|
190 |
my $date = $m->message_id->date; |
191 |
$date =~ s/\..+$//; |
192 |
my $msg = $m->message_id->message; |
193 |
$msg =~ s/\s+/ /gs; |
194 |
|
195 |
$l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); |
196 |
$l .= substr($msg, 0, 79 - length($l)); |
197 |
|
198 |
print "$l\n"; |
199 |
} |
200 |
|
201 |
} |
202 |
|
203 |
=item --send[=list_name] |
204 |
|
205 |
Send e-mails waiting in queue, or with optional argument, just send messages |
206 |
for single list. |
207 |
|
208 |
=cut |
209 |
|
210 |
} elsif (defined($send_opt)) { |
211 |
|
212 |
my $my_q; |
213 |
if ($send_opt ne '') { |
214 |
my $l_id = $lists->search_like( name => $send_opt )->first || |
215 |
die "can't find list $send_opt"; |
216 |
$my_q = $queue->search_like( list_id => $l_id ) || |
217 |
die "can't find list $send_opt"; |
218 |
} else { |
219 |
$my_q = $queue->retrieve_all; |
220 |
} |
221 |
|
222 |
while (my $m = $my_q->next) { |
223 |
next if ($m->all_sent); |
224 |
|
225 |
print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; |
226 |
my $msg = $m->message_id->message; |
227 |
|
228 |
foreach my $u ($user_list->search(list_id => $m->list_id)) { |
229 |
|
230 |
my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n"; |
231 |
|
232 |
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
233 |
print "SKIP ",$u->user_id->email," message allready sent\n"; |
234 |
} else { |
235 |
print "\t",$u->user_id->email,"\n"; |
236 |
send IO => "$hdr\n$msg"; |
237 |
$sent->create({ |
238 |
message_id => $m->message_id, |
239 |
user_id => $u->user_id, |
240 |
}); |
241 |
$sent->dbi_commit; |
242 |
} |
243 |
} |
244 |
$m->all_sent(1); |
245 |
$m->update; |
246 |
$m->dbi_commit; |
247 |
} |
248 |
|
249 |
} else { |
250 |
die "see perldoc $0 for help"; |
251 |
} |
252 |
|
253 |
=back |
254 |
|
255 |
|
256 |
|
257 |
=head2 Helper options |
258 |
|
259 |
=over 20 |
260 |
|
261 |
=item --debug |
262 |
|
263 |
Turn on debugging output from C<Class::DBI> |
264 |
|
265 |
=item --verbose |
266 |
|
267 |
Dump more info on screen. |
268 |
|
269 |
=back |
270 |
|
271 |
|
272 |
|
273 |
=head1 AUTHOR |
274 |
|
275 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
276 |
|
277 |
=cut |
278 |
|