/[notice-sender]/trunk/sender.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/sender.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Fri May 13 21:17:58 2005 UTC revision 8 by dpavlin, Sat May 14 13:12:00 2005 UTC
# Line 5  use Class::DBI::Loader::Pg; Line 5  use Class::DBI::Loader::Pg;
5  use Getopt::Long;  use Getopt::Long;
6  use Data::Dumper;  use Data::Dumper;
7    
8  my ($lists,$debug) = (0,0);  =head1 NAME
9    
10    sender.pl - command line notify sender utility
11    
12    =head1 SYNOPSYS
13    
14     sender.pl --add=mylist members.txt
15     sender.pl --list
16     sender.pl --queue=mylist message.txt
17     sender.pl --send=mylist
18    
19    =head2 All options
20    
21    =over 20
22    
23    =item --debug
24    
25    Turn on debugging output from C<Class::DBI>
26    
27    =cut
28    
29    my ($list_opt,$debug) = (0,0);
30    my $add_opt;
31    my $queue_opt;
32    
33  my $result = GetOptions(  my $result = GetOptions(
34          "lists" => \$lists,          "list"  => \$list_opt,
35            "add=s" => \$add_opt,
36            "queue=s" => \$queue_opt,
37          "debug" => \$debug,          "debug" => \$debug,
38  );  );
39    
# Line 21  my $loader = Class::DBI::Loader::Pg->new Line 46  my $loader = Class::DBI::Loader::Pg->new
46          namespace       => "Noticer",          namespace       => "Noticer",
47  #       additional_classes      => qw/Class::DBI::AbstractSearch/,  #       additional_classes      => qw/Class::DBI::AbstractSearch/,
48  #       additional_base_classes => qw/My::Stuff/,  #       additional_base_classes => qw/My::Stuff/,
49          relationships   => 1          relationships   => 1,
50  );  );
51    
52  if ($lists) {  my $lists = $loader->find_class('lists');
53          my $lists = $loader->find_class('lists');  my $users = $loader->find_class('users');
54          my $users = $loader->find_class('users');  my $user_list = $loader->find_class('user_list');
55          my $user_list = $loader->find_class('user_list');  my $messages = $loader->find_class('messages');
56    my $message_list = $loader->find_class('message_list');
57    
58    =item --list
59    
60    List all available lists and users on them
61    
62    =cut
63    
64    if ($list_opt) {
65          foreach my $list ($lists->retrieve_all) {          foreach my $list ($lists->retrieve_all) {
66                  print $list->name,"\n";                  print $list->name,"\n";
67                  foreach my $user_on_list ($user_list->search(list_id => $list->id)) {                  foreach my $user_on_list ($user_list->search(list_id => $list->id)) {
# Line 35  if ($lists) { Line 69  if ($lists) {
69                          print "\t",$user->full_name," <", $user->email, ">\n";                          print "\t",$user->full_name," <", $user->email, ">\n";
70                  }                  }
71          }          }
72    
73    =item --add=list_name
74    
75    Add users to list. Users are stored in file (which can be supplied as
76    argument) or read from C<STDIN>. List should be in following format:
77    
78     email@example.com      Optional full name of person
79     dpavlin@rot13.org      Dobrica Pavlinusic
80    
81    =cut
82    
83    } elsif ($add_opt) {
84            #my $noticer = $loader->find_class('Noticer') || die "can't find my class!";
85            my $list = $lists->find_or_create({
86                    name => $add_opt,
87            }) || die "can't add list $add_opt\n";
88    
89            my $added = 0;
90    
91            while(<>) {
92                    chomp;
93                    next if (/^#/ || /^\s*$/);
94                    my ($email, $name) = split(/\s+/,$_, 2);
95                    print "# $name <$email>\n";
96                    my $this_user = $users->find_or_create({
97                            email => $email,
98                            full_name => $name,
99                    }) || die "can't find or create member\n";
100                    my $user_on_list = $user_list->find_or_create({
101                            user_id => $this_user->id,
102                            list_id => $list->id,
103                    }) || die "can't add user to list";
104                    $added++;
105            }
106    
107            foreach my $c_name ($loader->tables) {
108                    my $c = $loader->find_class($c_name)|| die "can't find $c_name";
109                    $c->dbi_commit();
110            }
111    
112            print "list ",$list->name," has $added users\n";
113    
114    =item --queue=list_name
115    
116    Queue message for later delivery. Message can be read from file (specified as
117    argument) or read from C<STDIN>.
118    
119    =cut
120    
121    } elsif ($queue_opt) {
122            my $this_list = $lists->search(
123                    name => $queue_opt,
124            )->first || die "can't find list $queue_opt";
125    
126            my $message_text;
127            while(<>) {
128                    $message_text .= $_;
129            }
130    
131            die "no message" unless ($message_text);
132    
133            my $this_message = $messages->find_or_create({
134                    message => $message_text
135            }) || die "can't insert message";
136    
137            $this_message->dbi_commit();
138    
139            $message_list->find_or_create({
140                    message_id => $this_message->id,
141                    list_id => $this_list->id,
142            }) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name;
143    
144            print "added message ",$this_message->id, " to list ",$this_list->name,"\n";
145    
146  } else  {  } else  {
147          die "$0: unknown command";          die "see perldoc $0 for help";
148  }  }
149    
150    =back
151    
152    =head1 AUTHOR
153    
154    Dobrica Pavlinusic <dpavlin@rot13.org>
155    
156    =cut
157    

Legend:
Removed from v.1  
changed lines
  Added in v.8

  ViewVC Help
Powered by ViewVC 1.1.26