X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FInbox.pm;h=ada713c741657ef16522d4b118cffebcbd95c4be;hb=5422a844b7384c32b3532d128e15e0b50d24435b;hp=c07aaa9f7056509c16008ea1e301ba74e647a7d3;hpb=b8a44a8bf2356c1fc474b119b713d7e7df365a4a;p=public-inbox.git diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm index c07aaa9f..ada713c7 100644 --- a/lib/PublicInbox/Inbox.pm +++ b/lib/PublicInbox/Inbox.pm @@ -5,17 +5,23 @@ package PublicInbox::Inbox; use strict; use warnings; -use Scalar::Util qw(weaken); +use Scalar::Util qw(weaken isweak); use PublicInbox::Git; +use PublicInbox::MID qw(mid2path); sub new { my ($class, $opts) = @_; + my $v = $opts->{address} ||= 'public-inbox@example.com'; + my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v; + $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost'; bless $opts, $class; } sub weaken_all { my ($self) = @_; - weaken($self->{$_}) foreach qw(git mm search); + foreach my $f (qw(git mm search)) { + isweak($self->{$f}) or weaken($self->{$f}); + } } sub git { @@ -73,7 +79,7 @@ sub base_url { } else { # either called from a non-PSGI environment (e.g. NNTP/POP3) $self->{-base_url} ||= do { - my $url = $self->{url}; + my $url = $self->{url} or return undef; # expand protocol-relative URLs to HTTPS if we're # not inside a web server $url = "https:$url" if $url =~ m!\A//!; @@ -83,4 +89,24 @@ sub base_url { } } +sub nntp_usable { + my ($self) = @_; + my $ret = $self->mm && $self->search; + $self->{mm} = $self->{search} = undef; + $ret; +} + +sub msg_by_path ($$;$) { + my ($self, $path, $ref) = @_; + # TODO: allow other refs: + my $str = git($self)->cat_file('HEAD:'.$path, $ref); + $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str; + $str; +} + +sub msg_by_mid ($$;$) { + my ($self, $mid, $ref) = @_; + msg_by_path($self, mid2path($mid), $ref); +} + 1;