From 74dcc5265713cf2e6a791c1966469620c0eda621 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 10 Nov 2017 22:44:14 +0200 Subject: [PATCH] Refactor how user object is sent to the client --- client/views/actions/action.tpl | 2 +- client/views/actions/away.tpl | 2 +- client/views/actions/back.tpl | 2 +- client/views/actions/ctcp.tpl | 2 +- client/views/actions/invite.tpl | 4 +-- client/views/actions/join.tpl | 2 +- client/views/actions/kick.tpl | 4 +-- client/views/actions/mode.tpl | 2 +- client/views/actions/nick.tpl | 4 +-- client/views/actions/part.tpl | 2 +- client/views/actions/quit.tpl | 2 +- client/views/actions/topic.tpl | 2 +- client/views/actions/topic_set_by.tpl | 2 +- client/views/msg.tpl | 2 +- src/models/chan.js | 9 ------- src/plugins/irc-events/away.js | 3 +-- src/plugins/irc-events/connection.js | 18 ++++++------- src/plugins/irc-events/ctcp.js | 3 ++- src/plugins/irc-events/error.js | 24 +++++++++-------- src/plugins/irc-events/invite.js | 14 +++++----- src/plugins/irc-events/join.js | 29 +++++++++++--------- src/plugins/irc-events/kick.js | 39 +++++++++++++-------------- src/plugins/irc-events/link.js | 6 +++-- src/plugins/irc-events/list.js | 11 ++++---- src/plugins/irc-events/message.js | 20 +++++++------- src/plugins/irc-events/mode.js | 4 +-- src/plugins/irc-events/motd.js | 11 ++++---- src/plugins/irc-events/nick.js | 33 +++++++++++++---------- src/plugins/irc-events/part.js | 35 +++++++++++++----------- src/plugins/irc-events/quit.js | 27 ++++++++++--------- src/plugins/irc-events/topic.js | 21 ++++++++------- src/plugins/irc-events/unhandled.js | 4 +-- src/plugins/irc-events/welcome.js | 10 ++++--- src/plugins/irc-events/whois.js | 11 ++++---- 34 files changed, 191 insertions(+), 175 deletions(-) diff --git a/client/views/actions/action.tpl b/client/views/actions/action.tpl index f11a0d35..c8952188 100644 --- a/client/views/actions/action.tpl +++ b/client/views/actions/action.tpl @@ -1,4 +1,4 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} {{{parse text}}} {{#each previews}} diff --git a/client/views/actions/away.tpl b/client/views/actions/away.tpl index f4e52519..04a54bc6 100644 --- a/client/views/actions/away.tpl +++ b/client/views/actions/away.tpl @@ -1,3 +1,3 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} is away ({{{parse text}}}) diff --git a/client/views/actions/back.tpl b/client/views/actions/back.tpl index cb24ea5e..02520366 100644 --- a/client/views/actions/back.tpl +++ b/client/views/actions/back.tpl @@ -1,2 +1,2 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} is back diff --git a/client/views/actions/ctcp.tpl b/client/views/actions/ctcp.tpl index a64a296d..44b74e78 100644 --- a/client/views/actions/ctcp.tpl +++ b/client/views/actions/ctcp.tpl @@ -1,2 +1,2 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} {{ctcpType}} {{{parse ctcpMessage}}} diff --git a/client/views/actions/invite.tpl b/client/views/actions/invite.tpl index 703f643d..93215852 100644 --- a/client/views/actions/invite.tpl +++ b/client/views/actions/invite.tpl @@ -1,9 +1,9 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} invited {{#if invitedYou}} you {{else}} - {{> ../user_name nick=invited}} + {{> ../user_name invited}} {{/if}} to {{{parse channel}}} diff --git a/client/views/actions/join.tpl b/client/views/actions/join.tpl index d9749911..00fcc640 100644 --- a/client/views/actions/join.tpl +++ b/client/views/actions/join.tpl @@ -1,3 +1,3 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} ({{hostmask}}) has joined the channel diff --git a/client/views/actions/kick.tpl b/client/views/actions/kick.tpl index d739f4c9..9cdfab0f 100644 --- a/client/views/actions/kick.tpl +++ b/client/views/actions/kick.tpl @@ -1,6 +1,6 @@ -{{> ../user_name nick=from.nick mode=from.mode}} +{{> ../user_name from}} has kicked -{{> ../user_name nick=target.nick mode=target.mode}} +{{> ../user_name target}} {{#if text}} ({{{parse text}}}) {{/if}} diff --git a/client/views/actions/mode.tpl b/client/views/actions/mode.tpl index dc439d5d..6801eb7b 100644 --- a/client/views/actions/mode.tpl +++ b/client/views/actions/mode.tpl @@ -1,3 +1,3 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} sets mode {{{parse text}}} diff --git a/client/views/actions/nick.tpl b/client/views/actions/nick.tpl index 2a292a3d..09d6c36e 100644 --- a/client/views/actions/nick.tpl +++ b/client/views/actions/nick.tpl @@ -1,3 +1,3 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} is now known as -{{> ../user_name nick=new_nick}} +{{> ../user_name nick=new_nick mode=from.mode}} diff --git a/client/views/actions/part.tpl b/client/views/actions/part.tpl index 72d89cd0..0752ecbf 100644 --- a/client/views/actions/part.tpl +++ b/client/views/actions/part.tpl @@ -1,4 +1,4 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} ({{hostmask}}) has left the channel {{#if text}} diff --git a/client/views/actions/quit.tpl b/client/views/actions/quit.tpl index 497739ca..18247cb1 100644 --- a/client/views/actions/quit.tpl +++ b/client/views/actions/quit.tpl @@ -1,4 +1,4 @@ -{{> ../user_name nick=from}} +{{> ../user_name from}} ({{hostmask}}) has quit {{#if text}} diff --git a/client/views/actions/topic.tpl b/client/views/actions/topic.tpl index 88743be6..825b0d1e 100644 --- a/client/views/actions/topic.tpl +++ b/client/views/actions/topic.tpl @@ -1,5 +1,5 @@ {{#if from}} - {{> ../user_name nick=from}} + {{> ../user_name from}} has changed the topic to: {{else}} The topic is: diff --git a/client/views/actions/topic_set_by.tpl b/client/views/actions/topic_set_by.tpl index ac825fe3..b9b0e484 100644 --- a/client/views/actions/topic_set_by.tpl +++ b/client/views/actions/topic_set_by.tpl @@ -1,3 +1,3 @@ Topic set by -{{> ../user_name}} +{{> ../user_name from}} on {{localetime when}} diff --git a/client/views/msg.tpl b/client/views/msg.tpl index 9b5b13f2..4c14619b 100644 --- a/client/views/msg.tpl +++ b/client/views/msg.tpl @@ -4,7 +4,7 @@ {{#if from}} - {{> user_name nick=from}} + {{> user_name from}} {{/if}} diff --git a/src/models/chan.js b/src/models/chan.js index 66726f80..029df39b 100644 --- a/src/models/chan.js +++ b/src/models/chan.js @@ -126,15 +126,6 @@ Chan.prototype.getUser = function(nick) { return this.findUser(nick) || new User({nick: nick}); }; -Chan.prototype.getMode = function(name) { - var user = this.findUser(name); - if (user) { - return user.mode; - } - - return ""; -}; - Chan.prototype.toJSON = function() { var clone = _.clone(this); clone.users = []; // Do not send user list, the client will explicitly request it when needed diff --git a/src/plugins/irc-events/away.js b/src/plugins/irc-events/away.js index 34ad568b..cf64aea5 100644 --- a/src/plugins/irc-events/away.js +++ b/src/plugins/irc-events/away.js @@ -19,8 +19,7 @@ module.exports = function(irc, network) { type: away ? Msg.Type.AWAY : Msg.Type.BACK, text: away || "", time: data.time, - from: data.nick, - mode: user.mode, + from: user, }); chan.pushMessage(client, msg); diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index c6d82708..9ea5cd1e 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -1,12 +1,12 @@ "use strict"; -var _ = require("lodash"); -var Msg = require("../../models/msg"); -var Chan = require("../../models/chan"); -var Helper = require("../../helper"); +const _ = require("lodash"); +const Msg = require("../../models/msg"); +const Chan = require("../../models/chan"); +const Helper = require("../../helper"); module.exports = function(irc, network) { - var client = this; + const client = this; network.channels[0].pushMessage(client, new Msg({ text: "Network created, connecting to " + network.host + ":" + network.port + "...", @@ -27,10 +27,10 @@ module.exports = function(irc, network) { irc.raw("AWAY", client.awayMessage); } - var delay = 1000; - var commands = network.commands; - if (Array.isArray(commands)) { - commands.forEach((cmd) => { + let delay = 1000; + + if (Array.isArray(network.commands)) { + network.commands.forEach((cmd) => { setTimeout(function() { client.input({ target: network.channels[0].id, diff --git a/src/plugins/irc-events/ctcp.js b/src/plugins/irc-events/ctcp.js index 823d3c30..d1a9e215 100644 --- a/src/plugins/irc-events/ctcp.js +++ b/src/plugins/irc-events/ctcp.js @@ -7,6 +7,7 @@ module.exports = function(irc, network) { irc.on("ctcp response", function(data) { let chan = network.getChannel(data.nick); + if (typeof chan === "undefined") { chan = network.channels[0]; } @@ -14,7 +15,7 @@ module.exports = function(irc, network) { const msg = new Msg({ type: Msg.Type.CTCP, time: data.time, - from: data.nick, + from: chan.getUser(data.nick), ctcpType: data.type, ctcpMessage: data.message, }); diff --git a/src/plugins/irc-events/error.js b/src/plugins/irc-events/error.js index 9c66865b..5b925a80 100644 --- a/src/plugins/irc-events/error.js +++ b/src/plugins/irc-events/error.js @@ -1,17 +1,19 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; irc.on("irc error", function(data) { - var text = data.error; + let text = data.error; + if (data.reason) { text = data.reason + " (" + text + ")"; } - var lobby = network.channels[0]; - var msg = new Msg({ + + const lobby = network.channels[0]; + const msg = new Msg({ type: Msg.Type.ERROR, text: text, }); @@ -19,15 +21,15 @@ module.exports = function(irc, network) { }); irc.on("nick in use", function(data) { - var lobby = network.channels[0]; - var msg = new Msg({ + const lobby = network.channels[0]; + const msg = new Msg({ type: Msg.Type.ERROR, text: data.nick + ": " + (data.reason || "Nickname is already in use."), }); lobby.pushMessage(client, msg, true); if (irc.connection.registered === false) { - var random = (data.nick || irc.user.nick) + Math.floor(10 + (Math.random() * 89)); + const random = (data.nick || irc.user.nick) + Math.floor(10 + (Math.random() * 89)); irc.changeNick(random); } @@ -38,15 +40,15 @@ module.exports = function(irc, network) { }); irc.on("nick invalid", function(data) { - var lobby = network.channels[0]; - var msg = new Msg({ + const lobby = network.channels[0]; + const msg = new Msg({ type: Msg.Type.ERROR, text: data.nick + ": " + (data.reason || "Nickname is invalid."), }); lobby.pushMessage(client, msg, true); if (irc.connection.registered === false) { - var random = "i" + Math.random().toString(36).substr(2, 10); // 'i' so it never begins with a number + const random = "i" + Math.random().toString(36).substr(2, 10); // 'i' so it never begins with a number irc.changeNick(random); } diff --git a/src/plugins/irc-events/invite.js b/src/plugins/irc-events/invite.js index c67c03d0..761bbcfd 100644 --- a/src/plugins/irc-events/invite.js +++ b/src/plugins/irc-events/invite.js @@ -1,20 +1,22 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("invite", function(data) { - var chan = network.getChannel(data.channel); + let chan = network.getChannel(data.channel); + if (typeof chan === "undefined") { chan = network.channels[0]; } - var msg = new Msg({ + const msg = new Msg({ type: Msg.Type.INVITE, time: data.time, - from: data.nick, - invited: data.invited, + from: chan.getUser(data.nick), + invited: chan.getUser(data.invited), channel: data.channel, highlight: true, invitedYou: data.invited === irc.user.nick, diff --git a/src/plugins/irc-events/join.js b/src/plugins/irc-events/join.js index 90ac8119..40ccebf7 100644 --- a/src/plugins/irc-events/join.js +++ b/src/plugins/irc-events/join.js @@ -1,13 +1,15 @@ "use strict"; -var Chan = require("../../models/chan"); -var Msg = require("../../models/msg"); -var User = require("../../models/user"); +const Chan = require("../../models/chan"); +const Msg = require("../../models/msg"); +const User = require("../../models/user"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("join", function(data) { - var chan = network.getChannel(data.channel); + let chan = network.getChannel(data.channel); + if (typeof chan === "undefined") { chan = new Chan({ name: data.channel, @@ -22,18 +24,21 @@ module.exports = function(irc, network) { // Request channels' modes network.irc.raw("MODE", chan.name); } - chan.users.push(new User({nick: data.nick})); - chan.sortUsers(irc); - client.emit("users", { - chan: chan.id, - }); - var msg = new Msg({ + + const user = new User({nick: data.nick}); + const msg = new Msg({ time: data.time, - from: data.nick, + from: user, hostmask: data.ident + "@" + data.hostname, type: Msg.Type.JOIN, self: data.nick === irc.user.nick, }); chan.pushMessage(client, msg); + + chan.users.push(user); + chan.sortUsers(irc); + client.emit("users", { + chan: chan.id, + }); }); }; diff --git a/src/plugins/irc-events/kick.js b/src/plugins/irc-events/kick.js index e7ae7f9c..9794e586 100644 --- a/src/plugins/irc-events/kick.js +++ b/src/plugins/irc-events/kick.js @@ -1,38 +1,37 @@ "use strict"; -var _ = require("lodash"); -var Msg = require("../../models/msg"); +const _ = require("lodash"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("kick", function(data) { - var chan = network.getChannel(data.channel); + const chan = network.getChannel(data.channel); + if (typeof chan === "undefined") { return; } - const kicker = chan.getUser(data.nick); - const target = chan.getUser(data.kicked); - - if (data.kicked === irc.user.nick) { - chan.users = []; - } else { - chan.users = _.without(chan.users, target); - } - - client.emit("users", { - chan: chan.id, - }); - - var msg = new Msg({ + const msg = new Msg({ type: Msg.Type.KICK, time: data.time, - from: kicker, - target: target, + from: chan.getUser(data.nick), + target: chan.getUser(data.kicked), text: data.message || "", highlight: data.kicked === irc.user.nick, self: data.nick === irc.user.nick, }); chan.pushMessage(client, msg); + + if (data.kicked === irc.user.nick) { + chan.users = []; + } else { + chan.users = _.without(chan.users, msg.target); + } + + client.emit("users", { + chan: chan.id, + }); }); }; diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index 3336252d..1eba9dde 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -154,9 +154,11 @@ function fetch(uri, cb) { } catch (e) { return cb(null); } + const buffers = []; - var length = 0; - var limit = Helper.config.prefetchMaxImageSize * 1024; + let length = 0; + let limit = Helper.config.prefetchMaxImageSize * 1024; + req .on("response", function(res) { if (/^image\/.+/.test(res.headers["content-type"])) { diff --git a/src/plugins/irc-events/list.js b/src/plugins/irc-events/list.js index 281ce2f0..3122d1b8 100644 --- a/src/plugins/irc-events/list.js +++ b/src/plugins/irc-events/list.js @@ -1,11 +1,11 @@ "use strict"; -var Chan = require("../../models/chan"); -var Msg = require("../../models/msg"); +const Chan = require("../../models/chan"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; - var MAX_CHANS = 500; + const client = this; + const MAX_CHANS = 500; irc.on("channel list start", function() { network.chanCache = []; @@ -39,7 +39,8 @@ module.exports = function(irc, network) { }); function updateListStatus(msg) { - var chan = network.getChannel("Channel List"); + let chan = network.getChannel("Channel List"); + if (typeof chan === "undefined") { chan = new Chan({ type: Chan.Type.SPECIAL, diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index 0a9731fd..a078e28b 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -6,7 +6,7 @@ const LinkPrefetch = require("./link"); const Helper = require("../../helper"); module.exports = function(irc, network) { - var client = this; + const client = this; irc.on("notice", function(data) { // Some servers send notices without any nickname @@ -37,14 +37,16 @@ module.exports = function(irc, network) { function handleMessage(data) { let chan; + let user; let highlight = false; const self = data.nick === irc.user.nick; // Server messages go to server window, no questions asked if (data.from_server) { chan = network.channels[0]; + user = chan.getUser(data.nick); } else { - var target = data.target; + let target = data.target; // If the message is targeted at us, use sender as target instead if (target.toLowerCase() === irc.user.nick.toLowerCase()) { @@ -52,6 +54,7 @@ module.exports = function(irc, network) { } chan = network.getChannel(target); + if (typeof chan === "undefined") { // Send notices that are not targeted at us into the server window if (data.type === Msg.Type.NOTICE) { @@ -69,15 +72,13 @@ module.exports = function(irc, network) { } } + user = chan.getUser(data.nick); + // Query messages (unless self) always highlight if (chan.type === Chan.Type.QUERY) { highlight = !self; } else if (chan.type === Chan.Type.CHANNEL) { - const user = chan.findUser(data.nick); - - if (user) { - user.lastMessage = data.time || Date.now(); - } + user.lastMessage = data.time || Date.now(); } } @@ -87,11 +88,10 @@ module.exports = function(irc, network) { highlight = network.highlightRegex.test(data.message); } - var msg = new Msg({ + const msg = new Msg({ type: data.type, time: data.time, - mode: chan.getMode(data.nick), - from: data.nick, + from: user, text: data.message, self: self, highlight: highlight, diff --git a/src/plugins/irc-events/mode.js b/src/plugins/irc-events/mode.js index 19ff5a1d..f9f51410 100644 --- a/src/plugins/irc-events/mode.js +++ b/src/plugins/irc-events/mode.js @@ -1,7 +1,6 @@ "use strict"; const _ = require("lodash"); -const Chan = require("../../models/chan"); const Msg = require("../../models/msg"); module.exports = function(irc, network) { @@ -70,8 +69,7 @@ module.exports = function(irc, network) { const msg = new Msg({ time: data.time, type: Msg.Type.MODE, - mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "", - from: data.nick, + from: targetChan.getUser(data.nick), text: text, self: data.nick === irc.user.nick, }); diff --git a/src/plugins/irc-events/motd.js b/src/plugins/irc-events/motd.js index 9c621300..03743d92 100644 --- a/src/plugins/irc-events/motd.js +++ b/src/plugins/irc-events/motd.js @@ -1,15 +1,16 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("motd", function(data) { - var lobby = network.channels[0]; + const lobby = network.channels[0]; if (data.motd) { data.motd.split("\n").forEach((text) => { - var msg = new Msg({ + const msg = new Msg({ type: Msg.Type.MOTD, text: text, }); @@ -18,7 +19,7 @@ module.exports = function(irc, network) { } if (data.error) { - var msg = new Msg({ + const msg = new Msg({ type: Msg.Type.MOTD, text: data.error, }); diff --git a/src/plugins/irc-events/nick.js b/src/plugins/irc-events/nick.js index fed42664..9c7869ef 100644 --- a/src/plugins/irc-events/nick.js +++ b/src/plugins/irc-events/nick.js @@ -1,21 +1,23 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("nick", function(data) { let msg; - var self = false; - if (data.nick === irc.user.nick) { + const self = data.nick === irc.user.nick; + + if (self) { network.setNick(data.new_nick); - var lobby = network.channels[0]; + const lobby = network.channels[0]; msg = new Msg({ - text: "You're now known as " + data.new_nick, + text: `You're now known as ${data.new_nick}`, }); lobby.pushMessage(client, msg, true); - self = true; + client.save(); client.emit("nick", { network: network.id, @@ -25,23 +27,26 @@ module.exports = function(irc, network) { network.channels.forEach((chan) => { const user = chan.findUser(data.nick); + if (typeof user === "undefined") { return; } - user.nick = data.new_nick; - chan.sortUsers(irc); - client.emit("users", { - chan: chan.id, - }); + msg = new Msg({ time: data.time, - from: data.nick, + from: user, type: Msg.Type.NICK, - mode: chan.getMode(data.new_nick), new_nick: data.new_nick, self: self, }); chan.pushMessage(client, msg); + + user.nick = data.new_nick; + + chan.sortUsers(irc); + client.emit("users", { + chan: chan.id, + }); }); }); }; diff --git a/src/plugins/irc-events/part.js b/src/plugins/irc-events/part.js index dbc3b66b..3e23f51d 100644 --- a/src/plugins/irc-events/part.js +++ b/src/plugins/irc-events/part.js @@ -1,17 +1,19 @@ "use strict"; -var _ = require("lodash"); -var Msg = require("../../models/msg"); +const _ = require("lodash"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("part", function(data) { - var chan = network.getChannel(data.channel); + const chan = network.getChannel(data.channel); + if (typeof chan === "undefined") { return; } - var from = data.nick; - if (from === irc.user.nick) { + + if (data.nick === irc.user.nick) { network.channels = _.without(network.channels, chan); chan.destroy(); client.save(); @@ -19,20 +21,21 @@ module.exports = function(irc, network) { chan: chan.id, }); } else { - const user = chan.findUser(from); + const user = chan.getUser(data.nick); + + const msg = new Msg({ + type: Msg.Type.PART, + time: data.time, + text: data.message || "", + hostmask: data.ident + "@" + data.hostname, + from: user, + }); + chan.pushMessage(client, msg); + chan.users = _.without(chan.users, user); client.emit("users", { chan: chan.id, }); - var msg = new Msg({ - type: Msg.Type.PART, - time: data.time, - mode: (user && user.mode) || "", - text: data.message || "", - hostmask: data.ident + "@" + data.hostname, - from: from, - }); - chan.pushMessage(client, msg); } }); }; diff --git a/src/plugins/irc-events/quit.js b/src/plugins/irc-events/quit.js index a12bf236..f5958a33 100644 --- a/src/plugins/irc-events/quit.js +++ b/src/plugins/irc-events/quit.js @@ -1,29 +1,32 @@ "use strict"; -var _ = require("lodash"); -var Msg = require("../../models/msg"); +const _ = require("lodash"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("quit", function(data) { network.channels.forEach((chan) => { const user = chan.findUser(data.nick); + if (typeof user === "undefined") { return; } + + const msg = new Msg({ + time: data.time, + type: Msg.Type.QUIT, + text: data.message || "", + hostmask: data.ident + "@" + data.hostname, + from: user, + }); + chan.pushMessage(client, msg); + chan.users = _.without(chan.users, user); client.emit("users", { chan: chan.id, }); - var msg = new Msg({ - time: data.time, - type: Msg.Type.QUIT, - mode: user.mode || "", - text: data.message || "", - hostmask: data.ident + "@" + data.hostname, - from: data.nick, - }); - chan.pushMessage(client, msg); }); }); }; diff --git a/src/plugins/irc-events/topic.js b/src/plugins/irc-events/topic.js index a154e236..12fd2afb 100644 --- a/src/plugins/irc-events/topic.js +++ b/src/plugins/irc-events/topic.js @@ -1,20 +1,21 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("topic", function(data) { - var chan = network.getChannel(data.channel); + const chan = network.getChannel(data.channel); + if (typeof chan === "undefined") { return; } - var msg = new Msg({ + const msg = new Msg({ time: data.time, type: Msg.Type.TOPIC, - mode: (data.nick && chan.getMode(data.nick)) || "", - from: data.nick, + from: chan.getUser(data.nick), text: data.topic, self: data.nick === irc.user.nick, }); @@ -28,15 +29,15 @@ module.exports = function(irc, network) { }); irc.on("topicsetby", function(data) { - var chan = network.getChannel(data.channel); + const chan = network.getChannel(data.channel); + if (typeof chan === "undefined") { return; } - var msg = new Msg({ + const msg = new Msg({ type: Msg.Type.TOPIC_SET_BY, - mode: chan.getMode(data.nick), - nick: data.nick, + from: chan.getUser(data.nick), when: new Date(data.when * 1000), self: data.nick === irc.user.nick, }); diff --git a/src/plugins/irc-events/unhandled.js b/src/plugins/irc-events/unhandled.js index c7927f89..f9c111ac 100644 --- a/src/plugins/irc-events/unhandled.js +++ b/src/plugins/irc-events/unhandled.js @@ -1,9 +1,9 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; irc.on("unknown command", function(command) { // Do not display users own name diff --git a/src/plugins/irc-events/welcome.js b/src/plugins/irc-events/welcome.js index 783a8fe7..010eae8b 100644 --- a/src/plugins/irc-events/welcome.js +++ b/src/plugins/irc-events/welcome.js @@ -1,17 +1,19 @@ "use strict"; -var Msg = require("../../models/msg"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; + irc.on("registered", function(data) { network.setNick(data.nick); - var lobby = network.channels[0]; - var msg = new Msg({ + const lobby = network.channels[0]; + const msg = new Msg({ text: "You're now known as " + data.nick, }); lobby.pushMessage(client, msg); + client.save(); client.emit("nick", { network: network.id, diff --git a/src/plugins/irc-events/whois.js b/src/plugins/irc-events/whois.js index 490deb5a..13b41bfa 100644 --- a/src/plugins/irc-events/whois.js +++ b/src/plugins/irc-events/whois.js @@ -1,12 +1,13 @@ "use strict"; -var Chan = require("../../models/chan"); -var Msg = require("../../models/msg"); +const Chan = require("../../models/chan"); +const Msg = require("../../models/msg"); module.exports = function(irc, network) { - var client = this; + const client = this; irc.on("whois", function(data) { - var chan = network.getChannel(data.nick); + let chan = network.getChannel(data.nick); + if (typeof chan === "undefined") { chan = new Chan({ type: Chan.Type.QUERY, @@ -20,7 +21,7 @@ module.exports = function(irc, network) { }); } - var msg; + let msg; if (data.error) { msg = new Msg({ type: Msg.Type.ERROR,