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