Ticket #2267: SpidermonkeyLobbyCompatibility_v1.0.diff
File SpidermonkeyLobbyCompatibility_v1.0.diff, 37.2 KB (added by , 10 years ago) |
---|
-
source/gui/scripting/ScriptFunctions.cpp
27 27 #include "lib/timer.h" 28 28 #include "lib/utf8.h" 29 29 #include "lib/sysdep/sysdep.h" 30 #include "lobby/IXmppClient.h" 31 #include "lobby/sha.h" 30 #include "lobby/scripting/JSInterface_Lobby.h" 32 31 #include "maths/FixedVector3D.h" 33 32 #include "network/NetClient.h" 34 33 #include "network/NetServer.h" … … 613 612 g_Game->GetTurnManager()->RewindTimeWarp(); 614 613 } 615 614 616 /* Begin lobby related functions */617 618 bool HasXmppClient(void* UNUSED(cbdata))619 {620 return (g_XmppClient ? true : false);621 }622 623 #if CONFIG2_LOBBY624 void StartXmppClient(void* cbdata, std::wstring username, std::wstring password, std::wstring room, std::wstring nick)625 {626 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);627 628 ENSURE(!g_XmppClient);629 630 g_XmppClient = IXmppClient::create(guiManager->GetScriptInterface(),631 utf8_from_wstring(username), utf8_from_wstring(password),632 utf8_from_wstring(room), utf8_from_wstring(nick));633 g_rankedGame = true;634 }635 636 void StartRegisterXmppClient(void* cbdata, std::wstring username, std::wstring password)637 {638 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);639 640 ENSURE(!g_XmppClient);641 642 g_XmppClient = IXmppClient::create(guiManager->GetScriptInterface(),643 utf8_from_wstring(username), utf8_from_wstring(password),644 "", "", true);645 }646 647 void StopXmppClient(void* UNUSED(cbdata))648 {649 ENSURE(g_XmppClient);650 SAFE_DELETE(g_XmppClient);651 g_rankedGame = false;652 }653 654 void ConnectXmppClient(void* UNUSED(cbdata))655 {656 ENSURE(g_XmppClient);657 g_XmppClient->connect();658 }659 660 void DisconnectXmppClient(void* UNUSED(cbdata))661 {662 ENSURE(g_XmppClient);663 g_XmppClient->disconnect();664 }665 666 void RecvXmppClient(void* UNUSED(cbdata))667 {668 if (!g_XmppClient)669 return;670 g_XmppClient->recv();671 }672 673 void SendGetGameList(void* UNUSED(cbdata))674 {675 if (!g_XmppClient)676 return;677 g_XmppClient->SendIqGetGameList();678 }679 680 void SendGetBoardList(void* UNUSED(cbdata))681 {682 if (!g_XmppClient)683 return;684 g_XmppClient->SendIqGetBoardList();685 }686 687 void SendGameReport(void* UNUSED(cbdata), CScriptVal data)688 {689 if (!g_XmppClient)690 return;691 g_XmppClient->SendIqGameReport(data);692 }693 694 void SendRegisterGame(void* UNUSED(cbdata), CScriptVal data)695 {696 if (!g_XmppClient)697 return;698 g_XmppClient->SendIqRegisterGame(data);699 }700 701 void SendUnregisterGame(void* UNUSED(cbdata))702 {703 if (!g_XmppClient)704 return;705 g_XmppClient->SendIqUnregisterGame();706 }707 708 void SendChangeStateGame(void* UNUSED(cbdata), std::wstring nbp, std::wstring players)709 {710 if (!g_XmppClient)711 return;712 g_XmppClient->SendIqChangeStateGame(utf8_from_wstring(nbp), utf8_from_wstring(players));713 }714 715 CScriptVal GetPlayerList(void* UNUSED(cbdata))716 {717 if (!g_XmppClient)718 return CScriptVal();719 720 CScriptValRooted playerList = g_XmppClient->GUIGetPlayerList();721 722 return playerList.get();723 }724 725 CScriptVal GetGameList(void* UNUSED(cbdata))726 {727 if (!g_XmppClient)728 return CScriptVal();729 730 CScriptValRooted gameList = g_XmppClient->GUIGetGameList();731 732 return gameList.get();733 }734 735 CScriptVal GetBoardList(void* UNUSED(cbdata))736 {737 if (!g_XmppClient)738 return CScriptVal();739 740 CScriptValRooted boardList = g_XmppClient->GUIGetBoardList();741 742 return boardList.get();743 }744 745 CScriptVal LobbyGuiPollMessage(void* UNUSED(cbdata))746 {747 if (!g_XmppClient)748 return CScriptVal();749 750 CScriptValRooted poll = g_XmppClient->GuiPollMessage();751 752 return poll.get();753 }754 755 void LobbySendMessage(void* UNUSED(cbdata), std::wstring message)756 {757 if (!g_XmppClient)758 return;759 760 g_XmppClient->SendMUCMessage(utf8_from_wstring(message));761 }762 763 void LobbySetPlayerPresence(void* UNUSED(cbdata), std::wstring presence)764 {765 if (!g_XmppClient)766 return;767 768 g_XmppClient->SetPresence(utf8_from_wstring(presence));769 }770 771 void LobbySetNick(void* UNUSED(cbdata), std::wstring nick)772 {773 if (!g_XmppClient)774 return;775 776 g_XmppClient->SetNick(utf8_from_wstring(nick));777 }778 779 std::wstring LobbyGetNick(void* UNUSED(cbdata))780 {781 if (!g_XmppClient)782 return L"";783 784 std::string nick;785 g_XmppClient->GetNick(nick);786 return wstring_from_utf8(nick);787 }788 789 void LobbyKick(void* UNUSED(cbdata), std::wstring nick, std::wstring reason)790 {791 if (!g_XmppClient)792 return;793 794 g_XmppClient->kick(utf8_from_wstring(nick), utf8_from_wstring(reason));795 }796 797 void LobbyBan(void* UNUSED(cbdata), std::wstring nick, std::wstring reason)798 {799 if (!g_XmppClient)800 return;801 802 g_XmppClient->ban(utf8_from_wstring(nick), utf8_from_wstring(reason));803 }804 805 std::wstring LobbyGetPlayerPresence(void* UNUSED(cbdata), std::wstring nickname)806 {807 if (!g_XmppClient)808 return L"";809 810 std::string presence;811 g_XmppClient->GetPresence(utf8_from_wstring(nickname), presence);812 return wstring_from_utf8(presence);813 }814 815 // Non-public secure PBKDF2 hash function with salting and 1,337 iterations816 static std::string EncryptPassword(const std::string& password, const std::string& username)817 {818 const int DIGESTSIZE = SHA_DIGEST_SIZE;819 const int ITERATIONS = 1337;820 821 static const byte salt_base[DIGESTSIZE] = {822 244, 243, 249, 244, 32, 33, 34, 35, 10, 11, 12, 13, 14, 15, 16, 17,823 18, 19, 20, 32, 33, 244, 224, 127, 129, 130, 140, 153, 133, 123, 234, 123 };824 825 // initialize the salt buffer826 byte salt_buffer[DIGESTSIZE] = {0};827 SHA256 hash;828 hash.update(salt_base, sizeof(salt_base));829 hash.update(username.c_str(), username.length());830 hash.finish(salt_buffer);831 832 // PBKDF2 to create the buffer833 byte encrypted[DIGESTSIZE];834 pbkdf2(encrypted, (byte*)password.c_str(), password.length(), salt_buffer, DIGESTSIZE, ITERATIONS);835 836 static const char base16[] = "0123456789ABCDEF";837 char hex[2 * DIGESTSIZE];838 for (int i = 0; i < DIGESTSIZE; ++i)839 {840 hex[i*2] = base16[encrypted[i] >> 4]; // 4 high bits841 hex[i*2 + 1] = base16[encrypted[i] & 0x0F];// 4 low bits842 }843 return std::string(hex, sizeof(hex));844 }845 846 // Public hash interface.847 std::wstring EncryptPassword(void* UNUSED(cbdata), std::wstring pass, std::wstring user)848 {849 return wstring_from_utf8(EncryptPassword(utf8_from_wstring(pass), utf8_from_wstring(user)));850 }851 852 bool IsRankedGame(void* UNUSED(cbdata))853 {854 return g_rankedGame;855 }856 857 void SetRankedGame(void* UNUSED(cbdata), bool isRanked)858 {859 g_rankedGame = isRanked;860 }861 #endif // CONFIG2_LOBBY862 863 /* End lobby related functions */864 865 615 void QuickSave(void* UNUSED(cbdata)) 866 616 { 867 617 g_Game->GetTurnManager()->QuickSave(); … … 970 720 scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay"); 971 721 972 722 // Lobby functions 973 scriptInterface.RegisterFunction<bool, & HasXmppClient>("HasXmppClient");723 scriptInterface.RegisterFunction<bool, &JSI_Lobby::HasXmppClient>("HasXmppClient"); 974 724 #if CONFIG2_LOBBY // Allow the lobby to be disabled 975 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, std::wstring, std::wstring, & StartXmppClient>("StartXmppClient");976 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, & StartRegisterXmppClient>("StartRegisterXmppClient");977 scriptInterface.RegisterFunction<void, & StopXmppClient>("StopXmppClient");978 scriptInterface.RegisterFunction<void, & ConnectXmppClient>("ConnectXmppClient");979 scriptInterface.RegisterFunction<void, & DisconnectXmppClient>("DisconnectXmppClient");980 scriptInterface.RegisterFunction<void, & RecvXmppClient>("RecvXmppClient");981 scriptInterface.RegisterFunction<void, & SendGetGameList>("SendGetGameList");982 scriptInterface.RegisterFunction<void, & SendGetBoardList>("SendGetBoardList");983 scriptInterface.RegisterFunction<void, CScriptVal, & SendRegisterGame>("SendRegisterGame");984 scriptInterface.RegisterFunction<void, CScriptVal, & SendGameReport>("SendGameReport");985 scriptInterface.RegisterFunction<void, & SendUnregisterGame>("SendUnregisterGame");986 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, & SendChangeStateGame>("SendChangeStateGame");987 scriptInterface.RegisterFunction<CScriptVal, & GetPlayerList>("GetPlayerList");988 scriptInterface.RegisterFunction<CScriptVal, & GetGameList>("GetGameList");989 scriptInterface.RegisterFunction<CScriptVal, & GetBoardList>("GetBoardList");990 scriptInterface.RegisterFunction<CScriptVal, & LobbyGuiPollMessage>("LobbyGuiPollMessage");991 scriptInterface.RegisterFunction<void, std::wstring, & LobbySendMessage>("LobbySendMessage");992 scriptInterface.RegisterFunction<void, std::wstring, & LobbySetPlayerPresence>("LobbySetPlayerPresence");993 scriptInterface.RegisterFunction<void, std::wstring, & LobbySetNick>("LobbySetNick");994 scriptInterface.RegisterFunction<std::wstring, & LobbyGetNick>("LobbyGetNick");995 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, & LobbyKick>("LobbyKick");996 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, & LobbyBan>("LobbyBan");997 scriptInterface.RegisterFunction<std::wstring, std::wstring, & LobbyGetPlayerPresence>("LobbyGetPlayerPresence");998 scriptInterface.RegisterFunction<std::wstring, std::wstring, std::wstring, & EncryptPassword>("EncryptPassword");999 scriptInterface.RegisterFunction<bool, & IsRankedGame>("IsRankedGame");1000 scriptInterface.RegisterFunction<void, bool, & SetRankedGame>("SetRankedGame");725 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, std::wstring, std::wstring, &JSI_Lobby::StartXmppClient>("StartXmppClient"); 726 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &JSI_Lobby::StartRegisterXmppClient>("StartRegisterXmppClient"); 727 scriptInterface.RegisterFunction<void, &JSI_Lobby::StopXmppClient>("StopXmppClient"); 728 scriptInterface.RegisterFunction<void, &JSI_Lobby::ConnectXmppClient>("ConnectXmppClient"); 729 scriptInterface.RegisterFunction<void, &JSI_Lobby::DisconnectXmppClient>("DisconnectXmppClient"); 730 scriptInterface.RegisterFunction<void, &JSI_Lobby::RecvXmppClient>("RecvXmppClient"); 731 scriptInterface.RegisterFunction<void, &JSI_Lobby::SendGetGameList>("SendGetGameList"); 732 scriptInterface.RegisterFunction<void, &JSI_Lobby::SendGetBoardList>("SendGetBoardList"); 733 scriptInterface.RegisterFunction<void, CScriptVal, &JSI_Lobby::SendRegisterGame>("SendRegisterGame"); 734 scriptInterface.RegisterFunction<void, CScriptVal, &JSI_Lobby::SendGameReport>("SendGameReport"); 735 scriptInterface.RegisterFunction<void, &JSI_Lobby::SendUnregisterGame>("SendUnregisterGame"); 736 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &JSI_Lobby::SendChangeStateGame>("SendChangeStateGame"); 737 scriptInterface.RegisterFunction<CScriptVal, &JSI_Lobby::GetPlayerList>("GetPlayerList"); 738 scriptInterface.RegisterFunction<CScriptVal, &JSI_Lobby::GetGameList>("GetGameList"); 739 scriptInterface.RegisterFunction<CScriptVal, &JSI_Lobby::GetBoardList>("GetBoardList"); 740 scriptInterface.RegisterFunction<CScriptVal, &JSI_Lobby::LobbyGuiPollMessage>("LobbyGuiPollMessage"); 741 scriptInterface.RegisterFunction<void, std::wstring, &JSI_Lobby::LobbySendMessage>("LobbySendMessage"); 742 scriptInterface.RegisterFunction<void, std::wstring, &JSI_Lobby::LobbySetPlayerPresence>("LobbySetPlayerPresence"); 743 scriptInterface.RegisterFunction<void, std::wstring, &JSI_Lobby::LobbySetNick>("LobbySetNick"); 744 scriptInterface.RegisterFunction<std::wstring, &JSI_Lobby::LobbyGetNick>("LobbyGetNick"); 745 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &JSI_Lobby::LobbyKick>("LobbyKick"); 746 scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &JSI_Lobby::LobbyBan>("LobbyBan"); 747 scriptInterface.RegisterFunction<std::wstring, std::wstring, &JSI_Lobby::LobbyGetPlayerPresence>("LobbyGetPlayerPresence"); 748 scriptInterface.RegisterFunction<std::wstring, std::wstring, std::wstring, &JSI_Lobby::EncryptPassword>("EncryptPassword"); 749 scriptInterface.RegisterFunction<bool, &JSI_Lobby::IsRankedGame>("IsRankedGame"); 750 scriptInterface.RegisterFunction<void, bool, &JSI_Lobby::SetRankedGame>("SetRankedGame"); 1001 751 #endif // CONFIG2_LOBBY 1002 752 } -
source/lobby/scripting/JSInterface_Lobby.cpp
1 /* Copyright (C) 2013 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "precompiled.h" 19 20 #include "JSInterface_Lobby.h" 21 22 #include "gui/GUIManager.h" 23 #include "lib/utf8.h" 24 #include "lobby/IXmppClient.h" 25 #include "lobby/sha.h" 26 27 #include "scriptinterface/ScriptInterface.h" 28 29 bool JSI_Lobby::HasXmppClient(void* UNUSED(cbdata)) 30 { 31 return (g_XmppClient ? true : false); 32 } 33 34 #if CONFIG2_LOBBY 35 36 void JSI_Lobby::StartXmppClient(void* UNUSED(cbdata), std::wstring username, std::wstring password, std::wstring room, std::wstring nick) 37 { 38 ENSURE(!g_XmppClient); 39 40 g_XmppClient = IXmppClient::create(utf8_from_wstring(username), utf8_from_wstring(password), 41 utf8_from_wstring(room), utf8_from_wstring(nick)); 42 g_rankedGame = true; 43 } 44 45 void JSI_Lobby::StartRegisterXmppClient(void* UNUSED(cbdata), std::wstring username, std::wstring password) 46 { 47 ENSURE(!g_XmppClient); 48 49 g_XmppClient = IXmppClient::create(utf8_from_wstring(username), utf8_from_wstring(password), 50 "", "", true); 51 } 52 53 void JSI_Lobby::StopXmppClient(void* UNUSED(cbdata)) 54 { 55 ENSURE(g_XmppClient); 56 SAFE_DELETE(g_XmppClient); 57 g_rankedGame = false; 58 } 59 60 void JSI_Lobby::ConnectXmppClient(void* UNUSED(cbdata)) 61 { 62 ENSURE(g_XmppClient); 63 g_XmppClient->connect(); 64 } 65 66 void JSI_Lobby::DisconnectXmppClient(void* UNUSED(cbdata)) 67 { 68 ENSURE(g_XmppClient); 69 g_XmppClient->disconnect(); 70 } 71 72 void JSI_Lobby::RecvXmppClient(void* UNUSED(cbdata)) 73 { 74 if (!g_XmppClient) 75 return; 76 g_XmppClient->recv(); 77 } 78 79 void JSI_Lobby::SendGetGameList(void* UNUSED(cbdata)) 80 { 81 if (!g_XmppClient) 82 return; 83 g_XmppClient->SendIqGetGameList(); 84 } 85 86 void JSI_Lobby::SendGetBoardList(void* UNUSED(cbdata)) 87 { 88 if (!g_XmppClient) 89 return; 90 g_XmppClient->SendIqGetBoardList(); 91 } 92 93 void JSI_Lobby::SendGameReport(void* cbdata, CScriptVal data) 94 { 95 if (!g_XmppClient) 96 return; 97 98 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 99 g_XmppClient->SendIqGameReport(guiManager->GetScriptInterface(), data); 100 } 101 102 void JSI_Lobby::SendRegisterGame(void* cbdata, CScriptVal data) 103 { 104 if (!g_XmppClient) 105 return; 106 107 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 108 g_XmppClient->SendIqRegisterGame(guiManager->GetScriptInterface(), data); 109 } 110 111 void JSI_Lobby::SendUnregisterGame(void* UNUSED(cbdata)) 112 { 113 if (!g_XmppClient) 114 return; 115 g_XmppClient->SendIqUnregisterGame(); 116 } 117 118 void JSI_Lobby::SendChangeStateGame(void* UNUSED(cbdata), std::wstring nbp, std::wstring players) 119 { 120 if (!g_XmppClient) 121 return; 122 g_XmppClient->SendIqChangeStateGame(utf8_from_wstring(nbp), utf8_from_wstring(players)); 123 } 124 125 CScriptVal JSI_Lobby::GetPlayerList(void* cbdata) 126 { 127 if (!g_XmppClient) 128 return CScriptVal(); 129 130 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 131 CScriptValRooted playerList = g_XmppClient->GUIGetPlayerList(guiManager->GetScriptInterface()); 132 133 return playerList.get(); 134 } 135 136 CScriptVal JSI_Lobby::GetGameList(void* cbdata) 137 { 138 if (!g_XmppClient) 139 return CScriptVal(); 140 141 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 142 CScriptValRooted gameList = g_XmppClient->GUIGetGameList(guiManager->GetScriptInterface()); 143 144 return gameList.get(); 145 } 146 147 CScriptVal JSI_Lobby::GetBoardList(void* cbdata) 148 { 149 if (!g_XmppClient) 150 return CScriptVal(); 151 152 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 153 CScriptValRooted boardList = g_XmppClient->GUIGetBoardList(guiManager->GetScriptInterface()); 154 155 return boardList.get(); 156 } 157 158 CScriptVal JSI_Lobby::LobbyGuiPollMessage(void* cbdata) 159 { 160 if (!g_XmppClient) 161 return CScriptVal(); 162 163 CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata); 164 CScriptValRooted poll = g_XmppClient->GuiPollMessage(guiManager->GetScriptInterface()); 165 166 return poll.get(); 167 } 168 169 void JSI_Lobby::LobbySendMessage(void* UNUSED(cbdata), std::wstring message) 170 { 171 if (!g_XmppClient) 172 return; 173 174 g_XmppClient->SendMUCMessage(utf8_from_wstring(message)); 175 } 176 177 void JSI_Lobby::LobbySetPlayerPresence(void* UNUSED(cbdata), std::wstring presence) 178 { 179 if (!g_XmppClient) 180 return; 181 182 g_XmppClient->SetPresence(utf8_from_wstring(presence)); 183 } 184 185 void JSI_Lobby::LobbySetNick(void* UNUSED(cbdata), std::wstring nick) 186 { 187 if (!g_XmppClient) 188 return; 189 190 g_XmppClient->SetNick(utf8_from_wstring(nick)); 191 } 192 193 std::wstring JSI_Lobby::LobbyGetNick(void* UNUSED(cbdata)) 194 { 195 if (!g_XmppClient) 196 return L""; 197 198 std::string nick; 199 g_XmppClient->GetNick(nick); 200 return wstring_from_utf8(nick); 201 } 202 203 void JSI_Lobby::LobbyKick(void* UNUSED(cbdata), std::wstring nick, std::wstring reason) 204 { 205 if (!g_XmppClient) 206 return; 207 208 g_XmppClient->kick(utf8_from_wstring(nick), utf8_from_wstring(reason)); 209 } 210 211 void JSI_Lobby::LobbyBan(void* UNUSED(cbdata), std::wstring nick, std::wstring reason) 212 { 213 if (!g_XmppClient) 214 return; 215 216 g_XmppClient->ban(utf8_from_wstring(nick), utf8_from_wstring(reason)); 217 } 218 219 std::wstring JSI_Lobby::LobbyGetPlayerPresence(void* UNUSED(cbdata), std::wstring nickname) 220 { 221 if (!g_XmppClient) 222 return L""; 223 224 std::string presence; 225 g_XmppClient->GetPresence(utf8_from_wstring(nickname), presence); 226 return wstring_from_utf8(presence); 227 } 228 229 // Non-public secure PBKDF2 hash function with salting and 1,337 iterations 230 std::string JSI_Lobby::EncryptPassword(const std::string& password, const std::string& username) 231 { 232 const int DIGESTSIZE = SHA_DIGEST_SIZE; 233 const int ITERATIONS = 1337; 234 235 static const byte salt_base[DIGESTSIZE] = { 236 244, 243, 249, 244, 32, 33, 34, 35, 10, 11, 12, 13, 14, 15, 16, 17, 237 18, 19, 20, 32, 33, 244, 224, 127, 129, 130, 140, 153, 133, 123, 234, 123 }; 238 239 // initialize the salt buffer 240 byte salt_buffer[DIGESTSIZE] = {0}; 241 SHA256 hash; 242 hash.update(salt_base, sizeof(salt_base)); 243 hash.update(username.c_str(), username.length()); 244 hash.finish(salt_buffer); 245 246 // PBKDF2 to create the buffer 247 byte encrypted[DIGESTSIZE]; 248 pbkdf2(encrypted, (byte*)password.c_str(), password.length(), salt_buffer, DIGESTSIZE, ITERATIONS); 249 250 static const char base16[] = "0123456789ABCDEF"; 251 char hex[2 * DIGESTSIZE]; 252 for (int i = 0; i < DIGESTSIZE; ++i) 253 { 254 hex[i*2] = base16[encrypted[i] >> 4]; // 4 high bits 255 hex[i*2 + 1] = base16[encrypted[i] & 0x0F];// 4 low bits 256 } 257 return std::string(hex, sizeof(hex)); 258 } 259 260 std::wstring JSI_Lobby::EncryptPassword(void* UNUSED(cbdata), std::wstring pass, std::wstring user) 261 { 262 return wstring_from_utf8(JSI_Lobby::EncryptPassword(utf8_from_wstring(pass), utf8_from_wstring(user))); 263 } 264 265 bool JSI_Lobby::IsRankedGame(void* UNUSED(cbdata)) 266 { 267 return g_rankedGame; 268 } 269 270 void JSI_Lobby::SetRankedGame(void* UNUSED(cbdata), bool isRanked) 271 { 272 g_rankedGame = isRanked; 273 } 274 275 #endif 276 Kein Zeilenumbruch am Ende der Datei -
source/lobby/scripting/JSInterface_Lobby.h
1 /* Copyright (C) 2013 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef INCLUDED_JSI_LOBBY 19 #define INCLUDED_JSI_LOBBY 20 21 #include "scriptinterface/ScriptVal.h" 22 #include "lib/config2.h" // for CONFIG2_LOBBY 23 24 class ScriptInterface; 25 26 namespace JSI_Lobby 27 { 28 bool HasXmppClient(void* cbdata); 29 30 #if CONFIG2_LOBBY 31 void StartXmppClient(void* cbdata, std::wstring username, std::wstring password, std::wstring room, std::wstring nick); 32 void StartRegisterXmppClient(void* cbdata, std::wstring username, std::wstring password); 33 void StopXmppClient(void* cbdata); 34 void ConnectXmppClient(void* cbdata); 35 void DisconnectXmppClient(void* cbdata); 36 void RecvXmppClient(void* cbdata); 37 void SendGetGameList(void* cbdata); 38 void SendGetBoardList(void* cbdata); 39 void SendGameReport(void* cbdata, CScriptVal data); 40 void SendRegisterGame(void* cbdata, CScriptVal data); 41 void SendUnregisterGame(void* cbdata); 42 void SendChangeStateGame(void* cbdata, std::wstring nbp, std::wstring players); 43 CScriptVal GetPlayerList(void* cbdata); 44 CScriptVal GetGameList(void* cbdata); 45 CScriptVal GetBoardList(void* cbdata); 46 CScriptVal LobbyGuiPollMessage(void* cbdata); 47 void LobbySendMessage(void* cbdata, std::wstring message); 48 void LobbySetPlayerPresence(void* cbdata, std::wstring presence); 49 void LobbySetNick(void* cbdata, std::wstring nick); 50 std::wstring LobbyGetNick(void* cbdata); 51 void LobbyKick(void* cbdata, std::wstring nick, std::wstring reason); 52 void LobbyBan(void* cbdata, std::wstring nick, std::wstring reason); 53 std::wstring LobbyGetPlayerPresence(void* cbdata, std::wstring nickname); 54 55 // Non-public secure PBKDF2 hash function with salting and 1,337 iterations 56 std::string EncryptPassword(const std::string& password, const std::string& username); 57 58 // Public hash interface. 59 std::wstring EncryptPassword(void* cbdata, std::wstring pass, std::wstring user); 60 61 bool IsRankedGame(void* cbdata); 62 void SetRankedGame(void* cbdata, bool isRanked); 63 #endif // CONFIG2_LOBBY 64 } 65 66 #endif 67 Kein Zeilenumbruch am Ende der Datei -
source/lobby/XmppClient.cpp
60 60 return ret; 61 61 } 62 62 63 IXmppClient* IXmppClient::create( ScriptInterface& scriptInterface,const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt)63 IXmppClient* IXmppClient::create(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt) 64 64 { 65 return new XmppClient(s criptInterface, sUsername, sPassword, sRoom, sNick, regOpt);65 return new XmppClient(sUsername, sPassword, sRoom, sNick, regOpt); 66 66 } 67 67 68 68 /** 69 69 * Construct the xmpp client 70 70 */ 71 XmppClient::XmppClient( ScriptInterface& scriptInterface,const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt)72 : m_ ScriptInterface(scriptInterface), m_client(NULL), m_mucRoom(NULL), m_registration(NULL), m_username(sUsername), m_password(sPassword), m_nick(sNick)71 XmppClient::XmppClient(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt) 72 : m_client(NULL), m_mucRoom(NULL), m_registration(NULL), m_username(sUsername), m_password(sPassword), m_nick(sNick) 73 73 { 74 74 // Read lobby configuration from default.cfg 75 75 std::string sServer; … … 149 149 glooxwrapper::Tag::free(*it); 150 150 } 151 151 152 /// Game - script153 ScriptInterface& XmppClient::GetScriptInterface()154 {155 return m_ScriptInterface;156 }157 158 152 /// Network 159 153 void XmppClient::connect() 160 154 { … … 289 283 * 290 284 * @param data A JS array of game statistics 291 285 */ 292 void XmppClient::SendIqGameReport( CScriptVal data)286 void XmppClient::SendIqGameReport(ScriptInterface& scriptInterface, CScriptVal data) 293 287 { 294 288 glooxwrapper::JID xpartamuppJid(m_xpartamuppId); 295 289 jsval dataval = data.get(); … … 300 294 301 295 // Iterate through all the properties reported and add them to the stanza. 302 296 std::vector<std::string> properties; 303 m_ScriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties);297 scriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties); 304 298 for (std::vector<int>::size_type i = 0; i != properties.size(); i++) 305 299 { 306 300 std::wstring value; 307 m_ScriptInterface.GetProperty(dataval, properties[i].c_str(), value);301 scriptInterface.GetProperty(dataval, properties[i].c_str(), value); 308 302 report->addAttribute(properties[i], utf8_from_wstring(value)); 309 303 } 310 304 … … 323 317 * 324 318 * @param data A JS array of game attributes 325 319 */ 326 void XmppClient::SendIqRegisterGame( CScriptVal data)320 void XmppClient::SendIqRegisterGame(ScriptInterface& scriptInterface, CScriptVal data) 327 321 { 328 322 glooxwrapper::JID xpartamuppJid(m_xpartamuppId); 329 323 jsval dataval = data.get(); … … 337 331 338 332 // Iterate through all the properties reported and add them to the stanza. 339 333 std::vector<std::string> properties; 340 m_ScriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties);334 scriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties); 341 335 for (std::vector<int>::size_type i = 0; i != properties.size(); i++) 342 336 { 343 337 std::wstring value; 344 m_ScriptInterface.GetProperty(dataval, properties[i].c_str(), value);338 scriptInterface.GetProperty(dataval, properties[i].c_str(), value); 345 339 game->addAttribute(properties[i], utf8_from_wstring(value)); 346 340 } 347 341 … … 462 456 * 463 457 * @return A JS array containing all known players and their presences 464 458 */ 465 CScriptValRooted XmppClient::GUIGetPlayerList( )459 CScriptValRooted XmppClient::GUIGetPlayerList(ScriptInterface& scriptInterface) 466 460 { 467 461 std::string presence; 468 462 CScriptValRooted playerList; 469 m_ScriptInterface.Eval("({})", playerList);463 scriptInterface.Eval("({})", playerList); 470 464 for(std::map<std::string, gloox::Presence::PresenceType>::const_iterator it = m_PlayerMap.begin(); it != m_PlayerMap.end(); ++it) 471 465 { 472 466 CScriptValRooted player; 473 467 GetPresenceString(it->second, presence); 474 m_ScriptInterface.Eval("({})", player);475 m_ScriptInterface.SetProperty(player.get(), "name", wstring_from_utf8(it->first));476 m_ScriptInterface.SetProperty(player.get(), "presence", wstring_from_utf8(presence));468 scriptInterface.Eval("({})", player); 469 scriptInterface.SetProperty(player.get(), "name", wstring_from_utf8(it->first)); 470 scriptInterface.SetProperty(player.get(), "presence", wstring_from_utf8(presence)); 477 471 478 m_ScriptInterface.SetProperty(playerList.get(), wstring_from_utf8(it->first).c_str(), player);472 scriptInterface.SetProperty(playerList.get(), wstring_from_utf8(it->first).c_str(), player); 479 473 } 480 474 481 475 return playerList; … … 486 480 * 487 481 * @return A JS array containing all known games 488 482 */ 489 CScriptValRooted XmppClient::GUIGetGameList( )483 CScriptValRooted XmppClient::GUIGetGameList(ScriptInterface& scriptInterface) 490 484 { 491 485 CScriptValRooted gameList; 492 m_ScriptInterface.Eval("([])", gameList);486 scriptInterface.Eval("([])", gameList); 493 487 for(std::vector<const glooxwrapper::Tag*>::const_iterator it = m_GameList.begin(); it != m_GameList.end(); ++it) 494 488 { 495 489 CScriptValRooted game; 496 m_ScriptInterface.Eval("({})", game);490 scriptInterface.Eval("({})", game); 497 491 498 492 const char* stats[] = { "name", "ip", "state", "nbp", "tnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", "victoryCondition" }; 499 493 short stats_length = 11; 500 494 for (short i = 0; i < stats_length; i++) 501 m_ScriptInterface.SetProperty(game.get(), stats[i], wstring_from_utf8((*it)->findAttribute(stats[i]).to_string()));495 scriptInterface.SetProperty(game.get(), stats[i], wstring_from_utf8((*it)->findAttribute(stats[i]).to_string())); 502 496 503 m_ScriptInterface.CallFunctionVoid(gameList.get(), "push", game);497 scriptInterface.CallFunctionVoid(gameList.get(), "push", game); 504 498 } 505 499 506 500 return gameList; … … 511 505 * 512 506 * @return A JS array containing all known leaderboard data 513 507 */ 514 CScriptValRooted XmppClient::GUIGetBoardList( )508 CScriptValRooted XmppClient::GUIGetBoardList(ScriptInterface& scriptInterface) 515 509 { 516 510 CScriptValRooted boardList; 517 m_ScriptInterface.Eval("([])", boardList);511 scriptInterface.Eval("([])", boardList); 518 512 for(std::vector<const glooxwrapper::Tag*>::const_iterator it = m_BoardList.begin(); it != m_BoardList.end(); ++it) 519 513 { 520 514 CScriptValRooted board; 521 m_ScriptInterface.Eval("({})", board);515 scriptInterface.Eval("({})", board); 522 516 523 517 const char* attributes[] = { "name", "rank", "rating" }; 524 518 short attributes_length = 3; 525 519 for (short i = 0; i < attributes_length; i++) 526 m_ScriptInterface.SetProperty(board.get(), attributes[i], wstring_from_utf8((*it)->findAttribute(attributes[i]).to_string()));520 scriptInterface.SetProperty(board.get(), attributes[i], wstring_from_utf8((*it)->findAttribute(attributes[i]).to_string())); 527 521 528 m_ScriptInterface.CallFunctionVoid(boardList.get(), "push", board);522 scriptInterface.CallFunctionVoid(boardList.get(), "push", board); 529 523 } 530 524 531 525 return boardList; … … 538 532 /** 539 533 * Send GUI message queue when queried. 540 534 */ 541 CScriptValRooted XmppClient::GuiPollMessage( )535 CScriptValRooted XmppClient::GuiPollMessage(ScriptInterface& scriptInterface) 542 536 { 543 537 if (m_GuiMessageQueue.empty()) 544 538 return CScriptValRooted(); 545 539 546 CScriptValRooted r = m_GuiMessageQueue.front(); 540 GUIMessage message = m_GuiMessageQueue.front(); 541 CScriptValRooted messageVal; 542 543 scriptInterface.Eval("({})", messageVal); 544 scriptInterface.SetProperty(messageVal.get(), "type", message.type); 545 if (!message.from.empty()) 546 scriptInterface.SetProperty(messageVal.get(), "from", message.from); 547 if (!message.text.empty()) 548 scriptInterface.SetProperty(messageVal.get(), "text", message.text); 549 if (!message.level.empty()) 550 scriptInterface.SetProperty(messageVal.get(), "level", message.level); 551 if (!message.message.empty()) 552 scriptInterface.SetProperty(messageVal.get(), "message", message.message); 553 if (!message.data.empty()) 554 scriptInterface.SetProperty(messageVal.get(), "data", message.data); 555 547 556 m_GuiMessageQueue.pop_front(); 548 return r;557 return messageVal; 549 558 } 550 559 551 560 /** … … 561 570 * 562 571 * @param message Message to add to the queue 563 572 */ 564 void XmppClient::PushGuiMessage( const CScriptValRooted&message)573 void XmppClient::PushGuiMessage(XmppClient::GUIMessage message) 565 574 { 566 ENSURE(!message.undefined());567 575 m_GuiMessageQueue.push_back(message); 568 576 } 569 577 … … 574 582 { 575 583 DbgXMPP(msg.from().resource() << " said " << msg.body()); 576 584 577 CScriptValRootedmessage;578 m _ScriptInterface.Eval("({ 'type':'mucmessage'})", message);579 m _ScriptInterface.SetProperty(message.get(), "from", wstring_from_utf8(msg.from().resource().to_string()));580 m _ScriptInterface.SetProperty(message.get(), "text", wstring_from_utf8(msg.body().to_string()));585 GUIMessage message; 586 message.type = L"mucmessage"; 587 message.from = wstring_from_utf8(msg.from().resource().to_string()); 588 message.text = wstring_from_utf8(msg.body().to_string()); 581 589 PushGuiMessage(message); 582 590 } 583 591 … … 589 597 DbgXMPP("type " << msg.subtype() << ", subject " << msg.subject() 590 598 << ", message " << msg.body() << ", thread id " << msg.thread()); 591 599 592 CScriptValRooted message; 593 m_ScriptInterface.Eval("({'type':'message'})", message); 594 m_ScriptInterface.SetProperty(message.get(), "from", wstring_from_utf8(msg.from().username().to_string())); 595 m_ScriptInterface.SetProperty(message.get(), "text", wstring_from_utf8(msg.body().to_string())); 600 GUIMessage message; 601 message.from = wstring_from_utf8(msg.from().username().to_string()); 602 message.message = wstring_from_utf8(msg.body().to_string()); 596 603 PushGuiMessage(message); 597 604 } 598 605 … … 654 661 */ 655 662 void XmppClient::CreateSimpleMessage(const std::string& type, const std::string& text, const std::string& level, const std::string& data) 656 663 { 657 CScriptValRooted message; 658 m_ScriptInterface.Eval("({})", message); 659 m_ScriptInterface.SetProperty(message.get(), "type", wstring_from_utf8(type)); 660 m_ScriptInterface.SetProperty(message.get(), "level", wstring_from_utf8(level)); 661 m_ScriptInterface.SetProperty(message.get(), "text", wstring_from_utf8(text)); 662 m_ScriptInterface.SetProperty(message.get(), "data", wstring_from_utf8(data)); 664 GUIMessage message; 665 message.type = wstring_from_utf8(type); 666 message.level = wstring_from_utf8(level); 667 message.text = wstring_from_utf8(text); 668 message.data = wstring_from_utf8(data); 663 669 PushGuiMessage(message); 664 670 } 665 671 -
source/lobby/XmppClient.h
39 39 { 40 40 NONCOPYABLE(XmppClient); 41 41 private: 42 //Game - script43 ScriptInterface& m_ScriptInterface;44 42 //Components 45 43 glooxwrapper::Client* m_client; 46 44 glooxwrapper::MUCRoom* m_mucRoom; … … 53 51 54 52 public: 55 53 //Basic 56 XmppClient( ScriptInterface& scriptInterface,const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt = false);54 XmppClient(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt = false); 57 55 virtual ~XmppClient(); 58 56 59 57 //Network … … 62 60 void recv(); 63 61 void SendIqGetGameList(); 64 62 void SendIqGetBoardList(); 65 void SendIqGameReport( CScriptVal data);66 void SendIqRegisterGame( CScriptVal data);63 void SendIqGameReport(ScriptInterface& scriptInterface, CScriptVal data); 64 void SendIqRegisterGame(ScriptInterface& scriptInterface, CScriptVal data); 67 65 void SendIqUnregisterGame(); 68 66 void SendIqChangeStateGame(const std::string& nbp, const std::string& players); 69 67 void SetNick(const std::string& nick); … … 73 71 void SetPresence(const std::string& presence); 74 72 void GetPresence(const std::string& nickname, std::string& presence); 75 73 76 CScriptValRooted GUIGetPlayerList( );77 CScriptValRooted GUIGetGameList( );78 CScriptValRooted GUIGetBoardList( );74 CScriptValRooted GUIGetPlayerList(ScriptInterface& scriptInterface); 75 CScriptValRooted GUIGetGameList(ScriptInterface& scriptInterface); 76 CScriptValRooted GUIGetBoardList(ScriptInterface& scriptInterface); 79 77 80 78 //Script 81 79 ScriptInterface& GetScriptInterface(); … … 120 118 std::string StanzaErrorToString(gloox::StanzaError err); 121 119 public: 122 120 /* Messages */ 123 CScriptValRooted GuiPollMessage(); 121 struct GUIMessage 122 { 123 std::wstring type; 124 std::wstring level; 125 std::wstring text; 126 std::wstring data; 127 std::wstring from; 128 std::wstring message; 129 }; 130 CScriptValRooted GuiPollMessage(ScriptInterface& scriptInterface); 124 131 void SendMUCMessage(const std::string& message); 125 132 protected: 126 void PushGuiMessage( const CScriptValRooted&message);133 void PushGuiMessage(XmppClient::GUIMessage message); 127 134 void CreateSimpleMessage(const std::string& type, const std::string& text, const std::string& level = "standard", const std::string& data = ""); 128 135 129 136 private: … … 134 141 /// List of rankings 135 142 std::vector<const glooxwrapper::Tag*> m_BoardList; 136 143 /// Queue of messages 137 std::deque< CScriptValRooted> m_GuiMessageQueue;144 std::deque<GUIMessage> m_GuiMessageQueue; 138 145 }; 139 146 140 147 #endif // XMPPCLIENT_H -
source/lobby/IXmppClient.h
25 25 class IXmppClient 26 26 { 27 27 public: 28 static IXmppClient* create( ScriptInterface& scriptInterface,const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt = false);28 static IXmppClient* create(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt = false); 29 29 virtual ~IXmppClient() {} 30 30 31 31 virtual void connect() = 0; … … 33 33 virtual void recv() = 0; 34 34 virtual void SendIqGetGameList() = 0; 35 35 virtual void SendIqGetBoardList() = 0; 36 virtual void SendIqGameReport( CScriptVal data) = 0;37 virtual void SendIqRegisterGame( CScriptVal data) = 0;36 virtual void SendIqGameReport(ScriptInterface& scriptInterface, CScriptVal data) = 0; 37 virtual void SendIqRegisterGame(ScriptInterface& scriptInterface, CScriptVal data) = 0; 38 38 virtual void SendIqUnregisterGame() = 0; 39 39 virtual void SendIqChangeStateGame(const std::string& nbp, const std::string& players) = 0; 40 40 virtual void SetNick(const std::string& nick) = 0; … … 44 44 virtual void SetPresence(const std::string& presence) = 0; 45 45 virtual void GetPresence(const std::string& nickname, std::string& presence) = 0; 46 46 47 virtual CScriptValRooted GUIGetPlayerList( ) = 0;48 virtual CScriptValRooted GUIGetGameList( ) = 0;49 virtual CScriptValRooted GUIGetBoardList( ) = 0;47 virtual CScriptValRooted GUIGetPlayerList(ScriptInterface& scriptInterface) = 0; 48 virtual CScriptValRooted GUIGetGameList(ScriptInterface& scriptInterface) = 0; 49 virtual CScriptValRooted GUIGetBoardList(ScriptInterface& scriptInterface) = 0; 50 50 51 virtual ScriptInterface& GetScriptInterface() = 0; 52 53 virtual CScriptValRooted GuiPollMessage() = 0; 51 virtual CScriptValRooted GuiPollMessage(ScriptInterface& scriptInterface) = 0; 54 52 virtual void SendMUCMessage(const std::string& message) = 0; 55 53 }; 56 54