use warnings;
use App::Promulger -command;
-use Promulger::Schema;
use Promulger::List;
-
use Config::General;
sub abstract {
}
$self->{config} = { Config::General->new($cf)->getall };
- @$args == 1 or die "pmg newlist needs a list name\n";
+ $self->{config}{config_file} = $cf;
}
+
sub run {
my ($self, $opt, $args) = @_;
- Promulger::Schema->connect($self->{config}{store});
+ @$args == 1 or die "pmg newlist needs a list name\n";
my $list = Promulger::List->new(
listname => $args->[0],
);
- $list->setup_aliases_at($self->{config});
- Promulger::Schema->store($list);
+ $list->setup($self->{config});
}
1;
--- /dev/null
+package Promulger::Dispatch;
+use strict;
+use warnings;
+
+use Email::Simple;
+# XXX allow the user to specify their own Email::Sender::Transport -- apeiron,
+# 2010-03-13
+use Email::Sender::Simple qw(sendmail);
+use Mail::Verp;
+
+sub dispatch {
+ my($message, $config) = @_;
+
+ my $email = Email::Simple->new($message);
+ my $recipient = $email->header('To');
+ my $sender = $email->header('From');
+ my $subject = $email->header('Subject');
+
+ my $list = Promulger::List->resolve($recipient);
+ unless($list) {
+ reject($recipient, $sender);
+ }
+
+ if($recipient =~ /-request$/) {
+ handle_request($list, $sender, $recipient, $subject, $config);
+ }
+
+ # they don't have a request for us, so they want to post a message
+ post_message($list, $email, $config);
+}
+
+sub handle_request {
+ my ($list, $sender, $recipient, $subject, $config) = @_;
+
+ if($subject =~ /^subscribe/i) {
+ $list->subscribe($sender, $config)
+ or already_subscribed($list, $sender, $config);
+ } elsif($subject =~ /^unsubscribe/i) {
+ $list->unsubscribe($sender, $config)
+ or not_subscribed($list, $sender, $config);
+ }
+}
+
+sub post_message {
+ my($list, $email, $config) = @_;
+
+ my $sender = $email->header('From');
+ my $recipient = $email->header('To');
+
+ reject($recipient, $sender) unless $list->accept_posts_from($sender);
+ reject($recipient, $sender) unless $list->active;
+
+ # they're allowed to post (subscribed or not), the list is active. let's do
+ # this thing.
+
+ # XXX no MIME or other fancy handling for now -- apeiron, 2010-03-13
+ my $body = $email->body;
+ for my $subscriber ($list->subscribers) {
+ my $verped_from = Mail::Verp->encode($list->address, $subscriber);
+ # XXX we let the MTA create the message-id for us for now -- apeiron,
+ # 2010-03-13
+ my $new_message = Email::Simple->create(
+ header => [
+ From => $verped_from,
+ To => $subscriber,
+ Subject => $email->subject,
+ ],
+ body => $body,
+ );
+ # XXX no queuing or job distribution for now beyond what the MTA provides
+ # -- apeiron, 2010-03-13
+ sendmail($new_message);
+ }
+}
+
+sub reject {}
+sub not_subscribed {}
+sub already_subscribed {}
+
+'http://www.shadowcat.co.uk/blog/matt-s-trout/oh-subdispatch-oh-subdispatch/';
package Promulger::List;
use Moose;
+use MooseX::Storage;
+use autodie ':all';
use Carp;
-use Dir::Self;
-use File::Slurp qw/read_file write_file/;
+use Path::Class;
+use Fcntl ':flock';
has listname => (
is => 'ro',
default => 1,
);
-sub setup_aliases_at {
+with Storage (
+ format => 'JSON',
+ io => 'File',
+);
+
+sub resolve {}
+
+sub subscribe {}
+
+sub unsubscribe {}
+
+sub accept_posts_from {}
+
+sub setup {
my($self, $config) = @_;
my $name = $self->listname;
- my @current_contents = read_file $config->{aliases};
+ my $path = file($config->{list_home}, $name . ".list");
+ eval {
+ __PACKAGE__->load($path->stringify);
+ };
+ croak "${name} already a known list" unless $@;
+
+ open my $fh, '+<', $config->{aliases};
+ flock $fh, LOCK_EX;
+ my @current_contents = <$fh>;
my @aliases = ($name, "${name}-request");
for my $alias (@aliases) {
- if(grep { $_ =~ /^${alias}:/ } @current_contents) {
+ if(grep { /^${alias}:/ } @current_contents) {
croak "${alias} already in $config->{aliases}";
}
- push @current_contents, qq(${alias}: "|$config->{bin_root}/pmg msg"\n);
+ push @current_contents,
+ qq(${alias}: "|$config->{bin_root}/pmg msg -c $config->{config_file}"\n);
}
- write_file $config->{aliases}, @current_contents;
+ $self->store($path->stringify);
+ print $fh @current_contents;
+ flock $fh, LOCK_UN;
}
1;