-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
151 lines (134 loc) · 4.9 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
var irc = require ('irc');
var S = require('string');
var settings = require('./settings');
var Database = require('./src/db');
var util = require('./src/util');
var user = require('./src/user');
var admin = require('./src/admin');
main();
function speak(text, bundle) {
var lines = text.split("<br>");
var truncated_lines = lines.slice(0, 3);
for (var i = 0; i < truncated_lines.length; i++) {
processed_text = util.htmlToIRC(S(truncated_lines[i]).decodeHTMLEntities().s);
bundle.bot.say(bundle.to, processed_text);
util.addToHistory({ "from": bundle.bot.opt.nick, "to": bundle.to, "text": processed_text }, bundle);
}
if (lines.length > truncated_lines.length) {
speak("... (truncated output -- originally " + lines.length + " lines) ...", bundle);
}
}
function setup(mode) {
var mode_config = settings.modes[mode];
var db = new Database(settings.database_file, mode);
var password = undefined;
var sasl = false;
if (mode_config['password'] && mode_config['password'].length > 0) {
var password = mode_config['password'];
var sasl = true;
}
var bot = new irc.Client(
mode_config['server'],
mode_config['bot_name'],
{
channels: mode_config['channels'],
realName: 'IRC bot by Aqwis',
userName: S(mode_config['bot_name']).camelize().s,
sasl: sasl,
password: password
}
);
util.logToFile.mode = mode;
var reactToMessage = function(nick, to, text, message) {
var trimmed_message = text.trim();
var result_text = "";
var bundle = {
"mode": mode,
"bot": bot,
"nick": nick,
"to": to,
"db": db,
"message": message,
};
util.addToHistory({"from": nick, "to": to, "text": text}, bundle);
if (trimmed_message[0] === ".") {
// Commands available to all users are prefixed with .
user.lookupUserCommand(trimmed_message.slice(1), bundle, function(result_text) {
if (result_text) {
speak(result_text, bundle);
}
});
} else if (trimmed_message[0] === "@") {
// Commands available to administrators are prefixed with @
admin.lookupAdminCommand(trimmed_message.slice(1), bundle, function(result_text) {
if (result_text) {
speak(result_text, bundle);
}
});
} else if (trimmed_message[0] === "^") {
user.repeatLine(trimmed_message.slice(1), bundle, function(result_text) {
if (result_text) {
speak(result_text, bundle);
}
});
}
};
bot.addListener("message#", reactToMessage);
bot.addListener("action", reactToMessage);
bot.addListener("message", function(nick, to, text, message) {
if (to === mode_config['bot_name']) {
reactToMessage(nick, nick, text, message);
}
});
bot.addListener("error", function(message) {
console.log('ERROR:', message);
});
bot.addListener("registered", function(messsage) {
console.log('REGISTERED');
});
bot.addListener("motd", function(motd) {
console.log('MOTD:', motd);
});
bot.addListener("topic", function(channel, topic, nick, message) {
console.log('Topic (set by', nick, '):', topic);
});
bot.addListener("join", function(channel, nick, message) {
console.log('JOIN:', nick, 'joined', channel);
});
bot.addListener("part", function(channel, nick, reason, message) {
console.log('PART:', nick, 'left', channel);
});
bot.addListener("quit", function(nick, reason, channels, message) {
console.log('QUIT:', nick, 'quit');
});
bot.addListener("kick", function(channel, nick, by, reason, message) {
console.log('KICK:', nick, 'kicked from', channel, 'by', by);
});
bot.addListener("kill", function(nick, reason, channels, message) {
console.log('KILL:', nick, 'was killed (', reason, ')');
});
bot.addListener("nick", function(oldnick, newnick, channels, message) {
console.log('NICK:', oldnick, 'changed their nickname to', newnick);
});
}
function convert(mode) {
var db = new Database(settings.database_file, mode);
db.convertToSqlite3(mode, function() {
console.log("Finished converting.");
});
}
function main() {
var mode = process.argv[2];
if (mode == "convert") {
if (!(process.argv[3] in settings.modes)) {
throw "Please specify a mode to convert"
}
convert(process.argv[3]);
} else {
if (!(mode in settings.modes)) {
throw "Invalid mode";
} else {
setup(mode);
}
}
}