Create a single helper function to write messages

This commit is contained in:
Pavel Djundik 2016-04-19 13:20:18 +03:00
parent 116dbc07be
commit 6dc807ef07
20 changed files with 81 additions and 177 deletions

View file

@ -147,13 +147,10 @@ Client.prototype.connect = function(args) {
if (config.lockNetwork) { if (config.lockNetwork) {
// This check is needed to prevent invalid user configurations // This check is needed to prevent invalid user configurations
if (args.host && args.host.length > 0 && args.host !== config.defaults.host) { if (args.host && args.host.length > 0 && args.host !== config.defaults.host) {
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed." text: "Hostname you specified is not allowed."
}) }));
});
return; return;
} }
@ -163,13 +160,10 @@ Client.prototype.connect = function(args) {
} }
if (network.host.length === 0) { if (network.host.length === 0) {
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You must specify a hostname to connect." text: "You must specify a hostname to connect."
}) }));
});
return; return;
} }
@ -264,13 +258,10 @@ Client.prototype.input = function(data) {
} }
if (!connected) { if (!connected) {
this.emit("msg", { target.chan.pushMessage(this, new Msg({
chan: target.chan.id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You are not connected to the IRC network, unable to send your command." text: "You are not connected to the IRC network, unable to send your command."
}) }));
});
} }
}; };

View file

@ -1,4 +1,5 @@
var _ = require("lodash"); var _ = require("lodash");
var Helper = require("../helper");
module.exports = Chan; module.exports = Chan;
@ -9,6 +10,7 @@ Chan.Type = {
}; };
var id = 0; var id = 0;
var config = Helper.getConfig();
function Chan(attr) { function Chan(attr) {
_.merge(this, _.extend({ _.merge(this, _.extend({
@ -23,6 +25,19 @@ function Chan(attr) {
}, attr)); }, attr));
} }
Chan.prototype.pushMessage = function(client, msg) {
this.messages.push(msg);
if (config.maxHistory >= 0 && this.messages.length > config.maxHistory) {
this.messages.splice(0, this.messages.length - config.maxHistory);
}
client.emit("msg", {
chan: this.id,
msg: msg
});
};
Chan.prototype.sortUsers = function(irc) { Chan.prototype.sortUsers = function(irc) {
var userModeSortPriority = {}; var userModeSortPriority = {};
irc.network.options.PREFIX.forEach(function(prefix, index) { irc.network.options.PREFIX.forEach(function(prefix, index) {

View file

@ -6,13 +6,10 @@ exports.allowDisconnected = true;
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
if (chan.type === "lobby") { if (chan.type === "lobby") {
this.emit("msg", { chan.pushMessage(this, new Msg({
chan: chan.id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You can not part from networks, use /quit instead." text: "You can not part from networks, use /quit instead."
}) }));
});
return; return;
} }

View file

@ -17,25 +17,19 @@ exports.input = function(network, chan, cmd, args) {
var char = target[0]; var char = target[0];
if (network.irc.network.options.CHANTYPES && network.irc.network.options.CHANTYPES.indexOf(char) !== -1) { if (network.irc.network.options.CHANTYPES && network.irc.network.options.CHANTYPES.indexOf(char) !== -1) {
this.emit("msg", { chan.pushMessage(this, new Msg({
chan: chan.id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You can not open query windows for channels, use /join instead." text: "You can not open query windows for channels, use /join instead."
}) }));
});
return; return;
} }
for (var i = 0; i < network.irc.network.options.PREFIX.length; i++) { for (var i = 0; i < network.irc.network.options.PREFIX.length; i++) {
if (network.irc.network.options.PREFIX[i].symbol === char) { if (network.irc.network.options.PREFIX[i].symbol === char) {
this.emit("msg", { chan.pushMessage(this, new Msg({
chan: chan.id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You can not open query windows for names starting with a user prefix." text: "You can not open query windows for names starting with a user prefix."
}) }));
});
return; return;
} }
} }

View file

@ -5,53 +5,44 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
text: "Network created, connecting to " + network.host + ":" + network.port + "..." text: "Network created, connecting to " + network.host + ":" + network.port + "..."
}) }));
});
irc.on("raw socket connected", function() { irc.on("raw socket connected", function() {
identd.hook(irc.connection.socket, network.username); identd.hook(irc.connection.socket, network.username);
}); });
irc.on("socket connected", function() { irc.on("socket connected", function() {
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
text: "Connected to the network." text: "Connected to the network."
}) }));
});
}); });
irc.on("socket close", function() { irc.on("socket close", function() {
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
text: "Disconnected from the network." text: "Disconnected from the network."
}) }));
});
}); });
irc.on("socket error", function(err) { irc.on("socket error", function(err) {
console.log(err); console.log(err);
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "Socket error: " + err text: "Socket error: " + err
}) }));
});
}); });
irc.on("reconnecting", function() { irc.on("reconnecting", function() {
client.emit("msg", { network.channels[0].pushMessage(client, new Msg({
chan: network.channels[0].id,
msg: new Msg({
text: "Reconnecting..." text: "Reconnecting..."
}) }));
}); });
irc.on("ping timeout", function() {
network.channels[0].pushMessage(client, new Msg({
text: "Ping timeout, disconnecting..."
}));
}); });
irc.on("server options", function(data) { irc.on("server options", function(data) {

View file

@ -17,11 +17,7 @@ module.exports = function(irc, network) {
ctcpType: data.type, ctcpType: data.type,
ctcpMessage: data.message ctcpMessage: data.message
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
irc.on("ctcp request", function(data) { irc.on("ctcp request", function(data) {

View file

@ -20,10 +20,7 @@ module.exports = function(irc, network) {
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: text, text: text,
}); });
client.emit("msg", { lobby.pushMessage(client, msg);
chan: lobby.id,
msg: msg
});
}); });
irc.on("nick in use", function(data) { irc.on("nick in use", function(data) {
@ -32,10 +29,7 @@ module.exports = function(irc, network) {
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: data.nick + ": " + (data.reason || "Nickname is already in use."), text: data.nick + ": " + (data.reason || "Nickname is already in use."),
}); });
client.emit("msg", { lobby.pushMessage(client, msg);
chan: lobby.id,
msg: msg
});
if (irc.connection.registered === false) { if (irc.connection.registered === false) {
var random = (data.nick || irc.user.nick) + Math.floor(10 + (Math.random() * 89)); var random = (data.nick || irc.user.nick) + Math.floor(10 + (Math.random() * 89));
@ -49,10 +43,7 @@ module.exports = function(irc, network) {
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: data.nick + ": " + (data.reason || "Nickname is invalid."), text: data.nick + ": " + (data.reason || "Nickname is invalid."),
}); });
client.emit("msg", { lobby.pushMessage(client, msg);
chan: lobby.id,
msg: msg
});
if (irc.connection.registered === false) { if (irc.connection.registered === false) {
var random = "i" + Math.random().toString(36).substr(2, 10); // 'i' so it never begins with a number var random = "i" + Math.random().toString(36).substr(2, 10); // 'i' so it never begins with a number

View file

@ -16,10 +16,6 @@ module.exports = function(irc, network) {
channel: data.channel, channel: data.channel,
invitedYou: data.invited === irc.user.nick invitedYou: data.invited === irc.user.nick
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}; };

View file

@ -29,10 +29,6 @@ module.exports = function(irc, network) {
type: Msg.Type.JOIN, type: Msg.Type.JOIN,
self: data.nick === irc.user.nick self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}; };

View file

@ -29,10 +29,6 @@ module.exports = function(irc, network) {
highlight: data.kicked === irc.user.nick, highlight: data.kicked === irc.user.nick,
self: data.nick === irc.user.nick self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}; };

View file

@ -35,11 +35,7 @@ module.exports = function(irc, network) {
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.TOGGLE, type: Msg.Type.TOGGLE,
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
var link = escapeHeader(links[0]); var link = escapeHeader(links[0]);
fetch(link, function(res) { fetch(link, function(res) {

View file

@ -1,10 +1,8 @@
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
var Helper = require("../../helper");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
var config = Helper.getConfig();
irc.on("notice", function(data) { irc.on("notice", function(data) {
// Some servers send notices without any nickname // Some servers send notices without any nickname
@ -83,15 +81,6 @@ module.exports = function(irc, network) {
self: self, self: self,
highlight: highlight highlight: highlight
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
if (config.maxHistory >= 0 && chan.messages.length > config.maxHistory) {
chan.messages.splice(0, chan.messages.length - config.maxHistory);
}
client.emit("msg", {
chan: chan.id,
msg: msg
});
} }
}; };

View file

@ -38,11 +38,7 @@ module.exports = function(irc, network) {
text: text, text: text,
self: data.nick === irc.user.nick self: data.nick === irc.user.nick
}); });
targetChan.messages.push(msg); targetChan.pushMessage(client, msg);
client.emit("msg", {
chan: targetChan.id,
msg: msg,
});
} }
if (usersUpdated) { if (usersUpdated) {

View file

@ -11,11 +11,7 @@ module.exports = function(irc, network) {
type: Msg.Type.MOTD, type: Msg.Type.MOTD,
text: text text: text
}); });
lobby.messages.push(msg); lobby.pushMessage(client, msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
}); });
} }
@ -24,11 +20,7 @@ module.exports = function(irc, network) {
type: Msg.Type.MOTD, type: Msg.Type.MOTD,
text: data.error text: data.error
}); });
lobby.messages.push(msg); lobby.pushMessage(client, msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
} }
}); });
}; };

View file

@ -10,11 +10,7 @@ module.exports = function(irc, network) {
var msg = new Msg({ var msg = new Msg({
text: "You're now known as " + data.new_nick, text: "You're now known as " + data.new_nick,
}); });
lobby.messages.push(msg); lobby.pushMessage(client, msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
self = true; self = true;
client.save(); client.save();
client.emit("nick", { client.emit("nick", {
@ -41,11 +37,7 @@ module.exports = function(irc, network) {
new_nick: data.new_nick, new_nick: data.new_nick,
self: self self: self
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}); });
}; };

View file

@ -29,11 +29,7 @@ module.exports = function(irc, network) {
hostmask: data.ident + "@" + data.hostname, hostmask: data.ident + "@" + data.hostname,
from: from from: from
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
} }
}); });
}; };

View file

@ -22,11 +22,7 @@ module.exports = function(irc, network) {
hostmask: data.ident + "@" + data.hostname, hostmask: data.ident + "@" + data.hostname,
from: from from: from
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}); });
}; };

View file

@ -16,11 +16,7 @@ module.exports = function(irc, network) {
text: data.topic, text: data.topic,
self: data.nick === irc.user.nick self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
chan.topic = data.topic; chan.topic = data.topic;
client.emit("topic", { client.emit("topic", {
@ -42,10 +38,6 @@ module.exports = function(irc, network) {
when: new Date(data.when * 1000), when: new Date(data.when * 1000),
self: data.nick === irc.user.nick self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}; };

View file

@ -8,11 +8,7 @@ module.exports = function(irc, network) {
var msg = new Msg({ var msg = new Msg({
text: "You're now known as " + data.nick text: "You're now known as " + data.nick
}); });
lobby.messages.push(msg); lobby.pushMessage(client, msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
client.save(); client.save();
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.id,

View file

@ -30,10 +30,6 @@ module.exports = function(irc, network) {
}); });
} }
chan.messages.push(msg); chan.pushMessage(client, msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}); });
}; };