Ticket #2267: SpidermonkeyLobbyCompatibility_v1.0.diff

File SpidermonkeyLobbyCompatibility_v1.0.diff, 37.2 KB (added by Yves, 10 years ago)
  • source/gui/scripting/ScriptFunctions.cpp

     
    2727#include "lib/timer.h"
    2828#include "lib/utf8.h"
    2929#include "lib/sysdep/sysdep.h"
    30 #include "lobby/IXmppClient.h"
    31 #include "lobby/sha.h"
     30#include "lobby/scripting/JSInterface_Lobby.h"
    3231#include "maths/FixedVector3D.h"
    3332#include "network/NetClient.h"
    3433#include "network/NetServer.h"
     
    613612    g_Game->GetTurnManager()->RewindTimeWarp();
    614613}
    615614
    616 /* Begin lobby related functions */
    617 
    618 bool HasXmppClient(void* UNUSED(cbdata))
    619 {
    620     return (g_XmppClient ? true : false);
    621 }
    622 
    623 #if CONFIG2_LOBBY
    624 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 iterations
    816 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 buffer
    826     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 buffer
    833     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 bits
    841         hex[i*2 + 1] = base16[encrypted[i] & 0x0F];// 4 low bits
    842     }
    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_LOBBY
    862 
    863 /* End lobby related functions */
    864 
    865615void QuickSave(void* UNUSED(cbdata))
    866616{
    867617    g_Game->GetTurnManager()->QuickSave();
     
    970720    scriptInterface.RegisterFunction<void, bool, &SetBoundingBoxDebugOverlay>("SetBoundingBoxDebugOverlay");
    971721
    972722    // Lobby functions
    973     scriptInterface.RegisterFunction<bool, &HasXmppClient>("HasXmppClient");
     723    scriptInterface.RegisterFunction<bool, &JSI_Lobby::HasXmppClient>("HasXmppClient");
    974724#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");
    1001751#endif // CONFIG2_LOBBY
    1002752}
  • 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
     29bool JSI_Lobby::HasXmppClient(void* UNUSED(cbdata))
     30{
     31    return (g_XmppClient ? true : false);
     32}
     33
     34#if CONFIG2_LOBBY
     35
     36void 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
     45void 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
     53void JSI_Lobby::StopXmppClient(void* UNUSED(cbdata))
     54{
     55    ENSURE(g_XmppClient);
     56    SAFE_DELETE(g_XmppClient);
     57    g_rankedGame = false;
     58}
     59
     60void JSI_Lobby::ConnectXmppClient(void* UNUSED(cbdata))
     61{
     62    ENSURE(g_XmppClient);
     63    g_XmppClient->connect();
     64}
     65
     66void JSI_Lobby::DisconnectXmppClient(void* UNUSED(cbdata))
     67{
     68    ENSURE(g_XmppClient);
     69    g_XmppClient->disconnect();
     70}
     71
     72void JSI_Lobby::RecvXmppClient(void* UNUSED(cbdata))
     73{
     74    if (!g_XmppClient)
     75        return;
     76    g_XmppClient->recv();
     77}
     78
     79void JSI_Lobby::SendGetGameList(void* UNUSED(cbdata))
     80{
     81    if (!g_XmppClient)
     82        return;
     83    g_XmppClient->SendIqGetGameList();
     84}
     85
     86void JSI_Lobby::SendGetBoardList(void* UNUSED(cbdata))
     87{
     88    if (!g_XmppClient)
     89        return;
     90    g_XmppClient->SendIqGetBoardList();
     91}
     92
     93void 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
     102void 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
     111void JSI_Lobby::SendUnregisterGame(void* UNUSED(cbdata))
     112{
     113    if (!g_XmppClient)
     114        return;
     115    g_XmppClient->SendIqUnregisterGame();
     116}
     117
     118void 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
     125CScriptVal 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
     136CScriptVal 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
     147CScriptVal 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
     158CScriptVal 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
     169void 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
     177void 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
     185void 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
     193std::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
     203void 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
     211void 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
     219std::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
     230std::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
     260std::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
     265bool JSI_Lobby::IsRankedGame(void* UNUSED(cbdata))
     266{
     267    return g_rankedGame;
     268}
     269
     270void 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
     24class ScriptInterface;
     25
     26namespace 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

     
    6060    return ret;
    6161}
    6262
    63 IXmppClient* IXmppClient::create(ScriptInterface& scriptInterface, const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt)
     63IXmppClient* IXmppClient::create(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, bool regOpt)
    6464{
    65     return new XmppClient(scriptInterface, sUsername, sPassword, sRoom, sNick, regOpt);
     65    return new XmppClient(sUsername, sPassword, sRoom, sNick, regOpt);
    6666}
    6767
    6868/**
    6969 * Construct the xmpp client
    7070 */
    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)
     71XmppClient::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)
    7373{
    7474    // Read lobby configuration from default.cfg
    7575    std::string sServer;
     
    149149        glooxwrapper::Tag::free(*it);
    150150}
    151151
    152 /// Game - script
    153 ScriptInterface& XmppClient::GetScriptInterface()
    154 {
    155     return m_ScriptInterface;
    156 }
    157 
    158152/// Network
    159153void XmppClient::connect()
    160154{
     
    289283 *
    290284 * @param data A JS array of game statistics
    291285 */
    292 void XmppClient::SendIqGameReport(CScriptVal data)
     286void XmppClient::SendIqGameReport(ScriptInterface& scriptInterface, CScriptVal data)
    293287{
    294288    glooxwrapper::JID xpartamuppJid(m_xpartamuppId);
    295289    jsval dataval = data.get();
     
    300294
    301295    // Iterate through all the properties reported and add them to the stanza.
    302296    std::vector<std::string> properties;
    303     m_ScriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties);
     297    scriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties);
    304298    for (std::vector<int>::size_type i = 0; i != properties.size(); i++)
    305299    {
    306300        std::wstring value;
    307         m_ScriptInterface.GetProperty(dataval, properties[i].c_str(), value);
     301        scriptInterface.GetProperty(dataval, properties[i].c_str(), value);
    308302        report->addAttribute(properties[i], utf8_from_wstring(value));
    309303    }
    310304
     
    323317 *
    324318 * @param data A JS array of game attributes
    325319 */
    326 void XmppClient::SendIqRegisterGame(CScriptVal data)
     320void XmppClient::SendIqRegisterGame(ScriptInterface& scriptInterface, CScriptVal data)
    327321{
    328322    glooxwrapper::JID xpartamuppJid(m_xpartamuppId);
    329323    jsval dataval = data.get();
     
    337331
    338332    // Iterate through all the properties reported and add them to the stanza.
    339333    std::vector<std::string> properties;
    340     m_ScriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties);
     334    scriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties);
    341335    for (std::vector<int>::size_type i = 0; i != properties.size(); i++)
    342336    {
    343337        std::wstring value;
    344         m_ScriptInterface.GetProperty(dataval, properties[i].c_str(), value);
     338        scriptInterface.GetProperty(dataval, properties[i].c_str(), value);
    345339        game->addAttribute(properties[i], utf8_from_wstring(value));
    346340    }
    347341
     
    462456 *
    463457 * @return A JS array containing all known players and their presences
    464458 */
    465 CScriptValRooted XmppClient::GUIGetPlayerList()
     459CScriptValRooted XmppClient::GUIGetPlayerList(ScriptInterface& scriptInterface)
    466460{
    467461    std::string presence;
    468462    CScriptValRooted playerList;
    469     m_ScriptInterface.Eval("({})", playerList);
     463    scriptInterface.Eval("({})", playerList);
    470464    for(std::map<std::string, gloox::Presence::PresenceType>::const_iterator it = m_PlayerMap.begin(); it != m_PlayerMap.end(); ++it)
    471465    {
    472466        CScriptValRooted player;
    473467        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));
    477471
    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);
    479473    }
    480474
    481475    return playerList;
     
    486480 *
    487481 * @return A JS array containing all known games
    488482 */
    489 CScriptValRooted XmppClient::GUIGetGameList()
     483CScriptValRooted XmppClient::GUIGetGameList(ScriptInterface& scriptInterface)
    490484{
    491485    CScriptValRooted gameList;
    492     m_ScriptInterface.Eval("([])", gameList);
     486    scriptInterface.Eval("([])", gameList);
    493487    for(std::vector<const glooxwrapper::Tag*>::const_iterator it = m_GameList.begin(); it != m_GameList.end(); ++it)
    494488    {
    495489        CScriptValRooted game;
    496         m_ScriptInterface.Eval("({})", game);
     490        scriptInterface.Eval("({})", game);
    497491
    498492        const char* stats[] = { "name", "ip", "state", "nbp", "tnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", "victoryCondition" };
    499493        short stats_length = 11;
    500494        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()));
    502496
    503         m_ScriptInterface.CallFunctionVoid(gameList.get(), "push", game);
     497        scriptInterface.CallFunctionVoid(gameList.get(), "push", game);
    504498    }
    505499
    506500    return gameList;
     
    511505 *
    512506 * @return A JS array containing all known leaderboard data
    513507 */
    514 CScriptValRooted XmppClient::GUIGetBoardList()
     508CScriptValRooted XmppClient::GUIGetBoardList(ScriptInterface& scriptInterface)
    515509{
    516510    CScriptValRooted boardList;
    517     m_ScriptInterface.Eval("([])", boardList);
     511    scriptInterface.Eval("([])", boardList);
    518512    for(std::vector<const glooxwrapper::Tag*>::const_iterator it = m_BoardList.begin(); it != m_BoardList.end(); ++it)
    519513    {
    520514        CScriptValRooted board;
    521         m_ScriptInterface.Eval("({})", board);
     515        scriptInterface.Eval("({})", board);
    522516
    523517        const char* attributes[] = { "name", "rank", "rating" };
    524518        short attributes_length = 3;
    525519        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()));
    527521
    528         m_ScriptInterface.CallFunctionVoid(boardList.get(), "push", board);
     522        scriptInterface.CallFunctionVoid(boardList.get(), "push", board);
    529523    }
    530524
    531525    return boardList;
     
    538532/**
    539533 * Send GUI message queue when queried.
    540534 */
    541 CScriptValRooted XmppClient::GuiPollMessage()
     535CScriptValRooted XmppClient::GuiPollMessage(ScriptInterface& scriptInterface)
    542536{
    543537    if (m_GuiMessageQueue.empty())
    544538        return CScriptValRooted();
    545539
    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   
    547556    m_GuiMessageQueue.pop_front();
    548     return r;
     557    return messageVal;
    549558}
    550559
    551560/**
     
    561570 *
    562571 * @param message Message to add to the queue
    563572 */
    564 void XmppClient::PushGuiMessage(const CScriptValRooted& message)
     573void XmppClient::PushGuiMessage(XmppClient::GUIMessage message)
    565574{
    566     ENSURE(!message.undefined());
    567575    m_GuiMessageQueue.push_back(message);
    568576}
    569577
     
    574582{
    575583    DbgXMPP(msg.from().resource() << " said " << msg.body());
    576584
    577     CScriptValRooted message;
    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());
    581589    PushGuiMessage(message);
    582590}
    583591
     
    589597    DbgXMPP("type " << msg.subtype() << ", subject " << msg.subject()
    590598      << ", message " << msg.body() << ", thread id " << msg.thread());
    591599
    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());
    596603    PushGuiMessage(message);
    597604}
    598605
     
    654661 */
    655662void XmppClient::CreateSimpleMessage(const std::string& type, const std::string& text, const std::string& level, const std::string& data)
    656663{
    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);
    663669    PushGuiMessage(message);
    664670}
    665671
  • source/lobby/XmppClient.h

     
    3939{
    4040    NONCOPYABLE(XmppClient);
    4141private:
    42     //Game - script
    43     ScriptInterface& m_ScriptInterface;
    4442    //Components
    4543    glooxwrapper::Client* m_client;
    4644    glooxwrapper::MUCRoom* m_mucRoom;
     
    5351
    5452public:
    5553    //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);
    5755    virtual ~XmppClient();
    5856
    5957    //Network
     
    6260    void recv();
    6361    void SendIqGetGameList();
    6462    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);
    6765    void SendIqUnregisterGame();
    6866    void SendIqChangeStateGame(const std::string& nbp, const std::string& players);
    6967    void SetNick(const std::string& nick);
     
    7371    void SetPresence(const std::string& presence);
    7472    void GetPresence(const std::string& nickname, std::string& presence);
    7573
    76     CScriptValRooted GUIGetPlayerList();
    77     CScriptValRooted GUIGetGameList();
    78     CScriptValRooted GUIGetBoardList();
     74    CScriptValRooted GUIGetPlayerList(ScriptInterface& scriptInterface);
     75    CScriptValRooted GUIGetGameList(ScriptInterface& scriptInterface);
     76    CScriptValRooted GUIGetBoardList(ScriptInterface& scriptInterface);
    7977
    8078    //Script
    8179    ScriptInterface& GetScriptInterface();
     
    120118    std::string StanzaErrorToString(gloox::StanzaError err);
    121119public:
    122120    /* 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);
    124131    void SendMUCMessage(const std::string& message);
    125132    protected:
    126     void PushGuiMessage(const CScriptValRooted& message);
     133    void PushGuiMessage(XmppClient::GUIMessage message);
    127134    void CreateSimpleMessage(const std::string& type, const std::string& text, const std::string& level = "standard", const std::string& data = "");
    128135
    129136private:
     
    134141    /// List of rankings
    135142    std::vector<const glooxwrapper::Tag*> m_BoardList;
    136143    /// Queue of messages
    137     std::deque<CScriptValRooted> m_GuiMessageQueue;
     144    std::deque<GUIMessage> m_GuiMessageQueue;
    138145};
    139146
    140147#endif // XMPPCLIENT_H
  • source/lobby/IXmppClient.h

     
    2525class IXmppClient
    2626{
    2727public:
    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);
    2929    virtual ~IXmppClient() {}
    3030
    3131    virtual void connect() = 0;
     
    3333    virtual void recv() = 0;
    3434    virtual void SendIqGetGameList() = 0;
    3535    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;
    3838    virtual void SendIqUnregisterGame() = 0;
    3939    virtual void SendIqChangeStateGame(const std::string& nbp, const std::string& players) = 0;
    4040    virtual void SetNick(const std::string& nick) = 0;
     
    4444    virtual void SetPresence(const std::string& presence) = 0;
    4545    virtual void GetPresence(const std::string& nickname, std::string& presence) = 0;
    4646
    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;
    5050
    51     virtual ScriptInterface& GetScriptInterface() = 0;
    52 
    53     virtual CScriptValRooted GuiPollMessage() = 0;
     51    virtual CScriptValRooted GuiPollMessage(ScriptInterface& scriptInterface) = 0;
    5452    virtual void SendMUCMessage(const std::string& message) = 0;
    5553};
    5654