28
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*/
|
|
33 |
|
|
34 |
#include <QMessageBox>
|
|
35 |
#include "netclient.h"
|
|
36 |
|
|
37 |
HWNet::HWNet()
|
|
38 |
: QObject()
|
|
39 |
{
|
|
40 |
state = nsDisconnected;
|
|
41 |
IRCmsg_cmd_param = new QRegExp("^[A-Z]+ :.+$");
|
|
42 |
IRCmsg_number_param = new QRegExp("^:\\S+ [0-9]{3} .+$");
|
29
|
43 |
IRCmsg_who_cmd_param = new QRegExp("^:\\S+ [A-Z]+ \\S+");
|
|
44 |
IRCmsg_who_cmd_param_text = new QRegExp("^:\\S+ [A-Z]+ \\S+ :.+$");
|
|
45 |
isOp = false;
|
28
|
46 |
|
|
47 |
connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
48 |
connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
|
|
49 |
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
|
|
50 |
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
|
|
51 |
SLOT(displayError(QAbstractSocket::SocketError)));
|
|
52 |
}
|
|
53 |
|
|
54 |
void HWNet::ClientRead()
|
|
55 |
{
|
|
56 |
while (NetSocket.canReadLine())
|
|
57 |
{
|
|
58 |
ParseLine(NetSocket.readLine().trimmed());
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
void HWNet::displayError(QAbstractSocket::SocketError socketError)
|
|
63 |
{
|
|
64 |
switch (socketError)
|
|
65 |
{
|
|
66 |
case QAbstractSocket::RemoteHostClosedError:
|
|
67 |
break;
|
|
68 |
case QAbstractSocket::HostNotFoundError:
|
|
69 |
QMessageBox::information(0, tr("Error"),
|
|
70 |
tr("The host was not found. Please check the host name and port settings."));
|
|
71 |
break;
|
|
72 |
case QAbstractSocket::ConnectionRefusedError:
|
|
73 |
QMessageBox::information(0, tr("Error"),
|
|
74 |
tr("Connection refused"));
|
|
75 |
break;
|
|
76 |
default:
|
|
77 |
QMessageBox::information(0, tr("Error"),
|
|
78 |
NetSocket.errorString());
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
29
|
82 |
void HWNet::Connect(const QString & hostName, quint16 port, const QString & nick)
|
28
|
83 |
{
|
|
84 |
state = nsConnecting;
|
|
85 |
NetSocket.connectToHost(hostName, port);
|
29
|
86 |
mynick = nick;
|
|
87 |
opnick = "";
|
|
88 |
opCount = 0;
|
28
|
89 |
}
|
|
90 |
|
|
91 |
|
|
92 |
void HWNet::OnConnect()
|
|
93 |
{
|
|
94 |
state = nsConnected;
|
|
95 |
SendNet(QString("USER hwgame 1 2 Hedgewars game"));
|
29
|
96 |
SendNet(QString("NICK %1").arg(mynick));
|
28
|
97 |
}
|
|
98 |
|
|
99 |
void HWNet::OnDisconnect()
|
|
100 |
{
|
|
101 |
state = nsDisconnected;
|
|
102 |
}
|
|
103 |
|
|
104 |
void HWNet::Perform()
|
|
105 |
{
|
29
|
106 |
// SendNet(QString("LIST"));
|
|
107 |
SendNet(QString("JOIN #alex"));
|
28
|
108 |
}
|
|
109 |
|
|
110 |
void HWNet::Disconnect()
|
|
111 |
{
|
|
112 |
switch (state)
|
|
113 |
{
|
|
114 |
case nsDisconnected:
|
|
115 |
{
|
|
116 |
break;
|
|
117 |
}
|
|
118 |
case nsConnecting:
|
|
119 |
case nsQuitting:
|
|
120 |
{
|
|
121 |
NetSocket.disconnect();
|
|
122 |
break;
|
|
123 |
}
|
|
124 |
default:
|
|
125 |
{
|
|
126 |
state = nsQuitting;
|
|
127 |
SendNet(QString("QUIT :oops"));
|
|
128 |
}
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
void HWNet::SendNet(const QString & str)
|
|
133 |
{
|
|
134 |
SendNet(str.toLatin1());
|
|
135 |
}
|
|
136 |
|
|
137 |
void HWNet::SendNet(const QByteArray & buf)
|
|
138 |
{
|
|
139 |
if (buf.size() > 510) return;
|
|
140 |
NetSocket.write(buf);
|
|
141 |
NetSocket.write("\x0d\x0a", 2);
|
|
142 |
}
|
|
143 |
|
|
144 |
void HWNet::ParseLine(const QString & msg)
|
|
145 |
{
|
29
|
146 |
//QMessageBox::information(0, "", msg);
|
28
|
147 |
if (IRCmsg_cmd_param->exactMatch(msg))
|
|
148 |
{
|
|
149 |
msgcmd_paramHandler(msg);
|
|
150 |
} else
|
|
151 |
if (IRCmsg_number_param->exactMatch(msg))
|
|
152 |
{
|
|
153 |
msgnumber_paramHandler(msg);
|
29
|
154 |
} else
|
|
155 |
if (IRCmsg_who_cmd_param->exactMatch(msg))
|
|
156 |
{
|
|
157 |
msgwho_cmd_paramHandler(msg);
|
|
158 |
} else
|
|
159 |
if (IRCmsg_who_cmd_param_text->exactMatch(msg))
|
|
160 |
{
|
|
161 |
msgwho_cmd_param_textHandler(msg);
|
28
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
void HWNet::msgcmd_paramHandler(const QString & msg)
|
|
166 |
{
|
|
167 |
QStringList list = msg.split(" :");
|
|
168 |
if (list[0] == "PING")
|
|
169 |
{
|
|
170 |
SendNet(QString("PONG %1").arg(list[1]));
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
void HWNet::msgnumber_paramHandler(const QString & msg)
|
|
175 |
{
|
29
|
176 |
int pos = msg.indexOf(" :");
|
|
177 |
QString text = msg.mid(pos + 2);
|
|
178 |
QStringList list = msg.mid(0, pos).split(" ");
|
28
|
179 |
bool ok;
|
|
180 |
quint16 number = list[1].toInt(&ok);
|
|
181 |
if (!ok)
|
|
182 |
return ;
|
|
183 |
switch (number)
|
|
184 |
{
|
|
185 |
case 001 :
|
|
186 |
{
|
|
187 |
Perform();
|
|
188 |
emit Connected();
|
|
189 |
break;
|
|
190 |
}
|
29
|
191 |
case 322 : // RPL_LIST
|
28
|
192 |
{
|
|
193 |
emit AddGame(list[3]);
|
29
|
194 |
break;
|
|
195 |
}
|
|
196 |
case 353 : // RPL_NAMREPLY
|
|
197 |
{
|
|
198 |
QStringList ops = text.split(" ").filter(QRegExp("^@\\S+$"));
|
|
199 |
opCount += ops.size();
|
|
200 |
if (ops.size() == 1)
|
|
201 |
{
|
|
202 |
opnick = ops[0].mid(1);
|
|
203 |
}
|
|
204 |
break;
|
|
205 |
}
|
|
206 |
case 366 : // RPL_ENDOFNAMES
|
|
207 |
{
|
|
208 |
if (opCount != 1)
|
|
209 |
{
|
|
210 |
opnick = "";
|
|
211 |
}
|
|
212 |
SendNet(QString("PRIVMSG #alex :%1 ops here").arg(opCount));
|
|
213 |
opCount = 0;
|
|
214 |
break;
|
|
215 |
}
|
|
216 |
case 432 : // ERR_ERRONEUSNICKNAME
|
|
217 |
case 433 : // ERR_NICKNAMEINUSE
|
|
218 |
{
|
|
219 |
// ask for another nick
|
28
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|
29
|
223 |
|
|
224 |
void HWNet::msgwho_cmd_paramHandler(const QString & msg)
|
|
225 |
{
|
|
226 |
QStringList list = msg.split(" ");
|
|
227 |
QString who = list[0].mid(1).split("!")[0];
|
|
228 |
if (list[1] == "NICK")
|
|
229 |
{
|
|
230 |
if (mynick == who)
|
|
231 |
mynick = list[2];
|
|
232 |
if (opnick == who)
|
|
233 |
opnick = list[2];
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
void HWNet::msgwho_cmd_param_textHandler(const QString & msg)
|
|
238 |
{
|
|
239 |
int pos = msg.indexOf(" :");
|
|
240 |
QString text = msg.mid(pos + 2);
|
|
241 |
QStringList list = msg.mid(0, pos).split(" ");
|
|
242 |
QString who = list[0].mid(1).split("!")[0];
|
|
243 |
if (list[1] == "PRIVMSG")
|
|
244 |
{
|
|
245 |
SendNet(QString("PRIVMSG #alex :%1 said \"%2\" to %3").arg(who, text, list[2]));
|
|
246 |
}
|
|
247 |
}
|