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 Email::Valid; |
|
|
use Email::Send; |
|
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
9 |
|
|
40 |
"email=s" => \$email_opt, |
"email=s" => \$email_opt, |
41 |
); |
); |
42 |
|
|
43 |
|
my $nos = new Nos( |
44 |
my $loader = Class::DBI::Loader::Pg->new( |
dsn => 'dbi:Pg:dbname=notices', |
45 |
debug => $debug, |
user => 'dpavlin', |
46 |
dsn => "dbi:Pg:dbname=notices", |
passwd => '', |
47 |
user => "dpavlin", |
debug => $debug, |
48 |
password => "", |
verbose => $verbose, |
|
namespace => "Noticer", |
|
|
# additional_classes => qw/Class::DBI::AbstractSearch/, |
|
|
# additional_base_classes => qw/My::Stuff/, |
|
|
relationships => 1, |
|
49 |
); |
); |
50 |
|
|
51 |
|
my $loader = $nos->{'loader'} || die "can't find loader?"; |
52 |
|
|
53 |
my $lists = $loader->find_class('lists'); |
my $lists = $loader->find_class('lists'); |
54 |
my $users = $loader->find_class('users'); |
my $users = $loader->find_class('users'); |
55 |
my $user_list = $loader->find_class('user_list'); |
my $user_list = $loader->find_class('user_list'); |
104 |
=cut |
=cut |
105 |
|
|
106 |
} elsif ($add_opt) { |
} elsif ($add_opt) { |
|
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
|
107 |
my $list = $lists->find_or_create({ |
my $list = $lists->find_or_create({ |
108 |
name => $add_opt, |
name => $add_opt, |
109 |
}) || die "can't add list $add_opt\n"; |
}) || die "can't add list $add_opt\n"; |
110 |
|
|
111 |
if ($email_opt && $list->email ne $email_opt) { |
if ($email_opt && $list->email ne $email_opt) { |
112 |
$list->email($email_opt); |
$list->email($email_opt); |
113 |
$list->update; |
$list->update; |
120 |
chomp; |
chomp; |
121 |
next if (/^#/ || /^\s*$/); |
next if (/^#/ || /^\s*$/); |
122 |
my ($email, $name) = split(/\s+/,$_, 2); |
my ($email, $name) = split(/\s+/,$_, 2); |
123 |
$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(); |
|
124 |
} |
} |
125 |
|
|
126 |
print "list ",$list->name," has $added users\n"; |
print "list ",$list->name," has $added users\n"; |
196 |
|
|
197 |
} elsif (defined($send_opt)) { |
} elsif (defined($send_opt)) { |
198 |
|
|
199 |
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; |
|
|
} |
|
|
|
|
|
while (my $m = $my_q->next) { |
|
|
next if ($m->all_sent); |
|
|
|
|
|
print "sending message ",$m->message_id," enqueued on ",$m->date," to list ",$m->list_id->name,"\n"; |
|
|
my $msg = $m->message_id->message; |
|
|
|
|
|
foreach my $u ($user_list->search(list_id => $m->list_id)) { |
|
|
|
|
|
if ($sent->search( message_id => $m->message_id, user_id => $u->user_id )) { |
|
|
print "SKIP ",$u->user_id->email," message allready sent\n"; |
|
|
} else { |
|
|
print "\t",$u->user_id->email,"\n"; |
|
|
|
|
|
my $hdr = "From: " . $u->list_id->name . " <" . $u->list_id->email . ">\n" . |
|
|
"To: " . $u->user_id->full_name . " <". $u->user_id->email. ">\n"; |
|
|
|
|
|
# FIXME do real sending :-) |
|
|
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; |
|
|
} |
|
200 |
|
|
201 |
} else { |
} else { |
202 |
die "see perldoc $0 for help"; |
die "see perldoc $0 for help"; |