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 Mail::CheckUser qw(check_email); |
|
|
use Email::Valid; |
|
|
use Email::Send; |
|
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
9 |
|
|
11 |
|
|
12 |
=head1 SYNOPSYS |
=head1 SYNOPSYS |
13 |
|
|
14 |
|
sender.pl --new=mylist |
15 |
sender.pl --add=mylist members.txt |
sender.pl --add=mylist members.txt |
16 |
|
sender.pl --delete=mylist members.txt |
17 |
sender.pl --list[=mylist] |
sender.pl --list[=mylist] |
18 |
sender.pl --queue[=mylist message.txt] |
sender.pl --queue[=mylist message.txt] |
19 |
sender.pl --send=mylist |
sender.pl --send=mylist |
20 |
|
|
21 |
=head2 All options |
In C</etc/aliases> something like: |
22 |
|
|
23 |
=over 20 |
mylist: "| cd /path/to && ./sender.pl --inbox=mylist" |
24 |
|
|
25 |
=item --debug |
=head2 Command options |
26 |
|
|
27 |
Turn on debugging output from C<Class::DBI> |
=over 20 |
28 |
|
|
29 |
=cut |
=cut |
30 |
|
|
31 |
my $debug = 0; |
my $debug = 0; |
32 |
my $list_opt; |
my $verbose = 0; |
33 |
my $add_opt; |
my $opt; |
|
my $queue_opt; |
|
|
my $send_opt; |
|
34 |
|
|
35 |
my $result = GetOptions( |
my $result = GetOptions( |
36 |
"list:s" => \$list_opt, |
"new=s" => \$opt->{'new'}, |
37 |
"add=s" => \$add_opt, |
"list:s" => \$opt->{'list'}, |
38 |
"queue:s" => \$queue_opt, |
"add=s" => \$opt->{'add'}, |
39 |
"send:s" => \$send_opt, |
"delete=s" => \$opt->{'delete'}, |
40 |
|
"queue:s" => \$opt->{'queue'}, |
41 |
|
"send:s" => \$opt->{'send'}, |
42 |
|
"inbox=s" => \$opt->{'inbox'}, |
43 |
"debug" => \$debug, |
"debug" => \$debug, |
44 |
|
"verbose" => \$verbose, |
45 |
|
"from=s" => \$opt->{'from'}, |
46 |
|
"driver=s" => \$opt->{'email_send_driver'}, |
47 |
|
"sleep=i" => \$opt->{'sleep'}, |
48 |
|
"aliases=s" => \$opt->{'aliases'}, |
49 |
); |
); |
50 |
|
|
51 |
|
my $nos = new Nos( |
52 |
my $loader = Class::DBI::Loader::Pg->new( |
dsn => 'dbi:Pg:dbname=notices', |
53 |
debug => $debug, |
user => 'dpavlin', |
54 |
dsn => "dbi:Pg:dbname=notices", |
passwd => '', |
55 |
user => "dpavlin", |
debug => $debug, |
56 |
password => "", |
verbose => $verbose, |
|
namespace => "Noticer", |
|
|
# additional_classes => qw/Class::DBI::AbstractSearch/, |
|
|
# additional_base_classes => qw/My::Stuff/, |
|
|
relationships => 1, |
|
57 |
); |
); |
58 |
|
|
59 |
|
my $loader = $nos->{'loader'} || die "can't find loader?"; |
60 |
|
|
61 |
my $lists = $loader->find_class('lists'); |
my $lists = $loader->find_class('lists'); |
62 |
my $users = $loader->find_class('users'); |
my $users = $loader->find_class('users'); |
63 |
my $user_list = $loader->find_class('user_list'); |
my $user_list = $loader->find_class('user_list'); |
64 |
my $messages = $loader->find_class('messages'); |
my $messages = $loader->find_class('messages'); |
65 |
my $queue = $loader->find_class('queue'); |
my $queue = $loader->find_class('queue'); |
66 |
|
my $sent = $loader->find_class('sent'); |
67 |
|
|
68 |
$queue->set_sql( list_queue => qq{ |
$queue->set_sql( list_queue => qq{ |
69 |
SELECT messages.message, messages.date AS message_date, lists.name AS list |
SELECT messages.message, messages.date AS date, lists.name AS list |
70 |
FROM queue |
FROM queue |
71 |
JOIN messages on message_id = messages.id |
JOIN messages on message_id = messages.id |
72 |
JOIN lists on list_id = lists.id |
JOIN lists on list_id = lists.id |
73 |
} ); |
} ); |
74 |
|
|
75 |
|
my $list_name; |
76 |
|
|
77 |
|
|
78 |
|
=item --new=list_name my-list@example.com |
79 |
|
|
80 |
|
Adds new list. You can also feed list name as first line to C<STDIN>. |
81 |
|
|
82 |
|
You can also add C<--from='Full name of list'> to specify full name (comment) |
83 |
|
in outgoing e-mail. |
84 |
|
|
85 |
|
Optional parametar C<--aliases='/full/path/to/aliases'> can be used to |
86 |
|
specify aliases file other than C</etc/aliases>. |
87 |
|
|
88 |
|
=cut |
89 |
|
|
90 |
|
if ($list_name = $opt->{'new'}) { |
91 |
|
|
92 |
|
my $email = shift @ARGV || <>; |
93 |
|
chomp($email); |
94 |
|
|
95 |
|
die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); |
96 |
|
|
97 |
|
my $aliases = $opt->{'aliases'} || '/etc/aliases'; |
98 |
|
|
99 |
|
my $id = $nos->new_list( |
100 |
|
list => $list_name, |
101 |
|
from => ($opt->{'from'} || ''), |
102 |
|
email => $email, |
103 |
|
aliases => $aliases, |
104 |
|
) || die "can't add list $list_name\n"; |
105 |
|
|
106 |
|
print "added list $list_name with ID $id\n"; |
107 |
|
|
108 |
|
|
109 |
=item --list[=list_name] |
=item --list[=list_name] |
110 |
|
|
115 |
|
|
116 |
=cut |
=cut |
117 |
|
|
118 |
if (defined($list_opt)) { |
} elsif (defined($list_name = $opt->{'list'})) { |
119 |
|
|
120 |
my @lists; |
my @lists; |
121 |
if ($list_opt ne '') { |
|
122 |
@lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt"; |
if ($list_name ne '') { |
123 |
|
@lists = $lists->search( name=> $list_name )->first || die "can't find list $list_name"; |
124 |
} else { |
} else { |
125 |
@lists = $lists->retrieve_all; |
@lists = $lists->retrieve_all; |
126 |
} |
} |
127 |
|
|
128 |
foreach my $list (@lists) { |
foreach my $list (@lists) { |
129 |
print $list->name,"\n"; |
print $list->name,": ",$list->from_addr," <",$list->email,">\n"; |
130 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
foreach my $u ($nos->list_members( list => $list->name )) { |
131 |
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"; |
|
132 |
} |
} |
133 |
} |
} |
134 |
|
|
135 |
|
|
136 |
=item --add=list_name |
=item --add=list_name |
137 |
|
|
138 |
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 |
143 |
|
|
144 |
=cut |
=cut |
145 |
|
|
146 |
} elsif ($add_opt) { |
} elsif ($list_name = $opt->{'add'}) { |
147 |
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
|
148 |
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"; |
|
149 |
|
|
150 |
my $added = 0; |
my $added = 0; |
151 |
|
|
153 |
chomp; |
chomp; |
154 |
next if (/^#/ || /^\s*$/); |
next if (/^#/ || /^\s*$/); |
155 |
my ($email, $name) = split(/\s+/,$_, 2); |
my ($email, $name) = split(/\s+/,$_, 2); |
156 |
if (! Email::Valid->address($email)) { |
$added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $list_name )); |
|
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++; |
|
157 |
} |
} |
158 |
|
|
159 |
foreach my $c_name ($loader->tables) { |
print "list ",$list->name," has $added users\n"; |
160 |
my $c = $loader->find_class($c_name)|| die "can't find $c_name"; |
|
161 |
$c->dbi_commit(); |
|
162 |
|
=item --delete=list_name |
163 |
|
|
164 |
|
Delete users from list. User e-mails can be stored in file (which can be |
165 |
|
supplied as argument) or read from C<STDIN>. |
166 |
|
|
167 |
|
=cut |
168 |
|
} elsif ($list_name = $opt->{'delete'}) { |
169 |
|
|
170 |
|
my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; |
171 |
|
|
172 |
|
my $deleted = 0; |
173 |
|
|
174 |
|
while(<>) { |
175 |
|
chomp; |
176 |
|
next if (/^#/ || /^\s*$/); |
177 |
|
my $email = $_; |
178 |
|
$deleted++ if ($nos->delete_member_from_list( email => $email, list => $list_name )); |
179 |
} |
} |
180 |
|
|
181 |
print "list ",$list->name," has $added users\n"; |
print "list ",$list->name," lost $deleted users\n"; |
182 |
|
|
183 |
|
|
184 |
=item --queue[=list_name] |
=item --queue[=list_name] |
185 |
|
|
186 |
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 |
187 |
argument) or read from C<STDIN>. |
argument) or read from C<STDIN>. |
188 |
|
|
189 |
This options without optional parametars it will display current queue. |
This option without optional parametar will display pending queue. If you |
190 |
|
add C<--verbose> flag, it will display all messages in queue. |
191 |
|
|
192 |
=cut |
=cut |
193 |
|
|
194 |
} elsif (defined($queue_opt)) { |
} elsif (defined($list_name = $opt->{'queue'})) { |
195 |
|
|
196 |
if ($queue_opt ne '') { |
if ($list_name ne '') { |
197 |
# add message to list queue |
# add message to list queue |
198 |
|
|
|
my $this_list = $lists->search( |
|
|
name => $queue_opt, |
|
|
)->first || die "can't find list $queue_opt"; |
|
|
|
|
199 |
my $message_text; |
my $message_text; |
200 |
while(<>) { |
while(<>) { |
201 |
$message_text .= $_; |
$message_text .= $_; |
202 |
} |
} |
203 |
|
|
204 |
die "no message" unless ($message_text); |
my $id = $nos->add_message_to_list( |
205 |
|
list => $list_name, |
206 |
|
message => $message_text, |
207 |
|
) || die "can't add message to list $list_name\n"; |
208 |
|
|
209 |
my $this_message = $messages->find_or_create({ |
print "added message $id to list $list_name\n"; |
|
message => $message_text |
|
|
}) || die "can't insert message"; |
|
210 |
|
|
211 |
$this_message->dbi_commit() || die "can't add message"; |
} else { |
212 |
|
# list messages in queue |
213 |
|
|
214 |
$queue->find_or_create({ |
foreach my $m ($queue->retrieve_all) { |
215 |
message_id => $this_message->id, |
next if ($m->all_sent && ! $verbose); |
|
list_id => $this_list->id, |
|
|
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
|
216 |
|
|
217 |
$queue->dbi_commit || die "can't add message to list ",$this_list->name; |
my $l = $m->all_sent ? 'S' : 'Q'; |
218 |
|
|
219 |
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
my $date = $m->message_id->date; |
220 |
|
$date =~ s/\..+$//; |
221 |
|
my $msg = $m->message_id->message; |
222 |
|
$msg =~ s/\s+/ /gs; |
223 |
|
|
224 |
} else { |
$l .= sprintf(" %-15s %15s : ", $m->list_id->name, $date); |
225 |
# list messages in queue |
$l .= substr($msg, 0, 79 - length($l)); |
226 |
|
|
|
foreach my $m ($queue->retrieve_all) { |
|
|
my $l = sprintf("%-10s %15s : ", $m->list_id->name, $m->message_id->date); |
|
|
$l .= substr($m->message_id->message, 0, 79 - length($l)); |
|
|
$l =~ s/[\n\r]/ /gs; |
|
227 |
print "$l\n"; |
print "$l\n"; |
228 |
} |
} |
229 |
|
|
230 |
} |
} |
231 |
|
|
232 |
|
|
233 |
=item --send[=list_name] |
=item --send[=list_name] |
234 |
|
|
235 |
Send e-mail waiting in queue for all lists, or with optional argument for |
Send e-mails waiting in queue, or with optional argument, just send messages |
236 |
just single list. |
for single list. |
237 |
|
|
238 |
=cut |
Optional argument C<--driver=smtp> forces sending using SMTP server at |
239 |
|
localhost (127.0.0.1). |
240 |
|
|
241 |
} elsif (defined($send_opt)) { |
Optional argument C<--sleep=42> defines that sender will sleep 42 seconds |
242 |
|
between sending e-mail. |
243 |
|
|
244 |
|
=cut |
245 |
|
|
246 |
die "send option not yet implemented"; |
} elsif (defined($list_name = $opt->{'send'})) { |
247 |
|
|
248 |
my @q; |
unless ($opt->{'email_send_driver'}) { |
249 |
if ($send_opt ne '') { |
print "WARNING: this will dump debugging output to STDERR\n"; |
250 |
# @q => $queue->search( name => 'foo' ); |
print "enter alternative driver (e.g. smtp): "; |
251 |
|
my $d = <STDIN>; |
252 |
|
chomp($d); |
253 |
|
$opt->{'email_send_driver'} = $d; |
254 |
} |
} |
255 |
foreach my $q (@q) { |
|
256 |
|
$nos->send_queued_messages( |
257 |
|
list => $list_name, |
258 |
|
driver => $opt->{'email_send_driver'}, |
259 |
|
sleep => $opt->{'sleep'}, |
260 |
|
); |
261 |
|
|
262 |
|
|
263 |
|
=item --inbox=list_name |
264 |
|
|
265 |
|
Feed incomming message back into notice sender. |
266 |
|
|
267 |
|
=cut |
268 |
|
|
269 |
|
} elsif ($list_name = $opt->{'inbox'}) { |
270 |
|
|
271 |
|
my $message; |
272 |
|
while(<>) { |
273 |
|
$message .= $_; |
274 |
} |
} |
275 |
|
|
276 |
|
$nos->inbox_message( |
277 |
|
list => $list_name, |
278 |
|
message => $message, |
279 |
|
) || die "can't receive message for list $list_name"; |
280 |
|
|
281 |
|
|
282 |
} else { |
} else { |
283 |
die "see perldoc $0 for help"; |
die "see perldoc $0 for help\n"; |
284 |
} |
} |
285 |
|
|
286 |
=back |
=back |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
=head2 Helper options |
291 |
|
|
292 |
|
=over 20 |
293 |
|
|
294 |
|
=item --debug |
295 |
|
|
296 |
|
Turn on debugging output from C<Class::DBI> |
297 |
|
|
298 |
|
=item --verbose |
299 |
|
|
300 |
|
Dump more info on screen. |
301 |
|
|
302 |
|
=back |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
=head1 AUTHOR |
=head1 AUTHOR |
307 |
|
|
308 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
Dobrica Pavlinusic <dpavlin@rot13.org> |