Added support for multiple users

This commit is contained in:
Mattias Erming 2014-06-23 19:28:36 +02:00
parent ffabc7cfea
commit a690395086
24 changed files with 285 additions and 265 deletions

View File

@ -81,8 +81,6 @@ socket.emit("join", {
id: 0,
name: "",
type: "",
network: "",
count: 0,
messages: [],
users: [],
}
@ -113,7 +111,7 @@ socket.emit("networks", {
networks: [{
id: 0,
host: "",
name: "",
name: "",
channels: [],
}]
});

View File

@ -57,7 +57,7 @@ button {
font: bold 12px Lato, sans-serif;
letter-spacing: 1px;
margin-bottom: 10px;
padding: 12px 18px;
padding: 10px 18px;
text-transform: uppercase;
transition: background .2s, border-color .2s, color .2s;
word-spacing: 3px;
@ -138,12 +138,7 @@ button {
}
#sidebar .octicon {
float: left;
margin-top: 1px;
margin-right: 8px;
}
#sidebar .octicon-plus {
margin-left: 1px;
margin-right: 11px;
margin: 1px 8px 0 1px;
}
#sidebar .lobby {
color: #84d1ff !important;
@ -221,6 +216,9 @@ button {
#sidebar.signed-in #settings-link {
display: block;
}
#sidebar .hidden {
display: none !important;
}
#main {
bottom: 0;
left: 220px;
@ -244,6 +242,7 @@ button {
margin: 0 auto;
max-width: 480px;
overflow: auto;
-webkit-overflow-scrolling: touch;
padding: 0 20px;
}
#windows a {
@ -258,7 +257,8 @@ button {
#windows h2 {
color: #2c3e50;
font: 300 48px Lato, sans-serif;
line-height: 1.5;
line-height: 1.4;
margin-bottom: 16px;
}
#windows h2,
#windows h3 {
@ -268,7 +268,6 @@ button {
#windows h3 {
border-bottom: 1px solid #eee;
font-weight: 300;
margin-bottom: 10px;
padding-bottom: 7px;
}
@ -276,20 +275,24 @@ button {
color: #95a5a6;
font-size: 18px;
font-weight: normal;
margin-top: 0;
margin-bottom: 10px;
}
#windows form {
margin: 0 0;
max-width: 250px;
}
#windows .btn {
margin-top: 8px;
}
#windows .input {
border: 2px solid #e9ecef;
border-radius: 4px;
color: #95a5a6;
font-size: 24px;
font-size: 18px;
margin-bottom: 16px;
outline: 0;
padding: 10px 14px;
padding: 8px 10px;
transition: border-color .2s;
-webkit-appearance: none;
width: 100%;
@ -418,6 +421,7 @@ button {
}
#chat .messages {
display: table;
margin: 5px 0;
width: 100%;
}
#chat .row {
@ -429,12 +433,6 @@ button {
padding-top: 2px;
padding-bottom: 2px;
}
#chat .row:first-child span {
padding-top: 5px;
}
#chat .row:last-child span {
padding-bottom: 5px;
}
#chat .row:hover .time {
color: #aaa;
}

View File

@ -28,16 +28,14 @@
<i class="octicon octicon-clippy"></i>
Settings
</a>
<% if (password) { %>
<a id="login" href="#sign-in" data-name="Sign in">
<i class="octicon octicon-sign-in"></i>
Sign in
</a>
<a id="logout" href="">
<a id="logout" href="" class="hidden">
<i class="octicon octicon-sign-out"></i>
Sign out
</a>
<% } %>
</section>
<div id="networks"></div>
<footer class="footer">
@ -95,11 +93,12 @@
</div>
<div id="sign-in" class="window">
<div class="wrap">
<h1>Shout</h1>
<h2>You need to sign in to continue.</h2>
<h1>Sign in</h1>
<form id="sign-in-form" method="post">
<h4>Username:</h4>
<input name="user" class="name input" autocomplete="off">
<h4>Password:</h4>
<input id="sign-in-input" class="input" type="password" autofocus>
<input name="password" class="password input" type="password">
<button type="submit" class="btn">
Sign in
</button>

View File

@ -260,7 +260,11 @@ $(function() {
.end();
if (!touchDevice) {
window.find("input").focus();
if (window.is("#sign-in")) {
window.find("input[name='user']").focus();
} else {
window.find("input:last").focus();
}
}
});
@ -411,8 +415,16 @@ $(function() {
$("#sign-in-form").on("submit", function(e) {
e.preventDefault();
socket.emit("auth", $("#sign-in-input").val());
});
var user = $(this).find(".name").val();
var password = $(this).find(".password").val();
$.cookie("user", user);
$("#logout").removeClass("hidden");
socket.emit("auth", {
user: user,
password: password
});
}).find(".name")
.val($.cookie("user") || "");
$("#notification").on("click", function() {
pop.play();

View File

@ -1,21 +1,22 @@
module.exports = {
port: 9000,
password: "",
log: false,
theme: "",
defaults: {
nick: "shout-user",
realname: "http://github.com/erming/shout",
},
networks: [{
host: "chat.freenode.net",
port: 6697,
tls: true,
onConnect: {
commands: [""],
join: [
"#shout-irc",
]
users: {
"username": {
password: "",
nick: "shout",
realname: "http://github.com/erming/shout",
log: false,
networks: [{
host: "irc.freenode.org",
port: 6667,
tls: false,
onConnect: {
join: [
"#shout-irc"
]
}
}]
}
}]
}
};

View File

@ -3,14 +3,22 @@ var fs = require("fs");
var moment = require("moment");
module.exports = function log(chan, msg) {
if (!config.log) {
var client = chan.network.client;
if (!client.config.log) {
return;
}
var network = chan.network.name;
var dir = "logs/" + network + "/";
var dir = "logs/";
dir += client.name + "/";
if (!fs.existsSync(dir)) {
fs.mkdir(dir);
fs.mkdirSync(dir);
}
dir += network + "/";
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
var date = moment().format("YYYY-MM-DD HH:mm");

View File

@ -9,8 +9,7 @@ function Chan(attr) {
id: global.id = ++global.id || 1,
name: "",
type: "channel",
network: "",
count: 0,
network: null,
messages: [],
users: [],
}, attr));
@ -47,8 +46,9 @@ Chan.prototype.sortUsers = function() {
};
Chan.prototype.toJSON = function() {
var clone = _.omit(this, ["network"]);
clone.count = clone.messages.length;
var clone = _.omit(this, [
"network"
]);
clone.messages = clone.messages.slice(-100);
return clone;
};

17
lib/models/client.js Normal file
View File

@ -0,0 +1,17 @@
var _ = require("lodash");
module.exports = Client;
function Client(attr) {
_.merge(this, _.extend({
name: "",
networks: [],
sockets: null,
}, attr));
};
Client.prototype.emit = function(event, data) {
if (this.sockets != null) {
this.sockets.in(this.name).emit(event, data);
}
};

View File

@ -6,8 +6,8 @@ module.exports = Network;
function Network(attr) {
_.merge(this, _.extend({
id: global.id = ++global.id || 1,
client: null,
connected: false,
slate: null,
host: "",
name: attr.host.split(".")[1].capitalize() || attr.host,
channels: [],
@ -28,10 +28,13 @@ Network.prototype.toJSON = function() {
var clone = _.omit(this, [
"client",
"connected",
"slate"
]);
return clone;
};
// Helper
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}

View File

@ -1,10 +1,9 @@
var config = require("../../config") || {};
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("errors", function(data) {
sockets.in("chat").emit("msg", {
module.exports = function(slate, network) {
var client = this;
slate.on("errors", function(data) {
client.emit("msg", {
msg: new Msg({
type: "error",
from: "-!-",
@ -13,8 +12,8 @@ module.exports = function(client, sockets) {
});
if (!network.connected) {
if (data.cmd == "ERR_NICKNAMEINUSE") {
var random = config.defaults.nick + Math.floor(10 + (Math.random() * 89));
client.nick(random);
var random = client.nick + Math.floor(10 + (Math.random() * 89));
slate.nick(random);
}
}
});

View File

@ -3,16 +3,16 @@ var Chan = require("../models/chan");
var Msg = require("../models/msg");
var User = require("../models/user");
module.exports = function(client, sockets) {
var network = this;
client.on("join", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
module.exports = function(slate, network) {
var client = this;
slate.on("join", function(data) {
var chan = _.find(network.channels, {name: data.channel});
if (!chan) {
chan = new Chan({
name: data.channel,
name: data.channel
});
network.addChan(chan);
sockets.in("chat").emit("join", {
client.emit("join", {
id: network.id,
chan: chan,
});
@ -20,7 +20,7 @@ module.exports = function(client, sockets) {
var users = chan.users;
users.push(new User({name: data.nick}));
chan.sortUsers();
sockets.in("chat").emit("users", {
client.emit("users", {
id: chan.id,
users: users,
});
@ -29,7 +29,7 @@ module.exports = function(client, sockets) {
type: "join",
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,19 +2,19 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("kick", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("kick", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
}
if (data.client == client.me) {
if (data.client == slate.me) {
chan.users = [];
} else {
chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.client}));
}
sockets.in("chat").emit("users", {
client.emit("users", {
id: chan.id,
users: chan.users,
});
@ -24,7 +24,7 @@ module.exports = function(client, sockets) {
text: data.client,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -3,9 +3,9 @@ var Chan = require("../models/chan");
var Msg = require("../models/msg");
var Network = require("../models/network");
module.exports = function(client, sockets) {
var network = this;
client.on("message", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("message", function(data) {
var target = data.to;
var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from});
if (typeof chan === "undefined") {
@ -14,7 +14,7 @@ module.exports = function(client, sockets) {
type: "query",
});
network.addChan(chan);
sockets.in("chat").emit("join", {
client.emit("join", {
id: network.id,
chan: chan,
});
@ -26,7 +26,7 @@ module.exports = function(client, sockets) {
text = text.replace(/\u0001|ACTION/g, "");
}
text.split(' ').forEach(function(w) {
if (w.indexOf(client.me) == 0) type += " highlight";
if (w.indexOf(slate.me) == 0) type += " highlight";
});
var msg = new Msg({
type: type || "normal",
@ -34,7 +34,7 @@ module.exports = function(client, sockets) {
text: text,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,15 +2,15 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
module.exports = function(slate, network) {
var client = this;
var timer = null;
client.on("mode", function(data) {
slate.on("mode", function(data) {
var chan = _.findWhere(network.channels, {name: data.target});
if (typeof chan !== "undefined") {
clearTimeout(timer);
timer = setTimeout(function() {
client.write("NAMES " + data.target);
slate.write("NAMES " + data.target);
}, 200);
var nick = data.nick;
if (nick.indexOf(".") !== -1) {
@ -22,7 +22,7 @@ module.exports = function(client, sockets) {
text: data.mode + " " + data.client,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -1,9 +1,9 @@
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("motd", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("motd", function(data) {
var rows = data.motd;
var chan = network.channels[0];
var msg = new Msg({
@ -11,7 +11,7 @@ module.exports = function(client, sockets) {
from: "-!-"
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});
@ -22,7 +22,7 @@ module.exports = function(client, sockets) {
text: text,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,9 +2,9 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var User = require("../models/user");
module.exports = function(client, sockets) {
var network = this;
client.on("names", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("names", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
@ -14,7 +14,7 @@ module.exports = function(client, sockets) {
chan.addUser(new User(n));
});
chan.sortUsers();
sockets.in("chat").emit("users", {
client.emit("users", {
id: chan.id,
users: chan.users,
});

View File

@ -2,17 +2,17 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("nick", function(data) {
if (data["new"] == client.me) {
module.exports = function(slate, network) {
var client = this;
slate.on("nick", function(data) {
if (data["new"] == slate.me) {
var chan = network.channels[0];
var msg = new Msg({
from: "-!-",
text: "You're now known as " + data["new"],
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});
@ -24,7 +24,7 @@ module.exports = function(client, sockets) {
}
user.name = data["new"];
chan.sortUsers();
sockets.in("chat").emit("users", {
client.emit("users", {
id: chan.id,
users: chan.users,
});
@ -34,7 +34,7 @@ module.exports = function(client, sockets) {
text: data["new"],
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -1,9 +1,9 @@
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("notice", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("notice", function(data) {
var chan = network.channels[0];
var from = data.from || "-!-";
if (data.to == "*" || data.from.indexOf(".") !== -1) {
@ -15,7 +15,7 @@ module.exports = function(client, sockets) {
text: data.message,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,21 +2,21 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("part", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("part", function(data) {
var chan = _.findWhere(network.channels, {name: data.channels[0]});
if (typeof chan === "undefined") {
return;
}
if (data.nick == client.me) {
if (data.nick == slate.me) {
network.channels = _.without(network.channels, chan);
sockets.in("chat").emit("part", {
client.emit("part", {
id: chan.id,
});
} else {
chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.nick}));
sockets.in("chat").emit("users", {
client.emit("users", {
id: chan.id,
users: chan.users,
});
@ -25,7 +25,7 @@ module.exports = function(client, sockets) {
from: data.nick,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,16 +2,16 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("quit", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("quit", function(data) {
network.channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
if (!user) {
return;
}
chan.users = _.without(chan.users, user);
sockets.in("chat").emit("users", {
client.emit("users", {
id: chan.id,
users: chan.users,
});
@ -20,7 +20,7 @@ module.exports = function(client, sockets) {
from: data.nick,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,9 +2,9 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("topic", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("topic", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
@ -16,7 +16,7 @@ module.exports = function(client, sockets) {
text: data.topic,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -1,17 +1,18 @@
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("welcome", function(data) {
module.exports = function(slate, network) {
var client = this;
slate.on("welcome", function(data) {
network.connected = true;
slate.write("PING " + network.host);
var chan = network.channels[0];
var msg = new Msg({
from: "-!-",
text: "You're now known as " + data,
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -2,9 +2,9 @@ var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
module.exports = function(client, sockets) {
var network = this;
client.on("whois", function(err, data) {
module.exports = function(slate, network) {
var client = this;
slate.on("whois", function(err, data) {
if (!data) {
return;
}
@ -15,7 +15,7 @@ module.exports = function(client, sockets) {
name: data.nickname,
});
network.addChan(chan);
sockets.in("chat").emit("join", {
client.emit("join", {
id: network.id,
chan: chan,
});
@ -38,7 +38,7 @@ module.exports = function(client, sockets) {
text: key + " " + data[k],
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});
@ -49,7 +49,7 @@ module.exports = function(client, sockets) {
text: "End of /WHOIS list.",
});
chan.addMsg(msg);
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});

View File

@ -7,11 +7,12 @@ var irc = require("slate-irc");
var net = require("net");
var tls = require("tls");
var Client = require("./models/client");
var Msg = require("./models/msg");
var Network = require("./models/network");
var sockets = null;
var networks = [];
var clients = [];
var plugins = [
"errors",
@ -37,32 +38,28 @@ module.exports = function listen() {
.use(http.static("client"))
.listen(port);
sockets = io.listen(app, {log: 0}).sockets.on("connection", function(socket) {
init.call(
socket,
!!config.password
);
});
sockets = io.listen(app, {log: 0});
(config.networks || []).forEach(function(n) {
connect(n);
});
}
function init(login) {
if (login) {
this.on("auth", auth);
this.emit("auth");
} else {
this.join("chat");
this.on("debug", debug);
this.on("input", input);
this.on("fetch", fetch);
this.emit(
"networks",
{networks: networks}
);
var users = config.users;
for (var user in users) {
var client = new Client({
name: user,
config: users[user],
sockets: sockets
});
clients.push(client);
users[user].networks.forEach(function(network) {
connect(client, network);
});
}
sockets.on("connection", function(socket) {
if (clients.length == 1 && !clients[0].config.password) {
init.call(socket, clients[0]);
} else {
init.call(socket);
}
});
}
function index(req, res, next) {
@ -79,84 +76,75 @@ function index(req, res, next) {
});
}
function connect(params) {
_.defaults(
params,
config.defaults
);
function init(client) {
var socket = this;
if (!client) {
socket.on("auth", auth);
socket.emit("auth");
} else {
socket.on("input", function(data) { input(client, data); });
socket.on("fetch", function(data) { fetch(client, socket, data); });
socket.join(client.name);
socket.emit("networks", {
networks: client.networks
});
}
}
function auth(data) {
var user = config.users[data.user];
if (user && data.password == user.password) {
var socket = this;
clients.forEach(function(c) {
if (c.name == data.user) init.call(socket, c);
});
}
}
function connect(client, params) {
var host = params.host;
var port = params.port || 6667;
var options = {
host: host,
port: port,
port: port
};
var stream = params.tls ? tls.connect(options) : net.connect(options);
var stream = params.tls ? tls.connect(options) : net.connect(options);
stream.on("error", function(e) {
console.log(e);
});
var client = irc(stream);
var slate = irc(stream);
slate.nick(client.config.nick);
slate.user(client.config.nick, client.config.realname);
var network = new Network({
host: host,
client: client,
host: host,
slate: slate,
});
networks.push(network);
sockets.in("chat").emit("networks", {networks: networks});
client.nick(params.nick);
client.user(params.nick, params.realname);
client.networks.push(network);
client.emit("networks", {
networks: client.networks
});
plugins.forEach(function(plugin) {
require("./plugins/" + plugin).apply(network, [client, sockets]);
require("./plugins/" + plugin).apply(client, [slate, network]);
});
if (!params.onConnect) {
return;
}
client.once("welcome", function() {
client.write("PING " + network.host);
var channels = params.onConnect.join || [];
channels.forEach(function(chan) {
client.join.apply(
client,
chan.split(' ')
slate.on("welcome", function() {
((params.onConnect || {}).join || []).forEach(function(chan) {
slate.join.apply(
slate,
chan.split(" ")
);
});
});
client.once("pong", function() {
var delay = 1000;
var commands = params.onConnect.commands || [];
commands.forEach(function(cmd) {
setTimeout(function() {
input({
id: network.channels[0].id,
text: cmd
});
}, delay);
delay += 1000;
});
});
}
function auth(password) {
if (password == config.password) {
this.disable = false;
init.call(this);
}
}
function debug(data) {
console.log(data);
}
function input(data) {
var target = find(data.id);
function input(client, data) {
var target = find(client.networks, data.id);
if (!target) {
return;
}
@ -164,8 +152,8 @@ function input(data) {
var network = target.network;
var chan = target.chan;
var client = network.client;
var slate = network.slate;
var id = data.id;
var text = data.text;
if (!text) {
@ -187,9 +175,9 @@ function input(data) {
case "msg":
var user;
var text = args.slice(2).join(" ");
if (client) {
user = client.me;
client.send(args[1], text);
if (slate) {
user = slate.me;
slate.send(args[1], text);
}
var chan = _.findWhere(network.channels, {name: args[1]});
if (typeof chan !== "undefined") {
@ -198,7 +186,7 @@ function input(data) {
text: text,
});
chan.addMsg(msg)
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});
@ -206,8 +194,8 @@ function input(data) {
break;
case "notice":
if (client && args[2]) {
client.notice(args[1], args.slice(2).join(" "));
if (slate && args[2]) {
slate.notice(args[1], args.slice(2).join(" "));
}
break;
@ -219,9 +207,9 @@ function input(data) {
}
var user;
var text = slap || args.slice(1).join(" ");
if (client) {
user = client.me;
client.action(chan.name, text);
if (slate) {
user = slate.me;
slate.action(chan.name, text);
}
var msg = new Msg({
type: "action",
@ -229,7 +217,7 @@ function input(data) {
text: text,
});
chan.addMsg(msg)
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});
@ -238,7 +226,7 @@ function input(data) {
case "ame":
var type = "action";
case "amsg":
var user = client.me;
var user = slate.me;
var text = args.slice(1).join(" ");
var channels = [];
network.channels.forEach(function(chan) {
@ -250,7 +238,7 @@ function input(data) {
text: text,
});
chan.addMsg(msg)
sockets.in("chat").emit("msg", {
client.emit("msg", {
id: chan.id,
msg: msg,
});
@ -265,16 +253,16 @@ function input(data) {
case "server":
case "connect":
if (args[1]) {
connect({host: args[1]});
connect(client, {host: args[1]});
}
break;
case "join":
if (client && args[1]) {
if (slate && args[1]) {
if (!args[2] || args[2].charAt(0) == "#") {
client.join(args.slice(1));
slate.join(args.slice(1));
} else {
client.join(
slate.join(
args[1],
args[2] // Password
);
@ -283,8 +271,8 @@ function input(data) {
break;
case "nick":
if (client && args[1]) {
client.nick(args[1]);
if (slate && args[1]) {
slate.nick(args[1]);
}
break;
@ -299,12 +287,14 @@ function input(data) {
}
var id = chan.id;
if (chan.type == "query" || !chan.users.length) {
remove(id);
sockets.in("chat").emit("part", {
client.networks.forEach(function(n) {
n.channels = _.without(n.channels, _.findWhere(n.channels, {id: id}));
});
client.emit("part", {
id: id,
});
} else if (client) {
client.part(chan.name);
} else if (slate) {
slate.part(chan.name);
}
break;
@ -313,39 +303,37 @@ function input(data) {
network.channels.forEach(function(c) {
if (c.type == "channel") part.push(c.name);
});
console.log("PART");
console.log(part);
client.part(part);
slate.part(part);
break;
case "invite":
if (client && args[2]) {
client.invite(args[1], args[2]);
if (slate && args[2]) {
slate.invite(args[1], args[2]);
}
break;
case "topic":
if (client) {
if (slate) {
var msg = "TOPIC";
msg += " " + chan.name;
msg += args[1] ? " :" + args.slice(1).join(" ") : "";
client.write(msg);
slate.write(msg);
}
break;
case "whoami":
var user = client.me;
var user = slate.me;
case "query":
case "whois":
var user = user || args[1];
if (client && user) {
client.whois(user);
if (slate && user) {
slate.whois(user);
}
break;
case "kick":
if (client && args[1]) {
client.kick(chan.name, args[1]);
if (slate && args[1]) {
slate.kick(chan.name, args[1]);
}
break;
@ -354,7 +342,7 @@ function input(data) {
case "voice":
case "devoice":
case "mode":
if (!client || !args[1]) {
if (!slate || !args[1]) {
break;
}
var mode;
@ -373,7 +361,7 @@ function input(data) {
mode = args[1];
user = args[2];
}
client.mode(
slate.mode(
chan.name,
mode,
user
@ -383,38 +371,39 @@ function input(data) {
case "quit":
case "disconnect":
if (client) {
networks = _.without(networks, network);
sockets.in("chat").emit("networks", {networks: networks});
client.quit();
client.networks = _.without(client.networks, network);
client.emit("networks", {networks: client.networks});
slate.quit();
}
break;
case "raw":
case "send":
if (client) {
client.write(args.slice(1).join(" "));
if (slate) {
slate.write(args.slice(1).join(" "));
}
break;
}
}
function fetch(data) {
var socket = this;
var target = find(data.id);
function fetch(client, socket, data) {
var target = find(client.networks, data.id);
if (!target) {
return;
}
var chan = target.chan;
var messages = chan
.messages
.slice(0, chan.messages.length - (data.count || 0));
socket.emit("messages", {
id: data.id,
msg: messages,
});
}
function find(id) {
function find(networks, id) {
for (var i = 0; i < networks.length; i++) {
var result = {
network: networks[i],
@ -424,10 +413,5 @@ function find(id) {
return result;
}
}
}
function remove(id) {
networks.forEach(function(n) {
n.channels = _.without(n.channels, _.findWhere(n.channels, {id: id}));
});
return false;
}