]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Linkify.pm
linkify: require parentheses pairs in URLs
[public-inbox.git] / lib / PublicInbox / Linkify.pm
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # two-step linkification.
5 # intended usage is in the following order:
6 #
7 #   linkify_1
8 #   <escape unsafe chars for HTML>
9 #   linkify_2
10 #
11 # Maybe this could be done more efficiently...
12 package PublicInbox::Linkify;
13 use strict;
14 use warnings;
15 use Digest::SHA qw/sha1_hex/;
16
17 my $SALT = rand;
18 my $LINK_RE = qr{([\('!])?\b((?:ftps?|https?|nntps?|gopher)://
19                  [\@:\w\.-]+(?:/
20                  (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
21                  (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
22                  (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
23                  )?
24                 )}xi;
25
26 sub new { bless {}, $_[0] }
27
28 # try to distinguish paired punctuation chars from the URL itself
29 # Maybe other languages/formats can be supported here, too...
30 my %pairs = (
31         "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
32         "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
33         "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
34 );
35
36 sub linkify_1 {
37         $_[1] =~ s^$LINK_RE^
38                 my $beg = $1 || '';
39                 my $url = $2;
40                 my $end = '';
41
42                 # it's fairly common to end URLs in messages with
43                 # '.', ',' or ';' to denote the end of a statement;
44                 # assume the intent was to end the statement/sentence
45                 # in English
46                 if (defined(my $re = $pairs{$beg})) {
47                         if ($url =~ s/$re//) {
48                                 $end = $1;
49                         }
50                 } elsif ($url =~ s/(\))?([\.,;])\z//) {
51                         $end = $2;
52                         # require ')' to be paired with '('
53                         if (defined $1) { # ')'
54                                 if (index($url, '(') < 0) {
55                                         $end = ")$end";
56                                 } else {
57                                         $url .= ')';
58                                 }
59                         }
60                 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
61                         $end = ')';
62                 }
63
64                 # salt this, as this could be exploited to show
65                 # links in the HTML which don't show up in the raw mail.
66                 my $key = sha1_hex($url . $SALT);
67
68                 # only escape ampersands, others do not match LINK_RE
69                 $url =~ s/&/&#38;/g;
70                 $_[0]->{$key} = $url;
71                 $beg . 'PI-LINK-'. $key . $end;
72         ^ge;
73         $_[1];
74 }
75
76 sub linkify_2 {
77         # Added "PI-LINK-" prefix to avoid false-positives on git commits
78         $_[1] =~ s!\bPI-LINK-([a-f0-9]{40})\b!
79                 my $key = $1;
80                 my $url = $_[0]->{$key};
81                 if (defined $url) {
82                         "<a\nhref=\"$url\">$url</a>";
83                 } else {
84                         # false positive or somebody tried to mess with us
85                         $key;
86                 }
87         !ge;
88         $_[1];
89 }
90
91 1;