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 |
|
|
16 |
sender.pl --queue[=mylist message.txt] |
sender.pl --queue[=mylist message.txt] |
17 |
sender.pl --send=mylist |
sender.pl --send=mylist |
18 |
|
|
19 |
|
In C</etc/aliases> something like: |
20 |
|
|
21 |
|
mylist: "| /path/to/sender.pl --inbox=mylist" |
22 |
|
|
23 |
=head2 Command options |
=head2 Command options |
24 |
|
|
25 |
=over 20 |
=over 20 |
32 |
my $add_opt; |
my $add_opt; |
33 |
my $queue_opt; |
my $queue_opt; |
34 |
my $send_opt; |
my $send_opt; |
35 |
|
my $email_opt; |
36 |
|
my $inbox_opt; |
37 |
|
|
38 |
my $result = GetOptions( |
my $result = GetOptions( |
39 |
"list:s" => \$list_opt, |
"list:s" => \$list_opt, |
40 |
"add=s" => \$add_opt, |
"add=s" => \$add_opt, |
41 |
"queue:s" => \$queue_opt, |
"queue:s" => \$queue_opt, |
42 |
"send:s" => \$send_opt, |
"send:s" => \$send_opt, |
43 |
|
"inbox=s" => \$inbox_opt, |
44 |
"debug" => \$debug, |
"debug" => \$debug, |
45 |
"verbose" => \$verbose, |
"verbose" => \$verbose, |
46 |
|
"email=s" => \$email_opt, |
47 |
); |
); |
48 |
|
|
49 |
|
my $nos = new Nos( |
50 |
my $loader = Class::DBI::Loader::Pg->new( |
dsn => 'dbi:Pg:dbname=notices', |
51 |
debug => $debug, |
user => 'dpavlin', |
52 |
dsn => "dbi:Pg:dbname=notices", |
passwd => '', |
53 |
user => "dpavlin", |
debug => $debug, |
54 |
password => "", |
verbose => $verbose, |
|
namespace => "Noticer", |
|
|
# additional_classes => qw/Class::DBI::AbstractSearch/, |
|
|
# additional_base_classes => qw/My::Stuff/, |
|
|
relationships => 1, |
|
55 |
); |
); |
56 |
|
|
57 |
|
my $loader = $nos->{'loader'} || die "can't find loader?"; |
58 |
|
|
59 |
my $lists = $loader->find_class('lists'); |
my $lists = $loader->find_class('lists'); |
60 |
my $users = $loader->find_class('users'); |
my $users = $loader->find_class('users'); |
61 |
my $user_list = $loader->find_class('user_list'); |
my $user_list = $loader->find_class('user_list'); |
89 |
} |
} |
90 |
|
|
91 |
foreach my $list (@lists) { |
foreach my $list (@lists) { |
92 |
print $list->name,"\n"; |
print $list->name," <",$list->email,">\n"; |
93 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
94 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
95 |
print "\t",$user->full_name," <", $user->email, ">\n"; |
print "\t",$user->full_name," <", $user->email, ">\n"; |
104 |
email@example.com Optional full name of person |
email@example.com Optional full name of person |
105 |
dpavlin@rot13.org Dobrica Pavlinusic |
dpavlin@rot13.org Dobrica Pavlinusic |
106 |
|
|
107 |
|
You may use C<--email> parametar at any time to set From: e-mail address for list. |
108 |
|
B<This seems somewhat cludgy, and it will probably change in future>. |
109 |
|
|
110 |
=cut |
=cut |
111 |
|
|
112 |
} elsif ($add_opt) { |
} elsif ($add_opt) { |
|
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
|
113 |
my $list = $lists->find_or_create({ |
my $list = $lists->find_or_create({ |
114 |
name => $add_opt, |
name => $add_opt, |
115 |
}) || die "can't add list $add_opt\n"; |
}) || die "can't add list $add_opt\n"; |
116 |
|
|
117 |
|
if ($email_opt && $list->email ne $email_opt) { |
118 |
|
$list->email($email_opt); |
119 |
|
$list->update; |
120 |
|
$list->dbi_commit; |
121 |
|
} |
122 |
|
|
123 |
my $added = 0; |
my $added = 0; |
124 |
|
|
125 |
while(<>) { |
while(<>) { |
126 |
chomp; |
chomp; |
127 |
next if (/^#/ || /^\s*$/); |
next if (/^#/ || /^\s*$/); |
128 |
my ($email, $name) = split(/\s+/,$_, 2); |
my ($email, $name) = split(/\s+/,$_, 2); |
129 |
$name ||= ''; |
$added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $add_opt )); |
|
if (! Email::Valid->address($email)) { |
|
|
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++; |
|
|
} |
|
|
|
|
|
foreach my $c_name ($loader->tables) { |
|
|
my $c = $loader->find_class($c_name)|| die "can't find $c_name"; |
|
|
$c->dbi_commit(); |
|
130 |
} |
} |
131 |
|
|
132 |
print "list ",$list->name," has $added users\n"; |
print "list ",$list->name," has $added users\n"; |
136 |
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 |
137 |
argument) or read from C<STDIN>. |
argument) or read from C<STDIN>. |
138 |
|
|
139 |
This options without optional parametars it will display current queue. |
This option without optional parametar will display pending queue. If you |
140 |
|
add C<--verbose> flag, it will display all messages in queue. |
141 |
|
|
142 |
=cut |
=cut |
143 |
|
|
146 |
if ($queue_opt ne '') { |
if ($queue_opt ne '') { |
147 |
# add message to list queue |
# add message to list queue |
148 |
|
|
|
my $this_list = $lists->search( |
|
|
name => $queue_opt, |
|
|
)->first || die "can't find list $queue_opt"; |
|
|
|
|
149 |
my $message_text; |
my $message_text; |
150 |
while(<>) { |
while(<>) { |
151 |
$message_text .= $_; |
$message_text .= $_; |
152 |
} |
} |
153 |
|
|
154 |
die "no message" unless ($message_text); |
my $id = $nos->add_message_to_list( |
155 |
|
list => $queue_opt, |
156 |
my $this_message = $messages->find_or_create({ |
message => $message_text, |
157 |
message => $message_text |
); |
|
}) || die "can't insert message"; |
|
158 |
|
|
159 |
$this_message->dbi_commit() || die "can't add message"; |
print "added message $id to list $queue_opt\n"; |
|
|
|
|
$queue->find_or_create({ |
|
|
message_id => $this_message->id, |
|
|
list_id => $this_list->id, |
|
|
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
|
|
|
|
|
$queue->dbi_commit || die "can't add message to list ",$this_list->name; |
|
|
|
|
|
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
|
160 |
|
|
161 |
} else { |
} else { |
162 |
# list messages in queue |
# list messages in queue |
181 |
|
|
182 |
=item --send[=list_name] |
=item --send[=list_name] |
183 |
|
|
184 |
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 |
185 |
just single list. |
for single list. |
186 |
|
|
187 |
=cut |
=cut |
188 |
|
|
189 |
} elsif (defined($send_opt)) { |
} elsif (defined($send_opt)) { |
190 |
|
|
191 |
my $my_q; |
$nos->send_queued_messages($send_opt); |
|
if ($send_opt ne '') { |
|
|
my $l_id = $lists->search_like( name => $send_opt )->first || |
|
|
die "can't find list $send_opt"; |
|
|
$my_q = $queue->search_like( list_id => $l_id ) || |
|
|
die "can't find list $send_opt"; |
|
|
} else { |
|
|
$my_q = $queue->retrieve_all; |
|
|
} |
|
192 |
|
|
193 |
while (my $m = $my_q->next) { |
=item --inbox=list_name |
|
next if ($m->all_sent); |
|
194 |
|
|
195 |
print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; |
Feed incomming message back into notice sender. |
|
my $msg = $m->message_id->message; |
|
196 |
|
|
197 |
foreach my $u ($user_list->search(list_id => $m->list_id)) { |
=cut |
198 |
|
|
199 |
my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n"; |
} elsif ($inbox_opt) { |
200 |
|
|
201 |
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
warn "inbox option is not implemented"; |
|
print "SKIP ",$u->user_id->email," message allready sent\n"; |
|
|
} else { |
|
|
print "\t",$u->user_id->email,"\n"; |
|
|
send IO => "$hdr\n$msg"; |
|
|
$sent->create({ |
|
|
message_id => $m->message_id, |
|
|
user_id => $u->user_id, |
|
|
}); |
|
|
$sent->dbi_commit; |
|
|
} |
|
|
} |
|
|
$m->all_sent(1); |
|
|
$m->update; |
|
|
$m->dbi_commit; |
|
|
} |
|
202 |
|
|
203 |
} else { |
} else { |
204 |
die "see perldoc $0 for help"; |
die "see perldoc $0 for help"; |
220 |
|
|
221 |
Dump more info on screen. |
Dump more info on screen. |
222 |
|
|
223 |
|
=item --email |
224 |
|
|
225 |
|
Used to specify e-mail address where needed. |
226 |
|
|
227 |
=back |
=back |
228 |
|
|
229 |
|
|