Merge pull request #266 from thelounge/xpaw/server-log

Create a single helper function to write messages
This commit is contained in:
Jérémie Astori 2016-04-23 20:27:09 -04:00
commit 8c41975dd7
22 changed files with 99 additions and 189 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

@ -8,14 +8,6 @@ program.version(pkg.version, "-v, --version");
program.option(""); program.option("");
program.option(" --home <path>" , "home path"); program.option(" --home <path>" , "home path");
require("./start");
require("./config");
require("./list");
require("./add");
require("./remove");
require("./reset");
require("./edit");
var argv = program.parseOptions(process.argv); var argv = program.parseOptions(process.argv);
if (program.home) { if (program.home) {
Helper.HOME = program.home; Helper.HOME = program.home;
@ -32,6 +24,14 @@ if (!fs.existsSync(config)) {
console.log(config); console.log(config);
} }
require("./start");
require("./config");
require("./list");
require("./add");
require("./remove");
require("./reset");
require("./edit");
program.parse(argv.args); program.parse(argv.args);
if (!program.args.length) { if (!program.args.length) {

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,25 @@ function Chan(attr) {
}, attr)); }, attr));
} }
Chan.prototype.pushMessage = function(client, msg) {
client.emit("msg", {
chan: this.id,
msg: msg
});
// Never store messages in public mode as the session
// is completely destroyed when the page gets closed
if (config.public) {
return;
}
this.messages.push(msg);
if (config.maxHistory >= 0 && this.messages.length > config.maxHistory) {
this.messages.splice(0, this.messages.length - config.maxHistory);
}
};
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
});
}); });
}; };

View File

@ -3,6 +3,7 @@ var util = require("util");
var _ = require("lodash"); var _ = require("lodash");
var express = require("express"); var express = require("express");
var Network = require("../src/models/network"); var Network = require("../src/models/network");
var Chan = require("../src/models/chan");
function MockClient(opts) { function MockClient(opts) {
this.user = {nick: "test-user"}; this.user = {nick: "test-user"};
@ -30,10 +31,9 @@ module.exports = {
createNetwork: function() { createNetwork: function() {
return new Network({ return new Network({
host: "example.com", host: "example.com",
channels: [{ channels: [new Chan({
name: "#test-channel", name: "#test-channel"
messages: [] })]
}]
}); });
}, },
createWebserver: function() { createWebserver: function() {