|
|
@@ -2,41 +2,173 @@
|
|
|
const parseIrcCmd = require('./ircCmd.js').parse
|
|
|
,accountManager = require("../models/accounts.js").accountManager
|
|
|
|
|
|
+ ,MultiChatManager = require("../multichatManager.js").MultiChatManager
|
|
|
+ ,slackManager = require("../slackManager.js").SlackManager
|
|
|
+
|
|
|
+ ,CONFIG = require("../../config.js");
|
|
|
+
|
|
|
+const SERVER_CONFIG = {
|
|
|
+ port: CONFIG.port,
|
|
|
+ hostname: CONFIG.hostname,
|
|
|
+ created: (new Date()).toString()
|
|
|
+};
|
|
|
+
|
|
|
IrcConnection = module.exports.IrcConnection = function(sock) {
|
|
|
var _this = this;
|
|
|
|
|
|
this.account = null;
|
|
|
- this.username = "mimou";
|
|
|
+ this.nickname;
|
|
|
+ this.username;
|
|
|
+ this.hostname;
|
|
|
+ this.servername;
|
|
|
+ this.realname;
|
|
|
+
|
|
|
+ this.polling = false;
|
|
|
+ this.joinedChannels = {};
|
|
|
|
|
|
this.sock = sock;
|
|
|
this.sock.on("data", (data) => {
|
|
|
data.toString("utf-8").split(/\r?\n/g).forEach((str) => { _this.parse(str); });
|
|
|
});
|
|
|
+ this.sock.once("close", () => {
|
|
|
+ console.log("[IRC] closed connection" +(this.account ? (" for user #" +this.account.id) : ""));
|
|
|
+ this.polling = false;
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
IrcConnection.prototype.write = function(line) {
|
|
|
+ console.log("[IRC] >>> " +line);
|
|
|
this.sock.write(line +"\r\n");
|
|
|
};
|
|
|
|
|
|
+IrcConnection.prototype.isAuthed = function() {
|
|
|
+ return this.account && this.nickname && this.username;
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.checkConnectionDone = function(account, nickname, username, hostname, servername, realname) {
|
|
|
+ var authed = this.isAuthed();
|
|
|
+
|
|
|
+ if (account)
|
|
|
+ this.account = account;
|
|
|
+ if (nickname)
|
|
|
+ this.nickname = nickname;
|
|
|
+ if (username)
|
|
|
+ this.username = username;
|
|
|
+ if (hostname)
|
|
|
+ this.hostname = hostname;
|
|
|
+ if (servername)
|
|
|
+ this.servername = servername;
|
|
|
+ if (realname)
|
|
|
+ this.realname = realname;
|
|
|
+
|
|
|
+ if (!authed && this.isAuthed()) {
|
|
|
+ console.log("Authed new IRC client for user #" +this.account.id);
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 001 " +this.nickname +" :Welcome");
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 002 " +this.nickname +" :Your host is " +SERVER_CONFIG.hostname +"[" +this.sock.localAddress +"/" +SERVER_CONFIG.port +"], running version mimouchat");
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 003 " +this.nickname +" :This server was created " +SERVER_CONFIG.created);
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 004 " +this.nickname +" " +SERVER_CONFIG.hostname +" mimouchat-irc DOQRSZaghilopswz CFILMPQSbcefgijklmnopqrstvz bkloveqjfI");
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 375 " +this.nickname +" :- Mimouchat Message of the Day -");
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 372 " +this.nickname +" :- Welcome to mimouchat -");
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" 376 " +this.nickname +" :End of /MOTD command.");
|
|
|
+ this.write(":" +this.nickname +" MODE " +this.nickname +" :+i");
|
|
|
+
|
|
|
+ this.chatContext = new MultiChatManager();
|
|
|
+ var services = this.account.getServices(),
|
|
|
+ now = Date.now();
|
|
|
+ for (var serviceId in services) {
|
|
|
+ switch (services[serviceId].type) {
|
|
|
+ case "Slack":
|
|
|
+ this.chatContext.push(slackManager.lazyGet(serviceId, services[serviceId].oauthParam, now));
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ console.error("Unknown service type for ", services[serviceId], " with account #" +req.account.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.polling = true;
|
|
|
+ this.poll();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.sendJoin = function(channel) {
|
|
|
+ var res = this.joinedChannels[channel.id] = { topic: "", purpose: "" };
|
|
|
+ this.write(":" +this.nickname +" JOIN #" +channel.id);
|
|
|
+ return res;
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.sendTopic = function(channel) {
|
|
|
+ this.joinedChannels[channel.id].topic = channel.topic.value;
|
|
|
+ this.write(":" +(channel.topic.creator ? this.chatContext.getUser(channel.topic.creator).getName() : SERVER_CONFIG.hostname) +" TOPIC #" +channel.id +" :" +channel.topic.value);
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.sendPurpose = function(channel) {
|
|
|
+ this.joinedChannels[channel.id].purpose = channel.purpose.value;
|
|
|
+ //this.write(":" +this.nickname +" JOIN #" +channel.id);
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.sendStaticUpdate = function(static) {
|
|
|
+ for (var team in static) {
|
|
|
+ if (static[team].channels)
|
|
|
+ static[team].channels.forEach((channel) => {
|
|
|
+ var jChannel = this.joinedChannels[channel.id];
|
|
|
+
|
|
|
+ if (!jChannel)
|
|
|
+ jChannel = this.sendJoin(channel);
|
|
|
+ if (channel.purpose && jChannel.purpose !== channel.purpose.value)
|
|
|
+ this.sendPurpose(channel);
|
|
|
+ if (jChannel.topic !== (channel.topic ? channel.topic.value : ""))
|
|
|
+ this.sendTopic(channel);
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.sendLiveUpdate = function(live) {
|
|
|
+};
|
|
|
+
|
|
|
+IrcConnection.prototype.poll = function() {
|
|
|
+ var now = Date.now();
|
|
|
+ var _this = this;
|
|
|
+ this.chatContext.poll(this.version || 0, now, (data) => {
|
|
|
+ if (_this.polling) {
|
|
|
+ if (data.static)
|
|
|
+ _this.sendStaticUpdate(data.static);
|
|
|
+ if (data.live)
|
|
|
+ _this.sendLiveUpdate(data.live);
|
|
|
+ if (data.v)
|
|
|
+ _this.version = data.v;
|
|
|
+ _this.poll();
|
|
|
+ }
|
|
|
+ }, null, false);
|
|
|
+};
|
|
|
+
|
|
|
IrcConnection.prototype.parse = function(str) {
|
|
|
var cmd = parseIrcCmd(str),
|
|
|
_this = this;
|
|
|
if (cmd) {
|
|
|
- if (!this.account) {
|
|
|
+ if (!this.isAuthed()) {
|
|
|
if (cmd.command === "PASS") {
|
|
|
accountManager.fromIrcPass(cmd.args[0], (account) => {
|
|
|
if (account) {
|
|
|
- _this.account = account;
|
|
|
- _this.write("PING :mzRH@PKG?l");
|
|
|
- _this.write(":mimouchat.knacki.info 001 " +_this.username +" :Welcome");
|
|
|
- _this.write(":mimouchat.knacki.info 001 " +_this.username +" :Welcome");
|
|
|
- }
|
|
|
- else
|
|
|
+ _this.checkConnectionDone(account);
|
|
|
+ } else {
|
|
|
+ console.log("Invalid password from IRC gateway");
|
|
|
_this.sock.end();
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
+ else if (cmd.command === "NICK") {
|
|
|
+ this.checkConnectionDone(null, cmd.args[0]);
|
|
|
+ }
|
|
|
+ else if (cmd.command === "USER") {
|
|
|
+ this.checkConnectionDone(null, null, cmd.args[0], cmd.args[1], cmd.args[2], cmd.args[3]);
|
|
|
+ }
|
|
|
} else {
|
|
|
switch (cmd.command) {
|
|
|
+ case "PING":
|
|
|
+ this.write(":" +SERVER_CONFIG.hostname +" PONG :" +cmd.args[0]);
|
|
|
+ break;
|
|
|
default:
|
|
|
console.log(cmd);
|
|
|
break;
|