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 --add=mylist members.txt |
15 |
sender.pl --list[=mylist] |
16 |
sender.pl --queue[=mylist message.txt] |
17 |
sender.pl --send=mylist |
18 |
|
19 |
=head2 Command options |
20 |
|
21 |
=over 20 |
22 |
|
23 |
=cut |
24 |
|
25 |
my $debug = 0; |
26 |
my $verbose = 0; |
27 |
my $list_opt; |
28 |
my $add_opt; |
29 |
my $queue_opt; |
30 |
my $send_opt; |
31 |
my $email_opt; |
32 |
|
33 |
my $result = GetOptions( |
34 |
"list:s" => \$list_opt, |
35 |
"add=s" => \$add_opt, |
36 |
"queue:s" => \$queue_opt, |
37 |
"send:s" => \$send_opt, |
38 |
"debug" => \$debug, |
39 |
"verbose" => \$verbose, |
40 |
"email=s" => \$email_opt, |
41 |
); |
42 |
|
43 |
my $nos = new Nos( |
44 |
dsn => 'dbi:Pg:dbname=notices', |
45 |
user => 'dpavlin', |
46 |
passwd => '', |
47 |
debug => $debug, |
48 |
verbose => $verbose, |
49 |
); |
50 |
|
51 |
my $loader = $nos->{'loader'} || die "can't find loader?"; |
52 |
|
53 |
my $lists = $loader->find_class('lists'); |
54 |
my $users = $loader->find_class('users'); |
55 |
my $user_list = $loader->find_class('user_list'); |
56 |
my $messages = $loader->find_class('messages'); |
57 |
my $queue = $loader->find_class('queue'); |
58 |
my $sent = $loader->find_class('sent'); |
59 |
|
60 |
$queue->set_sql( list_queue => qq{ |
61 |
SELECT messages.message, messages.date AS date, lists.name AS list |
62 |
FROM queue |
63 |
JOIN messages on message_id = messages.id |
64 |
JOIN lists on list_id = lists.id |
65 |
} ); |
66 |
|
67 |
|
68 |
=item --list[=list_name] |
69 |
|
70 |
List all available lists and users on them. |
71 |
|
72 |
Optional value is name of list. With it, this option will produce just users |
73 |
on that list. |
74 |
|
75 |
=cut |
76 |
|
77 |
if (defined($list_opt)) { |
78 |
my @lists; |
79 |
if ($list_opt ne '') { |
80 |
@lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt"; |
81 |
} else { |
82 |
@lists = $lists->retrieve_all; |
83 |
} |
84 |
|
85 |
foreach my $list (@lists) { |
86 |
print $list->name," <",$list->email,">\n"; |
87 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
88 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
89 |
print "\t",$user->full_name," <", $user->email, ">\n"; |
90 |
} |
91 |
} |
92 |
|
93 |
=item --add=list_name |
94 |
|
95 |
Add users to list. Users are stored in file (which can be supplied as |
96 |
argument) or read from C<STDIN>. List should be in following format: |
97 |
|
98 |
email@example.com Optional full name of person |
99 |
dpavlin@rot13.org Dobrica Pavlinusic |
100 |
|
101 |
You may use C<--email> parametar at any time to set From: e-mail address for list. |
102 |
B<This seems somewhat cludgy, and it will probably change in future>. |
103 |
|
104 |
=cut |
105 |
|
106 |
} elsif ($add_opt) { |
107 |
my $list = $lists->find_or_create({ |
108 |
name => $add_opt, |
109 |
}) || die "can't add list $add_opt\n"; |
110 |
|
111 |
if ($email_opt && $list->email ne $email_opt) { |
112 |
$list->email($email_opt); |
113 |
$list->update; |
114 |
$list->dbi_commit; |
115 |
} |
116 |
|
117 |
my $added = 0; |
118 |
|
119 |
while(<>) { |
120 |
chomp; |
121 |
next if (/^#/ || /^\s*$/); |
122 |
my ($email, $name) = split(/\s+/,$_, 2); |
123 |
$added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $add_opt )); |
124 |
} |
125 |
|
126 |
print "list ",$list->name," has $added users\n"; |
127 |
|
128 |
=item --queue[=list_name] |
129 |
|
130 |
Queue message for later delivery. Message can be read from file (specified as |
131 |
argument) or read from C<STDIN>. |
132 |
|
133 |
This option without optional parametar will display pending queue. If you |
134 |
add C<--verbose> flag, it will display all messages in queue. |
135 |
|
136 |
=cut |
137 |
|
138 |
} elsif (defined($queue_opt)) { |
139 |
|
140 |
if ($queue_opt ne '') { |
141 |
# add message to list queue |
142 |
|
143 |
my $this_list = $lists->search( |
144 |
name => $queue_opt, |
145 |
)->first || die "can't find list $queue_opt"; |
146 |
|
147 |
my $message_text; |
148 |
while(<>) { |
149 |
$message_text .= $_; |
150 |
} |
151 |
|
152 |
die "no message" unless ($message_text); |
153 |
|
154 |
my $this_message = $messages->find_or_create({ |
155 |
message => $message_text |
156 |
}) || die "can't insert message"; |
157 |
|
158 |
$this_message->dbi_commit() || die "can't add message"; |
159 |
|
160 |
$queue->find_or_create({ |
161 |
message_id => $this_message->id, |
162 |
list_id => $this_list->id, |
163 |
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
164 |
|
165 |
$queue->dbi_commit || die "can't add message to list ",$this_list->name; |
166 |
|
167 |
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
168 |
|
169 |
} else { |
170 |
# list messages in queue |
171 |
|
172 |
foreach my $m ($queue->retrieve_all) { |
173 |
next if ($m->all_sent && ! $verbose); |
174 |
|
175 |
my $l = $m->all_sent ? 'S' : 'Q'; |
176 |
|
177 |
my $date = $m->message_id->date; |
178 |
$date =~ s/\..+$//; |
179 |
my $msg = $m->message_id->message; |
180 |
$msg =~ s/\s+/ /gs; |
181 |
|
182 |
$l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); |
183 |
$l .= substr($msg, 0, 79 - length($l)); |
184 |
|
185 |
print "$l\n"; |
186 |
} |
187 |
|
188 |
} |
189 |
|
190 |
=item --send[=list_name] |
191 |
|
192 |
Send e-mails waiting in queue, or with optional argument, just send messages |
193 |
for single list. |
194 |
|
195 |
=cut |
196 |
|
197 |
} elsif (defined($send_opt)) { |
198 |
|
199 |
$nos->send_queued_messages($send_opt); |
200 |
|
201 |
} else { |
202 |
die "see perldoc $0 for help"; |
203 |
} |
204 |
|
205 |
=back |
206 |
|
207 |
|
208 |
|
209 |
=head2 Helper options |
210 |
|
211 |
=over 20 |
212 |
|
213 |
=item --debug |
214 |
|
215 |
Turn on debugging output from C<Class::DBI> |
216 |
|
217 |
=item --verbose |
218 |
|
219 |
Dump more info on screen. |
220 |
|
221 |
=item --email |
222 |
|
223 |
Used to specify e-mail address where needed. |
224 |
|
225 |
=back |
226 |
|
227 |
|
228 |
|
229 |
=head1 AUTHOR |
230 |
|
231 |
Dobrica Pavlinusic <dpavlin@rot13.org> |
232 |
|
233 |
=cut |
234 |
|