1 |
dpavlin |
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 |
dpavlin |
6 |
=head1 NAME |
9 |
|
|
|
10 |
|
|
sender.pl - command line notify sender utility |
11 |
|
|
|
12 |
|
|
=cut |
13 |
|
|
|
14 |
dpavlin |
2 |
my ($list_opt,$debug) = (0,0); |
15 |
|
|
my $add_opt; |
16 |
dpavlin |
6 |
my $queue_opt; |
17 |
dpavlin |
1 |
|
18 |
|
|
my $result = GetOptions( |
19 |
dpavlin |
2 |
"list" => \$list_opt, |
20 |
|
|
"add=s" => \$add_opt, |
21 |
dpavlin |
6 |
"queue=s" => \$queue_opt, |
22 |
dpavlin |
1 |
"debug" => \$debug, |
23 |
|
|
); |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
my $loader = Class::DBI::Loader::Pg->new( |
27 |
|
|
debug => $debug, |
28 |
|
|
dsn => "dbi:Pg:dbname=notices", |
29 |
|
|
user => "dpavlin", |
30 |
|
|
password => "", |
31 |
|
|
namespace => "Noticer", |
32 |
|
|
# additional_classes => qw/Class::DBI::AbstractSearch/, |
33 |
|
|
# additional_base_classes => qw/My::Stuff/, |
34 |
dpavlin |
3 |
relationships => 1, |
35 |
dpavlin |
1 |
); |
36 |
|
|
|
37 |
dpavlin |
2 |
my $lists = $loader->find_class('lists'); |
38 |
|
|
my $users = $loader->find_class('users'); |
39 |
|
|
my $user_list = $loader->find_class('user_list'); |
40 |
dpavlin |
6 |
my $messages = $loader->find_class('messages'); |
41 |
|
|
my $message_list = $loader->find_class('message_list'); |
42 |
dpavlin |
2 |
|
43 |
|
|
if ($list_opt) { |
44 |
dpavlin |
1 |
foreach my $list ($lists->retrieve_all) { |
45 |
|
|
print $list->name,"\n"; |
46 |
|
|
foreach my $user_on_list ($user_list->search(list_id => $list->id)) { |
47 |
|
|
my $user = $users->retrieve( id => $user_on_list->user_id ); |
48 |
|
|
print "\t",$user->full_name," <", $user->email, ">\n"; |
49 |
|
|
} |
50 |
|
|
} |
51 |
dpavlin |
2 |
} elsif ($add_opt) { |
52 |
|
|
#my $noticer = $loader->find_class('Noticer') || die "can't find my class!"; |
53 |
|
|
my $list = $lists->find_or_create({ |
54 |
|
|
name => $add_opt, |
55 |
|
|
}) || die "can't add list $add_opt\n"; |
56 |
dpavlin |
6 |
|
57 |
|
|
my $added = 0; |
58 |
|
|
|
59 |
dpavlin |
2 |
while(<>) { |
60 |
|
|
chomp; |
61 |
|
|
next if (/^#/ || /^\s*$/); |
62 |
|
|
my ($email, $name) = split(/\s+/,$_, 2); |
63 |
|
|
print "# $name <$email>\n"; |
64 |
|
|
my $this_user = $users->find_or_create({ |
65 |
|
|
email => $email, |
66 |
|
|
full_name => $name, |
67 |
|
|
}) || die "can't find or create member\n"; |
68 |
|
|
my $user_on_list = $user_list->find_or_create({ |
69 |
|
|
user_id => $this_user->id, |
70 |
|
|
list_id => $list->id, |
71 |
|
|
}) || die "can't add user to list"; |
72 |
dpavlin |
6 |
$added++; |
73 |
dpavlin |
2 |
} |
74 |
dpavlin |
3 |
|
75 |
|
|
foreach my $c_name ($loader->tables) { |
76 |
|
|
my $c = $loader->find_class($c_name)|| die "can't find $c_name"; |
77 |
|
|
$c->dbi_commit(); |
78 |
|
|
} |
79 |
|
|
|
80 |
dpavlin |
6 |
print "list ",$list->name," has $added users\n"; |
81 |
|
|
|
82 |
|
|
} elsif ($queue_opt) { |
83 |
|
|
my $this_list = $lists->search( |
84 |
|
|
name => $queue_opt, |
85 |
|
|
)->first || die "can't find list $queue_opt"; |
86 |
|
|
|
87 |
|
|
my $message_text; |
88 |
|
|
while(<>) { |
89 |
|
|
$message_text .= $_; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
die "no message" unless ($message_text); |
93 |
|
|
|
94 |
|
|
my $this_message = $messages->find_or_create({ |
95 |
|
|
message => $message_text |
96 |
|
|
}) || die "can't insert message"; |
97 |
|
|
|
98 |
|
|
$this_message->dbi_commit(); |
99 |
|
|
|
100 |
|
|
$message_list->find_or_create({ |
101 |
|
|
message_id => $this_message->id, |
102 |
|
|
list_id => $this_list->id, |
103 |
|
|
}) || die "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
104 |
|
|
|
105 |
|
|
print "added message ",$this_message->id, " to list ",$this_list->name,"\n"; |
106 |
|
|
|
107 |
dpavlin |
1 |
} else { |
108 |
dpavlin |
6 |
die $0.' |
109 |
|
|
--list show all lists and users |
110 |
|
|
--add=name_of_list < users.txt add users (email@example.com full name) |
111 |
|
|
--queue=name_of_list < message queue message for sending to list |
112 |
|
|
--debug |
113 |
|
|
'; |
114 |
dpavlin |
1 |
} |
115 |
|
|
|