--- trunk/sender.pl 2005/05/13 21:17:58 1 +++ trunk/sender.pl 2005/08/22 20:39:38 73 @@ -1,42 +1,343 @@ #!/usr/bin/perl -w use strict; -use Class::DBI::Loader::Pg; +use blib; +use Nos 0.7; use Getopt::Long; -use Data::Dumper; +use Pod::Usage; -my ($lists,$debug) = (0,0); +=head1 NAME + +sender.pl - command line notify sender utility + +=head1 SYNOPSIS + + sender.pl --create=mylist + sender.pl --drop=mylist + sender.pl --add=mylist members.txt + sender.pl --delete=mylist members.txt + sender.pl --list[=mylist] + sender.pl --queue[=mylist message.txt] + sender.pl --send=mylist + sender.pl --help + sender.pl --man + +=head1 OPTIONS + +=over 20 + +=cut + +my $debug = 0; +my $verbose = 0; +my $opt; my $result = GetOptions( - "lists" => \$lists, + "create=s" => \$opt->{'create'}, + "drop=s" => \$opt->{'drop'}, + "list:s" => \$opt->{'list'}, + "add=s" => \$opt->{'add'}, + "delete=s" => \$opt->{'delete'}, + "queue:s" => \$opt->{'queue'}, + "send:s" => \$opt->{'send'}, + "inbox=s" => \$opt->{'inbox'}, "debug" => \$debug, -); + "verbose" => \$verbose, + "from=s" => \$opt->{'from'}, + "driver=s" => \$opt->{'email_send_driver'}, + "sleep=i" => \$opt->{'sleep'}, + "aliases=s" => \$opt->{'aliases'}, + "help" => \$opt->{'help'}, + "man" => \$opt->{'man'} +) || pod2usage(-verbose => 0); +pod2usage(-verbose => 1) if ($opt->{'help'}); +pod2usage(-verbose => 2) if ($opt->{'man'}); -my $loader = Class::DBI::Loader::Pg->new( - debug => $debug, - dsn => "dbi:Pg:dbname=notices", - user => "dpavlin", - password => "", - namespace => "Noticer", -# additional_classes => qw/Class::DBI::AbstractSearch/, -# additional_base_classes => qw/My::Stuff/, - relationships => 1 +my $nos = new Nos( + dsn => 'dbi:Pg:dbname=notices', + user => 'dpavlin', + passwd => '', + debug => $debug, + verbose => $verbose, ); -if ($lists) { - my $lists = $loader->find_class('lists'); - my $users = $loader->find_class('users'); - my $user_list = $loader->find_class('user_list'); - foreach my $list ($lists->retrieve_all) { - print $list->name,"\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"; +my $loader = $nos->{'loader'} || die "can't find loader?"; + +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 +} ); + +my $list_name; + +=item --aliases=/full/path/to/aliases + +Optional parametar C<--aliases> can be used to specify aliases file other +than default C. + +=cut + +my $aliases = $opt->{'aliases'} || '/etc/aliases'; + + +=item --create=list_name my-list@example.com + +Adds new list. You can also feed list name as first line to C. + +You can also add C<--from='Full name of list'> to specify full name (comment) +in outgoing e-mail. + +=cut + +if ($list_name = $opt->{'create'}) { + + my $email = shift @ARGV || <>; + chomp($email); + + die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); + + my $id = $nos->create_list( + list => $list_name, + from => ($opt->{'from'} || ''), + email => $email, + aliases => $aliases, + ) || die "can't add list $list_name\n"; + + print "added list $list_name with ID $id\n"; + + +=item --drop=list_name + +Remove list. + +Optional parametar C<--aliases='/full/path/to/aliases'> can be used to +specify aliases file other than C. + +=cut + +} elsif ($list_name = $opt->{'drop'}) { + + my $id = $nos->drop_list( + list => $list_name, + aliases => $aliases, + ) || die "can't remove list $list_name\n"; + + print "drop list $list_name with ID $id\n"; + + +=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. + +=cut + +} elsif (defined($list_name = $opt->{'list'})) { + + my @lists; + + if ($list_name ne '') { + @lists = $lists->search( name=> $list_name )->first || die "can't find list $list_name"; + } else { + @lists = $lists->retrieve_all; + } + + foreach my $list (@lists) { + print $list->name,": ",$list->from_addr," <",$list->email,">\n"; + foreach my $u ($nos->list_members( list => $list->name )) { + print "\t",$u->{'name'}, " <", $u->{'email'}, ">",( $u->{'ext_id'} ? ' ['.$u->{'ext_id'}.']' : '' ),"\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 + +=cut + +} elsif ($list_name = $opt->{'add'}) { + + my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; + + my $added = 0; + + while(<>) { + chomp; + next if (/^#/ || /^\s*$/); + my ($email, $name) = split(/\s+/,$_, 2); + $added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $list_name )); + } + + print "list ",$list->name," has $added users\n"; + + +=item --delete=list_name + +Delete users from list. User e-mails can be stored in file (which can be +supplied as argument) or read from C. + +=cut +} elsif ($list_name = $opt->{'delete'}) { + + my $list = $nos->_get_list($list_name) || die "can't find list $list_name\n"; + + my $deleted = 0; + + while(<>) { + chomp; + next if (/^#/ || /^\s*$/); + my $email = $_; + $deleted++ if ($nos->delete_member_from_list( email => $email, list => $list_name )); + } + + print "list ",$list->name," lost $deleted 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($list_name = $opt->{'queue'})) { + + if ($list_name ne '') { + # add message to list queue + + my $message_text; + while(<>) { + $message_text .= $_; + } + + my $id = $nos->add_message_to_list( + list => $list_name, + message => $message_text, + ) || die "can't add message to list $list_name\n"; + + print "added message $id to list $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(" %-15s %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. + +Optional argument C<--driver=smtp> forces sending using SMTP server at +localhost (127.0.0.1). + +Optional argument C<--sleep=42> defines that sender will sleep 42 seconds +between sending e-mail. + +=cut + +} elsif (defined($list_name = $opt->{'send'})) { + + unless ($opt->{'email_send_driver'}) { + print "WARNING: this will dump debugging output to STDERR\n"; + print "enter alternative driver (e.g. smtp): "; + my $d = ; + chomp($d); + $opt->{'email_send_driver'} = $d; + } + + $nos->send_queued_messages( + list => $list_name, + driver => $opt->{'email_send_driver'}, + sleep => $opt->{'sleep'}, + ); + + +=item --inbox=list_name + +Feed incomming message back into notice sender. + +=cut + +} elsif ($list_name = $opt->{'inbox'}) { + + my $message; + while(<>) { + $message .= $_; + } + + $nos->inbox_message( + list => $list_name, + message => $message, + ) || die "can't receive message for list $list_name"; + + } else { - die "$0: unknown command"; + pod2usage(-verbose=>0); } +=back + + + +=head2 Helper options + +=over 20 + +=item --debug + +Turn on debugging output from C + +=item --verbose + +Dump more info on screen. + +=back + +=head1 DESCRIPTION + +This command will use notice-sender C module directly to make modifications on lists +or with C<--inbox> option server as incomming mail filter. + +=head1 AUTHOR + +Dobrica Pavlinusic + +=cut