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 |
=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( |
34 |
"list" => \$list_opt, |
35 |
"add=s" => \$add_opt, |
36 |
"queue=s" => \$queue_opt, |
37 |
"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 |
relationships => 1, |
50 |
); |
51 |
|
52 |
my $lists = $loader->find_class('lists'); |
53 |
my $users = $loader->find_class('users'); |
54 |
my $user_list = $loader->find_class('user_list'); |
55 |
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) { |
66 |
print $list->name,"\n"; |
67 |
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
68 |
my $user = $users->retrieve( id => $user_on_list->user_id ); |
69 |
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 { |
147 |
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 |
|