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