forked from thedjpetersen/subway
/
irclink.js
301 lines (276 loc) · 10.4 KB
/
irclink.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var irc = require('irc');
// constructor
module.exports = function IRCLink(connID, app) {
// are we connected?
this.connected = false;
this.created = new Date();
// establish db models
var User = app.db.models.User;
var Connection = app.db.models.Connection;
var Message = app.db.models.Message;
var PrivateMessage = app.db.models.PM;
var Channel = app.db.models.Channel;
var instance = this;
// find the connection ID in the db and wake it up
Connection.find(connID, function (err, userConn) {
// that's not good, give up
if (err || userConn === null)
return;
Channel.all({where: {conn_id: connID}}, function (err, userChans) {
var channels = [];
for (var i=0; i<userChans.length; i++) {
channels.push(userChans[i].name);
}
if (app.config.debug) {
console.log('Restoring connection '+connID+'...');
}
// make sure this is reasonable
try {
instance.port = parseInt(userConn.port);
if (isNaN(instance.port) || instance.port < 1024 || instance.port > 65535)
instance.port = 6667;
} catch (pErr) {
instance.port = 6667;
}
if (userConn.server_password && userConn.server_password.length > 0)
instance.srvPassword = userConn.server_password;
else
instance.srvPassword = null;
if (userConn.real_name && userConn.real_name.length > 0)
instance.realName = userConn.real_name;
else
instance.realName = userConn.nick;
instance.client = new irc.Client(userConn.hostname, userConn.nick, {
password: instance.srvPassword,
userName: userConn.nick,
realName: instance.realName,
port: instance.port,
autoRejoin: false,
channels: channels,
floodProtection: true,
secure: userConn.ssl.valueOf(),
selfSigned: userConn.selfSigned,
encoding: userConn.encoding,
certExpired: false,
stripColors: userConn.stripColors
});
// add ourself as a connection
app.ircConnectionArray.push(connID);
// send an event to any connected clients
instance.xmitEvent = function(event, dict) {
// dictionary is optional
var dict = (typeof dict === "undefined") ? {} : dict;
// always add connection ID to event payloads
dict['connId'] = connID;
// blast out the event name to the user's room with the payload attached
app.io.sockets.in(userConn.user_id).emit(event, dict);
};
instance.logMessage = function(from, to, msg) {
if (!userConn.temporary.valueOf()) {
Message.create({
conn_id: connID
, by: from
, chan: to
, at: Date.now()
, msg: msg
});
}
};
instance.logPM = function(from, msg) {
if (!userConn.temporary.valueOf()) {
PrivateMessage.create({
conn_id: connID
, by: from
, at: Date.now()
, msg: msg
});
}
};
instance.incrementUnread = function(channel) {
try {
instance.client.chans[channel.toLowerCase()].unread_messages++;
} catch (err) {}
};
instance.incrementMention = function(channel) {
try {
instance.client.chans[channel.toLowerCase()].unread_mentions++;
} catch (err) {}
};
instance.events = {
'connect': ['message'],
'registered': ['message'],
'motd': ['motd'],
'names': ['channel', 'nicks'],
'topic': ['channel', 'topic', 'nick'],
'join': ['channel', 'nick', 'message'],
'part': ['channel', 'nick', 'message'],
'quit': ['nick', 'reason', 'channels', 'message'],
'kick': ['channel', 'nick', 'by', 'reason'],
'kill': ['nick', 'reason', 'channels', 'message'],
'message#': ['from', 'to', 'text'],
'action': ['from', 'to', 'text'],
'notice': ['nick', 'to', 'text'],
'ping': [],
'pm': ['nick', 'text'],
'nick': ['oldNick', 'newNick', 'channels'],
'invite': ['channel', 'from'],
'+mode': ['channel', 'by', 'mode', 'argument'],
'-mode': ['channel', 'by', 'mode', 'argument'],
'whois': ['info'],
'error': ['message'],
'close': []
};
// add a listener on client for the given event & argument names
instance.activateListener = function(event, argNames) {
instance.client.addListener(event, function() {
// associate specified names with callback arguments
var callbackArgs = arguments;
var args = {};
argNames.forEach(function(arg, index) {
args[arg] = callbackArgs[index];
});
// emit events to any connected clients
instance.xmitEvent(event, args);
// special cases
switch (event) {
case 'connect':
instance.connected = true;
// TODO: set timeout for nickserv
// TODO: set timeout for channel join
break;
case 'join':
// create unread/mention count on channel object
var clientChan = instance.client.chans[args.channel.toLowerCase()];
if (clientChan !== undefined) {
clientChan.unread_messages = 0;
clientChan.unread_mentions = 0;
}
// add to db for connection
Channel.count({conn_id: connID, name: args.channel.toLowerCase()}, function (err, count) {
if (count === 0) {
Channel.create({
conn_id: connID
, name: args.channel.toLowerCase()
});
}
});
break;
case 'part':
// remove from db for connection
Channel.findOne({where: {conn_id: connID, name: args.channel.toLowerCase()}}, function (err, chan) {
// boom
if (!err && chan)
chan.destroy();
});
break;
case 'message#':
// increment unread message and mention counts
instance.incrementUnread(args.to);
var re = new RegExp('\\b' + instance.client.nick.toLowerCase() + '\\b', 'g');
if(re.test(args.text.toLowerCase())){
instance.incrementMention(args.to);
}
// log it
instance.logMessage(args.from, args.to, args.text);
break;
case 'action':
// increment unread message and mention counts
instance.incrementUnread(args.to);
var re = new RegExp('\\b' + instance.client.nick.toLowerCase() + '\\b', 'g');
if(re.test(args.text.toLowerCase())){
instance.incrementMention(args.to);
}
// log it
instance.logMessage(args.from, args.to, '\u0001ACTION ' + args.text);
break;
case 'nick':
if (args.oldNick === userConn.nick) {
// TODO: update db model with user's new nick
}
break;
case 'pm':
// log it
instance.logPM(args.nick, args.text);
break;
case 'ping':
// TODO: update last seen timestamp in db
break;
case 'close':
instance.connected = false;
app.ircConnectionArray.splice(app.ircConnectionArray.indexOf(connID), 1);
break;
default:
break;
}
});
};
// setup all node-irc listeners declared above
for (var event in instance.events) {
instance.activateListener(event, instance.events[event]);
}
// Handle requests from user
app.ircbridge.on(connID, function(event, data) {
if (instance.connected) {
switch (event) {
case 'say':
instance.client.say(data.to.toLowerCase(), data.text);
instance.xmitEvent('message#', {to:data.to.toLowerCase(), from: instance.client.nick, text:data.text});
// log it
instance.logMessage(instance.client.nick, data.to.toLowerCase(), data.text);
break;
case 'join':
instance.client.join(data.channel.toLowerCase());
break;
case 'restore':
instance.xmitEvent('restore_connection', {nick: instance.client.nick,
server: instance.client.opt.server, channels: instance.client.chans});
break;
case 'clearunread':
// clear all unreads
for(chan in instance.client.chans){
try {
instance.client.chans[chan].unread_messages = 0;
instance.client.chans[chan].unread_mentions = 0;
} catch (err) {}
}
break;
case 'part':
instance.client.part(data.channel.toLowerCase());
break;
case 'action':
instance.client.action(data.target.toLowerCase(), data.message);
instance.xmitEvent('message#', {to: data.target.toLowerCase(), from: instance.client.nick, text: '\u0001ACTION ' + data.message});
// log it
instance.logMessage(instance.client.nick, data.target.toLowerCase(), '\u0001ACTION ' + data.message);
break;
case 'whois':
instance.client.whois(data.nick.toLowerCase());
break;
case 'topic':
instance.client.send('TOPIC', data.name, data.topic);
break;
case 'command':
instance.client.send(data.command);
break;
case 'nick':
instance.client.send('NICK', data.nick);
break;
case 'disconnect':
if (instance.connected) {
instance.connected = false;
instance.client.disconnect();
// remove ourselves from the IRC connection array
app.ircConnectionArray.splice(app.ircConnectionArray.indexOf(connID), 1);
}
break;
case 'away':
instance.client.send('AWAY', data.text);
break;
default:
break;
}
}
});
});
});
}