Sfoglia il codice sorgente

[wip] parse irc commands

B Thibault 8 anni fa
parent
commit
03a4df9a08

+ 23 - 0
srv/src/ircServer/ircCmd.js

@@ -0,0 +1,23 @@
+
+const CMD_REG = (/(\:\S+ )?(\w+)((?: [^:\ ][^\ ]*)*)(?: :(.*))?/)
+
+function parseParams(str) {
+    if (str && str.length)
+        str = str.substr(1);
+    return str.split(/\s/);
+}
+
+function IrcCmd(regResult) {
+    this.prefix = regResult[1];
+    this.command = regResult[2];
+    this.args = parseParams(regResult[3]);
+    this.trailing = regResult[4];
+}
+
+IrcCmd.parse = function(str) {
+    var regResult = CMD_REG.exec(str);
+    return regResult ? new IrcCmd(regResult) : null;
+}
+
+module.exports.parse = IrcCmd.parse;
+

+ 34 - 1
srv/src/ircServer/ircConnection.js

@@ -1,14 +1,47 @@
 
+const parseIrcCmd = require('./ircCmd.js').parse
+    ,accountManager = require("../models/accounts.js").accountManager
+
 IrcConnection = module.exports.IrcConnection = function(sock) {
     var _this = this;
 
+    this.account = null;
+    this.username = "mimou";
+
     this.sock = sock;
     this.sock.on("data", (data) => {
         data.toString("utf-8").split(/\r?\n/g).forEach((str) => { _this.parse(str); });
     });
 }
 
+IrcConnection.prototype.write = function(line) {
+    this.sock.write(line +"\r\n");
+};
+
 IrcConnection.prototype.parse = function(str) {
-    console.log("IN {" +str +"}");
+    var cmd = parseIrcCmd(str),
+        _this = this;
+    if (cmd) {
+        if (!this.account) {
+            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.sock.end();
+                });
+            }
+        } else {
+            switch (cmd.command) {
+                default:
+                    console.log(cmd);
+                break;
+            }
+        }
+    }
 };
 

+ 19 - 0
srv/src/models/accounts.js

@@ -26,6 +26,13 @@ AccountManager.prototype.fromSlackIdAuth = function(id, cb) {
     });
 };
 
+AccountManager.prototype.fromIrcPass = function(pass, cb) {
+    var self = this;
+    db.database.queryFirst(TABLE_NAME, { authIrcPass: pass }, (err, result) => {
+        cb(self.fromDb(result));
+    });
+};
+
 AccountManager.prototype.fromId = function(id, cb) {
     var self = this;
     db.database.queryFirst(TABLE_NAME, { id: id }, (err, result) => {
@@ -45,6 +52,7 @@ function Account(dbResult) {
     this.authGoogleUserId;
     this.authFacebookUserId;
     this.authSlackUserEmailAndTeam;
+    this.authIrcPass;
 
     this.certificates;
     this.cguReadAndAccepted;
@@ -58,6 +66,7 @@ function Account(dbResult) {
         this.authSlackUserEmailAndTeam = dbResult.authSlackUserEmailAndTeam;
         this.authGoogleUserId = dbResult.authGoogleUserId;
         this.authFacebookUserId = dbResult.authFacebookUserId;
+        this.authIrcPass = dbResult.authIrcPass;
 
         this.certificates = dbResult.certificates;
         this.cguReadAndAccepted = !!dbResult.cguReadAndAccepted;
@@ -69,6 +78,7 @@ function Account(dbResult) {
         this.authSlackUserEmailAndTeam = null;
         this.authGoogleUserId = null;
         this.authFacebookUserId = null;
+        this.authIrcPass = null;
 
         this.createCertificate();
         this.cguReadAndAccepted = false;
@@ -82,6 +92,7 @@ Account.prototype.toDb = function() {
         authGoogleUserId: this.authGoogleUserId
         ,authFacebookUserId: this.authFacebookUserId
         ,authSlackUserEmailAndTeam: this.authSlackUserEmailAndTeam
+        ,authIrcPass: this.authIrcPass
         ,certificates: this.certificates
         ,cguReadAndAccepted: this.cguReadAndAccepted ? 1 : 0
         ,services: JSON.stringify(this.services)
@@ -94,6 +105,13 @@ Account.prototype.createCertificate = function() {
     this.certificates = null;
 };
 
+Account.prototype.generateIrcPass = function() {
+    // FIXME secure token generator
+    this.edit();
+    this.authIrcPass = "test";
+    return this;
+};
+
 Account.prototype.addService = function(serviceType, serviceName, serviceId, oauthParam) {
     this.services.push([serviceType, serviceName, serviceId, oauthParam]);
 };
@@ -150,6 +168,7 @@ module.exports.updateTable = function(dbObject, currentVersion, cb) {
             +"`authGoogleUserId` STRING UNIQUE,"
             +"`authFacebookUserId` STRING UNIQUE,"
             +"`authSlackUserEmailAndTeam` STRING UNIQUE,"
+            +"`authIrcPass` STRING UNIQUE,"
             +"`certificates` STRING,"
             +"`cguReadAndAccepted` BOOLEAN NOT NULL DEFAULT FALSE,"
             +"`services` STRING NOT NULL"