| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Net::Async::IRC; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # We need to use C3 MRO to make the ->isupport etc.. methods work properly | ||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use base qw( Net::Async::IRC::Protocol Protocol::IRC::Client ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Socket qw( SOCK_STREAM ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use constant HAVE_MSWIN32 => ( $^O eq "MSWin32" ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | C<Net::Async::IRC> - use IRC with C<IO::Async> | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use IO::Async::Loop; | ||||||||||||||||||||||||||||
| 30 | use Net::Async::IRC; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | my $irc = Net::Async::IRC->new( | ||||||||||||||||||||||||||||
| 35 | on_message_text => sub { | ||||||||||||||||||||||||||||
| 36 | my ( $self, $message, $hints ) = @_; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | print "$hints->{prefix_name} says: $hints->{text}\n"; | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | $loop->add( $irc ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | $irc->login( | ||||||||||||||||||||||||||||
| 45 | nick => "MyName", | ||||||||||||||||||||||||||||
| 46 | host => "irc.example.org", | ||||||||||||||||||||||||||||
| 47 | )->get; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $irc->send_message( "PRIVMSG", undef, "YourName", "Hello world!" ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | $loop->loop_forever; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This object class implements an asynchronous IRC client, for use in programs | ||||||||||||||||||||||||||||
| 56 | based on L<IO::Async>. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | This documentation is very much still in a state of TODO; it is being released | ||||||||||||||||||||||||||||
| 59 | now in the hope it is currently somewhat useful, with the intention of putting | ||||||||||||||||||||||||||||
| 60 | more work into both the code and its documentation at some near point in the | ||||||||||||||||||||||||||||
| 61 | future. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub new | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 5 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=3.1
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 5 | my %args = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | my $on_closed = delete $args{on_closed}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | return $class->SUPER::new( | ||||||||||||||||||||||||||||
| 73 | %args, | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | on_closed => sub { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if( $self->{on_login_f} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $_->fail( "Closed" ) for @{ $self->{on_login_f} }; | |||||||||||||||||||||||||||
| 80 | 0 | undef $self->{on_login_f}; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $on_closed->( $self ) if $on_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | 5 | ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub _init | ||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 91 | 5 | $self->SUPER::_init( @_ ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | $self->{user} = $ENV{LOGNAME} || | ||||||||||||||||||||||||||||
| 94 | 5 | ▶ | ( HAVE_MSWIN32 ? Win32::LoginName() : getpwuid($>) ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'LOGNAME'} || getpwuid $>
MC/DC0%0/2
$ENV{'LOGNAME'} || getpwuid $>
$ENV{'LOGNAME'}
getpwuid $>
Truth table33%1/3
A || B
A= $ENV{'LOGNAME'}B= getpwuid $>
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 5 | $self->{realname} = "Net::Async::IRC client $VERSION"; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 PARAMETERS | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =over 8 | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item nick => STRING | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item user => STRING | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =item realname => STRING | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Connection details. See also C<connect>, C<login>. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | If C<user> is not supplied, it will default to either C<$ENV{LOGNAME}> or the | ||||||||||||||||||||||||||||
| 114 | current user's name as supplied by C<getpwuid()> or C<Win32::LoginName()>. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | If unconnected, changing these properties will set the default values to use | ||||||||||||||||||||||||||||
| 117 | when logging in. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | If logged in, changing the C<nick> property is equivalent to calling | ||||||||||||||||||||||||||||
| 120 | C<change_nick>. Changing the other properties will not take effect until the | ||||||||||||||||||||||||||||
| 121 | next login. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item use_caps => ARRAY of STRING | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Attempts to negotiate IRC v3.1 CAP at connect time. The array gives the names | ||||||||||||||||||||||||||||
| 126 | of capabilities which will be requested, if the server supports them. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =back | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub configure | ||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 6 | my %args = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 6 | for (qw( user realname use_caps )) { | |||||||||||||||||||||||||||
| 138 | 18 | ▶ | $self->{$_} = delete $args{$_} if exists $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 6 | ▶ | if( exists $args{nick} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | $self->_set_nick( delete $args{nick} ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 6 | $self->SUPER::configure( %args ); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 METHODS | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 $irc->connect( %args ) ==> $irc | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Connects to the IRC server. This method does not perform the complete IRC | ||||||||||||||||||||||||||||
| 155 | login sequence; for that see instead the C<login> method. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =over 8 | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =item host => STRING | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Hostname of the IRC server. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item service => STRING or NUMBER | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Optional. Port number or service name of the IRC server. Defaults to 6667. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Any other arguments are passed into the underlying C<IO::Async::Loop> | ||||||||||||||||||||||||||||
| 170 | C<connect> method. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 $irc->connect( %args ) | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The following additional arguments are used to provide continuations when not | ||||||||||||||||||||||||||||
| 175 | returning a Future. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =over 8 | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =item on_connected => CODE | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Continuation to invoke once the connection has been established. Usually used | ||||||||||||||||||||||||||||
| 182 | by the C<login> method to perform the actual login sequence. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | $on_connected->( $irc ) | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =item on_error => CODE | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Continuation to invoke in the case of an error preventing the connection from | ||||||||||||||||||||||||||||
| 189 | taking place. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | $on_error->( $errormsg ) | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =back | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # TODO: Most of this needs to be moved into an abstract Net::Async::Connection role | ||||||||||||||||||||||||||||
| 198 | sub connect | ||||||||||||||||||||||||||||
| 199 | { | ||||||||||||||||||||||||||||
| 200 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=5 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 5 | my %args = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Largely for unit testing | ||||||||||||||||||||||||||||
| 204 | 5 | ▶ | return $self->{connect_f} ||= Future->new->done( $self ) if | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"connect_f"} ||= "Future"->new->done($self)
MC/DC0%0/2
$self->{"connect_f"} ||= "Future"->new->done($self)
$self->{"connect_f"}
"Future"->new->done($self)
Truth table66%2/3
A ||= B
A= $self->{"connect_f"}B= "Future"->new->done($self)
| |||||||||||||||||||||||||||||
| 205 | $self->read_handle; | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 1 | my $on_error = delete $args{on_error}; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 1 | ▶ | $args{service} ||= "6667"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"service"} ||= 6667
MC/DC0%0/1
$args{"service"} ||= 6667
$args{"service"}
Truth table50%1/2
A
A= $args{"service"}
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | return $self->{connect_f} ||= $self->SUPER::connect( | ||||||||||||||||||||||||||||
| 212 | %args, | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | on_resolve_error => sub { | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my ( $msg ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 216 | 0 | chomp $msg; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | if( $args{on_resolve_error} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 219 | 0 | $args{on_resolve_error}->( $msg ); | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | elsif( $on_error ) { | ||||||||||||||||||||||||||||
| 222 | 0 | $on_error->( "Cannot resolve - $msg" ); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | }, | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | on_connect_error => sub { | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if( $args{on_connect_error} ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 228 | 0 | $args{on_connect_error}->( @_ ); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | elsif( $on_error ) { | ||||||||||||||||||||||||||||
| 231 | 0 | $on_error->( "Cannot connect" ); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | }, | ||||||||||||||||||||||||||||
| 234 | 1 | ▶ | )->on_fail( sub { undef $self->{connect_f} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$self->{"connect_f"} ||= $self->SUPER::connect(%args, "on_resolve_error", sub {
my($msg) = @_;
chomp $msg;
if ($args{"on_resolve_error"}) {
$args{"on_resolve_error"}->($msg);
}
elsif ($on_error) {
&$on_error("Cannot resolve - $msg");
};
}
, "on_connect_error", sub {
if ($args{"on_connect_error"}) {
$args{"on_connect_error"}->(@_);
}
elsif ($on_error) {
&$on_error("Cannot connect");
};
}
)->on_fail(sub {
undef $self->{"connect_f"};
}
)
MC/DC0%0/2
$self->{"connect_f"} ||= $self->SUPER::connect(%args, "on_resolve_error", sub {
my($msg) = @_;
chomp $msg;
if ($args{"on_resolve_error"}) {
$args{"on_resolve_error"}->($msg);
}
elsif ($on_error) {
&$on_error("Cannot resolve - $msg");
};
}
, "on_connect_error", sub {
if ($args{"on_connect_error"}) {
$args{"on_connect_error"}->(@_);
}
elsif ($on_error) {
&$on_error("Cannot connect");
};
}
)->on_fail(sub {
undef $self->{"connect_f"};
}
)
$self->{"connect_f"}
$self->SUPER::connect(%args, "on_resolve_error", sub {
my($msg) = @_;
chomp $msg;
if ($args{"on_resolve_error"}) {
$args{"on_resolve_error"}->($msg);
}
elsif ($on_error) {
&$on_error("Cannot resolve - $msg");
};
}
, "on_connect_error", sub {
if ($args{"on_connect_error"}) {
$args{"on_connect_error"}->(@_);
}
elsif ($on_error) {
&$on_error("Cannot connect");
};
}
)->on_fail(sub {
undef $self->{"connect_f"};
}
)
Truth table33%1/3
A ||= B
A= $self->{"connect_f"}B= $self->SUPER::connect(%args, "on_resolve_error", sub {
my($msg) = @_;
chomp $msg;
if ($args{"on_resolve_error"}) {
$args{"on_resolve_error"}->($msg);
}
elsif ($on_error) {
&$on_error("Cannot resolve - $msg");
};
}
, "on_connect_error", sub {
if ($args{"on_connect_error"}) {
$args{"on_connect_error"}->(@_);
}
elsif ($on_error) {
&$on_error("Cannot connect");
};
}
)->on_fail(sub {
undef $self->{"connect_f"};
}
)
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head2 $irc->login( %args ) ==> $irc | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Logs in to the IRC network, connecting first using the C<connect> method if | ||||||||||||||||||||||||||||
| 240 | required. Takes the following named arguments: | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =over 8 | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =item nick => STRING | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =item user => STRING | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =item realname => STRING | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | IRC connection details. Defaults can be set with the C<new> or C<configure> | ||||||||||||||||||||||||||||
| 251 | methods. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =item pass => STRING | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Server password to connect with. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =back | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Any other arguments that are passed, are forwarded to the C<connect> method if | ||||||||||||||||||||||||||||
| 260 | it is required; i.e. if C<login> is invoked when not yet connected to the | ||||||||||||||||||||||||||||
| 261 | server. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =head2 $irc->login( %args ) | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | The following additional arguments are used to provide continuations when not | ||||||||||||||||||||||||||||
| 266 | returning a Future. | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =over 8 | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =item on_login => CODE | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | A continuation to invoke once login is successful. | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | $on_login->( $irc ) | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =back | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =cut | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | sub login | ||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||
| 282 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
login: called CC=10 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 4 | ▶ | my $nick = delete $args{nick} || $self->{nick} or croak "Need a login nick"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
delete $args{'nick'} || $self->{'nick'}
MC/DC0%0/2
delete $args{'nick'} || $self->{'nick'}
delete $args{'nick'}
$self->{'nick'}
Truth table66%2/3
A || B
A= delete $args{'nick'}B= $self->{'nick'}
| |||||||||||||||||||||||||||||
| 286 | 4 | ▶ | my $user = delete $args{user} || $self->{user} or croak "Need a login user"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
delete $args{'user'} || $self->{'user'}
MC/DC0%0/2
delete $args{'user'} || $self->{'user'}
delete $args{'user'}
$self->{'user'}
Truth table66%2/3
A || B
A= delete $args{'user'}B= $self->{'user'}
| |||||||||||||||||||||||||||||
| 287 | 4 | ▶ | my $realname = delete $args{realname} || $self->{realname}; | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $args{'realname'} || $self->{'realname'}
MC/DC0%0/2
delete $args{'realname'} || $self->{'realname'}
delete $args{'realname'}
$self->{'realname'}
Truth table66%2/3
A || B
A= delete $args{'realname'}B= $self->{'realname'}
| |||||||||||||||||||||||||||||
| 288 | 4 | my $pass = delete $args{pass}; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 4 | ▶ | if( !defined $self->{nick} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 3 | $self->_set_nick( $nick ); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 4 | my $on_login = delete $args{on_login}; | |||||||||||||||||||||||||||
| 295 | 4 | ▶ | !defined $on_login or ref $on_login eq "CODE" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $on_login or ref $on_login eq "CODE"
MC/DC0%0/2
not defined $on_login or ref $on_login eq "CODE"
not defined $on_login
ref $on_login eq "CODE"
Truth table66%2/3
not(A) or B
A= not defined $on_loginB= ref $on_login eq "CODE"
| |||||||||||||||||||||||||||||
| 296 | croak "Expected 'on_login' to be a CODE reference"; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | return $self->{login_f} ||= $self->connect( %args )->then( sub { | ||||||||||||||||||||||||||||
| 299 | 4 | ▶ | $self->send_message( "CAP", undef, "LS" ) if $self->{use_caps}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 4 | ▶ | $self->send_message( "PASS", undef, $pass ) if defined $pass; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 4 | $self->send_message( "USER", undef, $user, "0", "*", $realname ); | |||||||||||||||||||||||||||
| 303 | 4 | $self->send_message( "NICK", undef, $nick ); | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 4 | my $f = $self->loop->new_future; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 4 | push @{ $self->{on_login_f} }, $f; | |||||||||||||||||||||||||||
| 308 | 4 | ▶ | $f->on_done( $on_login ) if $on_login; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 4 | return $f; | |||||||||||||||||||||||||||
| 311 | 4 | ▶ | })->on_fail( sub { undef $self->{login_f} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$self->{"login_f"} ||= $self->connect(%args)->then(sub {
$self->send_message("CAP", undef, "LS") if $self->{"use_caps"};
$self->send_message("PASS", undef, $pass) if defined $pass;
$self->send_message("USER", undef, $user, 0, "*", $realname);
$self->send_message("NICK", undef, $nick);
my $f = $self->loop->new_future;
push @{$$self{"on_login_f"};}, $f;
$f->on_done($on_login) if $on_login;
return $f;
}
)->on_fail(sub {
undef $self->{"login_f"};
}
)
MC/DC0%0/2
$self->{"login_f"} ||= $self->connect(%args)->then(sub {
$self->send_message("CAP", undef, "LS") if $self->{"use_caps"};
$self->send_message("PASS", undef, $pass) if defined $pass;
$self->send_message("USER", undef, $user, 0, "*", $realname);
$self->send_message("NICK", undef, $nick);
my $f = $self->loop->new_future;
push @{$$self{"on_login_f"};}, $f;
$f->on_done($on_login) if $on_login;
return $f;
}
)->on_fail(sub {
undef $self->{"login_f"};
}
)
$self->{"login_f"}
$self->connect(%args)->then(sub {
$self->send_message("CAP", undef, "LS") if $self->{"use_caps"};
$self->send_message("PASS", undef, $pass) if defined $pass;
$self->send_message("USER", undef, $user, 0, "*", $realname);
$self->send_message("NICK", undef, $nick);
my $f = $self->loop->new_future;
push @{$$self{"on_login_f"};}, $f;
$f->on_done($on_login) if $on_login;
return $f;
}
)->on_fail(sub {
undef $self->{"login_f"};
}
)
Truth table33%1/3
A ||= B
A= $self->{"login_f"}B= $self->connect(%args)->then(sub {
$self->send_message("CAP", undef, "LS") if $self->{"use_caps"};
$self->send_message("PASS", undef, $pass) if defined $pass;
$self->send_message("USER", undef, $user, 0, "*", $realname);
$self->send_message("NICK", undef, $nick);
my $f = $self->loop->new_future;
push @{$$self{"on_login_f"};}, $f;
$f->on_done($on_login) if $on_login;
return $f;
}
)->on_fail(sub {
undef $self->{"login_f"};
}
)
| |||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =head2 $irc->change_nick( $newnick ) | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Requests to change the nick. If unconnected, the change happens immediately | ||||||||||||||||||||||||||||
| 317 | to the stored defaults. If logged in, sends a C<NICK> command to the server, | ||||||||||||||||||||||||||||
| 318 | which may suceed or fail at a later point. | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =cut | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | sub change_nick | ||||||||||||||||||||||||||||
| 323 | { | ||||||||||||||||||||||||||||
| 324 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
change_nick: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 325 | 3 | my ( $newnick ) = @_; | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 3 | ▶ | if( !$self->is_connected ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | $self->_set_nick( $newnick ); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | else { | ||||||||||||||||||||||||||||
| 331 | 3 | $self->send_message( "NICK", undef, $newnick ); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | ############################ | ||||||||||||||||||||||||||||
| 336 | # Message handling methods # | ||||||||||||||||||||||||||||
| 337 | ############################ | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | =head1 IRC v3.1 CAPABILITIES | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | The following methods relate to IRC v3.1 capabilities negotiations. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =cut | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | sub on_message_cap_LS | ||||||||||||||||||||||||||||
| 346 | { | ||||||||||||||||||||||||||||
| 347 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
on_message_cap_LS: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 348 | 1 | my ( $message, $hints ) = @_; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 1 | my $supported = $self->{caps_supported} = $hints->{caps}; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 1 | my @request = grep { $supported->{$_} } @{$self->{use_caps}}; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 1 | ▶ | if( @request ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 1 | $self->{caps_enabled} = { map { $_ => undef } @request }; | |||||||||||||||||||||||||||
| 356 | 1 | $self->send_message( "CAP", undef, "REQ", join( " ", @request ) ); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | else { | ||||||||||||||||||||||||||||
| 359 | 0 | $self->send_message( "CAP", undef, "END" ); | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 1 | return 1; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 5 | *on_message_cap_ACK = *on_message_cap_NAK = \&_on_message_cap_reply; | |||||||||||||||||||||||||||
| 366 | sub _on_message_cap_reply | ||||||||||||||||||||||||||||
| 367 | { | ||||||||||||||||||||||||||||
| 368 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_on_message_cap_reply: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 369 | 1 | my ( $message, $hints ) = @_; | |||||||||||||||||||||||||||
| 370 | 1 | my $ack = $hints->{verb} eq "ACK"; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 1 | $self->{caps_enabled}{$_} = $ack for keys %{ $hints->{caps} }; | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # Are any outstanding | ||||||||||||||||||||||||||||
| 375 | 1 | ▶ | !defined and return 1 for values %{ $self->{caps_enabled} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 1 | $self->send_message( "CAP", undef, "END" ); | |||||||||||||||||||||||||||
| 378 | 1 | return 1; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =head2 $caps = $irc->caps_supported | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Returns a HASH whose keys give the capabilities listed by the server as | ||||||||||||||||||||||||||||
| 384 | supported in its C<CAP LS> response. If the server ignored the C<CAP> | ||||||||||||||||||||||||||||
| 385 | negotiation then this method returns C<undef>. | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =cut | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | sub caps_supported | ||||||||||||||||||||||||||||
| 390 | { | ||||||||||||||||||||||||||||
| 391 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
caps_supported: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 392 | 2 | return $self->{caps_supported}; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head2 $supported = $irc->cap_supported( $cap ) | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Returns a boolean indicating if the server supports the named capability. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =cut | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | sub cap_supported | ||||||||||||||||||||||||||||
| 402 | { | ||||||||||||||||||||||||||||
| 403 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
cap_supported: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 404 | 1 | my ( $cap ) = @_; | |||||||||||||||||||||||||||
| 405 | 1 | return !!$self->{caps_supported}{$cap}; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =head2 $caps = $irc->caps_enabled | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | Returns a HASH whose keys give the capabilities successfully enabled by the | ||||||||||||||||||||||||||||
| 411 | server as part of the C<CAP REQ> login sequence. If the server ignored the | ||||||||||||||||||||||||||||
| 412 | C<CAP> negotiation then this method returns C<undef>. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =cut | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | sub caps_enabled | ||||||||||||||||||||||||||||
| 417 | { | ||||||||||||||||||||||||||||
| 418 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
caps_enabled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 419 | 2 | return $self->{caps_enabled}; | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head2 $enabled = $irc->cap_enabled( $cap ) | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | Returns a boolean indicating if the client successfully enabled the named | ||||||||||||||||||||||||||||
| 425 | capability. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =cut | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | sub cap_enabled | ||||||||||||||||||||||||||||
| 430 | { | ||||||||||||||||||||||||||||
| 431 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
cap_enabled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 432 | 1 | my ( $cap ) = @_; | |||||||||||||||||||||||||||
| 433 | 1 | return !!$self->{caps_enabled}{$cap}; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head1 MESSAGE HANDLING | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =cut | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | sub on_message_NICK | ||||||||||||||||||||||||||||
| 441 | { | ||||||||||||||||||||||||||||
| 442 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
on_message_NICK: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 443 | 1 | my ( $message, $hints ) = @_; | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 1 | ▶ | if( $hints->{prefix_is_me} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | 1 | $self->_set_nick( $hints->{new_nick} ); | |||||||||||||||||||||||||||
| 447 | 1 | return 1; | |||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 0 | return 0; | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | sub on_message_RPL_WELCOME | ||||||||||||||||||||||||||||
| 454 | { | ||||||||||||||||||||||||||||
| 455 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
on_message_RPL_WELCOME: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 456 | 4 | my ( $message ) = @_; | |||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | # set our nick to be what the server logged us in as | ||||||||||||||||||||||||||||
| 459 | 4 | $self->_set_nick( $message->{args}[0] ); | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 4 | ▶ | if( $self->{on_login_f} and @{ $self->{on_login_f} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"on_login_f"} and @{$self->{"on_login_f"};}
MC/DC0%0/2
$self->{"on_login_f"} and @{$self->{"on_login_f"};}
$self->{"on_login_f"}
@{$self->{"on_login_f"};}
Truth table33%1/3
A and B
A= $self->{"on_login_f"}B= @{$self->{"on_login_f"};}
| |||||||||||||||||||||||||||||
| 462 | 4 | my @futures = @{ $self->{on_login_f} }; | |||||||||||||||||||||||||||
| 463 | 4 | undef $self->{on_login_f}; | |||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 4 | foreach my $f ( @futures ) { | |||||||||||||||||||||||||||
| 466 | 4 | $f->done( $self ); | |||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | # Don't eat it | ||||||||||||||||||||||||||||
| 471 | 4 | return 0; | |||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =over 4 | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | =item * | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | L<http://tools.ietf.org/html/rfc2812> - Internet Relay Chat: Client Protocol | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =back | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =cut | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 5 | 0x55AA; | |||||||||||||||||||||||||||