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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
|
#####################################################################
# #
# Net::IRC -- Object-oriented Perl interface to an IRC server #
# #
# Connection.pm: The basic functions for a simple IRC connection #
# #
# #
# Copyright (c) 1997 Greg Bacon & Dennis Taylor. #
# All rights reserved. #
# #
# This module is free software; you can redistribute it #
# and/or modify it under the terms of the Perl Artistic #
# License, distributed with this module. #
# #
#####################################################################
#####################################################################
# Here's the headers. Package vars are all defined at the bottom, #
# though, because they're big and long and boring. :P #
#####################################################################
package Net::IRC::Connection;
use Net::IRC::Event;
use Net::IRC::DCC;
use IO::Socket;
use Carp;
use strict; # A little anal-retention never hurt...
use vars ( # with a few exceptions...
'$AUTOLOAD', # - the name of the sub in &AUTOLOAD
'%_udef', # - the hash containing the user's global handlers
'%autoloaded', # - the hash containing names of &AUTOLOAD methods
);
# The names of the methods to be handled by &AUTOLOAD.
# It seems the values ought to be useful *somehow*...
my %autoloaded = (
'ircname' => undef,
'port' => undef,
'username' => undef,
'socket' => undef,
'verbose' => undef,
'parent' => undef,
);
# This hash will contain any global default handlers that the user specifies.
my %_udef = ();
#####################################################################
# Methods start here, arranged in alphabetical order. #
#####################################################################
# This sub is the common backend to add_handler and add_global_handler
#
sub _add_generic_handler
{
my ($self, $event, $ref, $rp, $hash_ref, $real_name) = @_;
my $ev;
my %define = ( "replace" => 0, "before" => 1, "after" => 2 );
unless (@_ >= 3) {
croak "Not enough arguments to $real_name()";
}
unless (ref($ref) eq 'CODE') {
croak "Second argument of $real_name isn't a coderef";
}
# Translate REPLACE, BEFORE and AFTER.
if (not defined $rp) {
$rp = 0;
} elsif ($rp =~ /^\D/) {
$rp = $define{lc $rp} || 0;
}
foreach $ev (ref $event eq "ARRAY" ? @{$event} : $event) {
# Translate numerics to names
if ($ev =~ /^\d/) {
$ev = Net::IRC::Event->trans($ev);
unless ($ev) {
carp "Unknown event type in $real_name: $ev";
return;
}
}
$hash_ref->{lc $ev} = [ $ref, $rp ];
}
return 1;
}
# This sub will assign a user's custom function to a particular event which
# might be received by any Connection object.
# Takes 3 args: the event to modify, as either a string or numeric code
# If passed an arrayref, the array is assumed to contain
# all event names which you want to set this handler for.
# a reference to the code to be executed for the event
# (optional) A value indicating whether the user's code should replace
# the built-in handler, or be called with it. Possible values:
# 0 - Replace the built-in handlers entirely. (the default)
# 1 - Call this handler right before the default handler.
# 2 - Call this handler right after the default handler.
# These can also be referred to by the #define-like strings in %define.
sub add_global_handler {
my ($self, $event, $ref, $rp) = @_;
return $self->_add_generic_handler($event, $ref, $rp,
\%_udef, 'add_global_handler');
}
# This sub will assign a user's custom function to a particular event which
# this connection might receive. Same args as above.
sub add_handler {
my ($self, $event, $ref, $rp) = @_;
return $self->_add_generic_handler($event, $ref, $rp,
$self->{_handler}, 'add_handler');
}
# -- #perl was here! --
# fimmtiu: Oh, dear. There actually _is_ an alt.fan.jwz.
# Freiheit: "Join us. *whapdewhapwhap* Join us now. *whapdewhapwhap* Join
# us now and share the software."
# Freiheit: is that actually RMS singing or is it a voice-synthesizer?
# Why do I even bother writing subs this simple? Sends an ADMIN command.
# Takes 1 optional arg: the name of the server you want to query.
sub admin {
my $self = shift; # Thank goodness for AutoLoader, huh?
$self->sl("ADMIN" . ($_[0] ? " $_[0]" : ""));
}
# Takes care of the methods in %autoloaded
# Sets specified attribute, or returns its value if called without args.
sub AUTOLOAD {
my $self = @_; ## can't modify @_ for goto &name
my $class = ref $self; ## die here if !ref($self) ?
my $meth;
# -- #perl was here! --
# <Teratogen> absolute power corrupts absolutely, but it's a helluva lot
# of fun.
# <Teratogen> =)
($meth = $AUTOLOAD) =~ s/^.*:://; ## strip fully qualified portion
unless (exists $autoloaded{$meth}) {
croak "No method called \"$meth\" for $class object.";
}
eval <<EOSub;
sub $meth {
my \$self = shift;
if (\@_) {
my \$old = \$self->{"_$meth"};
\$self->{"_$meth"} = shift;
return \$old;
}
else {
return \$self->{"_$meth"};
}
}
EOSub
## no reason to play this game every time
goto &$meth;
}
# -- #perl was here! --
# <crab> to irc as root demonstrates about the same brains as a man in a
# thunderstorm waving a lightning rod and standing in a copper tub
# of salt water yelling "ALL GODS ARE BASTARDS!"
# DrForr saves that one.
# Attempts to connect to the specified IRC (server, port) with the specified
# (nick, username, ircname). Will close current connection if already open.
sub connect {
my $self = shift;
if (@_) {
my (%arg) = @_;
$self->nick($arg{'Nick'}) if exists $arg{'Nick'};
$self->port($arg{'Port'}) if exists $arg{'Port'};
$self->server($arg{'Server'}) if exists $arg{'Server'};
$self->ircname($arg{'Ircname'}) if exists $arg{'Ircname'};
$self->username($arg{'Username'}) if exists $arg{'Username'};
}
# Lots of error-checking claptrap first...
unless ($self->server) {
unless ($ENV{IRCSERVER}) {
croak "No server address specified in connect()";
}
$self->server( $ENV{IRCSERVER} );
}
unless ($self->nick) {
$self->nick($ENV{IRCNICK} || eval { scalar getpwuid($>) }
|| $ENV{USER} || $ENV{LOGNAME} || "WankerBot");
}
unless ($self->port) {
$self->port($ENV{IRCPORT} || 6667);
}
unless ($self->ircname) {
$self->ircname($ENV{IRCNAME} || eval { (getpwuid($>))[6] }
|| "Just Another Perl Hacker");
}
unless ($self->username) {
$self->username(eval { scalar getpwuid($>) } || $ENV{USER}
|| $ENV{LOGNAME} || "japh");
}
# Now for the socket stuff...
if ($self->connected) {
$self->quit("Changing servers");
}
my $sock = IO::Socket::INET->new(PeerAddr => $self->server,
PeerPort => $self->port,
Proto => "tcp",
);
if ($sock) {
$self->socket($sock);
} else {
carp (sprintf "Can't open socket for %s:%s !",
$self->server, $self->port);
$self->error(1);
return;
}
# Now, log in to the server...
unless ($self->sl(sprintf("NICK %s", $self->nick)) &&
$self->sl(sprintf("USER %s %s %s :%s",
$self->username,
"foo.bar.com",
$self->server,
$self->ircname))) {
carp "Couldn't send introduction to server: $!";
$self->error(1);
$! = "Couldn't send NICK/USER introduction to " . $self->server;
return;
}
$self->{_connected} = 1;
}
# Returns a boolean value based on the state of the object's socket.
sub connected {
my $self = shift;
my $sock = $self->socket;
return ($self->{_connected} && $sock->opened);
}
# Sends a CTCP request to some hapless victim(s).
# Takes at least two args: the type of CTCP request (case insensitive)
# the nick or channel of the intended recipient(s)
# Any further args are arguments to CLIENTINFO, ERRMSG, or ACTION.
sub ctcp {
my ($self, $type, $target) = splice @_, 0, 3;
$type = uc $type;
unless ($target) {
croak "Not enough arguments to ctcp()";
}
if ($type eq "PING") {
unless ($self->sl("PRIVMSG $target :\001PING " . time . "\001")) {
carp "Socket error sending $type request in ctcp()";
return;
}
} elsif (($type eq "CLIENTINFO" or $type eq "ACTION") and @_) {
unless ($self->sl("PRIVMSG $target :\001$type " .
join(" ", @_) . "\001")) {
carp "Socket error sending $type request in ctcp()";
return;
}
} elsif ($type eq "ERRMSG") {
unless (@_) {
carp "Not enough arguments to $type in ctcp()";
return;
}
unless ($self->sl("PRIVMSG $target :\001ERRMSG " .
join(" ", @_) . "\001")) {
carp "Socket error sending $type request in ctcp()";
return;
}
} else {
unless ($self->sl("PRIVMSG $target :\001$type " .
join(" ",@_) . "\001")) {
carp "Socket error sending $type request in ctcp()";
return;
}
}
}
# Sends replies to CTCP queries. Simple enough, right?
# Takes 2 args: the target person or channel to send a reply to
# the text of the reply
sub ctcp_reply {
my $self = shift;
$self->notice($_[0], "\001" . $_[1] . "\001");
}
# Dequotes CTCP messages according to ctcp.spec. Nothing special.
# Then it breaks them into their component parts in a flexible, ircII-
# compatible manner. This is not quite as trivial. Oh, well.
# Takes 1 arg: the line to be dequoted.
sub dequote {
my $line = shift;
my ($order, @chunks) = (0, ()); # CHUNG! CHUNG! CHUNG!
# Filter misplaced \001s before processing... (Thanks, Tom!)
substr($line, rindex($line, "\001"), 1) = '\\a'
unless ($line =~ tr/\001//) % 2 == 0;
# Thanks to Abigail (abigail@fnx.com) for this clever bit.
if (index($line, "\cP") >= 0) { # dequote low-level \n, \r, ^P, and \0.
my (%h) = (n => "\012", r => "\015", 0 => "\0", "\cP" => "\cP");
$line =~ s/\cP([nr0\cP])/$h{$1}/g;
}
$line =~ s/\\([^\\a])/$1/g; # dequote unnecessarily quoted characters.
# -- #perl was here! --
# roy7: Chip Chip he's our man!
# fimmtiu: If he can't do it, Larry can!
# ChipDude: I thank you! No applause, just throw RAM chips!
# If true, it's in odd order... ctcp commands start with first chunk.
$order = 1 if index($line, "\001") == 0;
@chunks = map { s/\\\\/\\/g; $_ } (split /\cA/, $line);
return ($order, @chunks);
}
# Standard destructor method for the GC routines. (HAHAHAH! DIE! DIE! DIE!)
sub DESTROY {
my $self = shift;
$self->handler("destroy", "nobody will ever use this");
$self->quit();
# anything else?
}
# Tells IRC.pm if there was an error opening this connection. It's just
# for sane error passing.
# Takes 1 optional arg: the new value for $self->{'iserror'}
sub error {
my $self = shift;
$self->{'iserror'} = $_[0] if @_;
return $self->{'iserror'};
}
# -- #perl was here! --
# <nocarrier> No, I commute Mon-Wed-Fri from Allentown.
# <rudefix> the billy joel and skinhead place
# <nocarrier> that's what they say.
# <\lembit> it's hard to keep a good man down.
# <qw[jeff]> but only the good die young!
# \lembit won't be getting up today.
# <rudefix> because they're under too much pressure, jeff
# <qw[jeff]> and it surely will catch up to them, somewhere along the line.
# Lets the user set or retrieve a format for a message of any sort.
# Takes at least 1 arg: the event whose format you're inquiring about
# (optional) the new format to use for this event
sub format {
my ($self, $ev) = splice @_, 0, 2;
unless ($ev) {
croak "Not enough arguments to format()";
}
if (@_) {
$self->{'_format'}->{$ev} = $_[0];
} else {
return ($self->{'_format'}->{$ev} ||
$self->{'_format'}->{'default'});
}
}
# -- #perl was here! --
# <q[merlyn]> \lem... know any good austin Perl hackers for hire?
# <q[merlyn]> I'm on a hunt for one for a friend.
# <archon> for a job?
# <Stupid_> No, in his spare time merlyn bow-hunts for perl programmers
# by their scent.
# Calls the appropriate handler function for a specified event.
# BEWARE: The data structure for the handlers is shockingly ugly. I bet this
# will be totally incomprehensible to me 10 minutes after I write it.
# Takes 2 args: the name of the event to handle
# the arguments to the handler function
sub handler {
my ($self, $event) = splice @_, 0, 2;
# Get name of event.
my $ev;
if (ref $event) {
$ev = $event->type;
} elsif (defined $event) {
$ev = $event;
$event = Net::IRC::Event->new($event, '', '', '');
} else {
croak "Not enough arguments to handler()";
}
# -- #perl was here! --
# <\lembit> tainted code...oh-oh..tainted code...sometimes I know I've
# got to (boink boink) run away...
# <Excession> \lembit I'd ease up on the caffiene if I were you
my $handler = undef;
if (exists $self->{_handler}->{$ev}) {
$handler = $self->{_handler}->{$ev};
} elsif (exists $_udef{$ev}) {
$handler = $_udef{$ev};
} else {
return $self->_default($event, @_);
}
my ($code, $rp) = @{$handler};
# If we have args left, try to call the handler.
if ($rp == 0) { # REPLACE
&$code($self, $event, @_);
} elsif ($rp == 1) { # BEFORE
&$code($self, $event, @_);
$self->_default($event, @_);
} elsif ($rp == 2) { # AFTER
$self->_default($event, @_);
&$code($self, $event, @_);
} else {
confess "Bad parameter passed to handler(): rp=$rp";
}
return 1;
}
# Lets a user set hostmasks to discard certain messages from, or (if called
# with only 1 arg), show a list of currently ignored hostmasks of that type.
# Takes 2 args: type of ignore (public, msg, ctcp, etc)
# (optional) [mask(s) to be added to list of specified type]
sub ignore {
my $self = shift;
unless (@_) {
croak "Not enough arguments to ignore()";
}
if (@_ == 1) {
if (exists $self->{_ignore}->{$_[0]}) {
return @{ $self->{_ignore}->{$_[0]} };
} else {
return ();
}
} elsif (@_ > 1) { # code defensively, remember...
my $type = shift;
# I moved this part further down as an Obsessive Efficiency
# Initiative. It shouldn't be a problem if I do _parse right...
# ... but those are famous last words, eh?
unless (grep {$_ eq $type}
qw(public msg ctcp notice channel nick other all)) {
carp "$type isn't a valid type to ignore()";
return;
}
if ( exists $self->{_ignore}->{$type} ) {
push @{$self->{_ignore}->{$type}}, @_;
} else {
$self->{_ignore}->{$type} = [ @_ ];
}
}
}
# Yet Another Ridiculously Simple Sub. Sends an INFO command.
# Takes 1 optional arg: the name of the server to query.
sub info {
my $self = shift;
$self->sl("INFO" . ($_[0] ? " $_[0]" : ""));
}
# Invites someone to an invite-only channel. Whoop.
# Takes 2 args: the nick of the person to invite
# the channel to invite them to.
# I hate the syntax of this command... always seemed like a protocol flaw.
sub invite {
my $self = shift;
unless (@_ > 1) {
croak "Not enough arguments to invite()";
}
$self->sl("INVITE $_[0] $_[1]");
}
# Checks if a particular nickname is in use.
# Takes at least 1 arg: nickname(s) to look up.
sub ison {
my $self = shift;
unless (@_) {
croak 'Not enough args to ison().';
}
$self->sl("ISON " . join(" ", @_));
}
# Joins a channel on the current server if connected, eh?.
# Corresponds to /JOIN command.
# Takes 2 args: name of channel to join
# optional channel password, for +k channels
sub join {
my $self = shift;
unless ( $self->connected ) {
carp "Can't join() -- not connected to a server";
return;
}
# -- #perl was here! --
# *** careful is Starch@ncb.mb.ca (The WebMaster)
# *** careful is on IRC via server irc.total.net (Montreal Hub &
# Client Server)
# careful: well, it's hard to buy more books now too cause where the
# heck do you put them all? i have to move and my puter room is
# almost 400 square feet, it's the largest allowed in my basement
# without calling it a room and pay taxes, hehe
unless (@_) {
croak "Not enough arguments to join()";
}
# \petey: paying taxes by the room?
# \petey boggles
# careful: that's what they do for finished basements and stuff
# careful: need an emergency exit and stuff
# jjohn: GOOD GOD! ARE THEY HEATHENS IN CANADA? DO THEY EAT THEIR
# OWN YOUNG?
return $self->sl("JOIN $_[0]" . ($_[1] ? " $_[1]" : ""));
# \petey: "On the 'net nobody knows you're Canadian, eh?"
# jjohn: shut up, eh?
}
# Opens a righteous can of whoop-ass on any luser foolish enough to ask a
# CGI question in #perl. Eat flaming death, web wankers!
# Takes at least 2 args: the channel to kick the bastard from
# the nick of the bastard in question
# (optional) a parting comment to the departing bastard
sub kick {
my $self = shift;
unless (@_ > 1) {
croak "Not enough arguments to kick()";
}
return $self->sl("KICK $_[0] $_[1]" . ($_[2] ? " :$_[2]" : ""));
}
# -- #perl was here! --
# sputnik1 listens in glee to the high-pitched whine of the Pratt
# and Whitney generator heating up on the launcher of his
# AGM-88B HARM missile
# <lej> sputnik1: calm down, little commie satellite
# Gets a list of all the servers that are linked to another visible server.
# Takes 2 optional args: it's a bitch to describe, and I'm too tired right
# now, so read the RFC.
sub links {
my ($self) = (shift, undef);
$self->sl("LINKS" . (scalar(@_) ? " " . join(" ", @_[0,1]) : ""));
}
# Requests a list of channels on the server, or a quick snapshot of the current
# channel (the server returns channel name, # of users, and topic for each).
sub list {
my $self = shift;
$self->sl("LIST " . join(",", @_));
}
# -- #perl was here! --
# fimmtiu assumes no responsibility for this quote. :-)
# ---------------------
# <china`blu> see, neo?
# <china`blu> they're crowded
# <china`blu> i bet some programmers/coders might be here
# <fimmtiu> Nope. No programmers here. We're just Larry Wall groupies.
# <china`blu> come on
# <Kilbaniar> Larry Wall isn't as good in bed as you'd think.
# <Kilbaniar> For the record...
# Sends a request for some server/user stats.
# Takes 1 optional arg: the name of a server to request the info from.
sub lusers {
my $self = shift;
$self->sl("LUSERS" . ($_[0] ? " $_[0]" : ""));
}
# Gets and/or sets the max line length. The value previous to the sub
# call will be returned.
# Takes 1 (optional) arg: the maximum line length (in bytes)
sub maxlinelen {
my $self = shift;
my $ret = $self->{_maxlinelen};
$self->{_maxlinelen} = shift if @_;
return $ret;
}
# Sends an action to the channel/nick you specify. It's truly amazing how
# many IRCers have no idea that /me's are actually sent via CTCP.
# Takes 2 args: the channel or nick to bother with your witticism
# the action to send (e.g., "weed-whacks billn's hand off.")
sub me {
my $self = shift;
$self->ctcp("ACTION", $_[0], $_[1]);
}
# -- #perl was here! --
# *** china`blu (azizam@pm5-30.flinet.com) has joined channel #perl
# <china`blu> hi guys
# <china`blu> and girls
# <purl> I am NOT a lesbian!
# Change channel and user modes (this one is easy... the handler is a bitch.)
# Takes at least 2 args: the target of the command (channel or nick)
# the mode string (i.e., "-boo+i")
# (optional) operands of the mode string (nicks, hostmasks, etc.)
sub mode {
my $self = shift;
unless (@_ > 1) {
croak "Not enough arguments to mode()";
}
$self->sl("MODE $_[0] $_[1] " . join(" ", @_[2..$#_]));
}
# Sends a MOTD command to a server.
# Takes 1 optional arg: the server to query (defaults to current server)
sub motd {
my $self = shift;
$self->sl("MOTD" . ($_[0] ? " $_[0]" : ""));
}
# -- #perl was here! --
# <Roderick> "Women were put on this earth to weaken us. Drain our energy.
# Laugh at us when they see us naked."
# <qw[jeff]> rod - maybe YOUR women...
# <fimmtiu> jeff: Oh, just wait....
# <Roderick> "Love is a snowmobile racing across the tundra, which
# suddenly flips over, pinning you underneath. At night,
# the ice weasels come."
# <qw[jeff]> rod - where do you GET these things?!
# <Roderick> They do tend to accumulate. Clutter in the brain.
# Requests the list of users for a particular channel (or the entire net, if
# you're a masochist).
# Takes 1 or more optional args: name(s) of channel(s) to list the users from.
sub names {
my $self = shift;
$self->sl("NAMES " . join(",", @_));
} # Was this the easiest sub in the world, or what?
# Creates a new IRC object and assigns some default attributes.
sub new {
my $proto = shift;
# -- #perl was here! --
# <\merlyn> just don't use ref($this) || $this;
# <\merlyn> tchrist's abomination.
# <\merlyn> lame lame lame. frowned upon by any OO programmer I've seen.
# <tchrist> randal disagrees, but i don't care.
# <tchrist> Randal isn't being flexible/imaginative.
# <ChipDude> fimm: WRT "ref ($proto) || $proto", I'm against. Class
# methods and object methods are distinct.
# my $class = ref($proto) || $proto; # Man, am I confused...
my $self = { # obvious defaults go here, rest are user-set
_port => 6667,
# Evals are for non-UNIX machines, just to make sure.
_username => eval { scalar getpwuid($>) } || $ENV{USER}
|| $ENV{LOGNAME} || "japh",
_ircname => $ENV{IRCNAME} || eval { (getpwuid($>))[6] }
|| "Just Another Perl Hacker",
_nick => $ENV{IRCNICK} || eval { scalar getpwuid($>) }
|| $ENV{USER} || $ENV{LOGNAME} || "WankerBot", # heheh...
_ignore => {},
_handler => {},
_verbose => 0, # Is this an OK default?
_parent => shift,
_frag => '',
_connected => 0,
_maxlinelen => 510, # The RFC says we shouldn't exceed this.
_format => {
'default' => "[%f:%t] %m <%d>",
}
};
bless $self, $proto;
# do any necessary initialization here
$self->connect(@_) if @_;
return $self;
}
# Creates and returns a DCC CHAT object, analogous to IRC.pm's newconn().
# Takes at least 1 arg: An Event object for the DCC CHAT request.
# OR A list or listref of args to be passed to new(),
# consisting of:
# - A boolean value indicating whether or not
# you're initiating the CHAT connection.
# - The nick of the chattee
# - The address to connect to
# - The port to connect on
sub new_chat {
my $self = shift;
my ($init, $nick, $address, $port);
if (ref($_[0]) =~ /Event/) {
# If it's from an Event object, we can't be initiating, right?
($init, undef, undef, undef, $address, $port) = (0, $_[0]->args);
$nick = $_[0]->nick;
} elsif (ref($_[0]) eq "ARRAY") {
($init, $nick, $address, $port) = @{$_[0]};
} else {
($init, $nick, $address, $port) = @_;
}
# -- #perl was here! --
# gnat snorts.
# gnat: no fucking microsoft products, thanks :)
# ^Pudge: what about non-fucking MS products? i hear MS Bob is a virgin.
Net::IRC::DCC::CHAT->new($self, $init, $nick, $address, $port);
}
# Creates and returns a DCC GET object, analogous to IRC.pm's newconn().
# Takes at least 1 arg: An Event object for the DCC SEND request.
# OR A list or listref of args to be passed to new(),
# consisting of:
# - The nick of the file's sender
# - The name of the file to receive
# - The address to connect to
# - The port to connect on
# - The size of the incoming file
# For all of the above, an extra argument can be added at the end:
# An open filehandle to save the incoming file into,
# in globref, FileHandle, or IO::* form.
sub new_get {
my $self = shift;
my ($nick, $name, $address, $port, $size, $handle);
if (ref($_[0]) =~ /Event/) {
(undef, undef, $name, $address, $port, $size) = $_[0]->args;
$nick = $_[0]->nick;
$handle = $_[1] if defined $_[1];
} elsif (ref($_[0]) eq "ARRAY") {
($nick, $name, $address, $port, $size) = @{$_[0]};
$handle = $_[1] if defined $_[1];
} else {
($nick, $name, $address, $port, $size, $handle) = @_;
}
unless (defined $handle and ref $handle and
(ref $handle eq "GLOB" or $handle->can('print')))
{
carp ("Filehandle argument to Connection->new_get() must be ".
"a glob reference or object");
return; # is this behavior OK?
}
my $dcc = Net::IRC::DCC::GET->new($self, $nick, $address,
$port, $size, $name, $handle);
$self->parent->addconn($dcc) if $dcc;
return $dcc;
}
# Creates and returns a DCC SEND object, analogous to IRC.pm's newconn().
# Takes at least 2 args: The nickname of the person to send to
# The name of the file to send
# (optional) The blocksize for the connection (default 1k)
sub new_send {
my $self = shift;
my ($nick, $filename, $blocksize);
if (ref($_[0]) eq "ARRAY") {
($nick, $filename, $blocksize) = @{$_[0]};
} else {
($nick, $filename, $blocksize) = @_;
}
Net::IRC::DCC::SEND->new($self, $nick, $filename, $blocksize);
}
# -- #perl was here! --
# [petey suspects I-Que of not being 1337!
# <fimmtiu> Eat flaming death, petey.
# <I-Que> I'm only 22!
# <I-Que> not 1337
# Selects nick for this object or returns currently set nick.
# No default; must be set by user.
# If changed while the object is already connected to a server, it will
# automatically try to change nicks.
# Takes 1 arg: the nick. (I bet you could have figured that out...)
sub nick {
my $self = shift;
if (@_) {
$self->{'_nick'} = shift;
if ($self->connected) {
return $self->sl("NICK " . $self->{'_nick'});
}
}
else
{ return $self->{'_nick'} }
}
# Sends a notice to a channel or person.
# Takes 2 args: the target of the message (channel or nick)
# the text of the message to send
# The message will be chunked if it is longer than the _maxlinelen
# attribute, but it doesn't try to protect against flooding. If you
# give it too much info, the IRC server will kick you off!
sub notice {
my ($self, $to) = splice @_, 0, 2;
unless (@_) {
croak "Not enough arguments to notice()";
}
my ($buf, $length, $line) = (join("", @_), $self->{_maxlinelen});
while($buf) {
($line, $buf) = unpack("a$length a*", $buf);
$self->sl("NOTICE $to :$line");
}
}
# -- #perl was here! --
# <TorgoX> this was back when I watched Talk Soup, before I had to stop
# because I sow friends of mine on it.
# [petey chuckles at TorgoX
# <Technik> TorgoX: on the Jerry Springer clips?
# <TorgoX> I mean, when people you know appear on, like, some Springer
# knockoff, in a cheap disguise, and the Talk Soup host makes fun
# of them, you just have to stop.
# <Technik> TorgoX: you need to get better friends
# <TorgoX> I was shamed. I left town.
# <TorgoX> grad school was just the pretext for the move. this was the
# real reason.
# <Technik> lol
# Makes you an IRCop, if you supply the right username and password.
# Takes 2 args: Operator's username
# Operator's password
sub oper {
my $self = shift;
unless (@_ > 1) {
croak "Not enough arguments to oper()";
}
$self->sl("OPER $_[0] $_[1]");
}
# This function splits apart a raw server line into its component parts
# (message, target, message type, CTCP data, etc...) and passes it to the
# appropriate handler. Takes no args, really.
sub parse {
my ($self) = shift;
my ($from, $type, $message, @stuff, $itype, $ev, @lines, $line);
# Read newly arriving data from $self->socket
# -- #perl was here! --
# <Tkil2> hm.... any joy if you add a 'defined' to the test? like
# if (defined $sock...
# <fimmtiu> Much joy now.
# archon rejoices
if (defined $self->socket->recv($line, 10240) and
(length($self->{_frag}) + length($line)) > 0) {
# grab any remnant from the last go and split into lines
my $chunk = $self->{_frag} . $line;
@lines = split /\012/, $chunk;
# if the last line was incomplete, pop it off the chunk and
# stick it back into the frag holder.
$self->{_frag} = (substr($chunk, -1) ne "\012" ? pop @lines : '');
} else {
# um, if we can read, i say we should read more than 0
# besides, recv isn't returning undef on closed
# sockets. getting rid of this connection...
$self->handler(Net::IRC::Event->new( "disconnect",
$self->server,
'',
'error',
'Connection reset by peer'));
$self->parent->removeconn($self);
return;
}
foreach $line (@lines) {
# Clean the lint filter every 2 weeks...
$line =~ s/[\012\015]+$//;
next unless $line;
# Like the RFC says: "respond as quickly as possible..."
if ($line =~ /^PING/) {
$ev = (Net::IRC::Event->new( "ping",
$self->server,
$self->nick,
"serverping", # FIXME?
substr($line, 5)
));
# Had to move this up front to avoid a particularly pernicious bug.
} elsif ($line =~ /^NOTICE/) {
$ev = Net::IRC::Event->new( "other",
$self->server,
'',
'server',
$line );
# Spurious backslashes are for the benefit of cperl-mode, of course.
# Assumptions: all hostnames have periods in them
# all non-numeric message types begin with a letter
} elsif ($line =~ /^:?
([][}{\w\\\`^|\-]+? # The nick (valid nickname chars)
! # The nick-username separator
.+? # The username
\@)? # Umm, duh...
\S+ # The hostname
\s+ # Space between mask and message type
[A-Za-z] # First char of message type
[^\s:]+? # The rest of the message type
/x) # That ought to do it for now...
{
$line = substr $line, 1 if $line =~ /^:/;
($from, $line) = split ":", $line, 2;
($from, $type, @stuff) = split /\s+/, $from;
$type = lc $type;
# This should be fairly intuitive... (cperl-mode sucks, though)
if (defined $line and index($line, "\001") >= 0) {
$itype = "ctcp";
unless ($type eq "notice") {
$type = (($stuff[0] =~ tr/\#\&//) ? "public" : "msg");
}
} elsif ($type eq "privmsg") {
$itype = $type = (($stuff[0] =~ tr/\#\&//) ? "public" : "msg");
} elsif ($type eq "notice") {
$itype = "notice";
} elsif ($type eq "join" or $type eq "part" or
$type eq "mode" or $type eq "topic" or
$type eq "kick") {
$itype = "channel";
} elsif ($type eq "nick") {
$itype = "nick";
} else {
$itype = "other";
}
# This goes through the list of ignored addresses for this message
# type and drops out of the sub if it's from an ignored hostmask.
study $from;
foreach ( $self->ignore($itype), $self->ignore("all") ) {
$_ = quotemeta; s/\\\*/.*/g;
return 1 if $from =~ /$_/;
}
# It used to look a lot worse. Here was the original version...
# the optimization above was proposed by Silmaril, for which I am
# eternally grateful. (Mine still looks cooler, though. :)
# return if grep { $_ = join('.*', split(/\\\*/,
# quotemeta($_))); /$from/ }
# ($self->ignore($type), $self->ignore("all"));
# Add $line to @stuff for the handlers
push @stuff, $line if defined $line;
# Now ship it off to the appropriate handler and forget about it.
if ( $itype eq "ctcp" ) { # it's got CTCP in it!
$self->parse_ctcp($type, $from, $stuff[0], $line);
return 1;
} elsif ($type eq "public" or $type eq "msg" or
$type eq "notice" or $type eq "mode" or
$type eq "join" or $type eq "part" or
$type eq "topic" or $type eq "invite" ) {
$ev = Net::IRC::Event->new( $type,
$from,
shift(@stuff),
$type,
@stuff,
);
} elsif ($type eq "quit" or $type eq "nick") {
$ev = Net::IRC::Event->new( $type,
$from,
$from,
$type,
@stuff,
);
} elsif ($type eq "kick") {
$ev = Net::IRC::Event->new( $type,
$from,
$stuff[1],
$type,
@stuff[0,2..$#stuff],
);
} elsif ($type eq "kill") {
$ev = Net::IRC::Event->new($type,
$from,
'',
$type,
$line); # Ahh, what the hell.
} else {
carp "Unknown event type: $type";
}
}
# -- #perl was here! --
# *** orwant (orwant@media.mit.edu) has joined channel #perl
# orwant: Howdy howdy.
# orwant: Just came back from my cartooning class.
# orwant: I'm working on a strip for TPJ.
# njt: it's happy bouncy clown jon from clownland! say 'hi' to
# the kiddies, jon!
# orwant splits open njt like a wet bag of groceries and
# dances on his sticky bones.
# njt: excuse me, ladies, but I've got to go eviscerate myself with
# a leaky biro. don't wait up.
elsif ($line =~ /^:? # Here's Ye Olde Numeric Handler!
.+? # the servername (can't assume RFC hostname)
\s+? # Some spaces here...
\d+? # The actual number
\b/x # Some other crap, whatever...
) {
$ev = $self->parse_num($line);
} elsif (index($line, $self->nick . " MODE") == 1) {
$ev = Net::IRC::Event->new( 'umode',
$self->server,
$self->nick,
'server',
substr($line, index($line,':',1) + 1));
} elsif ($line =~ /^ERROR/) {
if ($line =~ /^ERROR :Closing link/) { # is this compatible?
$ev = Net::IRC::Event->new( "disconnect",
$self->server,
'',
'error',
($line =~ /(.*)/));
} else {
$ev = Net::IRC::Event->new( "error",
$self->server,
'',
'error',
(split /:/, $line, 2)[1]);
}
} elsif ($line =~ /^Closing [lL]ink/) {
$ev = Net::IRC::Event->new( "disconnect",
$self->server,
'',
'error',
($line =~ /(.*)/));
}
if ($ev) {
$self->handler($ev);
} else {
# If it gets down to here, it's some exception I forgot about.
carp "Funky parse case: $line\n";
}
}
}
# The backend that parse() sends CTCP requests off to. Pay no attention
# to the camel behind the curtain.
# Takes 4 arguments: the type of message
# who it's from
# the first bit of stuff
# the line from the server.
sub parse_ctcp {
my ($self, $type, $from, $stuff, $line) = @_;
my ($one, $two);
my ($odd, @foo) = (&dequote($line));
while (($one, $two) = (splice @foo, 0, 2)) {
($one, $two) = ($two, $one) if $odd;
my ($ctype) = $one =~ /^(\w+)\b/;
my $prefix = undef;
if ($type eq 'notice') {
$prefix = 'cr';
} elsif ($type eq 'public' or
$type eq 'msg' ) {
$prefix = 'c';
} else {
carp "Unknown CTCP type: $type";
return;
}
if ($prefix) {
my $handler = $prefix . lc $ctype; # unit. value prob with $ctype
# -- #perl was here! --
# fimmtiu: Words cannot describe my joy. Sil, you kick ass.
# fimmtiu: I was passing the wrong arg to Event::new()
$self->handler(Net::IRC::Event->new($handler, $from, $stuff,
$handler, (split /\s/, $one)));
}
# This next line is very likely broken somehow. Sigh.
$self->handler(Net::IRC::Event->new($type, $from, $stuff, $type, $two))
if ($two);
}
return 1;
}
# Does special-case parsing for numeric events. Separate from the rest of
# parse() for clarity reasons (I can hear Tkil gasping in shock now. :-).
# Takes 1 arg: the raw server line
sub parse_num {
my ($self, $line) = @_;
my ($from, $type, @stuff) = split /\s+/, $line;
$from = substr $from, 1 if $from =~ /^:/;
return Net::IRC::Event->new( $type,
$from,
'',
'server',
@stuff );
}
# -- #perl was here! --
# <megas> heh, why are #windowsNT people so quiet? are they all blue screened?
# <Hiro> they're busy flapping their arms and making swooshing jet noises
# Helps you flee those hard-to-stand channels.
# Takes at least one arg: name(s) of channel(s) to leave.
sub part {
my $self = shift;
unless (@_) {
croak "No arguments provided to part()";
}
$self->sl("PART " . join(",", @_)); # "A must!!!"
}
# Tells what's on the other end of a connection. Returns a 2-element list
# consisting of the name on the other end and the type of connection.
# Takes no args.
sub peer {
my $self = shift;
return ($self->server(), "IRC connection");
}
# Prints a message to the defined error filehandle(s).
# No further description should be necessary.
sub printerr {
shift;
print STDERR @_, "\n";
}
# Prints a message to the defined output filehandle(s).
sub print {
shift;
print STDOUT @_, "\n";
}
# Sends a message to a channel or person.
# Takes 2 args: the target of the message (channel or nick)
# the text of the message to send
# Don't use this for sending CTCPs... that's what the ctcp() function is for.
# The message will be chunked if it is longer than the _maxlinelen
# attribute, but it doesn't try to protect against flooding. If you
# give it too much info, the IRC server will kick you off!
sub privmsg {
my ($self, $to) = splice @_, 0, 2;
unless (@_) {
croak 'Not enough arguments to privmsg()';
}
my $buf = join '', @_;
my $length = $self->{_maxlinelen} - 11 - length($to);
my $line;
# -- #perl was here! --
# <v0id_> i really haven't dug into Net::IRC yet.
# <v0id_> hell, i still need to figure out how to make it just say
# something on its current channel...
# <fimmtiu> $connection->privmsg('#channel', "Umm, hi.");
# <v0id_> but you have to know the channel already eh?
# <fimmtiu> Yes. This is how IRC works. :-)
# <v0id_> damnit, why can't everything be a default. :)
# <happybob> v0id_: it can. you end up with things like a 1 button
# mouse then, though. :)
if (ref($to) =~ /^IO::Socket/) {
while($buf) {
($line, $buf) = unpack("a$length a*", $buf);
$to->send($line . "\012");
}
} else {
while($buf) {
($line, $buf) = unpack("a$length a*", $buf);
$self->sl("PRIVMSG $to :$line");
}
}
}
# Closes connection to IRC server. (Corresponding function for /QUIT)
# Takes 1 optional arg: parting message, defaults to "Leaving" by custom.
sub quit {
my $self = shift;
# Do any user-defined stuff before leaving
$self->handler("disconnect");
# -- #perl was here! --
# <fimmtiu> Should an $irc->quit method return an error if the instance
# isn't connected, or die silently?
# <Stupid> fimm: Go quietly into the night.
unless ( $self->connected ) { return 1 }
# Why bother checking for sl() errors now, after all? :)
$self->sl("QUIT :" . (scalar(@_) ? $_[0] : "Leaving"));
$self->socket->close;
$self->{_connected} = undef;
return 1;
}
# Schedules an event to be executed after some length of time.
# Takes at least 2 args: the number of seconds to wait until it's executed
# a coderef to execute when time's up
# Any extra args are passed as arguments to the user's coderef.
sub schedule {
my ($self, $time, $code) = splice @_, 0, 3;
unless ($code) {
croak 'Not enough arguments to Connection->schedule()';
}
unless (ref $code eq 'CODE') {
croak 'Second argument to schedule() isn\'t a coderef';
}
$time = time + int $time;
$self->parent->queue($time, $code, $self, @_);
}
# Lets J. Random IRCop connect one IRC server to another. How uninteresting.
# Takes at least 1 arg: the name of the server to connect your server with
# (optional) the port to connect them on (default 6667)
# (optional) the server to connect to arg #1. Used mainly by
# servers to communicate with each other.
sub sconnect {
my $self = shift;
unless (@_) {
croak "Not enough arguments to sconnect()";
}
$self->sl("CONNECT " . join(" ", @_));
}
# Sets/changes the IRC server which this instance should connect to.
# Takes 1 arg: the name of the server (see below for possible syntaxes)
# ((syntaxen? syntaxi? syntaces?))
sub server {
my ($self) = shift;
if (@_) {
# cases like "irc.server.com:6668"
if (index($_[0], ':') > 0) {
my ($serv, $port) = split /:/, $_[0];
if ($port =~ /\D/) {
carp "$port is not a valid port number in server()";
return;
}
$self->{_server} = $serv;
$self->port($port);
# cases like ":6668" (buried treasure!)
} elsif (index($_[0], ':') == 0 and $_[0] =~ /^:(\d+)/) {
$self->port($1);
# cases like "irc.server.com"
} else {
$self->{_server} = shift;
}
return $self->connect; # Is this behavior OK?
}
else
{ return $self->{_server}; }
}
# Sends a raw IRC line to the server.
# Corresponds to the internal sirc function of the same name.
# Takes 1 arg: string to send to server. (duh. :)
sub sl {
my $self = shift;
unless (@_) {
croak "Not enough arguments to sl()";
}
# RFC compliance can be kinda nice...
my $rv = $self->{_socket}->send("$_[0]\015\012");
unless ($rv) {
$self->handler("sockerror");
return;
}
return $rv;
# discards any extra arguments silently... is that bad? Should it join()?
}
# -- #perl was here! --
# <mandrake> the person at wendy's in front of me had a heart attack while
# I was at lunch
# <Stupid> mandrake: Before or -after- they ate the food?
# <DrForr> mandrake: What did he have?
# <mandrake> DrForr: a big bacon classic
# Tells any server that you're an oper on to disconnect from the IRC network.
# Takes at least 1 arg: the name of the server to disconnect
# (optional) a comment about why it was disconnected
sub squit {
my $self = shift;
unless (@_) {
croak "Not enough arguments to squit()";
}
$self->sl("SQUIT $_[0]" . ($_[1] ? " :$_[1]" : ""));
}
# Gets various server statistics for the specified host.
# Takes at least 1 arg: the type of stats to request [chiklmouy]
# (optional) the server to request from (default is current server)
sub stats {
my $self = shift;
unless (@_) {
croak "Not enough arguments passed to stats()";
}
$self->sl("STATS $_[0]" . ($_[1] ? " $_[1]" : ""));
}
# Requests timestamp from specified server. Easy enough, right?
# Takes 1 optional arg: a server name/mask to query
sub time {
my ($self, $serv) = (shift, undef);
$self->sl("TIME" . ($_[0] ? " $_[0]" : ""));
}
# -- #perl was here! --
# <Murr> DrForr, presumably the tank crew *knew* how to swim, but not how
# to escape from a tank with open hatch that had turned on its roof
# before sinking.
# <DrForr> The tank flipped over -then- sank? Now that's rich.
# <arkuat> what is this about? cisco is building tanks now?
# <Winkola> arkuat: If they do, you can count on a lot of drowned newbie
# net admins.
# <Winkola> "To report a drowning emergency, press 1, and hold for 27 minutes."
# Sends request for current topic, or changes it to something else lame.
# Takes at least 1 arg: the channel whose topic you want to screw around with
# (optional) the new topic you want to impress everyone with
sub topic {
my $self = shift;
unless (@_) {
croak "Not enough arguments to topic()";
}
# Can you tell I've been reading the Nethack source too much? :)
$self->sl("TOPIC $_[0]" . ($_[1] ? " :$_[1]" : ""));
}
# -- #perl was here! --
# crimethnk: problem found.
# crimethnk: log file was 2GB and i could not write to it anymore.
# crimethnk: shit. lost almost a week of stats.
# vorsprung: crimethnk> i guess you'll have to rotate the logs more frequently
# crimethnk: i usually rotate once a month. i missed last month.
# crimethnk: i thought i was pregnant.
# Sends a trace request to the server. Whoop.
# Take 1 optional arg: the server or nickname to trace.
sub trace {
my $self = shift;
$self->sl("TRACE" . ($_[0] ? " $_[0]" : ""));
}
# Requests userhost info from the server.
# Takes at least 1 arg: nickname(s) to look up.
sub userhost {
my $self = shift;
unless (@_) {
croak 'Not enough args to userhost().';
}
$self->sl("USERHOST " . join (" ", @_));
}
# Sends a users request to the server, which may or may not listen to you.
# Take 1 optional arg: the server to query.
sub users {
my $self = shift;
$self->sl("USERS" . ($_[0] ? " $_[0]" : ""));
}
# Asks the IRC server what version and revision of ircd it's running. Whoop.
# Takes 1 optional arg: the server name/glob. (default is current server)
sub version {
my $self = shift;
$self->sl("VERSION" . ($_[0] ? " $_[0]" : ""));
}
# Sends a message to all opers on the network. Hypothetically.
# Takes 1 arg: the text to send.
sub wallops {
my $self = shift;
unless ($_[0]) {
croak 'No arguments passed to wallops()';
}
$self->sl("WALLOPS :" . join("", @_));
}
# Asks the server about stuff, you know. Whatever. Pass the Fritos, dude.
# Takes 2 optional args: the bit of stuff to ask about
# an "o" (nobody ever uses this...)
sub who {
my $self = shift;
# Obfuscation!
$self->sl( "WHO" . ($_[0] ? " $_[0]" : "") . ($_[1] ? " $_[1]" : ""));
}
# -- #perl was here! --
# <\lembit> linda mccartney died yesterday, didn't she?
# <q[merlyn]> yes... she's dead.
# <q[merlyn]> WHY COULDN'T IT HAVE BEEN YOKO?
# If you've gotten this far, you probably already know what this does.
# Takes at least 1 arg: nickmasks or channels to /whois
sub whois {
my $self = shift;
unless (@_) {
croak "Not enough arguments to whois()";
}
return $self->sl("WHOIS " . join(",", @_));
}
# Same as above, in the past tense.
# Takes at least 1 arg: nick to do the /whowas on
# (optional) max number of hits to display
# (optional) server or servermask to query
sub whowas {
my $self = shift;
unless (@_) {
croak "Not enough arguments to whowas()";
}
return $self->sl("WHOWAS $_[0]" . ($_[1] ? " $_[1]" : "") .
(($_[1] && $_[2]) ? " $_[2]" : ""));
}
# This sub executes the default action for an event with no user-defined
# handlers. It's all in one sub so that we don't have to make a ton of
# separate anonymous subs stuffed in a hash. (ouch)
sub _default {
my ($self, $event) = @_;
my $verbose = $self->verbose;
# Users should only see this if the programmer (me) fucked up.
unless ($event) {
croak "You EEEEEDIOT!!! Not enough args to _default()!";
}
# Reply to PING from server as quickly as possible.
if ($event->type eq "ping") {
$self->sl("PONG " . (join ' ', $event->args));
} elsif ($event->type eq "disconnect") {
$self->socket->close;
$self->parent->removeconn($self);
}
return 1;
}
##############################################################################
# THAT'S ALL, FOLKS... #
# #
# <fimmtiu> OK, once you've passed the point where caffeine no longer has #
# any discernible effect on any part of your body but your #
# bladder, it's time to sleep. #
# <fimmtiu> 'Night, all. #
# <regex> Night, fimm #
# #
##############################################################################
1;
__END__
=head1 NAME
Net::IRC::Connection - Object-oriented interface to a single IRC connection
=head1 SYNOPSIS
Hard hat area: This section under construction.
=head1 DESCRIPTION
This documentation is a subset of the main Net::IRC documentation. If
you haven't already, please "perldoc Net::IRC" before continuing.
Net::IRC::Connection defines a class whose instances are individual
connections to a single IRC server. Several Net::IRC::Connection objects may
be handled simultaneously by one Net::IRC object.
=head1 METHOD DESCRIPTIONS
This section is under construction, but hopefully will be finally written up
by the next release. Please see the C<irctest> script and the source for
details about this module.
=head1 AUTHORS
Conceived and initially developed by Greg Bacon E<lt>gbacon@adtran.comE<gt> and
Dennis Taylor E<lt>corbeau@execpc.comE<gt>.
Ideas and large amounts of code donated by Nat "King" Torkington E<lt>gnat@frii.comE<gt>.
Currently being hacked on, hacked up, and worked over by the members of the
Net::IRC developers mailing list. For details, see
http://www.execpc.com/~corbeau/irc/list.html .
=head1 URL
Up-to-date source and information about the Net::IRC project can be found at
http://netirc.betterbox.net/ .
=head1 SEE ALSO
=over
=item *
perl(1).
=item *
RFC 1459: The Internet Relay Chat Protocol
=item *
http://www.irchelp.org/, home of fine IRC resources.
=back
=cut
|