| 1 | package Net::Nostr::Client; | ||
| 2 | |||
| 3 | 2 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.002000'; | ||
| 6 | |||
| 7 | 2 | ▶ | use Net::Nostr::_ConstructorArgs (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Scalar::Util qw(weaken); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use AnyEvent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use AnyEvent::WebSocket::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Net::Nostr::Message; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | use Class::Tiny qw( | ||
| 16 | _ws_client | ||
| 17 | _conn | ||
| 18 | _callbacks | ||
| 19 | challenge | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $class = shift; | ||
| 24 | my %args = Net::Nostr::_ConstructorArgs::normalize(@_); | ||
| 25 | my %allowed = map { $_ => 1 } qw(ssl_no_verify ssl_ca_file); | ||
| 26 | my @unknown = grep { !$allowed{$_} } keys %args; | ||
| 27 | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||
| 28 | my $self = bless {}, $class; | ||
| 29 | $self->_ws_client(AnyEvent::WebSocket::Client->new(%args)); | ||
| 30 | $self->_callbacks({}); | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub connect { | ||
| 35 | my ($self, $url, $cb) = @_; | ||
| 36 | croak "url is required" unless defined $url; | ||
| 37 | croak "callback must be a CODE ref" if defined $cb && ref($cb) ne 'CODE'; | ||
| 38 | |||
| 39 | my $cv = AnyEvent->condvar; | ||
| 40 | $self->_ws_client->connect($url)->cb(sub { | ||
| 41 | my $conn = eval { shift->recv }; | ||
| 42 | if ($@) { | ||
| 43 | $cv->croak("connect failed: $@"); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | $self->_conn($conn); | ||
| 47 | $self->_setup_handlers; | ||
| 48 | $cv->send; | ||
| 49 | }); | ||
| 50 | |||
| 51 | if ($cb) { | ||
| 52 | $cv->cb(sub { eval { shift->recv }; $cb->($@ || undef) }); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | $cv->recv; | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub is_connected { | ||
| 61 | my ($self) = @_; | ||
| 62 | return defined $self->_conn ? 1 : 0; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub disconnect { | ||
| 66 | my ($self) = @_; | ||
| 67 | if ($self->_conn) { | ||
| 68 | $self->_conn->close; | ||
| 69 | $self->_conn(undef); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub publish { | ||
| 74 | my ($self, $event) = @_; | ||
| 75 | croak "not connected" unless $self->is_connected; | ||
| 76 | my $msg = Net::Nostr::Message->new(type => 'EVENT', event => $event); | ||
| 77 | $self->_conn->send($msg->serialize); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub subscribe { | ||
| 81 | my ($self, $sub_id, @filters) = @_; | ||
| 82 | croak "not connected" unless $self->is_connected; | ||
| 83 | my $msg = Net::Nostr::Message->new( | ||
| 84 | type => 'REQ', | ||
| 85 | subscription_id => $sub_id, | ||
| 86 | filters => \@filters, | ||
| 87 | ); | ||
| 88 | $self->_conn->send($msg->serialize); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub count { | ||
| 92 | my ($self, $sub_id, @filters) = @_; | ||
| 93 | croak "not connected" unless $self->is_connected; | ||
| 94 | my $msg = Net::Nostr::Message->new( | ||
| 95 | type => 'COUNT', | ||
| 96 | subscription_id => $sub_id, | ||
| 97 | filters => \@filters, | ||
| 98 | ); | ||
| 99 | $self->_conn->send($msg->serialize); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub close { | ||
| 103 | my ($self, $sub_id) = @_; | ||
| 104 | croak "not connected" unless $self->is_connected; | ||
| 105 | my $msg = Net::Nostr::Message->new(type => 'CLOSE', subscription_id => $sub_id); | ||
| 106 | $self->_conn->send($msg->serialize); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub neg_open { | ||
| 110 | my ($self, $sub_id, $filter, $initial_msg) = @_; | ||
| 111 | croak "not connected" unless $self->is_connected; | ||
| 112 | my $msg = Net::Nostr::Message->new( | ||
| 113 | type => 'NEG-OPEN', | ||
| 114 | subscription_id => $sub_id, | ||
| 115 | filter => $filter, | ||
| 116 | neg_msg => $initial_msg, | ||
| 117 | ); | ||
| 118 | $self->_conn->send($msg->serialize); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub neg_msg { | ||
| 122 | my ($self, $sub_id, $neg_msg) = @_; | ||
| 123 | croak "not connected" unless $self->is_connected; | ||
| 124 | my $msg = Net::Nostr::Message->new( | ||
| 125 | type => 'NEG-MSG', | ||
| 126 | subscription_id => $sub_id, | ||
| 127 | neg_msg => $neg_msg, | ||
| 128 | ); | ||
| 129 | $self->_conn->send($msg->serialize); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub neg_close { | ||
| 133 | my ($self, $sub_id) = @_; | ||
| 134 | croak "not connected" unless $self->is_connected; | ||
| 135 | my $msg = Net::Nostr::Message->new( | ||
| 136 | type => 'NEG-CLOSE', | ||
| 137 | subscription_id => $sub_id, | ||
| 138 | ); | ||
| 139 | $self->_conn->send($msg->serialize); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub authenticate { | ||
| 143 | my ($self, $key, $relay_url) = @_; | ||
| 144 | croak "not connected" unless $self->is_connected; | ||
| 145 | croak "key is required" unless $key; | ||
| 146 | croak "relay_url is required" unless $relay_url; | ||
| 147 | croak "no challenge received" unless defined $self->challenge; | ||
| 148 | |||
| 149 | my $auth_event = $key->create_event( | ||
| 150 | kind => 22242, | ||
| 151 | content => '', | ||
| 152 | tags => [ | ||
| 153 | ['relay', $relay_url], | ||
| 154 | ['challenge', $self->challenge], | ||
| 155 | ], | ||
| 156 | ); | ||
| 157 | my $msg = Net::Nostr::Message->new(type => 'AUTH', event => $auth_event); | ||
| 158 | $self->_conn->send($msg->serialize); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub on { | ||
| 162 | my ($self, $type, $cb) = @_; | ||
| 163 | croak "callback must be a CODE ref" unless defined $cb && ref($cb) eq 'CODE'; | ||
| 164 | $self->_callbacks->{$type} = $cb; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _emit { | ||
| 168 | my ($self, $type, @args) = @_; | ||
| 169 | my $cb = $self->_callbacks->{$type} or return; | ||
| 170 | eval { $cb->(@args); 1 } or warn "callback '$type' died: $@"; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _setup_handlers { | ||
| 174 | my ($self) = @_; | ||
| 175 | weaken(my $weak_self = $self); | ||
| 176 | $self->_conn->on(each_message => sub { | ||
| 177 | my ($conn, $message) = @_; | ||
| 178 | my $self = $weak_self or return; | ||
| 179 | my $msg = eval { Net::Nostr::Message->parse($message->body) }; | ||
| 180 | return warn "bad message from relay: $@\n" if $@; | ||
| 181 | |||
| 182 | if ($msg->type eq 'EVENT') { | ||
| 183 | my $event = $msg->event; | ||
| 184 | my $valid = eval { $event->validate; 1 }; | ||
| 185 | unless ($valid) { | ||
| 186 | warn "invalid event from relay: $@"; | ||
| 187 | return; | ||
| 188 | } | ||
| 189 | $self->_emit('event', $msg->subscription_id, $event); | ||
| 190 | } elsif ($msg->type eq 'OK') { | ||
| 191 | $self->_emit('ok', $msg->event_id, $msg->accepted, $msg->message); | ||
| 192 | } elsif ($msg->type eq 'EOSE') { | ||
| 193 | $self->_emit('eose', $msg->subscription_id, $msg->hints); | ||
| 194 | } elsif ($msg->type eq 'NOTICE') { | ||
| 195 | $self->_emit('notice', $msg->message); | ||
| 196 | } elsif ($msg->type eq 'COUNT') { | ||
| 197 | $self->_emit('count', $msg->subscription_id, $msg->count, $msg->approximate); | ||
| 198 | } elsif ($msg->type eq 'CLOSED') { | ||
| 199 | $self->_emit('closed', $msg->subscription_id, $msg->message); | ||
| 200 | } elsif ($msg->type eq 'AUTH') { | ||
| 201 | $self->challenge($msg->challenge); | ||
| 202 | $self->_emit('auth', $msg->challenge); | ||
| 203 | } elsif ($msg->type eq 'NEG-MSG') { | ||
| 204 | $self->_emit('neg_msg', $msg->subscription_id, $msg->neg_msg); | ||
| 205 | } elsif ($msg->type eq 'NEG-ERR') { | ||
| 206 | $self->_emit('neg_err', $msg->subscription_id, $msg->message); | ||
| 207 | } | ||
| 208 | }); | ||
| 209 | |||
| 210 | $self->_conn->on(finish => sub { | ||
| 211 | my $self = $weak_self or return; | ||
| 212 | $self->_conn(undef); | ||
| 213 | }); | ||
| 214 | } | ||
| 215 | |||
| 216 | 1; | ||
| 217 | |||
| 218 | __END__ | ||