/
client.js
146 lines (135 loc) · 5.09 KB
/
client.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
'use strict';
const EventEmitter = require('events').EventEmitter,
IRCClient = require('irc').Client,
commands = require('./commands');
const spaces = /[ \f\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+/;
function registerListeners(client, handleit) {
const handlers = {
join: (channel, nick, message) => handleit('join', nick, channel, 'joined channel ' + channel, message),
part: (channel, nick, reason, message) => handleit('part', nick, channel,
'left channel ' + channel + ' (' + reason + ')', message),
quit: (nick, reason, message) => handleit('quit', nick, null, 'quit (' + reason + ')', message),
kick: (channel, nick, by, reason, message) => handleit('kick', by, channel,
'kicked ' + nick + ' (' + reason + ')', message),
kill: (nick, reason, _, message) => handleit('kill', nick, null, 'killed (' + reason + ')', message),
message: (actor, target, text, message) => {
if (target === client.opt.nick) {
return;
}
handleit('message', actor, target, text, message);
},
pm: (actor, text, message) => handleit('pm', actor, client.opt.nick, text, message),
notice: (nick, target, text, message) => handleit('notice', nick, target, text, message),
nick: (oldnick, newnick, _, message) => handleit('nick', oldnick, null,
'changed nickname to ' + newnick, message),
raw: (message) => handleit('raw', null, null, null, message),
error: (message) => handleit('error', null, null, null, message),
action: (nick, target, text, message) => handleit('action', nick, target,
text, message)
};
for (let key of Object.keys(handlers)) {
client.on(key, handlers[key]);
}
}
function selectEvent(type) {
let event = 'unknown';
if (['message', 'pm', 'notice', 'action'].indexOf(type) >= 0) {
event = 'message_received';
} else if (['join', 'part', 'kick'].indexOf(type) >= 0) {
event = 'channel_action';
} else if (['quit', 'nick', 'kill'].indexOf(type) >= 0) {
event = 'user_action';
} else if (['selfMessage', 'selfNotice', 'selfAction'].indexOf(type) >= 0) {
event = 'message_sent';
} else if (['raw', 'error'].indexOf(type) >= 0) {
event = type;
}
return event;
}
function buildEvent(type, who, what, text, raw) {
let command, args = [],
reply = what;
text = text || '';
if (text[0] === '!') {
args = text.split(spaces);
command = args.shift();
}
if (type === 'pm') {
reply = who;
}
return {
type: type,
who: who,
what: what,
reply: reply,
text: text,
raw: raw,
command: command,
args: args
};
}
function getHandler(emitter, client) {
return function handleMessage(type, who, what, text, raw) {
const event = selectEvent(type),
payload = buildEvent(type, who, what, text, raw);
if (event === 'message_received') {
commands.process(client, payload);
}
emitter.emit(event, payload);
};
}
function augmentEvents(events, client) {
events.say = (target, text) => {
const payload = buildEvent('selfMessage', client.opt.nick, target, text, {});
const event = selectEvent('selfMessage');
client.say(target, text);
events.emit(event, payload);
};
events.notice = (target, text) => {
const payload = buildEvent('selfNotice', client.opt.nick, target, text, {});
const event = selectEvent('selfNotice');
client.notice(target, text);
events.emit(event, payload);
};
events.action = (target, action) => {
const payload = buildEvent('selfAction', client.opt.nick, target, action, {});
const event = selectEvent('selfAction');
client.action(target, action);
events.emit(event, payload);
};
events.join = (target) => client.join(target);
events.part = (target, reason) => client.part(target, reason);
}
function connect(config) {
const events = new EventEmitter(),
client = new IRCClient(config.server, config.nick, {
channels: config.channels,
userName: config.nick,
realName: config.nick,
password: config.password,
floodProtection: true,
autoConnect: false
}),
handler = getHandler(events, client);
registerListeners(client, handler);
augmentEvents(events, client);
/* istanbul ignore else */
if (typeof GLOBAL.describe === 'function') {
// expose client when in test mode
events.client = client;
} else {
// do not connect client in test mode.
client.connect();
}
return events;
}
exports.connect = connect;
/* istanbul ignore else */
if (typeof GLOBAL.describe === 'function') {
//test is running
exports.registerListeners = registerListeners;
exports.selectEvent = selectEvent;
exports.buildEvent = buildEvent;
exports.getHandler = getHandler;
exports.augmentEvents = augmentEvents;
}