|
|
@ -18,67 +18,72 @@ server.create = function create() { |
|
|
|
let server = net.createServer((socket) => { |
|
|
|
const user = new User(socket) |
|
|
|
socket.on('data', function (data) { |
|
|
|
let splitted = data.toString("ascii").split("\r\n")[0].split(" ") |
|
|
|
let tokenized = [] |
|
|
|
let lastParam = -1 |
|
|
|
for (let i = 0; i < splitted.length; i++) { |
|
|
|
if (lastParam > 0) { |
|
|
|
tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i] |
|
|
|
} else if (splitted[i].charAt(0) == ":" && i > 0) { |
|
|
|
lastParam = i |
|
|
|
tokenized[lastParam] = splitted[i].slice(1) |
|
|
|
} else { |
|
|
|
tokenized[i] = splitted[i] |
|
|
|
} |
|
|
|
} |
|
|
|
let command = tokenized[0].toUpperCase() |
|
|
|
switch (command) { |
|
|
|
case "PING": |
|
|
|
if (tokenized[1] && tokenized[1] === "irc.example.com") { |
|
|
|
socket.write("PONG irc.example.com\r\n") |
|
|
|
} |
|
|
|
break; |
|
|
|
case "NICK": |
|
|
|
if (tokenized[1]) { |
|
|
|
let nickname = tokenized[1] |
|
|
|
// nick collision test
|
|
|
|
if (!Object.keys(userlist).includes(nickname)) { |
|
|
|
user.setNickname(nickname) |
|
|
|
userlist[user.nickname] = user |
|
|
|
} else { |
|
|
|
socket.write(ERR_NICKNAMEINUSE, " nickname in use") |
|
|
|
} |
|
|
|
data.toString("ascii").split("\r\n").forEach(function (commandline) { |
|
|
|
if (commandline == "") { |
|
|
|
return |
|
|
|
} |
|
|
|
let splitted = commandline.split(" ") |
|
|
|
let tokenized = [] |
|
|
|
let lastParam = -1 |
|
|
|
for (let i = 0; i < splitted.length; i++) { |
|
|
|
if (lastParam > 0) { |
|
|
|
tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i] |
|
|
|
} else if (splitted[i].charAt(0) == ":" && i > 0) { |
|
|
|
lastParam = i |
|
|
|
tokenized[lastParam] = splitted[i].slice(1) |
|
|
|
} else { |
|
|
|
tokenized[i] = splitted[i] |
|
|
|
} |
|
|
|
} |
|
|
|
let command = tokenized[0].toUpperCase() |
|
|
|
switch (command) { |
|
|
|
case "PING": |
|
|
|
if (tokenized[1] && tokenized[1] === "irc.example.com") { |
|
|
|
socket.write("PONG irc.example.com\r\n") |
|
|
|
} |
|
|
|
break; |
|
|
|
case "NICK": |
|
|
|
if (tokenized[1]) { |
|
|
|
let nickname = tokenized[1] |
|
|
|
// nick collision test
|
|
|
|
if (!Object.keys(userlist).includes(nickname)) { |
|
|
|
user.setNickname(nickname) |
|
|
|
userlist[user.nickname] = user |
|
|
|
} else { |
|
|
|
socket.write(ERR_NICKNAMEINUSE, " nickname in use") |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
socket.write(ERR_NONICKNAMEGIVEN, "ERROR: NO NICKNAME PROVIDED") |
|
|
|
} |
|
|
|
} else { |
|
|
|
socket.write(ERR_NONICKNAMEGIVEN, "ERROR: NO NICKNAME PROVIDED") |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
case "USER": |
|
|
|
// ignoring servername and hostname, not useful these days
|
|
|
|
user.register(tokenized[1], tokenized[4]) |
|
|
|
let address = user.getAddress() |
|
|
|
socket.write(`001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}`) |
|
|
|
break; |
|
|
|
case "PRIVMSG": |
|
|
|
let target = userlist[tokenized[1]] |
|
|
|
let message = tokenized[2] |
|
|
|
case "USER": |
|
|
|
// ignoring servername and hostname, not useful these days
|
|
|
|
user.register(tokenized[1], tokenized[4]) |
|
|
|
let address = user.getAddress() |
|
|
|
socket.write(`:irc.example.com 001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}\r\n`, "ascii") |
|
|
|
break; |
|
|
|
case "PRIVMSG": |
|
|
|
let target = userlist[tokenized[1]] |
|
|
|
let message = tokenized[2] |
|
|
|
|
|
|
|
target.sendMsg(user, message) |
|
|
|
break; |
|
|
|
case "QUIT": |
|
|
|
case "QUIT": |
|
|
|
server.closeConnection(user.nickname) |
|
|
|
break; |
|
|
|
case "PASS": |
|
|
|
case "PASS": |
|
|
|
if (tokenized[1]) |
|
|
|
user.setPassword(tokenized[1]) |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
console.error(`Unknown command: ${command}`); |
|
|
|
} |
|
|
|
}); |
|
|
|
}).on('error', (err) => { |
|
|
|
console.error(err); |
|
|
|
}) |
|
|
|