Advertisement
Guest User

irc.js

a guest
Jan 21st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var pr = console.log;
  3.  
  4. var net = require('net');
  5. var readline = require('readline');
  6.  
  7. //Simple IRC client
  8.  
  9. function IRC(ip, port, nick, room, debug) {
  10.     this.ip = ip;
  11.     this.port = port;
  12.     this.room = room;
  13.     this.nick = nick;
  14.     this.debug = debug;
  15.     this.maxMsgLen = 250;
  16.     this.loginCmdWait = 5000;
  17.    
  18.     this.prefix = ':'+this.nick+'!~'+'irc.js ';
  19.     this.handlers = {};
  20.    
  21.     this.socket = net.Socket({readable: true, writable:true});
  22.    
  23.     var that = this;
  24.     this.rl = readline.createInterface({
  25.         input: that.socket
  26.     });
  27.    
  28.     this.socket.setEncoding('utf8');
  29.    
  30.     function delayMsg(f) {
  31.       setTimeout(f, that.loginCmdWait);
  32.     }
  33.    
  34.     this.socket.on('connect', function () {
  35.         that.debug && pr('Connected.');
  36.         that.sendMsg('NICK', that.nick, delayMsg(function () {
  37.             that.sendMsg('USER', that.nick+' 0 * :Real name', delayMsg(function () {
  38.                 that.sendMsg('JOIN', ':'+that.room, delayMsg(function () {
  39.                     that.handlers.connect && that.handlers.connect();
  40.                 }));
  41.             }));
  42.         }));
  43.     });
  44.    
  45.     var pingRE = /^PING \:([^\s]+)/;
  46.     var messageRE = new RegExp(':([^\\s]+)\\sPRIVMSG\\s'+this.room+'\\s:(.+)', 'i');
  47.     var nickRE = /^([^\s\!]+)/;
  48.    
  49.     this.rl.on('line', function (line) {
  50.         that.debug && pr('Received:',line);
  51.         var _match;
  52.         if (_match = line.match(pingRE)) {
  53.             that.debug && pr('PING',_match[1]);
  54.             that.sendMsg('PONG',':'+_match[1]);
  55.         } else if (_match = line.match(messageRE)) {
  56.             //that.debug && pr('Matches:',_match);
  57.             var nick = _match[1].match(nickRE)[1];
  58.             var msg = _match[2];
  59.             that.handlers.message && that.handlers.message(nick, msg);
  60.         }
  61.     });
  62.    
  63.     this.socket.connect({host: ip, port:port});
  64. }
  65.  
  66. IRC.prototype.on = function(event, handler) {
  67.     this.handlers[event] = handler;
  68. };
  69.  
  70. IRC.prototype.sendMsg = function(cmdName, data, onDone) {
  71.     var that = this;
  72.     this.socket.write(this.prefix+cmdName+' '+data+'\n', 'utf8', function () {
  73.         that.debug && pr('Sent:',cmdName,data);
  74.         onDone && onDone.apply(that, arguments);
  75.     });
  76. };
  77.  
  78. //Splits messages that are longer than this.maxMsgLen (== 445 by default) bytes when utf encoded
  79. //into multiple msgs
  80. IRC.prototype.send = function(str, onDone) {
  81.    
  82.     var blen = Buffer.byteLength(str, 'utf8');
  83.     var msgs = [];
  84.    
  85.     //Split long messages into chunks, reverse the chunk array so you can send chunks by .pop()
  86.     //until array is empty
  87.     if (blen > this.maxMsgLen) {
  88.         this.debug && pr('msg length:',blen);
  89.         var rightPart = str;
  90.         do {
  91.             msgs.push(rightPart.substr(0, this.maxMsgLen));
  92.             rightPart = rightPart.substr(this.maxMsgLen);
  93.         } while (rightPart.length > this.maxMsgLen)
  94.         rightPart.length && msgs.push(rightPart);
  95.         msgs.reverse();
  96.         this.debug && pr(msgs);
  97.     } else {
  98.         //Short messages are just arrays of one chunk
  99.         msgs = [str];
  100.     }
  101.    
  102.     var that = this;
  103.    
  104.     function send() {
  105.         if (msgs.length) {
  106.             var data = msgs.pop();
  107.             that.sendMsg('PRIVMSG', that.room+' :'+data, send);
  108.         } else {
  109.             onDone && onDone.apply(that, arguments);
  110.         }
  111.     }
  112.    
  113.     send();
  114. };
  115.  
  116. // How to use
  117.  
  118. var ip = '80.65.57.18';
  119. var port = 6667;
  120. var room = '#r9kprog';
  121. var nick = 'testbot891023';
  122.  
  123. var client = new IRC(ip, port, nick, room, true);
  124.  
  125. client.on('message', function(nick, msg) {
  126.     pr('Message from',nick+':',msg);
  127.     if (msg.match(/^[Nn]ode/)) {
  128.         client.send(nick+' told me that "'+msg+'", I remember everything!');
  129.     }
  130. });
  131.  
  132. client.on('connect', function () {
  133.     client.send('Node connected.');
  134. });
  135.  
  136. //module.exports = IRC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement