--- trunk/sender.pl 2005/05/13 22:08:44 2 +++ trunk/sender.pl 2005/05/14 18:20:50 11 @@ -5,12 +5,35 @@ use Getopt::Long; use Data::Dumper; +=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 All options + +=over 20 + +=item --debug + +Turn on debugging output from C + +=cut + my ($list_opt,$debug) = (0,0); my $add_opt; +my $queue_opt; my $result = GetOptions( - "list" => \$list_opt, + "list:s" => \$list_opt, "add=s" => \$add_opt, + "queue=s" => \$queue_opt, "debug" => \$debug, ); @@ -23,32 +46,56 @@ 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'); + +=item --list[=list_name] + +List all available lists and users on them. Optional value is name of list +and it will produce users just on that list. -if ($list_opt) { - foreach my $list ($lists->retrieve_all) { +=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,"\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 + +=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"; + my $added = 0; + while(<>) { chomp; next if (/^#/ || /^\s*$/); @@ -62,10 +109,57 @@ 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(); + } + + 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. + +=cut + +} elsif ($queue_opt) { + my $this_list = $lists->search( + name => $queue_opt, + )->first || die "can't find list $queue_opt"; + + my $message_text; + while(<>) { + $message_text .= $_; } - print "processed $added members\n"; + + 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(); + + $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; + + print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; + } else { - die "$0 --lists --list-add=name_of_list --debug\n"; + die "see perldoc $0 for help"; } +=back + +=head1 AUTHOR + +Dobrica Pavlinusic + +=cut