2 |
|
|
3 |
use strict; |
use strict; |
4 |
use blib; |
use blib; |
5 |
use Nos; |
use Nos 0.7; |
6 |
use Getopt::Long; |
use Getopt::Long; |
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
11 |
|
|
12 |
=head1 SYNOPSYS |
=head1 SYNOPSYS |
13 |
|
|
14 |
sender.pl --new=mylist |
sender.pl --create=mylist |
15 |
|
sender.pl --drop=mylist |
16 |
sender.pl --add=mylist members.txt |
sender.pl --add=mylist members.txt |
17 |
|
sender.pl --delete=mylist members.txt |
18 |
sender.pl --list[=mylist] |
sender.pl --list[=mylist] |
19 |
sender.pl --queue[=mylist message.txt] |
sender.pl --queue[=mylist message.txt] |
20 |
sender.pl --send=mylist |
sender.pl --send=mylist |
21 |
|
|
22 |
In C</etc/aliases> something like: |
In C</etc/aliases> something like: |
23 |
|
|
24 |
mylist: "| /path/to/sender.pl --inbox=mylist" |
mylist: "| cd /path/to && ./sender.pl --inbox=mylist" |
25 |
|
|
26 |
=head2 Command options |
=head2 Command options |
27 |
|
|
34 |
my $opt; |
my $opt; |
35 |
|
|
36 |
my $result = GetOptions( |
my $result = GetOptions( |
37 |
"new=s" => \$opt->{'new'}, |
"create=s" => \$opt->{'create'}, |
38 |
|
"drop=s" => \$opt->{'drop'}, |
39 |
"list:s" => \$opt->{'list'}, |
"list:s" => \$opt->{'list'}, |
40 |
"add=s" => \$opt->{'add'}, |
"add=s" => \$opt->{'add'}, |
41 |
|
"delete=s" => \$opt->{'delete'}, |
42 |
"queue:s" => \$opt->{'queue'}, |
"queue:s" => \$opt->{'queue'}, |
43 |
"send:s" => \$opt->{'send'}, |
"send:s" => \$opt->{'send'}, |
44 |
"inbox=s" => \$opt->{'inbox'}, |
"inbox=s" => \$opt->{'inbox'}, |
46 |
"verbose" => \$verbose, |
"verbose" => \$verbose, |
47 |
"from=s" => \$opt->{'from'}, |
"from=s" => \$opt->{'from'}, |
48 |
"driver=s" => \$opt->{'email_send_driver'}, |
"driver=s" => \$opt->{'email_send_driver'}, |
49 |
|
"sleep=i" => \$opt->{'sleep'}, |
50 |
|
"aliases=s" => \$opt->{'aliases'}, |
51 |
); |
); |
52 |
|
|
53 |
my $nos = new Nos( |
my $nos = new Nos( |
76 |
|
|
77 |
my $list_name; |
my $list_name; |
78 |
|
|
79 |
|
=item --aliases=/full/path/to/aliases |
80 |
|
|
81 |
=item --new=list_name my-list@example.com |
Optional parametar C<--aliases> can be used to specify aliases file other |
82 |
|
than default C</etc/aliases>. |
83 |
|
|
84 |
|
=cut |
85 |
|
|
86 |
|
my $aliases = $opt->{'aliases'} || '/etc/aliases'; |
87 |
|
|
88 |
|
|
89 |
|
=item --create=list_name my-list@example.com |
90 |
|
|
91 |
Adds new list. You can also feed list name as first line to C<STDIN>. |
Adds new list. You can also feed list name as first line to C<STDIN>. |
92 |
|
|
95 |
|
|
96 |
=cut |
=cut |
97 |
|
|
98 |
if ($list_name = $opt->{'new'}) { |
if ($list_name = $opt->{'create'}) { |
99 |
|
|
100 |
my $email = shift @ARGV || <>; |
my $email = shift @ARGV || <>; |
101 |
chomp($email); |
chomp($email); |
102 |
|
|
103 |
die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); |
die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); |
104 |
|
|
105 |
my $id = $nos->new_list( |
my $id = $nos->create_list( |
106 |
list => $list_name, |
list => $list_name, |
107 |
from => ($opt->{'from'} || ''), |
from => ($opt->{'from'} || ''), |
108 |
email => $email, |
email => $email, |
109 |
|
aliases => $aliases, |
110 |
) || die "can't add list $list_name\n"; |
) || die "can't add list $list_name\n"; |
111 |
|
|
112 |
print "added list $list_name with ID $id\n"; |
print "added list $list_name with ID $id\n"; |
113 |
|
|
114 |
|
|
115 |
|
=item --drop=list_name |
116 |
|
|
117 |
|
Remove list. |
118 |
|
|
119 |
|
Optional parametar C<--aliases='/full/path/to/aliases'> can be used to |
120 |
|
specify aliases file other than C</etc/aliases>. |
121 |
|
|
122 |
|
=cut |
123 |
|
|
124 |
|
} elsif ($list_name = $opt->{'drop'}) { |
125 |
|
|
126 |
|
my $id = $nos->drop_list( |
127 |
|
list => $list_name, |
128 |
|
aliases => $aliases, |
129 |
|
) || die "can't remove list $list_name\n"; |
130 |
|
|
131 |
|
print "drop list $list_name with ID $id\n"; |
132 |
|
|
133 |
|
|
134 |
=item --list[=list_name] |
=item --list[=list_name] |
135 |
|
|
136 |
List all available lists and users on them. |
List all available lists and users on them. |
151 |
} |
} |
152 |
|
|
153 |
foreach my $list (@lists) { |
foreach my $list (@lists) { |
154 |
print $list->name," <",$list->email,">\n"; |
print $list->name,": ",$list->from_addr," <",$list->email,">\n"; |
155 |
foreach my $u ($nos->list_members( list => $list->name )) { |
foreach my $u ($nos->list_members( list => $list->name )) { |
156 |
print "\t",$u->{'name'}, " <", $u->{'email'}, ">\n"; |
print "\t",$u->{'name'}, " <", $u->{'email'}, ">",( $u->{'ext_id'} ? ' ['.$u->{'ext_id'}.']' : '' ),"\n"; |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
170 |
|
|
171 |
} elsif ($list_name = $opt->{'add'}) { |
} elsif ($list_name = $opt->{'add'}) { |
172 |
|
|
173 |
my $list = $lists->find_or_create({ |
my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; |
|
name => $list_name, |
|
|
}) || die "can't add list $list_name\n"; |
|
174 |
|
|
175 |
my $added = 0; |
my $added = 0; |
176 |
|
|
184 |
print "list ",$list->name," has $added users\n"; |
print "list ",$list->name," has $added users\n"; |
185 |
|
|
186 |
|
|
187 |
|
=item --delete=list_name |
188 |
|
|
189 |
|
Delete users from list. User e-mails can be stored in file (which can be |
190 |
|
supplied as argument) or read from C<STDIN>. |
191 |
|
|
192 |
|
=cut |
193 |
|
} elsif ($list_name = $opt->{'delete'}) { |
194 |
|
|
195 |
|
my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; |
196 |
|
|
197 |
|
my $deleted = 0; |
198 |
|
|
199 |
|
while(<>) { |
200 |
|
chomp; |
201 |
|
next if (/^#/ || /^\s*$/); |
202 |
|
my $email = $_; |
203 |
|
$deleted++ if ($nos->delete_member_from_list( email => $email, list => $list_name )); |
204 |
|
} |
205 |
|
|
206 |
|
print "list ",$list->name," lost $deleted users\n"; |
207 |
|
|
208 |
|
|
209 |
=item --queue[=list_name] |
=item --queue[=list_name] |
210 |
|
|
211 |
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 |
263 |
Optional argument C<--driver=smtp> forces sending using SMTP server at |
Optional argument C<--driver=smtp> forces sending using SMTP server at |
264 |
localhost (127.0.0.1). |
localhost (127.0.0.1). |
265 |
|
|
266 |
|
Optional argument C<--sleep=42> defines that sender will sleep 42 seconds |
267 |
|
between sending e-mail. |
268 |
|
|
269 |
=cut |
=cut |
270 |
|
|
271 |
} elsif (defined($list_name = $opt->{'send'})) { |
} elsif (defined($list_name = $opt->{'send'})) { |
272 |
|
|
273 |
$nos->send_queued_messages($list_name, $opt->{'email_send_driver'}); |
unless ($opt->{'email_send_driver'}) { |
274 |
|
print "WARNING: this will dump debugging output to STDERR\n"; |
275 |
|
print "enter alternative driver (e.g. smtp): "; |
276 |
|
my $d = <STDIN>; |
277 |
|
chomp($d); |
278 |
|
$opt->{'email_send_driver'} = $d; |
279 |
|
} |
280 |
|
|
281 |
|
$nos->send_queued_messages( |
282 |
|
list => $list_name, |
283 |
|
driver => $opt->{'email_send_driver'}, |
284 |
|
sleep => $opt->{'sleep'}, |
285 |
|
); |
286 |
|
|
287 |
|
|
288 |
=item --inbox=list_name |
=item --inbox=list_name |