-
Notifications
You must be signed in to change notification settings - Fork 6
/
catbot-runner.js
137 lines (110 loc) · 3.9 KB
/
catbot-runner.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
function CatRunner() {
console.log("constructing.");
this.RtmClient = undefined;
this.RTM_EVENTS = undefined;
this.token = undefined;
this.rtm = undefined;
this.commonStorage = undefined;
this.userStorage = undefined;
this.moduleStorage = undefined;
this.DEFAULT_MODULE_NAME = 'default';
console.log("constructed.");
}
CatRunner.prototype.init = function(client, events, tok) {
console.log("initializing.");
this.RtmClient = client;
this.RTM_EVENTS = events;
this.token = tok;
this.rtm = new this.RtmClient(this.token, { logLevel: 'warning' });
this.sanitize = require("sanitize-filename");
var config = require('config');
var mysql = require('mysql');
var dbConfig = config.get('DB');
if (dbConfig.useJawsURL){
this.connection = mysql.createConnection(process.env.JAWSDB_URL);
}else{
this.connection = mysql.createConnection(dbConfig);
}
this.connection.connect();
// Ensure tables exist.
var sprintf = require('sprintf');
var create_query = 'CREATE TABLE IF NOT EXISTS %s (id VARCHAR(64) NOT NULL, data_key VARCHAR(64) NOT NULL, data_value VARCHAR(32767) NOT NULL, PRIMARY KEY(id, data_key)) ENGINE=InnoDB;';
this.connection.query(sprintf(create_query, 'user_data'), function(err, result){
// TODO: error handling.
});
this.connection.query(sprintf(create_query, 'module_data'), function(err, result){
// TODO: error handling.
});
this.connection.query(sprintf(create_query, 'global_data'), function(err, result){
// TODO: error handling.
});
this.storageFactory = require("./storage_factory").StorageFactory;
this.channelRe = /#.*/;
this.userRe = /<@[UW][A-Za-z0-9]+>/;
console.log("initialized.");
this.regex = /^\?/;
};
CatRunner.prototype.start = function() {
console.log("starting");
this.rtm.start();
var self = this;
this.rtm.on(this.RTM_EVENTS.MESSAGE, function(m) {
self.handleRtmMessage(m);
});
this.rtm.on(this.RTM_EVENTS.REACTION_ADDED, function handleRtmReactionAdded(reaction) {
// TODO
});
this.rtm.on(this.RTM_EVENTS.REACTION_REMOVED, function handleRtmReactionRemoved(reaction) {
// TODO
});
console.log("started");
};
CatRunner.prototype.loader = function(moduleName) {
// don't throw if moduleName doesn't exist.
try { return require(moduleName); } catch (e) {console.log(e); };
};
CatRunner.prototype.shouldInvokeOn = function(message) {
return (message.type == 'message' && message.text && message.text.match && message.text.match(this.regex));
};
CatRunner.prototype.handleRtmMessage = function(message) {
if (this.shouldInvokeOn(message)) {
var cleanMessage = message.text.replace(this.regex, '');
var pieces = cleanMessage.split(' ');
var bareModule = this.sanitize(pieces[0]);
var moduleName = './modules/' + bareModule + '.js';
console.log("loading " + moduleName);
var handler = this.loader(moduleName);
if (!handler) {
// if we didn't find a handler, try the default handler.
console.log("loading default handler");
moduleName = './modules/' + this.DEFAULT_MODULE_NAME + '.js';
handler = this.loader(moduleName);
}
if (!handler){
console.log('no handler');
return;
}
pieces.shift();
// protect ourselves from bad code/bugs in the handlers
// TODO: maybe only do this if "production" flag is on or something like that.
try {
var self = this;
var moduleStorageFactory = new this.storageFactory(this.connection, this.sanitize(moduleName));
handler.handle(message.user, pieces.slice(0), moduleStorageFactory,
function(result){
if (result) {
if (result.message) {
// TODO: allow bots to return attachments; use them here.
self.rtm.sendMessage(result.message, message.channel);
}
}
}, bareModule);
} catch (e) {
console.log("Error while executing " + moduleName + ": " + e);
}
// unload the module so changes will be picked up without restarting the server
var name = require.resolve(moduleName);
delete require.cache[name];
}
};
exports.CatRunner = CatRunner;