Just about ready to go live
[p5sagit/Promulger.git] / lib / Promulger / List.pm
CommitLineData
514dce63 1package Promulger::List;
2use Moose;
f5baca29 3use MooseX::Storage;
514dce63 4
f5baca29 5use autodie ':all';
514dce63 6use Carp;
f5baca29 7use Path::Class;
8use Fcntl ':flock';
514dce63 9
21baaef0 10use Promulger::Config;
11
514dce63 12has listname => (
13 is => 'ro',
14 isa => 'Str',
15 required => 1,
16);
17
18has active => (
19 is => 'rw',
20 isa => 'Bool',
21 required => 1,
22 default => 1,
23);
24
21baaef0 25has subscribers => (
26 is => 'rw',
27 isa => 'HashRef',
28 required => 1,
29 default => sub { {} },
30);
31
f5baca29 32with Storage (
33 format => 'JSON',
34 io => 'File',
35);
36
21baaef0 37sub resolve {
38 my($proto) = @_;
39 $proto =~ s/-request$//;
40 my $path = find_path_for($proto);
41 my $maybe_list;
42 eval {
43 $maybe_list = __PACKAGE__->load($path->stringify);
44 };
45 return $maybe_list;
46}
f5baca29 47
21baaef0 48sub subscribe {
49 my($self, $new) = @_;
50 return if $self->subscribers->at($new);
51 $self->subscribers->put($new, 1);
52 $self->store(find_path_for($self->name));
53}
f5baca29 54
21baaef0 55sub unsubscribe {
56 my($self, $ex) = @_;
57 return unless $self->subscribers->at($ex);
58 $self->subscribers->delete($ex);
59 $self->store(find_path_for($self->name));
60}
f5baca29 61
21baaef0 62# XXX implement ACLs and other shinies -- apeiron, 2010-03-13
63sub accept_posts_from {
64 my($self, $sender) = @_;
65 return grep { $sender eq $_ } @{$self->subscribers};
66}
f5baca29 67
68sub setup {
21baaef0 69 my($self) = @_;
70 my $config = Promulger::Config->config;
514dce63 71 my $name = $self->listname;
21baaef0 72 croak "${name} already a known list" if resolve($name);
73 my $path = find_path_for($name);
f5baca29 74
75 open my $fh, '+<', $config->{aliases};
76 flock $fh, LOCK_EX;
77 my @current_contents = <$fh>;
514dce63 78 my @aliases = ($name, "${name}-request");
21baaef0 79
514dce63 80 for my $alias (@aliases) {
f5baca29 81 if(grep { /^${alias}:/ } @current_contents) {
514dce63 82 croak "${alias} already in $config->{aliases}";
83 }
f5baca29 84 push @current_contents,
85 qq(${alias}: "|$config->{bin_root}/pmg msg -c $config->{config_file}"\n);
514dce63 86 }
21baaef0 87
f5baca29 88 $self->store($path->stringify);
89 print $fh @current_contents;
90 flock $fh, LOCK_UN;
514dce63 91}
92
21baaef0 93sub find_path_for {
94 my ($proto) = @_;
95 my $path = file(Promulger::Config->config->{list_home}, $proto . ".list");
96}
97
98'http://mitpress.mit.edu/sicp/';