<commit_before>var util        = require( "util" );
var socketio    = require( "socket.io" );
var crypto      = require( "crypto" );
var chatclient  = require( "./chat.client" );

function Server( app, server, prefix, settings, backend, log )
{
  this.log = log;
  this.backend = backend;
  this.version = [ 0, 0, 1 ];
  this.protocolVersion = 0;
  this.debug = settings.debug;
  this.options = {
    tokenBytes: 16
  };
  this.io = socketio.listen( server );
  this.io._owner = this;
  this.io.set( "origins", settings.origin + ":*" );
  this.io.set( "log level", settings.debug ? 3 : 2 );
  this.io.set( "logger", this.log );
  this.io.set( "transports", ["websocket", "htmlfile", "xhr-polling", "jsonp-polling"] );
  this.io.set( "heartbeats", true );
  this.io.set( "destroy upgrade", true );
  this.io.set( "browser client", true );
  this.io.set( "browser client cache", true );
  this.io.set( "close timeout", 60 );
  this.io.set( "heartbeat timeout", 60 );
  this.io.set( "heartbeat interval", 25 );
  this.io.set( "polling duration", 20 );
  if ( !settings.debug )
  {
    this.io.enable( "browser client minification" );
    this.io.enable( "browser client etag" );
    this.io.enable( "browser client gzip" );
  }
  this.io.set( "authorization",
    function( handshake, callback )
    {
      this._owner.onAuthorization.call( this._owner, handshake, callback );
    }
  );
  this.io.on( "connection",
    function( socket )
    {
      this._owner.onConnection.call( this._owner, socket );
    }
  );
}

Server.prototype.preClientAuthed = function( userid )
{
  var clients = this.io.sockets.clients();
  clients.forEach( function( socket )
  {
    socket.get( "client", function( dummy, client )
    {
      if ( client && client.user && client.user.id == userid )
      {
        client.kickOut( "Rejoined from elsewhere" );
      }
    });
  });
};

Server.prototype.onClientAuthed = function( client )
{
  this.sendWhoTo( client );
  this.io.sockets.in( "authed" ).emit( "ngc_join", { user: client.user.toJSON() } );
  client.socket.join( "authed" );
};

Server.prototype.onClientMessage = function( client, message )
{
  this.io.sockets.in( "authed" ).emit( "ngc_msg", { user: client.user.toJSON(), "message": message } );
};

Server.prototype.onClientWhisper = function( sclient, target, message ) 
{ 
  var target_user = null;
  var that = this;
  this.io.sockets.in( "authed" ).clients().forEach(function ( socket ) 
  {
    socket.get( "client", function( dummy, client ) 
    {
      if ( client && client.isVisible && client.user.name === target )
      {
	that.log.debug("found recipient, " + client.user.name);
        target_user = client.user;
        socket.emit( "ngc_whisper", { user: sclient.user.toJSON(), "message": message });
      } 
    });
  });
  if (!!target_user) 
  {
    // message delivered, echo it back
    sclient.socket.emit( "ngc_whisper", { user: target_user.toJSON(), "message": message });
  }
}

Server.prototype.onClientDisconnected = function( client )
{
  if ( client.user )
    this.io.sockets.in( "authed" ).emit( "ngc_leave", { user: client.user.toJSON() } );
};

Server.prototype.broadcastJoin = function( client )
{
  var clients = this.io.sockets.clients();
  clients.forEach( function( socket )
  {
    socket.get( "client", function( dummy, client )
    {
      if ( client && client.isVisible && client.user )
        who.push( client.user.toJSON() );
    });
  });
};

Server.prototype.sendWhoTo = function( sclient )
{
  var who = [];
  var clients = this.io.sockets.clients();
  clients.forEach( function( socket )
  {
    socket.get( "client", function( dummy, client )
    {
      if ( client && client.isVisible && client.user && client.id != sclient.id )
        who.push( client.user.toJSON() );
    });
  });
  sclient.socket.emit( "ngc_who",
  {
    users: who
  });
};<commit_msg>actually call isVisible<commit_after>var util        = require( "util" );
var socketio    = require( "socket.io" );
var crypto      = require( "crypto" );
var chatclient  = require( "./chat.client" );

function Server( app, server, prefix, settings, backend, log )
{
  this.log = log;
  this.backend = backend;
  this.version = [ 0, 0, 1 ];
  this.protocolVersion = 0;
  this.debug = settings.debug;
  this.options = {
    tokenBytes: 16
  };
  this.io = socketio.listen( server );
  this.io._owner = this;
  this.io.set( "origins", settings.origin + ":*" );
  this.io.set( "log level", settings.debug ? 3 : 2 );
  this.io.set( "logger", this.log );
  this.io.set( "transports", ["websocket", "htmlfile", "xhr-polling", "jsonp-polling"] );
  this.io.set( "heartbeats", true );
  this.io.set( "destroy upgrade", true );
  this.io.set( "browser client", true );
  this.io.set( "browser client cache", true );
  this.io.set( "close timeout", 60 );
  this.io.set( "heartbeat timeout", 60 );
  this.io.set( "heartbeat interval", 25 );
  this.io.set( "polling duration", 20 );
  if ( !settings.debug )
  {
    this.io.enable( "browser client minification" );
    this.io.enable( "browser client etag" );
    this.io.enable( "browser client gzip" );
  }
  this.io.set( "authorization",
    function( handshake, callback )
    {
      this._owner.onAuthorization.call( this._owner, handshake, callback );
    }
  );
  this.io.on( "connection",
    function( socket )
    {
      this._owner.onConnection.call( this._owner, socket );
    }
  );
}

Server.prototype.preClientAuthed = function( userid )
{
  var clients = this.io.sockets.clients();
  clients.forEach( function( socket )
  {
    socket.get( "client", function( dummy, client )
    {
      if ( client && client.user && client.user.id == userid )
      {
        client.kickOut( "Rejoined from elsewhere" );
      }
    });
  });
};

Server.prototype.onClientAuthed = function( client )
{
  this.sendWhoTo( client );
  this.io.sockets.in( "authed" ).emit( "ngc_join", { user: client.user.toJSON() } );
  client.socket.join( "authed" );
};

Server.prototype.onClientMessage = function( client, message )
{
  this.io.sockets.in( "authed" ).emit( "ngc_msg", { user: client.user.toJSON(), "message": message } );
};

Server.prototype.onClientWhisper = function( sclient, target, message ) 
{ 
  var target_user = null;
  var that = this;
  this.io.sockets.in( "authed" ).clients().forEach(function ( socket ) 
  {
    socket.get( "client", function( dummy, client ) 
    {
      if ( client && client.isVisible() && client.user.name === target )
      {
	that.log.debug("found recipient, " + client.user.name);
        target_user = client.user;
        socket.emit( "ngc_whisper", { user: sclient.user.toJSON(), "message": message });
      } 
    });
  });
  if (!!target_user) 
  {
    // message delivered, echo it back
    sclient.socket.emit( "ngc_whisper", { user: target_user.toJSON(), "message": message });
  }
}

Server.prototype.onClientDisconnected = function( client )
{
  if ( client.user )
    this.io.sockets.in( "authed" ).emit( "ngc_leave", { user: client.user.toJSON() } );
};

Server.prototype.broadcastJoin = function( client )
{
  var clients = this.io.sockets.clients();
  clients.forEach( function( socket )
  {
    socket.get( "client", function( dummy, client )
    {
      if ( client && client.isVisible() && client.user )
        who.push( client.user.toJSON() );
    });
  });
};

Server.prototype.sendWhoTo = function( sclient )
{
  var who = [];
  var clients = this.io.sockets.clients();
  clients.forEach( function( socket )
  {
    socket.get( "client", function( dummy, client )
    {
      if ( client && client.isVisible() && client.user && client.id != sclient.id )
        who.push( client.user.toJSON() );
    });
  });
  sclient.socket.emit( "ngc_who",
  {
    users: who
  });
};
{'fraction_non_alphanumeric': 0.17283311772315654, 'fraction_numerical': 0.004139715394566624, 'mean_word_length': 3.058267716535433, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5750571', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2638, 'n_words': 562}