Merge pull request #167 from thelounge/irc-framework

Drop slate-irc, switch to irc-framework
This commit is contained in:
Jérémie Astori 2016-04-13 01:28:32 -04:00
commit 116dbc07be
51 changed files with 644 additions and 401 deletions

View file

@ -789,7 +789,8 @@ button,
#chat .nick .text, #chat .nick .text,
#chat .part .text, #chat .part .text,
#chat .quit .text, #chat .quit .text,
#chat .topic .text { #chat .topic .text,
#chat .topic_set_by .text {
color: #999; color: #999;
} }
@ -826,6 +827,11 @@ button,
color: #2ecc40; color: #2ecc40;
} }
#chat .ctcp .from:before {
font-family: FontAwesome;
content: "\f0f6";
}
#chat .whois .from:before { #chat .whois .from:before {
font-family: FontAwesome; font-family: FontAwesome;
content: "\f007"; content: "\f007";
@ -867,6 +873,10 @@ button,
color: #f00; color: #f00;
} }
#chat .msg.toggle .time {
visibility: hidden;
}
#chat .toggle-button { #chat .toggle-button {
background: #f5f5f5; background: #f5f5f5;
border-radius: 2px; border-radius: 2px;

View file

@ -0,0 +1,7 @@
Handlebars.registerHelper(
"localeDate", function(date) {
date = new Date(date);
return date.toLocaleString();
}
);

View file

@ -0,0 +1,7 @@
"use strict";
Handlebars.registerHelper(
"toJSON", function(context) {
return JSON.stringify(context);
}
);

View file

@ -221,8 +221,10 @@ $(function() {
"part", "part",
"quit", "quit",
"topic", "topic",
"topic_set_by",
"action", "action",
"whois", "whois",
"ctcp",
].indexOf(type) !== -1) { ].indexOf(type) !== -1) {
data.msg.template = "actions/" + type; data.msg.template = "actions/" + type;
msg = $(render("msg_action", data.msg)); msg = $(render("msg_action", data.msg));
@ -309,6 +311,10 @@ $(function() {
sortable(); sortable();
}); });
socket.on("network_changed", function(data) {
sidebar.find("#network-" + data.network).data("options", data.serverOptions);
});
socket.on("nick", function(data) { socket.on("nick", function(data) {
var id = data.network; var id = data.network;
var nick = data.nick; var nick = data.nick;
@ -769,7 +775,7 @@ $(function() {
if (msg.type === "invite") { if (msg.type === "invite") {
title = "New channel invite:"; title = "New channel invite:";
body = msg.from + " invited you to " + msg.text; body = msg.from + " invited you to " + msg.channel;
} else { } else {
title = msg.from; title = msg.from;
if (!isQuery) { if (!isQuery) {

View file

@ -0,0 +1,2 @@
<a href="#" class="user" data-name="{{from}}">{{from}}</a>
<b>{{ctcpType}}</b> {{ctcpMessage}}

View file

@ -3,7 +3,7 @@ invited
{{#if invitedYou}} {{#if invitedYou}}
you you
{{else}} {{else}}
<a href="#" class="user" data-name="{{target}}">{{target}}</a> <a href="#" class="user" data-name="{{invited}}">{{invited}}</a>
{{/if}} {{/if}}
to to
{{{parse text}}} {{{parse channel}}}

View file

@ -1,3 +1,3 @@
<a href="#" class="user" data-name="{{from}}">{{mode}}{{from}}</a> <a href="#" class="user" data-name="{{nick}}">{{mode}}{{nick}}</a>
is now known as is now known as
<a href="#" class="user" data-name="{{text}}">{{mode}}{{text}}</a> <a href="#" class="user" data-name="{{new_nick}}">{{mode}}{{new_nick}}</a>

View file

@ -1,8 +1,8 @@
{{#if isSetByChan}} {{#if from}}
The topic is:
{{else}}
<a href="#" class="user" data-name="{{from}}">{{mode}}{{from}}</a> <a href="#" class="user" data-name="{{from}}">{{mode}}{{from}}</a>
has changed the topic to: has changed the topic to:
{{else}}
The topic is:
{{/if}} {{/if}}
<span class="new-topic">{{{parse text}}}</span> <span class="new-topic">{{{parse text}}}</span>

View file

@ -0,0 +1 @@
Topic set by <a href="#" class="user" data-name="{{nick}}">{{mode}}{{nick}}</a> on {{localeDate when}}

View file

@ -1,26 +1,35 @@
<div> <div>
<a href="#" class="user" data-name="{{whois.nickname}}">{{whois.nickname}}</a> <a href="#" class="user" data-name="{{whois.nick}}">{{whois.nick}}</a>
<i class="hostmask">({{whois.username}}@{{whois.hostname}})</i>: <i class="hostmask">({{whois.user}}@{{whois.host}})</i>:
<b>{{whois.realname}}</b> <b>{{whois.real_name}}</b>
</div> </div>
{{#if whois.account}}
<div>
<a href="#" class="user" data-name="{{whois.nick}}">{{whois.nick}}</a>
is logged in as <b>{{whois.account}}</b>
</div>
{{/if}}
{{#if whois.channels}} {{#if whois.channels}}
<div> <div>
<a href="#" class="user" data-name="{{whois.nickname}}">{{whois.nickname}}</a> <a href="#" class="user" data-name="{{whois.nick}}">{{whois.nick}}</a>
is on the following channels: is on the following channels: {{{parse whois.channels}}}
{{#each whois.channels}}
{{{parse this}}}
{{/each}}
</div> </div>
{{/if}} {{/if}}
{{#if whois.server}} {{#if whois.server}}
<div> <div>
<a href="#" class="user" data-name="{{whois.nickname}}">{{whois.nickname}}</a> <a href="#" class="user" data-name="{{whois.nick}}">{{whois.nick}}</a>
is connected to {{whois.server}} is connected to {{whois.server}} <i>({{whois.server_info}})</i>
</div>
{{/if}}
{{#if whois.secure}}
<div>
<a href="#" class="user" data-name="{{whois.nick}}">{{whois.nick}}</a>
is using a secure connection
</div> </div>
{{/if}} {{/if}}
{{#if whois.away}} {{#if whois.away}}
<div> <div>
<a href="#" class="user" data-name="{{whois.nickname}}">{{whois.nickname}}</a> <a href="#" class="user" data-name="{{whois.nick}}">{{whois.nick}}</a>
is away <i>({{whois.away}})</i> is away <i>({{whois.away}})</i>
</div> </div>
{{/if}} {{/if}}

View file

@ -1,5 +1,5 @@
{{#each networks}} {{#each networks}}
<section id="network-{{id}}" class="network" data-id="{{id}}" data-nick="{{nick}}"> <section id="network-{{id}}" class="network" data-id="{{id}}" data-nick="{{nick}}" data-options="{{toJSON serverOptions}}">
{{partial "chan"}} {{partial "chan"}}
</section> </section>
{{/each}} {{/each}}

View file

@ -33,6 +33,7 @@
"event-stream": "3.3.2", "event-stream": "3.3.2",
"express": "4.13.4", "express": "4.13.4",
"lodash": "4.6.1", "lodash": "4.6.1",
"irc-framework": "1.0.10",
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"moment": "2.12.0", "moment": "2.12.0",
"read": "1.0.7", "read": "1.0.7",

View file

@ -1,19 +1,17 @@
var _ = require("lodash"); var _ = require("lodash");
var Chan = require("./models/chan"); var Chan = require("./models/chan");
var crypto = require("crypto"); var crypto = require("crypto");
var identd = require("./identd");
var log = require("./log"); var log = require("./log");
var net = require("net");
var Msg = require("./models/msg"); var Msg = require("./models/msg");
var Network = require("./models/network"); var Network = require("./models/network");
var slate = require("slate-irc"); var ircFramework = require("irc-framework");
var tls = require("tls");
var Helper = require("./helper"); var Helper = require("./helper");
module.exports = Client; module.exports = Client;
var id = 0; var id = 0;
var events = [ var events = [
"connection",
"ctcp", "ctcp",
"error", "error",
"invite", "invite",
@ -25,7 +23,6 @@ var events = [
"link", "link",
"names", "names",
"nick", "nick",
"notice",
"part", "part",
"quit", "quit",
"topic", "topic",
@ -33,7 +30,7 @@ var events = [
"whois" "whois"
]; ];
var inputs = [ var inputs = [
// These inputs are sorted in order that is most likely to be used "ctcp",
"msg", "msg",
"part", "part",
"action", "action",
@ -50,7 +47,7 @@ var inputs = [
var path = "./plugins/inputs/" + name; var path = "./plugins/inputs/" + name;
var plugin = require(path); var plugin = require(path);
plugin.commands.forEach(function(command) { plugin.commands.forEach(function(command) {
plugins[command] = plugin.input; plugins[command] = plugin;
}); });
return plugins; return plugins;
}, {}); }, {});
@ -129,106 +126,68 @@ Client.prototype.connect = function(args) {
var config = Helper.getConfig(); var config = Helper.getConfig();
var client = this; var client = this;
if (config.lockNetwork) { var nick = args.nick || "lounge-user";
// This check is needed to prevent invalid user configurations
if (args.host && args.host.length > 0 && args.host !== config.defaults.host) {
var invalidHostnameMsg = new Msg({
type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed."
});
client.emit("msg", {
msg: invalidHostnameMsg
});
return;
}
args.host = config.defaults.host; var network = new Network({
args.port = config.defaults.port;
args.tls = config.defaults.tls;
}
var server = {
name: args.name || "", name: args.name || "",
host: args.host || "", host: args.host || "",
port: parseInt(args.port, 10) || (args.tls ? 6697 : 6667), port: parseInt(args.port, 10) || (args.tls ? 6697 : 6667),
rejectUnauthorized: false
};
if (server.host.length === 0) {
var emptyHostnameMsg = new Msg({
type: Msg.Type.ERROR,
text: "You must specify a hostname to connect."
});
client.emit("msg", {
msg: emptyHostnameMsg
});
return;
}
if (config.bind) {
server.localAddress = config.bind;
if (args.tls) {
var socket = net.connect(server);
server.socket = socket;
}
}
var stream = args.tls ? tls.connect(server) : net.connect(server);
stream.on("error", function(e) {
console.log("Client#connect():\n" + e);
stream.end();
var msg = new Msg({
type: Msg.Type.ERROR,
text: "Connection error."
});
client.emit("msg", {
msg: msg
});
});
var nick = args.nick || "lounge-user";
var username = args.username || nick.replace(/[^a-zA-Z0-9]/g, "");
var realname = args.realname || "The Lounge User";
var irc = slate(stream);
identd.hook(stream, username);
if (args.password) {
irc.pass(args.password);
}
irc.me = nick;
irc.nick(nick);
irc.user(username, realname);
var network = new Network({
name: server.name,
host: server.host,
port: server.port,
tls: !!args.tls, tls: !!args.tls,
password: args.password, password: args.password,
username: username, username: args.username || nick.replace(/[^a-zA-Z0-9]/g, ""),
realname: realname, realname: args.realname || "The Lounge User",
commands: args.commands commands: args.commands
}); });
network.irc = irc;
client.networks.push(network); client.networks.push(network);
client.emit("network", { client.emit("network", {
network: network network: network
}); });
events.forEach(function(plugin) { if (config.lockNetwork) {
var path = "./plugins/irc-events/" + plugin; // This check is needed to prevent invalid user configurations
require(path).apply(client, [ if (args.host && args.host.length > 0 && args.host !== config.defaults.host) {
irc, client.emit("msg", {
network chan: network.channels[0].id,
]); msg: new Msg({
type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed."
})
});
return;
}
network.host = config.defaults.host;
network.port = config.defaults.port;
network.tls = config.defaults.tls;
}
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."
})
});
return;
}
network.irc = new ircFramework.Client();
network.irc.connect({
host: network.host,
port: network.port,
nick: nick,
username: network.username,
gecos: network.realname,
password: network.password,
tls: network.tls,
localAddress: config.bind,
rejectUnauthorized: false,
auto_reconnect: false, // TODO: Enable auto reconnection
}); });
irc.once("welcome", function() { network.irc.on("registered", function() {
var delay = 1000; var delay = 1000;
var commands = args.commands; var commands = args.commands;
if (Array.isArray(commands)) { if (Array.isArray(commands)) {
@ -242,18 +201,23 @@ Client.prototype.connect = function(args) {
delay += 1000; delay += 1000;
}); });
} }
setTimeout(function() {
irc.write("PING " + network.host);
}, delay);
});
irc.once("pong", function() {
var join = (args.join || ""); var join = (args.join || "");
if (join) { if (join) {
join = join.replace(/\,/g, " ").split(/\s+/g); setTimeout(function() {
irc.join(join); join = join.split(/\s+/);
network.irc.join(join[0], join[1]);
}, delay);
} }
}); });
events.forEach(function(plugin) {
var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
network.irc,
network
]);
});
}; };
Client.prototype.setPassword = function(hash) { Client.prototype.setPassword = function(hash) {
@ -286,10 +250,27 @@ Client.prototype.input = function(data) {
var args = text.split(" "); var args = text.split(" ");
var cmd = args.shift().toLowerCase(); var cmd = args.shift().toLowerCase();
var irc = target.network.irc;
var connected = irc && irc.connection && irc.connection.connected;
if (cmd in inputs) { if (cmd in inputs) {
inputs[cmd].apply(client, [target.network, target.chan, cmd, args]); var plugin = inputs[cmd];
} else { if (connected || plugin.allowDisconnected) {
target.network.irc.write(text); connected = true;
plugin.input.apply(client, [target.network, target.chan, cmd, args]);
}
} else if (connected) {
irc.raw(text);
}
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."
})
});
} }
}; };
@ -375,11 +356,8 @@ Client.prototype.quit = function() {
} }
} }
this.networks.forEach(function(network) { this.networks.forEach(function(network) {
var irc = network.irc; if (network.irc) {
if (network.connected) { network.irc.quit("Page closed");
irc.quit();
} else {
irc.stream.end();
} }
}); });
}; };

View file

@ -23,18 +23,21 @@ function Chan(attr) {
}, attr)); }, attr));
} }
Chan.prototype.sortUsers = function() { Chan.prototype.sortUsers = function(irc) {
this.users = _.sortBy( var userModeSortPriority = {};
this.users, irc.network.options.PREFIX.forEach(function(prefix, index) {
function(u) { return u.name.toLowerCase(); } userModeSortPriority[prefix.symbol] = index;
); });
["+", "%", "@", "&", "~"].forEach(function(mode) { userModeSortPriority[""] = 99; // No mode is lowest
this.users = _.remove(
this.users, this.users = this.users.sort(function(a, b) {
function(u) { return u.mode === mode; } if (a.mode === b.mode) {
).concat(this.users); return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
}, this); }
return userModeSortPriority[a.mode] - userModeSortPriority[b.mode];
});
}; };
Chan.prototype.getMode = function(name) { Chan.prototype.getMode = function(name) {

View file

@ -14,7 +14,9 @@ Msg.Type = {
PART: "part", PART: "part",
QUIT: "quit", QUIT: "quit",
TOGGLE: "toggle", TOGGLE: "toggle",
CTCP: "ctcp",
TOPIC: "topic", TOPIC: "topic",
TOPIC_SET_BY: "topic_set_by",
WHOIS: "whois" WHOIS: "whois"
}; };
@ -27,8 +29,13 @@ function Msg(attr) {
from: "", from: "",
id: id++, id: id++,
text: "", text: "",
time: new Date(),
type: Msg.Type.MESSAGE, type: Msg.Type.MESSAGE,
self: false self: false
}, attr)); }, attr));
if (this.time > 0) {
this.time = new Date(this.time);
} else {
this.time = new Date();
}
} }

View file

@ -16,9 +16,11 @@ function Network(attr) {
username: "", username: "",
realname: "", realname: "",
channels: [], channels: [],
connected: false,
id: id++, id: id++,
irc: null, irc: null,
serverOptions: {
PREFIX: [],
},
}, attr)); }, attr));
this.name = attr.name || prettify(attr.host); this.name = attr.name || prettify(attr.host);
this.channels.unshift( this.channels.unshift(
@ -30,7 +32,7 @@ function Network(attr) {
} }
Network.prototype.toJSON = function() { Network.prototype.toJSON = function() {
var json = _.extend(this, {nick: (this.irc || {}).me || ""}); var json = _.extend(this, {nick: (this.irc && this.irc.user.nick) || ""});
return _.omit(json, "irc", "password"); return _.omit(json, "irc", "password");
}; };
@ -45,7 +47,7 @@ Network.prototype.export = function() {
"realname", "realname",
"commands" "commands"
]); ]);
network.nick = (this.irc || {}).me; network.nick = (this.irc && this.irc.user.nick) || "";
network.join = _.map( network.join = _.map(
_.filter(this.channels, {type: "channel"}), _.filter(this.channels, {type: "channel"}),
"name" "name"
@ -53,6 +55,14 @@ Network.prototype.export = function() {
return network; return network;
}; };
Network.prototype.getChannel = function(name) {
name = name.toLowerCase();
return _.find(this.channels, function(that) {
return that.name.toLowerCase() === name;
});
};
function prettify(host) { function prettify(host) {
var name = capitalize(host.split(".")[1]); var name = capitalize(host.split(".")[1]);
if (!name) { if (!name) {

View file

@ -3,6 +3,10 @@ var _ = require("lodash");
module.exports = User; module.exports = User;
function User(attr) { function User(attr) {
// TODO: Remove this
attr.name = attr.name || attr.nick;
attr.mode = attr.mode || (attr.modes && attr.modes[0]) || "";
_.merge(this, _.extend({ _.merge(this, _.extend({
mode: "", mode: "",
name: "" name: ""

View file

@ -2,25 +2,24 @@ exports.commands = ["slap", "me"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
var irc = network.irc; var irc = network.irc;
var text;
switch (cmd) { switch (cmd) {
case "slap": case "slap":
var slap = "slaps " + args[0] + " around a bit with a large trout"; text = "slaps " + args[0] + " around a bit with a large trout";
/* fall through */ /* fall through */
case "me": case "me":
if (args.length === 0) { if (args.length === 0) {
break; break;
} }
var text = slap || args.join(" "); text = text || args.join(" ");
irc.action(
chan.name, irc.say(chan.name, "\u0001ACTION " + text + "\u0001");
text irc.emit("action", {
); nick: irc.user.nick,
irc.emit("message", { target: chan.name,
from: irc.me, message: text
to: chan.name,
message: "\u0001ACTION " + text
}); });
break; break;
} }

View file

@ -1,4 +1,5 @@
exports.commands = ["connect", "server"]; exports.commands = ["connect", "server"];
exports.allowDisconnected = true;
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
if (args.length === 0) { if (args.length === 0) {

View file

@ -0,0 +1,8 @@
exports.commands = ["ctcp"];
exports.input = function(network, chan, cmd, args) {
if (args.length > 1) {
var irc = network.irc;
irc.ctcpRequest(args[0], args.slice(1).join(" "));
}
};

View file

@ -4,9 +4,9 @@ exports.input = function(network, chan, cmd, args) {
var irc = network.irc; var irc = network.irc;
if (args.length === 2) { if (args.length === 2) {
irc.invite(args[0], args[1]); // Channel provided in the command irc.raw("INVITE", args[0], args[1]); // Channel provided in the command
} else if (args.length === 1 && chan.type === "channel") { } else if (args.length === 1 && chan.type === "channel") {
irc.invite(args[0], chan.name); // Current channel irc.raw("INVITE", args[0], chan.name); // Current channel
} }
return true; return true;

View file

@ -3,7 +3,7 @@ exports.commands = ["kick"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.kick(chan.name, args[0]); irc.raw("KICK", chan.name, args[0], args.slice(1).join(" "));
} }
return true; return true;

View file

@ -21,12 +21,9 @@ exports.input = function(network, chan, cmd, args) {
mode = args[0]; mode = args[0];
user = args[1]; user = args[1];
} }
var irc = network.irc; var irc = network.irc;
irc.mode( irc.raw("MODE", chan.name, mode, user);
chan.name,
mode,
user
);
return true; return true;
}; };

View file

@ -1,11 +1,10 @@
var _ = require("lodash");
exports.commands = ["msg", "say"]; exports.commands = ["msg", "say"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
if (args.length === 0 || args[0] === "") { if (args.length === 0 || args[0] === "") {
return true; return true;
} }
var irc = network.irc; var irc = network.irc;
var target = ""; var target = "";
if (cmd === "msg") { if (cmd === "msg") {
@ -16,13 +15,15 @@ exports.input = function(network, chan, cmd, args) {
} else { } else {
target = chan.name; target = chan.name;
} }
var msg = args.join(" "); var msg = args.join(" ");
irc.send(target, msg); irc.say(target, msg);
var channel = _.find(network.channels, {name: target});
var channel = network.getChannel(target);
if (typeof channel !== "undefined") { if (typeof channel !== "undefined") {
irc.emit("message", { irc.emit("privmsg", {
from: irc.me, nick: irc.user.nick,
to: channel.name, target: channel.name,
message: msg message: msg
}); });
} }

View file

@ -1,6 +1,3 @@
var _ = require("lodash");
var Msg = require("../../models/msg");
exports.commands = ["notice"]; exports.commands = ["notice"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
@ -12,22 +9,16 @@ exports.input = function(network, chan, cmd, args) {
var irc = network.irc; var irc = network.irc;
irc.notice(args[0], message); irc.notice(args[0], message);
var targetChan = _.find(network.channels, {name: args[0]}); var targetChan = network.getChannel(args[0]);
if (typeof targetChan === "undefined") { if (typeof targetChan === "undefined") {
message = "{to " + args[0] + "} " + message; message = "{to " + args[0] + "} " + message;
targetChan = chan; targetChan = chan;
} }
var msg = new Msg({ irc.emit("notice", {
type: Msg.Type.NOTICE, nick: irc.user.nick,
mode: targetChan.getMode(irc.me), target: targetChan.name,
from: irc.me, message: message
text: message
});
targetChan.messages.push(msg);
this.emit("msg", {
chan: targetChan.id,
msg: msg
}); });
return true; return true;

View file

@ -2,6 +2,7 @@ var _ = require("lodash");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
exports.commands = ["close", "leave", "part"]; exports.commands = ["close", "leave", "part"];
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") {
@ -15,8 +16,8 @@ exports.input = function(network, chan, cmd, args) {
return; return;
} }
if (chan.type === "channel") {
var irc = network.irc; var irc = network.irc;
if (irc && chan.type === "channel") {
irc.part(chan.name, args.join(" ")); irc.part(chan.name, args.join(" "));
} }

View file

@ -1,5 +1,6 @@
var _ = require("lodash"); var _ = require("lodash");
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
exports.commands = ["query"]; exports.commands = ["query"];
@ -14,11 +15,31 @@ exports.input = function(network, chan, cmd, args) {
return; return;
} }
// If target doesn't start with an allowed character, ignore var char = target[0];
if (!/^[a-zA-Z_\\\[\]{}^`|]/.test(target)) { 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."
})
});
return; 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."
})
});
return;
}
}
var newChan = new Chan({ var newChan = new Chan({
type: Chan.Type.QUERY, type: Chan.Type.QUERY,
name: target name: target

View file

@ -1,6 +1,7 @@
var _ = require("lodash"); var _ = require("lodash");
exports.commands = ["quit", "disconnect"]; exports.commands = ["quit", "disconnect"];
exports.allowDisconnected = true;
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
var client = this; var client = this;
@ -13,7 +14,9 @@ exports.input = function(network, chan, cmd, args) {
network: network.id network: network.id
}); });
if (irc) {
irc.quit(quitMessage); irc.quit(quitMessage);
}
return true; return true;
}; };

View file

@ -3,7 +3,7 @@ exports.commands = ["raw", "send", "quote"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc; var irc = network.irc;
irc.write(args.join(" ")); irc.raw(args);
} }
return true; return true;

View file

@ -1,12 +1,8 @@
exports.commands = ["topic"]; exports.commands = ["topic"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
var msg = "TOPIC";
msg += " " + chan.name;
msg += args[0] ? (" :" + args.join(" ")) : "";
var irc = network.irc; var irc = network.irc;
irc.write(msg); irc.raw("TOPIC", chan.name, args.join(" "));
return true; return true;
}; };

View file

@ -0,0 +1,75 @@
var _ = require("lodash");
var identd = require("../../identd");
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 + "..."
})
});
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."
})
});
});
irc.on("socket close", function() {
client.emit("msg", {
chan: network.channels[0].id,
msg: 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
})
});
});
irc.on("reconnecting", function() {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
text: "Reconnecting..."
})
});
});
irc.on("server options", function(data) {
if (network.serverOptions.PREFIX === data.options.PREFIX) {
return;
}
network.prefixLookup = {};
_.each(data.options.PREFIX, function(mode) {
network.prefixLookup[mode.mode] = mode.symbol;
});
network.serverOptions.PREFIX = data.options.PREFIX;
client.emit("network_changed", {
network: network.id,
serverOptions: network.serverOptions
});
});
};

View file

@ -1,22 +1,38 @@
var pkg = require(process.cwd() + "/package.json"); var pkg = require(process.cwd() + "/package.json");
var Msg = require("../../models/msg");
module.exports = function(irc/* , network */) { module.exports = function(irc, network) {
irc.on("message", function(data) { var client = this;
if (data.message.indexOf("\001") !== 0) {
return; irc.on("ctcp response", function(data) {
var chan = network.getChannel(data.nick);
if (typeof chan === "undefined") {
chan = network.channels[0];
} }
var msg = data.message.replace(/\001/g, "");
var split = msg.split(" "); var msg = new Msg({
switch (split[0]) { type: Msg.Type.CTCP,
time: data.time,
from: data.nick,
ctcpType: data.type,
ctcpMessage: data.message
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
irc.on("ctcp request", function(data) {
switch (data.type) {
case "VERSION": case "VERSION":
irc.ctcp( irc.ctcpResponse(data.nick, "VERSION", pkg.name + " " + pkg.version);
data.from,
"VERSION " + pkg.name + " " + pkg.version
);
break; break;
case "PING": case "PING":
var split = data.message.split(" ");
if (split.length === 2) { if (split.length === 2) {
irc.ctcp(data.from, "PING " + split[1]); irc.ctcpResponse(data.nick, "PING", split[1]);
} }
break; break;
} }

View file

@ -2,21 +2,61 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("errors", function(data) {
// TODO: remove later
irc.on("irc_error", function(data) {
console.log("Got an irc_error");
irc.emit("error", data);
});
irc.on("error", function(data) {
console.log("error", data);
var text = data.error;
if (data.reason) {
text = data.reason + " (" + text + ")";
}
var lobby = network.channels[0]; var lobby = network.channels[0];
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: data.message, text: text,
}); });
client.emit("msg", { client.emit("msg", {
chan: lobby.id, chan: lobby.id,
msg: msg msg: msg
}); });
if (!network.connected) { });
if (data.cmd === "ERR_NICKNAMEINUSE") {
var random = irc.me + Math.floor(10 + (Math.random() * 89)); irc.on("nick in use", function(data) {
irc.nick(random); var lobby = network.channels[0];
var msg = new Msg({
type: Msg.Type.ERROR,
text: data.nick + ": " + (data.reason || "Nickname is already in use."),
});
client.emit("msg", {
chan: lobby.id,
msg: msg
});
if (irc.connection.registered === false) {
var random = (data.nick || irc.user.nick) + Math.floor(10 + (Math.random() * 89));
irc.changeNick(random);
} }
});
irc.on("nick invalid", function(data) {
var lobby = network.channels[0];
var msg = new Msg({
type: Msg.Type.ERROR,
text: data.nick + ": " + (data.reason || "Nickname is invalid."),
});
client.emit("msg", {
chan: lobby.id,
msg: msg
});
if (irc.connection.registered === false) {
var random = "i" + Math.random().toString(36).substr(2, 10); // 'i' so it never begins with a number
irc.changeNick(random);
} }
}); });
}; };

View file

@ -1,22 +1,20 @@
var _ = require("lodash");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("invite", function(data) { irc.on("invite", function(data) {
var target = data.to; var chan = network.getChannel(data.channel);
var chan = _.find(network.channels, {name: data.channel});
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
chan = network.channels[0]; chan = network.channels[0];
} }
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.INVITE, type: Msg.Type.INVITE,
from: data.from, time: data.time,
target: target, from: data.nick,
text: data.channel, invited: data.invited,
invitedYou: target.toLowerCase() === irc.me.toLowerCase() channel: data.channel,
invitedYou: data.invited === irc.user.nick
}); });
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {

View file

@ -1,4 +1,3 @@
var _ = require("lodash");
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
var User = require("../../models/user"); var User = require("../../models/user");
@ -6,7 +5,7 @@ var User = require("../../models/user");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("join", function(data) { irc.on("join", function(data) {
var chan = _.find(network.channels, {name: data.channel}); var chan = network.getChannel(data.channel);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
chan = new Chan({ chan = new Chan({
name: data.channel name: data.channel
@ -18,20 +17,17 @@ module.exports = function(irc, network) {
chan: chan chan: chan
}); });
} }
chan.users.push(new User({name: data.nick})); chan.users.push(new User({nick: data.nick, modes: ""}));
chan.sortUsers(); chan.sortUsers(irc);
client.emit("users", { client.emit("users", {
chan: chan.id chan: chan.id
}); });
var self = false;
if (data.nick.toLowerCase() === irc.me.toLowerCase()) {
self = true;
}
var msg = new Msg({ var msg = new Msg({
time: data.time,
from: data.nick, from: data.nick,
hostmask: data.hostmask.username + "@" + data.hostmask.hostname, hostmask: data.ident + "@" + data.hostname,
type: Msg.Type.JOIN, type: Msg.Type.JOIN,
self: self self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {

View file

@ -4,35 +4,30 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("kick", function(data) { irc.on("kick", function(data) {
var from = data.nick; var chan = network.getChannel(data.channel);
var chan = _.find(network.channels, {name: data.channel});
var mode = chan.getMode(from);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
if (data.client === irc.me) { if (data.kicked === irc.user.nick) {
chan.users = []; chan.users = [];
} else { } else {
chan.users = _.without(chan.users, _.find(chan.users, {name: data.client})); chan.users = _.without(chan.users, _.find(chan.users, {name: data.kicked}));
} }
client.emit("users", { client.emit("users", {
chan: chan.id chan: chan.id
}); });
var self = false;
if (data.nick.toLowerCase() === irc.me.toLowerCase()) {
self = true;
}
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.KICK, type: Msg.Type.KICK,
mode: mode, time: data.time,
from: from, mode: chan.getMode(data.nick),
target: data.client, from: data.nick,
target: data.kicked,
text: data.message || "", text: data.message || "",
self: self highlight: data.kicked === irc.user.nick,
self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {

View file

@ -9,7 +9,7 @@ process.setMaxListeners(0);
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("message", function(data) { irc.on("privmsg", function(data) {
var config = Helper.getConfig(); var config = Helper.getConfig();
if (!config.prefetch) { if (!config.prefetch) {
return; return;
@ -27,15 +27,13 @@ module.exports = function(irc, network) {
return; return;
} }
var self = data.to.toLowerCase() === irc.me.toLowerCase(); var chan = network.getChannel(data.target);
var chan = _.find(network.channels, {name: self ? data.from : data.to});
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.TOGGLE, type: Msg.Type.TOGGLE,
time: ""
}); });
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {

View file

@ -1,4 +1,3 @@
var _ = require("lodash");
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
var Helper = require("../../helper"); var Helper = require("../../helper");
@ -7,22 +6,48 @@ module.exports = function(irc, network) {
var client = this; var client = this;
var config = Helper.getConfig(); var config = Helper.getConfig();
irc.on("message", function(data) { irc.on("notice", function(data) {
if (data.message.indexOf("\u0001") === 0 && data.message.substring(0, 7) !== "\u0001ACTION") { // Some servers send notices without any nickname
// Hide ctcp messages. if (!data.nick) {
return; data.from_server = true;
data.nick = network.host;
} }
var target = data.to; data.type = Msg.Type.NOTICE;
if (target.toLowerCase() === irc.me.toLowerCase()) { handleMessage(data);
target = data.from; });
irc.on("action", function(data) {
data.type = Msg.Type.ACTION;
handleMessage(data);
});
irc.on("privmsg", function(data) {
data.type = Msg.Type.MESSAGE;
handleMessage(data);
});
function handleMessage(data) {
// Server messages go to server window, no questions asked
if (data.from_server) {
chan = network.channels[0];
} else {
var target = data.target;
// If the message is targeted at us, use sender as target instead
if (target.toLowerCase() === irc.user.nick.toLowerCase()) {
target = data.nick;
} }
var chan = _.find(network.channels, {name: target}); var chan = network.getChannel(target);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
// Send notices that are not targeted at us into the server window
if (data.type === Msg.Type.NOTICE) {
chan = network.channels[0];
} else {
chan = new Chan({ chan = new Chan({
type: Chan.Type.QUERY, type: Chan.Type.QUERY,
name: data.from name: target
}); });
network.channels.push(chan); network.channels.push(chan);
client.emit("join", { client.emit("join", {
@ -30,21 +55,15 @@ module.exports = function(irc, network) {
chan: chan chan: chan
}); });
} }
}
var type = Msg.Type.MESSAGE;
var text = data.message;
var textSplit = text.split(" ");
if (textSplit[0] === "\u0001ACTION") {
type = Msg.Type.ACTION;
text = text.replace(/^\u0001ACTION|\u0001$/g, "");
} }
var self = (data.from.toLowerCase() === irc.me.toLowerCase()); var self = data.nick === irc.user.nick;
// Self messages are never highlighted // Self messages are never highlighted
// Non-self messages are highlighted as soon as the nick is detected // Non-self messages are highlighted as soon as the nick is detected
var highlight = !self && textSplit.some(function(w) { var highlight = !self && data.message.split(" ").some(function(w) {
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.me.toLowerCase()) === 0); return (w.replace(/^@/, "").toLowerCase().indexOf(irc.user.nick.toLowerCase()) === 0);
}); });
if (chan.id !== client.activeChannel) { if (chan.id !== client.activeChannel) {
@ -55,12 +74,12 @@ module.exports = function(irc, network) {
} }
} }
var name = data.from;
var msg = new Msg({ var msg = new Msg({
type: type, type: data.type,
mode: chan.getMode(name), time: data.time,
from: name, mode: chan.getMode(data.nick),
text: text, from: data.nick,
text: data.message,
self: self, self: self,
highlight: highlight highlight: highlight
}); });
@ -74,5 +93,5 @@ module.exports = function(irc, network) {
chan: chan.id, chan: chan.id,
msg: msg msg: msg
}); });
}); }
}; };

View file

@ -1,34 +1,53 @@
var _ = require("lodash"); var _ = require("lodash");
var Chan = require("../../models/chan");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("mode", function(data) { irc.on("mode", function(data) {
var chan = _.find(network.channels, {name: data.target}); var targetChan;
if (typeof chan !== "undefined") {
setTimeout(function() { if (data.target === irc.user.nick) {
irc.write("NAMES " + data.target); targetChan = network.channels[0];
}, 200); } else {
var from = data.nick; targetChan = network.getChannel(data.target);
if (from.indexOf(".") !== -1) { if (typeof targetChan === "undefined") {
from = data.target; return;
} }
var self = false;
if (from.toLowerCase() === irc.me.toLowerCase()) {
self = true;
} }
var usersUpdated;
for (var i = 0; i < data.modes.length; i++) {
var mode = data.modes[i];
var text = mode.mode;
if (mode.param) {
text += " " + mode.param;
var user = _.find(targetChan.users, {name: mode.param});
if (typeof user !== "undefined") {
usersUpdated = true;
}
}
var msg = new Msg({ var msg = new Msg({
time: data.time,
type: Msg.Type.MODE, type: Msg.Type.MODE,
mode: chan.getMode(from), mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "",
from: from, from: data.nick,
text: data.mode + " " + (data.client || ""), text: text,
self: self self: data.nick === irc.user.nick
}); });
chan.messages.push(msg); targetChan.messages.push(msg);
client.emit("msg", { client.emit("msg", {
chan: chan.id, chan: targetChan.id,
msg: msg, msg: msg,
}); });
} }
if (usersUpdated) {
// TODO: This is horrible
irc.raw("NAMES", data.target);
}
}); });
}; };

View file

@ -4,7 +4,9 @@ module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("motd", function(data) { irc.on("motd", function(data) {
var lobby = network.channels[0]; var lobby = network.channels[0];
data.motd.forEach(function(text) {
if (data.motd) {
data.motd.split("\n").forEach(function(text) {
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.MOTD, type: Msg.Type.MOTD,
text: text text: text
@ -15,5 +17,18 @@ module.exports = function(irc, network) {
msg: msg msg: msg
}); });
}); });
}
if (data.error) {
var msg = new Msg({
type: Msg.Type.MOTD,
text: data.error
});
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
}
}); });
}; };

View file

@ -3,16 +3,23 @@ var User = require("../../models/user");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("names", function(data) { irc.on("userlist", function(data) {
var chan = _.find(network.channels, {name: data.channel}); var chan = network.getChannel(data.channel);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
chan.users = []; chan.users = [];
_.each(data.names, function(u) { _.each(data.users, function(u) {
chan.users.push(new User(u)); var user = new User(u);
// irc-framework sets characater mode, but lounge works with symbols
if (user.mode) {
user.mode = network.prefixLookup[user.mode];
}
chan.users.push(user);
}); });
chan.sortUsers(); chan.sortUsers(irc);
client.emit("users", { client.emit("users", {
chan: chan.id chan: chan.id
}); });

View file

@ -5,11 +5,10 @@ module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("nick", function(data) { irc.on("nick", function(data) {
var self = false; var self = false;
var nick = data["new"]; if (data.nick === irc.user.nick) {
if (nick === irc.me) {
var lobby = network.channels[0]; var lobby = network.channels[0];
var msg = new Msg({ var msg = new Msg({
text: "You're now known as " + nick, text: "You're now known as " + data.new_nick,
}); });
lobby.messages.push(msg); lobby.messages.push(msg);
client.emit("msg", { client.emit("msg", {
@ -20,23 +19,26 @@ module.exports = function(irc, network) {
client.save(); client.save();
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.id,
nick: nick nick: data.new_nick
}); });
} }
network.channels.forEach(function(chan) { network.channels.forEach(function(chan) {
var user = _.find(chan.users, {name: data.nick}); var user = _.find(chan.users, {name: data.nick});
if (typeof user === "undefined") { if (typeof user === "undefined") {
return; return;
} }
user.name = nick; user.name = data.new_nick;
chan.sortUsers(); chan.sortUsers(irc);
client.emit("users", { client.emit("users", {
chan: chan.id chan: chan.id
}); });
var msg = new Msg({ var msg = new Msg({
time: data.time,
type: Msg.Type.NICK, type: Msg.Type.NICK,
from: data.nick, mode: chan.getMode(data.new_nick),
text: nick, nick: data.nick,
new_nick: data.new_nick,
self: self self: self
}); });
chan.messages.push(msg); chan.messages.push(msg);

View file

@ -1,33 +0,0 @@
var _ = require("lodash");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("notice", function(data) {
var target = data.to;
if (target.toLowerCase() === irc.me.toLowerCase()) {
target = data.from;
}
var chan = _.find(network.channels, {name: target});
if (typeof chan === "undefined") {
chan = network.channels[0];
}
var from = data.from || "";
if (data.to === "*" || data.from.indexOf(".") !== -1) {
from = "";
}
var msg = new Msg({
type: Msg.Type.NOTICE,
mode: chan.getMode(from),
from: from,
text: data.message
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
};

View file

@ -4,12 +4,12 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("part", function(data) { irc.on("part", function(data) {
var chan = _.find(network.channels, {name: data.channels[0]}); var chan = network.getChannel(data.channel);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
var from = data.nick; var from = data.nick;
if (from === irc.me) { if (from === irc.user.nick) {
network.channels = _.without(network.channels, chan); network.channels = _.without(network.channels, chan);
client.save(); client.save();
client.emit("part", { client.emit("part", {
@ -23,9 +23,10 @@ module.exports = function(irc, network) {
}); });
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.PART, type: Msg.Type.PART,
mode: chan.getMode(from), time: data.time,
mode: (user && user.mode) || "",
text: data.message || "", text: data.message || "",
hostmask:data.hostmask.username + "@" + data.hostmask.hostname, hostmask: data.ident + "@" + data.hostname,
from: from from: from
}); });
chan.messages.push(msg); chan.messages.push(msg);

View file

@ -15,10 +15,11 @@ module.exports = function(irc, network) {
chan: chan.id chan: chan.id
}); });
var msg = new Msg({ var msg = new Msg({
time: data.time,
type: Msg.Type.QUIT, type: Msg.Type.QUIT,
mode: chan.getMode(from), mode: user.mode || "",
text: data.message || "", text: data.message || "",
hostmask: data.hostmask.username + "@" + data.hostmask.hostname, hostmask: data.ident + "@" + data.hostname,
from: from from: from
}); });
chan.messages.push(msg); chan.messages.push(msg);

View file

@ -1,33 +1,51 @@
var _ = require("lodash");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("topic", function(data) { irc.on("topic", function(data) {
var chan = _.find(network.channels, {name: data.channel}); var chan = network.getChannel(data.channel);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
var from = data.nick || chan.name;
var topic = data.topic;
var msg = new Msg({ var msg = new Msg({
time: data.time,
type: Msg.Type.TOPIC, type: Msg.Type.TOPIC,
mode: chan.getMode(from), mode: (data.nick && chan.getMode(data.nick)) || "",
from: from, from: data.nick,
text: topic, text: data.topic,
isSetByChan: from === chan.name, self: data.nick === irc.user.nick
self: (from.toLowerCase() === irc.me.toLowerCase())
}); });
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {
chan: chan.id, chan: chan.id,
msg: msg msg: msg
}); });
chan.topic = topic;
chan.topic = data.topic;
client.emit("topic", { client.emit("topic", {
chan: chan.id, chan: chan.id,
topic: chan.topic topic: chan.topic
}); });
}); });
irc.on("topicsetby", function(data) {
var chan = network.getChannel(data.channel);
if (typeof chan === "undefined") {
return;
}
var msg = new Msg({
type: Msg.Type.TOPIC_SET_BY,
mode: chan.getMode(data.nick),
nick: data.nick,
when: new Date(data.when * 1000),
self: data.nick === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
}; };

View file

@ -2,13 +2,11 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("welcome", function(data) { irc.on("registered", function(data) {
network.connected = true; network.nick = data.nick;
irc.write("PING " + network.host);
var lobby = network.channels[0]; var lobby = network.channels[0];
var nick = data;
var msg = new Msg({ var msg = new Msg({
text: "You're now known as " + nick text: "You're now known as " + data.nick
}); });
lobby.messages.push(msg); lobby.messages.push(msg);
client.emit("msg", { client.emit("msg", {
@ -18,7 +16,7 @@ module.exports = function(irc, network) {
client.save(); client.save();
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.id,
nick: nick nick: data.nick
}); });
}); });
}; };

View file

@ -1,18 +1,14 @@
var _ = require("lodash");
var Chan = require("../../models/chan"); var Chan = require("../../models/chan");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("whois", function(err, data) { irc.on("whois", function(data) {
if (data === null) { var chan = network.getChannel(data.nick);
return;
}
var chan = _.find(network.channels, {name: data.nickname});
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
chan = new Chan({ chan = new Chan({
type: Chan.Type.QUERY, type: Chan.Type.QUERY,
name: data.nickname name: data.nick
}); });
network.channels.push(chan); network.channels.push(chan);
client.emit("join", { client.emit("join", {
@ -20,10 +16,20 @@ module.exports = function(irc, network) {
chan: chan chan: chan
}); });
} }
var msg = new Msg({
var msg;
if (data.error) {
msg = new Msg({
type: Msg.Type.ERROR,
text: "No such nick: " + data.nick
});
} else {
msg = new Msg({
type: Msg.Type.WHOIS, type: Msg.Type.WHOIS,
whois: data whois: data
}); });
}
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {
chan: chan.id, chan: chan.id,

View file

@ -5,8 +5,9 @@ var expect = require("chai").expect;
var Chan = require("../../src/models/chan"); var Chan = require("../../src/models/chan");
var User = require("../../src/models/user"); var User = require("../../src/models/user");
function makeUser(name, mode) { function makeUser(name) {
return new User({name: name, mode: mode}); // TODO Update/Fix this when User constructor gets reworked (see its TODO)
return new User({nick: name, mode: ""});
} }
function getUserNames(chan) { function getUserNames(chan) {
@ -14,13 +15,20 @@ function getUserNames(chan) {
} }
describe("Chan", function() { describe("Chan", function() {
describe("#sortUsers()", function() { describe("#sortUsers(irc)", function() {
var fullNetworkPrefix = [
{symbol: "~", mode: "q"},
{symbol: "&", mode: "a"},
{symbol: "@", mode: "o"},
{symbol: "%", mode: "h"},
{symbol: "+", mode: "v"}
];
it("should sort a simple user list", function() { it("should sort a simple user list", function() {
var chan = new Chan({users: [ var chan = new Chan({users: [
"JocelynD", "YaManicKill", "astorije", "xPaw", "Max-P" "JocelynD", "YaManicKill", "astorije", "xPaw", "Max-P"
].map(makeUser)}); ].map(makeUser)});
chan.sortUsers(); chan.sortUsers({network: {options: {PREFIX: fullNetworkPrefix}}});
expect(getUserNames(chan)).to.deep.equal([ expect(getUserNames(chan)).to.deep.equal([
"astorije", "JocelynD", "Max-P", "xPaw", "YaManicKill" "astorije", "JocelynD", "Max-P", "xPaw", "YaManicKill"
@ -35,7 +43,7 @@ describe("Chan", function() {
new User({name: "xPaw", mode: "~"}), new User({name: "xPaw", mode: "~"}),
new User({name: "Max-P", mode: "@"}), new User({name: "Max-P", mode: "@"}),
]}); ]});
chan.sortUsers(); chan.sortUsers({network: {options: {PREFIX: fullNetworkPrefix}}});
expect(getUserNames(chan)).to.deep.equal([ expect(getUserNames(chan)).to.deep.equal([
"xPaw", "JocelynD", "Max-P", "astorije", "YaManicKill" "xPaw", "JocelynD", "Max-P", "astorije", "YaManicKill"
@ -50,7 +58,7 @@ describe("Chan", function() {
new User({name: "xPaw"}), new User({name: "xPaw"}),
new User({name: "Max-P", mode: "@"}), new User({name: "Max-P", mode: "@"}),
]}); ]});
chan.sortUsers(); chan.sortUsers({network: {options: {PREFIX: fullNetworkPrefix}}});
expect(getUserNames(chan)).to.deep.equal( expect(getUserNames(chan)).to.deep.equal(
["Max-P", "YaManicKill", "astorije", "JocelynD", "xPaw"] ["Max-P", "YaManicKill", "astorije", "JocelynD", "xPaw"]
@ -59,7 +67,7 @@ describe("Chan", function() {
it("should be case-insensitive", function() { it("should be case-insensitive", function() {
var chan = new Chan({users: ["aB", "Ad", "AA", "ac"].map(makeUser)}); var chan = new Chan({users: ["aB", "Ad", "AA", "ac"].map(makeUser)});
chan.sortUsers(); chan.sortUsers({network: {options: {PREFIX: fullNetworkPrefix}}});
expect(getUserNames(chan)).to.deep.equal(["AA", "aB", "ac", "Ad"]); expect(getUserNames(chan)).to.deep.equal(["AA", "aB", "ac", "Ad"]);
}); });
@ -69,7 +77,7 @@ describe("Chan", function() {
"[foo", "]foo", "(foo)", "{foo}", "<foo>", "_foo", "@foo", "^foo", "[foo", "]foo", "(foo)", "{foo}", "<foo>", "_foo", "@foo", "^foo",
"&foo", "!foo", "+foo", "Foo" "&foo", "!foo", "+foo", "Foo"
].map(makeUser)}); ].map(makeUser)});
chan.sortUsers(); chan.sortUsers({network: {options: {PREFIX: fullNetworkPrefix}}});
expect(getUserNames(chan)).to.deep.equal([ expect(getUserNames(chan)).to.deep.equal([
"!foo", "&foo", "(foo)", "+foo", "<foo>", "@foo", "[foo", "]foo", "!foo", "&foo", "(foo)", "+foo", "<foo>", "@foo", "[foo", "]foo",

View file

@ -22,7 +22,7 @@ describe("Network", function() {
username: "", username: "",
realname: "", realname: "",
commands: [], commands: [],
nick: undefined, nick: "",
join: "#thelounge,&foobar", join: "#thelounge,&foobar",
}); });
}); });

View file

@ -2,9 +2,10 @@ var EventEmitter = require("events").EventEmitter;
var util = require("util"); var util = require("util");
var _ = require("lodash"); var _ = require("lodash");
var express = require("express"); var express = require("express");
var Network = require("../src/models/network");
function MockClient(opts) { function MockClient(opts) {
this.me = "test-user"; this.user = {nick: "test-user"};
for (var k in opts) { for (var k in opts) {
this[k] = opts[k]; this[k] = opts[k];
@ -13,14 +14,13 @@ function MockClient(opts) {
util.inherits(MockClient, EventEmitter); util.inherits(MockClient, EventEmitter);
MockClient.prototype.createMessage = function(opts) { MockClient.prototype.createMessage = function(opts) {
var message = _.extend({ var message = _.extend({
message: "dummy message", message: "dummy message",
from: "test-user", nick: "test-user",
to: "test-channel" target: "#test-channel"
}, opts); }, opts);
this.emit("message", message); this.emit("privmsg", message);
}; };
module.exports = { module.exports = {
@ -28,12 +28,13 @@ module.exports = {
return new MockClient(); return new MockClient();
}, },
createNetwork: function() { createNetwork: function() {
return { return new Network({
host: "example.com",
channels: [{ channels: [{
name: "test-channel", name: "#test-channel",
messages: [] messages: []
}] }]
}; });
}, },
createWebserver: function() { createWebserver: function() {
return express(); return express();