This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathserver.js
More file actions
246 lines (220 loc) · 7.94 KB
/
Copy pathserver.js
File metadata and controls
246 lines (220 loc) · 7.94 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// npm install request node-xmpp express nodester-api
const config = require('./config.js').settings;
var express = require('express');
var app = express.createServer();
app.configure(function(){
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use("/css", express.static(__dirname + '/css'));
});
app.get('/', function(req, res){
var data = {
title: config.status_message,
bot_email: config.client.jid
};
res.render('index.jade', data);
});
app.listen(3000);
execute_bot();
function execute_bot() {
/**
* A simple XMPP client bot aimed specifically at Google Talk
* @author Simon Holywell
* @version 2011.09.16
*/
const xmpp = require('node-xmpp');
const util = require('util');
const request_helper = require('request');
const conn = new xmpp.Client(config.client);
conn.socket.setTimeout(0);
conn.socket.setKeepAlive(true, 10000);
var commands = {};
/**
* Request the roster from the Google identity query service
* http://code.google.com/apis/talk/jep_extensions/roster_attributes.html#3
*/
function request_google_roster() {
var roster_elem = new xmpp.Element('iq', { from: conn.jid, type: 'get', id: 'google-roster'})
.c('query', { xmlns: 'jabber:iq:roster', 'xmlns:gr': 'google:roster', 'gr:ext': '2' });
conn.send(roster_elem);
}
/**
* Accept any subscription request stanza that is sent over the wire
* @param {Object} stanza
*/
function accept_subscription_requests(stanza) {
if(stanza.is('presence')
&& stanza.attrs.type === 'subscribe') {
var subscribe_elem = new xmpp.Element('presence', {
to: stanza.attrs.from,
type: 'subscribed'
});
conn.send(subscribe_elem);
send_help_information(stanza.attrs.from);
}
}
/**
* Set the status message of the bot to the supplied string
* @param {String} status_message
*/
function set_status_message(status_message) {
var presence_elem = new xmpp.Element('presence', { })
.c('show').t('chat').up()
.c('status').t(status_message);
conn.send(presence_elem);
}
/**
* Send a XMPP ping element to the server
* http://xmpp.org/extensions/xep-0199.html
*/
function send_xmpp_ping() {
var elem = new xmpp.Element('iq', { from: conn.jid, type: 'get', id: 'c2s1' })
.c('ping', { 'xmlns': 'urn:xmpp:ping' });
conn.send(elem);
}
/**
* Send a message to the supplied JID
* @param {String} to_jid
* @param {String} message_body
*/
function send_message(to_jid, message_body) {
var elem = new xmpp.Element('message', { to: to_jid, type: 'chat' })
.c('body').t(message_body);
conn.send(elem);
util.log('[message] SENT: ' + elem.up().toString());
}
/**
* A wrapper for send message to wrap the supplied command in help
* text
*/
function send_unknown_command_message(request) {
send_message(request.stanza.attrs.from, 'Unknown command: "' + request.command + '". Type "help" for more information.');
}
/**
* Send out some help information detailing the available
* bot commands
* @param {String} to_jid
*/
function send_help_information(to_jid) {
var message_body = "Currently 'bounce', 'status' and 'twitter' are supported:\n";
message_body += "b;example text\n";
message_body += "t;some search string\n";
message_body += "s;A new status message\n\n";
message_body += "See http://njsbot.simonholywell.com/ for more information.\n";
send_message(to_jid, message_body);
}
/**
* Break the message up into components
* @param {Object} stanza
*/
function split_request(stanza) {
var message_body = stanza.getChildText('body');
if(null !== message_body) {
message_body = message_body.split(config.command_argument_separator);
var command = message_body[0].trim().toLowerCase();
if(typeof message_body[1] !== "undefined") {
return { "command" : command,
"argument": message_body[1].trim(),
"stanza" : stanza };
} else {
send_help_information(stanza.attrs.from);
}
}
return false;
}
/**
* Dispatch requests sent in message stanzas
* @param {Object} stanza
*/
function message_dispatcher(stanza) {
if('error' === stanza.attrs.type) {
util.log('[error] ' + stanza.toString());
} else if(stanza.is('message')) {
var request = split_request(stanza);
if(request) {
if(!execute_command(request)) {
send_unknown_command_message(request);
}
}
}
}
/**
* Add a command to the bot for processing
* @param {String} command
* @param {Function} callback (should return true on success)
*/
function add_command(command, callback) {
commands[command] = callback;
}
/**
* Execute a command
* @param {Object} request
*/
function execute_command(request) {
if(typeof commands[request.command] === "function") {
return commands[request.command](request);
}
return false;
}
/**
* Bounce any message the user sends to the bot back to them
* @param {Object} request
*/
add_command('b', function(request) {
send_message(request.stanza.attrs.from, request.stanza.getChildText('body'));
return true;
});
/**
* Search twitter for the provided term and give back 5 tweets
* @param {Object} request
*/
add_command('t', function(request) {
var to_jid = request.stanza.attrs.from;
send_message(to_jid, 'Searching twitter, please be patient...');
var url = 'http://search.twitter.com/search.json?rpp=5&show_user=true&lang=en&q='
+ encodeURIComponent(request.argument);
request_helper(url, function(error, response, body){
if (!error && response.statusCode == 200) {
var body = JSON.parse(body);
if(body.results.length) {
for(var i in body.results) {
send_message(to_jid, body.results[i].text);
}
} else {
send_message(to_jid, 'There are no results for your query. Please try again.');
}
} else {
send_message(to_jid, 'Twitter was unable to provide a satisfactory response. Please try again.');
}
});
return true;
});
/**
* Set the bot's status message to the provided term
* @param {Object} request
*/
add_command('s', function(request) {
//set_status_message(request.argument);
send_message(request.stanza.attrs.from, "Status message now set to " + request.argument);
send_message(request.stanza.attrs.from, "This feature has been disabled on this public bot due to abuse. Sorry");
return true;
});
if(config.allow_auto_subscribe) {
// allow the bot to respond to subscription requests
// and automatically accept them if enabled in the config
conn.addListener('online', request_google_roster);
conn.addListener('stanza', accept_subscription_requests);
}
conn.addListener('stanza', message_dispatcher);
conn.on('online', function() {
set_status_message(config.status_message);
// send whitespace to keep the connection alive
// and prevent timeouts
setInterval(function() {
conn.send(' ');
}, 30000);
});
conn.on('error', function(stanza) {
util.log('[error] ' + stanza.toString());
});
}