Repository navigation
Expand file tree
/
Copy pathmumbles.js
More file actions
139 lines (112 loc) · 3.21 KB
/
Copy pathmumbles.js
File metadata and controls
139 lines (112 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// first attempt to connect to campfire from node
// mumblebot, mumblez
var http = require('http');
var https = require('https');
/*******************************************************************************
* need functionality
* 1. Connect Campfire, join a room
* 2. Listen to Stream
* 3. React to messages
*******************************************************************************/
// Basic object to define our connection to campfire
var Campfire = function(name, token) {
this.name = name;
this.token = token;
this.http = https;
this.port = 443; // hard-coded to SSL for now, biatch
this.room = 394860; // hard-coded to Danger room
this.domain = name + ".campfirenow.com";
};
Campfire.prototype.get = function(path, callback) {
this.request('GET', path, null, callback);
};
Campfire.prototype.post = function(path, body, callback) {
this.request('POST', path, body, callback);
};
Campfire.prototype.request = function(method, path, body, callback) {
var headers = {
'Authorization' : 'Basic ' + new Buffer(this.token).toString('base64'),
'host' : this.domain,
'Content-Type': 'application/json'
};
if (method == 'POST') {
if (typeof(body) != 'string') {
body = JSON.stringify(body);
}
headers['Content-Length'] = body.length;
}
var options = {
host: this.domain,
port: this.port,
path: path,
method: method,
headers: headers
};
var request = this.http.request(options, function(response) {
var data = "";
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
try {
data = JSON.parse(data);
} catch(e) {
}
callback(data);
});
});
if (method == 'POST') {
request.write(body);
}
request.end();
};
Campfire.prototype.join = function(callback) {
this.post('/room/' + this.room + '/join.json', '', callback);
};
Campfire.prototype.listen = function(callback) {
var headers = {
'Authorization': 'Basic ' + new Buffer(this.token).toString('base64'),
'host' : 'streaming.campfirenow.com',
'Content-Type': 'application/json'
};
var options = {
host: "streaming.campfirenow.com",
port: this.port,
path: '/room/' + this.room + '/live.json',
method: 'GET',
headers: headers
};
var request = this.http.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
if (chunk.trim() === '') {
return;
}
chunk = chunk.split("\r");
for (var i = 0; i < chunk.length; ++i) {
if (chunk[i].trim() !== '') {
try {
callback(JSON.parse(chunk[i]));
} catch(e) {}
}
}
});
}).end();
};
/* needs configuration */
var instance = new Campfire(null,null);
instance.join(function() {
console.log("joined room");
});
instance.listen(function(data) {
jf(data.type == 'TextMessage') {
var formumblebot = /mumblebot/.test(data.body);
if(formumblebot) {
console.log(data.body.split(/\s/g));
}
}
});
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body>Welcome to Mumblebot Control</body></html>');
}).listen(9400, "0.0.0.0");