forked from sdwilsh/tree-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.js
307 lines (293 loc) · 9.42 KB
/
channels.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
var format = require("./format");
var reporter = require("./reporter");
var builds = require("./builds");
var welshify = require("./welshify");
var mehdify = require("./mehdify");
var randompicker = require("./randompicker");
var committers = require("./committers");
var textutils = require("./textutils");
var TemporalCache = require("./temporalcache");
var updater = require("./updater");
var naturaltyping = require("./naturaltyping");
var eliza = require("./eliza");
var kIssueTrackerUrl = 'https://github.com/sdwilsh/tree-bot/issues/new';
var treeNames = require('./jsondb')('treenames.json').db;
function canonicalizeTreeName(name) {
if (treeNames.hasOwnProperty(name))
return treeNames[name];
return name;
}
function interceptFormat(interceptor, originalfn)
{
return function () {
var args = arguments;
if (args[0] === "")
return;
interceptor(args[0], function (fmt) {
args[0] = fmt;
originalfn.apply(null, args);
});
};
}
function filterEventsByRev(rev, cb) {
return function (event) {
if (event.rev === rev)
cb(event);
}
}
var interceptors = [
{ 'chance' : 0.01, 'fn' : welshify },
{ 'chance' : 0.10, 'fn' : mehdify },
];
function chooseCallbackFunction(origfn)
{
var fn = randompicker(interceptors, 'fn');
if (fn === undefined) {
return origfn;
} else {
return interceptFormat(fn, origfn);
}
}
function fprintf(output)
{
return naturaltyping.type(function () {
var text = format.apply(null, arguments);
if (text !== "")
output(text);
});
}
function Channel(name, backend) {
this.name = name;
this.trees = {};
this.watches = new TemporalCache();
this.backend = backend;
this.say = fprintf(backend.say);
this.pm = function (who) {
var output = backend.pm.bind(backend, who);
return fprintf(output);
};
this.controller = new ChannelController(this);
}
Channel.prototype = {
configDidChange: function () {
if (this.restoring)
return;
// TODO: include changeset watches
var state = {
trees: Object.keys(this.trees)
};
this.backend.channelStateChanged(state);
},
restoring: false,
restoreState: function (state) {
this.restoring = true;
state.trees.forEach(this.watch.bind(this));
this.restoring = false;
},
watch: function (name) {
if (this.trees.hasOwnProperty(name))
return;
var tree = this.trees[name] = {
watcher: new builds.Watcher(name),
success: this._treeStatusCallback.bind(this, name, 'success', reporter.success),
warning: this._treeStatusCallback.bind(this, name, 'warning', reporter.warning),
failure: this._treeStatusCallback.bind(this, name, 'failure', reporter.failure),
reportStatus: new TemporalCache(),
};
tree.watcher.on("success", tree.success);
tree.watcher.on("warning", tree.warning);
tree.watcher.on("failure", tree.failure);
this.configDidChange();
},
_treeStatusCallback: function (name, type, reporter, event) {
var cb = chooseCallbackFunction(this.say);
var tree = this.trees[name];
if (tree === undefined)
return;
var status = tree.reportStatus.get(event.rev);
if (status === undefined || event.result > status) {
// Remember status for 12 hours
tree.reportStatus.add(event.rev, event.result, 12);
reporter(cb, event);
}
},
watchChangeset: function (treeName, rev, who) {
var key = treeName+rev+who;
var cb = this.pm(who);
var watcher = new builds.Watcher(treeName);
// Do individual success reports matter?
//watcher.on("success", reporter.success.bind(reporter, cb));
watcher.on("warning", filterEventsByRev(rev, reporter.warning.bind(reporter, cb)));
watcher.on("failure", filterEventsByRev(rev, reporter.failure.bind(reporter, cb)));
// Watch for 12 hours - then no more
this.watches.add(key, watcher, 12);
this.configDidChange();
},
unwatchChangeset: function (treeName, rev, who) {
var key = treeName+rev+who;
var isWatching = this.watches.has(key);
if (isWatching) {
this.watches.remove(key);
this.configDidChange();
}
return isWatching;
},
unwatch: function (name) {
if (!this.trees.hasOwnProperty(name))
return false;
var tree = this.trees[name];
tree.watcher.removeListener("success", tree.success);
tree.watcher.removeListener("warning", tree.warning);
tree.watcher.removeListener("failure", tree.failure);
delete this.trees[name];
this.configDidChange();
return true;
},
tell: function (person) {
var self = this;
return function () {
var cb = chooseCallbackFunction(self.say);
if (person !== '')
cb = interceptFormat(textutils.prepend(person + ": "), cb);
cb.apply(self, arguments);
};
},
handleCommand: function (from, text) {
this.controller.handleCommand(from, text);
}
};
function ChannelController(channel)
{
this.channel = channel;
// TODO: allow more than one instance per channel
this.eliza = new eliza.ElizaBot();
this.eliza.memSize = 100;
this.eliza.reset();
}
ChannelController.prototype = {
watch: function (from, tree) {
tree = canonicalizeTreeName(tree);
try {
this.channel.watch(tree);
var trees = textutils.naturalJoin(Object.keys(this.channel.trees));
this.channel.tell(from)("I'm now watching: {0}", trees);
} catch (e) {
this.channel.tell(from)("I'm sorry, but I don't know anything about that tree. If it's real, please file an issue at {0}", kIssueTrackerUrl);
}
},
unwatch: function (from, tree) {
tree = canonicalizeTreeName(tree);
if (this.channel.unwatch(tree)) {
this.channel.tell(from)("I'm longer watching {0}", tree);
} else {
this.channel.tell(from)("I wasn't watching {0}", tree);
}
},
watchTree: function (from, rev, tree, who) {
tree = canonicalizeTreeName(tree);
try {
if (who === undefined || who === 'me') {
this.channel.watchChangeset(tree, rev, from);
this.channel.tell(from)("I'll let you know if {0} burns {1}", rev, tree);
} else {
this.channel.watchChangeset(tree, rev, who);
this.channel.tell(from)("I'll let {2} know if {0} burns {1}", rev, tree, who);
}
} catch (e) {
this.channel.tell(from)("I'm sorry, but I don't know anything about that tree. If it's real, please file an issue at {0}", kIssueTrackerUrl);
}
},
unwatchTree: function (from, rev, tree, who) {
tree = canonicalizeTreeName(tree);
if (who === undefined || who === 'me') {
if (this.channel.unwatchChangeset(tree, rev, from)) {
this.channel.tell(from)("No longer watching if {0} burns {1}", rev, tree);
} else {
this.channel.tell(from)("I wasn't watching {0} on {1} for you", rev, tree);
}
} else {
if (this.channel.unwatchChangeset(tree, rev, who)) {
this.channel.tell(from)("I'll stop letting {2} know if {0} burns {1}", rev, tree, who);
} else {
this.channel.tell(from)("I wasn't watching {0} on {1} for {2}", rev, tree, who);
}
}
},
identify: function (from, email, name) {
// Identity is reflexive, canonicalize order if necessary
if (/(.+)@(.+)/.test(name)) {
var tmp = name;
name = email;
email = tmp;
} else if (!/(.+)@(.+)/.test(email)) {
// This is not the command they were looking for (likely eliza)
return false;
}
if (name === 'me' || name === 'I')
name = from;
committers.add(email, name);
this.channel.tell(from)("thank you!");
},
help: function (from) {
this.channel.tell(from)("See https://github.com/sdwilsh/tree-bot/blob/master/README for a list of commands");
},
version: function (from) {
this.channel.tell(from)("I am running version {0}", updater.version);
},
update: function (from) {
if (this.channel.backend.isAuthorizedUser(from))
updater.update(this.channel.tell(from));
},
restart: function (from) {
if (this.channel.backend.isAuthorizedUser(from))
updater.restart();
},
handleCommand: function (from, text) {
var self = this;
function tryCommand(matcher, name) {
var cb = self[name];
var match = matcher.exec(text);
if (match) {
Array.prototype.splice.call(match, 0, 1, from);
var result = cb.apply(self, match);
// Some commands may have more complex conditions that they check
if (result !== undefined)
return result;
}
return match !== null;
}
var handled = this.commands.reduce(function (skip, cmd) {
if (skip) return skip;
return tryCommand.apply(null, cmd);
}, false);
if (!handled) {
this.channel.tell(from)(this.eliza.transform(text));
}
},
commands: [
[/^watch ([A-Za-z-]+)$/, "watch"],
[/^unwatch ([A-Za-z-]+)$/, "unwatch"],
[/^((?:\w|@|\.|\+)+) is ((?:\w|@|\.|\+)+)$/, "identify"],
[/^(\w) am (.+)$/, "identify"],
[/^watch ([A-Fa-f0-9]{12}) on ([A-Za-z-]+)(?: for (.+))?/, "watchTree"],
[/^unwatch ([A-Fa-f0-9]{12}) on ([A-Za-z-]+)(?: for (.+))?/, "unwatchTree"],
[/^h[ae]lp/, "help"],
[/^version$/, "version"],
[/^update/, "update"],
[/^restart$/, "restart"],
]
};
var channels = {};
exports.add = function (name, output) {
var channel = channels[name] = new Channel(name, output);
reporter.greet(channel.say);
return channel;
};
exports.has = function (name) {
return channels.hasOwnProperty(name);
};
exports.get = function (name) {
if (!channels.hasOwnProperty(name))
return undefined;
return channels[name];
};