Commits

PolCPP committed 346f32e

Minor tweaks to the tethealla mockup so it sends a Real Chat packet every time.
Added a "chatgate" (inserts chat messages sent by a post request into the eden chatbox, secured by a key using really dumb auth)
The server now sends post request with the chat messages to the chatgate

  • Participants
  • Parent commits 27a25ab

Comments (0)

Files changed (14)

File chatgate/gateway.php

+?php
+$MIN_LOBBY_ID = 2;
+$submit = false;
+if (isset($_POST["submit"]))
+        $submit = $_POST["submit"];
+
+if (!$submit) {
+echo "";
+?>
+
+<html>
+<title>Chokoserv new chatbox test interface</title>
+<body>
+<center>
+<table width="650">
+<tr>
+<td>
+<form method="POST" action="gateway.php">
+User_name: <input name="username"><br>
+Message: <input name="message"><br>
+GuildCard: <input name="gc"><br>
+Lobby: <input name="lobby"><br>
+Secret: <input name="secret"><br>
+
+<br><input type="submit" name="submit" value="Send">
+</td></tr></table>
+</body>
+</html>
+
+<?php
+}
+else
+{
+if ($_POST["secret"] == "3a89889bb937c6544592ad60f87f948d") {
+$username = html_entity_decode($_POST["username"])." (". html_entity_decode($_POST["gc"])  .") <i> (From PSO @ lobby ". html_entity_decode($_POST["lobby"]) .")</i>";
+$time = time();
+$message = html_entity_decode($_POST['message']);
+if (!$db = @mysql_connect ("localhost", "edenforum", "hXdkPKfgNe") )
+        die ("Can't connect to MySQL.");
+if (!@mysql_select_db ("edenforum", $db))
+        die ("Can't load db.");
+$sql = "INSERT into phpbb_chat 
+(chat_id,user_id,username,user_colour,message,time) values 
+(1,32,'$username','ffffff', '$message', $time)";
+$result = mysql_query ($sql, $db);
+if (!$result) die ("Unable to send message.");
+echo "Message sent succesfully";
+}
+}
+?>

File client.coffee

   stream: false
   events: false
 
-  constructor: (@net, @port, @host, @ask_chat) ->
+  constructor: (@net, @port, @host) ->
 
   ###
   Initializes the client
   onConnect: -> 
     this.client.attempts = 1
     this.client.connected = true
-    this.write "205"
+    if settings.askChat
+      if settings.chatLobby != ""
+        this.write "205"
+      else
+        this.write "205" + settings.chatLobby
     console.log "Connected"
 
   onData: (data)  ->
 console.log "# Check LICENSE.MIT for more details"
 console.log "############################################################"
 
-ask_chat = false
-
-if process.argv.length == 2 && process.argv[0] != "coffee"
-  ask_chat = true
-
-if process.argv.length == 3 && process.argv[0] == "coffee" 
-  ask_chat = true
-
 connection = mysql.createConnection settings.dbConnection
 
-client = new tethClient.client net, settings.tetheallaPort, settings.tetheallaHost, ask_chat
+client = new tethClient.client net, settings.tetheallaPort, settings.tetheallaHost
 client.initialize()
 
 server = new vespServer.Server net, settings.vesperiaPort, settings.vesperiaHost, connection, client

File mock/tethealla.coffee

   socket.on 'data', (data) ->
     data = data.toString()
     if data == "205"
-	    console.log "Recieved: " + data
-	    socket.write "2251 42000004         E B l a h              f   "
+      console.log "Recieved: " + data
+      buf = new Buffer [0x32, 0x32, 0x35, 0x31, 0x00, 0x34, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x34, 0x09, 0x00, 0x45, 0x00, 0x42, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x21, 0x00, 0x00, 0x00, 0x06]
+      socket.write buf
     if data.length > 35
       command = data.substring 0, 3
       uid = data.substring 3, 35

File node_modules/ent/.npmignore

+node_modules

File node_modules/ent/README.markdown

+ent
+===
+
+Encode and decode HTML entities
+
+example
+=======
+
+    var ent = require('ent');
+    console.log(ent.encode('<span>©moo</span>'))
+    console.log(ent.decode('&pi; &amp; &rho;'));
+
+output:
+
+    &lt;span&gt;&copy;moo&lt;/span&gt;
+    π & ρ
+
+methods
+=======
+
+encode(str)
+-----------
+
+Escape unsafe characters in `str` with html entities.
+
+decode(str)
+-----------
+
+Convert html entities in `str` back to raw text.
+
+credits
+=======
+
+HTML entity tables shamelessly lifted from perl's
+[HTML::Entities](http://cpansearch.perl.org/src/GAAS/HTML-Parser-3.68/lib/HTML/Entities.pm)

File node_modules/ent/entities.json

+{
+    "amp" : "&",
+    "gt" : ">",
+    "lt" : "<",
+    "quot" : "\"",
+    "apos" : "'",
+    "AElig" : 198,
+    "Aacute" : 193,
+    "Acirc" : 194,
+    "Agrave" : 192,
+    "Aring" : 197,
+    "Atilde" : 195,
+    "Auml" : 196,
+    "Ccedil" : 199,
+    "ETH" : 208,
+    "Eacute" : 201,
+    "Ecirc" : 202,
+    "Egrave" : 200,
+    "Euml" : 203,
+    "Iacute" : 205,
+    "Icirc" : 206,
+    "Igrave" : 204,
+    "Iuml" : 207,
+    "Ntilde" : 209,
+    "Oacute" : 211,
+    "Ocirc" : 212,
+    "Ograve" : 210,
+    "Oslash" : 216,
+    "Otilde" : 213,
+    "Ouml" : 214,
+    "THORN" : 222,
+    "Uacute" : 218,
+    "Ucirc" : 219,
+    "Ugrave" : 217,
+    "Uuml" : 220,
+    "Yacute" : 221,
+    "aacute" : 225,
+    "acirc" : 226,
+    "aelig" : 230,
+    "agrave" : 224,
+    "aring" : 229,
+    "atilde" : 227,
+    "auml" : 228,
+    "ccedil" : 231,
+    "eacute" : 233,
+    "ecirc" : 234,
+    "egrave" : 232,
+    "eth" : 240,
+    "euml" : 235,
+    "iacute" : 237,
+    "icirc" : 238,
+    "igrave" : 236,
+    "iuml" : 239,
+    "ntilde" : 241,
+    "oacute" : 243,
+    "ocirc" : 244,
+    "ograve" : 242,
+    "oslash" : 248,
+    "otilde" : 245,
+    "ouml" : 246,
+    "szlig" : 223,
+    "thorn" : 254,
+    "uacute" : 250,
+    "ucirc" : 251,
+    "ugrave" : 249,
+    "uuml" : 252,
+    "yacute" : 253,
+    "yuml" : 255,
+    "copy" : 169,
+    "reg" : 174,
+    "nbsp" : 160,
+    "iexcl" : 161,
+    "cent" : 162,
+    "pound" : 163,
+    "curren" : 164,
+    "yen" : 165,
+    "brvbar" : 166,
+    "sect" : 167,
+    "uml" : 168,
+    "ordf" : 170,
+    "laquo" : 171,
+    "not" : 172,
+    "shy" : 173,
+    "macr" : 175,
+    "deg" : 176,
+    "plusmn" : 177,
+    "sup1" : 185,
+    "sup2" : 178,
+    "sup3" : 179,
+    "acute" : 180,
+    "micro" : 181,
+    "para" : 182,
+    "middot" : 183,
+    "cedil" : 184,
+    "ordm" : 186,
+    "raquo" : 187,
+    "frac14" : 188,
+    "frac12" : 189,
+    "frac34" : 190,
+    "iquest" : 191,
+    "times" : 215,
+    "divide" : 247,
+    "OElig;" : 338,
+    "oelig;" : 339,
+    "Scaron;" : 352,
+    "scaron;" : 353,
+    "Yuml;" : 376,
+    "fnof;" : 402,
+    "circ;" : 710,
+    "tilde;" : 732,
+    "Alpha;" : 913,
+    "Beta;" : 914,
+    "Gamma;" : 915,
+    "Delta;" : 916,
+    "Epsilon;" : 917,
+    "Zeta;" : 918,
+    "Eta;" : 919,
+    "Theta;" : 920,
+    "Iota;" : 921,
+    "Kappa;" : 922,
+    "Lambda;" : 923,
+    "Mu;" : 924,
+    "Nu;" : 925,
+    "Xi;" : 926,
+    "Omicron;" : 927,
+    "Pi;" : 928,
+    "Rho;" : 929,
+    "Sigma;" : 931,
+    "Tau;" : 932,
+    "Upsilon;" : 933,
+    "Phi;" : 934,
+    "Chi;" : 935,
+    "Psi;" : 936,
+    "Omega;" : 937,
+    "alpha;" : 945,
+    "beta;" : 946,
+    "gamma;" : 947,
+    "delta;" : 948,
+    "epsilon;" : 949,
+    "zeta;" : 950,
+    "eta;" : 951,
+    "theta;" : 952,
+    "iota;" : 953,
+    "kappa;" : 954,
+    "lambda;" : 955,
+    "mu;" : 956,
+    "nu;" : 957,
+    "xi;" : 958,
+    "omicron;" : 959,
+    "pi;" : 960,
+    "rho;" : 961,
+    "sigmaf;" : 962,
+    "sigma;" : 963,
+    "tau;" : 964,
+    "upsilon;" : 965,
+    "phi;" : 966,
+    "chi;" : 967,
+    "psi;" : 968,
+    "omega;" : 969,
+    "thetasym;" : 977,
+    "upsih;" : 978,
+    "piv;" : 982,
+    "ensp;" : 8194,
+    "emsp;" : 8195,
+    "thinsp;" : 8201,
+    "zwnj;" : 8204,
+    "zwj;" : 8205,
+    "lrm;" : 8206,
+    "rlm;" : 8207,
+    "ndash;" : 8211,
+    "mdash;" : 8212,
+    "lsquo;" : 8216,
+    "rsquo;" : 8217,
+    "sbquo;" : 8218,
+    "ldquo;" : 8220,
+    "rdquo;" : 8221,
+    "bdquo;" : 8222,
+    "dagger;" : 8224,
+    "Dagger;" : 8225,
+    "bull;" : 8226,
+    "hellip;" : 8230,
+    "permil;" : 8240,
+    "prime;" : 8242,
+    "Prime;" : 8243,
+    "lsaquo;" : 8249,
+    "rsaquo;" : 8250,
+    "oline;" : 8254,
+    "frasl;" : 8260,
+    "euro;" : 8364,
+    "image;" : 8465,
+    "weierp;" : 8472,
+    "real;" : 8476,
+    "trade;" : 8482,
+    "alefsym;" : 8501,
+    "larr;" : 8592,
+    "uarr;" : 8593,
+    "rarr;" : 8594,
+    "darr;" : 8595,
+    "harr;" : 8596,
+    "crarr;" : 8629,
+    "lArr;" : 8656,
+    "uArr;" : 8657,
+    "rArr;" : 8658,
+    "dArr;" : 8659,
+    "hArr;" : 8660,
+    "forall;" : 8704,
+    "part;" : 8706,
+    "exist;" : 8707,
+    "empty;" : 8709,
+    "nabla;" : 8711,
+    "isin;" : 8712,
+    "notin;" : 8713,
+    "ni;" : 8715,
+    "prod;" : 8719,
+    "sum;" : 8721,
+    "minus;" : 8722,
+    "lowast;" : 8727,
+    "radic;" : 8730,
+    "prop;" : 8733,
+    "infin;" : 8734,
+    "ang;" : 8736,
+    "and;" : 8743,
+    "or;" : 8744,
+    "cap;" : 8745,
+    "cup;" : 8746,
+    "int;" : 8747,
+    "there4;" : 8756,
+    "sim;" : 8764,
+    "cong;" : 8773,
+    "asymp;" : 8776,
+    "ne;" : 8800,
+    "equiv;" : 8801,
+    "le;" : 8804,
+    "ge;" : 8805,
+    "sub;" : 8834,
+    "sup;" : 8835,
+    "nsub;" : 8836,
+    "sube;" : 8838,
+    "supe;" : 8839,
+    "oplus;" : 8853,
+    "otimes;" : 8855,
+    "perp;" : 8869,
+    "sdot;" : 8901,
+    "lceil;" : 8968,
+    "rceil;" : 8969,
+    "lfloor;" : 8970,
+    "rfloor;" : 8971,
+    "lang;" : 9001,
+    "rang;" : 9002,
+    "loz;" : 9674,
+    "spades;" : 9824,
+    "clubs;" : 9827,
+    "hearts;" : 9829,
+    "diams;" : 9830
+}

File node_modules/ent/examples/simple.js

+var ent = require('ent');
+console.log(ent.encode('<span>©moo</span>'))
+console.log(ent.decode('&pi; &amp; &rho;'));

File node_modules/ent/index.js

+var fs = require('fs');
+var entities = JSON.parse(
+    fs.readFileSync(__dirname + '/entities.json', 'utf8')
+);
+
+var revEntities = {};
+Object.keys(entities).forEach(function (key) {
+    var e = entities[key];
+    var s = typeof e === 'number' ? String.fromCharCode(e) : e;
+    revEntities[s] = key;
+});
+
+exports.encode = function (str) {
+    if (typeof str !== 'string') {
+        throw new TypeError('Expected a String');
+    }
+    
+    return str.split('').map(function (c) {
+        var e = revEntities[c];
+        var cc = c.charCodeAt(0);
+        if (e) {
+            return '&' + (e.match(/;$/) ? e : e + ';');
+        }
+        else if (c.match(/\s/)) {
+            return c;
+        }
+        else if (cc < 32 || cc >= 127) {
+            return '&#' + cc + ';';
+        }
+        else {
+            return c;
+        }
+    }).join('');
+};
+
+exports.decode = function (str) {
+    if (typeof str !== 'string') {
+        throw new TypeError('Expected a String');
+    }
+    
+    return str
+        .replace(/&#(\d+);?/g, function (_, code) {
+            return String.fromCharCode(code);
+        })
+        .replace(/&#[xX]([A-Fa-f0-9]+);?/g, function (_, hex) {
+            return String.fromCharCode(parseInt(hex, 16));
+        })
+        .replace(/&([^;\W]+;?)/g, function (m, e) {
+            var ee = e.replace(/;$/, '');
+            var target = entities[e]
+                || (e.match(/;$/) && entities[ee])
+            ;
+            
+            if (typeof target === 'number') {
+                return String.fromCharCode(target);
+            }
+            else if (typeof target === 'string') {
+                return target;
+            }
+            else {
+                return m;
+            }
+        })
+    ;
+};

File node_modules/ent/package.json

+{
+  "name": "ent",
+  "description": "Encode and decode HTML entities",
+  "version": "0.0.4",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/substack/node-ent.git"
+  },
+  "author": {
+    "name": "James Halliday",
+    "email": "mail@substack.net",
+    "url": "http://substack.net"
+  },
+  "main": "./index.js",
+  "keywords": [
+    "entities",
+    "entitify",
+    "entity",
+    "html",
+    "encode",
+    "decode"
+  ],
+  "license": "perl",
+  "scripts": {
+    "test": "expresso"
+  },
+  "engines": {
+    "node": ">=0.2.0"
+  },
+  "readme": "ent\n===\n\nEncode and decode HTML entities\n\nexample\n=======\n\n    var ent = require('ent');\n    console.log(ent.encode('<span>©moo</span>'))\n    console.log(ent.decode('&pi; &amp; &rho;'));\n\noutput:\n\n    &lt;span&gt;&copy;moo&lt;/span&gt;\n    π & ρ\n\nmethods\n=======\n\nencode(str)\n-----------\n\nEscape unsafe characters in `str` with html entities.\n\ndecode(str)\n-----------\n\nConvert html entities in `str` back to raw text.\n\ncredits\n=======\n\nHTML entity tables shamelessly lifted from perl's\n[HTML::Entities](http://cpansearch.perl.org/src/GAAS/HTML-Parser-3.68/lib/HTML/Entities.pm)\n",
+  "_id": "ent@0.0.4",
+  "dist": {
+    "shasum": "f55859fcf6112bdba76e6550ff2bb3943581adee"
+  },
+  "_from": "ent"
+}

File node_modules/ent/test/codes.js

+var assert = require('assert');
+var ent = require('ent');
+
+exports.amp = function () {
+    var a = 'a & b & c';
+    var b = 'a &amp; b &amp; c';
+    assert.eql(ent.encode(a), b);
+    assert.eql(ent.decode(b), a);
+};
+
+exports.html = function () {
+    var a = '<html> © π " \'';
+    var b = '&lt;html&gt; &copy; &pi; &quot; &apos;';
+    assert.eql(ent.encode(a), b);
+    assert.eql(ent.decode(b), a);
+};
+
+exports.num = function () {
+    var a = String.fromCharCode(1337);
+    var b = '&#1337;';
+    assert.eql(ent.encode(a), b);
+    assert.eql(ent.decode(b), a);
+    
+    assert.eql(ent.encode(a + a), b + b);
+    assert.eql(ent.decode(b + b), a + a);
+};
+
+exports.hex = function () {
+    for (var i = 0; i < 32; i++) {
+        var a = String.fromCharCode(i);
+        if (a.match(/\s/)) {
+            assert.equal(ent.decode(a), a);
+        }
+        else {
+            var b = '&#x' + i.toString(16) + ';';
+            assert.equal(ent.decode(b), a);
+            assert.equal(ent.encode(a), '&#' + i + ';');
+        }
+    }
+    
+    for (var i = 127; i < 2000; i++) {
+        var a = String.fromCharCode(i);
+        var b = '&#x' + i.toString(16) + ';';
+        var c = '&#X' + i.toString(16) + ';';
+        
+        assert.equal(ent.decode(b), a);
+        assert.equal(ent.decode(c), a);
+        
+        var encoded = ent.encode(a);
+        var encoded2 = ent.encode(a + a);
+        if (!encoded.match(/^&\w+;/)) {
+            assert.equal(encoded, '&#' + i + ';');
+            assert.equal(encoded2, '&#' + i + ';&#' + i + ';');
+        }
+    }
+};
+querystring = require 'querystring'
+http = require 'http'
+
+post_data = querystring.stringify
+  'username' : 'White Ninja'
+  'message' : 'Test message'
+  'gc' : '42000001'
+  'lobby': '1'
+  'secret': 'crab bongos'
+  'submit': 'true'
+  'json': 'true'
+
+post_options = 
+      host: 'edenserv.net'
+      port: '80'
+      path: '/chatgate/gateway.php'
+      method: 'POST'
+      headers: 
+          'Content-Type': 'application/x-www-form-urlencoded'
+          'Content-Length': post_data.length
+
+
+post_req = http.request post_options, (res) -> 
+  res.setEncoding('utf8')
+  res.on 'data', (chunk) -> 
+    console.log('Response: ' + chunk)
+
+post_req.write(post_data)
+post_req.end()

File server.coffee

 
 Security = require "./model/security"
 crypto = require "crypto"
+querystring = require 'querystring'
+http = require 'http'
+settings = require './settings'
+ent = require 'ent'
 
 #The global var is used to jump from a socket to the server.
 gServer = false
 
+
 class Server
+  chatbox_req = false
   clients:  {}
   socket: false
   security: false
+
   constructor: (@net, @port, @host, @connection, @client) ->
     gServer = this
     this.security = new Security connection
     this.client.events.on 'message', (message) -> gServer._parseTethPacket(message)
+
       
   ###
   Initializes the server
         when "225"
           lobby = data[3..4].toString().trim();
           gc = data[5..13].toString().trim();
-          char= data[18..38].toString('utf16le').trim();
-          message = data[39..].toString('utf16le').trim();
-          console.log "Cool message On lobby:" + lobby + " by GC: " + gc + " Char name: " + char + " said: " + message
-          console.log lobby
+          char= data[17..36].toString('utf16le').trim();
+          message = data[37..].toString('utf16le').trim();
+          console.log "Message recieved: Sending it to the chatbox"
+          this._sendToChatbox char, message, gc, lobby
 
         when  "220", "221"
           dataString = data.toString();
             if typeof gServer.clients[uid] != "undefined"
               gServer.clients[uid].write message
 
-
     ###
       command = data.substring 0, 3
       if command == "220" || command == "221"
         gServer.client.reconnect()        
         socket.write "202"
 
+  _sendToChatbox: (char, message, gc, lobby) ->
+    postData = querystring.stringify
+      'username' : ent.encode char
+      'message' : ent.encode message
+      'gc' : ent.encode gc
+      'lobby': ent.encode lobby
+      'secret': settings.chatboxSecret
+      'submit': 'true'
+      'json': 'true'
+    postOptions =
+      host: settings.chatboxHost
+      port: settings.chatboxPort
+      path: settings.chatboxPath
+      method: 'POST'
+      headers:
+        'Content-Type': 'application/x-www-form-urlencoded'
+        'Content-Length': postData.length
+    chatboxReq = http.request postOptions, (res) ->
+      res.setEncoding('utf8')
+      res.on 'data', (chunk) ->
+        console.log('Response: ' + chunk)
+    chatboxReq.write(postData)
+    chatboxReq.end()
+
 module.exports.Server = Server

File settings.coffee

 # Max tethealla reconnection wait time
 attemptWait = 2000
 
+#Asks the server for the messsages said on the lobby
+askChat = true
+
+#Put a number between the "" if you only want messages from a determinate lobby
+chatLobby = "1"
+
+#Chatbox settings: The server sends a post request to the web server to publish chatbox message
+chatboxHost = "edenserv.net"
+chatboxPort = "80"
+chatboxPath = "/chatgate/gateway.php"
+chatboxSecret = "3a89889bb937c6544592ad60f87f948d"
+
 module.exports.vesperiaHost = vesperiaHost
 module.exports.vesperiaPort = vesperiaPort
 module.exports.tetheallaHost = tetheallaHost      
 module.exports.dbConnection = dbConnection
 module.exports.maxAttempts = maxAttempts
 module.exports.attemptWait = attemptWait
+module.exports.askChat = askChat
+module.exports.chatLobby = chatLobby
+module.exports.chatboxHost = chatboxHost
+module.exports.chatboxPort = chatboxPort
+module.exports.chatboxPath = chatboxPath
+module.exports.chatboxSecret = chatboxSecret