123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- module.exports.connected = connected;
- module.exports.processMessage = processMessage;
- module.exports.processSender = processSender;
- module.exports.setHandlers = setHandlers;
- module.exports.setSettings = setSettings;
- var handlers = {
- sendHandler: null,
- findServer: null,
- findUser: null,
- killUser: null,
- newServer: null,
- newUser: null,
- parseUmodes: null,
- getChannel: null
- };
- var settings = {
- ID: null,
- password: null,
- name: null,
- description: null,
- version: null,
- me: null
- };
- var prefixes = {
- '~': 'q',
- '&': 'a',
- '@': 'o',
- '%': 'h',
- '+': 'v'
- };
- function parseModePrefix(text){
- var modes = [];
- for(var i=0; i<text.length; i++){
- var c = text.charAt(i);
- if(c in prefixes){
- modes[prefixes[c]] = true;
- } else break;
- }
- var uid = text.substring(i);
- var user = handlers.findUser(uid);
- if(!user){
- handlers.killUser(uid);
- return null;
- }
- return { 'user': user, 'modes': modes };
- }
- function connected(){
- ircSend(null, settings.ID, 'PASS', [settings.password]);
- ircSend(null, settings.ID, 'PROTOCTL', ['NICKv2', 'VHP', 'UMODE2', 'NICKIP', 'SJOIN', 'SJOIN2', 'SJ3', 'NOQUIT', 'TKLEXT', 'MLOCK', 'SID']);
- ircSend(null, settings.ID, 'PROTOCTL', ['EAUTH=' + settings.name + ',,,' + settings.version]);
- ircSend(null, settings.ID, 'PROTOCTL', ['SID=' + settings.ID]);
- ircSend(null, settings.ID, 'SERVER', [settings.name, '1', settings.description]);
- }
- /*
- this.sender = {
- 'nick': '',
- 'ident': '',
- 'host': '',
- 'server': false,
- 'user': false
- };
- */
- function processSender(sender){
- if(sender.nick){
- if(server = handlers.findServer(sender.nick)){
- sender.nick = server.name;
- sender.server = server;
- } else if(user = handlers.findUser(sender.nick)){
- sender.nick = user.name
- sender.ident = user.ident;
- sender.host = user.host;
- sender.user = user;
- } else {
- handlers.killUser(sender.nick);
- return false;
- }
- }
- return sender;
- }
- function processMessage(msg){
- if(!msg) return;
- if(msg.command in cmdBinds){
- cmdBinds[msg.command](msg);
- } else {
- console.log('Unhandled cmd: '+msg.command);
- console.log(msg);
- }
- }
- function setHandlers(newHandlers){
- handlers = newHandlers;
- }
- function setSettings(newSettings){
- settings = newSettings;
- }
- function ircSend(tags, from, cmd, args){
- handlers.send(tags, from, cmd, args);
- }
- var cmdBinds = {
- 'PROTOCTL': function(msg){
- console.log(msg);
- },
-
- 'SERVER': function(msg){
- var expr = /^([^-]+)-([^-]+)-([^ ]+) (.*)$/;
- var match = expr.exec(msg.args[2]);
- if(match){
- handlers.newServer(msg.args[0], match[3], match[4], msg.args[1], settings.me);
- } else {
- throw 'Unknown SERVER message';
- }
- },
-
- 'MD': function(msg){
- },
-
- 'SMOD': function(msg){
- },
-
- 'EOS': function(msg){
- },
-
- 'SINFO': function(msg){
- console.log(msg);
- },
- 'SID': function(msg){
- handlers.newServer(msg.args[0], msg.args[2], msg.args[3], msg.args[1], msg.sender);
- },
- /*
- messagedata {
- text: 'James Wheare',
- args: [
- 'jwheare', // nick 0
- '0', // hopcount 1
- '1583326466', // TS 2
- 'sid2', // ident 3
- '192.184.10.9', // host 4
- '0932YWXX0', // uid 5
- '0', // account 6
- '+T', // umodes 7
- '192.184.10.9', // vhost 8
- '142AA462.921FA108.2DA10B49.IP', // cloakedHost 9
- 'wLgKCQ==', // ip 10
- 'James Wheare' // realname 11
- ],
- tags: [],
- command: 'UID',
- sender: {
- nick: 'test1.pirc.pl',
- ident: '',
- host: '',
- server: true,
- user: false
- },
- time: 2020-03-09T08:33:46.447Z,
- reply: [Function],
- originalString: ':093 UID jwheare 0 1583326466 sid2 192.184.10.9 0932YWXX0 0 +T 192.184.10.9 142AA462.921FA108.2DA10B49.IP wLgKCQ== :James Wheare'
- }
- */
- 'UID': function(msg){ // nick, distance, TS, ident, host, uid, account, umodes, vhost, cloakedHost, ip, realname, uplink
- handlers.newUser(msg.args[0], msg.args[1], msg.args[2], msg.args[3], msg.args[4], msg.args[5], msg.args[6], handlers.parseUmodes(msg.args[7]), msg.args[8], msg.args[9], msg.args[10], msg.args[11], msg.sender.server);
- },
- /*
- >>> :143 SJOIN 1580651090 #help +nt :0936CITMS 0931Y7O8W
- messagedata {
- text: '0936CITMS 0931Y7O8W',
- args: [ '1580651090', '#help', '+nt', '0936CITMS 0931Y7O8W' ],
- tags: [],
- command: 'SJOIN',
- sender: {
- nick: 'test3.pirc.pl',
- ident: '',
- host: '',
- server: server {
- name: 'test3.pirc.pl',
- sid: '143',
- description: 'serwer testowy!',
- distance: '1',
- uplink: [server],
- introduce: [Function]
- },
- user: false
- },
- time: 2020-03-09T10:05:44.305Z,
- reply: [Function],
- originalString: ':143 SJOIN 1580651090 #help +nt :0936CITMS 0931Y7O8W'
- }
- >>> :143 SJOIN 1582647660 #jwheare +nt :@0936CITMS
- messagedata {
- text: '@0936CITMS',
- args: [ '1582647660', '#jwheare', '+nt', '@0936CITMS' ],
- tags: [],
- command: 'SJOIN',
- sender: {
- nick: 'test3.pirc.pl',
- ident: '',
- host: '',
- server: server {
- name: 'test3.pirc.pl',
- sid: '143',
- description: 'serwer testowy!',
- distance: '1',
- uplink: [server],
- introduce: [Function]
- },
- user: false
- },
- time: 2020-03-09T10:05:44.306Z,
- reply: [Function],
- originalString: ':143 SJOIN 1582647660 #jwheare +nt :@0936CITMS'
- }
- */
- 'SJOIN': function(msg){
- var channel = handlers.getChannel(msg.args[1]);
- channel.setTS(msg.args[0]);
- var modeArgs = [];
- for(var i=3; i<msg.args.length - 1; i++){
- modeArgs.push(msg.args[i]);
- }
- channel.addModes(msg.args[2], modeArgs);
- var members = msg.text.split(' ');
- for(var i=0; i<members.length; i++){
- var member = members[i];
- var c = member.charAt(0);
- if(c == '&'){ // +b
- channel.addBan(member.substring(1));
- } else if(c == '"'){ // +e
- channel.addExcept(member.substring(1));
- } else if(c == '\''){ // +I
- channel.addInvex(member.substring(1));
- } else {
- var udata = parseModePrefix(member);
- if(!udata) return;
- console.log(udata.user.name + ' joined ' + msg.args[1]);
- channel.uJoin(udata.user);
- channel.setStatusModes(udata.user, udata.modes);
- }
- }
- console.log(channel);
- },
-
- 'SWHOIS': function(msg){
- },
-
- 'TKL': function(msg){
- },
-
- 'METADATA': function(msg){
- },
-
- 'NETINFO': function(msg){
- console.log(msg);
- },
-
- 'SASL': function(msg){
- },
-
- 'PING': function(msg){
- msg.reply(null, 'PONG', msg.args);
- }
- };
|