<commit_before>// http://www.faqs.org/rfcs/rfc1459.html :)
var sys = require('sys'),
    tcp = require('tcp');
    
process.mixin(GLOBAL, require("./parser_grammar"));

/* ------------------------------ CONSTANTS ------------------------------ */
var DEBUG = true;

/* ------------------------------ MISCELLANEOUS ------------------------------ */
function if_exists(data, no_pad, pad_char) {
  return data ? param(data, no_pad, pad_char) : "";
}

function param(data, no_pad, pad_char) {
  return (no_pad ? "" : (pad_char ? pad_char : " ")) + data.toString();
}

function not_blank(item) {
  return (!!item && item.toString().replace(/\s/g, '') != '');
}

function $w(item) {
  return item.toString().split(/\s+/).filter(not_blank);
}

Function.prototype.bind = (function(){
  var _slice = Array.prototype.slice;
  return function(context) {
    var fn = this,
        args = _slice.call(arguments, 1);
    
    if (args.length) { 
      return function() {
        return arguments.length
          ? fn.apply(context, args.concat(_slice.call(arguments)))
          : fn.apply(context, args);
      }
    } 
    return function() {
      return arguments.length
        ? fn.apply(context, arguments)
        : fn.call(context);
    }; 
  }
})();

/* ------------------------------ IRC Class ------------------------------ */
function IRC(options) {
  // Options
  var internal = {buffer: "", listener_cache: {}, single_listener_cache: {}, cache: {}, locks: {}},
      emitter  = new process.EventEmitter();
  this.options = process.mixin(process.mixin({}, IRC.options), options);
  
  // Client setup
  var socket = tcp.createConnection(this.options.port, this.options.server);
  socket.close(); // XXX It's shitty that I have to do this, but it connects immediately
  socket.setEncoding(this.options.encoding);
  
  // Connect
  socket.addListener("connect", function () {
    var notConnected = true;
    while (notConnected) notConnected = !(socket.readyState == "open");
    
    internal.connected_since = Date.now();
    socket.setTimeout(0);
    
    if (! typeof this.options.pass === "undefined") socket.pass(this.options.pass);
    this.nick(this.options.nick);
    this.user(this.options.user.username, this.options.user.hostname, this.options.user.servername, this.options.user.realname);
    
    emitter.emit("connected");
  }.bind(this));
  
  // Receive data
  socket.addListener("data", parseMessage.bind(this));
  
  // Disconnect
  socket.addListener("end", function () {
    socket.close();
  }.bind(this));
  
  // Parse incoming messages
  function parseMessage(data) {
    internal.last_message = Date.now();
    
    // Apply previous buffer, split, re-buffer
    if (!!internal.buffer) {
      data = internal.buffer + data;
      internal.buffer = "";
    }
    var buffer = data.split(/\r\n/), last;
    if (last = buffer.pop()) internal.buffer = last;
    
    // Parse Messages
    try {
      for (var i = 0; i < buffer.length; i++) {
        var line = buffer[i], message = buildMessage(Parser.parse(line+"\r\n"));
        emitter.emit(message.command.toLowerCase(), message);
      }
    }
    catch (e) {
      if (DEBUG) sys.puts("[ERROR] Parsing '" + line + "'");
    }
  }
  
  // Build message object to be passed to listeners
  function buildMessage(node) {
    var o={}, i, n;
    
    switch (node.type) {
      case "message":
        for (i=0; i<node.children.length; i++) {
          n = buildMessage(node.children[i]);
          o[n[0]] = n[1];
        }
        return o;
      
      case "servername":
        return ["servername", node.value];
      
      case "person":
        var p = {};
        for (i=0; i<node.children.length; i++) p[node.children[i].type] = node.children[i].value;
        return ["person", p];
      
      case "command":
        return ["command", node.value];
      
      case "params":
        var p = [];
        for (i=0; i<node.children.length; i++) p.push(node.children[i].value.toString());
        return ["params", p];
    }
  }
  
  /* ------------------------------ Basic Client ------------------------------ */
  emitter.addListener("ping", function(message){
    this.raw("PONG " + ":" + message.params[0]);
  }.bind(this));<commit_msg>Fire "connected" event only after first server command.<commit_after>// http://www.faqs.org/rfcs/rfc1459.html :)
var sys = require('sys'),
    tcp = require('tcp');
    
process.mixin(GLOBAL, require("./parser_grammar"));

/* ------------------------------ CONSTANTS ------------------------------ */
var DEBUG = true;

/* ------------------------------ MISCELLANEOUS ------------------------------ */
function if_exists(data, no_pad, pad_char) {
  return data ? param(data, no_pad, pad_char) : "";
}

function param(data, no_pad, pad_char) {
  return (no_pad ? "" : (pad_char ? pad_char : " ")) + data.toString();
}

function not_blank(item) {
  return (!!item && item.toString().replace(/\s/g, '') != '');
}

function $w(item) {
  return item.toString().split(/\s+/).filter(not_blank);
}

Function.prototype.bind = (function(){
  var _slice = Array.prototype.slice;
  return function(context) {
    var fn = this,
        args = _slice.call(arguments, 1);
    
    if (args.length) { 
      return function() {
        return arguments.length
          ? fn.apply(context, args.concat(_slice.call(arguments)))
          : fn.apply(context, args);
      }
    } 
    return function() {
      return arguments.length
        ? fn.apply(context, arguments)
        : fn.call(context);
    }; 
  }
})();

/* ------------------------------ IRC Class ------------------------------ */
function IRC(options) {
  // Options
  var internal = {buffer: "", listener_cache: {}, single_listener_cache: {}, cache: {}, locks: {}},
      emitter  = new process.EventEmitter();
  this.options = process.mixin(process.mixin({}, IRC.options), options);
  
  // Client setup
  var socket = tcp.createConnection(this.options.port, this.options.server);
  socket.close(); // XXX It's shitty that I have to do this, but it connects immediately
  socket.setEncoding(this.options.encoding);
  
  // Connect
  socket.addListener("connect", function () {
    var notConnected = true;
    while (notConnected) notConnected = !(socket.readyState == "open");
    
    internal.connected_since = Date.now();
    socket.setTimeout(0);
    
    if (! typeof this.options.pass === "undefined") socket.pass(this.options.pass);
    this.nick(this.options.nick);
    this.user(this.options.user.username, this.options.user.hostname, this.options.user.servername, this.options.user.realname);
    
  }.bind(this));
  
  // Receive data
  socket.addListener("data", parseMessage.bind(this));
  
  // Disconnect
  socket.addListener("end", function () {
    socket.close();
  }.bind(this));
  
  // Parse incoming messages
  function parseMessage(data) {
    internal.last_message = Date.now();
    
    // Apply previous buffer, split, re-buffer
    if (!!internal.buffer) {
      data = internal.buffer + data;
      internal.buffer = "";
    }
    var buffer = data.split(/\r\n/), last;
    if (last = buffer.pop()) internal.buffer = last;
    
    // Parse Messages
    try {
      for (var i = 0; i < buffer.length; i++) {
        var line = buffer[i], message = buildMessage(Parser.parse(line+"\r\n"));
        emitter.emit(message.command.toLowerCase(), message);
      }
    }
    catch (e) {
      if (DEBUG) sys.puts("[ERROR] Parsing '" + line + "'");
    }
  }
  
  // Build message object to be passed to listeners
  function buildMessage(node) {
    var o={}, i, n;
    
    switch (node.type) {
      case "message":
        for (i=0; i<node.children.length; i++) {
          n = buildMessage(node.children[i]);
          o[n[0]] = n[1];
        }
        return o;
      
      case "servername":
        return ["servername", node.value];
      
      case "person":
        var p = {};
        for (i=0; i<node.children.length; i++) p[node.children[i].type] = node.children[i].value;
        return ["person", p];
      
      case "command":
		if (node.value == "001") emitter.emit("connected");
        return ["command", node.value];
      
      case "params":
        var p = [];
        for (i=0; i<node.children.length; i++) p.push(node.children[i].value.toString());
        return ["params", p];
    }
  }
  
  /* ------------------------------ Basic Client ------------------------------ */
  emitter.addListener("ping", function(message){
    this.raw("PONG " + ":" + message.params[0]);
  }.bind(this));
{'fraction_non_alphanumeric': 0.21700879765395895, 'fraction_numerical': 0.003401759530791789, 'mean_word_length': 2.1742367833209233, 'pattern_counts': {'":': 12, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3977579', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2590, 'n_words': 543}