/[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 29 - (hide annotations)
Mon May 16 20:58:44 2005 UTC (18 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 4617 byte(s)
attempt at validating queued mail messages, sending with unique hash,
documentation for inbox option (but not implementation),
renamed add_message_to_queue to add_message_to_list

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

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26