Skip to content

Commit 3908b44

Browse files
committed
added more shizit
1 parent 3198b38 commit 3908b44

File tree

7 files changed

+131
-8
lines changed

7 files changed

+131
-8
lines changed

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
[submodule "ezcrypto-js"]
22
path = ezcrypto-js
33
url = git://github.com/tjgillies/ezcrypto-js.git
4+
[submodule "mustache.js"]
5+
path = mustache.js
6+
url = https://github.com/janl/mustache.js.git
7+
[submodule "Mu"]
8+
path = Mu
9+
url = https://github.com/raycmorgan/Mu.git
10+
[submodule "node-sqlite"]
11+
path = node-sqlite
12+
url = https://github.com/orlandov/node-sqlite.git

Mu

Submodule Mu added at 3bd7403

node-sqlite

Submodule node-sqlite added at 409251a

node.js/message.js

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,100 @@ var dgram = require('dgram');
22
var message = new Buffer(JSON.stringify({"+end":"38666817e1b38470644e004b9356c1622368fa57"}));
33
var socket = dgram.createSocket("udp4");
44
var ezcrypto = require('../ezcrypto-js/ezcrypto.js').ezcrypto;
5+
var Mu = require('../Mu/lib/mu');
6+
var fs = require('fs');
7+
var sqlite = require('../node-sqlite/sqlite');
8+
var db = new sqlite.Database();
9+
var http = require('http');
10+
var parser = require('url').parse;
11+
Mu.templateRoot = './templates';
12+
var index;
13+
var logic = {
14+
servername: "Yahoo"
15+
};
516

617

18+
db.open("tweets.db", function(error){
19+
if (error) {
20+
console.log("Error opening database");
21+
throw error;
22+
}
23+
db.getTweets = function(res){
24+
db.execute("select name,message,timestamp from tweets join names on tweets.key = names.key", function(error, rows){
25+
if (error && error.message.search("no such table") != -1){
26+
console.log(error);//throw error;
27+
db.execute("CREATE TABLE tweets (key, message, timestamp)",function(){});
28+
db.getTweets();
29+
}
30+
//res.end(JSON.stringify(rows));
31+
http.renderTweets(res, rows);
32+
console.log(rows);
33+
});
34+
};
35+
//db.getTweets();
36+
37+
});
738

8-
socket.sendData = function(message){ console.log(message.toString()); this.send(message, 0, message.length, 42424, "telehash.org"); }
39+
Mu.render('index.html', logic, {}, function(err, output){
40+
if (err){
41+
throw err;
42+
}
43+
44+
var buffer = '';
45+
46+
output.addListener('data', function (c) {buffer += c; })
47+
.addListener('end', function () { index = buffer; });
48+
});
49+
50+
51+
http.renderTweets = function(res, rows){
52+
var tweetLogic = {
53+
tweets: rows
54+
};
55+
56+
Mu.render('tweets.html', tweetLogic, {}, function(err, output){
57+
if (err){
58+
throw err;
59+
}
60+
61+
var buffer = '';
62+
63+
output.addListener('data', function (c) {buffer += c; })
64+
.addListener('end', function () { res.end(buffer) });
65+
});
66+
};
67+
68+
http.createServer(function(req,res){
69+
res.writeHead(200, {'Content-Type': 'text/html'});
70+
var output = ("An error occured");
71+
var reqdict = parser(req.url, true);
72+
var pathname = reqdict.pathname;
73+
console.log(reqdict);
74+
if (pathname == "/"){
75+
output = index;
76+
}
77+
if (pathname == "/message") {
78+
var data = reqdict.query.data
79+
};
80+
if (pathname == "/getmessage"){};
81+
if (pathname == "/tweets"){
82+
db.getTweets(res);
83+
return;
84+
}
85+
86+
res.end(output);
87+
}).listen(8080, "127.0.0.01");
988

89+
socket.sendData = function(message){ console.log(message.toString()); this.send(message, 0, message.length, 42424, "telehash.org"); }
90+
//socket.formatter = function(message){ new Buffer(JSON.stringify({".tap":[{"has":["+key"]}],"_line":ring, "_to":"208.68.163.247:42424"}));
1091
socket.on("message", function(data, rinfo){
1192
console.log(data.toString());
1293
telex = JSON.parse(data.toString());
1394
//console.log("TELEX: " + JSON.stringify(telex));
1495
if (telex["_ring"]){
1596
ring = telex["_ring"];
1697
console.log("LINE: " + ring);
98+
this.line = ring;
1799
var response = new Buffer(JSON.stringify({".tap":[{"has":["+key"]}],"_line":ring, "_to":"208.68.163.247:42424"}));
18100
this.sendData(response);
19101
}
@@ -31,6 +113,11 @@ socket.on("message", function(data, rinfo){
31113
//console.log(bool);
32114
if (test){
33115
console.log("Key validates");
116+
var timestamp = new Date().toString();
117+
db.execute("INSERT INTO tweets (key, message, timestamp) VALUES (?,?,?)", [key, message, timestamp], function(error, rows){
118+
if (error) throw error;
119+
});
120+
34121
} else {
35122
console.log("Key doesn't validate");
36123
}
@@ -40,11 +127,10 @@ socket.on("message", function(data, rinfo){
40127

41128
socket.on("listening", function(){
42129
console.log("Now listening");
43-
socket.send(message, 0, message.length, 42424, "telehash.org");
130+
setTimeout(function(){ socket.send(message, 0, message.length, 42424, "telehash.org"); socket.ping() }, 1000);
44131

45132
});
46133

47134
socket.bind(12345);
48-
49-
50-
135+
var pingmsg = new Buffer(JSON.stringify({"_line":socket.line, "_to":"208.68.163.247:42424"}));
136+
socket.ping = function(){ setTimeout(function(){ socket.sendData(pingmsg); socket.ping(); },30000) };

node.js/send.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ var dgram = require('dgram');
22
var crypto = require('crypto');
33
var fs = require('fs');
44
var ezcrypto = require('../ezcrypto-js/ezcrypto.js').ezcrypto;
5-
var keys = ezcrypto.generateKey();
6-
5+
//var process = require('process');
76

87
//var keyPem = fs.readFileSync("mykey.pem", 'ascii');
98
//var pubKey = fs.readFileSync("mycert.pem", 'ascii');
109
//var cred = crypto.createCredentials({key:keyPem});
1110
//var key = cred.key;
11+
try {
12+
data = fs.readFileSync('./keys','ascii')
13+
keys = JSON.parse(data);
14+
} catch(e) {
15+
console.log(e);
16+
keys = ezcrypto.generateKey();
17+
fs.writeFileSync('./keys',JSON.stringify(keys));
18+
}
1219

1320
var socket = dgram.createSocket("udp4");
1421
//var signer = crypto.createSign('RSA-SHA1');
15-
var msg = "I like cheese";
22+
var msg = process.argv[2];
1623
//signer.update(msg);
1724
//var signature = signer.sign(keyPem, output_format='hex');
1825
var hash = ezcrypto.hash(msg);

node.js/templates/index.html.mu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
3+
<textarea id='text-area'>Enter messages here...</textarea><br>
4+
<button id='submit'>Submit</button>
5+
<script>
6+
$("#submit").bind("click", function(){
7+
$.getJSON("/message",{data: $("#text-area").val()});
8+
});
9+
</script>
10+
</html>

node.js/templates/tweets.html.mu

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
{{#tweets}}
3+
<p>
4+
Name: {{name}}<br>
5+
Message: {{message}}<br>
6+
Timestamp: {{timestamp}}
7+
</p>
8+
{{/tweets}}
9+
</html>

0 commit comments

Comments
 (0)