]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxWritable.pm
introduce InboxWritable class
[public-inbox.git] / lib / PublicInbox / InboxWritable.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Extends read-only Inbox for writing
5 package PublicInbox::InboxWritable;
6 use strict;
7 use warnings;
8 use base qw(PublicInbox::Inbox);
9 use PublicInbox::Import;
10
11 sub new {
12         my ($class, $ibx) = @_;
13         bless $ibx, $class;
14 }
15
16 sub importer {
17         my ($self, $parallel) = @_;
18         $self->{-importer} ||= eval {
19                 my $v = $self->{version} || 1;
20                 if ($v == 2) {
21                         eval { require PublicInbox::V2Writable };
22                         die "v2 not supported: $@\n" if $@;
23                         my $v2w = PublicInbox::V2Writable->new($self);
24                         $v2w->{parallel} = $parallel;
25                         $v2w;
26                 } elsif ($v == 1) {
27                         my $git = $self->git;
28                         my $name = $self->{name};
29                         my $addr = $self->{-primary_address};
30                         PublicInbox::Import->new($git, $name, $addr, $self);
31                 } else {
32                         die "unsupported inbox version: $v\n";
33                 }
34         }
35 }
36
37 sub filter {
38         my ($self) = @_;
39         my $f = $self->{filter};
40         if ($f && $f =~ /::/) {
41                 my @args = (-inbox => $self);
42                 # basic line splitting, only
43                 # Perhaps we can have proper quote splitting one day...
44                 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
45
46                 eval "require $f";
47                 if ($@) {
48                         warn $@;
49                 } else {
50                         # e.g: PublicInbox::Filter::Vger->new(@args)
51                         return $f->new(@args);
52                 }
53         }
54         undef;
55 }
56
57 1;