| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Net::ICB ver 1.6 10/7/98 | ||||||||||||||||||||||||||||
| 4 | # John M Vinopal banshee@resort.com | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Copyright (C) 1996-98, John M Vinopal, All Rights Reserved. | ||||||||||||||||||||||||||||
| 7 | # Permission is granted to copy and modify this program for | ||||||||||||||||||||||||||||
| 8 | # non-commercial purposes, so long as this copyright notice is | ||||||||||||||||||||||||||||
| 9 | # preserved. This software is distributed without warranty. | ||||||||||||||||||||||||||||
| 10 | # Commercial users must contact the author for licensing terms. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Net::ICB; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | require 5.004; | |||||||||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | $VERSION = '1.6'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use vars qw($M_LOGIN $M_LOGINOK $M_OPEN $M_PERSONAL $M_STATUS | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | $M_ERROR $M_ALERT $M_EXIT $M_COMMAND $M_CMDOUT $M_PROTO | ||||||||||||||||||||||||||||
| 24 | 1 | $M_BEEP $M_PING $M_PONG $M_OOPEN $M_OPERSONAL); | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | use vars qw($DEF_HOST $DEF_PORT $DEF_GROUP $DEF_CMD $DEF_USER); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | %EXPORT_TAGS = ( client => [qw($M_LOGIN $M_LOGINOK $M_OPEN $M_PERSONAL | |||||||||||||||||||||||||||
| 33 | $M_STATUS $M_ERROR $M_ALERT $M_EXIT $M_COMMAND $M_CMDOUT | ||||||||||||||||||||||||||||
| 34 | $M_PROTO $M_BEEP $M_PING)], | ||||||||||||||||||||||||||||
| 35 | defaults => [qw($DEF_HOST $DEF_PORT $DEF_GROUP $DEF_CMD $DEF_USER)] ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | @EXPORT_OK = qw( $M_LOGIN $M_LOGINOK $M_OPEN $M_PERSONAL $M_STATUS $M_ERROR | |||||||||||||||||||||||||||
| 38 | $M_ALERT $M_EXIT $M_COMMAND $M_CMDOUT $M_PROTO $M_BEEP $M_PING | ||||||||||||||||||||||||||||
| 39 | $M_PONG $M_OOPEN $M_OPERSONAL | ||||||||||||||||||||||||||||
| 40 | $DEF_HOST $DEF_PORT $DEF_GROUP $DEF_CMD $DEF_USER); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Default connection values. | ||||||||||||||||||||||||||||
| 43 | # (evolve.icb.net, empire.icb.net, cjnetworks.icb.net, swcp.icb.net) | ||||||||||||||||||||||||||||
| 44 | 1 | $DEF_HOST = "default.icb.net"; | |||||||||||||||||||||||||||
| 45 | 1 | $DEF_PORT = 7326; | |||||||||||||||||||||||||||
| 46 | 1 | $DEF_GROUP = "1"; | |||||||||||||||||||||||||||
| 47 | 1 | $DEF_CMD = "login"; # cmds are only "login" and "w" | |||||||||||||||||||||||||||
| 48 | 1 | ▶ | $DEF_USER = eval { getlogin() } || "user".substr(rand(), 2, 5); | ||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
getlogin
}
} || 'user' . substr(rand(), 2, 5)
MC/DC0%0/1
eval {
do {
getlogin
}
} || 'user' . substr(rand(), 2, 5)
eval {
do {
getlogin
}
}
Truth table50%1/2
A
A= eval {
do {
getlogin
}
}
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Protocol definitions: all nice cleartext. | ||||||||||||||||||||||||||||
| 51 | 1 | my $DEL = "\001"; # Packet argument delimiter. | |||||||||||||||||||||||||||
| 52 | 1 | $M_LOGIN = 'a'; # login packet | |||||||||||||||||||||||||||
| 53 | 1 | $M_LOGINOK = 'a'; # login response | |||||||||||||||||||||||||||
| 54 | 1 | $M_OPEN = 'b'; # open msg to group | |||||||||||||||||||||||||||
| 55 | 1 | $M_PERSONAL = 'c'; # personal message | |||||||||||||||||||||||||||
| 56 | 1 | $M_STATUS = 'd'; # group status update message | |||||||||||||||||||||||||||
| 57 | 1 | $M_ERROR = 'e'; # error message | |||||||||||||||||||||||||||
| 58 | 1 | $M_ALERT = 'f'; # important announcement | |||||||||||||||||||||||||||
| 59 | 1 | $M_EXIT = 'g'; # quit packet from server | |||||||||||||||||||||||||||
| 60 | 1 | $M_COMMAND = 'h'; # send a command from user | |||||||||||||||||||||||||||
| 61 | 1 | $M_CMDOUT = 'i'; # output from a command | |||||||||||||||||||||||||||
| 62 | 1 | $M_PROTO = 'j'; # protocol/version information | |||||||||||||||||||||||||||
| 63 | 1 | $M_BEEP = 'k'; # beeps | |||||||||||||||||||||||||||
| 64 | 1 | $M_PING = 'l'; # ping packet from server | |||||||||||||||||||||||||||
| 65 | 1 | $M_PONG = 'm'; # return for ping packet | |||||||||||||||||||||||||||
| 66 | # Archaic packets: some sort of echo scheme? | ||||||||||||||||||||||||||||
| 67 | 1 | $M_OOPEN = 'n'; # for own open messages | |||||||||||||||||||||||||||
| 68 | 1 | $M_OPERSONAL = 'o'; # for own personal messages | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Create a new fnet object and optionally connect to a server. | ||||||||||||||||||||||||||||
| 71 | # keys: host port user nick group cmd passwd | ||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 74 | 2 | my $self = {}; | |||||||||||||||||||||||||||
| 75 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 76 | 2 | ▶ | if (!@_ or $self->connect(@_)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not @_ or $self->connect(@_)
MC/DC50%1/2
not @_ or $self->connect(@_)
not @_
$self->connect(@_)
Truth table66%2/3
not(A) or B
A= not @_B= $self->connect(@_)
| |||||||||||||||||||||||||||||
| 77 | 1 | return $self; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 1 | carp $self->error(); | |||||||||||||||||||||||||||
| 80 | 1 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 85 | 2 | $self->close(); | |||||||||||||||||||||||||||
| 86 | 2 | undef $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Version Checking. | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub version { $VERSION; } | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Error reporting. | ||||||||||||||||||||||||||||
| 93 | 8 | ▶ | sub error { my $self = shift; return $self->{'errstr'}; } | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | sub clearerr { my $self = shift; delete $self->{'errstr'}; } | ||||||||||||||||||||||||||
|
Subroutine
clearerr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Debugging. | ||||||||||||||||||||||||||||
| 97 | 4 | ▶ | sub debug { my $self = shift; my $level = shift; $self->{'debug'} = $level; } | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Return the object's socket. | ||||||||||||||||||||||||||||
| 100 | 34 | ▶ | sub fd { | ||||||||||||||||||||||||||
|
Subroutine
fd: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||
| 102 | 34 | return $self->{'socket'}; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Open or group wide message. | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub sendopen { | ||||||||||||||||||||||||||
|
Subroutine
sendopen: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | @_ == 2 && ref($_[0]) or die '$obj->sendopen(string)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 and ref $_[0]
MC/DC0%0/2
@_ == 2 and ref $_[0]
@_ == 2
ref $_[0]
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[0]
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self, $txt) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | my ($pbuf) = "$M_OPEN$txt"; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (eval { $self->_sendpacket($pbuf) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | return 'ok'; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 0 | $self->{'errstr'} = $@; | |||||||||||||||||||||||||||
| 115 | 0 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Private or user-directed message. | ||||||||||||||||||||||||||||
| 119 | 3 | ▶ | sub sendpriv { | ||||||||||||||||||||||||||
|
Subroutine
sendpriv: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | @_ >= 2 && ref($_[0]) or die '$obj->sendpriv(user, string)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ >= 2 and ref $_[0]
MC/DC0%0/2
@_ >= 2 and ref $_[0]
@_ >= 2
ref $_[0]
Truth table33%1/3
A and B
A= @_ >= 2B= ref $_[0]
| |||||||||||||||||||||||||||||
| 121 | 3 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 122 | 3 | return $self->sendcmd("m", @args); | |||||||||||||||||||||||||||
| 123 | # Any $self->{'errstr'} will be set in sendcmd(). | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Server processed command. | ||||||||||||||||||||||||||||
| 127 | #sub sendcmd(cmd, args) | ||||||||||||||||||||||||||||
| 128 | 4 | ▶ | sub sendcmd { | ||||||||||||||||||||||||||
|
Subroutine
sendcmd: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | @_ >= 2 && ref($_[0]) or die '$obj->sendpriv(cmd, [args])'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ >= 2 and ref $_[0]
MC/DC0%0/2
@_ >= 2 and ref $_[0]
@_ >= 2
ref $_[0]
Truth table33%1/3
A and B
A= @_ >= 2B= ref $_[0]
| |||||||||||||||||||||||||||||
| 130 | 4 | my ($self, $cmd, @args) = @_; | |||||||||||||||||||||||||||
| 131 | 4 | my $pbuf = "$M_COMMAND$cmd$DEL@args"; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | ▶ | if (eval { $self->_sendpacket($pbuf) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 3 | return 'ok'; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 1 | $self->{'errstr'} = $@; | |||||||||||||||||||||||||||
| 137 | 1 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Ping reply. | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub sendpong { | ||||||||||||||||||||||||||
|
Subroutine
sendpong: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | @_ == 1 && ref($_[0]) or die '$obj->sendpong()'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 and ref $_[0]
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1
ref $_[0]
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0]
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 144 | 0 | my ($pbuf) = "$M_PONG"; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (eval { $self->_sendpacket($pbuf) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | return 'ok'; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 0 | $self->{'errstr'} = $@; | |||||||||||||||||||||||||||
| 150 | 0 | return; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Send a raw packet (ie: don't insert a packet type) | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub sendraw { | ||||||||||||||||||||||||||
|
Subroutine
sendraw: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | @_ == 2 && ref($_[0]) or die '$obj->sendraw()'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 and ref $_[0]
MC/DC0%0/2
@_ == 2 and ref $_[0]
@_ == 2
ref $_[0]
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[0]
| |||||||||||||||||||||||||||||
| 156 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 157 | 0 | my ($pbuf) = shift; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (eval { $self->_sendpacket($pbuf) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | return 'ok'; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 0 | $self->{'errstr'} = $@; | |||||||||||||||||||||||||||
| 163 | 0 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Read a message from the server and break it into its fields. | ||||||||||||||||||||||||||||
| 167 | # XXX - timeout to prevent sitting on bad socket? | ||||||||||||||||||||||||||||
| 168 | 28 | ▶ | sub readmsg { | ||||||||||||||||||||||||||
|
Subroutine
readmsg: called CC=5 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 169 | 28 | ▶ | @_ == 1 && ref($_[0]) or die '$obj->readmsg()'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and ref $_[0]
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1
ref $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0]
| |||||||||||||||||||||||||||||
| 170 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | 28 | my $msg; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Read the waiting packet. | ||||||||||||||||||||||||||||
| 174 | 28 | ▶ | if (eval { $msg = $self->_recvpacket() }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | # Break up the message. | ||||||||||||||||||||||||||||
| 176 | 28 | my ($type, $buf) = unpack("aa*", $msg); | |||||||||||||||||||||||||||
| 177 | 28 | my @split = split($DEL, $buf); | |||||||||||||||||||||||||||
| 178 | # Reply to a ping with a pong. | ||||||||||||||||||||||||||||
| 179 | # XXX - let client decide about this? | ||||||||||||||||||||||||||||
| 180 | 28 | ▶ | $self->sendpong() if $type eq $M_PING; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 28 | return ($type, @split); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 0 | $self->{'errstr'} = $@; | |||||||||||||||||||||||||||
| 184 | 0 | return; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Connect to a server and send our login packet. | ||||||||||||||||||||||||||||
| 188 | # keys: host port user nick group cmd passwd | ||||||||||||||||||||||||||||
| 189 | 4 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 4 | undef %$self; # Clear previous values. | |||||||||||||||||||||||||||
| 194 | 4 | $self->debug(0); | |||||||||||||||||||||||||||
| 195 | 4 | ▶ | my $hostname = $args{'host'} || $DEF_HOST; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'host'} || $Net::ICB::DEF_HOST
MC/DC0%0/2
$args{'host'} || $Net::ICB::DEF_HOST
$args{'host'}
$Net::ICB::DEF_HOST
Truth table33%1/3
A || B
A= $args{'host'}B= $Net::ICB::DEF_HOST
| |||||||||||||||||||||||||||||
| 196 | 4 | ▶ | my $portnumber = $args{'port'} || $DEF_PORT; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'port'} || $Net::ICB::DEF_PORT
MC/DC0%0/2
$args{'port'} || $Net::ICB::DEF_PORT
$args{'port'}
$Net::ICB::DEF_PORT
Truth table33%1/3
A || B
A= $args{'port'}B= $Net::ICB::DEF_PORT
| |||||||||||||||||||||||||||||
| 197 | 4 | ▶ | if (eval { $self->_tcpopen($hostname,$portnumber) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 1 | ▶ | if (eval { $self->_sendlogin(@_) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 1 | return 'ok'; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 0 | undef %$self; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 3 | $self->{'errstr'} = "connect: $@"; | |||||||||||||||||||||||||||
| 204 | 3 | return; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 2 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 208 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 209 | 2 | ▶ | if (defined $self->{'socket'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 1 | shutdown($self->{'socket'}, 2); | |||||||||||||||||||||||||||
| 211 | 1 | CORE::close($self->{'socket'}); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 2 | undef %$self; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | #### Internal Methods for Net::ICB #### | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # Sends a login packet to the server. It specifies our login name, | ||||||||||||||||||||||||||||
| 219 | # nickname, active group, a command "login" or "w", and our passwd. | ||||||||||||||||||||||||||||
| 220 | 1 | ▶ | sub _sendlogin { | ||||||||||||||||||||||||||
|
Subroutine
_sendlogin: called CC=7 SCAR=22.2
| |||||||||||||||||||||||||||||
| 221 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 222 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 223 | 1 | my ($user, $nick, $group, $cmd, $passwd); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | ▶ | $user = $args{'user'} || $DEF_USER; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'user'} || $Net::ICB::DEF_USER
MC/DC0%0/2
$args{'user'} || $Net::ICB::DEF_USER
$args{'user'}
$Net::ICB::DEF_USER
Truth table33%1/3
A || B
A= $args{'user'}B= $Net::ICB::DEF_USER
| |||||||||||||||||||||||||||||
| 226 | 1 | ▶ | $nick = $args{'nick'} || $user; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'nick'} || $user
MC/DC0%0/2
$args{'nick'} || $user
$args{'nick'}
$user
Truth table33%1/3
A || B
A= $args{'nick'}B= $user
| |||||||||||||||||||||||||||||
| 227 | 1 | ▶ | $group = $args{'group'} || $DEF_GROUP; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'group'} || $Net::ICB::DEF_GROUP
MC/DC0%0/2
$args{'group'} || $Net::ICB::DEF_GROUP
$args{'group'}
$Net::ICB::DEF_GROUP
Truth table33%1/3
A || B
A= $args{'group'}B= $Net::ICB::DEF_GROUP
| |||||||||||||||||||||||||||||
| 228 | 1 | ▶ | $cmd = $args{'cmd'} || $DEF_CMD; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'cmd'} || $Net::ICB::DEF_CMD
MC/DC0%0/2
$args{'cmd'} || $Net::ICB::DEF_CMD
$args{'cmd'}
$Net::ICB::DEF_CMD
Truth table33%1/3
A || B
A= $args{'cmd'}B= $Net::ICB::DEF_CMD
| |||||||||||||||||||||||||||||
| 229 | 1 | ▶ | $passwd = $args{'passwd'} || ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'passwd'} || ''
MC/DC0%0/1
$args{'passwd'} || ''
$args{'passwd'}
Truth table50%1/2
A
A= $args{'passwd'}
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 1 | my ($pbuf) = $M_LOGIN; | |||||||||||||||||||||||||||
| 232 | 1 | $pbuf .= join($DEL, ($user, $nick, $group, $cmd, $passwd)); | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 1 | ▶ | if (eval { $self->_sendpacket($pbuf) }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 1 | $self->{'user'} = $user; | |||||||||||||||||||||||||||
| 236 | # XXX - wait for protocol and loginok packets? | ||||||||||||||||||||||||||||
| 237 | 1 | return 'ok'; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 0 | die "sendlogin: $@"; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # Send a packet to the server. | ||||||||||||||||||||||||||||
| 243 | 5 | ▶ | sub _sendpacket { | ||||||||||||||||||||||||||
|
Subroutine
_sendpacket: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||
| 244 | 5 | my ($self, $packet) = @_; | |||||||||||||||||||||||||||
| 245 | 5 | my ($socket) = $self->fd; | |||||||||||||||||||||||||||
| 246 | 5 | my ($plen) = length($packet); # Size plus null. | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 5 | ▶ | print "SEND: ",$plen+1,"b -- $packet\\0" if ($self->{'debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | # Bounds checking to MAXCHAR-1 (terminating null). | ||||||||||||||||||||||||||||
| 250 | 5 | ▶ | if ($plen > 254) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 1 | die "send: packet > 255 bytes"; | |||||||||||||||||||||||||||
| 252 | # XXX - truncate and send instead? | ||||||||||||||||||||||||||||
| 253 | #$plen = 254; | ||||||||||||||||||||||||||||
| 254 | #warn "truncated to $plen bytes\n"; | ||||||||||||||||||||||||||||
| 255 | #$packet = substr($packet, 0, $plen); | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # Add the terminating null. | ||||||||||||||||||||||||||||
| 259 | 4 | $packet .= "\0"; $plen++; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Add the packet length (<= 255) to the packet head. | ||||||||||||||||||||||||||||
| 262 | 4 | $packet = chr($plen).$packet; $plen++; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 4 | my $wrotelen = send($socket, $packet, 0); | |||||||||||||||||||||||||||
| 265 | 4 | ▶ | if (not defined($wrotelen)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 266 | 0 | die "send: $!"; | |||||||||||||||||||||||||||
| 267 | } elsif ($wrotelen != $plen) { | ||||||||||||||||||||||||||||
| 268 | 0 | die "send: wrote $wrotelen of $plen: $!"; | |||||||||||||||||||||||||||
| 269 | } else { | ||||||||||||||||||||||||||||
| 270 | 4 | return 'ok'; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 0 | return; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # Read a pending packet from the socket. Will block forever. | ||||||||||||||||||||||||||||
| 277 | 28 | ▶ | sub _recvpacket { | ||||||||||||||||||||||||||
|
Subroutine
_recvpacket: called CC=7 SCAR=20.9
| |||||||||||||||||||||||||||||
| 278 | 28 | my ($self) = @_; | |||||||||||||||||||||||||||
| 279 | 28 | my ($socket) = $self->fd; | |||||||||||||||||||||||||||
| 280 | 28 | my ($slen, $buffer, $ret); | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | # Read a byte of packet length. | ||||||||||||||||||||||||||||
| 283 | 28 | $ret = recv($socket, $slen, 1, 0); | |||||||||||||||||||||||||||
| 284 | 28 | ▶ | if (not defined($ret)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 285 | 0 | die "recv size: $!"; | |||||||||||||||||||||||||||
| 286 | } elsif (length($slen) != 1) { | ||||||||||||||||||||||||||||
| 287 | 0 | die "recv size != 1: $!"; | |||||||||||||||||||||||||||
| 288 | } else { | ||||||||||||||||||||||||||||
| 289 | # Convert char to integer. | ||||||||||||||||||||||||||||
| 290 | 28 | $slen = ord($slen); | |||||||||||||||||||||||||||
| 291 | 28 | ▶ | print "RECV: reading $slen" if ($self->{'debug'} > 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 28 | ▶ | while ($slen) { # Read the entire packet. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 28 | my $pbuf; | |||||||||||||||||||||||||||
| 294 | 28 | $ret = recv($socket, $pbuf, $slen, 0); | |||||||||||||||||||||||||||
| 295 | 28 | ▶ | if (not defined($ret)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | die "recv msg: $!"; | |||||||||||||||||||||||||||
| 297 | } else { | ||||||||||||||||||||||||||||
| 298 | 28 | $slen -= length($pbuf); | |||||||||||||||||||||||||||
| 299 | 28 | $buffer .= $pbuf; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | 28 | ▶ | print "RECV: ",length($buffer),"b -- $buffer\\0" if ($self->{'debug'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | # Remove trailing null. | ||||||||||||||||||||||||||||
| 304 | 28 | chop($buffer); | |||||||||||||||||||||||||||
| 305 | 28 | return($buffer); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | 0 | return; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # tcpopen(hostname,portnumber); | ||||||||||||||||||||||||||||
| 311 | # Returns a connected socket if all goes well. | ||||||||||||||||||||||||||||
| 312 | 4 | ▶ | sub _tcpopen { | ||||||||||||||||||||||||||
|
Subroutine
_tcpopen: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 313 | 4 | my ($self,$hostname,$portnumber) = @_; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 4 | ▶ | my $socket = new IO::Socket::INET( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | PeerAddr => $hostname, | ||||||||||||||||||||||||||||
| 317 | PeerPort => "($portnumber)", | ||||||||||||||||||||||||||||
| 318 | Proto => 'tcp') or die "_tcpopen: $@"; | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 1 | $self->{'socket'} = $socket; | |||||||||||||||||||||||||||
| 321 | 1 | $self->{'host'} = $hostname; | |||||||||||||||||||||||||||
| 322 | 1 | $self->{'port'} = $portnumber; | |||||||||||||||||||||||||||
| 323 | 1 | return 'ok'; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 1 | 1; | |||||||||||||||||||||||||||
| 327 | __END__ | ||||||||||||||||||||||||||||