Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
216 lines (174 sloc) 4.86 KB
#Irssi script to complement chess backend server
use strict;
use Irssi;
use Irssi::Irc;
use IO::Socket;
use vars qw($VERSION %IRSSI);
authors => "kodgehopper (kodgehopper\",
contact => "kodgehopper\",
name => "IRC-Chess",
description => "Chess server for IRC. Allows for multiple 2-player games to be played simultaneously",
license => "GNU GPL",
url => "none as yet",
my $gameRunning=0;
sub processColors
#replace foreground/background colors
my @fgbg;
my $numFgBg=(@fgbg=/(<B\w+?><b\w+?>)/g);
for (my $j=0; $j < $numFgBg; $j++)
my $t=$fgbg[$j];
my ($background, $foreground)=($t=~/(<B\w+?>)(<b\w+?>)/);
my $orig=$background.$foreground;
my $result=$foreground.$background;
#replace background-only colors
#replace rest of colors
return $_;
#message formats:
#1. simple format:
#2. complex format:
sub processMsgFromServer
my ($server, $msg, $nick)=@_;
my $delimiter="<:=:>";
#determine the type of message from the number of delimiters
my $numDelims=(my @list=/$delimiter/g);
if ($numDelims==0)
#simple message
my ($username, $message)=/^\s*\[(.+?)\](.*?)$/;
#send message to player
$server->command("eval msg $nick $message");
#complex message
my ($user1, $msg1, $user2, $msg2, $commonMessage)=/^\s*\[(.+?)\](.*?)$delimiter\[(.+?)\](.*?)$delimiter(.*)$/s;
#split message into seperate lines
my @commonMessageList=split(/\n/, $commonMessage);
#send common message to both users
Irssi::print("Sending common message to both users");
my $numStrings;
my @list;
#print out blank lines since the string was split on newlines so
#now they're lost. an extra space == blank line
$server->command("eval msg $user1 \\co");
$server->command("eval msg $user2 \\co");
my $commonListSize=@commonMessageList;
for (my $j=0; $j<$commonListSize; $j++)
$server->command("eval msg $user1 $commonMessageList[$j]");
$server->command("eval msg $user2 $commonMessageList[$j]");
$server->command("eval msg $user1 \\co");
$server->command("eval msg $user2 \\co");
#send messages for each user
my @msg1List=split(/\n/, $msg1);
my $msg1ListSize=@msg1List;
for (my $j=0; $j<$msg1ListSize; $j++)
$server->command("eval msg $user1 \\cb$msg1List[$j]\\co");
my @msg2List=split(/\n/, $msg2);
my $msg2ListSize=@msg2List;
for (my $j=0; $j<$msg2ListSize; $j++)
$server->command("eval msg $user2 \\cb$msg2List[$j]\\co");
} #else
#process a message received from the user.
#this will be something like "game start k"
#this will have to be changed to something like
#"game start k k1", where k1 is the user.
#if the format of the message is not correct,
#return INVALID. format checking is minimal.
#basically, if the first word is "game", then
#slap on the nickname and send it to the server.
sub processMsgFromClient
my ($server, $msg, $nick)=@_;
#Irssi::print("msg from client:\n$msg\n");
if ($msg=~/^game\b/)
$msg = $msg." $nick";
return $msg;
Irssi::print("sending: msg $nick Error: Invalid Message");
$server->command("msg $nick Error: Invalid Message");
return "INVALID";
#private messages received from other users eg. if they want to
#register a new game
sub sig_processPvt
my($server, $msg, $nick, $address)=@_;
my $msgToSend=processMsgFromClient($server, $msg, $nick);
if ($msgToSend !~ /^INVALID$/)
Irssi::print("Sending message now");
Irssi::print("Waiting for message from server\n");
my $buffer;
recv(SOCKET,$buffer,32678,0); #read a max of 32k.
processMsgFromServer($server, $buffer, $nick);
#function to terminate game. it basically just closes
#the connection to the server
sub cmd_endGame
Irssi::print("Game ended. Socket shut down");
my $PORT=1234;
Irssi::print("connecting to server\n");
my $tcpProtocolNumber = getprotobyname('tcp') || 6;
socket(SOCKET, PF_INET(), SOCK_STREAM(), $tcpProtocolNumber)
or die("socket: $!");
my $internetPackedAddress = pack('S na4 x8', AF_INET(), $PORT,;
connect(SOCKET, $internetPackedAddress) or die("connect: $!");
Irssi::print("Game is now running");
Irssi::signal_add("message private","sig_processPvt");
Irssi::command_bind("end_game", "cmd_endGame");
You can’t perform that action at this time.