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 Command options |
22 |
|
|
23 |
|
=over 20 |
24 |
|
|
25 |
=cut |
=cut |
26 |
|
|
27 |
my ($list_opt,$debug) = (0,0); |
my $debug = 0; |
28 |
|
my $verbose = 0; |
29 |
|
my $list_opt; |
30 |
my $add_opt; |
my $add_opt; |
31 |
my $queue_opt; |
my $queue_opt; |
32 |
|
my $send_opt; |
33 |
|
|
34 |
my $result = GetOptions( |
my $result = GetOptions( |
35 |
"list" => \$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 |
); |
); |
42 |
|
|
43 |
|
|
56 |
my $users = $loader->find_class('users'); |
my $users = $loader->find_class('users'); |
57 |
my $user_list = $loader->find_class('user_list'); |
my $user_list = $loader->find_class('user_list'); |
58 |
my $messages = $loader->find_class('messages'); |
my $messages = $loader->find_class('messages'); |
59 |
my $message_list = $loader->find_class('message_list'); |
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 |
if ($list_opt) { |
=cut |
78 |
foreach my $list ($lists->retrieve_all) { |
|
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"; |
print $list->name,"\n"; |
89 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
90 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
91 |
print "\t",$user->full_name," <", $user->email, ">\n"; |
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) { |
} elsif ($add_opt) { |
106 |
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
107 |
my $list = $lists->find_or_create({ |
my $list = $lists->find_or_create({ |
114 |
chomp; |
chomp; |
115 |
next if (/^#/ || /^\s*$/); |
next if (/^#/ || /^\s*$/); |
116 |
my ($email, $name) = split(/\s+/,$_, 2); |
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"; |
print "# $name <$email>\n"; |
123 |
my $this_user = $users->find_or_create({ |
my $this_user = $users->find_or_create({ |
124 |
email => $email, |
email => $email, |
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 |
|
next if ($m->all_sent && ! $verbose); |
186 |
|
|
187 |
|
my $l = $m->all_sent ? 'S' : 'Q'; |
188 |
|
|
189 |
|
my $date = $m->message_id->date; |
190 |
|
$date =~ s/\..+$//; |
191 |
|
my $msg = $m->message_id->message; |
192 |
|
$msg =~ s/\s+/ /gs; |
193 |
|
|
194 |
|
$l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); |
195 |
|
$l .= substr($msg, 0, 79 - length($l)); |
196 |
|
|
197 |
|
print "$l\n"; |
198 |
|
} |
199 |
|
|
|
my $message_text; |
|
|
while(<>) { |
|
|
$message_text .= $_; |
|
200 |
} |
} |
201 |
|
|
202 |
die "no message" unless ($message_text); |
=item --send[=list_name] |
203 |
|
|
204 |
my $this_message = $messages->find_or_create({ |
Send e-mail waiting in queue for all lists, or with optional argument for |
205 |
message => $message_text |
just single list. |
|
}) || die "can't insert message"; |
|
206 |
|
|
207 |
$this_message->dbi_commit(); |
=cut |
208 |
|
|
209 |
|
} elsif (defined($send_opt)) { |
210 |
|
|
211 |
|
my $my_q; |
212 |
|
if ($send_opt ne '') { |
213 |
|
my $l_id = $lists->search_like( name => $send_opt )->first || |
214 |
|
die "can't find list $send_opt"; |
215 |
|
$my_q = $queue->search_like( list_id => $l_id ) || |
216 |
|
die "can't find list $send_opt"; |
217 |
|
} else { |
218 |
|
$my_q = $queue->retrieve_all; |
219 |
|
} |
220 |
|
|
221 |
$message_list->find_or_create({ |
while (my $m = $my_q->next) { |
222 |
message_id => $this_message->id, |
next if ($m->all_sent); |
|
list_id => $this_list->id, |
|
|
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
|
223 |
|
|
224 |
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; |
225 |
|
my $msg = $m->message_id->message; |
226 |
|
|
227 |
|
foreach my $u ($user_list->search(list_id => $m->list_id)) { |
228 |
|
|
229 |
|
my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n"; |
230 |
|
|
231 |
|
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
232 |
|
print "SKIP ",$u->user_id->email," message allready sent\n"; |
233 |
|
} else { |
234 |
|
print "\t",$u->user_id->email,"\n"; |
235 |
|
send IO => "$hdr\n$msg"; |
236 |
|
$sent->create({ |
237 |
|
message_id => $m->message_id, |
238 |
|
user_id => $u->user_id, |
239 |
|
}); |
240 |
|
$sent->dbi_commit; |
241 |
|
} |
242 |
|
} |
243 |
|
$m->all_sent(1); |
244 |
|
$m->update; |
245 |
|
$m->dbi_commit; |
246 |
|
} |
247 |
|
|
248 |
} else { |
} else { |
249 |
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 |
|
|
'; |
|
250 |
} |
} |
251 |
|
|
252 |
|
=back |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
=head2 Helper options |
257 |
|
|
258 |
|
=over 20 |
259 |
|
|
260 |
|
=item --debug |
261 |
|
|
262 |
|
Turn on debugging output from C<Class::DBI> |
263 |
|
|
264 |
|
=item --verbose |
265 |
|
|
266 |
|
Dump more info on screen. |
267 |
|
|
268 |
|
=back |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
=head1 AUTHOR |
273 |
|
|
274 |
|
Dobrica Pavlinusic <dpavlin@rot13.org> |
275 |
|
|
276 |
|
=cut |
277 |
|
|