-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
191 lines (170 loc) · 4.51 KB
/
server.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
const net = require('net');
const { debuglog } = require('util');
const each = require('each-async');
const writer = require('flush-write-stream');
const User = require('./user');
const Channel = require('./channel');
const Message = require('./message');
const commands = require('./commands');
const debug = debuglog('ircs:Server')
/**
* Represents a single IRC server.
*/
class Server extends net.Server {
/**
* Creates a server instance.
*
* @see Server
* @return {Server}
*/
static createServer(options, messageHandler) {
return new Server(options, messageHandler)
}
/**
* Create an IRC server.
*
* @param {Object} options `net.Server` options.
* @param {function()} messageHandler `net.Server` connection listener.
*/
constructor(options = {}, messageHandler) {
super(options)
this.users = [];
this.middleware = [];
this.created = new Date();
this.channels = new Map();
this.hostname = options.hostname || 'localhost';
this.on('connection', sock => {
const user = new User(sock);
this.users.push(user);
this.emit('user', user);
});
this.on('user', user => {
user.pipe(writer.obj((message, enc, cb) => {
this.emit('message', message, user);
cb();
}));
});
this.on('message', message => {
this.execute(message);
debug('message', message + '')
});
for (const command in commands) {
const fn = commands[command];
this.use(command, fn);
}
if (messageHandler) {
this.on('message', messageHandler);
}
debug('server started')
}
/**
* Finds a user by their nickname.
*
* @param {string} nickname Nickname to look for.
*
* @return {User|undefined} Relevant User object if found, `undefined` if not found.
*/
findUser(nickname) {
nickname = normalize(nickname)
return this.users.find(user => normalize(user.nickname) === nickname);
}
/**
* Finds a channel on the server.
*
* @param {string} channelName Channel name.
*
* @return {Channel|undefined} Relevant Channel object if found, `undefined` if not found.
*/
findChannel(channelName) {
return this.channels.get(normalize(channelName))
}
/**
* Creates a new channel with the given name.
*
* @param {string} channelName Channel name.
*
* @return {Channel} The new Channel.
*/
createChannel(channelName) {
channelName = normalize(channelName)
if (!Channel.isValidChannelName(channelName)) {
throw new Error('Invalid channel name')
}
if (!this.channels.has(channelName)) {
this.channels.set(channelName, new Channel(channelName))
}
return this.channels.get(channelName)
}
/**
* Gets a channel by name, creating a new one if it does not yet exist.
*
* @param {string} channelName Channel name.
*
* @return {Channel} The Channel.
*/
getChannel(channelName) {
if (!Channel.isValidChannelName(channelName)) return;
return this.findChannel(channelName) || this.createChannel(channelName);
}
/**
* Checks if there is a channel of a given name.
*
* @param {string} channelName Channel name.
*
* @return {boolean} True if the channel exists, false if not.
*/
hasChannel(channelName) {
return this.channels.has(normalize(channelName))
}
use(command, fn) {
if (!fn) {
[command, fn] = ['', command]
}
debug('register middleware', command)
this.middleware.push({ command, fn })
}
execute(message, cb) {
debug('exec', message + '')
message.server = this
each(this.middleware, (mw, idx, next) => {
if (mw.command === '' || mw.command === message.command) {
debug('executing', mw.command, message.parameters)
if (mw.fn.length < 2) {
mw.fn(message)
next(null)
} else {
mw.fn(message, next)
}
}
}, cb)
}
/**
* Send a message to every user on the server, including the sender.
*
* That sounds dangerous.
*
* @param {Message} message Message to send.
*/
send(message) {
if (!(message instanceof Message)) {
message = new Message(...arguments)
}
this.users.forEach(u => { u.send(message) })
}
/**
* Gives the server mask.
*
* @return {string} Mask.
*/
mask() {
return this.hostname;
}
}
function normalize(str) {
return str.toLowerCase().trim()
// {, } and | are uppercase variants of [, ] and \ respectively
.replace(/{/g, '[')
.replace(/}/g, ']')
.replace(/\|/g, '\\')
}
module.exports = Server;