Permalink
Browse files

added more shizit

  • Loading branch information...
1 parent 3198b38 commit 3908b44ab6e4b757253dd8b3cae6883cfa214d70 @tjgillies tjgillies committed Jan 30, 2011
Showing with 131 additions and 8 deletions.
  1. +9 −0 .gitmodules
  2. +1 −0 Mu
  3. +1 −0 node-sqlite
  4. +91 −5 node.js/message.js
  5. +10 −3 node.js/send.js
  6. +10 −0 node.js/templates/index.html.mu
  7. +9 −0 node.js/templates/tweets.html.mu
View
@@ -1,3 +1,12 @@
[submodule "ezcrypto-js"]
path = ezcrypto-js
url = git://github.com/tjgillies/ezcrypto-js.git
+[submodule "mustache.js"]
+ path = mustache.js
+ url = https://github.com/janl/mustache.js.git
+[submodule "Mu"]
+ path = Mu
+ url = https://github.com/raycmorgan/Mu.git
+[submodule "node-sqlite"]
+ path = node-sqlite
+ url = https://github.com/orlandov/node-sqlite.git
1 Mu
Submodule Mu added at 3bd740
Submodule node-sqlite added at 409251
View
@@ -2,18 +2,100 @@ var dgram = require('dgram');
var message = new Buffer(JSON.stringify({"+end":"38666817e1b38470644e004b9356c1622368fa57"}));
var socket = dgram.createSocket("udp4");
var ezcrypto = require('../ezcrypto-js/ezcrypto.js').ezcrypto;
+var Mu = require('../Mu/lib/mu');
+var fs = require('fs');
+var sqlite = require('../node-sqlite/sqlite');
+var db = new sqlite.Database();
+var http = require('http');
+var parser = require('url').parse;
+Mu.templateRoot = './templates';
+var index;
+var logic = {
+ servername: "Yahoo"
+};
+db.open("tweets.db", function(error){
+ if (error) {
+ console.log("Error opening database");
+ throw error;
+ }
+ db.getTweets = function(res){
+ db.execute("select name,message,timestamp from tweets join names on tweets.key = names.key", function(error, rows){
+ if (error && error.message.search("no such table") != -1){
+ console.log(error);//throw error;
+ db.execute("CREATE TABLE tweets (key, message, timestamp)",function(){});
+ db.getTweets();
+ }
+ //res.end(JSON.stringify(rows));
+ http.renderTweets(res, rows);
+ console.log(rows);
+ });
+ };
+ //db.getTweets();
+
+});
-socket.sendData = function(message){ console.log(message.toString()); this.send(message, 0, message.length, 42424, "telehash.org"); }
+Mu.render('index.html', logic, {}, function(err, output){
+ if (err){
+ throw err;
+ }
+
+ var buffer = '';
+
+ output.addListener('data', function (c) {buffer += c; })
+ .addListener('end', function () { index = buffer; });
+});
+
+
+http.renderTweets = function(res, rows){
+ var tweetLogic = {
+ tweets: rows
+ };
+
+ Mu.render('tweets.html', tweetLogic, {}, function(err, output){
+ if (err){
+ throw err;
+ }
+
+ var buffer = '';
+
+ output.addListener('data', function (c) {buffer += c; })
+ .addListener('end', function () { res.end(buffer) });
+ });
+};
+
+http.createServer(function(req,res){
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ var output = ("An error occured");
+ var reqdict = parser(req.url, true);
+ var pathname = reqdict.pathname;
+ console.log(reqdict);
+ if (pathname == "/"){
+ output = index;
+ }
+ if (pathname == "/message") {
+ var data = reqdict.query.data
+ };
+ if (pathname == "/getmessage"){};
+ if (pathname == "/tweets"){
+ db.getTweets(res);
+ return;
+ }
+
+ res.end(output);
+}).listen(8080, "127.0.0.01");
+socket.sendData = function(message){ console.log(message.toString()); this.send(message, 0, message.length, 42424, "telehash.org"); }
+//socket.formatter = function(message){ new Buffer(JSON.stringify({".tap":[{"has":["+key"]}],"_line":ring, "_to":"208.68.163.247:42424"}));
socket.on("message", function(data, rinfo){
console.log(data.toString());
telex = JSON.parse(data.toString());
//console.log("TELEX: " + JSON.stringify(telex));
if (telex["_ring"]){
ring = telex["_ring"];
console.log("LINE: " + ring);
+ this.line = ring;
var response = new Buffer(JSON.stringify({".tap":[{"has":["+key"]}],"_line":ring, "_to":"208.68.163.247:42424"}));
this.sendData(response);
}
@@ -31,6 +113,11 @@ socket.on("message", function(data, rinfo){
//console.log(bool);
if (test){
console.log("Key validates");
+ var timestamp = new Date().toString();
+ db.execute("INSERT INTO tweets (key, message, timestamp) VALUES (?,?,?)", [key, message, timestamp], function(error, rows){
+ if (error) throw error;
+ });
+
} else {
console.log("Key doesn't validate");
}
@@ -40,11 +127,10 @@ socket.on("message", function(data, rinfo){
socket.on("listening", function(){
console.log("Now listening");
- socket.send(message, 0, message.length, 42424, "telehash.org");
+ setTimeout(function(){ socket.send(message, 0, message.length, 42424, "telehash.org"); socket.ping() }, 1000);
});
socket.bind(12345);
-
-
-
+var pingmsg = new Buffer(JSON.stringify({"_line":socket.line, "_to":"208.68.163.247:42424"}));
+socket.ping = function(){ setTimeout(function(){ socket.sendData(pingmsg); socket.ping(); },30000) };
View
@@ -2,17 +2,24 @@ var dgram = require('dgram');
var crypto = require('crypto');
var fs = require('fs');
var ezcrypto = require('../ezcrypto-js/ezcrypto.js').ezcrypto;
-var keys = ezcrypto.generateKey();
-
+//var process = require('process');
//var keyPem = fs.readFileSync("mykey.pem", 'ascii');
//var pubKey = fs.readFileSync("mycert.pem", 'ascii');
//var cred = crypto.createCredentials({key:keyPem});
//var key = cred.key;
+try {
+ data = fs.readFileSync('./keys','ascii')
+ keys = JSON.parse(data);
+} catch(e) {
+ console.log(e);
+ keys = ezcrypto.generateKey();
+ fs.writeFileSync('./keys',JSON.stringify(keys));
+}
var socket = dgram.createSocket("udp4");
//var signer = crypto.createSign('RSA-SHA1');
-var msg = "I like cheese";
+var msg = process.argv[2];
//signer.update(msg);
//var signature = signer.sign(keyPem, output_format='hex');
var hash = ezcrypto.hash(msg);
@@ -0,0 +1,10 @@
+<html>
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
+<textarea id='text-area'>Enter messages here...</textarea><br>
+<button id='submit'>Submit</button>
+<script>
+$("#submit").bind("click", function(){
+ $.getJSON("/message",{data: $("#text-area").val()});
+});
+</script>
+</html>
@@ -0,0 +1,9 @@
+<html>
+{{#tweets}}
+<p>
+Name: {{name}}<br>
+Message: {{message}}<br>
+Timestamp: {{timestamp}}
+</p>
+{{/tweets}}
+</html>

0 comments on commit 3908b44

Please sign in to comment.