1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test::More;
my $CRLF = "\x0d\x0a"; # because \r\n isn't portable
# We don't care what order we get these messages in, and we know we'll only
# get one of each type at once. Hash them
my %messages;
my $serverstream;
package TestIRC {
use base qw( Protocol::IRC );
sub new { return bless [], shift }
sub write { $serverstream .= $_[1] }
my %isupport = (
CHANTYPES => "#&",
channame_re => qr/^[#&]/,
PREFIX => "(ohv)@%+",
prefix_modes => 'ohv',
prefix_flags => '@%+',
prefixflag_re => qr/^[@%+]/,
prefix_map_m2f => { 'o' => '@', 'h' => '%', 'v' => '+' },
prefix_map_f2m => { '@' => 'o', '%' => 'h', '+' => 'v' },
);
sub isupport { return $isupport{$_[1]} }
sub nick { return "MyNick" }
sub on_message
{
my $self = shift;
my ( $command, $message, $hints ) = @_;
$messages{$command} = [ $message, $hints ];
return 1;
}
}
my $irc = TestIRC->new;
sub write_irc
{
my $line = $_[0];
$irc->on_read( $line );
length $line == 0 or die '$irc failed to read all of the line';
}
write_irc( ':Someone!theiruser@their.host PRIVMSG MyNick :Their message here' . $CRLF );
is_deeply( [ sort keys %messages ], [qw( PRIVMSG text )], 'keys %messages for PRIVMSG' );
my ( $msg, $hints );
( $msg, $hints ) = @{ $messages{PRIVMSG} };
is( $msg->command, "PRIVMSG", '$msg[PRIVMSG]->command for PRIVMSG' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[PRIVMSG]->prefix for PRIVMSG' );
is_deeply( $hints,
{ prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
targets => "MyNick",
text => "Their message here",
handled => 1 },
'$hints[PRIVMSG] for PRIVMSG' );
( $msg, $hints ) = @{ $messages{text} };
is( $msg->command, "PRIVMSG", '$msg[text]->command for PRIVMSG' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[text]->prefix for PRIVMSG' );
is_deeply( $hints,
{ synthesized => 1,
prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
target_name => "MyNick",
target_name_folded => "mynick",
target_is_me => 1,
target_type => "user",
is_notice => 0,
text => "Their message here",
handled => 1 },
'$hints[text] for PRIVMSG' );
undef %messages;
write_irc( ':Someone!theiruser@their.host PRIVMSG #channel :Message to all' . $CRLF );
is_deeply( [ sort keys %messages ], [qw( PRIVMSG text )], 'keys %messages for PRIVMSG to channel' );
( $msg, $hints ) = @{ $messages{PRIVMSG} };
is( $msg->command, "PRIVMSG", '$msg[PRIVMSG]->command for PRIVMSG to channel' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[PRIVMSG]->prefix for PRIVMSG to channel' );
is_deeply( $hints,
{ prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
targets => "#channel",
text => "Message to all",
handled => 1 },
'$hints[PRIVMSG] for PRIVMSG to channel' );
( $msg, $hints ) = @{ $messages{text} };
is( $msg->command, "PRIVMSG", '$msg[text]->command for PRIVMSG to channel' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[text]->prefix for PRIVMSG to channel' );
is_deeply( $hints,
{ synthesized => 1,
prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
target_name => "#channel",
target_name_folded => "#channel",
target_is_me => '',
target_type => "channel",
is_notice => 0,
restriction => '',
text => "Message to all",
handled => 1 },
'$hints[text] for PRIVMSG to channel' );
undef %messages;
write_irc( ':Someone!theiruser@their.host NOTICE #channel :Is anyone listening?' . $CRLF );
is_deeply( [ sort keys %messages ], [qw( NOTICE text )], 'keys %messages for NOTICE to channel' );
( $msg, $hints ) = @{ $messages{NOTICE} };
is( $msg->command, "NOTICE", '$msg[NOTICE]->command for NOTICE to channel' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[NOTICE]->prefix for NOTICE to channel' );
is_deeply( $hints,
{ prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
targets => "#channel",
text => "Is anyone listening?",
handled => 1 },
'$hints[NOTICE] for NOTICE to channel' );
( $msg, $hints ) = @{ $messages{text} };
is( $msg->command, "NOTICE", '$msg[text]->command for NOTICE to channel' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[text]->prefix for NOTICE to channel' );
is_deeply( $hints,
{ synthesized => 1,
prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
target_name => "#channel",
target_name_folded => "#channel",
target_is_me => '',
target_type => "channel",
is_notice => 1,
restriction => '',
text => "Is anyone listening?",
handled => 1 },
'$hints[text] for NOTICE to channel' );
undef %messages;
write_irc( ':Someone!theiruser@their.host PRIVMSG @#channel :To only the important people' . $CRLF );
is_deeply( [ sort keys %messages ], [qw( PRIVMSG text )], 'keys %messages for PRIVMSG to channel ops' );
( $msg, $hints ) = @{ $messages{PRIVMSG} };
is( $msg->command, "PRIVMSG", '$msg[PRIVMSG]->command for PRIVMSG to channel ops' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[PRIVMSG]->prefix for PRIVMSG to channel ops' );
is_deeply( $hints,
{ prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
targets => "@#channel",
text => "To only the important people",
handled => 1 },
'$hints[PRIVMSG] for PRIVMSG to channel ops' );
( $msg, $hints ) = @{ $messages{text} };
is( $msg->command, "PRIVMSG", '$msg[text]->command for PRIVMSG to channel ops' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[text]->prefix for PRIVMSG to channel ops' );
is_deeply( $hints,
{ synthesized => 1,
prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
target_name => "#channel",
target_name_folded => "#channel",
target_is_me => '',
target_type => "channel",
is_notice => 0,
restriction => '@',
text => "To only the important people",
handled => 1 },
'$hints[text] for PRIVMSG to channel ops' );
undef %messages;
write_irc( ":Someone!theiruser\@their.host PRIVMSG MyNick :\001ACTION does something\001" . $CRLF );
is_deeply( [ sort keys %messages ], ["PRIVMSG", "ctcp ACTION"], 'keys %messages for CTCP ACTION' );
( $msg, $hints ) = @{ $messages{PRIVMSG} };
is( $msg->command, "PRIVMSG", '$msg[PRIVMSG]->command for CTCP ACTION' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[PRIVMSG]->prefix for CTCP ACTION' );
is_deeply( $hints,
{ prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
targets => "MyNick",
text => "\001ACTION does something\001",
handled => 1 },
'$hints[PRIVMSG] for CTCP ACTION' );
( $msg, $hints ) = @{ $messages{"ctcp ACTION"} };
is( $msg->command, "PRIVMSG", '$msg[ctcp]->command for CTCP ACTION' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[ctcp]->prefix for CTCP ACTION' );
is_deeply( $hints,
{ synthesized => 1,
prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
target_name => "MyNick",
target_name_folded => "mynick",
target_is_me => 1,
target_type => "user",
is_notice => 0,
text => "\001ACTION does something\001",
ctcp_verb => "ACTION",
ctcp_args => "does something",
handled => 1 },
'$hints[ctcp] for CTCP ACTION' );
undef %messages;
$serverstream = "";
$irc->send_ctcp( undef, "target", "ACTION", "replies" );
is( $serverstream, "PRIVMSG target :\001ACTION replies\001$CRLF", 'server stream after send_ctcp' );
write_irc( ":Someone!theiruser\@their.host NOTICE MyNick :\001VERSION foo/1.2.3\001" . $CRLF );
is_deeply( [ sort keys %messages ], ["NOTICE", "ctcpreply VERSION"], 'keys %messages for CTCPREPLY VERSION' );
( $msg, $hints ) = @{ $messages{NOTICE} };
is( $msg->command, "NOTICE", '$msg[NOTICE]->command for CTCPREPLY VERSION' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[NOTICE]->prefix for CTCPREPLY VERSION' );
is_deeply( $hints,
{ prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
targets => "MyNick",
text => "\001VERSION foo/1.2.3\001",
handled => 1 },
'$hints[NOTICE] for CTCPREPLY VERSION' );
( $msg, $hints ) = @{ $messages{"ctcpreply VERSION"} };
is( $msg->command, "NOTICE", '$msg[ctcpreply]->command for CTCPREPLY VERSION' );
is( $msg->prefix, 'Someone!theiruser@their.host', '$msg[ctcpreply]->prefix for CTCPREPLY VERSION' );
is_deeply( $hints,
{ synthesized => 1,
prefix_nick => "Someone",
prefix_nick_folded => "someone",
prefix_user => "theiruser",
prefix_host => "their.host",
prefix_name => "Someone",
prefix_name_folded => "someone",
prefix_is_me => '',
target_name => "MyNick",
target_name_folded => "mynick",
target_is_me => 1,
target_type => "user",
is_notice => 1,
text => "\001VERSION foo/1.2.3\001",
ctcp_verb => "VERSION",
ctcp_args => "foo/1.2.3",
handled => 1 },
'$hints[ctcpreply] for CTCPREPLY VERSION' );
undef %messages;
$serverstream = "";
$irc->send_ctcpreply( undef, "target", "ACTION", "replies" );
is( $serverstream, "NOTICE target :\001ACTION replies\001$CRLF", 'server stream after send_ctcp' );
done_testing;
|