--- trunk/sender.pl 2005/05/15 22:30:54 24 +++ trunk/sender.pl 2005/05/24 16:44:34 49 @@ -11,11 +11,16 @@ =head1 SYNOPSYS + sender.pl --new=mylist sender.pl --add=mylist members.txt sender.pl --list[=mylist] sender.pl --queue[=mylist message.txt] sender.pl --send=mylist +In C something like: + + mylist: "| cd /path/to && ./sender.pl --inbox=mylist" + =head2 Command options =over 20 @@ -24,20 +29,20 @@ my $debug = 0; my $verbose = 0; -my $list_opt; -my $add_opt; -my $queue_opt; -my $send_opt; -my $email_opt; +my $opt; my $result = GetOptions( - "list:s" => \$list_opt, - "add=s" => \$add_opt, - "queue:s" => \$queue_opt, - "send:s" => \$send_opt, + "new=s" => \$opt->{'new'}, + "list:s" => \$opt->{'list'}, + "add=s" => \$opt->{'add'}, + "queue:s" => \$opt->{'queue'}, + "send:s" => \$opt->{'send'}, + "inbox=s" => \$opt->{'inbox'}, "debug" => \$debug, "verbose" => \$verbose, - "email=s" => \$email_opt, + "from=s" => \$opt->{'from'}, + "driver=s" => \$opt->{'email_send_driver'}, + "sleep=i" => \$opt->{'sleep'}, ); my $nos = new Nos( @@ -64,6 +69,33 @@ JOIN lists on list_id = lists.id } ); +my $list_name; + + +=item --new=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->{'new'}) { + + my $email = shift @ARGV || <>; + chomp($email); + + die "need e-mail address for list (as argument or on STDIN)\n" unless ($email); + + my $id = $nos->new_list( + list => $list_name, + from => ($opt->{'from'} || ''), + email => $email, + ) || die "can't add list $list_name\n"; + + print "added list $list_name with ID $id\n"; + =item --list[=list_name] @@ -74,22 +106,24 @@ =cut -if (defined($list_opt)) { +} elsif (defined($list_name = $opt->{'list'})) { + my @lists; - if ($list_opt ne '') { - @lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt"; + + 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->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"; + foreach my $u ($nos->list_members( list => $list->name )) { + print "\t",$u->{'name'}, " <", $u->{'email'}, ">\n"; } } + =item --add=list_name Add users to list. Users are stored in file (which can be supplied as @@ -98,21 +132,13 @@ 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 $list = $lists->find_or_create({ - name => $add_opt, - }) || die "can't add list $add_opt\n"; +} elsif ($list_name = $opt->{'add'}) { - if ($email_opt && $list->email ne $email_opt) { - $list->email($email_opt); - $list->update; - $list->dbi_commit; - } + my $list = $lists->find_or_create({ + name => $list_name, + }) || die "can't add list $list_name\n"; my $added = 0; @@ -120,11 +146,12 @@ chomp; next if (/^#/ || /^\s*$/); my ($email, $name) = split(/\s+/,$_, 2); - $added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $add_opt )); + $added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $list_name )); } 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 @@ -135,9 +162,9 @@ =cut -} elsif (defined($queue_opt)) { +} elsif (defined($list_name = $opt->{'queue'})) { - if ($queue_opt ne '') { + if ($list_name ne '') { # add message to list queue my $message_text; @@ -145,12 +172,12 @@ $message_text .= $_; } - my $id = $nos->add_message_to_queue( - list => $queue_opt, + 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 $queue_opt\n"; + print "added message $id to list $list_name\n"; } else { # list messages in queue @@ -165,7 +192,7 @@ my $msg = $m->message_id->message; $msg =~ s/\s+/ /gs; - $l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date); + $l .= sprintf(" %-15s %15s : ", $m->list_id->name, $date); $l .= substr($msg, 0, 79 - length($l)); print "$l\n"; @@ -173,19 +200,50 @@ } + =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($send_opt)) { +} elsif (defined($list_name = $opt->{'send'})) { + + $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"; - $nos->send_queued_messages($send_opt); } else { - die "see perldoc $0 for help"; + die "see perldoc $0 for help\n"; } =back @@ -204,10 +262,6 @@ Dump more info on screen. -=item --email - -Used to specify e-mail address where needed. - =back