/[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

Annotation of /trunk/sender.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (hide annotations)
Sat May 14 13:21:35 2005 UTC (18 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 3701 byte(s)
list just members of one list as option

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

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26