]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
inbox: cleanup and consolidate object weakening
[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 isweak);
9 use PublicInbox::Git;
10 use PublicInbox::MID qw(mid2path);
11
12 my $weakt;
13 eval {
14         $weakt = 'disabled';
15         require PublicInbox::EvCleanup;
16         $weakt = undef; # OK if we get here
17 };
18
19 my $WEAKEN = {}; # string(inbox) -> inbox
20 sub weaken_task () {
21         $weakt = undef;
22         _weaken_fields($_) for values %$WEAKEN;
23         $WEAKEN = {};
24 }
25
26 sub _weaken_later ($) {
27         my ($self) = @_;
28         $weakt ||= PublicInbox::EvCleanup::later(*weaken_task);
29         $WEAKEN->{"$self"} = $self;
30 }
31
32 sub new {
33         my ($class, $opts) = @_;
34         my $v = $opts->{address} ||= 'public-inbox@example.com';
35         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
36         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
37         bless $opts, $class;
38 }
39
40 sub _weaken_fields {
41         my ($self) = @_;
42         foreach my $f (qw(git mm search)) {
43                 isweak($self->{$f}) or weaken($self->{$f});
44         }
45 }
46
47 sub git {
48         my ($self) = @_;
49         $self->{git} ||= eval {
50                 _weaken_later($self);
51                 PublicInbox::Git->new($self->{mainrepo});
52         };
53 }
54
55 sub mm {
56         my ($self) = @_;
57         $self->{mm} ||= eval {
58                 _weaken_later($self);
59                 PublicInbox::Msgmap->new($self->{mainrepo});
60         };
61 }
62
63 sub search {
64         my ($self) = @_;
65         $self->{search} ||= eval {
66                 _weaken_later($self);
67                 PublicInbox::Search->new($self->{mainrepo});
68         };
69 }
70
71 sub try_cat {
72         my ($path) = @_;
73         my $rv = '';
74         if (open(my $fh, '<', $path)) {
75                 local $/;
76                 $rv = <$fh>;
77         }
78         $rv;
79 }
80
81 sub description {
82         my ($self) = @_;
83         my $desc = $self->{description};
84         return $desc if defined $desc;
85         $desc = try_cat("$self->{mainrepo}/description");
86         chomp $desc;
87         $desc =~ s/\s+/ /smg;
88         $desc = '($GIT_DIR/description missing)' if $desc eq '';
89         $self->{description} = $desc;
90 }
91
92 sub cloneurl {
93         my ($self) = @_;
94         my $url = $self->{cloneurl};
95         return $url if $url;
96         $url = try_cat("$self->{mainrepo}/cloneurl");
97         my @url = split(/\s+/s, $url);
98         chomp @url;
99         $self->{cloneurl} = \@url;
100 }
101
102 sub base_url {
103         my ($self, $env) = @_;
104         if ($env) { # PSGI env
105                 my $scheme = $env->{'psgi.url_scheme'};
106                 my $host_port = $env->{HTTP_HOST} ||
107                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
108                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
109                 # for mount in Plack::Builder
110                 $url .= '/' if $url !~ m!/\z!;
111                 $url .= $self->{name} . '/';
112         } else {
113                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
114                 $self->{-base_url} ||= do {
115                         my $url = $self->{url} or return undef;
116                         # expand protocol-relative URLs to HTTPS if we're
117                         # not inside a web server
118                         $url = "https:$url" if $url =~ m!\A//!;
119                         $url .= '/' if $url !~ m!/\z!;
120                         $url;
121                 };
122         }
123 }
124
125 sub nntp_usable {
126         my ($self) = @_;
127         my $ret = $self->mm && $self->search;
128         $self->{mm} = $self->{search} = undef;
129         $ret;
130 }
131
132 sub msg_by_path ($$;$) {
133         my ($self, $path, $ref) = @_;
134         # TODO: allow other refs:
135         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
136         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
137         $str;
138 }
139
140 sub path_check {
141         my ($self, $path) = @_;
142         git($self)->check('HEAD:'.$path);
143 }
144
145 sub msg_by_mid ($$;$) {
146         my ($self, $mid, $ref) = @_;
147         msg_by_path($self, mid2path($mid), $ref);
148 }
149
150 1;