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 0.7; |
6 |
use Getopt::Long; |
use Getopt::Long; |
7 |
use Mail::CheckUser qw(check_email); |
use Pod::Usage; |
|
use Email::Valid; |
|
|
use Email::Send; |
|
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
11 |
sender.pl - command line notify sender utility |
sender.pl - command line notify sender utility |
12 |
|
|
13 |
=head1 SYNOPSYS |
=head1 SYNOPSIS |
14 |
|
|
15 |
|
sender.pl --create=mylist |
16 |
|
sender.pl --drop=mylist |
17 |
sender.pl --add=mylist members.txt |
sender.pl --add=mylist members.txt |
18 |
|
sender.pl --delete=mylist members.txt |
19 |
sender.pl --list[=mylist] |
sender.pl --list[=mylist] |
20 |
sender.pl --queue[=mylist message.txt] |
sender.pl --queue[=mylist message.txt] |
21 |
sender.pl --send=mylist |
sender.pl --send=mylist |
22 |
|
sender.pl --help |
23 |
|
sender.pl --man |
24 |
|
|
25 |
=head2 Command options |
=head1 OPTIONS |
26 |
|
|
27 |
=over 20 |
=over 20 |
28 |
|
|
30 |
|
|
31 |
my $debug = 0; |
my $debug = 0; |
32 |
my $verbose = 0; |
my $verbose = 0; |
33 |
my $list_opt; |
my $opt; |
|
my $add_opt; |
|
|
my $queue_opt; |
|
|
my $send_opt; |
|
34 |
|
|
35 |
my $result = GetOptions( |
my $result = GetOptions( |
36 |
"list:s" => \$list_opt, |
"create=s" => \$opt->{'create'}, |
37 |
"add=s" => \$add_opt, |
"drop=s" => \$opt->{'drop'}, |
38 |
"queue:s" => \$queue_opt, |
"list:s" => \$opt->{'list'}, |
39 |
"send:s" => \$send_opt, |
"add=s" => \$opt->{'add'}, |
40 |
|
"delete=s" => \$opt->{'delete'}, |
41 |
|
"queue:s" => \$opt->{'queue'}, |
42 |
|
"send:s" => \$opt->{'send'}, |
43 |
|
"inbox=s" => \$opt->{'inbox'}, |
44 |
"debug" => \$debug, |
"debug" => \$debug, |
45 |
"verbose" => \$verbose, |
"verbose" => \$verbose, |
46 |
|
"from=s" => \$opt->{'from'}, |
47 |
|
"driver=s" => \$opt->{'email_send_driver'}, |
48 |
|
"sleep=i" => \$opt->{'sleep'}, |
49 |
|
"aliases=s" => \$opt->{'aliases'}, |
50 |
|
"help" => \$opt->{'help'}, |
51 |
|
"man" => \$opt->{'man'} |
52 |
|
) || pod2usage(-verbose => 0); |
53 |
|
|
54 |
|
pod2usage(-verbose => 1) if ($opt->{'help'}); |
55 |
|
pod2usage(-verbose => 2) if ($opt->{'man'}); |
56 |
|
|
57 |
|
my $nos = new Nos( |
58 |
|
dsn => 'dbi:Pg:dbname=notices', |
59 |
|
user => 'dpavlin', |
60 |
|
passwd => '', |
61 |
|
debug => $debug, |
62 |
|
verbose => $verbose, |
63 |
); |
); |
64 |
|
|
65 |
|
my $loader = $nos->{'loader'} || die "can't find loader?"; |
|
my $loader = Class::DBI::Loader::Pg->new( |
|
|
debug => $debug, |
|
|
dsn => "dbi:Pg:dbname=notices", |
|
|
user => "dpavlin", |
|
|
password => "", |
|
|
namespace => "Noticer", |
|
|
# additional_classes => qw/Class::DBI::AbstractSearch/, |
|
|
# additional_base_classes => qw/My::Stuff/, |
|
|
relationships => 1, |
|
|
); |
|
66 |
|
|
67 |
my $lists = $loader->find_class('lists'); |
my $lists = $loader->find_class('lists'); |
68 |
my $users = $loader->find_class('users'); |
my $users = $loader->find_class('users'); |
78 |
JOIN lists on list_id = lists.id |
JOIN lists on list_id = lists.id |
79 |
} ); |
} ); |
80 |
|
|
81 |
|
my $list_name; |
82 |
|
|
83 |
|
=item --aliases=/full/path/to/aliases |
84 |
|
|
85 |
|
Optional parametar C<--aliases> can be used to specify aliases file other |
86 |
|
than default C</etc/aliases>. |
87 |
|
|
88 |
|
=cut |
89 |
|
|
90 |
|
my $aliases = $opt->{'aliases'} || '/etc/aliases'; |
91 |
|
|
92 |
|
|
93 |
|
=item --create=list_name my-list@example.com |
94 |
|
|
95 |
|
Adds new list. You can also feed list name as first line to C<STDIN>. |
96 |
|
|
97 |
|
You can also add C<--from='Full name of list'> to specify full name (comment) |
98 |
|
in outgoing e-mail. |
99 |
|
|
100 |
|
=cut |
101 |
|
|
102 |
|
if ($list_name = $opt->{'create'}) { |
103 |
|
|
104 |
|
my $email = shift @ARGV || <>; |
105 |
|
chomp($email); |
106 |
|
|
107 |
|
die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); |
108 |
|
|
109 |
|
my $id = $nos->create_list( |
110 |
|
list => $list_name, |
111 |
|
from => ($opt->{'from'} || ''), |
112 |
|
email => $email, |
113 |
|
aliases => $aliases, |
114 |
|
) || die "can't add list $list_name\n"; |
115 |
|
|
116 |
|
print "added list $list_name with ID $id\n"; |
117 |
|
|
118 |
|
|
119 |
|
=item --drop=list_name |
120 |
|
|
121 |
|
Remove list. |
122 |
|
|
123 |
|
Optional parametar C<--aliases='/full/path/to/aliases'> can be used to |
124 |
|
specify aliases file other than C</etc/aliases>. |
125 |
|
|
126 |
|
=cut |
127 |
|
|
128 |
|
} elsif ($list_name = $opt->{'drop'}) { |
129 |
|
|
130 |
|
my $id = $nos->drop_list( |
131 |
|
list => $list_name, |
132 |
|
aliases => $aliases, |
133 |
|
) || die "can't remove list $list_name\n"; |
134 |
|
|
135 |
|
print "drop list $list_name with ID $id\n"; |
136 |
|
|
137 |
|
|
138 |
=item --list[=list_name] |
=item --list[=list_name] |
139 |
|
|
144 |
|
|
145 |
=cut |
=cut |
146 |
|
|
147 |
if (defined($list_opt)) { |
} elsif (defined($list_name = $opt->{'list'})) { |
148 |
|
|
149 |
my @lists; |
my @lists; |
150 |
if ($list_opt ne '') { |
|
151 |
@lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt"; |
if ($list_name ne '') { |
152 |
|
@lists = $lists->search( name=> $list_name )->first || die "can't find list $list_name"; |
153 |
} else { |
} else { |
154 |
@lists = $lists->retrieve_all; |
@lists = $lists->retrieve_all; |
155 |
} |
} |
156 |
|
|
157 |
foreach my $list (@lists) { |
foreach my $list (@lists) { |
158 |
print $list->name,"\n"; |
print $list->name,": ",$list->from_addr," <",$list->email,">\n"; |
159 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
foreach my $u ($nos->list_members( list => $list->name )) { |
160 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
print "\t",$u->{'name'}, " <", $u->{'email'}, ">",( $u->{'ext_id'} ? ' ['.$u->{'ext_id'}.']' : '' ),"\n"; |
|
print "\t",$user->full_name," <", $user->email, ">\n"; |
|
161 |
} |
} |
162 |
} |
} |
163 |
|
|
164 |
|
|
165 |
=item --add=list_name |
=item --add=list_name |
166 |
|
|
167 |
Add users to list. Users are stored in file (which can be supplied as |
Add users to list. Users are stored in file (which can be supplied as |
172 |
|
|
173 |
=cut |
=cut |
174 |
|
|
175 |
} elsif ($add_opt) { |
} elsif ($list_name = $opt->{'add'}) { |
176 |
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
|
177 |
my $list = $lists->find_or_create({ |
my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; |
|
name => $add_opt, |
|
|
}) || die "can't add list $add_opt\n"; |
|
178 |
|
|
179 |
my $added = 0; |
my $added = 0; |
180 |
|
|
182 |
chomp; |
chomp; |
183 |
next if (/^#/ || /^\s*$/); |
next if (/^#/ || /^\s*$/); |
184 |
my ($email, $name) = split(/\s+/,$_, 2); |
my ($email, $name) = split(/\s+/,$_, 2); |
185 |
$name ||= ''; |
$added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $list_name )); |
|
if (! Email::Valid->address($email)) { |
|
|
print "SKIPPING $name <$email>\n"; |
|
|
next; |
|
|
} |
|
|
print "# $name <$email>\n"; |
|
|
my $this_user = $users->find_or_create({ |
|
|
email => $email, |
|
|
full_name => $name, |
|
|
}) || die "can't find or create member\n"; |
|
|
my $user_on_list = $user_list->find_or_create({ |
|
|
user_id => $this_user->id, |
|
|
list_id => $list->id, |
|
|
}) || die "can't add user to list"; |
|
|
$added++; |
|
186 |
} |
} |
187 |
|
|
188 |
foreach my $c_name ($loader->tables) { |
print "list ",$list->name," has $added users\n"; |
189 |
my $c = $loader->find_class($c_name)|| die "can't find $c_name"; |
|
190 |
$c->dbi_commit(); |
|
191 |
|
=item --delete=list_name |
192 |
|
|
193 |
|
Delete users from list. User e-mails can be stored in file (which can be |
194 |
|
supplied as argument) or read from C<STDIN>. |
195 |
|
|
196 |
|
=cut |
197 |
|
} elsif ($list_name = $opt->{'delete'}) { |
198 |
|
|
199 |
|
my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; |
200 |
|
|
201 |
|
my $deleted = 0; |
202 |
|
|
203 |
|
while(<>) { |
204 |
|
chomp; |
205 |
|
next if (/^#/ || /^\s*$/); |
206 |
|
my $email = $_; |
207 |
|
$deleted++ if ($nos->delete_member_from_list( email => $email, list => $list_name )); |
208 |
} |
} |
209 |
|
|
210 |
print "list ",$list->name," has $added users\n"; |
print "list ",$list->name," lost $deleted users\n"; |
211 |
|
|
212 |
|
|
213 |
=item --queue[=list_name] |
=item --queue[=list_name] |
214 |
|
|
220 |
|
|
221 |
=cut |
=cut |
222 |
|
|
223 |
} elsif (defined($queue_opt)) { |
} elsif (defined($list_name = $opt->{'queue'})) { |
224 |
|
|
225 |
if ($queue_opt ne '') { |
if ($list_name ne '') { |
226 |
# add message to list queue |
# add message to list queue |
227 |
|
|
|
my $this_list = $lists->search( |
|
|
name => $queue_opt, |
|
|
)->first || die "can't find list $queue_opt"; |
|
|
|
|
228 |
my $message_text; |
my $message_text; |
229 |
while(<>) { |
while(<>) { |
230 |
$message_text .= $_; |
$message_text .= $_; |
231 |
} |
} |
232 |
|
|
233 |
die "no message" unless ($message_text); |
my $id = $nos->add_message_to_list( |
234 |
|
list => $list_name, |
235 |
|
message => $message_text, |
236 |
|
) || die "can't add message to list $list_name\n"; |
237 |
|
|
238 |
my $this_message = $messages->find_or_create({ |
print "added message $id to list $list_name\n"; |
|
message => $message_text |
|
|
}) || die "can't insert message"; |
|
|
|
|
|
$this_message->dbi_commit() || die "can't add message"; |
|
|
|
|
|
$queue->find_or_create({ |
|
|
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; |
|
|
|
|
|
$queue->dbi_commit || die "can't add message to list ",$this_list->name; |
|
|
|
|
|
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
|
239 |
|
|
240 |
} else { |
} else { |
241 |
# list messages in queue |
# list messages in queue |
250 |
my $msg = $m->message_id->message; |
my $msg = $m->message_id->message; |
251 |
$msg =~ s/\s+/ /gs; |
$msg =~ s/\s+/ /gs; |
252 |
|
|
253 |
$l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); |
$l .= sprintf(" %-15s %15s : ", $m->list_id->name, $date); |
254 |
$l .= substr($msg, 0, 79 - length($l)); |
$l .= substr($msg, 0, 79 - length($l)); |
255 |
|
|
256 |
print "$l\n"; |
print "$l\n"; |
258 |
|
|
259 |
} |
} |
260 |
|
|
261 |
|
|
262 |
=item --send[=list_name] |
=item --send[=list_name] |
263 |
|
|
264 |
Send e-mails waiting in queue, or with optional argument, just send messages |
Send e-mails waiting in queue, or with optional argument, just send messages |
265 |
for single list. |
for single list. |
266 |
|
|
267 |
|
Optional argument C<--driver=smtp> forces sending using SMTP server at |
268 |
|
localhost (127.0.0.1). |
269 |
|
|
270 |
|
Optional argument C<--sleep=42> defines that sender will sleep 42 seconds |
271 |
|
between sending e-mail. |
272 |
|
|
273 |
=cut |
=cut |
274 |
|
|
275 |
} elsif (defined($send_opt)) { |
} elsif (defined($list_name = $opt->{'send'})) { |
276 |
|
|
277 |
my $my_q; |
unless ($opt->{'email_send_driver'}) { |
278 |
if ($send_opt ne '') { |
print "WARNING: this will dump debugging output to STDERR\n"; |
279 |
my $l_id = $lists->search_like( name => $send_opt )->first || |
print "enter alternative driver (e.g. smtp): "; |
280 |
die "can't find list $send_opt"; |
my $d = <STDIN>; |
281 |
$my_q = $queue->search_like( list_id => $l_id ) || |
chomp($d); |
282 |
die "can't find list $send_opt"; |
$opt->{'email_send_driver'} = $d; |
|
} else { |
|
|
$my_q = $queue->retrieve_all; |
|
283 |
} |
} |
284 |
|
|
285 |
while (my $m = $my_q->next) { |
$nos->send_queued_messages( |
286 |
next if ($m->all_sent); |
list => $list_name, |
287 |
|
driver => $opt->{'email_send_driver'}, |
288 |
|
sleep => $opt->{'sleep'}, |
289 |
|
); |
290 |
|
|
|
print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; |
|
|
my $msg = $m->message_id->message; |
|
291 |
|
|
292 |
foreach my $u ($user_list->search(list_id => $m->list_id)) { |
=item --inbox=list_name |
293 |
|
|
294 |
my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n"; |
Feed incomming message back into notice sender. |
295 |
|
|
296 |
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
=cut |
297 |
print "SKIP ",$u->user_id->email," message allready sent\n"; |
|
298 |
} else { |
} elsif ($list_name = $opt->{'inbox'}) { |
299 |
print "\t",$u->user_id->email,"\n"; |
|
300 |
send IO => "$hdr\n$msg"; |
my $message; |
301 |
$sent->create({ |
while(<>) { |
302 |
message_id => $m->message_id, |
$message .= $_; |
|
user_id => $u->user_id, |
|
|
}); |
|
|
$sent->dbi_commit; |
|
|
} |
|
|
} |
|
|
$m->all_sent(1); |
|
|
$m->update; |
|
|
$m->dbi_commit; |
|
303 |
} |
} |
304 |
|
|
305 |
|
$nos->inbox_message( |
306 |
|
list => $list_name, |
307 |
|
message => $message, |
308 |
|
) || die "can't receive message for list $list_name"; |
309 |
|
|
310 |
|
|
311 |
} else { |
} else { |
312 |
die "see perldoc $0 for help"; |
pod2usage(-verbose=>0); |
313 |
} |
} |
314 |
|
|
315 |
=back |
=back |
330 |
|
|
331 |
=back |
=back |
332 |
|
|
333 |
|
=head1 DESCRIPTION |
334 |
|
|
335 |
|
This command will use notice-sender C<Nos.pm> module directly to make modifications on lists |
336 |
|
or with C<--inbox> option server as incomming mail filter. |
337 |
|
|
338 |
=head1 AUTHOR |
=head1 AUTHOR |
339 |
|
|