0
 ?    ‹      Õ–AoÚ0Çïýž»%� ['šxêÒÐ( Ê4MÓŽí‹$ŽlCÊ>ý�•M n‡Uåòâ<ÿcÿÞ?–íðU<éÏ¿Nèãün„¦Ÿ?Œ†}„=€/�>@<�›Ž®ß
 cŒpfmÕ¨ëÚ¯;¾Ò˜Ï ³EÞcµdÖç–crnsä
…™ Ü=Qh¥Í–\< •"P+k$íC¸*QòTæÂç¢P~f|¦Tå&àŠÏ˜ü vÓmp±–QA§×q©Uâ²V Ö¢´Á¶_2ŒV4‘¹´R˜‡{¶0Q|³E^ž£ØÙE“å¶©Ih3´¦¹\”¶ªÂ$”ÅÍ"’©Ò@’Óré/dŠÍm„¿û“ïN6Û~MBŠ2-Ò¿ïGã›Ic2¦…�žÖÜ9Í-&#j,*—©ü¬ø¾ßËçŒM,Ó²²R•�Rp%—ÊTn*êj}ã*Èô±ª‘ð§Ü ì73¦·³Áxg�%|;Ä#ÝsþlL¦T»Š¥Ì*½ÙÛÐ0½.SÝ^Ñ¾H-™Å!�Tþƒ3«rYªº<6ÇÒšL\8üƒ¨Ýj¿õ‚Àë´PpÝë¾CèŒÐm!Ÿž]¬1ëOSLÒôIð¶×j£Öu¯Ýz)àK±ÁÄ…ô<îÄÊmo—äza&.\ º9&.\à‚1¥[0.\ ë¦väõÿÿËÃö×œvw°škãO ¯«~™
    D   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þBGYÇÌc!B6ÜgùÅƒtN‡Ä_�uÐb&=LyZËcõÄÃ g    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class Cti::Driver::Base

  def initialize(params = {})
    @config = params[:config] || config
    @params = mapping(params[:params])
  end

  def mapping(params)
    params
  end

  def config
    {}
  end

  def process

    # validate directions
    result = direction_check
    return result if result.present?

    # reject inbound call
    result = reject_check
    if result.present? && result[:action] == 'reject'
      @params['comment'] = 'reject, busy'
      if @params['user'].present?
        @params['comment'] = "#{@params['user']} -> reject, busy"
      end
      Cti::Log.process(@params)
      return result
    end

    # set caller ID of outbound call
    result = caller_id_rewrite(@params)
    if result.present? && result[:action] == 'set_caller_id'
      @params['from'] = result[:params][:from_caller_id]
      Cti::Log.process(@params)
      return result
    end

    log = Cti::Log.process(@params)
    if log.present?

      # push new call notification
      push_incoming_call(log)

      # open screen if call got answered
      push_open_ticket_screen(log)
      push_pickup_target(log)
    end

    result || {}
  end

  def direction_check

    # check possible directions
    if @params['direction'] != 'in' && @params['direction'] != 'out'
      return {
        action: 'invalid_direction',
        params: @params
      }
    end

    nil
  end

  def reject_check
    return nil if @params['direction'] != 'in'
    return nil if @params['event'] != 'newCall'

    config_inbound = @config[:inbound] || {}
    block_caller_ids = config_inbound[:block_caller_ids] || []

    # check if call needs to be blocked
    block_caller_ids.each do |item|
      next if item[:caller_id] != @params['from']

      return {
        action: 'reject'
      }
    end
    nil
  end

  def caller_id_rewrite(params)
    return nil if params['direction'] != 'out'
    return nil if params['event'] != 'newCall'

    config_outbound = @config[:outbound]
    routing_table = nil
    default_caller_id = nil
    if config_outbound.present?
      routing_table = config_outbound[:routing_table]
      default_caller_id = config_outbound[:default_caller_id]
    end

    to = params[:to]
    return nil if to.blank?

    if routing_table.present?
      routing_table.each do |row|
        dest = row[:dest].gsub(%r{\*}, '.+?')
        next if !to.match?(%r{^#{dest}$})

        return {
          action: 'set_caller_id',
          params: {
            from_caller_id: row[:caller_id],
            to_caller_id:   params[:to],
          }
        }
      end
    end

    if default_caller_id.present?
      return {
        action: 'set_caller_id',
        params: {
          from_caller_id: default_caller_id,
          to_caller_id:   params[:to],
        }
      }
    end

    nil
  end

  def push_open_ticket_screen(log)
    user = answering_agent(log)
    return if !user

    customer_id = log.best_customer_id_of_log_entry

    # open user profile if user has a ticket in the last 30 days
    if customer_id
      last_activity = Setting.get('cti_customer_last_activity')
      if Ticket.where(customer_id: customer_id).exists?(['updated_at > ?', last_activity.seconds.ago])
        PushMessages.send_to(user.id, {
                               event: 'remote_task',
                               data:  {
          