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