-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.js
166 lines (136 loc) · 4.16 KB
/
logger.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
var irc = require('irc'),
path = require('path'),
utc = require('./utc'),
fs = require('fs'),
util = require('util');
var ircClients = {};
function getIrcClient(server, nick, auth) {
var hash = (nick + '@' + server).toLowerCase();
if (ircClients.hasOwnProperty(hash)) {
return ircClients[hash];
}
console.log("Creating IRC client for " + hash);
var client = new irc.Client(server, nick, {
channels: []
});
client.setMaxListeners(0);
if (auth) {
if (!auth instanceof Array) {
auth = [auth];
}
client.once('connect', function() {
for (var i = 0; i < auth.length; i++) {
var command = auth[i],
result;
if ((result = command.match(/^\/msg\s+(\S+)\s+(.*)/i))) {
client.say(result[1], result[2]);
} else if (/S/.test(command)) {
throw new Error("Unknown auth command:" + command);
}
}
});
}
ircClients[hash] = client;
return client;
}
exports.start = function(config) {
var server = config.server,
botNick = config.nick,
channel = config.channel,
auth = config.auth ||
(config.authFile && fs.readFileSync(config.authFile, 'utf8').split(/\n/)),
lcChannel = channel.toLowerCase(),
dir = config.dir,
ircClient = getIrcClient(server, botNick, auth);
if (ircClient.connected) {
ircClient.join(channel);
} else {
ircClient.once('connect', function() {
ircClient.join(channel);
});
}
ircClient.on('error', function(error) {
console.log('IRC error: ' + util.inspect(error));
});
function chanflt(channels) {
if (channels instanceof Array) {
// array
for (var i = 0; i < channels.length; i++) {
if (channels[i].toLowerCase() == lcChannel) {
return true;
}
}
return false;
} else {
// string
return channels.toLowerCase() == lcChannel;
}
}
ircClient.on('message', function(nick, channel, message) {
if (!chanflt(channel)) return;
var result = (message + '').match(/^\x01ACTION (.*)\x01$/);
// Filter /me messages
if (!result) {
log('message', { nick: nick, message: message });
} else {
log('action', { nick: nick, action: result[1] });
}
});
ircClient.on('nick', function(nick, new_nick, channel) {
if (!chanflt(channel)) return;
log('nick', { nick: nick, new_nick: new_nick });
});
var start_time = new Date();
ircClient.on('topic', function(channel, topic, nick) {
if (!chanflt(channel)) return;
// Ignore topic changes the first 10 seconds so we don't
// litter our logs with "topic" messages when the bot restarts.
if ((new Date()).getTime() - start_time.getTime() < 10000) return;
log('topic', { nick: nick, topic: topic });
});
ircClient.on('join', function(channel, nick) {
if (!chanflt(channel)) return;
if (nick == botNick) return;
log('join', { nick: nick });
});
ircClient.on('part', function(channel, nick, reason) {
if (!chanflt(channel)) return;
log('part', { nick: nick, reason: reason });
});
ircClient.on('kick', function(channel, victim, nick, reason) {
if (!chanflt(channel)) return;
log('kick', { victim: victim, nick: nick, reason: reason });
});
ircClient.on('quit', function(nick, reason, channel) {
if (!chanflt(channel)) return;
log('quit', { nick: nick, reason: reason });
});
ircClient.on('abort', function() {
// The logger stopped
process.exit();
});
var logStream = null,
lastUtcDate = null;
function log(type, fields) {
var date = new Date(),
utcDate = utc.getDate(date);
// Make sure we're writing to the correct file
if (lastUtcDate != utcDate) {
if (logStream) {
logStream.end();
}
var logFile = path.resolve(dir, utcDate + '.txt');
logStream = fs.createWriteStream(logFile, {
flags: 'a+',
mode: '0666',
encoding: 'utf8'
});
lastUtcDate = utcDate;
}
// Set the type and date
fields.date = date.toISOString();
fields.type = type;
// Write!
logStream.write(JSON.stringify(fields) + '\n');
}
};