]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
declare Inbox object for reusability
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
6 use strict;
7 use warnings;
8 use Scalar::Util qw(weaken);
9 use PublicInbox::Git;
10
11 sub new {
12         my ($class, $opts) = @_;
13         bless $opts, $class;
14 }
15
16 sub weaken_all {
17         my ($self) = @_;
18         weaken($self->{$_}) foreach qw(git mm search);
19 }
20
21 sub git {
22         my ($self) = @_;
23         $self->{git} ||= eval { PublicInbox::Git->new($self->{mainrepo}) };
24 }
25
26 sub mm {
27         my ($self) = @_;
28         $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{mainrepo}) };
29 }
30
31 sub search {
32         my ($self) = @_;
33         $self->{search} ||= eval { PublicInbox::Search->new($self->{mainrepo}) };
34 }
35
36 sub try_cat {
37         my ($path) = @_;
38         my $rv = '';
39         if (open(my $fh, '<', $path)) {
40                 local $/;
41                 $rv = <$fh>;
42         }
43         $rv;
44 }
45
46 sub description {
47         my ($self) = @_;
48         my $desc = $self->{description};
49         return $desc if defined $desc;
50         $desc = try_cat("$self->{mainrepo}/description");
51         chomp $desc;
52         $desc =~ s/\s+/ /smg;
53         $desc = '($GIT_DIR/description missing)' if $desc eq '';
54         $self->{description} = $desc;
55 }
56
57 sub cloneurl {
58         my ($self) = @_;
59         my $url = $self->{cloneurl};
60         return $url if $url;
61         $url = try_cat("$self->{mainrepo}/cloneurl");
62         my @url = split(/\s+/s, $url);
63         chomp @url;
64         $self->{cloneurl} = \@url;
65 }
66
67 sub footer_html {
68         my ($self) = @_;
69         my $footer = $self->{footer};
70         return $footer if defined $footer;
71         $footer = try_cat("$self->{mainrepo}/public-inbox/footer.html");
72         chomp $footer;
73         $self->{footer} = $footer;
74 }
75
76 1;