forked from zenirc/zenircbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
116 lines (105 loc) · 3.62 KB
/
bot.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
var irc = require('irc');
var api = require('./services/lib/api');
var opts = require('nomnom')
.option('config', {
abbr: 'c',
default: 'bot.json',
help: 'config file to use'
}).parse()
zenircbot = {
setup: function() {
zenircbot.config = api.load_config(opts.config);
zenircbot.pub = api.get_redis_client(zenircbot.config.redis);
zenircbot.sub = api.get_redis_client(zenircbot.config.redis);
zenircbot.unsetRedisKeys();
var cfg = zenircbot.server_config_for(0);
console.log('irc server: '+cfg.hostname+' nick: '+cfg.nick);
zenircbot.irc = new irc.Client(cfg.hostname, cfg.nick, cfg);
var bot = zenircbot.irc
bot.addListener('connect', function() {
zenircbot.pub.set('zenircbot:nick', bot.nick);
});
bot.addListener('message', function(nick, to, text, message) {
console.log(nick + ' said ' + text + ' to ' + to);
if (to == bot.nick) {
to = nick;
}
var msg = {
version: 1,
type: 'privmsg',
data: {
sender: nick,
channel: to,
message: text,
},
};
zenircbot.pub.publish('in', JSON.stringify(msg));
});
bot.addListener('nick', function(oldNick, newNick) {
zenircbot.pub.get('zenircbot:nick', function(err, nick) {
if (nick == oldNick) {
zenircbot.pub.set('zenircbot:nick', newNick);
}
});
});
bot.addListener('part', function(channel, nick, reason, message) {
console.log(nick + ' left ' + channel + ' because ' + reason);
var msg = {
version: 1,
type: 'part',
data: {
sender: nick,
channel: channel,
},
};
zenircbot.pub.publish('in', JSON.stringify(msg));
});
bot.addListener('quit', function(nick, reason, channels, message) {
console.log(nick + ' quit');
var msg = {
version: 1,
type: 'quit',
data: {
sender: nick,
},
};
zenircbot.pub.publish('in', JSON.stringify(msg));
});
bot.addListener('error', function(message) {
console.log(message);
});
var output_handlers = {
1: zenircbot.output_version_1,
};
zenircbot.sub.subscribe('out');
zenircbot.sub.on('message', function(channel, message) {
var msg = JSON.parse(message);
output_handlers[msg.version](bot, msg);
});
},
unsetRedisKeys: function(){
zenircbot.pub.del('zenircbot:nick');
},
server_config_for: function(idx) {
var cfg = zenircbot.config.servers[idx];
/* server-generic options */
cfg.debug = zenircbot.config.options.debug;
cfg.floodProtection = zenircbot.config.options.floodProtection;
cfg.showErrors = zenircbot.config.options.debug;
cfg.stripColors = zenircbot.config.options.stripColors;
return cfg;
},
output_version_1: function(bot, message) {
switch (message.type) {
case 'privmsg':
console.log(' privmsg');
bot.say(message.data.to, message.data.message);
break;
case 'raw':
console.log(' raw');
bot.send(message.command);
break;
}
}
}
zenircbot.setup();