3cef09e60663a28f9cf1e8cf3d5aa6f82ac0381e
[scpubgit/App-EzPz.git] / lib / Email / EzPz / ListCore.pm
1 package Email::EzPz::ListCore;
2
3 use Moo::Role;
4 use IO::All;
5 use Capture::Tiny qw(capture_merged);
6 use IPC::System::Simple qw(run capture);
7
8 has list_dir => (is => 'ro', required => 1);
9
10 has ezmlm_bindir => (is => 'ro', required => 1);
11
12 requires 'sublist_type';
13
14 sub _command_path {
15   my ($self, $command) = @_;
16   if (ref(my $bindir = $self->ezmlm_bindir) eq 'ARRAY') { # prefix list
17     return (@{$bindir}, "ezmlm-${command}");
18   } else {
19     return io->dir($bindir)->catfile("ezmlm-${command}");
20   }
21 }
22
23 sub _command_args {
24   my ($self, $command, @args) = @_;
25   return (
26     $self->_command_path($command),
27     $self->list_dir,
28     $self->sublist_type,
29     @args
30   );
31 }
32
33 sub _call_command {
34   my ($self, @cmd) = @_;
35   my $ok;
36   my $out = capture_merged {
37     $ok = eval { run $self->_command_args(@cmd); 1 };
38   };
39   die "Command failed: $out\n" unless $ok;
40   return;
41 }
42
43 sub _capture_command {
44   my ($self, @cmd) = @_;
45   map { chomp; $_ } capture $self->_command_args(@cmd);
46 }
47
48 sub add_member {
49   my ($self, $member) = @_;
50   $self->_call_command(sub => $member);
51   return $member;
52 }
53
54 sub remove_member {
55   my ($self, $member) = @_;
56   $self->_call_command(unsub => $member);
57   return $member;
58 }
59
60 sub members {
61   my ($self) = @_;
62   $self->_capture_command('list');
63 }
64
65 1;