Ticket #3832: t3832-post-revert.patch

File t3832-post-revert.patch, 9.1 KB (added by Itms, 8 years ago)
  • binaries/data/mods/public/gui/lobby/lobby.js

     
    116116            updateLeaderboard();
    117117            updatePlayerList();
    118118            Engine.GetGUIObjectByName("hostButton").enabled = false;
    119             addChatMessage({ "from": "system", "text": translate("Disconnected.") + msg.text, "color": g_SystemColor });
     119            addChatMessage({
     120                "from": "system",
     121                "text": translate("Disconnected.") + msg.text,
     122                "color": g_SystemColor,
     123                "time": msg.time
     124            });
    120125        },
    121126        "error": msg => {
    122             addChatMessage({ "from": "system", "text": msg.text, "color": g_SystemColor });
     127            addChatMessage({
     128                "from": "system",
     129                "text": msg.text,
     130                "color": g_SystemColor,
     131                "time": msg.time
     132            });
    123133        }
    124134    },
    125135    "chat": {
     
    129139        "join": msg => {
    130140            addChatMessage({
    131141                "text": "/special " + sprintf(translate("%(nick)s has joined."), { "nick": msg.text }),
    132                 "isSpecial": true
     142                "isSpecial": true,
     143                "time": msg.time
    133144            });
    134145            Engine.SendGetRatingList();
    135146        },
     
    136147        "leave": msg => {
    137148            addChatMessage({
    138149                "text": "/special " + sprintf(translate("%(nick)s has left."), { "nick": msg.text }),
    139                 "isSpecial": true
     150                "isSpecial": true,
     151                "time": msg.time
    140152            });
    141153        },
    142154        "presence": msg => {
     
    147159                    "oldnick": msg.text,
    148160                    "newnick": msg.data
    149161                }),
    150                 "isSpecial": true
     162                "isSpecial": true,
     163                "time": msg.time
    151164            });
    152165        },
    153166        "room-message": msg => {
     
    154167            addChatMessage({
    155168                "from": escapeText(msg.from),
    156169                "text": escapeText(msg.text),
    157                 "datetime": msg.datetime
     170                "time": msg.time
    158171            });
    159172        },
    160173        "private-message": msg => {
     
    162175                addChatMessage({
    163176                    "from": "(Private) " + escapeText(msg.from), // TODO: placeholder
    164177                    "text": escapeText(msg.text.trim()), // some XMPP clients send trailing whitespace
    165                     "datetime": msg.datetime
     178                    "time": msg.time
    166179                });
    167180        }
    168181    },
     
    663676            "text": sprintf(
    664677                translate("This game's address '%(ip)s' does not appear to be valid."),
    665678                { "ip": game.ip }
    666             )
     679            ),
     680            "time": Date.now() / 1000
    667681        });
    668682        return;
    669683    }
     
    774788    default:
    775789        addChatMessage({
    776790            "from": "system",
    777             "text": sprintf(translate("We're sorry, the '%(cmd)s' command is not supported."), { "cmd": cmd })
     791            "text": sprintf(translate("We're sorry, the '%(cmd)s' command is not supported."), { "cmd": cmd }),
     792            "time": Date.now() / 1000
    778793        });
    779794    }
    780795    return true;
     
    796811        if (g_Username != msg.from)
    797812            msg.text = msg.text.replace(g_Username, colorPlayerName(g_Username));
    798813
    799         // Run spam test if it's not a historical message
    800         if (!msg.datetime)
    801         {
    802             updateSpamMonitor(msg.from);
    803             if (isSpam(msg.text, msg.from))
    804                 return;
    805         }
     814        updateSpamMonitor(msg);
     815
     816        if (isSpam(msg.text, msg.from))
     817            return;
    806818    }
    807819
    808820    var formatted = ircFormat(msg);
     
    884896    if (!g_ShowTimestamp)
    885897        return formattedMessage;
    886898
    887     var time;
    888     if (msg.datetime)
    889     {
    890         let dTime = msg.datetime.split("T");
    891         let parserDate = dTime[0].split("-");
    892         let parserTime = dTime[1].split(":");
    893         // See http://xmpp.org/extensions/xep-0082.html#sect-idp285136 for format of datetime
    894         // Date takes Year, Month, Day, Hour, Minute, Second
    895         time = new Date(Date.UTC(parserDate[0], parserDate[1], parserDate[2], parserTime[0], parserTime[1], parserTime[2].split("Z")[0]));
    896     }
    897     else
    898         time = new Date(Date.now());
     899    // Convert from UTC to localtime
     900    var time = msg.time - new Date().getTimezoneOffset() * 60;
    899901
    900902    // Translation: Time as shown in the multiplayer lobby (when you enable it in the options page).
    901903    // For a list of symbols that you can use, see:
    902904    // https://sites.google.com/site/icuprojectuserguide/formatparse/datetime?pli=1#TOC-Date-Field-Symbol-Table
    903     var timeString = Engine.FormatMillisecondsIntoDateString(time.getTime(), translate("HH:mm"));
     905    var timeString = Engine.FormatMillisecondsIntoDateString(time * 1000, translate("HH:mm"));
    904906
    905907    // Translation: Time prefix as shown in the multiplayer lobby (when you enable it in the options page).
    906908    var timePrefixString = '[font="' + g_SenderFont + '"]' + sprintf(translate("\\[%(time)s]"), { "time": timeString }) + '[/font]';
     
    912914/**
    913915 * Update the spam monitor.
    914916 *
    915  * @param {string} from - User to update.
     917 * @param {Object} msg - Message containing user to update.
    916918 */
    917 function updateSpamMonitor(from)
     919function updateSpamMonitor(msg)
    918920{
    919     if (g_SpamMonitor[from])
    920         ++g_SpamMonitor[from].count;
     921    // Ignore historical messages
     922    if (msg.time < Date.now() / 1000 - g_SpamBlockDuration)
     923        return;
     924
     925    if (g_SpamMonitor[msg.from])
     926        ++g_SpamMonitor[msg.from].count;
    921927    else
    922         g_SpamMonitor[from] = {
     928        g_SpamMonitor[msg.from] = {
    923929            "count": 1,
    924930            "lastSend": Math.floor(Date.now() / 1000),
    925931            "lastBlock": 0
     
    961967        g_SpamMonitor[from].lastBlock = time;
    962968
    963969        if (from == g_Username)
    964             addChatMessage({ "from": "system", "text": translate("Please do not spam. You have been blocked for thirty seconds.") });
     970            addChatMessage({
     971                "from": "system",
     972                "text": translate("Please do not spam. You have been blocked for thirty seconds."),
     973                "time": Date.now() / 1000
     974            });
    965975
    966976        return true;
    967977    }
  • source/lobby/XmppClient.cpp

     
    1919#include "XmppClient.h"
    2020#include "StanzaExtensions.h"
    2121
    22 #include "glooxwrapper/glooxwrapper.h"
    2322#include "i18n/L10n.h"
     23
     24#if OS_WIN
     25#include "lib/sysdep/os/win/wposix/wtime.h"
     26#endif
    2427#include "lib/utf8.h"
     28
    2529#include "ps/CLogger.h"
    2630#include "ps/ConfigDB.h"
    2731#include "ps/Pyrogenesis.h"
     32
    2833#include "scriptinterface/ScriptInterface.h"
    2934
    3035//debug
     
    592597        scriptInterface.SetProperty(ret, "level", message.level);
    593598    if (!message.data.empty())
    594599        scriptInterface.SetProperty(ret, "data", message.data);
    595     if (!message.datetime.empty())
    596         scriptInterface.SetProperty(ret, "datetime", message.datetime);
     600    scriptInterface.SetProperty(ret, "time", message.time);
    597601
    598602    m_GuiMessageQueue.pop_front();
    599603}
     
    655659    message.level = L"room-message";
    656660    message.from = wstring_from_utf8(msg.from().resource().to_string());
    657661    message.text = wstring_from_utf8(msg.body().to_string());
    658     if (msg.when())
    659         // See http://xmpp.org/extensions/xep-0082.html#sect-idp285136 for format
    660         message.datetime = msg.when()->stamp().to_string();
     662    message.time = ComputeTimestamp(msg);
    661663    PushGuiMessage(message);
    662664}
    663665
     
    674676    message.level = L"private-message";
    675677    message.from = wstring_from_utf8(msg.from().username().to_string());
    676678    message.text = wstring_from_utf8(msg.body().to_string());
    677     if (msg.when())
    678         //See http://xmpp.org/extensions/xep-0082.html#sect-idp285136 for format
    679         message.datetime = msg.when()->stamp().to_string();
     679    message.time = ComputeTimestamp(msg);
    680680    PushGuiMessage(message);
    681681}
    682682
     
    769769    message.level = wstring_from_utf8(level);
    770770    message.text = wstring_from_utf8(text);
    771771    message.data = wstring_from_utf8(data);
     772    message.time = time(NULL);
    772773    PushGuiMessage(message);
    773774}
    774775
     
    939940 *****************************************************/
    940941
    941942/**
     943 * Compute the POSIX timestamp of a message. Uses message datetime when possible, current time otherwise.
     944 *
     945 * @param msg The message on which to base the computation
     946 * @returns POSIX UTC seconds since Jan. 1st 1970
     947 */
     948u32 XmppClient::ComputeTimestamp(const glooxwrapper::Message& msg)
     949{
     950    if (!msg.when())
     951        return 0;
     952
     953    glooxwrapper::string timestampStr = msg.when()->stamp();
     954    struct tm timestamp = {0};
     955
     956    // See http://xmpp.org/extensions/xep-0082.html#sect-idp285136 for format
     957    char* res = strptime(timestampStr.c_str(), "%Y-%m-%dT%H:%M:%SZ", &timestamp);
     958    if (!res)
     959        LOGERROR("Received delayed message with corrupted timestamp %s", timestampStr.to_string());
     960
     961    // FIXME this should be looked into during review
     962    // make sure no cast problems can arise +
     963    // create a real UTC timestamp without that hack
     964    return (u32)(mktime(&timestamp) - timezone);
     965}
     966
     967/**
    942968 * Convert a gloox presence type to string.
    943969 *
    944970 * @param p Presence to be converted
  • source/lobby/XmppClient.h

     
    127127    std::string StanzaErrorToString(gloox::StanzaError err) const;
    128128    std::string ConnectionErrorToString(gloox::ConnectionError err) const;
    129129    std::string RegistrationResultToString(gloox::RegistrationResult res) const;
     130    u32 ComputeTimestamp(const glooxwrapper::Message& msg);
    130131
    131132public:
    132133    /* Messages */
     
    138139        std::wstring data;
    139140        std::wstring from;
    140141        std::wstring message;
    141         std::string datetime;
     142        u32 time; // UTC timestamp in seconds
    142143    };
    143144    void GuiPollMessage(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
    144145    void SendMUCMessage(const std::string& message);