<commit_before>'use strict'

const url = require('url')
const xmlrpc = require('xmlrpc')
const uuid = require('uuid').v4

exports.init = loginInit

// SL uses its own tls-certificate
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

function loginInit (server) {
  server.route({
    method: 'POST',
    path: '/login',
    handler: processLogin
  })
}

// Sends a login request as a XML-RPC post to the grid
function processLogin (request, reply) {
  getMacAddress(request)
    .then(function (mac) {
      const reqData = request.payload

      var loginURL
      if (reqData.grid && typeof reqData.grid.url === 'string') {
        loginURL = url.parse(reqData.grid.url)
      } else {
        loginURL = {
          host: 'login.agni.lindenlab.com',
          port: 443,
          path: '/cgi-bin/login.cgi'
        }
      }
      if (!loginURL || loginURL.host == null) {
        reply(400)
        return
      }
      var xmlrpcClient = loginURL.protocol == null || loginURL.protocol === 'https:'
        ? xmlrpc.createSecureClient(loginURL)
        : xmlrpc.createClient(loginURL) // osgrid uses http for login ... why??

      reqData.mac = mac // adding the needed mac-address

      ;[
        'grid',
        'viewerUserId'
      ].forEach(key => {
        reqData[key] = undefined
      })

      xmlrpcClient.methodCall('login_to_simulator', [reqData], (err, data) => {
        if (err) {
          reply(err)
        } else {
          var response = reply(undefined, data)
          response.type('application/json')
        }
      })
    })
    .catch(function (err) {
      reply(err)
    })
}

function getMacAddress (request) {
  // If it is a logged in user
  if ('viewerUserId' in request.payload) {
    return request.server.plugins.account.api.accounts.find(request.payload.viewerUserId, {
      include: 'profile'
    })

      .then(function (user) {
        // test the mac-address
        if ('mac' in user.profile && /(?:[a-fA-F\d]{2}:){5}[a-fA-F\d]{2}/i.test(user.profile.mac)) {
          return user.profile.mac
        } else {
          // Add a mac-address to the user
          return request.server.plugins.account.api.accounts.update(user, function (user) {
            user.profile.mac = generateMacAddress()
            return user
          }, {
            include: 'profile'
          })

            .then(function (user) {
              return user.profile.mac
            })
        }
      })
  } else {
    // new user
    return Promise.resolve(
      generateMacAddressFromIP(request.info.remoteAddress)
    )
  }
}

function generateMacAddress (userId) {
  // generate a UUID and transfrom it into a "MAC"-address
  const num = uuid().replace(/-/g, '').slice(0, 12)

  let mac = ''
  for (let i = 0; i < 6; ++i) {
    const index = i * 2
    const sep = i === 0 ? '' : ':'
    mac += `${sep}${num.charAt(index)}${num.charAt(index + 1)}`
  }
  return mac
}

function generateMacAddressFromIP (ip) {
  const ip4 = ip.split('.')
  if (ip4.length === 4) {
    const hexNum = ip4.map(part => parseInt(part, 10).toString(16).padStart(2, '0'))
    return '00:00:' + hexNum.join(':')
  }

  // is IPv6
  const ip6 = ip.replace(/:/g, '')
  let resultAddress = ''<commit_msg>refactor(login): use an async function<commit_after>'use strict'

const url = require('url')
const xmlrpc = require('xmlrpc')
const uuid = require('uuid').v4

exports.init = loginInit

// SL uses its own tls-certificate
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

function loginInit (server) {
  server.route({
    method: 'POST',
    path: '/login',
    handler: processLogin
  })
}

// Sends a login request as a XML-RPC post to the grid
function processLogin (request, reply) {
  getMacAddress(request)
    .then(function (mac) {
      const reqData = request.payload

      var loginURL
      if (reqData.grid && typeof reqData.grid.url === 'string') {
        loginURL = url.parse(reqData.grid.url)
      } else {
        loginURL = {
          host: 'login.agni.lindenlab.com',
          port: 443,
          path: '/cgi-bin/login.cgi'
        }
      }
      if (!loginURL || loginURL.host == null) {
        reply(400)
        return
      }
      var xmlrpcClient = loginURL.protocol == null || loginURL.protocol === 'https:'
        ? xmlrpc.createSecureClient(loginURL)
        : xmlrpc.createClient(loginURL) // osgrid uses http for login ... why??

      reqData.mac = mac // adding the needed mac-address

      ;[
        'grid',
        'viewerUserId'
      ].forEach(key => {
        reqData[key] = undefined
      })

      xmlrpcClient.methodCall('login_to_simulator', [reqData], (err, data) => {
        if (err) {
          reply(err)
        } else {
          var response = reply(undefined, data)
          response.type('application/json')
        }
      })
    })
    .catch(function (err) {
      reply(err)
    })
}

async function getMacAddress (request) {
  const payload = request.payload

  // If it is a logged in user
  if ('viewerUserId' in payload) {
    const accounts = request.server.plugins.account.api.accounts

    try {
      const user = await accounts.find(payload.viewerUserId, { include: 'profile' })

      // test the mac-address
      if (/(?:[a-fA-F\d]{2}:){5}[a-fA-F\d]{2}/i.test(user.profile.mac)) {
        return user.profile.mac
      } else {
        // Add a mac-address to the user
        const updated = await accounts.update(user, user => {
          user.profile.mac = generateMacAddress()
          return user
        }, {
          include: 'profile'
        })

        return updated.profile.mac
      }
    } catch (err) {
      return generateMacAddressFromIP(request.info.remoteAddress)
    }
  } else {
    // new and anonym user
    return generateMacAddressFromIP(request.info.remoteAddress)
  }
}

function generateMacAddress (userId) {
  // generate a UUID and transfrom it into a "MAC"-address
  const num = uuid().replace(/-/g, '').slice(0, 12)

  let mac = ''
  for (let i = 0; i < 6; ++i) {
    const index = i * 2
    const sep = i === 0 ? '' : ':'
    mac += `${sep}${num.charAt(index)}${num.charAt(index + 1)}`
  }
  return mac
}

function generateMacAddressFromIP (ip) {
  const ip4 = ip.split('.')
  if (ip4.length === 4) {
    const hexNum = ip4.map(part => parseInt(part, 10).toString(16).padStart(2, '0'))
    return '00:00:' + hexNum.join(':')
  }

  // is IPv6
  const ip6 = ip.replace(/:/g, '')
  let resultAddress = ''
{'fraction_non_alphanumeric': 0.14632250038874203, 'fraction_numerical': 0.010884776862074327, 'mean_word_length': 2.227295534370296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '456158', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2069, 'n_words': 485}