--- trunk/sender.pl 2005/05/13 21:17:58 1 +++ trunk/sender.pl 2005/05/14 12:31:27 6 @@ -5,10 +5,20 @@ use Getopt::Long; use Data::Dumper; -my ($lists,$debug) = (0,0); +=head1 NAME + +sender.pl - command line notify sender utility + +=cut + +my ($list_opt,$debug) = (0,0); +my $add_opt; +my $queue_opt; my $result = GetOptions( - "lists" => \$lists, + "list" => \$list_opt, + "add=s" => \$add_opt, + "queue=s" => \$queue_opt, "debug" => \$debug, ); @@ -21,13 +31,16 @@ namespace => "Noticer", # additional_classes => qw/Class::DBI::AbstractSearch/, # additional_base_classes => qw/My::Stuff/, - relationships => 1 + relationships => 1, ); -if ($lists) { - my $lists = $loader->find_class('lists'); - my $users = $loader->find_class('users'); - my $user_list = $loader->find_class('user_list'); +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 $message_list = $loader->find_class('message_list'); + +if ($list_opt) { foreach my $list ($lists->retrieve_all) { print $list->name,"\n"; foreach my $user_on_list ($user_list->search(list_id => $list->id)) { @@ -35,8 +48,68 @@ print "\t",$user->full_name," <", $user->email, ">\n"; } } +} elsif ($add_opt) { + #my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; + 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*$/); + my ($email, $name) = split(/\s+/,$_, 2); + 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(); + } + + print "list ",$list->name," has $added users\n"; + +} 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 .= $_; + } + + 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(); + + $message_list->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: unknown command"; + die $0.' + --list show all lists and users + --add=name_of_list < users.txt add users (email@example.com full name) + --queue=name_of_list < message queue message for sending to list + --debug +'; } -