hardlounge/lib/shout.js

146 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-06-26 23:05:47 +00:00
var _ = require("lodash");
2014-06-29 01:07:38 +00:00
var Client = require("./models/client");
2014-06-26 23:05:47 +00:00
var config = require("../config") || {};
var http = require("connect");
2014-06-29 19:41:02 +00:00
var net = require("net");
var Network = require("./models/network");
2014-06-26 23:05:47 +00:00
var io = require("socket.io");
2014-06-29 19:41:02 +00:00
var slate = require("slate-irc");
var tls = require("tls");
2014-06-26 16:14:45 +00:00
2014-06-26 23:05:47 +00:00
var sockets = null;
var clients = [];
2014-06-26 16:14:45 +00:00
2014-06-26 23:05:47 +00:00
var inputs = [
"action",
2014-07-02 22:00:11 +00:00
"connect",
2014-06-26 23:05:47 +00:00
"invite",
"join",
"kick",
"mode",
"msg",
"nick",
"notice",
"part",
"quit",
"raw",
"topic",
"whois"
];
2014-06-26 16:14:45 +00:00
2014-06-26 23:05:47 +00:00
var events = [
"errors",
"join",
"kick",
"mode",
"motd",
"message",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
module.exports = function() {
sockets = io(http().use(http.static("client")).listen(config.port || 9000));
sockets.on("connection", function(socket) {
2014-06-29 01:07:38 +00:00
if (config.public) {
2014-06-30 01:20:54 +00:00
auth.call(socket);
2014-06-29 01:07:38 +00:00
} else {
init(socket);
}
2014-06-26 23:05:47 +00:00
});
};
2014-06-29 01:07:38 +00:00
function init(socket, client) {
2014-06-26 23:05:47 +00:00
if (!client) {
2014-06-27 00:47:36 +00:00
socket.emit("auth");
socket.on("auth", auth);
2014-06-26 23:05:47 +00:00
} else {
2014-06-29 01:07:38 +00:00
socket.on("input", function(data) { input(client, data); });
socket.join(client.id);
socket.emit("init", {
2014-06-30 01:20:54 +00:00
networks: client.networks
2014-06-29 01:07:38 +00:00
});
2014-06-26 23:05:47 +00:00
}
2014-06-29 19:41:02 +00:00
}
2014-06-26 23:05:47 +00:00
2014-06-30 01:20:54 +00:00
function auth(data) {
2014-06-27 00:47:36 +00:00
var socket = this;
2014-06-30 01:20:54 +00:00
if (config.public) {
// Temporary:
var client = clients[0];
if (clients.length === 0) {
2014-07-02 22:00:11 +00:00
client = new Client({sockets: sockets});
2014-06-30 01:20:54 +00:00
clients.push(client);
connect(client, {
host: "irc.freenode.org"
});
}
init(socket, client);
} else {
if (false) {
// ..
}
}
2014-06-29 19:41:02 +00:00
}
function connect(client, args) {
var options = {
host: args.host,
port: args.port || 6667
};
2014-06-30 01:20:54 +00:00
2014-06-29 19:41:02 +00:00
var stream = args.tls ? tls.connect(options) : net.connect(options);
stream.on("error", function(e) {
console.log(e);
});
2014-06-30 01:20:54 +00:00
2014-06-29 19:41:02 +00:00
var irc = slate(stream);
irc.nick("shout");
irc.user("shout", "Shout User");
2014-06-30 01:20:54 +00:00
2014-06-29 19:41:02 +00:00
client.nick = "shout";
2014-06-30 01:20:54 +00:00
2014-06-29 19:41:02 +00:00
var network = new Network({
host: options.host,
irc: irc
});
2014-06-30 01:20:54 +00:00
2014-06-29 19:41:02 +00:00
client.networks.push(network);
client.emit("network", {
network: network
});
2014-06-30 01:20:54 +00:00
2014-06-29 19:41:02 +00:00
events.forEach(function(plugin) {
require("./plugins/irc-events/" + plugin).apply(client, [irc, network]);
});
2014-06-30 01:20:54 +00:00
irc.on("welcome", function() {
irc.join("#shout-test");
});
2014-06-29 19:41:02 +00:00
}
function input(client, data) {
2014-07-02 22:00:11 +00:00
var target = client.find(data.target);
if (!target) {
return;
}
var text = data.text;
if (text.charAt(0) !== "/") {
text = "/say " + text;
}
var args = text.split(" ");
var cmd = args.shift().replace("/", "").toLowerCase();
inputs.forEach(function(plugin) {
require("./plugins/inputs/" + plugin).apply(client, [target.network, target.chan, cmd, args]);
});
2014-06-29 19:41:02 +00:00
}