Forráskód Böngészése

[tmp] receive/send messages (buggy)

B Thibault 8 éve
szülő
commit
8e64f14c99
1 módosított fájl, 97 hozzáadás és 12 törlés
  1. 97 12
      srv/src/ircServer/ircConnection.js

+ 97 - 12
srv/src/ircServer/ircConnection.js

@@ -5,6 +5,8 @@ const parseIrcCmd = require('./ircCmd.js').parse
     ,MultiChatManager = require("../multichatManager.js").MultiChatManager
     ,slackManager = require("../slackManager.js").SlackManager
 
+    ,PrivateMessageRoom = require("../room.js").PrivateMessageRoom
+
     ,CONFIG = require("../../config.js");
 
 const SERVER_CONFIG = {
@@ -92,15 +94,34 @@ IrcConnection.prototype.checkConnectionDone = function(account, nickname, userna
     return false;
 };
 
+const NAMES_NICK_LIMIT = 3;
+
 IrcConnection.prototype.sendJoin = function(channel) {
-    var res = this.joinedChannels[channel.id] = { topic: "", purpose: "" };
-    this.write(":" +this.nickname +" JOIN #" +channel.id);
+    let res = this.joinedChannels[channel.id] = { topic: "", purpose: "" },
+        chanCompat = this.chanCompat(channel.id);
+
+    this.write(":" +this.nickname +" JOIN " +chanCompat);
+    if (channel.members) {
+        let chanMembers = [];
+
+        channel.members.forEach((userId) => {
+            if (!chanMembers.length || chanMembers[chanMembers.length -1].length >= NAMES_NICK_LIMIT)
+                chanMembers.push([]);
+            chanMembers[chanMembers.length -1].push(this.userCompat(userId));
+        }, this);
+        chanMembers.forEach((userNames) => {
+            if (userNames.length) {
+                this.write(":" +SERVER_CONFIG.hostname +" 353 " +this.nickname +' @ ' +chanCompat +" :" +userNames.join(' '));
+            }
+        });
+        this.write(":" +SERVER_CONFIG.hostname +" 366 " +this.nickname +' ' +chanCompat +" :End of /NAMES list.");
+    }
     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);
+    this.write(":" +(channel.topic.creator ? this.chatContext.getUser(channel.topic.creator).getName() : SERVER_CONFIG.hostname) +" TOPIC #" +this.chanCompat(channel.id) +" :" +channel.topic.value);
 };
 
 IrcConnection.prototype.sendPurpose = function(channel) {
@@ -112,19 +133,54 @@ 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);
+                if (!channel.user) {
+                    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.userCompat = function(userId, replaceSelf) {
+    var ctx = this.chatContext.getUserContext(userId);
+    if (!ctx) {
+        return userId;
+    }
+    ctx = ctx.getChatContext();
+    if (replaceSelf !== false && ctx.self && ctx.self.id === userId) {
+        return this.nickname;
+    }
+    return ctx.users[userId].getName() +'@' +ctx.team.name;
+};
+
+IrcConnection.prototype.chanCompat = function(chanId) {
+    var ctx = this.chatContext.getChannelContext(chanId);
+    if (!ctx) {
+        return '#' +chanId;
+    }
+    ctx = ctx.getChatContext();
+    var chan = ctx.channels[chanId];
+    if (chan instanceof PrivateMessageRoom)
+        return this.userCompat(chan.user.id, false);
+    return '#' +chan.name.replace(/\s/g, '') +'@' +ctx.team.name;
+};
+
 IrcConnection.prototype.sendLiveUpdate = function(live) {
+    for (var chanId in live) {
+        let chanCompatId = this.chanCompat(chanId, this);
+        live[chanId].forEach((msg) => {
+            var pretext = ":" +(msg.user ? this.userCompat(msg.user) : msg.username) +" PRIVMSG " +chanCompatId +" :";
+            if (msg.isMeMessage)
+                pretext += String.fromCharCode(0x01) +"ACTION ";
+            this.write(pretext +msg.text);
+        }, this);
+    }
 };
 
 IrcConnection.prototype.poll = function() {
@@ -147,7 +203,10 @@ IrcConnection.prototype.parse = function(str) {
     var cmd = parseIrcCmd(str),
         _this = this;
     if (cmd) {
-        if (!this.isAuthed()) {
+        console.log("[IRC] <<< " +str);
+        if (cmd.command === "QUIT") {
+            _this.sock.end();
+        } else if (!this.isAuthed()) {
             if (cmd.command === "PASS") {
                 accountManager.fromIrcPass(cmd.args[0], (account) => {
                     if (account) {
@@ -169,6 +228,32 @@ IrcConnection.prototype.parse = function(str) {
                 case "PING":
                     this.write(":" +SERVER_CONFIG.hostname +" PONG :" +cmd.args[0]);
                 break;
+                case "PRIVMSG":
+                    let msg = cmd.args[1],
+                        target = cmd.args[0].split('@', 2);
+                    if (target[0] === '#')
+                        target[0] = target[0].substr(1);
+                    for (let i =0, nbCtx = this.chatContext.contexts.length; i < nbCtx; i++) {
+                        let ctx = this.chatContext.contexts[i];
+                        if (ctx.getChatContext().team && ctx.getChatContext().team.name === target[1]) {
+                            for (let chanId in ctx.getChatContext().channels) {
+                                let chan = ctx.getChatContext().channels[chanId];
+                                if (chan.name === target[0] || (chan.user && chan.user.getName() === target[0])) {
+                                    if (msg.charCodeAt(0) === 0x01 && msg.substr(1, 7) === "ACTION ") {
+                                        if (msg.charCodeAt(msg.length -1) === 0x01)
+                                            msg = msg.substr(8, msg.length -9);
+                                        else
+                                            msg = msg.substr(8);
+                                        ctx.sendMeMsg(chan, [ msg ]);
+                                    } else {
+                                        ctx.sendMsg(chan, [ msg ]);
+                                    }
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                break;
                 default:
                     console.log(cmd);
                 break;