]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Linkify.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / Linkify.pm
1 # Copyright (C) 2014-2021 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 use PublicInbox::Hval qw(ascii_html mid_href);
17 use PublicInbox::MID qw($MID_EXTRACT);
18
19 my $SALT = rand;
20 my $LINK_RE = qr{([\('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
21                  [\@:\w\.-]+(?:/
22                  (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
23                  (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
24                  (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
25                  )?
26                 )}xi;
27
28 sub new { bless {}, $_[0] }
29
30 # try to distinguish paired punctuation chars from the URL itself
31 # Maybe other languages/formats can be supported here, too...
32 my %pairs = (
33         "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
34         "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
35         "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
36 );
37
38 sub linkify_1 {
39         $_[1] =~ s^$LINK_RE^
40                 my $beg = $1 || '';
41                 my $url = $2;
42                 my $end = '';
43
44                 # it's fairly common to end URLs in messages with
45                 # '.', ',' or ';' to denote the end of a statement;
46                 # assume the intent was to end the statement/sentence
47                 # in English
48                 if (defined(my $re = $pairs{$beg})) {
49                         if ($url =~ s/$re//) {
50                                 $end = $1;
51                         }
52                 } elsif ($url =~ s/(\))?([\.,;])\z//) {
53                         $end = $2;
54                         # require ')' to be paired with '('
55                         if (defined $1) { # ')'
56                                 if (index($url, '(') < 0) {
57                                         $end = ")$end";
58                                 } else {
59                                         $url .= ')';
60                                 }
61                         }
62                 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
63                         $end = ')';
64                 }
65
66                 $url = ascii_html($url); # for IDN
67
68                 # salt this, as this could be exploited to show
69                 # links in the HTML which don't show up in the raw mail.
70                 my $key = sha1_hex($url . $SALT);
71
72                 $_[0]->{$key} = $url;
73                 $beg . 'PI-LINK-'. $key . $end;
74         ^geo;
75         $_[1];
76 }
77
78 sub linkify_2 {
79         # Added "PI-LINK-" prefix to avoid false-positives on git commits
80         $_[1] =~ s!\bPI-LINK-([a-f0-9]{40})\b!
81                 my $key = $1;
82                 my $url = $_[0]->{$key};
83                 if (defined $url) {
84                         "<a\nhref=\"$url\">$url</a>";
85                 } else {
86                         # false positive or somebody tried to mess with us
87                         $key;
88                 }
89         !ge;
90         $_[1];
91 }
92
93 # single pass linkification of <Message-ID@example.com> within $str
94 # with $pfx being the URL prefix
95 sub linkify_mids {
96         my ($self, $pfx, $str, $raw) = @_;
97         $$str =~ s!$MID_EXTRACT!
98                 my $mid = $1;
99                 my $html = ascii_html($mid);
100                 my $href = mid_href($mid);
101
102                 # salt this, as this could be exploited to show
103                 # links in the HTML which don't show up in the raw mail.
104                 my $key = sha1_hex($html . $SALT);
105                 my $repl = qq(&lt;<a\nhref="$pfx/$href/">$html</a>&gt;);
106                 $repl .= qq{ (<a\nhref="$pfx/$href/raw">raw</a>)} if $raw;
107                 $self->{$key} = $repl;
108                 'PI-LINK-'. $key;
109                 !ge;
110         $$str = ascii_html($$str);
111         $$str =~ s!\bPI-LINK-([a-f0-9]{40})\b!
112                 my $key = $1;
113                 my $repl = $_[0]->{$key};
114                 if (defined $repl) {
115                         $repl;
116                 } else {
117                         # false positive or somebody tried to mess with us
118                         $key;
119                 }
120         !ge;
121 }
122
123 sub to_html { linkify_2($_[0], ascii_html(linkify_1(@_))) }
124
125 1;