22 #include <QCryptographicHash> |
22 #include <QCryptographicHash> |
23 |
23 |
24 #include "hwconsts.h" |
24 #include "hwconsts.h" |
25 #include "newnetclient.h" |
25 #include "newnetclient.h" |
26 #include "proto.h" |
26 #include "proto.h" |
27 #include "gameuiconfig.h" |
|
28 #include "game.h" |
27 #include "game.h" |
29 #include "gamecfgwidget.h" |
|
30 #include "teamselect.h" |
|
31 #include "misc.h" |
28 #include "misc.h" |
32 /* only to get the ignoreList from the chat widget */ |
29 /* only to get the ignoreList from the chat widget */ |
33 #include "hwform.h" |
30 #include "hwform.h" |
34 #include "pageroomslist.h" |
31 #include "pageroomslist.h" |
35 #include "chatwidget.h" |
32 #include "chatwidget.h" |
36 |
33 |
37 char delimeter='\n'; |
34 char delimeter='\n'; |
38 |
35 |
39 HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget) : |
36 HWNewNet::HWNewNet() : |
40 config(config), |
|
41 m_pGameCFGWidget(pGameCFGWidget), |
|
42 m_pTeamSelWidget(pTeamSelWidget), |
|
43 isChief(false), |
37 isChief(false), |
44 m_game_connected(false), |
38 m_game_connected(false), |
45 loginStep(0), |
39 loginStep(0), |
46 netClientState(0) |
40 netClientState(0) |
47 { |
41 { |
49 connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
43 connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
50 connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect())); |
44 connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect())); |
51 connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect())); |
45 connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect())); |
52 connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, |
46 connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, |
53 SLOT(displayError(QAbstractSocket::SocketError))); |
47 SLOT(displayError(QAbstractSocket::SocketError))); |
54 |
|
55 // config stuff |
|
56 connect(this, SIGNAL(paramChanged(const QString &, const QStringList &)), pGameCFGWidget, SLOT(setParam(const QString &, const QStringList &))); |
|
57 connect(pGameCFGWidget, SIGNAL(paramChanged(const QString &, const QStringList &)), this, SLOT(onParamChanged(const QString &, const QStringList &))); |
|
58 connect(this, SIGNAL(configAsked()), pGameCFGWidget, SLOT(fullNetConfig())); |
|
59 } |
48 } |
60 |
49 |
61 HWNewNet::~HWNewNet() |
50 HWNewNet::~HWNewNet() |
62 { |
51 { |
63 if (m_game_connected) |
52 if (m_game_connected) |
64 { |
53 { |
65 RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit")); |
54 RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit")); |
66 emit Disconnected(); |
55 emit Disconnected(tr("User quit")); |
67 } |
56 } |
68 NetSocket.flush(); |
57 NetSocket.flush(); |
69 } |
58 } |
70 |
59 |
71 void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick) |
60 void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick) |
72 { |
61 { |
73 mynick = nick; |
62 mynick = nick; |
74 while (mynick.isEmpty()) { |
|
75 mynick = QInputDialog::getText(m_pGameCFGWidget, |
|
76 QObject::tr("Nickname"), |
|
77 QObject::tr("Please enter your nickname"), |
|
78 QLineEdit::Normal, |
|
79 QDir::home().dirName()); |
|
80 config->setValue("net/nick",mynick); |
|
81 config->updNetNick(); |
|
82 } |
|
83 myhost = hostName + QString(":%1").arg(port); |
63 myhost = hostName + QString(":%1").arg(port); |
84 NetSocket.connectToHost(hostName, port); |
64 NetSocket.connectToHost(hostName, port); |
85 } |
65 } |
86 |
66 |
87 void HWNewNet::Disconnect() |
67 void HWNewNet::Disconnect() |
164 RawSendNet(str.toUtf8()); |
149 RawSendNet(str.toUtf8()); |
165 } |
150 } |
166 |
151 |
167 void HWNewNet::RawSendNet(const QByteArray & buf) |
152 void HWNewNet::RawSendNet(const QByteArray & buf) |
168 { |
153 { |
169 qDebug() << "Client: " << QString(buf).split("\n"); |
154 qDebug() << "Client: " << QString(buf).split("\n"); |
170 NetSocket.write(buf); |
155 NetSocket.write(buf); |
171 NetSocket.write("\n\n", 2); |
156 NetSocket.write("\n\n", 2); |
172 } |
157 } |
173 |
158 |
174 void HWNewNet::ClientRead() |
159 void HWNewNet::ClientRead() |
189 { |
174 { |
190 } |
175 } |
191 |
176 |
192 void HWNewNet::OnDisconnect() |
177 void HWNewNet::OnDisconnect() |
193 { |
178 { |
194 if(m_game_connected) emit Disconnected(); |
179 if(m_game_connected) emit Disconnected(""); |
195 m_game_connected = false; |
180 m_game_connected = false; |
196 } |
181 } |
197 |
182 |
198 void HWNewNet::displayError(QAbstractSocket::SocketError socketError) |
183 void HWNewNet::displayError(QAbstractSocket::SocketError socketError) |
199 { |
184 { |
200 emit Disconnected(); |
|
201 m_game_connected = false; |
185 m_game_connected = false; |
202 |
186 |
203 switch (socketError) { |
187 switch (socketError) { |
204 case QAbstractSocket::RemoteHostClosedError: |
188 case QAbstractSocket::RemoteHostClosedError: |
205 break; |
189 break; |
206 case QAbstractSocket::HostNotFoundError: |
190 case QAbstractSocket::HostNotFoundError: |
207 emit showMessage(tr("The host was not found. Please check the host name and port settings.")); |
191 emit Disconnected(tr("The host was not found. Please check the host name and port settings.")); |
208 break; |
192 break; |
209 case QAbstractSocket::ConnectionRefusedError: |
193 case QAbstractSocket::ConnectionRefusedError: |
210 emit showMessage(tr("Connection refused")); |
194 emit Disconnected(tr("Connection refused")); |
211 break; |
195 break; |
212 default: |
196 default: |
213 emit showMessage(NetSocket.errorString()); |
197 emit Disconnected(NetSocket.errorString()); |
214 } |
198 } |
|
199 } |
|
200 |
|
201 void HWNewNet::SendPasswordHash(const QString & hash) |
|
202 { |
|
203 RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash)); |
215 } |
204 } |
216 |
205 |
217 void HWNewNet::ParseCmd(const QStringList & lst) |
206 void HWNewNet::ParseCmd(const QStringList & lst) |
218 { |
207 { |
219 qDebug() << "Server: " << lst; |
208 qDebug() << "Server: " << lst; |
233 if (lst[0] == "PROTO") |
222 if (lst[0] == "PROTO") |
234 return ; |
223 return ; |
235 |
224 |
236 if (lst[0] == "ERROR") { |
225 if (lst[0] == "ERROR") { |
237 if (lst.size() == 2) |
226 if (lst.size() == 2) |
238 emit showMessage("Error: " + lst[1]); |
227 emit Error(lst[1]); |
239 else |
228 else |
240 emit showMessage("Unknown error"); |
229 emit Error("Unknown error"); |
241 return; |
230 return; |
242 } |
231 } |
243 |
232 |
244 if (lst[0] == "WARNING") { |
233 if (lst[0] == "WARNING") { |
245 if (lst.size() == 2) |
234 if (lst.size() == 2) |
246 emit showMessage("Warning: " + lst[1]); |
235 emit Warning(lst[1]); |
247 else |
236 else |
248 emit showMessage("Unknown warning"); |
237 emit Warning("Unknown warning"); |
249 return; |
238 return; |
250 } |
239 } |
251 |
240 |
252 if (lst[0] == "CONNECTED") { |
241 if (lst[0] == "CONNECTED") { |
253 if(lst.size() < 3 || lst[2].toInt() < cMinServerVersion) |
242 if(lst.size() < 3 || lst[2].toInt() < cMinServerVersion) |
381 if(lst.size() != 2) |
370 if(lst.size() != 2) |
382 { |
371 { |
383 qWarning("Net: Bad REMOVETEAM message"); |
372 qWarning("Net: Bad REMOVETEAM message"); |
384 return; |
373 return; |
385 } |
374 } |
386 m_pTeamSelWidget->removeNetTeam(HWTeam(lst[1])); |
375 emit RemoveNetTeam(HWTeam(lst[1])); |
387 return; |
376 return; |
388 } |
377 } |
389 |
378 |
390 if(lst[0] == "ROOMABANDONED") { |
379 if(lst[0] == "ROOMABANDONED") { |
391 netClientState = 2; |
380 netClientState = 2; |
392 emit showMessage(HWNewNet::tr("Room destroyed")); |
381 emit LeftRoom(tr("Room destroyed")); |
393 emit LeftRoom(); |
|
394 return; |
382 return; |
395 } |
383 } |
396 |
384 |
397 if(lst[0] == "KICKED") { |
385 if(lst[0] == "KICKED") { |
398 netClientState = 2; |
386 netClientState = 2; |
399 emit showMessage(HWNewNet::tr("You got kicked")); |
387 emit LeftRoom(tr("You got kicked")); |
400 emit LeftRoom(); |
|
401 return; |
388 return; |
402 } |
389 } |
403 |
390 |
404 if(lst[0] == "JOINED") { |
391 if(lst[0] == "JOINED") { |
405 if(lst.size() < 2) |
392 if(lst.size() < 2) |
416 emit EnteredGame(); |
403 emit EnteredGame(); |
417 emit roomMaster(isChief); |
404 emit roomMaster(isChief); |
418 if (isChief) |
405 if (isChief) |
419 emit configAsked(); |
406 emit configAsked(); |
420 } |
407 } |
|
408 // TODO reactivate |
|
409 /* |
421 if (lst[i] != mynick && isChief && config->Form->ui.pageRoomsList->chatWidget->ignoreList.contains(lst[i], Qt::CaseInsensitive) && !config->Form->ui.pageRoomsList->chatWidget->friendsList.contains(lst[i], Qt::CaseInsensitive)) |
410 if (lst[i] != mynick && isChief && config->Form->ui.pageRoomsList->chatWidget->ignoreList.contains(lst[i], Qt::CaseInsensitive) && !config->Form->ui.pageRoomsList->chatWidget->friendsList.contains(lst[i], Qt::CaseInsensitive)) |
422 { |
411 { |
423 kickPlayer(lst[i]); |
412 kickPlayer(lst[i]); |
424 } |
413 } |
425 else |
414 else |
|
415 */ |
426 { |
416 { |
427 emit nickAdded(lst[i], isChief && (lst[i] != mynick)); |
417 emit nickAdded(lst[i], isChief && (lst[i] != mynick)); |
428 emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i])); |
418 emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i])); |
429 } |
419 } |
430 } |
420 } |
496 emit AskForRunGame(); |
486 emit AskForRunGame(); |
497 return; |
487 return; |
498 } |
488 } |
499 |
489 |
500 if (lst[0] == "ASKPASSWORD") { |
490 if (lst[0] == "ASKPASSWORD") { |
501 bool ok = false; |
491 emit AskForPassword(mynick); |
502 int passLength = config->value("net/passwordlength", 0).toInt(); |
|
503 QString hash = config->value("net/passwordhash", "").toString(); |
|
504 |
|
505 // If the password is blank, ask the user to enter one in |
|
506 if (passLength == 0) |
|
507 { |
|
508 QString password = QInputDialog::getText(m_pGameCFGWidget, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok); |
|
509 |
|
510 if (!ok) { |
|
511 Disconnect(); |
|
512 emit Disconnected(); |
|
513 return; |
|
514 } |
|
515 |
|
516 hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex(); |
|
517 config->setValue("net/passwordhash", hash); |
|
518 config->setValue("net/passwordlength", password.size()); |
|
519 config->setNetPasswordLength(password.size()); |
|
520 } |
|
521 |
|
522 RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash)); |
|
523 return; |
492 return; |
524 } |
493 } |
525 |
494 |
526 if (lst[0] == "NOTICE") { |
495 if (lst[0] == "NOTICE") { |
527 if(lst.size() < 2) |
496 if(lst.size() < 2) |
614 qWarning("Net: Bad BYE message"); |
583 qWarning("Net: Bad BYE message"); |
615 return; |
584 return; |
616 } |
585 } |
617 if (lst[1] == "Authentication failed") |
586 if (lst[1] == "Authentication failed") |
618 { |
587 { |
619 // Set the password blank if case the user tries to join and enter his password again |
588 emit AuthFailed(); |
620 config->setValue("net/passwordlength", 0); |
589 } |
621 config->setNetPasswordLength(0); |
590 m_game_connected = false; |
622 } |
591 Disconnect(); |
623 emit showMessage(HWNewNet::tr("Quit reason: ") + lst[1]); |
592 emit Disconnected(lst[1]); |
624 return; |
593 return; |
625 } |
594 } |
626 |
595 |
627 |
596 |
628 if (lst[0] == "ADMIN_ACCESS") { |
597 if (lst[0] == "ADMIN_ACCESS") { |
631 } |
600 } |
632 |
601 |
633 if (lst[0] == "ROOM_CONTROL_ACCESS") { |
602 if (lst[0] == "ROOM_CONTROL_ACCESS") { |
634 if (lst.size() < 2) |
603 if (lst.size() < 2) |
635 { |
604 { |
636 qWarning("Net: Bad BYE message"); |
605 qWarning("Net: Bad ROOM_CONTROL_ACCESS message"); |
637 return; |
606 return; |
638 } |
607 } |
639 bool b = lst[1] != "0"; |
608 isChief = (lst[1] != "0"); |
640 m_pGameCFGWidget->setEnabled(b); |
|
641 m_pTeamSelWidget->setInteractivity(b); |
|
642 isChief = b; |
|
643 emit roomMaster(isChief); |
609 emit roomMaster(isChief); |
644 |
|
645 return; |
610 return; |
646 } |
611 } |
647 |
612 |
648 qWarning() << "Net: Unknown message:" << lst; |
613 qWarning() << "Net: Unknown message:" << lst; |
649 } |
614 } |