1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use blib; |
5 |
use Nos; |
6 |
use Getopt::Long; |
7 |
|
8 |
=head1 NAME |
9 |
|
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 --list[=mylist] |
17 |
sender.pl --queue[=mylist message.txt] |
18 |
sender.pl --send=mylist |
19 |
|
20 |
In C</etc/aliases> something like: |
21 |
|
22 |
mylist: "| /path/to/sender.pl --inbox=mylist" |
23 |
|
24 |
=head2 Command options |
25 |
|
26 |
=over 20 |
27 |
|
28 |
=cut |
29 |
|
30 |
my $debug = 0; |
31 |
my $verbose = 0; |
32 |
my $opt; |
33 |
|
34 |
my $result = GetOptions( |
35 |
"new=s" => \$opt->{'new'}, |
36 |
"list:s" => \$opt->{'list'}, |
37 |
"add=s" => \$opt->{'add'}, |
38 |
"queue:s" => \$opt->{'queue'}, |
39 |
"send:s" => \$opt->{'send'}, |
40 |
"inbox=s" => \$opt->{'inbox'}, |
41 |
"debug" => \$debug, |
42 |
"verbose" => \$verbose, |
43 |
); |
44 |
|
45 |
my $nos = new Nos( |
46 |
dsn => 'dbi:Pg:dbname=notices', |
47 |
user => 'dpavlin', |
48 |
passwd => '', |
49 |
debug => $debug, |
50 |
verbose => $verbose, |
51 |
); |
52 |
|
53 |
my $loader = $nos->{'loader'} || die "can't find loader?"; |
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 |
my $list_name; |
70 |
|
71 |
|
72 |
=item --new=list_name my-list@example.com |
73 |
|
74 |
Adds new list. You can also feed list name as first line to C<STDIN>. |
75 |
|
76 |
=cut |
77 |
|
78 |
if ($list_name = $opt->{'new'}) { |
79 |
|
80 |
my $email = shift @ARGV || <>; |
81 |
chomp($email); |
82 |
|
83 |
die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); |
84 |
|
85 |
my $id = $nos->new_list( |
86 |
list => $list_name, |
87 |
email => $email, |
88 |
) || die "can't add list $list_name\n"; |
89 |
|
90 |
print "added list $list_name with ID $id\n"; |
91 |
|
92 |
|
93 |
=item --list[=list_name] |
94 |
|
95 |
List all available lists and users on them. |
96 |
|
97 |
Optional value is name of list. With it, this option will produce just users |
98 |
on that list. |
99 |
|
100 |
=cut |
101 |
|
102 |
} elsif (defined($list_name = $opt->{'list'})) { |
103 |
|
104 |
my @lists; |
105 |
|
106 |
if ($list_name ne '') { |
107 |
@lists = $lists->search( name=> $list_name )->first || die "can't find list $list_name"; |
108 |
} else { |
109 |
@lists = $lists->retrieve_all; |
110 |
} |
111 |
|
112 |
foreach my $list (@lists) { |
113 |
print $list->name," <",$list->email,">\n"; |
114 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
115 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
116 |
print "\t",$user->full_name," <", $user->email, ">\n"; |
117 |
} |
118 |
} |
119 |
|
120 |
|
121 |
=item --add=list_name |
122 |
|
123 |
Add users to list. Users are stored in file (which can be supplied as |
124 |
argument) or read from C<STDIN>. List should be in following format: |
125 |
|
126 |
email@example.com Optional full name of person |
127 |
dpavlin@rot13.org Dobrica Pavlinusic |
128 |
|
129 |
=cut |
130 |
|
131 |
} elsif ($list_name = $opt->{'add'}) { |
132 |
|
133 |
my $list = $lists->find_or_create({ |
134 |
name => $list_name, |
135 |
}) || die "can't add list $list_name\n"; |
136 |
|
137 |
my $added = 0; |
138 |
|
139 |
while(<>) { |
140 |
chomp; |
141 |
next if (/^#/ || /^\s*$/); |
142 |
my ($email, $name) = split(/\s+/,$_, 2); |
143 |
$added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $list_name )); |
144 |
} |
145 |
|
146 |
print "list ",$list->name," has $added users\n"; |
147 |
|
148 |
|
149 |
=item --queue[=list_name] |
150 |
|
151 |
Queue message for later delivery. Message can be read from file (specified as |
152 |
argument) or read from C<STDIN>. |
153 |
|
154 |
This option without optional parametar will display pending queue. If you |
155 |
add C<--verbose> flag, it will display all messages in queue. |
156 |
|
157 |
=cut |
158 |
|
159 |
} elsif (defined($list_name = $opt->{'queue'})) { |
160 |
|
161 |
if ($list_name ne '') { |
162 |
# add message to list queue |
163 |
|
164 |
my $message_text; |
165 |
while(<>) { |
166 |
$message_text .= $_; |
167 |
} |
168 |
|
169 |
my $id = $nos->add_message_to_list( |
170 |
list => $list_name, |
171 |
message => $message_text, |
172 |
) || die "can't add message to list $list_name\n"; |
173 |
|
174 |
print "added message $id to list $list_name\n"; |
175 |
|
176 |
} else { |
177 |
# list messages in queue |
178 |
|
179 |
foreach my $m ($queue->retrieve_all) { |
180 |
next if ($m->all_sent && ! $verbose); |
181 |
|
182 |
my $l = $m->all_sent ? 'S' : 'Q'; |
183 |
|
184 |
my $date = $m->message_id->date; |
185 |
$date =~ s/\..+$//; |
186 |
my $msg = $m->message_id->message; |
187 |
$msg =~ s/\s+/ /gs; |
188 |
|
189 |
$l .= sprintf(" %-15s %15s : ", $m->list_id->name, $date); |
190 |
$l .= substr($msg, 0, 79 - length($l)); |
191 |
|
192 |
print "$l\n"; |
193 |
} |
194 |
|
195 |
} |
196 |
|
197 |
|
198 |
=item --send[=list_name] |
199 |
|
200 |
Send e-mails waiting in queue, or with optional argument, just send messages |
201 |
for single list. |
202 |
|
203 |
=cut |
204 |
|
205 |
} elsif (defined($list_name = $opt->{'send'})) { |
206 |
|
207 |
$nos->send_queued_messages($list_name); |
208 |
|
209 |
|
210 |
=item --inbox=list_name |
211 |
|
212 |
Feed incomming message back into notice sender. |
213 |
|
214 |
=cut |
215 |
|
216 |
} elsif ($list_name = $opt->{'inbox'}) { |
217 |
|
218 |
my $message; |
219 |
while(<>) { |
220 |
$message .= $_; |
221 |
} |
222 |
|
223 |
$nos->inbox_message( |
224 |
list => $list_name, |
225 |
message => $message, |
226 |
) || die "can't receive message for list $list_name"; |
227 |
|
228 |
|
229 |
} else { |
230 |
die "see perldoc $0 for help\n"; |
231 |
} |
232 |
|
233 |
=back |
234 |
|
235 |
|
236 |
|
237 |
=head2 Helper options |
238 |
|
239 |
=over 20 |
240 |
|
241 |
=item --debug |
242 |
|
243 |
Turn on debugging output from C<Class::DBI> |
244 |
|
245 |
=item --verbose |
246 |
|
247 |
Dump more info on screen. |
248 |
|
249 |
=back |
250 |
|
251 |
|
252 |
|
253 |
=head1 AUTHOR |
254 |
|
255 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
256 |
|
257 |
=cut |
258 |
|