<commit_before>var moment = require('moment');
var whoAmI = require('./whoAmI');
var config = require('./../config');

var devices = require('./database').devices;

var clearCache = require('./clearCache');

function respond(uuid, callback, sendToken){
  whoAmI(uuid, true, function(check){
    var result = {uuid: uuid};
    if(!check.error){
      result.status = 201;
      result.device = check;
      if(sendToken){
        result.token = check.token;
      }
      callback(result);
    }else{
      result.status = 401;
      callback(result);
    }

  });
}

module.exports = function(socket, callback) {

  var newTimestamp = new Date().getTime();
  if(socket.uuid && socket.token){
    clearCache('DEVICE_'+socket.uuid);

    if (socket.ipAddress){
      try {
        var geoip = require('geoip-lite');        
        var geo = geoip.lookup(socket.ipAddress);
      } catch(geoip){
        var geo = null;
      }
    } else {
      var geo = null;
    }

    devices.update({
      uuid: socket.uuid, token: socket.token
    }, {
      $set: {socketid: socket.socketid, online: true, timestamp: moment(newTimestamp).toISOString(), ipAddress: socket.ipAddress, geo: geo, protocol: socket.protocol, secure: socket.secure}
    }, function(err, saved) {

      if(err || saved === 0 || (saved && config.mongo && config.mongo.databaseUrl && !saved.updatedExisting)) {
        console.log('Error: ' + err);
        callback({uuid: socket.uuid, status: 401});
      } else {
        respond(socket.uuid, callback);
      }
    });

  } else {
<commit_msg>suppressed mqtt ip address write on authenticate<commit_after>var moment = require('moment');
var whoAmI = require('./whoAmI');
var config = require('./../config');

var devices = require('./database').devices;

var clearCache = require('./clearCache');

function respond(uuid, callback, sendToken){
  whoAmI(uuid, true, function(check){
    var result = {uuid: uuid};
    if(!check.error){
      result.status = 201;
      result.device = check;
      if(sendToken){
        result.token = check.token;
      }
      callback(result);
    }else{
      result.status = 401;
      callback(result);
    }

  });
}

module.exports = function(socket, callback) {

  var newTimestamp = new Date().getTime();
  if(socket.uuid && socket.token){
    clearCache('DEVICE_'+socket.uuid);

    if (socket.ipAddress){
      try {
        var geoip = require('geoip-lite');        
        var geo = geoip.lookup(socket.ipAddress);
      } catch(geoip){
        var geo = null;
      }
      var updatedData = {socketid: socket.socketid, online: true, timestamp: moment(newTimestamp).toISOString(), ipAddress: socket.ipAddress, geo: geo, protocol: socket.protocol, secure: socket.secure}
    } else {
      var geo = null;
      var updatedData = {socketid: socket.socketid, online: true, timestamp: moment(newTimestamp).toISOString(), geo: geo, protocol: socket.protocol, secure: socket.secure}
    }

    devices.update({
      uuid: socket.uuid, token: socket.token
    }, {
      $set: updatedData
    }, function(err, saved) {

      if(err || saved === 0 || (saved && config.mongo && config.mongo.databaseUrl && !saved.updatedExisting)) {
        console.log('Error: ' + err);
        callback({uuid: socket.uuid, status: 401});
      } else {
        respond(socket.uuid, callback);
      }
    });

  } else {

{'fraction_non_alphanumeric': 0.15478312537136066, 'fraction_numerical': 0.0059417706476530005, 'mean_word_length': 2.8002257336343117, 'pattern_counts': {'":': 0, '<': 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': '1739431', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1002, 'n_words': 233}