Skip to content

Commit

Permalink
Can negotiate IRCv3 capabilities #444
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Nov 21, 2020
1 parent 0956428 commit 66875fb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/Convos/Core/Connection/Irc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ sub _connect_args {
return $self->SUPER::_connect_args;
}

sub _irc_event_cap {
my ($self, $msg) = @_;
$self->_irc_event_fallback($msg);

if ($msg->{raw_line} =~ m!\s(?:LIST|LS)[^:]+:(.*)!) {
$self->{myinfo}{capabilities}{$_} = true for split /\s/, $1;
my @cap_req; # TODO
$self->_write(@cap_req ? sprintf "CAP REQ :%s\r\n", join ' ', @cap_req : "CAP END\r\n");
}
elsif ($msg->{raw_line} =~ m!\sACK\s:(.+)!) {
$self->_write("CAP END\r\n");
}
elsif ($msg->{raw_line} =~ m!\sNAC!) {
$self->_write("CAP END\r\n");
}
}

sub _irc_event_ctcpreply_rtcz {
my ($self, $msg) = @_;
my ($nick) = IRC::Utils::parse_user($msg->{prefix});
Expand Down Expand Up @@ -1042,6 +1059,7 @@ sub _stream {
my $nick = $self->nick;
my $user = $url->username || $nick;
my $mode = $url->query->param('mode') || 0;
$self->_write("CAP LS\r\n");
$self->_write(sprintf "PASS %s\r\n", $url->password) if length $url->password;
$self->_write("NICK $nick\r\n");
$self->_write("USER $user $mode * :https://convos.chat/\r\n");
Expand Down
44 changes: 44 additions & 0 deletions t/irc-v3-capabilities.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!perl
BEGIN { $ENV{CONVOS_SKIP_CONNECT} = 1 }
use lib '.';
use t::Helper;
use t::Server::Irc;
use Convos::Core;
use Convos::Core::Backend::File;

my $server = t::Server::Irc->new->start;
my $core = Convos::Core->new;
my $user = $core->user({email => '[email protected]'});
$user->save_p->$wait_success;

note 'external';
my $connection = $user->connection({name => 'example', protocol => 'irc'});
$connection->url->userinfo('superwoman:superduper');
$connection->save_p->$wait_success;

$server->client($connection)->server_event_ok('_irc_event_cap')->server_event_ok('_irc_event_nick')
->server_write_ok(":example CAP * LS * :account-notify away-notify chghost extended-join\r\n")
->server_write_ok(":example CAP * LS :invite-notify multi-prefix sasl userhost-in-names\r\n")
->client_event_ok('_irc_event_cap')->server_event_ok('_irc_event_cap')
->server_write_ok(
"example 900 superwoman superwoman!superwoman\@localhost superwoman :You are now logged in as superwoman\r\n"
)->server_write_ok(['welcome.irc'])->client_event_ok('_irc_event_rpl_welcome')
->process_ok('capabilities handshake');

is_deeply $connection->TO_JSON->{me},
{
nick => 'superman',
capabilities => {
'account-notify' => true,
'away-notify' => true,
'chghost' => true,
'extended-join' => true,
'invite-notify' => true,
'multi-prefix' => true,
'sasl' => true,
'userhost-in-names' => true,
},
},
'got capabilities';

done_testing;

0 comments on commit 66875fb

Please sign in to comment.