--- trunk/sender.pl 2005/05/13 22:08:44 2 +++ trunk/sender.pl 2005/05/15 17:01:19 18 @@ -3,15 +3,42 @@ use strict; use Class::DBI::Loader::Pg; use Getopt::Long; -use Data::Dumper; +use Email::Valid; +use Email::Send; -my ($list_opt,$debug) = (0,0); +=head1 NAME + +sender.pl - command line notify sender utility + +=head1 SYNOPSYS + + sender.pl --add=mylist members.txt + sender.pl --list[=mylist] + sender.pl --queue[=mylist message.txt] + sender.pl --send=mylist + +=head2 Command options + +=over 20 + +=cut + +my $debug = 0; +my $verbose = 0; +my $list_opt; my $add_opt; +my $queue_opt; +my $send_opt; +my $email_opt; my $result = GetOptions( - "list" => \$list_opt, + "list:s" => \$list_opt, "add=s" => \$add_opt, + "queue:s" => \$queue_opt, + "send:s" => \$send_opt, "debug" => \$debug, + "verbose" => \$verbose, + "email=s" => \$email_opt, ); @@ -23,36 +50,84 @@ namespace => "Noticer", # additional_classes => qw/Class::DBI::AbstractSearch/, # additional_base_classes => qw/My::Stuff/, - relationships => 1 + relationships => 1, ); my $lists = $loader->find_class('lists'); my $users = $loader->find_class('users'); my $user_list = $loader->find_class('user_list'); +my $messages = $loader->find_class('messages'); +my $queue = $loader->find_class('queue'); +my $sent = $loader->find_class('sent'); + +$queue->set_sql( list_queue => qq{ + SELECT messages.message, messages.date AS date, lists.name AS list + FROM queue + JOIN messages on message_id = messages.id + JOIN lists on list_id = lists.id +} ); + + +=item --list[=list_name] + +List all available lists and users on them. + +Optional value is name of list. With it, this option will produce just users +on that list. -if ($list_opt) { - foreach my $list ($lists->retrieve_all) { - print $list->name,"\n"; +=cut + +if (defined($list_opt)) { + my @lists; + if ($list_opt ne '') { + @lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt"; + } else { + @lists = $lists->retrieve_all; + } + + foreach my $list (@lists) { + print $list->name," <",$list->email,">\n"; foreach my $user_on_list ($user_list->search(list_id => $list->id)) { my $user = $users->retrieve( id => $user_on_list->user_id ); print "\t",$user->full_name," <", $user->email, ">\n"; } } + +=item --add=list_name + +Add users to list. Users are stored in file (which can be supplied as +argument) or read from C. List should be in following format: + + email@example.com Optional full name of person + dpavlin@rot13.org Dobrica Pavlinusic + +You may use C<--email> parametar at any time to set From: e-mail address for list. +B. + +=cut + } elsif ($add_opt) { #my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; - foreach my $c_name ($loader->tables) { - my $c = $loader->find_class($c_name)|| die "can't find $c_name"; - $c->autoupdate(1); - } - my $list = $lists->find_or_create({ name => $add_opt, }) || die "can't add list $add_opt\n"; + if ($email_opt && $list->email ne $email_opt) { + $list->email($email_opt); + $list->update; + $list->dbi_commit; + } + my $added = 0; + while(<>) { chomp; next if (/^#/ || /^\s*$/); my ($email, $name) = split(/\s+/,$_, 2); + $name ||= ''; + if (! Email::Valid->address($email)) { + print "SKIPPING $name <$email>\n"; + next; + } print "# $name <$email>\n"; my $this_user = $users->find_or_create({ email => $email, @@ -62,10 +137,158 @@ user_id => $this_user->id, list_id => $list->id, }) || die "can't add user to list"; + $added++; } - print "processed $added members\n"; + + foreach my $c_name ($loader->tables) { + my $c = $loader->find_class($c_name)|| die "can't find $c_name"; + $c->dbi_commit(); + } + + print "list ",$list->name," has $added users\n"; + +=item --queue[=list_name] + +Queue message for later delivery. Message can be read from file (specified as +argument) or read from C. + +This option without optional parametar will display pending queue. If you +add C<--verbose> flag, it will display all messages in queue. + +=cut + +} elsif (defined($queue_opt)) { + + if ($queue_opt ne '') { + # add message to list queue + + my $this_list = $lists->search( + name => $queue_opt, + )->first || die "can't find list $queue_opt"; + + my $message_text; + while(<>) { + $message_text .= $_; + } + + die "no message" unless ($message_text); + + my $this_message = $messages->find_or_create({ + message => $message_text + }) || die "can't insert message"; + + $this_message->dbi_commit() || die "can't add message"; + + $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"; + + } else { + # list messages in queue + + foreach my $m ($queue->retrieve_all) { + next if ($m->all_sent && ! $verbose); + + my $l = $m->all_sent ? 'S' : 'Q'; + + my $date = $m->message_id->date; + $date =~ s/\..+$//; + my $msg = $m->message_id->message; + $msg =~ s/\s+/ /gs; + + $l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); + $l .= substr($msg, 0, 79 - length($l)); + + print "$l\n"; + } + + } + +=item --send[=list_name] + +Send e-mails waiting in queue, or with optional argument, just send messages +for single list. + +=cut + +} elsif (defined($send_opt)) { + + my $my_q; + 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)) { + + my $hdr = "To: ".$u->user_id->full_name." <". $u->user_id->email. ">\n"; + + 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"; + + # 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; + } + } else { - die "$0 --lists --list-add=name_of_list --debug\n"; + die "see perldoc $0 for help"; } +=back + + + +=head2 Helper options + +=over 20 + +=item --debug + +Turn on debugging output from C + +=item --verbose + +Dump more info on screen. + +=item --email + +Used to specify e-mail address where needed. + +=back + + + +=head1 AUTHOR + +Dobrica Pavlinusic + +=cut