1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # two-step linkification.
5 # intended usage is in the following order:
8 # <escape unsafe chars for HTML>
11 # Maybe this could be done more efficiently...
12 package PublicInbox::Linkify;
15 use Digest::SHA qw/sha1_hex/;
16 use PublicInbox::Hval qw(ascii_html);
19 my $LINK_RE = qr{([\('!])?\b((?:ftps?|https?|nntps?|gopher)://
21 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
22 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
23 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
27 sub new { bless {}, $_[0] }
29 # try to distinguish paired punctuation chars from the URL itself
30 # Maybe other languages/formats can be supported here, too...
32 "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
33 "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
34 "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
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
47 if (defined(my $re = $pairs{$beg})) {
48 if ($url =~ s/$re//) {
51 } elsif ($url =~ s/(\))?([\.,;])\z//) {
53 # require ')' to be paired with '('
54 if (defined $1) { # ')'
55 if (index($url, '(') < 0) {
61 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
65 $url = ascii_html($url); # for IDN
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);
72 $beg . 'PI-LINK-'. $key . $end;
78 # Added "PI-LINK-" prefix to avoid false-positives on git commits
79 $_[1] =~ s!\bPI-LINK-([a-f0-9]{40})\b!
81 my $url = $_[0]->{$key};
83 "<a\nhref=\"$url\">$url</a>";
85 # false positive or somebody tried to mess with us
92 # single pass linkification of <Message-ID@example.com> within $str
93 # with $pfx being the URL prefix
95 my ($self, $pfx, $str, $raw) = @_;
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
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(<<a\nhref="$pfx/$href/">$html</a>>);
106 $repl .= qq{ (<a\nhref="$pfx/$href/raw">raw</a>)} if $raw;
107 $self->{$key} = $repl;
110 $$str = ascii_html($$str);
111 $$str =~ s!\bPI-LINK-([a-f0-9]{40})\b!
113 my $repl = $_[0]->{$key};
117 # false positive or somebody tried to mess with us
123 sub to_html { linkify_2($_[0], ascii_html(linkify_1(@_))) }