Skip to content

Commit

Permalink
Handle BATCH command. Tag #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ascrod committed Jun 10, 2019
1 parent 546773d commit ba220b9
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions js/lib/irc.js
Expand Up @@ -13,6 +13,7 @@ const JSIRCV3_SUPPORTED_CAPS = [
"account-notify",
"account-tag",
"away-notify",
"batch",
"cap-notify",
"chghost",
"echo-message",
Expand Down Expand Up @@ -1577,6 +1578,17 @@ function serv_onRawData(e)
(e.code == "INVITE") || (e.code == "TAGMSG")))
return true;

// If the message is part of a batch, store it for later.
if (this.batches && e.tags["batch"] && e.code != "BATCH")
{
var reftag = e.tags["batch"];
// Check if the batch is already open.
// If not, ignore the incoming message.
if (this.batches[reftag])
this.batches[reftag].messages.push(e);
return false;
}

e.type = "parseddata";
e.destObject = this;
e.destMethod = "onParsedData";
Expand Down Expand Up @@ -2489,6 +2501,103 @@ function my_cap (e)
e.set = "network";
}

/* BATCH start or end */
CIRCServer.prototype.onBatch =
function serv_batch (e)
{
// We should at least get a ref tag.
if (e.params.length < 2)
return false;

e.reftag = e.params[1].substring(1);
switch (e.params[1][0])
{
case "+":
e.starting = true;
break;
case "-":
e.starting = false;
break;
default:
// Invalid reference tag.
return false;
}
var isPlayback = (this.batches && this.batches[e.reftag] &&
this.batches[e.reftag].playback);

if (!isPlayback)
{
if (e.starting)
{
// We're starting a batch, so we also need a type.
if (e.params.length < 3)
return false;

if (!this.batches)
this.batches = new Object();
// The batch object holds the messages queued up as part
// of this batch, and a boolean value indicating whether
// it is being played back.
var newBatch = new Object();
newBatch.messages = [e];
newBatch.type = e.params[2].toUpperCase();
newBatch.playback = false;
this.batches[e.reftag] = newBatch;
}
else
{
if (!this.batches[e.reftag])
{
// Got a close tag without an open tag, so ignore it.
return false;
}

var batch = this.batches[e.reftag];

// Closing the batch, prepare for playback.
batch.messages.push(e);
batch.playback = true;
if (e.tags["batch"])
{
// We are an inner batch. Append the message queue
// to the outer batch's message queue.
var parentRef = e.tags["batch"];
var parentMsgs = this.batches[parentRef].messages;
parentMsgs = parentMsgs.concat(batch.messages);
}
else
{
// We are an outer batch. Playback!
for (var i = 0; i < batch.messages.length; i++)
{
var ev = batch.messages[i];
ev.type = "parseddata";
ev.destObject = this;
ev.destMethod = "onParsedData";
this.parent.eventPump.routeEvent(ev);
}
}
}
return false;
}
else
{
// Batch command is ready for handling.
e.batchtype = this.batches[e.reftag].type;
e.destObject = this.parent;
e.set = "network";

if (!e.starting)
{
// If we've reached the end of a batch in playback,
// do some cleanup.
delete this.batches[e.reftag];
if (Object.entries(this.batches).length == 0)
delete this.batches;
}
}
}

/* SASL authentication responses */
CIRCServer.prototype.on902 = /* Nick locked */
CIRCServer.prototype.on903 = /* Auth success */
Expand Down

0 comments on commit ba220b9

Please sign in to comment.