-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
130 lines (102 loc) · 4.01 KB
/
server.js
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
//system and npm libs
var express = require('express');
var request = require('request');
var HTMLParser = require('htmlparser');
var Select = require( "soupselect" ).select;
// Configuration
var app = module.exports = express.createServer();
process.env.NODE_ENV = app.settings.env;
var io = require('socket.io').listen(app);
app.configure(function(){
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
function rstbot(msg, callback){
var command = msg.replace(/^rstbot /,'');
var returnMsg = '';
var cmdMsg = '';
var img = '';
if(command === 'whois'){
callback('<strong>rstbot whois:</strong> I\'m rstbot, welcome to chatty!');
}
else if(command === 'wat' || command === 'wut') {
request('http://watme.herokuapp.com/random', function (error, response, body) {
if (!error && response.statusCode == 200) {
var cmdMsg = '<strong>rstbot '+ command +': </strong>';
var img = '<img src="'+JSON.parse(body).wat+'" />';
callback(cmdMsg + ' ' + img);
}
});
}
else if(command === 'gob'){
var gobs = [
"http://bite-prod.s3.amazonaws.com/wp-content/uploads/2012/05/chicken-dance-2.gif",
"http://bite-prod.s3.amazonaws.com/wp-content/uploads/2012/05/Gob.gif",
"http://bite-prod.s3.amazonaws.com/wp-content/uploads/2012/05/pants.gif",
"http://bite-prod.s3.amazonaws.com/wp-content/uploads/2012/05/pennies1.gif",
"http://bite-prod.s3.amazonaws.com/wp-content/uploads/2012/05/queen_reveal.gif",
"http://bite-prod.s3.amazonaws.com/wp-content/uploads/2012/05/scotch.gif",
"http://farm1.static.flickr.com/223/511011836_56ae92ec1a_o.gif",
"http://cdn.nextround.net/wp-content/uploads/2010/03/gob-bluth-gif-6.gif"
];
cmdMsg = '<strong>rstbot '+ command +': </strong>';
img = '<img src="'+gobs[ Math.floor(Math.random() * gobs.length) ]+'" />';
callback(cmdMsg + ' ' + img);
}
else if(command === 'gif'){
var gifDomain ='http://www.gifbin.com/random';
request(gifDomain, function (error, response, body) {
var html_handler = new HTMLParser.DefaultHandler( function(){},{ ignoreWhitespace: true });
var html_parser = new HTMLParser.Parser(html_handler);
html_parser.parseComplete(body);
var url = Select( html_handler.dom, '.box a img' )[0].attribs.src;
cmdMsg = '<strong>rstbot '+ command +': </strong>';
img = '<img src="'+url+'" />';
callback(cmdMsg + ' ' + img);
});
}
else {
callback('cumbalacachanga');
}
}
app.listen(process.env.C9_PORT || process.env.PORT || 5000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('user login', function (user, cb) {
var u = user;
var next = cb;
socket.set('user', user, function(){
socket.broadcast.emit('broadcast login', u);
next(u);
});
});
socket.on('message', function (data, cb) {
if(/^rstbot .+/.test(data.msg)){
rstbot(data.msg, function(msg){
data.msg = msg;
socket.broadcast.emit('broadcast msg', data);
cb(msg);
});
}
else{
socket.broadcast.emit('broadcast msg', data);
cb(data.msg);
}
});
socket.on('disconnect', function () {
socket.get('user', function (err, user) {
if(!!user){
socket.broadcast.emit('user disconnected', user);
}
});
});
});