<commit_before>class ConfigSnomController < ApplicationController
  MAX_SIP_ACCOUNTS_COUNT = 11
  MAX_SOFTKEYS_COUNT = 12 + (42 * 3) - 1
  MAX_DIRECTORY_ENTRIES = 20
  KEY_REGEXP = {
    '0' => "[ -.,_0]+",
    '1' => "[ -.,_1]+",
    '2' => "[abc2\xC3\xA4]",
    '3' => "[def3\xC3\xA9]",
    '4' => "[ghi4\xC3\xAF]",
    '5' => "[jkl5]",
    '6' => "[mno6\xC3\xB6]",
    '7' => "[pqrs7\xC3\x9F]",
    '8' => "[tuv8\xC3\xBC]",
    '9' => "[wxyz9]",
  }

  SNOM_PHONE_DEFAULTS = {
    'Snom 300' => {
      :softkeys_physical => 6,
      :softkeys_logical => 6,
    },
      'Snom 320' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 360' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 360' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 370' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 820' => {
      :softkeys_physical => 4,
      :softkeys_logical => 16,
    },
      'Snom 821' => {
      :softkeys_physical => 4,
      :softkeys_logical => 12,
    },
    'Snom 870' => {
      :softkeys_physical => 0,
      :softkeys_logical => 16,
    },
    'Snom vision' => {
      :softkeys_physical => 16,
      :softkeys_logical => 48,
      :pages => 3,
    },
  }

  skip_authorization_check

  before_filter { |controller|
    @mac_address = params[:mac_address].to_s.upcase.gsub(/[^0-9A-F]/,'')
    @provisioning_authenticated = false
    if !params[:phone].blank?
      @phone = Phone.where({ :id => params[:phone].to_i }).first
      if !params[:sip_account].blank?
        @sip_account = @phone.sip_accounts.where({ :id => params[:sip_account].to_i }).first  || 
        if !@sip_account && @phone.fallback_sip_account && @phone.fallback_sip_account.id == params[:sip_account].to_i
          @sip_account = @phone.fallback_sip_account
        end
      end
    end

    @type = params[:type].blank? ? nil : params[:type].to_s.strip.downcase
    @dialpad_keys = params[:keys].blank? ? nil : params[:keys].to_s.strip
  }

  def show
    if @mac_address.blank?
      render(
        :status => 404,
        :layout => false,
        :content_type => 'text/plain',
        :text => "<!-- Phone not found -->",
      )
      return
    end

    mac_address_to_model = {
      '00041325' => 'Snom 300',
      '00041328' => 'Snom 300',
      '0004132D' => 'Snom 300',
      '0004132F' => 'Snom 300',
      '00041334' => 'Snom 300',
      '00041350' => 'Snom 300',
      '0004133B' => 'Snom 300',
      '00041337' => 'Snom 300',
      '00041324' => 'Snom 320',
      '00041327' => 'Snom 320',
      '0004132C' => 'Snom 320',
      '00041331' => 'Snom 320',
      '00041335' => 'Snom 320',
      '00041338' => 'Snom 320',
      '00041351' => 'Snom 320',
      '00041323' => 'Snom 360',
      '00041329' => 'Snom 360',
      '0004132B' => 'Snom 360',
      '00041339' => 'Snom 360',
      '00041390' => 'Snom 360',
      '00041326' => 'Snom 370',
      '0004132E' => 'Snom 370',
      '0004133A' => 'Snom 370',
      '00041352' => 'Snom 370',
      '00041340' => 'Snom 820',
      '00041345' => 'Snom 821',
      '00041348' => 'Snom 821',
      '00041341' => 'Snom 870',
      '00041332' => 'Snom meetingPoint',
      '00041343' => 'Snom vision',
    }

    phone_model_str = mac_address_to_model[@mac_address[0, 8]]
    if phone_model_str == 'Snom vision'
      snom_vision
    elsif !phone_model_str.blank?
      snom_phone
    end
  end
	
  def snom_phone
    if !params[:provisioning_key].blank?
      @phone = Phone.where({ :provisioning_key => params[:provisioning_key] }).first
      if @phone
        @provisioning_authenticated = true
        @mac_address = @phone.mac_address
      end
    end

    if ! @mac_address.blank? then
      if !@phone
        @phone = Phone.where({ :mac_address => @mac_address }).first
      end

      if ! @phone && GsParameter.get('PROVISIONING_AUTO_ADD_PHONE')
        tenant = Tenant.where(:id => GsParameter.get('PROVISIONING_AUTO_TENANT_ID')).first
        if ! tenant
          render(
            :status => 404,
            :layout => false,
            :content_type => 'text/plain',
            :text => "<!-- Tenant not found -->",
          )
          return
        end

        @phone = tenant.phones.build
        @phone.mac_address = @mac_address
        @phone.hot_deskable = true
        @phone.tenant = tenant

        mac_address_to_model = {
          '00041325' => 'Snom 300',
          '00041328' => 'Snom 300',
          '0004132D' => 'Snom 300',
          '0004132F' => 'Snom 300',
          '00041334' => 'Snom 300',
          '00041350' => 'Snom 300',
          '0004133B' => 'Snom 300',
          '00041337' => 'Snom 300',
          '00041324' => 'Snom 320',
          '00041327' => 'Snom 320',
          '0004132C' => 'Snom 320',
          '00041331' => 'Snom 320',
          '00041335' => 'Snom 320',
          '00041338' => 'Snom 320',
          '00041351' => 'Snom 320',
          '00041323' => 'Snom 360',
          '00041329' => 'Snom 360',
          '0004132B' => 'Snom 360',
          '00041339' => 'Snom 360',
          '00041390' => 'Snom 360',
          '00041326' => 'Snom 370',
          '0004132E' => 'Snom 370',
          '0004133A' => 'Snom 370',
          '00041352' => 'Snom 370',
          '00041340' => 'Snom 820',
          '00041345' => 'Snom 821',
          '00041348' => 'Snom 821',
          '00041341' => 'Snom 870',
          '00041332' => 'Snom meetingPoint',
        }

        @phone.phone_model = PhoneModel.where(:name => mac_address_to_model[@mac_address[0, 8]]).first
        if ! @phone.save
          render(
            :status => 500,
            :layout => false,
            :content_type => 'text/plain',
            :text => "<!-- #{@phone.errors.messages.inspect} -->",
          )
          return
        end

        if ! GsParameter.get('PROVISIONING_AUTO_ADD_SIP_ACCOUNT')
          return
        end

        caller_name_index = 0<commit_msg>Snom 320 - MAC range added<commit_after>class ConfigSnomController < ApplicationController
  MAX_SIP_ACCOUNTS_COUNT = 11
  MAX_SOFTKEYS_COUNT = 12 + (42 * 3) - 1
  MAX_DIRECTORY_ENTRIES = 20
  KEY_REGEXP = {
    '0' => "[ -.,_0]+",
    '1' => "[ -.,_1]+",
    '2' => "[abc2\xC3\xA4]",
    '3' => "[def3\xC3\xA9]",
    '4' => "[ghi4\xC3\xAF]",
    '5' => "[jkl5]",
    '6' => "[mno6\xC3\xB6]",
    '7' => "[pqrs7\xC3\x9F]",
    '8' => "[tuv8\xC3\xBC]",
    '9' => "[wxyz9]",
  }

  SNOM_PHONE_DEFAULTS = {
    'Snom 300' => {
      :softkeys_physical => 6,
      :softkeys_logical => 6,
    },
      'Snom 320' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 360' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 360' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 370' => {
      :softkeys_physical => 12,
      :softkeys_logical => 12,
    },
      'Snom 820' => {
      :softkeys_physical => 4,
      :softkeys_logical => 16,
    },
      'Snom 821' => {
      :softkeys_physical => 4,
      :softkeys_logical => 12,
    },
    'Snom 870' => {
      :softkeys_physical => 0,
      :softkeys_logical => 16,
    },
    'Snom vision' => {
      :softkeys_physical => 16,
      :softkeys_logical => 48,
      :pages => 3,
    },
  }

  MAC_ADDRESS_TO_MODEL = {
    '00041325' => 'Snom 300',
    '00041328' => 'Snom 300',
    '0004132D' => 'Snom 300',
    '0004132F' => 'Snom 300',
    '00041334' => 'Snom 300',
    '00041350' => 'Snom 300',
    '0004133B' => 'Snom 300',
    '00041337' => 'Snom 300',
    '00041324' => 'Snom 320',
    '00041327' => 'Snom 320',
    '0004132C' => 'Snom 320',
    '00041331' => 'Snom 320',
    '00041335' => 'Snom 320',
    '00041338' => 'Snom 320',
    '00041351' => 'Snom 320',
    '0004133F' => 'Snom 320',
    '00041323' => 'Snom 360',
    '00041329' => 'Snom 360',
    '0004132B' => 'Snom 360',
    '00041339' => 'Snom 360',
    '00041390' => 'Snom 360',
    '00041326' => 'Snom 370',
    '0004132E' => 'Snom 370',
    '0004133A' => 'Snom 370',
    '00041352' => 'Snom 370',
    '00041340' => 'Snom 820',
    '00041345' => 'Snom 821',
    '00041348' => 'Snom 821',
    '00041341' => 'Snom 870',
    '00041332' => 'Snom meetingPoint',
    '00041343' => 'Snom vision',
  }

  skip_authorization_check

  before_filter { |controller|
    @mac_address = params[:mac_address].to_s.upcase.gsub(/[^0-9A-F]/,'')
    @provisioning_authenticated = false
    if !params[:phone].blank?
      @phone = Phone.where({ :id => params[:phone].to_i }).first
      if !params[:sip_account].blank?
        @sip_account = @phone.sip_accounts.where({ :id => params[:sip_account].to_i }).first  || 
        if !@sip_account && @phone.fallback_sip_account && @phone.fallback_sip_account.id == params[:sip_account].to_i
          @sip_account = @phone.fallback_sip_account
        end
      end
    end

    @type = params[:type].blank? ? nil : params[:type].to_s.strip.downcase
    @dialpad_keys = params[:keys].blank? ? nil : params[:keys].to_s.strip
  }

  def show
    if @mac_address.blank?
      render(
        :status => 404,
        :layout => false,
        :content_type => 'text/plain',
        :text => "<!-- Phone not found -->",
      )
      return
    end

    

    phone_model_str = MAC_ADDRESS_TO_MODEL[@mac_address[0, 8]]
    if phone_model_str == 'Snom vision'
      snom_vision
    elsif !phone_model_str.blank?
      snom_phone
    end
  end
	
  def snom_phone
    if !params[:provisioning_key].blank?
      @phone = Phone.where({ :provisioning_key => params[:provisioning_key] }).first
      if @phone
        @provisioning_authenticated = true
        @mac_address = @phone.mac_address
      end
    end

    if ! @mac_address.blank? then
      if !@phone
        @phone = Phone.where({ :mac_address => @mac_address }).first
      end

      if ! @phone && GsParameter.get('PROVISIONING_AUTO_ADD_PHONE')
        tenant = Tenant.where(:id => GsParameter.get('PROVISIONING_AUTO_TENANT_ID')).first
        if ! tenant
          render(
            :status => 404,
            :layout => false,
            :content_type => 'text/plain',
            :text => "<!-- Tenant not found -->",
          )
          return
        end

        @phone = tenant.phones.build
        @phone.mac_address = @mac_address
        @phone.hot_deskable = true
        @phone.tenant = tenant

        @phone.phone_model = PhoneModel.where(:name => MAC_ADDRESS_TO_MODEL[@mac_address[0, 8]]).first
        if ! @phone.save
          render(
            :status => 500,
            :layout => false,
            :content_type => 'text/plain',
            :text => "<!-- #{@phone.errors.messages.inspect} -->",
          )
          return
        end

        if ! GsParameter.get('PROVISIONING_AUTO_ADD_SIP_ACCOUNT')
          return
        end

        caller_name_index = 0
{'fraction_non_alphanumeric': 0.17051282051282052, 'fraction_numerical': 0.10805860805860806, 'mean_word_length': 2.1256439610761304, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 211, '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': '964002', 'n_tokens_mistral': 4881, 'n_tokens_neox': 3979, 'n_words': 754}