-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
irc.js
51 lines (41 loc) · 1.29 KB
/
irc.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
var irc = require('irc')
var ElizaBot = require('./elizabot.js')
// hold all the sessions for each user
// She remembers you, even if you are on
// a different connection or in a different
// room.
var elizas = {}
module.exports = ElizaIRC
function ElizaIRC (host, nick, opts) {
irc.Client.call(this, host, nick, opts)
this.on('message', this._onMessage)
this.nick = nick
}
require('util').inherits(ElizaIRC, irc.Client)
ElizaIRC.prototype._onMessage = function (from, to, message) {
// figure out if this is for us
if (to !== this.nick && message.indexOf(this.nick) !== 0) {
return
}
// remove nick: from the start
if (message.indexOf(this.nick) !== 0) {
message = message.substr(this.nick.length)
message = message.replace(/^[,:]/, '').trim()
}
var private = this.nick === to
var where = private ? from : to
var prefix = private ? '' : (from + ': ')
// do we already have a bot for this user?
var eliza = elizas[from]
if (!eliza) {
eliza = elizas[from] = new ElizaBot
var init = prefix + elizas[from].getInitial()
this.say(where, init)
// just add it to the corpus so we rememeber it for later
eliza.transform(message)
return
}
// otherwise, pick up
this.say(where, prefix + eliza.transform(message))
if (eliza.quit) delete elizas[from]
}