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

View File

@ -1,4 +1,5 @@
var _ = require("lodash");
var Helper = require("../helper");
module.exports = Chan;
@ -9,6 +10,7 @@ Chan.Type = {
};
var id = 0;
var config = Helper.getConfig();
function Chan(attr) {
_.merge(this, _.extend({
@ -23,6 +25,19 @@ function Chan(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) {
var userModeSortPriority = {};
irc.network.options.PREFIX.forEach(function(prefix, index) {

View File

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

View File

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

View File

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

View File

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

View File

@ -20,10 +20,7 @@ module.exports = function(irc, network) {
type: Msg.Type.ERROR,
text: text,
});
client.emit("msg", {
chan: lobby.id,
msg: msg
});
lobby.pushMessage(client, msg);
});
irc.on("nick in use", function(data) {
@ -32,10 +29,7 @@ module.exports = function(irc, network) {
type: Msg.Type.ERROR,
text: data.nick + ": " + (data.reason || "Nickname is already in use."),
});
client.emit("msg", {
chan: lobby.id,
msg: msg
});
lobby.pushMessage(client, msg);
if (irc.connection.registered === false) {
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,
text: data.nick + ": " + (data.reason || "Nickname is invalid."),
});
client.emit("msg", {
chan: lobby.id,
msg: msg
});
lobby.pushMessage(client, msg);
if (irc.connection.registered === false) {
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,
invitedYou: data.invited === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
chan.pushMessage(client, msg);
});
};

View File

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

View File

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

View File

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

View File

@ -1,10 +1,8 @@
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
var Helper = require("../../helper");
module.exports = function(irc, network) {
var client = this;
var config = Helper.getConfig();
irc.on("notice", function(data) {
// Some servers send notices without any nickname
@ -83,15 +81,6 @@ module.exports = function(irc, network) {
self: self,
highlight: highlight
});
chan.messages.push(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
});
chan.pushMessage(client, msg);
}
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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