Skip to content

Instantly share code, notes, and snippets.

@prawnsalad
Created April 3, 2019 16:11
Show Gist options
  • Save prawnsalad/6a3a4b11ce730c66f1f02726b2eabf1f to your computer and use it in GitHub Desktop.
Save prawnsalad/6a3a4b11ce730c66f1f02726b2eabf1f to your computer and use it in GitHub Desktop.
'kiwi public';
/** @module */
/**
* Adds the +draft/typing IRCv3 spec to irc-framework
*/
export default function typingMiddleware() {
return function middleware(client, rawEvents, parsedEvents) {
addFunctionsToClient(client);
rawEvents.use(theMiddleware);
};
function theMiddleware(command, message, rawLine, client, next) {
if (command !== 'TAGMSG' || !message.tags['+draft/typing']) {
next();
return;
}
client.emit('typing', {
target: message.params[0],
nick: message.nick,
ident: message.ident,
hostname: message.hostname,
status: message.tags['+draft/typing'],
});
}
}
function addFunctionsToClient(client) {
let typing = client.typing = {};
let activeTyping = Object.create(null);
typing.start = function start(target) {
let lastSentStatus = activeTyping[target.toLowerCase()];
if (lastSentStatus && lastSentStatus > Date.now() - 3000) {
return;
}
activeTyping[target.toLowerCase()] = Date.now();
let message = new client.Message('TAGMSG', target);
message.tags['+draft/typing'] = 'active';
client.raw(message);
};
typing.pause = function pause(target) {
let message = new client.Message('TAGMSG', target);
message.tags['+draft/typing'] = 'paused';
client.raw(message);
};
typing.stop = function stop(target) {
let message = new client.Message('TAGMSG', target);
message.tags['+draft/typing'] = 'done';
client.raw(message);
delete activeTyping[target.toLowerCase()];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment