CyanChat/Server.pm

filter: Statementstmt 8.9% 17 / 189 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 27.7% 5 / 18 Podpod 100% 13 / 13 totaltotal 10.8% 35 / 322 SCAR 78.1
CC
54
Cov
6%
do_one_loop
1336.0
reply
20.0
sendWhoList
20.0
CRAP 2475.4
↑ Summary CyanChat.pm »
1 package Net::CyanChat::Server;
2  
3 #------------------------------------------------------------------------------#
4 # Net::CyanChat - Perl interface for connecting to Cyan Worlds' chat room. #
5 #------------------------------------------------------------------------------#
6 # POD documentation is at the very end of this source code. #
7 #------------------------------------------------------------------------------#
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.03';
15  
16 sub new {
17 0 ▶ my $proto = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
18 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
19  
20 0 my $self = {
21 host => 'localhost',
22 port => 1812,
23 password => undef, # Staff password for command 50
24 sock => undef,
25 select => undef,
26 welcome => [
27 "Welcome to Net::CyanChat::Server v. $VERSION",
28 "",
29 "There are only a few simple rules:",
30 " 1. Be respectful to other users.",
31 " 2. Keep the dialog \"G\" rated.",
32 " 3. And HAVE FUN!",
33 "",
34 "Termination of use can happen without warning!",
35 ],
36 conn => {},
37 who => {},
38 @_,
39 };
40  
41 0 bless ($self,$class);
42 0 return $self;
43 }
44  
45 sub version {
46 0 ▶ my ($self) = @_;
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
47 0 return $VERSION;
48 }
49  
50 sub debug {
51 0 ▶ my ($self,$msg) = @_;
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
52  
53 0 ▶ return unless $self->{debug} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"debug"} == 1 0 0 0
54 0 print "Net::CyanChat::debug // $msg\n";
55 }
56  
57 sub connect {
58 0 ▶ my ($self) = @_;
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
59  
60 # Create the socket.
61 $self->{sock} = IO::Socket::INET->new (
62 LocalAddr => $self->{host},
63 LocalPort => $self->{port},
64 0 ▶ Listen => 1,
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sock"} = "IO::Socket::INET"->new("LocalAddr", $self->{"host"}, "LocalPort", $self->{"port"}, "Listen", 1, "Reuse", 1) or die "Socket error: $!" 0 0 0
65 Reuse => 1,
66 ) or die "Socket error: $!";
67  
68 # Create a select object.
69 0 $self->{select} = IO::Select->new ($self->{sock});
70 0 return 1;
71 }
72  
73 sub start {
74 0 ▶ my ($self) = @_;
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
75 0 while (1) {
76 0 $self->do_one_loop();
77 }
78 0 return undef;
79 }
80  
81 sub reply {
82 0 ▶ my ($self,$socket,$msg) = @_;
Subroutine reply: not called CC=4 SCAR=30.0
Pod documented
83  
84 # Send the message.
85 0 print "S: $msg\n";
86 0 eval {
87 0 ▶ $socket->send ("$msg\x0d\x0a") or do {
Branch: 1 branch
BranchTrueFalseTotal
$socket->send("$msg\r\n") or do { my $id = $socket->fileno; if ($self->{"conn"}{$id}{"login"}) { my $user = $self->{"conn"}{$id}{"username"}; delete $self->{"who"}{$user}; delete $self->{"conn"}{$id}; $self->broadcast("31|$user|^3<mistakenly used an unsafe Linking Book without a maintainer's suit *ZZZZZWHAP*>"); $self->sendWhoList; } ; $self->{"select"}->remove($socket); $socket->close } 0 0 0
88 # He's been disconnected.
89 0 my $id = $socket->fileno;
90 0 ▶ if ($self->{conn}->{$id}->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"login"}) 0 0 0
91 # Remove him.
92 0 my $user = $self->{conn}->{$id}->{username};
93 0 delete $self->{who}->{$user};
94 0 delete $self->{conn}->{$id};
95  
96 # Broadcast it.
97 0 $self->broadcast ("31|$user|^3<mistakenly used an unsafe Linking Book without a maintainer's suit *ZZZZZWHAP*>");
98 0 $self->sendWhoList();
99 }
100  
101 0 $self->{select}->remove ($socket);
102 0 $socket->close();
103 }
104 };
105  
106 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 0 warn "<$@>";
108 0 $self->{select}->remove ($socket);
109 }
110 }
111  
112 sub do_one_loop {
113 0 ▶ my ($self) = @_;
Subroutine do_one_loop: not called CC=37 SCAR=72.0
Pod documented
114  
115 # Look for events.
116 0 my @ready = $self->{select}->can_read(.1);
117 0 ▶ return unless(@ready);
Branch: 1 branch
BranchTrueFalseTotal
unless @ready 0 0 0
118  
119 # Go through each event.
120 0 foreach my $socket (@ready) {
121 # If the listening socket is ready, accept a new connection.
122 0 ▶ if ($socket == $self->{sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket == $self->{'sock'}) { } 0 0 0
123 0 my $new = $self->{sock}->accept();
124 0 $self->{select}->add ($new);
125 0 print $new->fileno . ": connected\n";
126  
127 # Setup data for this connection.
128 0 my $nid = $new->fileno;
129 0 $self->{conn}->{$nid} = {
130 level => 0,
131 announce => 0,
132 nickname => undef,
133 username => undef,
134 login => 0,
135 };
136  
137 # Send a 35.
138 0 my @memlist = ();
139 0 foreach my $member (keys %{$self->{who}}) {
140 0 my $addr = $self->{who}->{$member};
141 0 push (@memlist,"$member,$addr");
142 }
143 0 my $mems = join ('|', @memlist);
144 0 $self->reply ($new,"35|$mems");
145 }
146 else {
147 # Get their ID.
148 0 my $id = $socket->fileno;
149  
150 # Read their request.
151 0 my $line = '';
152 0 $socket->recv ($line, 2048);
153 0 chomp $line;
154 0 $line =~ s/\r//ig;
155 0 $line =~ s/\n//ig;
156  
157 # Skip if this line is blank.
158 0 ▶ next if $line eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 0 0
159  
160 # Go through the events.
161 0 my ($cmd,@args) = split(/\|/, $line);
162  
163 0 print "C $id: $line\n";
164  
165 0 ▶ if ($cmd == 10) {
Branch: 8 branches
BranchTrueFalseTotal
if ($cmd == 10) { } 0 0 0
elsif ($cmd == 15) { } 0 0 0
elsif ($cmd == 20) { } 0 0 0
elsif ($cmd == 30) { } 0 0 0
elsif ($cmd == 40) { } 0 0 0
elsif ($cmd == 50) { } 0 0 0
elsif ($cmd == 60) { } 0 0 0
elsif ($cmd == 70) { } 0 0 0
166 # 10 = Sending their name.
167 0 ▶ if ($self->{conn}->{$id}->{announce}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"announce"}) 0 0 0
168 0 my $nick = join ("|",@args);
169 0 ▶ if (!defined $nick) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $nick) { } 0 0 0
170 # No nick defined.
171 0 $self->reply ($socket,"21|3ChatServer|^1No nickname was defined!");
172 }
173 else {
174 # Format their username.
175 0 my $user = join ("", $self->{conn}->{$id}->{level}, $nick);
176  
177 # Valid nick?
178 0 ▶ if (length $nick <= 20 && $nick !~ /\|/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nick <= 20 and not $nick =~ /\|/) { } 0 0 0
Condition0%0/3
length $nick <= 20 and not $nick =~ /\|/
!ll&&!rl&&r
000
MC/DC0%0/2
length $nick <= 20 and not $nick =~ /\|/
length $nick <= 20 not $nick =~ /\|/
Truth table0%0/3
A and not(B)
A= length $nick <= 20B= not $nick =~ /\|/
ABresult
0X0
100
111
179 # See if the nick isn't already logged on.
180 0 ▶ if (exists $self->{who}->{$user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'who'}{$user}) { } 0 0 0
181 0 $self->reply ($socket,"21|3ChatServer|^1The nickname is already in use.");
182 }
183 else {
184 # Setting another name?
185 0 ▶ if (length $self->{conn}->{$id}->{username} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"conn"}{$id}{"username"} > 0) 0 0 0
186 # Remove the old.
187 0 my $old = $self->{conn}->{$id}->{username};
188 0 delete $self->{who}->{$old};
189 }
190  
191 # Make up their join message.
192 0 my $join = "somewhere on the internet Age";
193  
194 # Staff?
195 0 ▶ if ($self->{conn}->{$id}->{level} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"level"} == 1) 0 0 0
196 0 $join = "Cyan Worlds, Inc.";
197 }
198  
199 # Join them.
200 0 $self->{who}->{$user} = $socket->peerhost;
201 0 $self->{conn}->{$id}->{username} = $user;
202 0 $self->{conn}->{$id}->{nickname} = $nick;
203 0 $self->{conn}->{$id}->{login} = 1;
204 0 $self->reply ($socket,"11"); # 11 = name accepted
205 0 $self->broadcast ("31|$user|^2<links in from $join>");
206  
207 # Update the Who List.
208 0 $self->sendWhoList();
209 }
210 }
211 else {
212 # Invalid nick.
213 0 $self->reply ($socket,"10"); # 10 = name invalid
214 }
215 }
216 }
217 }
218 elsif ($cmd == 15) {
219 # 15 = Remove their name (sign out).
220 0 ▶ if ($self->{conn}->{$id}->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"login"}) 0 0 0
221 # Exit them.
222 0 my $nick = $self->{conn}->{$id}->{username};
223 0 $self->{conn}->{$id}->{username} = undef;
224 0 $self->{conn}->{$id}->{nickname} = undef;
225 0 $self->{conn}->{$id}->{login} = 0;
226 0 delete $self->{who}->{$nick};
227 0 $self->broadcast ("31|$nick|^3<links safely back to their home Age>");
228 0 $self->sendWhoList();
229 }
230 }
231 elsif ($cmd == 20) {
232 # 20 = send private message.
233 0 ▶ if ($self->{conn}->{$id}->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"login"}) 0 0 0
234 0 my $to = shift @args;
235 0 my $msg = join ("|",@args);
236  
237 0 ▶ if ($to && $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to and $msg) 0 0 0
Condition0%0/3
$to and $msg
!ll&&!rl&&r
000
MC/DC0%0/2
$to and $msg
$to $msg
Truth table0%0/3
A and B
A= $toB= $msg
ABresult
0X0
100
111
238 # Send to this user's socket.
239 0 my $recipient = $self->getSocket ($to);
240 0 $self->reply ($recipient,"21|$to|$msg");
241 }
242 }
243 }
244 elsif ($cmd == 30) {
245 # 30 = send public message.
246 0 ▶ if ($self->{conn}->{$id}->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"login"}) 0 0 0
247 0 my $msg = join ("|",@args);
248 0 $self->broadcast ("31|$self->{conn}->{$id}->{username}|$msg");
249 }
250 }
251 elsif ($cmd == 40) {
252 # 40 = client ready.
253 0 my $proto = join ("|",@args);
254 0 ▶ $proto = 0 unless length $proto > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $proto > 0 0 0 0
255  
256 # Client is ready now.
257 0 $self->{conn}->{$id}->{announce} = 1;
258 0 my @welcome = reverse (@{$self->{welcome}});
259 0 foreach my $send (@welcome) {
260 0 $self->reply ($socket,"40|1$send");
261 }
262 }
263 elsif ($cmd == 50) {
264 # 50 = Staff password.
265 0 my $pass = join ("|",@args);
266  
267 0 ▶ if (defined $self->{password} && $pass eq $self->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"password"} and $pass eq $self->{"password"}) 0 0 0
Condition0%0/3
defined $self->{"password"} and $pass eq $self->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"password"} and $pass eq $self->{"password"}
defined $self->{"password"} $pass eq $self->{"password"}
Truth table0%0/3
A and B
A= defined $self->{"password"}B= $pass eq $self->{"password"}
ABresult
0X0
100
111
268 # This is a staff member.
269 0 print "Make $id a Staff Connection\n";
270 0 $self->{conn}->{$id}->{level} = 1;
271 }
272 }
273 elsif ($cmd == 60) {
274 # 60 = Promote other users
275 0 my ($user,$newlevel) = @args;
276  
277 # Only admin users can use this option.
278 0 ▶ if ($self->{conn}->{$id}->{level} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'conn'}{$id}{'level'} == 1) { } 0 0 0
279 # See that the user they mentioned exists.
280 0 my $targetid = -1;
281 0 my $oldwho = '';
282 0 foreach my $con (keys %{$self->{conn}}) {
283 0 ▶ next unless exists $self->{conn}->{$con}->{nickname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"conn"}{$con}{"nickname"} 0 0 0
284 0 ▶ if ($self->{conn}->{$con}->{nickname} eq $user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$con}{"nickname"} eq $user) 0 0 0
285 0 $oldwho = $self->{conn}->{$con}->{username};
286 0 $targetid = $con;
287 }
288 }
289  
290 0 ▶ if ($targetid >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($targetid >= 0) { } 0 0 0
291 # They do. Promote them.
292 0 my $newwho = join ("",$newlevel,$user);
293 0 $self->{conn}->{$targetid}->{level} = $newlevel;
294 0 $self->{conn}->{$targetid}->{username} = $newwho;
295 0 $self->{who}->{$newwho} = delete $self->{who}->{$oldwho};
296 0 print "Promote $user ($targetid) to $newlevel\n";
297 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
298 0 print Dumper($self);
299  
300 # Send the Who List.
301 0 $self->sendWhoList();
302 }
303 else {
304 0 $self->reply ($socket,"21|3ChatClient|^1User not found.");
305 }
306 }
307 else {
308 0 $self->reply ($socket,"21|3ChatClient|^Permission denied.");
309 }
310 }
311 elsif ($cmd == 70) {
312 # 70 = ignore user
313 0 my $target = join ("|",@args);
314 0 ▶ if (length $target > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $target > 0) 0 0 0
315 # Send mutual ignore to this user's client.
316 0 my $recipient = $self->getSocket ($target);
317 0 $self->reply ($recipient,"70|$self->{conn}->{$id}->{username}");
318 }
319 }
320 else {
321 # Unknown command.
322 0 ▶ if ($self->{conn}->{$id}->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"login"}) 0 0 0
323 0 $self->reply ($socket,"21|3ChatClient|^1Command not implemented.");
324 }
325 }
326 }
327 }
328 }
329  
330 sub setWelcome {
331 0 ▶ my ($self,@msgs) = @_;
Subroutine setWelcome: not called CC=2 SCAR=17.9
Pod documented
332  
333 # Keep these messages.
334 0 ▶ return unless @msgs;
Branch: 1 branch
BranchTrueFalseTotal
unless @msgs 0 0 0
335  
336 0 $self->{welcome} = [ @msgs ];
337  
338 0 return 1;
339 }
340  
341 sub setPassword {
342 0 ▶ my ($self,$pass) = @_;
Subroutine setPassword: not called CC=3 SCAR=24.8
Pod documented
343  
344 # Save the password.
345 0 ▶ if (defined $pass) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pass) 0 0 0
346 0 ▶ if (length $pass > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pass > 0) { } 0 0 0
347 0 $self->{password} = $pass;
348 }
349 else {
350 0 $self->{password} = undef;
351 }
352 }
353  
354 0 return $self->{password};
355 }
356  
357 sub url {
358 0 ▶ my ($self) = @_;
Subroutine url: not called CC=1 SCAR=6.9
Pod documented
359  
360 0 return join (':', $self->{host}, $self->{port});
361 }
362  
363 sub sendWhoList {
364 0 ▶ my ($self) = @_;
Subroutine sendWhoList: not called CC=4 SCAR=30.0
Pod documented
365  
366 # Get the Who List.
367 0 my @memlist = ();
368 0 foreach my $member (keys %{$self->{who}}) {
369 0 my $addr = $self->{who}->{$member};
370 0 push (@memlist,"$member,$addr");
371 }
372 0 my $list = join ('|', @memlist);
373  
374 # Send the Who List to all connections.
375 0 foreach my $socket ($self->{select}->handles) {
376 0 ▶ next if ($socket == $self->{sock});
Branch: 1 branch
BranchTrueFalseTotal
if $socket == $self->{"sock"} 0 0 0
377  
378 # Send the 35.
379 0 $self->reply ($socket,"35|$list");
380 }
381  
382 0 return 1;
383 }
384  
385 sub getSocket {
386 0 ▶ my ($self,$handle) = @_;
Subroutine getSocket: not called CC=4 SCAR=30.0
Pod documented
387  
388 # Find this handle's socket.
389 0 foreach my $socket ($self->{select}->handles) {
390 0 my $id = $socket->fileno;
391 0 ▶ if (exists $self->{conn}->{$id}->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"conn"}{$id}{"username"}) 0 0 0
392 0 ▶ if ($handle eq $self->{conn}->{$id}->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle eq $self->{"conn"}{$id}{"username"}) 0 0 0
393 0 return $socket;
394 }
395 }
396 }
397  
398 0 return undef;
399 }
400  
401 sub broadcast {
402 0 ▶ my ($self,$data) = @_;
Subroutine broadcast: not called CC=3 SCAR=24.8
Pod documented
403  
404 # Find this handle's socket.
405 0 foreach my $socket ($self->{select}->handles) {
406 0 my $id = $socket->fileno;
407 0 ▶ if ($self->{conn}->{$id}->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conn"}{$id}{"login"}) 0 0 0
408 # Send it.
409 0 $self->reply ($socket,$data);
410 }
411 }
412 }
413  
414 1 1;
415 __END__
↑ Summary CyanChat.pm »