| 1 | package Net::Nostr::Mention; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Net::Nostr::Bech32 qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | encode_npub encode_note | ||||||||||||||||||||||||||||||||||||||||||
| 8 | encode_nprofile encode_nevent encode_naddr | ||||||||||||||||||||||||||||||||||||||||||
| 9 | decode_bech32_entity | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||
| 14 | extract_mentions | ||||||||||||||||||||||||||||||||||||||||||
| 15 | replace_mentions | ||||||||||||||||||||||||||||||||||||||||||
| 16 | mention_pubkey | ||||||||||||||||||||||||||||||||||||||||||
| 17 | mention_event | ||||||||||||||||||||||||||||||||||||||||||
| 18 | mention_addr | ||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | my $HEX64 = qr/\A[0-9a-f]{64}\z/; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Match nostr: followed by bech32 characters (alphanumeric except 1bio) | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # NIP-19 bech32 uses qpzry9x8gf2tvdw0s3jn54khce6mua7l plus separator '1' | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | my $NOSTR_RE = qr/nostr:([a-z]+1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{6,})/i; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | sub extract_mentions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract_mentions: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | my ($content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | return () unless defined $content && length $content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $content and length $content
MC/DC50%1/2
defined $content and length $content
defined $content
length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | my @mentions; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | while ($content =~ /$NOSTR_RE/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my $bech32 = $1; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my $uri = $&; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | my $end = pos($content); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | my $start = $end - length($uri); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | my $decoded = eval { decode_bech32_entity($bech32) }; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | next unless $decoded; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # Skip nsec per NIP-21 | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | next if $decoded->{type} eq 'nsec'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | push @mentions, { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | type => $decoded->{type}, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | data => $decoded->{data}, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | uri => $uri, | |||||||||||||||||||||||||||||||||||||||||
| 48 | start => $start, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | end => $end, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | return @mentions; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | sub replace_mentions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace_mentions: called CC=6 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | my ($content, $callback) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | croak "callback must be a code reference" unless ref $callback eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | return $content unless defined $content && length $content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $content and length $content
MC/DC0%0/2
defined $content and length $content
defined $content
length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my @mentions = extract_mentions($content); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return $content unless @mentions; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # Build result from back to front to preserve offsets | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | for my $m (reverse @mentions) { | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $replacement = $callback->($m); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | substr($content, $m->{start}, $m->{end} - $m->{start}, $replacement); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | sub mention_pubkey { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mention_pubkey: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my ($pubkey, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | croak "pubkey must be 64-char lowercase hex" unless defined $pubkey && $pubkey =~ $HEX64; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $pubkey and $pubkey =~ /$HEX64/
MC/DC100%2/2
defined $pubkey and $pubkey =~ /$HEX64/
defined $pubkey
$pubkey =~ /$HEX64/
Truth table100%3/3
A and B
A= defined $pubkeyB= $pubkey =~ /$HEX64/
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | if ($opts{relays} && @{$opts{relays}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"relays"} and @{$opts{"relays"};}
MC/DC0%0/2
$opts{"relays"} and @{$opts{"relays"};}
$opts{"relays"}
@{$opts{"relays"};}
Truth table33%1/3
A and B
A= $opts{"relays"}B= @{$opts{"relays"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | return 'nostr:' . encode_nprofile(pubkey => $pubkey, relays => $opts{relays}); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | return 'nostr:' . encode_npub($pubkey); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | ▶ | sub mention_event { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mention_event: called CC=7 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | my ($id, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | croak "event id must be 64-char lowercase hex" unless defined $id && $id =~ $HEX64; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $id and $id =~ /$HEX64/
MC/DC50%1/2
defined $id and $id =~ /$HEX64/
defined $id
$id =~ /$HEX64/
Truth table66%2/3
A and B
A= defined $idB= $id =~ /$HEX64/
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | if ($opts{relays} || $opts{author} || defined $opts{kind}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$opts{"relays"} or $opts{"author"} or defined $opts{"kind"}
$opts{"relays"} or $opts{"author"}
MC/DC0%0/3
$opts{"relays"} or $opts{"author"} or defined $opts{"kind"}
$opts{"relays"}
$opts{"author"}
defined $opts{"kind"}
Truth table50%2/4
A or B or C
A= $opts{"relays"}B= $opts{"author"}C= defined $opts{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | return 'nostr:' . encode_nevent( | ||||||||||||||||||||||||||||||||||||||||||
| 89 | id => $id, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | (relays => $opts{relays}) x !!$opts{relays}, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | (author => $opts{author}) x !!$opts{author}, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | (defined $opts{kind} ? (kind => $opts{kind}) : ()), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return 'nostr:' . encode_note($id); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub mention_addr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mention_addr: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my (%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | croak "mention_addr requires 'identifier'" unless defined $opts{identifier}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | croak "mention_addr requires 'pubkey'" unless defined $opts{pubkey}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | croak "mention_addr requires 'kind'" unless defined $opts{kind}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | return 'nostr:' . encode_naddr( | ||||||||||||||||||||||||||||||||||||||||||
| 105 | identifier => $opts{identifier}, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | pubkey => $opts{pubkey}, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | kind => $opts{kind}, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | ($opts{relays} ? (relays => $opts{relays}) : ()), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||||||||||