/[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 24 - (hide annotations)
Sun May 15 22:30:54 2005 UTC (18 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 4346 byte(s)
added add_message_to_queue

1 dpavlin 1 #!/usr/bin/perl -w
2    
3     use strict;
4 dpavlin 20 use blib;
5     use Nos;
6 dpavlin 1 use Getopt::Long;
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 14 sender.pl --queue[=mylist message.txt]
17 dpavlin 8 sender.pl --send=mylist
18    
19 dpavlin 15 =head2 Command options
20 dpavlin 8
21     =over 20
22    
23 dpavlin 6 =cut
24    
25 dpavlin 12 my $debug = 0;
26 dpavlin 15 my $verbose = 0;
27 dpavlin 12 my $list_opt;
28 dpavlin 2 my $add_opt;
29 dpavlin 6 my $queue_opt;
30 dpavlin 14 my $send_opt;
31 dpavlin 18 my $email_opt;
32 dpavlin 1
33     my $result = GetOptions(
34 dpavlin 9 "list:s" => \$list_opt,
35 dpavlin 2 "add=s" => \$add_opt,
36 dpavlin 14 "queue:s" => \$queue_opt,
37     "send:s" => \$send_opt,
38 dpavlin 1 "debug" => \$debug,
39 dpavlin 15 "verbose" => \$verbose,
40 dpavlin 18 "email=s" => \$email_opt,
41 dpavlin 1 );
42    
43 dpavlin 20 my $nos = new Nos(
44     dsn => 'dbi:Pg:dbname=notices',
45     user => 'dpavlin',
46     passwd => '',
47     debug => $debug,
48     verbose => $verbose,
49 dpavlin 1 );
50    
51 dpavlin 20 my $loader = $nos->{'loader'} || die "can't find loader?";
52    
53 dpavlin 2 my $lists = $loader->find_class('lists');
54     my $users = $loader->find_class('users');
55     my $user_list = $loader->find_class('user_list');
56 dpavlin 6 my $messages = $loader->find_class('messages');
57 dpavlin 11 my $queue = $loader->find_class('queue');
58 dpavlin 15 my $sent = $loader->find_class('sent');
59 dpavlin 2
60 dpavlin 14 $queue->set_sql( list_queue => qq{
61 dpavlin 15 SELECT messages.message, messages.date AS date, lists.name AS list
62 dpavlin 14 FROM queue
63     JOIN messages on message_id = messages.id
64     JOIN lists on list_id = lists.id
65     } );
66    
67    
68 dpavlin 9 =item --list[=list_name]
69 dpavlin 8
70 dpavlin 14 List all available lists and users on them.
71 dpavlin 8
72 dpavlin 14 Optional value is name of list. With it, this option will produce just users
73     on that list.
74    
75 dpavlin 8 =cut
76    
77 dpavlin 9 if (defined($list_opt)) {
78     my @lists;
79     if ($list_opt ne '') {
80     @lists = $lists->search( name=> $list_opt )->first || die "can't find list $list_opt";
81     } else {
82     @lists = $lists->retrieve_all;
83     }
84    
85     foreach my $list (@lists) {
86 dpavlin 17 print $list->name," <",$list->email,">\n";
87 dpavlin 1 foreach my $user_on_list ($user_list->search(list_id => $list->id)) {
88     my $user = $users->retrieve( id => $user_on_list->user_id );
89     print "\t",$user->full_name," <", $user->email, ">\n";
90     }
91     }
92 dpavlin 8
93     =item --add=list_name
94    
95     Add users to list. Users are stored in file (which can be supplied as
96     argument) or read from C<STDIN>. List should be in following format:
97    
98     email@example.com Optional full name of person
99     dpavlin@rot13.org Dobrica Pavlinusic
100    
101 dpavlin 18 You may use C<--email> parametar at any time to set From: e-mail address for list.
102     B<This seems somewhat cludgy, and it will probably change in future>.
103    
104 dpavlin 8 =cut
105    
106 dpavlin 2 } elsif ($add_opt) {
107     my $list = $lists->find_or_create({
108     name => $add_opt,
109     }) || die "can't add list $add_opt\n";
110 dpavlin 21
111 dpavlin 18 if ($email_opt && $list->email ne $email_opt) {
112     $list->email($email_opt);
113     $list->update;
114     $list->dbi_commit;
115     }
116 dpavlin 6
117     my $added = 0;
118    
119 dpavlin 2 while(<>) {
120     chomp;
121     next if (/^#/ || /^\s*$/);
122     my ($email, $name) = split(/\s+/,$_, 2);
123 dpavlin 23 $added++ if ($nos->add_member_to_list( email => $email, name => $name, list => $add_opt ));
124 dpavlin 2 }
125 dpavlin 3
126 dpavlin 6 print "list ",$list->name," has $added users\n";
127    
128 dpavlin 14 =item --queue[=list_name]
129 dpavlin 8
130     Queue message for later delivery. Message can be read from file (specified as
131     argument) or read from C<STDIN>.
132    
133 dpavlin 16 This option without optional parametar will display pending queue. If you
134     add C<--verbose> flag, it will display all messages in queue.
135 dpavlin 14
136 dpavlin 8 =cut
137    
138 dpavlin 14 } elsif (defined($queue_opt)) {
139 dpavlin 6
140 dpavlin 14 if ($queue_opt ne '') {
141     # add message to list queue
142    
143     my $message_text;
144     while(<>) {
145     $message_text .= $_;
146     }
147    
148 dpavlin 24 my $id = $nos->add_message_to_queue(
149     list => $queue_opt,
150     message => $message_text,
151     );
152 dpavlin 14
153 dpavlin 24 print "added message $id to list $queue_opt\n";
154 dpavlin 14
155     } else {
156     # list messages in queue
157    
158     foreach my $m ($queue->retrieve_all) {
159 dpavlin 15 next if ($m->all_sent && ! $verbose);
160    
161     my $l = $m->all_sent ? 'S' : 'Q';
162    
163     my $date = $m->message_id->date;
164     $date =~ s/\..+$//;
165     my $msg = $m->message_id->message;
166     $msg =~ s/\s+/ /gs;
167    
168     $l .= sprintf(" %-10s %15s : ", $m->list_id->name, $date);
169     $l .= substr($msg, 0, 79 - length($l));
170    
171 dpavlin 14 print "$l\n";
172     }
173    
174 dpavlin 6 }
175    
176 dpavlin 14 =item --send[=list_name]
177 dpavlin 6
178 dpavlin 16 Send e-mails waiting in queue, or with optional argument, just send messages
179     for single list.
180 dpavlin 6
181 dpavlin 14 =cut
182 dpavlin 6
183 dpavlin 14 } elsif (defined($send_opt)) {
184 dpavlin 6
185 dpavlin 22 $nos->send_queued_messages($send_opt);
186 dpavlin 6
187 dpavlin 1 } else {
188 dpavlin 8 die "see perldoc $0 for help";
189 dpavlin 1 }
190    
191 dpavlin 8 =back
192    
193 dpavlin 15
194    
195     =head2 Helper options
196    
197     =over 20
198    
199     =item --debug
200    
201     Turn on debugging output from C<Class::DBI>
202    
203     =item --verbose
204    
205     Dump more info on screen.
206    
207 dpavlin 18 =item --email
208    
209     Used to specify e-mail address where needed.
210    
211 dpavlin 15 =back
212    
213    
214    
215 dpavlin 8 =head1 AUTHOR
216    
217     Dobrica Pavlinusic <dpavlin@rot13.org>
218    
219     =cut
220    

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26