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