Procházet zdrojové kódy

[add][IRC-con] /histo api

B Thibault před 8 roky
rodič
revize
097c00b227
2 změnil soubory, kde provedl 49 přidání a 5 odebrání
  1. 1 1
      srv/src/ircServer/ircCmd.js
  2. 48 4
      srv/src/ircServer/ircConnection.js

+ 1 - 1
srv/src/ircServer/ircCmd.js

@@ -9,7 +9,7 @@ function parseParams(str) {
 
 function IrcCmd(regResult) {
     this.prefix = regResult[1];
-    this.command = regResult[2];
+    this.command = (regResult[2] || '').toUpperCase();
     this.args = parseParams(regResult[3]);
     this.trailing = regResult[4];
     if (this.trailing)

+ 48 - 4
srv/src/ircServer/ircConnection.js

@@ -206,6 +206,17 @@ IrcConnection.prototype.chanCompat = function(chanId) {
     return '#' +chan.name.replace(/\s/g, '') +'@' +ctx.team.name;
 };
 
+IrcConnection.prototype.writeMsg = function(msg, chanCompatId) {
+    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);
+};
+
+IrcConnection.prototype.writeNotice = function(msg) {
+    this.write(":" +SERVER_CONFIG.hostname +" NOTICE " +SERVER_CONFIG.hostname +" :" +msg);
+};
+
 IrcConnection.prototype.sendLiveUpdate = function(live) {
     for (var chanId in live) {
         let chanCompatId = this.chanCompat(chanId, this);
@@ -219,10 +230,7 @@ IrcConnection.prototype.sendLiveUpdate = function(live) {
                     }
                 }
             }
-            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.writeMsg(msg, chanCompatId);
         }, this);
     }
 };
@@ -336,6 +344,42 @@ IrcConnection.prototype.parse = function(str) {
                     }
                 break;
 
+                case "HISTO":
+                case "HISTORY":
+                    if (cmd.args[0].toUpperCase() === "HELP") {
+                        this.writeNotice("Usage: /histo #channel [count=50]");
+                    } else {
+                        var target = cmd.args[0].split('@', 2),
+                            found = false;
+
+                        if (target[0][0] === '#')
+                            target[0] = target[0].substr(1);
+                        ctxLoop: 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])) {
+                                        found = true;
+                                        ctx.fetchHistory(chan, (msgs) => {
+                                            msgs.sort((a, b) => {
+                                                return a.ts -b.ts;
+                                            });
+                                            msgs.forEach((msg) => {
+                                                msg.user = msg.user || msg.userId;
+                                                this.writeMsg(msg, cmd.args[0]);
+                                            });
+                                        }, Math.max(5, Math.min(150, cmd.args[1] || 50)));
+                                        break ctxLoop;
+                                    }
+                                }
+                            }
+                        }
+                        if (!found)
+                            this.writeNotice("Chan not found: " +cmd.args[0]);
+                    }
+                break;
+
                 default:
                     if (DEBUG)
                         console.log(cmd);