script/news

filter: Statementstmt 90.2% 286 / 317 Branchbran 50.0% 61 / 122 Conditioncond 41.3% 38 / 92 MC/DCmcdc 6.2% 3 / 48 Subroutinesub 93.7% 30 / 32 Podpod n/a totaltotal 68.4% 418 / 611 CC 111 SCAR 31.4
CC
111
Cov
73%
__ANON__
67.5
__ANON__
31.1
__ANON__
19.0
CRAP 23.1
↑ Summary blib/lib/App/news.pm »
1 #! /usr/bin/env perl
2 # Copyright (C) 2023–2024 Alex Schroeder <alex@gnu.org>
3  
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
7 # later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Affero General Public License along
15 # with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 news - a web front-end to a local news server
22  
23 =head1 SYNOPSIS
24  
25 B<news>
26  
27 =head1 DESCRIPTION
28  
29 C<news> connects to the local news server via NNTP on port 119 and offers a
30 web interface for it.
31  
32 There are a number of views available:
33  
34 =over
35  
36 =item * the list of newsgroups available ("server view");
37  
38 =item * the list of articles available in a particular newsgroup ("group view");
39  
40 =item * a list of articles with a particular tag in a newsgroup ("tag view");
41  
42 =item * an article ("article view");
43  
44 =item * a reply;
45  
46 =item * a new post.
47  
48 =back
49  
50 When showing From fields, the value is stripped of things that look like email
51 addresses in angled brackets such as <alex@gnu.org> or in double quotes such as
52 "alex@gnu.org"; if an email address is followed by a real name in parenthesis
53 such as alex@gnu.org (Alex Schroeder), the address and the parenthesis are
54 stripped. If no full name is provided, "Anonymous" is used.
55  
56 In the article view, email addresses in angled brackets such as <alex@gnu.org>
57 or in double quotes such as "alex@gnu.org" are also stripped. Other things that
58 might look like email addresses are not stripped.
59  
60 =head2 Threading
61  
62 Technically, articles only have references back in time. In order to show links
63 to replies, the article view relies on a cache of the group view. If the group
64 view isn't in the cache, replies cannot be shown.
65  
66 =head2 Caching
67  
68 All the NNTP requests are cached for 5min. The cache relies on L<Mojo::Cache>.
69 That cache only holds 100 items by default, so on a busy server, NNTP requests
70 might get cached for less time. The cache isn't written to disk, so if you're a
71 developer, you can restart the server to empty the cache instead of waiting for
72 5min.
73  
74 =head2 Tags
75  
76 When an article's subject contains a string in square brackets C<[like this]>,
77 then this is treated as a tag. Click on the tag to see the tag view containing
78 articles with the same tag, irrespective of threading.
79  
80 =head2 Authentication
81  
82 When posting or replying, the username and password provided by the user are
83 passed along to the news server. If that allows the user to post, it works.
84  
85 =head2 Environment variables
86  
87 The news server is determined by L<Net::NNTP>: If no host is passed then two
88 environment variables are checked C<NNTPSERVER> then C<NEWSHOST>, then
89 L<Net::Config> is checked, and if a host is not found then C<news> is used.
90  
91 C<NEWS_INTRO_ID> can be set to a message id for a "start here" message. By
92 default, no such link is shown. This must be a message-id and cannot be a
93 message number (that would require a group, too).
94  
95 C<NEWS_MODE> can be set to "NOAUTH" in order to hide username and password on
96 the post form in case your newsserver isn't public and requires no
97 authorisation; if set to "NOPOST" then posting links are hidden.
98  
99 C<NEWS_GROUPS> can be set to a comma-separated list of patterns in the WILDMAT
100 format. The details are in RFC 3977. Usually it means: names separated by
101 commas, prefixed by C<!> if negated and C<*> used as a wildcard. Support for
102 this varies. The C<sn> server only accepts a single pattern, no negation. You
103 might have to experiment.
104  
105 =head2 Systemd
106  
107 To install as a service, use a C<news.service> file like the following:
108  
109 [Unit]
110 Description=News (a web front-end)
111 After=network-online.target
112 Wants=network-online.target
113 [Install]
114 WantedBy=multi-user.target
115 [Service]
116 Type=simple
117 DynamicUser=true
118 Restart=always
119 MemoryHigh=80M
120 MemoryMax=100M
121 Environment="NNTPSERVER=localhost"
122 Environment="NEWS_INTRO_ID=<u4d0i0$n72d$1@sibirocobombus.campaignwiki>"
123 ExecStart=/home/alex/perl5/perlbrew/perls/perl-5.32.0/bin/perl /home/alex/perl5/perlbrew/perls/perl-5.32.0/bin/news daemon
124  
125 =head2 Cookies
126  
127 The web app stores name, username and password in an encrypted cookie which
128 expires one week after posting an article.
129  
130 =head2 Caching
131  
132 The web app caches all the data it gets from the news server in a cache, using
133 L<Mojo::Cache>. By default, this cache is small (100 items). Each cached item is
134 cached with a timestamp and cache hits are only used if they aren't older than
135 5min.
136  
137 =head2 Superseding
138  
139 The web app allows superseding. It's up to the newsserver to allow or deny this.
140 There's currently no way for the user to supply their own cancel secret.
141  
142 =head1 EXAMPLES
143  
144 A remote news server.
145  
146 NNTPSERVER=cosmic.voyage news daemon
147  
148 The remote news server but only the C<campaignwiki.*> groups, with the pattern
149 in quotes to prevent shell expansion:
150  
151 NNTPSERVER=campaignwiki.org "NEWS_GROUPS=campaignwiki.*" news daemon
152  
153 The remote news server with all the groups except any C<*.test> groups, with the
154 pattern in quotes to prevent shell expansion. The C<sn> server can't parse this
155 pattern, unfortunately.
156  
157 NNTPSERVER=campaignwiki.org "NEWS_GROUPS=*,!*.test" news daemon
158  
159 The local news server requires no authorisation.
160  
161 NNTPSERVER=localhost NEWS_MODE=NOAUTH news daemon
162  
163 The news server requires authorisation and we want to point visitors to a first
164 post. We assume that NNTPSERVER or NEWSHOST is already set.
165  
166 NEWS_INTRO_ID='<u4d0i0$n72d$1@sibirocobombus.campaignwiki>' news daemon
167  
168 As a developer, run it under C<morbo> so that we can make changes to the script.
169 Provide the path to the script. This time with regular authorisation.
170  
171 PERL5LIB=lib NNTPSERVER=localhost morbo script/news
172  
173 =head1 SEE ALSO
174  
175 The Tildeverse also runs news. L<https://news.tildeverse.org/>
176  
177 L<RFC 3977|https://www.rfc-editor.org/rfc/rfc3977>: Network News Transfer
178 Protocol (NNTP).
179  
180 L<RFC 3987|https://www.rfc-editor.org/rfc/rfc3987>: Internationalized Resource
181 Identifiers (IRIs)
182  
183 L<RFC 4643|https://www.rfc-editor.org/rfc/rfc4643>: Network News Transfer
184 Protocol (NNTP) Extension for Authentication.
185  
186 L<RFC 5536|https://www.rfc-editor.org/rfc/rfc5536>: Netnews Article Format.
187  
188 L<RFC 5537|https://www.rfc-editor.org/rfc/rfc5537>: Netnews Architecture and
189 Protocols.
190  
191 L<RFC 8315|https://www.rfc-editor.org/rfc/rfc8315>: Cancel-Locks in Netnews
192 Articles
193  
194 =head1 LICENSE
195  
196 GNU Affero General Public License
197  
198 =cut
199  
200 # corelist
201 5 ▶use Net::NNTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 5 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
203 # not core
204 5 ▶use Mojolicious::Lite; # Mojolicious
Subroutine BEGIN: called CC=1 SCAR=0.0
205 5 ▶use Mojo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
206 5 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 5 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
208 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
209 # our own
210 5 ▶use App::news qw(wrap html_unwrap ranges);
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 5 my $cache = Mojo::Cache->new;
213  
214 1 ▶get '/' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 1 shift->redirect_to('index');
216 5 };
217  
218 5 under 'news';
219  
220 1 ▶get '/' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
221 1 my $c = shift;
222 1 ▶ my $list = cached("active " . ($ENV{NEWS_GROUPS} || "*"), sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
223 1 ▶ my $nntp = Net::NNTP->new() or return 'error';
Branch: 1 branch
BranchTrueFalseTotal
my $nntp = "Net::NNTP"->new or return "error" 1 0 1
224 1 ▶ my $value = $nntp->active($ENV{NEWS_GROUPS} || "*");
Condition50%1/2
$ENV{'NEWS_GROUPS'} || '*'
l!l
01
MC/DC0%0/1
$ENV{'NEWS_GROUPS'} || '*'
$ENV{'NEWS_GROUPS'}
Truth table50%1/2
A
A= $ENV{'NEWS_GROUPS'}
Aresult
11
00
225 1 $nntp->quit;
226 1 ▶ return $value });
Condition50%1/2
$ENV{'NEWS_GROUPS'} || '*'
l!l
01
MC/DC0%0/1
$ENV{'NEWS_GROUPS'} || '*'
$ENV{'NEWS_GROUPS'}
Truth table50%1/2
A
A= $ENV{'NEWS_GROUPS'}
Aresult
11
00
227 1 ▶ return $c->render(template => 'noserver') if $list eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $list eq "error" 0 1 1
228 $c->render(template => 'index', list => $list,
229 id => $ENV{NEWS_INTRO_ID},
230 1 address => $c->tx->req->url->to_abs->host);
231 5 } => 'index';
232  
233 11 ▶sub cached {
Subroutine cached: called CC=4 SCAR=13.9
234 11 my ($key, $sub) = @_;
235 11 my $cached = $cache->get($key);
236 11 my $value;
237 11 ▶ if (defined $cached) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached) 1 10 11
238 1 my ($ts, $data) = @$cached;
239 1 my $age = time - $ts;
240 1 app->log->debug("Cache age of $key: ${age}s");
241 1 ▶ $value = $data if $age <= 5 * 60; # cached for five minutes
Branch: 1 branch
BranchTrueFalseTotal
if $age <= 300 1 0 1
242 }
243 11 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 10 1 11
244 10 app->log->debug("Getting a fresh copy of $key");
245 10 $value = $sub->();
246 10 $cache->set($key => [time, $value]);
247 }
248 11 return $value;
249 }
250  
251 5 my $per_page = 50;
252 5 my $per_search = 500;
253  
254 2 ▶get '/group/#group' => sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
255 2 my $c = shift;
256 2 my $group = $c->param('group');
257 2 my $edit = $c->param('edit');
258 2 ▶ my $page = $c->param('page') || "";
Condition50%1/2
$c->param('page') || ''
l!l
02
MC/DC0%0/1
$c->param('page') || ''
$c->param('page')
Truth table50%1/2
A
A= $c->param('page')
Aresult
11
00
259 2 my $nntp; # only created on demand
260 2 ▶ my $description = cached("$group description", sub {
Subroutine __ANON__: called CC=5 SCAR=19.4
261 2 ▶ $nntp ||= Net::NNTP->new() or return 'error';
Branch: 1 branch
BranchTrueFalseTotal
$nntp ||= "Net::NNTP"->new or return "error" 2 0 2
Condition33%1/3
$nntp ||= "Net::NNTP"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$nntp ||= "Net::NNTP"->new
$nntp "Net::NNTP"->new
Truth table33%1/3
A ||= B
A= $nntpB= "Net::NNTP"->new
ABresult
1X1
011
000
262 2 my $newsgroups = $nntp->newsgroups($group);
263 2 ▶ return $newsgroups && $newsgroups->{$group} || "" });
Condition40%2/5
$newsgroups && $newsgroups->{$group} || ''
l!l
20
$newsgroups and $newsgroups->{$group}
!ll&&!rl&&r
002
MC/DC0%0/2
$newsgroups && $newsgroups->{$group} || ''
$newsgroups $newsgroups->{$group}
Truth table33%1/3
A and B
A= $newsgroupsB= $newsgroups->{$group}
ABresult
111
0X0
100
264 2 ▶ return $c->render(template => 'noserver') if 'error' eq $description;
Branch: 1 branch
BranchTrueFalseTotal
if "error" eq $description 0 2 2
265 2 ▶ my $data = cached("$group list $page", sub {
Subroutine __ANON__: called CC=13 SCAR=27.7
266 2 ▶ $nntp ||= Net::NNTP->new() or return 'error';
Branch: 1 branch
BranchTrueFalseTotal
$nntp ||= "Net::NNTP"->new or return "error" 2 0 2
Condition33%1/3
$nntp ||= "Net::NNTP"->new
l!l&&r!l&&!r
200
MC/DC0%0/2
$nntp ||= "Net::NNTP"->new
$nntp "Net::NNTP"->new
Truth table33%1/3
A ||= B
A= $nntpB= "Net::NNTP"->new
ABresult
1X1
011
000
267 2 ▶ my ($nums, $first, $last) = $nntp->group($group) or return [];
Branch: 1 branch
BranchTrueFalseTotal
() = $nntp->group($group) or return [] 2 0 2
268 2 my $last_page = int($last / $per_page) + 1;
269 2 ▶ $page ||= $last_page;
Condition33%1/3
$page ||= $last_page
l!l&&r!l&&!r
020
MC/DC0%0/2
$page ||= $last_page
$page $last_page
Truth table33%1/3
A ||= B
A= $pageB= $last_page
ABresult
1X1
011
000
270 2 my $to = $page * $per_page;
271 2 ▶ $to = $last if $to > $last;
Branch: 1 branch
BranchTrueFalseTotal
if $to > $last 2 0 2
272 2 my $from = ($page - 1) * $per_page;
273 2 ▶ $from = $first if $from < $first;
Branch: 1 branch
BranchTrueFalseTotal
if $from < $first 2 0 2
274 2 my $fmt = $nntp->overview_fmt;
275 2 app->log->debug("Getting $group $from-$to");
276 2 my $messages = $nntp->xover("$from-$to");
277 2 my $articles = [];
278 2 my $parser = DateTime::Format::Mail->new->loose;
279 2 for my $num (sort { $b <=> $a } keys %$messages) {
280 4 my ($subject, $from, $date, $id, $references) = @{$messages->{$num}};
281 4 ▶ $subject = decode("MIME-Header", $subject) || "?";
Condition50%1/2
&decode('MIME-Header', $subject) || '?'
l!l
40
MC/DC0%0/1
&decode('MIME-Header', $subject) || '?'
&decode('MIME-Header', $subject)
Truth table50%1/2
A
A= &decode('MIME-Header', $subject)
Aresult
11
00
282 4 my ($tag) = $subject =~ /\[(.*?)\]/;
283 4 $from = no_email(decode("MIME-Header", $from));
284 4 my $dt = $parser->parse_datetime($date);
285 4 my $url = $c->url_for('article', group => $group, id => $num);
286 4 ▶ $url = $url->query(edit => $edit) if $edit;
Branch: 1 branch
BranchTrueFalseTotal
if $edit 0 4 4
287 4 push(@$articles, {
288 id => $id,
289 num => $num,
290 tag => $tag,
291 url => $url,
292 from => $from,
293 subject => $subject,
294 date => [$dt->ymd, sprintf("%02d:%02d", $dt->hour, $dt->minute)],
295 references => [split(/\s+/, decode("MIME-Header", $references))],
296 replies => [] })
297 };
298 # link replies based on references but only the articles on the same pages (!)
299 2 for my $article (@$articles) {
300 4 for my $reference (@{$article->{references}}) {
301 2 ▶ my $original = first { $reference eq $_->{id} } @$articles;
Subroutine __ANON__: called CC=1 SCAR=0.0
302 2 ▶ next unless $original;
Branch: 1 branch
BranchTrueFalseTotal
unless $original 2 0 2
303 0 push(@{$original->{replies}}, $article->{id});
304 0 app->log->debug("$article->{id} is a reply to $original->{id}");
305 }
306 }
307 return {
308 2 articles => $articles,
309 2 pagination => {page => $page, last_page => $last_page}}});
310 2 ▶ return $c->render(template => 'noserver') if 'error' eq $data;
Branch: 1 branch
BranchTrueFalseTotal
if "error" eq $data 0 2 2
311 2 ▶ $nntp->quit if $nntp;
Branch: 1 branch
BranchTrueFalseTotal
if $nntp 2 0 2
312 $c->render(template => 'group', group => $group, edit => $edit, description => $description,
313 2 list => $data->{articles}, pagination => $data->{pagination});
314 5 } => 'group';
315  
316 11 ▶sub no_email {
Subroutine no_email: called CC=2 SCAR=7.0
317 11 my $from = shift;
318 11 $from =~ s/\s*<.*>//;
319 11 $from =~ s/\s*"\S+@\S+"//;
320 11 $from =~ s/\S+@\S+\s+\((.*?)\)/$1/;
321 11 ▶ return $from || "Anonymous";
Condition50%1/2
$from || 'Anonymous'
l!l
110
MC/DC0%0/1
$from || 'Anonymous'
$from
Truth table50%1/2
A
A= $from
Aresult
11
00
322 }
323  
324 1 ▶get '/tag/#group/#tag' => sub {
Subroutine __ANON__: called CC=9 SCAR=27.0
325 1 my $c = shift;
326 1 my $group = $c->param('group');
327 1 my $edit = $c->param('edit');
328 1 my $tag = $c->param('tag');
329 # We start counting in the back… This is different from the /group list.
330 # There, we take the first and last message numbers and compute page numbers
331 # based on that. Starting at the front makes this stable. The same articles
332 # stay on the same pages. Given first and last article numbers and a search
333 # pattern, we can't do this. Therefore, we start at the present and scan into
334 # the past until we have the page we want.
335 1 ▶ my $page = $c->param('page') // 0;
Condition50%1/2
$c->param("page") // 0
l!l
01
MC/DC0%0/1
$c->param("page") // 0
$c->param("page")
Truth table50%1/2
A
A= $c->param("page")
Aresult
11
00
336 1 ▶ my $include = $c->param('include') // 0;
Condition50%1/2
$c->param("include") // 0
l!l
10
MC/DC0%0/1
$c->param("include") // 0
$c->param("include")
Truth table50%1/2
A
A= $c->param("include")
Aresult
11
00
337 1 my $nntp; # only created on demand
338 1 ▶ my $data = cached("$group tag $tag", sub {
Subroutine __ANON__: called CC=26 SCAR=42.1
339 1 ▶ $nntp ||= Net::NNTP->new() or return 'error';
Branch: 1 branch
BranchTrueFalseTotal
$nntp ||= "Net::NNTP"->new or return "error" 1 0 1
Condition33%1/3
$nntp ||= "Net::NNTP"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$nntp ||= "Net::NNTP"->new
$nntp "Net::NNTP"->new
Truth table33%1/3
A ||= B
A= $nntpB= "Net::NNTP"->new
ABresult
1X1
011
000
340 1 ▶ my ($nums, $first, $last) = $nntp->group($group) or return [];
Branch: 1 branch
BranchTrueFalseTotal
() = $nntp->group($group) or return [] 1 0 1
341 1 app->log->debug("$group has $first-$last");
342 1 my $seen = 0; # set when we have seen $include
343 1 my $to = $last;
344 1 my $from = $to - $per_search;
345 1 ▶ $from = $first if $from < $first;
Branch: 1 branch
BranchTrueFalseTotal
if $from < $first 1 0 1
346 1 my $pattern = "*\\[$tag\\]*";
347 1 $pattern =~ s/ /?/g;
348 1 my $result = $nntp->xpat("Subject", $pattern, [$from, $to]);
349 1 my @nums = sort keys %$result;
350 1 app->log->debug("Searching pattern $pattern $from-$to found " . scalar(@nums) . " articles");
351 1 ▶ $seen = grep { $_ == $include } @nums if $include;
Branch: 1 branch
BranchTrueFalseTotal
if $include 1 0 1
352 # keep checking more, if necessary
353 1 ▶ while (($page and @nums / $per_page < $page
Branch: 1 branch
BranchTrueFalseTotal
if ($page && @nums / $per_page < $page || $include && !$seen and $from > $first) 0 1 1
Condition33%4/12
$page && @nums / $per_page < $page || $include && !$seen and $from > $first
!ll&&!rl&&r
100
$page and @nums / $per_page < $page or $include and not $seen
l!l&&r!l&&!r
001
$page and @nums / $per_page < $page
!ll&&!rl&&r
100
$include and not $seen
!ll&&!rl&&r
010
MC/DC0%0/5
$page && @nums / $per_page < $page || $include && !$seen and $from > $first
$page @nums / $per_page < $page $include not $seen $from > $first
Truth table10%1/10
A and B or C and not(D) and E
A= $pageB= @nums / $per_page < $pageC= $includeD= not $seenE= $from > $first
ABCDEresult
0X0XX0
0X10X0
100XX0
1010X0
11XX00
0X1100
101100
11XX11
0X1111
101111
354 or $include and not $seen)
355 and $from > $first) {
356 0 $to -= $per_search;
357 0 $from -= $per_search;
358 0 ▶ $from = $first if $from < $first;
Branch: 1 branch
BranchTrueFalseTotal
if $from < $first 0 0 0
359 0 $result = $nntp->xpat("Subject", $pattern, [$from, $to]);
360 0 app->log->debug("Searching pattern $pattern $from-$to found " . scalar(@nums) . " articles");
361 0 ▶ $seen = grep { $_ == $include } keys %$result if $include;
Branch: 1 branch
BranchTrueFalseTotal
if $include 0 0 0
362 0 unshift(@nums, sort keys %$result);
363 }
364 # add pagination
365 1 ▶ if ($page) {
Branch: 2 branches
BranchTrueFalseTotal
if ($page) { } 0 1 1
elsif ($include) { } 1 0 1
366 0 @nums = @nums[(-$page-1) * $per_page + 1, -$page * $per_page];
367 } elsif ($include) {
368 1 my @page;
369 0 ▶ while (@nums > $per_page and not grep { $_ == $include } @page) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nums > $per_page and not grep({$_ == $include;} @page)) 0 1 1
Condition33%1/3
@nums > $per_page and not grep({$_ == $include;} @page)
!ll&&!rl&&r
100
MC/DC0%0/2
@nums > $per_page and not grep({$_ == $include;} @page)
@nums > $per_page not grep({$_ == $include;} @page)
Truth table33%1/3
A and not(B)
A= @nums > $per_pageB= not grep({$_ == $include;} @page)
ABresult
0X0
100
111
370 0 @page = splice(@nums, -$per_page);
371 0 $page++;
372 }
373 1 ▶ @nums = @page if @page;
Branch: 1 branch
BranchTrueFalseTotal
if @page 0 1 1
374 }
375 1 my $ranges = ranges(@nums);
376 1 my $fmt = $nntp->overview_fmt;
377 1 my $re = quotemeta($tag);
378 1 my $articles = [];
379 1 my $parser = DateTime::Format::Mail->new->loose;
380 1 for my $range (@$ranges) {
381 2 ▶ app->log->debug("Getting $group " . (ref $range ? join("-", @$range) : $range));
Branch: 1 branch
BranchTrueFalseTotal
ref $range ? : 1 1 2
382 2 my $messages = $nntp->xover($range);
383 2 app->log->debug("Received " . scalar(keys %$messages) . " messages");
384 2 for my $num (sort keys %$messages) {
385 3 my ($subject, $from, $date, $id, $references) = @{$messages->{$num}};
386 3 ▶ $subject = decode("MIME-Header", $subject) || "?";
Condition50%1/2
&decode('MIME-Header', $subject) || '?'
l!l
30
MC/DC0%0/1
&decode('MIME-Header', $subject) || '?'
&decode('MIME-Header', $subject)
Truth table50%1/2
A
A= &decode('MIME-Header', $subject)
Aresult
11
00
387 3 $subject =~ s/\[$re\]\s*//;
388 3 $from = no_email(decode("MIME-Header", $from));
389 3 my $dt = $parser->parse_datetime($date);
390 3 my $url = $c->url_for('article', group => $group, id => $num);
391 3 ▶ $url = $url->query(edit => $edit) if $edit;
Branch: 1 branch
BranchTrueFalseTotal
if $edit 0 3 3
392 3 push(@$articles, {
393 id => $id,
394 num => $num,
395 url => $url,
396 from => $from,
397 subject => $subject,
398 date => [$dt->ymd, sprintf("%02d:%02d", $dt->hour, $dt->minute)],
399 references => [split(/\s+/, decode("MIME-Header", $references))],
400 replies => [] });
401 }
402 }
403 # link replies based on references but only the articles on the same page (!)
404 1 for my $article (@$articles) {
405 3 for my $reference (@{$article->{references}}) {
406 1 ▶ my $original = first { $reference eq $_->{id} } @$articles;
Subroutine __ANON__: called CC=1 SCAR=0.0
407 1 ▶ next unless $original;
Branch: 1 branch
BranchTrueFalseTotal
unless $original 1 0 1
408 0 push(@{$original->{replies}}, $article->{id});
409 0 app->log->debug("$article->{id} is a reply to $original->{id}");
410 }
411 }
412 # reverse the list of articles, latest ones come first
413 1 return [reverse @$articles]});
414 1 ▶ return $c->render(template => 'noserver') if 'error' eq $data;
Branch: 1 branch
BranchTrueFalseTotal
if "error" eq $data 0 1 1
415 1 ▶ $nntp->quit if $nntp;
Branch: 1 branch
BranchTrueFalseTotal
if $nntp 1 0 1
416 # If the cached data did not include our article, delete the cache and retry.
417 # This could be optimized to extend the existing data…
418 1 ▶ if ($include and (@$data == 0 or $include < $data->[$#$data]->{num})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include and @$data == 0 || $include < $$data[$data->$#*]{"num"}) 0 1 1
Condition33%2/6
$include and @$data == 0 || $include < $$data[$data->$#*]{"num"}
!ll&&!rl&&r
010
@$data == 0 or $include < $$data[$data->$#*]{"num"}
l!l&&r!l&&!r
001
MC/DC0%0/3
$include and @$data == 0 || $include < $$data[$data->$#*]{"num"}
$include @$data == 0 $include < $$data[$data->$#*]{"num"}
Truth table25%1/4
A and B or C
A= $includeB= @$data == 0C= $include < $$data[$data->$#*]{"num"}
ABCresult
0XX0
1000
11X1
1011
419 0 my $seen = grep { $_->{num} == $include } @$data;
420 0 ▶ if (not $seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 0 0 0
421 0 app->log->debug("$include was not seen in the cached data");
422 0 $cache->set("$group tag $tag" => undef);
423 0 return $c->redirect_to('tag');
424 }
425 }
426 1 $c->render(template => 'tag', group => $group, tag => $tag, edit => $edit, list => $data);
427 5 } => 'tag';
428  
429 # This only works for message-ids, not for message numbers (since they require a
430 # group).
431 0 ▶get '/article/#id' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
432 0 my $c = shift;
433 0 show_article($c, $c->param('id'));
434 5 } => 'article_id';
435  
436 2 ▶get '/article/#group/#id' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
437 2 my $c = shift;
438 2 show_article($c, $c->param('id'), $c->param('group'));
439 5 } => 'article';
440  
441 2 ▶sub show_article {
Subroutine show_article: called CC=2 SCAR=7.2
442 # When following a link from the group, $id_or_num is a num and $group is
443 # important. When following a reference from an article, $id_or_num is a
444 # message-id and $group is only used for the reply form.
445 2 my ($c, $id_or_num, $group) = @_;
446 2 ▶ my $article = cached("$group article $id_or_num", sub {
Subroutine __ANON__: called CC=16 SCAR=29.4
447 2 ▶ my $nntp = Net::NNTP->new() or return 'noserver';
Branch: 1 branch
BranchTrueFalseTotal
my $nntp = "Net::NNTP"->new or return "noserver" 2 0 2
448 2 ▶ $nntp->group($group) if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 2 0 2
449 2 my $article = $nntp->article($id_or_num);
450 2 ▶ return 'unknown' unless $article;
Branch: 1 branch
BranchTrueFalseTotal
unless $article 0 2 2
451 # app->log->trace(join("", @$article));
452 # $article is header lines, an empty line, and body lines
453 2 my $headers = Mojo::Headers->new;
454 2 ▶ while ($_ = shift(@$article)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @$article) 14 0 14
455 14 $headers->parse("$_\r\n");
456 14 ▶ last unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/u 2 12 14
457 }
458 2 my $id = $headers->header("message-id");
459 2 ▶ my $subject = decode("MIME-Header", $headers->header("subject")) || "?";
Condition50%1/2
&decode('MIME-Header', scalar $headers->header('subject')) || '?'
l!l
20
MC/DC0%0/1
&decode('MIME-Header', scalar $headers->header('subject')) || '?'
&decode('MIME-Header', scalar $headers->header('subject'))
Truth table50%1/2
A
A= &decode('MIME-Header', scalar $headers->header('subject'))
Aresult
11
00
460 2 my $from = no_email(decode("MIME-Header", $headers->header("from")));
461 2 my $date = $headers->header("date");
462 2 my $dt = DateTime::Format::Mail->new->loose->parse_datetime($date);
463 2 $date = [$dt->ymd, sprintf("%02d:%02d", $dt->hour, $dt->minute)];
464 2 ▶ my $newsgroups = [split(/\s*,\s*/, decode("MIME-Header", $headers->header("newsgroups")) || "")];
Condition50%1/2
&decode('MIME-Header', scalar $headers->header('newsgroups')) || ''
l!l
20
MC/DC0%0/1
&decode('MIME-Header', scalar $headers->header('newsgroups')) || ''
&decode('MIME-Header', scalar $headers->header('newsgroups'))
Truth table50%1/2
A
A= &decode('MIME-Header', scalar $headers->header('newsgroups'))
Aresult
11
00
465 2 ▶ $group ||= "@$newsgroups";
Condition33%1/3
$group ||= join($", @$newsgroups)
l!l&&r!l&&!r
200
MC/DC0%0/2
$group ||= join($", @$newsgroups)
$group join($", @$newsgroups)
Truth table33%1/3
A ||= B
A= $groupB= join($", @$newsgroups)
ABresult
1X1
011
000
466 2 ▶ my $references = [split(/\s+/, decode("MIME-Header", $headers->header("references")) || "")];
Condition50%1/2
&decode('MIME-Header', scalar $headers->header('references')) || ''
l!l
02
MC/DC0%0/1
&decode('MIME-Header', scalar $headers->header('references')) || ''
&decode('MIME-Header', scalar $headers->header('references'))
Truth table50%1/2
A
A= &decode('MIME-Header', scalar $headers->header('references'))
Aresult
11
00
467 2 my $body = join("", @$article);
468 2 $body =~ s/\s*<\S*?@\S*?>//g; # remove email addresses
469 2 $body =~ s/\s*"\S*?@\S*?"//g; # remove email addresses
470 2 ▶ if ($headers->header('content-type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->header("content-type")) 0 2 2
471 0 my ($charset) = $headers->header('content-type') =~ /charset=['"]?([^;'"]*)/;
472 0 ▶ $body = decode($charset, $body) if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 0 0 0
473 }
474 2 my $value = {
475 id => $id,
476 group => $group,
477 from => $from,
478 subject => $subject,
479 date => $date,
480 newsgroups => $newsgroups,
481 references => $references,
482 html_body => html_unwrap($body),
483 body => $body,
484 };
485 # perhaps we have cached replies from looking at the group (space and no page number at the end)
486 2 ▶ my $cached_group = cached("$group list ", sub {}) || {};
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
cached("$group list ", sub { } ) || {}
l!l
11
MC/DC100%1/1
cached("$group list ", sub { } ) || {}
cached("$group list ", sub { } )
Truth table100%2/2
A
A= cached("$group list ", sub { } )
Aresult
11
00
487 2 ▶ my $cached_article = (first { $_->{id} eq $id } @{$cached_group->{articles}}) || {};
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
&first(sub { $_->{'id'} eq $id; } , @{$$cached_group{'articles'};}) || {}
l!l
11
MC/DC100%1/1
&first(sub { $_->{'id'} eq $id; } , @{$$cached_group{'articles'};}) || {}
&first(sub { $_->{'id'} eq $id; } , @{$$cached_group{'articles'};})
Truth table100%2/2
A
A= &first(sub { $_->{'id'} eq $id; } , @{$$cached_group{'articles'};})
Aresult
11
00
488 2 ▶ $value->{replies} = $cached_article->{replies} || [];
Condition100%2/2
$cached_article->{'replies'} || []
l!l
11
MC/DC100%1/1
$cached_article->{'replies'} || []
$cached_article->{'replies'}
Truth table100%2/2
A
A= $cached_article->{'replies'}
Aresult
11
00
489 2 app->log->debug("$id replies: @{$value->{replies}}");
490 2 $nntp->quit;
491 # If $id_or_num was a number, add a second key to the cache in case we need
492 # the same article but following a reference.
493 2 ▶ $cache->set("$group article $id" => [time, $value]) if $id_or_num ne $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id_or_num ne $id 2 0 2
494 2 return $value });
495 2 ▶ return $c->render(template => $article) unless ref $article;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $article 0 2 2
496 2 $c->render(template => 'article', article => $article, edit => $c->param('edit'));
497 }
498  
499 1 ▶get '/post/#group' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
500 1 my $c = shift;
501 # copy from the cookie
502 1 $c->param($_ => $c->session->{$_}) for qw(name username password);
503 1 $c->render(template => 'post',
504 id => '',
505 subject => '',
506 supersedes => '',
507 references => '');
508 5 } => 'new';
509  
510 1 ▶post '/reply' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
511 1 my $c = shift;
512 # copy from the cookie
513 1 $c->param($_ => $c->session->{$_}) for qw(name username password);
514 1 $c->render(template => 'post',
515 id => $c->param('id'),
516 group => $c->param('group'),
517 subject => $c->param('subject'),
518 supersedes => '',
519 references => $c->param('references'));
520 5 } => 'reply';
521  
522 0 ▶post '/supersede' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
523 0 my $c = shift;
524 # copy from the cookie
525 0 $c->param($_ => $c->session->{$_}) for qw(name username password);
526 0 $c->render(template => 'post',
527 id => '',
528 body => $c->param('body'),
529 group => $c->param('group'),
530 subject => $c->param('subject'),
531 supersedes => $c->param('supersedes'),
532 references => $c->param('references'));
533 5 } => 'supersede';
534  
535 1 ▶post '/post' => sub {
Subroutine __ANON__: called CC=20 SCAR=34.4
536 1 my $c = shift;
537 1 $c->session(expiration => time + 7 * 24 * 60 * 60); # one week
538 1 my $username = $c->param('username');
539 1 ▶ return $c->error("No username") unless $username or $ENV{NEWS_MODE} and $ENV{NEWS_MODE} eq "NOAUTH";
Branch: 1 branch
BranchTrueFalseTotal
unless $username or $ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH" 0 1 1
Condition16%1/6
$username or $ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH"
l!l&&r!l&&!r
100
$ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH"
!ll&&!rl&&r
000
MC/DC0%0/3
$username or $ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH"
$username $ENV{"NEWS_MODE"} $ENV{"NEWS_MODE"} eq "NOAUTH"
Truth table25%1/4
A or B and C
A= $usernameB= $ENV{"NEWS_MODE"}C= $ENV{"NEWS_MODE"} eq "NOAUTH"
ABCresult
1XX1
0111
00X0
0100
540 1 $c->session(username => $username);
541  
542 1 my $password = $c->param('password');
543 1 ▶ return $c->error("No password") unless $password or $ENV{NEWS_MODE} and $ENV{NEWS_MODE}eq "NOAUTH";
Branch: 1 branch
BranchTrueFalseTotal
unless $password or $ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH" 0 1 1
Condition16%1/6
$password or $ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH"
l!l&&r!l&&!r
100
$ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH"
!ll&&!rl&&r
000
MC/DC0%0/3
$password or $ENV{"NEWS_MODE"} and $ENV{"NEWS_MODE"} eq "NOAUTH"
$password $ENV{"NEWS_MODE"} $ENV{"NEWS_MODE"} eq "NOAUTH"
Truth table25%1/4
A or B and C
A= $passwordB= $ENV{"NEWS_MODE"}C= $ENV{"NEWS_MODE"} eq "NOAUTH"
ABCresult
1XX1
0111
00X0
0100
544 1 $c->session(password => $password);
545  
546 1 my $name = $c->param('name');
547 1 ▶ return $c->error("No from address specified") unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 1 1
548 1 $name =~ s/[^[:graph:] ]//g;
549 1 ▶ return $c->error("From address does not have the format 'Your Name <mail\@example.org>'") unless $name =~ /<\S+@\S+\.\S+>/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /<\S+\@\S+\.\S+>/u 0 1 1
550 1 $c->session(name => $name);
551  
552 1 my $group = $c->param('group');
553 1 ▶ return $c->error("No group") unless $group;
Branch: 1 branch
BranchTrueFalseTotal
unless $group 0 1 1
554 1 $group =~ s/[^[:graph:]]//g;
555 1 ▶ return $c->error("No group") unless $group;
Branch: 1 branch
BranchTrueFalseTotal
unless $group 0 1 1
556  
557 1 my $references = $c->param('references');
558 1 my $supersedes = $c->param('supersedes');
559  
560 1 my $subject = $c->param('subject');
561 1 ▶ return $c->error("No subject") unless $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless $subject 0 1 1
562 # $subject = encode("MIME-Header", $subject);
563  
564 1 my $body = $c->param('body');
565 1 ▶ return $c->error("No body") unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 1 1
566  
567 1 ▶ $body = wrap($body) if $c->param('wrap');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("wrap") 0 1 1
568  
569 1 ▶ my $nntp = Net::NNTP->new() or return $c->render(template => 'noserver');
Branch: 1 branch
BranchTrueFalseTotal
my $nntp = "Net::NNTP"->new or return $c->render("template", "noserver") 1 0 1
570 1 ▶ $nntp->authinfo($username, $password) if $username and $password;
Branch: 1 branch
BranchTrueFalseTotal
if $username and $password 1 0 1
Condition33%1/3
$username and $password
!ll&&!rl&&r
001
MC/DC0%0/2
$username and $password
$username $password
Truth table33%1/3
A and B
A= $usernameB= $password
ABresult
0X0
100
111
571 1 my $article = [];
572 1 push(@$article, "From: $name\r\n");
573 1 push(@$article, "Subject: $subject\r\n");
574 1 push(@$article, "Newsgroups: $group\r\n");
575 1 ▶ push(@$article, "References: $references\r\n") if $references;
Branch: 1 branch
BranchTrueFalseTotal
if $references 1 0 1
576 1 ▶ push(@$article, "Supersedes: $supersedes\r\n") if $supersedes;
Branch: 1 branch
BranchTrueFalseTotal
if $supersedes 0 1 1
577 1 push(@$article, "MIME-Version: 1.0\r\n");
578 1 push(@$article, "Content-Type: text/plain; charset=UTF-8\r\n");
579 1 push(@$article, "Content-Transfer-Encoding: 8bit\r\n");
580 1 push(@$article, "\r\n");
581 1 push(@$article, map { "$_\r\n" } split(/\r?\n/, encode('UTF-8', $body)));
582 1 app->log->debug(join("", @$article));
583 1 my $ok = $nntp->post($article);
584 1 ▶ $cache->set("$group list " => undef) if $ok; # includes space and no page number
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
585 1 $nntp->quit;
586 1 $c->render('posted', group => $group, ok => $ok);
587 5 } => 'post';
588  
589 1 ▶get '/latest' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
590 1 my $c = shift;
591 1 ▶ my $list = cached("news " . ($ENV{NEWS_GROUPS} || "*"), sub {
Subroutine __ANON__: called CC=10 SCAR=23.6
592 1 ▶ my $nntp = Net::NNTP->new() or return 'error';
Branch: 1 branch
BranchTrueFalseTotal
my $nntp = "Net::NNTP"->new or return "error" 1 0 1
593 1 my $since = time() - 7 * 24 * 60 * 60; # one week
594 1 ▶ my $ids = $nntp->newnews($since, $ENV{NEWS_GROUPS} || "*");
Condition50%1/2
$ENV{'NEWS_GROUPS'} || '*'
l!l
01
MC/DC0%0/1
$ENV{'NEWS_GROUPS'} || '*'
$ENV{'NEWS_GROUPS'}
Truth table50%1/2
A
A= $ENV{'NEWS_GROUPS'}
Aresult
11
00
595 1 ▶ $ids = [@$ids[$#$ids - $per_page .. $#$ids]] if @$ids > $per_page;
Branch: 1 branch
BranchTrueFalseTotal
if @$ids > $per_page 0 1 1
596 1 my $articles = [];
597 1 my $parser = DateTime::Format::Mail->new->loose;
598 1 for my $id (@$ids) {
599 2 my $head = $nntp->head($id);
600 2 ▶ next unless $head;
Branch: 1 branch
BranchTrueFalseTotal
unless $head 0 2 2
601 2 my $headers = Mojo::Headers->new;
602 2 for my $line (@$head) {
603 13 $headers->parse("$line\r\n");
604 }
605 2 $headers->parse("\r\n"); # make sure it finishes correctly
606 2 ▶ my $subject = decode("MIME-Header", $headers->header("subject")) || "?";
Condition50%1/2
&decode('MIME-Header', scalar $headers->header('subject')) || '?'
l!l
20
MC/DC0%0/1
&decode('MIME-Header', scalar $headers->header('subject')) || '?'
&decode('MIME-Header', scalar $headers->header('subject'))
Truth table50%1/2
A
A= &decode('MIME-Header', scalar $headers->header('subject'))
Aresult
11
00
607 2 my ($tag) = $subject =~ /\[(.*?)\]/;
608 2 my $from = no_email(decode("MIME-Header", $headers->header("from")));
609 2 my $date = $headers->header("date");
610 2 ▶ app->log->debug("$from/$subject/$date") unless $date;
Branch: 1 branch
BranchTrueFalseTotal
unless $date 0 2 2
611 2 my $dt = $parser->parse_datetime($date);
612 2 $date = [$dt->ymd, sprintf("%02d:%02d", $dt->hour, $dt->minute)];
613 2 ▶ my $newsgroups = [split(/\s*,\s*/, decode("MIME-Header", $headers->header("newsgroups")) || "")];
Condition50%1/2
&decode('MIME-Header', scalar $headers->header('newsgroups')) || ''
l!l
20
MC/DC0%0/1
&decode('MIME-Header', scalar $headers->header('newsgroups')) || ''
&decode('MIME-Header', scalar $headers->header('newsgroups'))
Truth table50%1/2
A
A= &decode('MIME-Header', scalar $headers->header('newsgroups'))
Aresult
11
00
614 2 my $group = "@$newsgroups";
615 2 my $url = $c->url_for('article', group => $group, id => $id); # $num is not available
616 2 push(@$articles, {
617 id => $id,
618 tag => $tag,
619 url => $url,
620 group => $group,
621 from => $from,
622 subject => $subject,
623 date => $date,
624 newsgroups => $newsgroups, });
625 };
626 1 $nntp->quit;
627 1 ▶ return $articles });
Condition50%1/2
$ENV{'NEWS_GROUPS'} || '*'
l!l
01
MC/DC0%0/1
$ENV{'NEWS_GROUPS'} || '*'
$ENV{'NEWS_GROUPS'}
Truth table50%1/2
A
A= $ENV{'NEWS_GROUPS'}
Aresult
11
00
628 1 ▶ return $c->render(template => 'noserver') if $list eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $list eq "error" 0 1 1
629 1 $c->render(template => 'latest', list => $list);
630 5 } => 'latest';
631  
632 5 app->start;
633  
634 __DATA__
↑ Summary blib/lib/App/news.pm »