This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
teemant-old/server/teemant_irc/parser.js

62 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-09-23 21:38:09 +00:00
// :nickname!username@hostname command arg ume nts :trailing
// or
// :hostname command arg ume nts :trailing
2016-10-01 19:42:17 +00:00
function parseERROR(line) {
let final = {
2016-12-15 21:30:55 +00:00
user: { nickname: '', username: '', hostname: '' },
command: 'ERROR',
message: '',
raw: line.join(' ')
};
let pass1 = line.slice(1).join(' ');
if(pass1.indexOf(':') == 0)
2016-10-01 19:42:17 +00:00
pass1 = pass1.substring(1);
final.message = pass1;
return final;
}
2016-09-23 21:38:09 +00:00
module.exports = function(rawline) {
let final = {
user: {
2016-12-15 21:30:55 +00:00
nickname: '',
username: '',
hostname: ''
2016-09-23 21:38:09 +00:00
},
2016-12-15 21:30:55 +00:00
command: '',
2016-09-23 21:38:09 +00:00
arguments: [],
2016-12-15 21:30:55 +00:00
trailing: '',
2016-09-23 21:38:09 +00:00
raw: rawline
2016-12-15 21:30:55 +00:00
};
2016-09-23 21:38:09 +00:00
2016-12-15 21:30:55 +00:00
let pass1 = (rawline.indexOf(':') == 0 ? rawline.substring(1).split(' ') : rawline.split(' '));
if (pass1[0] === 'ERROR')
2016-10-01 19:42:17 +00:00
return parseERROR(pass1);
2016-09-23 21:38:09 +00:00
2016-12-15 21:30:55 +00:00
if(pass1[0].indexOf('!') != -1) {
2016-09-23 21:38:09 +00:00
let nickuser = pass1[0].split('!');
final.user.nickname = nickuser[0];
let userhost = nickuser[1].split('@');
final.user.username = userhost[0];
final.user.hostname = userhost[1];
} else {
final.user.hostname = pass1[0];
}
final.command = pass1[1];
2016-12-15 21:30:55 +00:00
let pass2 = pass1.slice(2).join(' ');
if(pass2.indexOf(':') != -1) {
final.arguments = pass2.substring(0, pass2.indexOf(' :')).split(' ');
2016-09-23 21:38:09 +00:00
final.trailing = pass2.substring(pass2.indexOf(':')+1);
} else {
2016-12-15 21:30:55 +00:00
final.arguments = pass2.split(' ');
2016-09-23 21:38:09 +00:00
}
return final;
2016-12-15 21:30:55 +00:00
};