-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
175 lines (141 loc) · 4.93 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
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
var nconf = require('nconf');
var irc = require('irc');
var http = require('http');
var logger = require('./lib/logger.js');
var restServer = require('./lib/restServer.js');
var pluginsManager = require('./lib/pluginsManager.js');
// First consider commandline arguments and environment variables, respectively.
nconf.argv().env();
// Then load configuration from a designated file.
nconf.file({
file : 'config.json'
});
nconf.defaults({
'debug' : 'false',
'password': 'password'
});
loadedMessage = pluginsManager.load('plugins');
var client = new irc.Client(nconf.get('irc:server'), nconf.get('bot-name'), {
channels : nconf.get('irc:channels'),
debug : nconf.get('debug'),
floodProtection : true
});
client.addListener('topic', function(channel, topic, nick, message) {
client.say(channel, "Tu as changé le topic... JAYJAY.");
});
client.addListener('message', PluginMessageListener);
client.addListener('message', parseMsg);
restServer.start(client);
function PluginMessageListener(from, to, message) {
for (var plugin in pluginsManager.plugins) {
try {
pluginObject = pluginsManager.plugins[plugin];
if (pluginObject.support(message)) {
pluginObject.handle(client, from, to, message);
}
} catch(e) {
client.say(from, "Error " + e.message);
client.say(from, "Stack trace : "+ e.stack);
}
}
};
/*
* client.addListener('pm', function (from, message) { console.log(from + ' =>
* ME: ' + message); parseMsg(from, '#eistitest', message); }); //
*/
//@TODO Move this to plugin
function parseMsg(from, to, message) {
if ('!' === message[0]) {
var cmd = message.split(' ');
logger.info(from + ' => ' + to + ': ' + message);
// @TODO: ajouter la possibilité de préciser le chan sur lequel
// envoyer le message dans la commande
// ex: !lastfm #channel ternel
if (nconf.get('bot-name') == to) {
to = '#eistibranlos';
}
if ('!link' == cmd[0]) {
if ('add' == cmd[1]) {
Links_saveLink(from, to, cmd);
}
if ('last' == cmd[1]) {
Links_lastsLink(from, to, cmd);
}
}
// Random item from reddit :)
if ('!reddit' == cmd[0]) {
Reddit_getRandomLink(from, to, cmd);
}
}
}
function Reddit_getRandomLink(from, to, cmd) {
logger.info("[Reddit] Reddit_getRandomLink");
var subreddit = cmd[1];
var limit = 5;
var options = {
host : 'www.reddit.com',
port : 80,
path : '/r/' + subreddit + '/.json?limit=' + limit,
'user-agent' : 'belzebot, a shitty nodejs bot by /u/ternel'
};
http.get(options, function(res) {
var json_data = '';
res.on('data', function(chunk) {
json_data = json_data + chunk;
try {
var parsed_data = JSON.parse(json_data);
var min = 0;
var rand_index = Math.floor(Math.random() * (limit - min + 1)) + min;
client.say(to, "[Reddit:" + subreddit + ":" + rand_index + "] " + parsed_data.data.children[rand_index].data.title + " - " + parsed_data.data.children[rand_index].data.url);
json_data = '';
} catch (e) {
}
});
logger.info("[Reddit] Got response from " + options.host + options.path + ": " + res.statusCode);
}).on('error', function(e) {
logger.error("[Reddit] Got error: " + e.message);
});
}
function Links_saveLink(from, to, cmd) {
var catArr = new Array('dev', 'fun', 'sexy', 'nsfw');
var cat = cmd[2];
var url = cmd[3];
var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
if (!RegExp.test(url) || !(catArr.inArray(cat))) {
client.say(to, "[Links:Help] Utilisation : !link add [dev|fun|sexy|nsfw] url");
return false;
}
var redis = require("redis"), redisClient = redis.createClient();
// save in redis
redisClient.on("error", function(err) {
logger.error(err);
});
redisClient.lpush(cat, url, redis.print);
}
function Links_lastsLink(from, to, cmd) {
var cat = cmd[2];
var redis = require("redis"), redisClient = redis.createClient();
if ('undefined' != cmd[2]) {
limit = parseInt(cmd[2], 10);
}
// get in redis
redisClient.lrange(cat, 0, 10, function(err, replies) {
if (err) {
return logger.error("error response - " + err);
}
logger.log(replies.length + " replies:");
replies.forEach(function(reply, i) {
logger.info(" " + i + ": " + reply);
client.say(to, "[Links:" + cat + "] " + reply);
});
});
}
Array.prototype.inArray = function(p_val) {
var l = this.length;
for ( var i = 0; i < l; i++) {
if (this[i] == p_val) {
return true;
}
}
return false;
};