summaryrefslogtreecommitdiffstats
path: root/scripts/irc_chess.pl
blob: a8f8ef5bfe75b40ad5a7b03c50f2f652c1231c33 (plain)
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
#
#Irssi script to complement chess backend server
#
use strict;
use Irssi;
use Irssi::Irc;
use IO::Socket;
use vars qw($VERSION %IRSSI);

$VERSION="0.1";
%IRSSI =
(
	authors     => "kodgehopper (kodgehopper\@netscape.net)",
	contact     => "kodgehopper\@netscape.net",
	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
{
	$_=$_[0];

	#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;

		$foreground=~s/<bBLACK>/\\cc1/g;
		$foreground=~s/<bWHITE>/\\cc0/g;

		$background=~s/<BBLUE>/,2/g;
		$background=~s/<BYELLOW>/,7/g;

		my $result=$foreground.$background;

		s/$orig/$result/;

	}

	#replace background-only colors
	s/<BBLUE>/\\cc0,2/g;
	s/<BYELLOW>/\\cc0,7/g;

	#replace rest of colors
	s/<NORMAL>/\\co/g;

	return $_;
}#processColors

#
#message formats:
#1. simple format:
#[username]Message
#
#2. complex format:
#[user1]msg1<:=:>[user2]msg2<:=:>commonMessage
#
sub processMsgFromServer 
{
	my ($server, $msg, $nick)=@_;
	my $delimiter="<:=:>";
	$_=$msg;

	#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*\[(.+?)\](.*?)$/;

		$message=processColors($message);

		#send message to player
		$server->command("eval msg $nick $message");
	}
	else
	{
		#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++)
		{
			$commonMessageList[$j]=processColors($commonMessageList[$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
}#processOutput

#
#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");
	$msg=lc($msg);	

	if ($msg=~/^game\b/)
	{
		$msg = $msg." $nick";
		return $msg;
	}
	else
	{
		Irssi::print("sending: msg $nick Error: Invalid Message");
		$server->command("msg $nick Error: Invalid Message");
		return "INVALID";	
	}
}#processMsgFromClient

#
#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");
		send(SOCKET,$msgToSend,0);
		Irssi::print("Waiting for message from server\n");
		my $buffer;
		recv(SOCKET,$buffer,32678,0); #read a max of 32k. 
		processMsgFromServer($server, $buffer, $nick);
	}
}#sig_processPvt

#
#function to terminate game. it basically just closes 
#the connection to the server
#
sub cmd_endGame
{
	shutdown(SOCKET,2);
	close(SOCKET);
	Irssi::print("Game ended. Socket shut down");
	$gameRunning=0;
}#cmd_endGame

BEGIN
{
	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, 127.0.0.1); 
	connect(SOCKET, $internetPackedAddress) or die("connect: $!");

	Irssi::print("Game is now running");
	$gameRunning=1;
}


Irssi::signal_add("message private","sig_processPvt");
Irssi::command_bind("end_game", "cmd_endGame");