about summary refs log tree commit homepage
path: root/lib/PublicInbox/InboxWritable.pm
blob: 0a976ea2dc3dfe6278747d54a88a2ef8ba19dcad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (C) 2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# Extends read-only Inbox for writing
package PublicInbox::InboxWritable;
use strict;
use warnings;
use base qw(PublicInbox::Inbox);
use PublicInbox::Import;

sub new {
	my ($class, $ibx) = @_;
	bless $ibx, $class;
}

sub importer {
	my ($self, $parallel) = @_;
	$self->{-importer} ||= eval {
		my $v = $self->{version} || 1;
		if ($v == 2) {
			eval { require PublicInbox::V2Writable };
			die "v2 not supported: $@\n" if $@;
			my $v2w = PublicInbox::V2Writable->new($self);
			$v2w->{parallel} = $parallel;
			$v2w;
		} elsif ($v == 1) {
			my $git = $self->git;
			my $name = $self->{name};
			my $addr = $self->{-primary_address};
			PublicInbox::Import->new($git, $name, $addr, $self);
		} else {
			die "unsupported inbox version: $v\n";
		}
	}
}

sub filter {
	my ($self) = @_;
	my $f = $self->{filter};
	if ($f && $f =~ /::/) {
		my @args = (-inbox => $self);
		# basic line splitting, only
		# Perhaps we can have proper quote splitting one day...
		($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;

		eval "require $f";
		if ($@) {
			warn $@;
		} else {
			# e.g: PublicInbox::Filter::Vger->new(@args)
			return $f->new(@args);
		}
	}
	undef;
}

1;