-
Notifications
You must be signed in to change notification settings - Fork 29
/
shared.js
343 lines (291 loc) · 9.35 KB
/
shared.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// This is for common functions defined in many bots at once
var Sandbox = require("./lib/sandbox");
var FeelingLucky = require("./lib/feelinglucky");
var Gist = require("./lib/paste/gist");
function parse_regex_literal (text) {
var regexparsed = text.match(/s\/((?:[^\\\/]|\\.)*)\/((?:[^\\\/]|\\.)*)\/([gi]*)$/);
if (!regexparsed) {
throw new SyntaxError("Syntax is `s/expression/replacetext/gi`.");
}
var regex = new RegExp(regexparsed[1], regexparsed[3]);
return [regex, regexparsed[2].replace(/\\\//g, '/')];
}
function cleanReply(text, result){
text = text
.split('\n')
.map(function(x){
return x.trim()
.replace(/[\n\t]|\s\s+/g, ' ')
.replace(/\s\s+/g, ' ');
})
.join(' ');
var prefix = result.success ? '(okay)' : '(error)';
text = prefix + ' ' + text;
return text;
}
function factoidFindHelper(bot, context, text, suppressSearch) {
try {
var results;
var factoid = bot.factoids.find(text, true);
if (results = factoid.match(bot.executeRegex)) {
context.channel.send_reply(context.channel, results[0] + " @" + context.intent.name, {color: true});
Shared.execute_js.apply(bot, [context, factoid].concat(results.slice(1)));
} else {
context.channel.send_reply(context.intent, factoid, {color: true});
}
} catch(e) {
if (!suppressSearch) {
var reply = ["Could not find `"+text+"`."];
var found = bot.factoids.search(text);
found = found.map(function(item) {
return "\x033"+item+"\x0F";
});
if (found.length) {
reply = ["Found:"];
if (found.length > 1) {
found[found.length-1] = "and "+found[found.length-1];
}
reply.push(found.join(found.length-2 ? ", " : " "));
}
context.channel.send_reply(context.intent, reply.join(" "), {color: true});
}
}
}
var Shared = module.exports = {
google: function(context, text) {
FeelingLucky(text + " -site:w3schools.com", function(data) {
if (data) {
context.channel.send_reply (context.intent,
"\x02"+data.title+"\x0F \x032< "+data.url+" >\x0F", {color: true});
} else {
context.channel.send_reply (context.sender, "No search results found.");
}
});
},
execute_js: function(context, text, command, code) {
var engine;
var person = context.sender;
/* This should be temporary. */
if (!context.priv) {
if (command === "v8>" && context.channel.userlist["v8bot"]) {
return;
}
if (command === "js>" && context.channel.userlist["gbot2"]) {
return;
}
}
switch (command) {
case "|>": /* Multi-line input */
person.js = person.js || {timeout: null, code: []};
/* Clear input buffer after a minute */
clearTimeout (person.js.timeout);
person.js.timeout = setTimeout (function() {
person.js.code.length = 0;
context.channel.send_reply (context.sender, "Your `|>` line input has been cleared. (1 minute)");
}, 1000 * 60);
person.js.code.push (code);
return;
case "h>":
case "hs>":
engine = Sandbox.Haskell; break;
case ">>>":
case "v>":
case "v8>":
// context.channel.send_reply(context.intent, "v8 temporarily disabled, please use js> instead."); return;
engine = Sandbox.V8; break;
case "n>":
engine = Sandbox.Node; break;
case "b>":
engine = Sandbox.Babel; break;
default:
engine = Sandbox.SpiderMonkey; break;
}
if (person.js && person.js.code.length) {
code = person.js.code.join("\n") + "\n" + code;
person.js.code.length = 0;
clearTimeout (person.js.timeout);
}
this.sandbox.run(engine, 4000, code, function(result) {
var reply;
if (result.text) result.text = result.text.replace(/\r\n?/g, '\n');
if (result.reason) result.reason = result.reason.replace(/\r\n?/g, '\n');
try {
/* If theres an error, show that.
If not, show the type along with the result */
if (result.isJSEval) {
if (result.reason) {
reply = result.reason;
} else if (!result.success) {
reply = result.text;
} else {
reply = result.text;
}
if (reply.length > 100) {
var visibleReply = reply.slice(0, 80);
var formattedCode = code;
function markdownEscape(str){
return str.replace(/```/g, '{triple backticks escaped}');
}
var gistFile = "";
gistFile += "For this code:";
gistFile += "\n\n";
gistFile += "```js\n";
gistFile += markdownEscape(formattedCode);
gistFile += "\n";
gistFile += "```";
gistFile += "\n\n";
gistFile += "This was the "
+ (result.success ? 'output' : 'error')
+ ':';
gistFile += "\n\n";
gistFile += "```";
gistFile += "\n";
gistFile += markdownEscape(result.text);
gistFile += "\n";
gistFile += "```";
gistFile += "\n\n";
Gist.createGist(gistFile, {
filename: result.success ? "result.md" : "error.md",
description: "result of " + context.sender.name + "'s code"
})
.then(function(url){
var fullReply = cleanReply(visibleReply, result) + " ... " + url;
context.channel.send_reply(context.intent, fullReply, {truncate: false});
})
.catch(function(err){
var fullReply = cleanReply(visibleReply, result) + " ... <unable to create gist url>";
context.channel.send_reply(context.intent, fullReply, {truncate: false});
});
} else {
context.channel.send_reply(context.intent, cleanReply(reply, result), {truncate: true});
}
return;
}
if (result.error !== null) {
reply = result.error;
} else {
if (result.data.type !== "undefined") {
reply = (result.data.obvioustype ? "" :
"("+result.data.type+") ") + result.result;
} else {
reply = "undefined";
}
}
if (Array.isArray(result.data.console) && result.data.console.length) {
// Add console log output
reply += "; Console: "+result.data.console.join(", ");
}
context.channel.send_reply(context.intent, reply, {truncate: true});
} catch (e) {
context.channel.send_reply(
context.intent, "Unforeseen Error: "+e.name+": "+e.message);
}
}, this);
},
learn: function(context, text) {
try {
var parsed = text.match(/^(alias)?\s*("[^"]*"|.+?)\s*(=~?)\s*(.+)$/i);
if (!parsed) {
throw new SyntaxError(
"Syntax is `learn ( [alias] foo = bar | foo =~ s/expression/replace/gi )`.");
}
var alias = !!parsed[1];
var factoid = parsed[2];
var operation = parsed[3];
var value = parsed[4];
if (factoid.charAt(0) === '"') {
factoid = JSON.parse(factoid);
}
if (alias) {
var key = this.factoids.alias(factoid, value, context.sender.name);
context.channel.send_reply(context.sender,
"Learned `"+factoid+"` => `"+key+"`.");
return;
}
/* Setting the text of a factoid */
if (operation === "=") {
this.factoids.learn(factoid, value, context.sender.name);
context.channel.send_reply(context.sender, "Learned `"+factoid+"`.");
return;
/* Replacing the text of a factoid based on regular expression */
} else if (operation === "=~") {
var regexinfo = parse_regex_literal (value);
var regex = regexinfo[0];
var old = this.factoids.find(factoid, false);
var result = old.replace(regex, regexinfo[1]);
if (old === result) {
context.channel.send_reply(context.sender, "Nothing changed.");
} else {
this.factoids.learn(factoid, result, context.sender.name, value);
context.channel.send_reply(context.sender, "Changed `"+factoid+
"` to: "+result);
}
return;
}
} catch (e) {
context.channel.send_reply(context.sender, e);
}
},
forget: function(context, text) {
try {
this.factoids.forget(text, context.sender.name);
context.channel.send_reply(context.sender, "Forgot '"+text+"'.");
} catch(e) {
context.channel.send_reply(context.sender, e);
}
},
commands: function(context, text) {
var commands = this.get_commands();
var trigger = this.__trigger;
context.channel.send_reply (context.intent,
"Valid commands are: " + trigger + commands.join(", " + trigger));
},
find: function(context, text) {
factoidFindHelper(this, context, text);
},
findPlus: function(context, text, suppressSearch) {
factoidFindHelper(this, context, text, suppressSearch);
},
topic: function(context, text) {
try {
if (text) {
if (text === "revert") {
var oldtopic = context.channel.oldtopic;
if (oldtopic) {
set_topic (oldtopic);
return;
} else {
throw new Error("No topic to revert to.");
}
}
try {
var template = this.factoids.find("topic", true);
var data = JSON.parse (text);
template = template.replace (/{([a-z]+)}/g, function (match, name) {
return data.hasOwnProperty(name) ? data[name] : "";
});
set_topic (template);
} catch (e) {
var regexinfo = parse_regex_literal(text);
var regex = regexinfo[0];
var topic = context.channel.topic.replace(regex, regexinfo[1]);
if (topic === context.channel.topic) throw new Error("Nothing changed.");
set_topic (topic.replace(/\n/g, ' '));
//context.channel.set_topic(topic);
}
} else {
context.channel.send_reply(context.intent, context.channel.topic);
}
} catch (e) {
context.channel.send_reply(context.sender, e);
}
function set_topic (topic) {
context.channel.oldtopic = context.channel.topic;
context.client.get_user("ChanServ")
.send("TOPIC "+context.channel.name+" "+topic);
}
},
reauthenticate: function(context, text) {
context.client.authenticate();
}
};