-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenircbot-api.js
120 lines (107 loc) · 3.37 KB
/
zenircbot-api.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
var fs = require('fs');
var redis_lib = require('redis');
var through = require('through');
function ZenIRCBot(conf) {
var args = process.argv.slice(2)
if (args.length) {
this.host = args[0]
this.port = args[1]
this.db = args[2]
} else if (conf) {
this.host = conf.host
this.port = conf.port
this.db = conf.db
}
this.redis = this.get_redis_client()
this.out_channel = this.get_redis_client()
this.out_channel.subscribe('in')
}
ZenIRCBot.prototype.send_privmsg = function(to, message) {
this.publish_out('privmsg', to, message);
};
ZenIRCBot.prototype.send_action = function(to, message) {
this.publish_out('privmsg_action', to, message);
};
ZenIRCBot.prototype.publish_out = function(type, to, message) {
var self = this;
if (typeof(to) == 'string') {
to = [to];
}
to.forEach(function(destination) {
self.redis.publish('out', JSON.stringify({
version: 1,
type: type,
data: {
to: destination,
message: message
}
}));
});
};
ZenIRCBot.prototype.join_channel = function(channel) {
var self = this;
self.redis.publish('out', JSON.stringify({
version: 1,
type: 'raw',
command: 'JOIN ' + channel
}));
};
ZenIRCBot.prototype.part_channel = function(channel, message) {
var self = this;
var message = message || 'Doing as my master bids.'
self.redis.publish('out', JSON.stringify({
version: 1,
type: 'raw',
command: 'PART ' + channel + ' :' + message
}));
};
ZenIRCBot.prototype.send_admin_message = function(message) {
var self = this;
self.redis.get('zenircbot:admin_spew_channels', function(err, channels) {
self.send_privmsg(channels, message);
});
};
ZenIRCBot.prototype.register_commands = function(service, commands) {
var self = this;
self.send_admin_message(service + ' online!');
var filtered = self.filter({version: 1, type: 'directed_privmsg'})
filtered.on('data', function(msg){
if (msg.data.message == 'commands') {
commands.forEach( function(command) {
self.send_privmsg(msg.data.sender,
service + ': ' +
command.name + ' - ' +
command.description);
});
} else if (msg.data.message == 'services') {
self.send_privmsg(msg.data.sender, service);
}
});
};
ZenIRCBot.prototype.get_redis_client = function() {
return redis_lib.createClient(this.port,
this.host, {
selected_db: this.db
});
};
ZenIRCBot.prototype.filter = function(query) {
var stream = through()
this.out_channel.on('message', function(subChannel, message) {
var msg = JSON.parse(message)
var results = true
for (var param in query) {
if (query.hasOwnProperty(param)) {
results = results && msg[param] == query[param]
}
}
if (results) {
stream.queue(msg)
}
})
return stream
};
function load_config(name) {
return JSON.parse(fs.readFileSync(name, 'utf8'));
}
module.exports.ZenIRCBot = ZenIRCBot;
module.exports.load_config = load_config;