<commit_before>module UniversalCrm
  module Concerns
    module InboundMail
      extend ActiveSupport::Concern
      
      included do
        protect_from_forgery except: %w(inbound)
        
        def index
          #list all tickets  
        end

        #we don't have universal scope here, so need to establish it from the to address or sender
        def inbound
          logger.warn "#### Inbound CRM mail received from #{params['From']}"
          logger.info params
          #find the email address we're sending to
          to = params['ToFull'][0]['Email'] if !params['ToFull'].blank?
          cc = params['CcFull'][0]['Email'] if !params['CcFull'].blank?
          bcc = params['BccFull'][0]['Email'] if !params['BccFull'].blank?

          #parse the email, and create a ticket where necessary:
          if !to.blank? and !params['From'].blank?
            logger.warn "To: #{to}"
            ticket=nil
            #find the senders first, there could be multiple across different scope, if the user config is scoped
            if !Universal::Configuration.class_name_user.blank?
              if UniversalCrm::Configuration.user_scoped
                senders = Universal::Configuration.class_name_user.classify.constantize.where(email: params['From'])
              else #user is not scoped - easy
                sender = Universal::Configuration.class_name_user.classify.constantize.find_by(email: params['From'])
              end
            end

            #check if we're sending to an inbound email address
            config = UniversalCrm::Config.find_by(inbound_email_addresses: to.downcase)
            if config.nil?
              #check if we're sending to a particular config/scope
              possible_token = to[0, to.index('@')]
              logger.info "possible_token: #{possible_token}"
              config = UniversalCrm::Config.find_by(token: possible_token)
            end
            if !config.nil?#we are sending to our general scope
              logger.warn "Direct to config"
              #we probably have to create a new customer
              if !sender.nil?
                customer = UniversalCrm::Customer.find_by(subject: sender)
              end
              customer ||= UniversalCrm::Customer.find_or_create_by(scope: config.scope, email: params['From'])
              customer.update(name: params['FromName']) if customer.name.blank?
              ticket = customer.tickets.create  kind: :email,
                                      title: params['Subject'],
                                      content: params['TextBody'].hideQuotedLines,
                                      html_body: params['HtmlBody'].hideQuotedLines,
                                      scope: config.scope,
                                      to_email: to,
                                      from_email: params['From']

              #Send this ticket to the customer now, so they can reply to it
              UniversalCrm::Mailer.new_ticket(config, customer, ticket, false).deliver_now
            else
              #find email addresses that match our config domains
              inbound_domains = UniversalCrm::Config.all.map{|c| c.inbound_domain}
              puts inbound_domains
              if !to.blank? and inbound_domains.include?(to[to.index('@')+1, to.length])
                to = to
              elsif !cc.blank? and inbound_domains.include?(cc[cc.index('@')+1, cc.length])
                to = cc
              elsif !bcc.blank? and inbound_domains.include?(bcc[bcc.index('@')+1, bcc.length])
                to = bcc
              end<commit_msg>dont' send ticket emails to inbound addresses<commit_after>module UniversalCrm
  module Concerns
    module InboundMail
      extend ActiveSupport::Concern
      
      included do
        protect_from_forgery except: %w(inbound)
        
        def index
          #list all tickets  
        end

        #we don't have universal scope here, so need to establish it from the to address or sender
        def inbound
          logger.warn "#### Inbound CRM mail received from #{params['From']}"
          logger.info params
          #find the email address we're sending to
          to = params['ToFull'][0]['Email'] if !params['ToFull'].blank?
          cc = params['CcFull'][0]['Email'] if !params['CcFull'].blank?
          bcc = params['BccFull'][0]['Email'] if !params['BccFull'].blank?

          #parse the email, and create a ticket where necessary:
          if !to.blank? and !params['From'].blank?
            logger.warn "To: #{to}"
            ticket=nil
            #find the senders first, there could be multiple across different scope, if the user config is scoped
            if !Universal::Configuration.class_name_user.blank?
              if UniversalCrm::Configuration.user_scoped
                senders = Universal::Configuration.class_name_user.classify.constantize.where(email: params['From'])
              else #user is not scoped - easy
                sender = Universal::Configuration.class_name_user.classify.constantize.find_by(email: params['From'])
              end
            end

            #check if we're sending to an inbound email address
            config = UniversalCrm::Config.find_by(inbound_email_addresses: to.downcase)
            if config.nil?
              #check if we're sending to a particular config/scope
              possible_token = to[0, to.index('@')]
              logger.info "possible_token: #{possible_token}"
              config = UniversalCrm::Config.find_by(token: possible_token)
            end
            if !config.nil?#we are sending to our general scope
              logger.warn "Direct to config"
              #we probably have to create a new customer
              if !sender.nil?
                customer = UniversalCrm::Customer.find_by(subject: sender)
              end
              customer ||= UniversalCrm::Customer.find_or_create_by(scope: config.scope, email: params['From'])
              customer.update(name: params['FromName']) if customer.name.blank?
              ticket = customer.tickets.create  kind: :email,
                                      title: params['Subject'],
                                      content: params['TextBody'].hideQuotedLines,
                                      html_body: params['HtmlBody'].hideQuotedLines,
                                      scope: config.scope,
                                      to_email: to,
                                      from_email: params['From']

              #Send this ticket to the customer now, so they can reply to it
              #If it WASN'T sent to one of our inboud addresses that is:
              if !config.inbound_email_addresses.include?(to)
                UniversalCrm::Mailer.new_ticket(config, customer, ticket, false).deliver_now
              end
            else
              #find email addresses that match our config domains
              inbound_domains = UniversalCrm::Config.all.map{|c| c.inbound_domain}
              puts inbound_domains
              if !to.blank? and inbound_domains.include?(to[to.index('@')+1, to.length])
                to = to
              elsif !cc.blank? and inbound_domains.include?(cc[cc.index('@')+1, cc.length])
                to = cc
              elsif !bcc.blank? and inbound_domains.include?(bcc[bcc.index('@')+1, bcc.length])
                to = bcc
              end
{'fraction_non_alphanumeric': 0.09426614744192292, 'fraction_numerical': 0.0018799516583859273, 'mean_word_length': 1.8405797101449275, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6715944', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1878, 'n_words': 528}