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