standardize on strictures+autodie+Test::Most where applicable
[p5sagit/Promulger.git] / lib / Promulger / List.pm
CommitLineData
514dce63 1package Promulger::List;
243baf4d 2use Moo;
2a007b5a 3use autodie;
514dce63 4
f5baca29 5use autodie ':all';
514dce63 6use Carp;
f5baca29 7use Path::Class;
8use Fcntl ':flock';
9bfd7b60 9use Tie::File;
0456162d 10use File::Slurp qw/read_file write_file/;
243baf4d 11use Data::Dumper;
0456162d 12use Try::Tiny;
1beca6d0 13use FindBin qw($Bin);
243baf4d 14$Data::Dumper::Purity = 1;
514dce63 15
21baaef0 16use Promulger::Config;
17
514dce63 18has listname => (
19 is => 'ro',
243baf4d 20 isa => sub { $_[0] =~ /^\w+$/ or die "listname must be a string" },
514dce63 21 required => 1,
22);
23
24has active => (
25 is => 'rw',
243baf4d 26 isa => sub { ($_[0] == 0 || $_[0] == 1) or die "active must be 0 or 1" },
514dce63 27 required => 1,
243baf4d 28 default => sub { 1 },
514dce63 29);
30
21baaef0 31has subscribers => (
32 is => 'rw',
243baf4d 33 isa => sub { ref $_[0] eq 'HASH' or die "subscribers must be a hashref" },
21baaef0 34 required => 1,
35 default => sub { {} },
36);
37
21baaef0 38sub resolve {
9bfd7b60 39 my ($self, $proto) = @_;
21baaef0 40 $proto =~ s/-request$//;
41 my $path = find_path_for($proto);
42 my $maybe_list;
0456162d 43 try {
21baaef0 44 $maybe_list = __PACKAGE__->load($path->stringify);
0456162d 45 } catch {
46 die "oh noes: $_";
21baaef0 47 };
48 return $maybe_list;
49}
f5baca29 50
21baaef0 51sub subscribe {
9bfd7b60 52 my ($self, $new) = @_;
8ad89cb2 53 return if $self->subscribers->{$new};
54 $self->subscribers->{$new} = 1;
55 $self->store(find_path_for($self->listname)->stringify);
56 return 1;
21baaef0 57}
f5baca29 58
21baaef0 59sub unsubscribe {
9bfd7b60 60 my ($self, $ex) = @_;
8ad89cb2 61 return unless exists $self->subscribers->{$ex};
62 delete $self->subscribers->{$ex};
63 $self->store(find_path_for($self->listname)->stringify);
64 return 1;
21baaef0 65}
f5baca29 66
21baaef0 67# XXX implement ACLs and other shinies -- apeiron, 2010-03-13
68sub accept_posts_from {
9bfd7b60 69 my ($self, $sender) = @_;
8ad89cb2 70 return grep { $sender eq $_ } keys %{$self->subscribers};
21baaef0 71}
f5baca29 72
73sub setup {
9bfd7b60 74 my ($self) = @_;
21baaef0 75 my $config = Promulger::Config->config;
514dce63 76 my $name = $self->listname;
9bfd7b60 77 croak "${name} already a known list" if $self->resolve($name);
21baaef0 78 my $path = find_path_for($name);
f5baca29 79
7636373d 80 my $tie = tie my @aliases, 'Tie::File', $config->{aliases}
81 or die "cannot tie " . $config->{aliases} . ": $!";
9bfd7b60 82 $tie->flock;
83 my @list_aliases = ($name, "${name}-request");
21baaef0 84
9bfd7b60 85 for my $list_alias (@list_aliases) {
86 if(grep { /^${list_alias}:/ } @aliases) {
87 croak "${list_alias} already in $config->{aliases}";
514dce63 88 }
9bfd7b60 89 push @aliases,
1beca6d0 90 qq(${list_alias}: "|$Bin msg -c $config->{config_file}"\n);
514dce63 91 }
21baaef0 92
f5baca29 93 $self->store($path->stringify);
9bfd7b60 94}
95
96sub delete {
97 my ($self) = @_;
98 my $config = Promulger::Config->config;
99 my $name = $self->listname;
100
101 my $tie = tie my @aliases, 'Tie::File', $config->{aliases};
102 $tie->flock;
103
104 my @list_aliases = ($name, "${name}-request");
105 @aliases = grep {
106 $_ !~ /^$list_aliases[0]:/ &&
107 $_ !~ /^$list_aliases[1]:/
108 } @aliases;
109
8ad89cb2 110 unlink find_path_for($self->listname)->stringify;
514dce63 111}
112
21baaef0 113sub find_path_for {
114 my ($proto) = @_;
115 my $path = file(Promulger::Config->config->{list_home}, $proto . ".list");
9bfd7b60 116 return $path;
21baaef0 117}
118
243baf4d 119sub store {
120 my ($self, $path) = @_;
121 my $dumped = 'do { my '. Dumper($self) . '; $VAR1; }';
122 write_file($path, $dumped);
123}
124
125sub load {
126 my ($class, $path) = @_;
127 return do $path;
128}
129
0456162d 130sub get_lists {
131 my ($self) = @_;
132 my $config = Promulger::Config->config;
133 my @lists = map { $_->basename}
134 grep { -f } dir($config->{list_home})->children;
135 s/\.list//g for @lists;
136 return @lists;
137}
138
21baaef0 139'http://mitpress.mit.edu/sicp/';