Repository navigation
Expand file tree
/
Copy pathnBot.js
More file actions
executable file
·269 lines (244 loc) · 6.96 KB
/
Copy pathnBot.js
File metadata and controls
executable file
·269 lines (244 loc) · 6.96 KB
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
#!/usr/bin/env node
// nBot, stupid irc bot made for fun
// Copyright (C) 2015 nnnn20430 (nnnn20430@mindcraft.si.eu.org)
//
// nBot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// nBot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
"use strict";
//variables
var fs = require('fs');
var Bot = require(__dirname+'/lib/bot');
var Shell = require(__dirname+'/lib/shell');
//instance manager object
var im = {};
//im variables
im.options = {};
im.iOpts = [];
im.iArr = [];
im.shell = {};
//settings constructor
im.SettingsConstructor = {
main: function (modified) {
//force 'new' object keyword
if(!(this instanceof im.SettingsConstructor.main)) {
return new im.SettingsConstructor.main(modified);
}
var attrname;
this.shellEnabled = true;
this.shellPrompt = '> ';
this.logs = true;
this.ignoreUncaughtExceptions = false;
for (attrname in modified) {this[attrname]=modified[attrname];}
},
connection: function (modified) {
//force 'new' object keyword
if(!(this instanceof im.SettingsConstructor.connection)) {
return new im.SettingsConstructor.connection(modified);
}
var attrname;
this.connectionName = 'Connection0';
this.botName = 'nBot';
this.botMode = '0';
this.botUpdateInterval = 10000;
this.ircServer = 'localhost';
this.ircServerPort = 6667;
this.ircServerPassword = '';
this.localBindAddress = null;
this.localBindPort = null;
this.ipFamily = 4;
this.tls = false;
this.tlsRejectUnauthorized = false;
this.socks5_host = '';
this.socks5_port = 1080;
this.socks5_username = '';
this.socks5_password = '';
this.channels = [ '#nBot' ];
this.ircRelayServerEnabled = true;
this.ircResponseListenerLimit = 30;
this.ircMultilineMessageMaxLines = 300;
this.errorsIncludeStack = false;
this.pluginDir = './plugins';
this.plugins = [
'cron',
'reconnect',
'simpleMsg',
'commands'
];
this.pluginsSettings = {};
for (attrname in modified) {this[attrname]=modified[attrname];}
}
};
//handle uncaught exceptions
im.uncaughtExceptionHandle = function (err) {
console.log(err.stack);
console.log('An uncaught exception occurred please report this.');
if (im.options.ignoreUncaughtExceptions) {
console.log('WARNING: Ignoring of uncaught exceptions enabled!');
} else {
process.exit(1);
}
};
//load settings
im.settingsLoad = function (file, callback) {
file = file||"settings.json";
var s, c;
if (callback !== undefined) {
fs.access(file, fs.F_OK, function (err) {
if (!err) {
try {
fs.readFile(file, {"encoding": "utf8"}, function (err, data) {
if (err) throw err;
s = JSON.parse(data);
s = new im.SettingsConstructor.main(s);
for (c in s.connections) {
s.connections[c] = new im.SettingsConstructor.connection(s.connections[c]);
}
callback(s);
});
} catch (e) {
im.log('Error happened when loading settings:\n'+e.stack);
}
} else if (err.code == "ENOENT") {
s = new im.SettingsConstructor.main({
connections: [new im.SettingsConstructor.connection({})]
});
im.settingsSave(null, s, function () {callback(s);});
}
});
}
};
//save settings
im.settingsSave = function (file, data, callback) {
file = file||"settings.json";
data = data||im.options;
try {
fs.writeFile(file, JSON.stringify(data, null, '\t'), function (err) {
if (err) throw err;
if (callback !== undefined) {
callback();
}
});
} catch (e) {
im.log('Error happened when saving settings:\n'+e.stack);
}
};
//kill one bot instance using iId
im.killInstance = function (iId, reason, force) {
force = (reason)?
((force)?true:false):
((force===false)?false:true);
reason = reason||'Leaving';
var bot = im.iArr[iId];
if (bot) {
for (var plugin in bot.plugins) {
bot.pluginDisable(plugin);
}
if (force === true) {
bot.kill();
} else {
if (!bot.ircConnection.destroyed) {
bot.ircSendCommandQUIT(reason);
} else {
bot.kill();
}
}
}
};
//kill all bot instances
im.killAllInstances = function (reason, force) {
for (var iId in im.iArr) {
im.killInstance(iId, reason, force);
}
};
//log using shell lib or directly to stdout
im.log = function (data) {
if (im.options.logs) {
if (im.options.shellEnabled) {
im.shell.log(data);
} else {
process.stdout.write(data+'\n');
}
}
};
//handle botReceivedPRIVMSG botEvent from bot instance
im.botEventHandle_botReceivedPRIVMSG = function (iId, data) {
var nick = data[1][0],
to = data[4][0],
message = data[5]||data[4][1];
var connectionName = im.iOpts[iId].connectionName||iId;
im.log('['+connectionName+':'+to+'] <'+nick+'>: '+message);
};
//handle botReceivedNOTICE botEvent from bot instance
im.botEventHandle_botReceivedNOTICE = function (iId, data) {
var nick = data[1][0],
to = data[4][0],
message = data[5]||data[4][1];
var connectionName = im.iOpts[iId].connectionName||iId;
im.log('[NOTICE('+connectionName+':'+to+')] <'+nick+'>: '+message);
};
//handle botEvent from bot instance
im.botEventHandle = function (iId, event) {
if (im['botEventHandle_'+event.eventName] !== undefined) {
im['botEventHandle_'+event.eventName](iId, event.eventData);
}
};
//handle log messages from bot instance
im.botLogMessageHandle = function (iId, data) {
var connectionName = im.iOpts[iId].connectionName||iId;
im.log(connectionName+"-> "+data);
};
//init shell
im.initShell = function () {
im.shell = new Shell(im);
im.shell.init();
};
//create bot instance, from instance options, using instance id
im.createInstance = function (iId) {
var bot = new Bot(im.iOpts[iId]);
var options = bot.options;
//wrapper functions to preserve current scope (iID)
function botEventHandle(event) {
im.botEventHandle(iId, event);
}
function botLogMessageHandle(data) {
im.botLogMessageHandle(iId, data);
}
//add bot to im
im.iArr[iId] = bot;
//expose instance manager
bot.im = im;
//listen for events
bot.eventsAdd('botEvent', botEventHandle);
bot.eventsAdd('botLogMessage', botLogMessageHandle);
//load plugins from settings
for (var plugin in im.iOpts[iId].plugins) {
bot.pluginLoad(options.plugins[plugin],
options.pluginDir+'/'+options.plugins[plugin]+'.js');
}
//init bot
bot.init();
};
//entry point
(function main(){
//handle uncaught errors
process.on('uncaughtException', im.uncaughtExceptionHandle);
//load settings and start the bot
im.settingsLoad(null, function (data) {
im.options = data;
im.iOpts = im.options.connections;
if(im.options.shellEnabled){im.initShell();}
for (var iId in im.iOpts) {
im.createInstance(iId);
}
});
})();