Bug 957918 - Port chat/ changes from Instantbird to comm-central - 7 - Bug 955606 - IRC ping does not properly send a timestamp, r=aleth,fqueze.

This commit is contained in:
Patrick Cloke 2013-10-18 17:59:39 -04:00
Родитель 7ca7b5b7d0
Коммит 4f7fd44651
5 изменённых файлов: 63 добавлений и 41 удалений

Просмотреть файл

@ -33,11 +33,6 @@ options.partMessage=Part message
options.showServerTab=Show messages from the server
options.alternateNicks=Alternate nicks
# LOCALIZATION NOTE (ctcp.ping): Semi-colon list of plural forms.
# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
# %1$S is the nickname of the user who was pinged.
# #2 is the delay (in seconds).
ctcp.ping=Ping reply from %1$S in #2 second.;Ping reply from %1$S in #2 seconds.
# LOCALIZATION NOTE (ctcp.version):
# %1$S is the nickname of the user whose version was requested.
# %2$S is the version response from the client.
@ -144,6 +139,12 @@ message.banMasks=Users connected from the following locations are banned from %S
message.noBanMasks=There are no banned locations for %S.
message.banMaskAdded=Users connected from locations matching %1$S have been banned by %2$S.
message.banMaskRemoved=Users connected from locations matching %1$S are no longer banned by %2$S.
# LOCALIZATION NOTE (message.ping): Semi-colon list of plural forms.
# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
# %1$S is the nickname of the user or the server that was pinged.
# #2 is the delay (in milliseconds).
message.ping=Ping reply from %1$S in #2 millisecond.;Ping reply from %1$S in #2 milliseconds.
# LOCALIZATION NOTE (error.*):
# These are shown as error messages in the server tab.

Просмотреть файл

@ -11,6 +11,9 @@ Cu.import("resource:///modules/ircHandlers.jsm");
Cu.import("resource:///modules/jsProtoHelper.jsm");
Cu.import("resource:///modules/socket.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
"resource://gre/modules/PluralForm.jsm");
/*
* Parses a raw IRC message into an object (see section 2.3 of RFC 2812). This
* returns an object with the following fields:
@ -636,8 +639,10 @@ ircSocket.prototype = {
_converter: null,
sendPing: function() {
// Send a ping using the current timestamp as a payload.
this._account.sendMessage("PING", Date.now());
// Send a ping using the current timestamp as a payload prefixed with
// an underscore to signify this was an "automatic" PING (used to avoid
// socket timeouts).
this._account.sendMessage("PING", "_" + Date.now());
},
_initCharsetConverter: function() {
@ -1213,6 +1218,34 @@ ircAccount.prototype = {
return true;
},
handlePingReply: function(aSource, aPongTime) {
// Received PING response, display to the user.
let sentTime = new Date(parseInt(aPongTime, 10));
// The received timestamp is invalid.
if (isNaN(sentTime)) {
this.WARN(aMessage.servername +
" returned an invalid timestamp from a PING: " + aPongTime);
return false;
}
// Find the delay in milliseconds.
let delay = Date.now() - sentTime;
// If the delay is negative or greater than 1 minute, something is
// feeding us a crazy value. Don't display this to the user.
if (delay < 0 || 60 * 1000 < delay) {
this.WARN(aMessage.servername +
" returned an invalid delay from a PING: " + delay);
return false;
}
let msg = PluralForm.get(delay, _("message.ping", aSource))
.replace("#2", delay);
this.getConversation(aSource).writeMessage(aSource, msg, {system: true});
return true;
},
countBytes: function(aStr) {
// Assume that if it's not UTF-8 then each character is 1 byte.
if (this._encoding != "UTF-8")

Просмотреть файл

@ -276,9 +276,16 @@ var ircBase = {
},
"PONG": function(aMessage) {
// PONG <server> [ <server2> ]
let pongTime = aMessage.params[1];
// Ping to keep the connection alive.
this._socket.cancelDisconnectTimer();
return true;
if (pongTime.startsWith("_")) {
this._socket.cancelDisconnectTimer();
return true;
}
// Otherwise, the ping was from a user command.
else
return this.handlePingReply(aMessage.servername, pongTime);
},
"PRIVMSG": function(aMessage) {
// PRIVMSG <msgtarget> <text to be sent>

Просмотреть файл

@ -17,11 +17,6 @@ Cu.import("resource:///modules/imXPCOMUtils.jsm");
Cu.import("resource:///modules/ircHandlers.jsm");
Cu.import("resource:///modules/ircUtils.jsm");
XPCOMUtils.defineLazyGetter(this, "PluralForm", function() {
Cu.import("resource://gre/modules/PluralForm.jsm");
return PluralForm;
});
// Split into a CTCP message which is a single command and a single parameter:
// <command> " " <parameter>
// The high level dequote is to unescape \001 in the message content.
@ -147,36 +142,17 @@ var ctcpBase = {
// Used to measure the delay of the IRC network between clients.
"PING": function(aMessage) {
// PING timestamp
if (aMessage.command == "PRIVMSG") {
// PING timestamp
// Received PING request, send PING response.
this.LOG("Received PING request from " + aMessage.nickname +
". Sending PING response: \"" + aMessage.ctcp.param + "\".");
this.sendCTCPMessage("PING", aMessage.ctcp.param, aMessage.nickname,
true);
true);
return true;
}
else {
// PING timestamp
// Received PING response, display to the user.
let sentTime = new Date(aMessage.ctcp.param);
// The received timestamp is invalid
if (isNaN(sentTime)) {
this.WARN(aMessage.nickname +
" returned an invalid timestamp from a CTCP PING: " +
aMessage.ctcp.param);
return false;
}
// Find the delay in seconds.
let delay = (Date.now() - sentTime) / 1000;
let message = PluralForm.get(delay, _("ctcp.ping", aMessage.nickname))
.replace("#2", delay);
this.getConversation(aMessage.nickname)
.writeMessage(aMessage.nickname, message, {system: true});
}
return true;
else
return this.handlePingReply(aMessage.nickname, aMessage.ctcp.param);
},
// An encryption protocol between clients without any known reference.

Просмотреть файл

@ -337,9 +337,14 @@ var commands = [
name: "ping",
get helpString() _("command.ping", "ping"),
run: function(aMsg, aConv) {
if (!aMsg || !aMsg.trim().length)
return false;
ctcpCommand(aConv, aMsg, "PING");
// Send a ping to the entered nick using the current time (in
// milliseconds) as the param. If no nick is entered, ping the
// server.
if (aMsg && aMsg.trim().length)
ctcpCommand(aConv, aMsg, "PING", Date.now());
else
getAccount(aConv).sendMessage("PING", Date.now());
return true;
}
},