<commit_before>module Formageddon
  class FormageddonContactStep < ActiveRecord::Base
    belongs_to :formageddon_recipient, :polymorphic => true
    has_one :formageddon_form, :dependent => :destroy
    
    accepts_nested_attributes_for :formageddon_form
    
    attr_accessor :error_msg, :captcha_image
    
    @@contact_fields = [ 
      :title, :first_name, :last_name, :email, :address1, :address2, :zip5, :zip4, :city, :state,                
      :phone, :issue_area, :subject, :message, :submit_button, :leave_blank, :captcha_solution
    ]
    def self.contact_fields
      @@contact_fields
    end
    
    def has_captcha?
      command =~ /submit_form/ && formageddon_form.has_captcha?
    end
    
    def execute(browser, options = {})
      raise "Browser is nil!" if browser.nil? 
      
      save_states = options[:save_states].nil? ? true : options[:save_states]
      
      if save_states
        delivery_attempt = options[:delivery_attempt]
        delivery_attempt.letter_contact_step = self.step_number unless delivery_attempt.nil?
      end
      
      case self.command
      when /^visit::/
        command, url = self.command.split(/::/)
        
        begin
          browser.get(url)
        rescue Timeout::Error
          save_after_error($!, options[:letter], delivery_attempt, save_states)
          
          return false
        rescue 
          save_after_error($!, options[:letter], delivery_attempt, save_states)
          
          return false
        end
        
        return true
      when /^submit_form/
        raise "Must submit :letter to execute!" if options[:letter].nil?
        letter = options[:letter]
        
        delivery_attempt.save_before_browser_state(browser) if save_states
        
        if formageddon_form.has_captcha? and letter.captcha_solution.nil?
          letter.status = 'CAPTCHA_REQUIRED'
          letter.save
          
          if save_states
             delivery_attempt.result = 'CAPTCHA_REQUIRED'
             delivery_attempt.save
          end
          
          save_captcha_image(browser, letter)
          return false
        end
        
        form = browser.page.forms[formageddon_form.form_number]
        form.fields.each_with_index do |field, field_index|
          puts "TRYING FIELD: #{field.name}"
          if formageddon_form.use_field_names?
            ff = formageddon_form.formageddon_form_fields.select{|f| f.name == field.name }.first     
          else
            ff = formageddon_form.formageddon_form_fields[field_index]
          end
          
          unless ff.nil?
            if letter.kind_of? FormageddonLetter
            
              if ff.value == 'email' and not Formageddon::configuration.reply_domain.nil?
                field.value = "formageddon+#{letter.formageddon_thread.id}@#{Formageddon::configuration.reply_domain}"
              else
                field.value = letter.value_for(ff.value) unless ff.not_changeable?
              end
            
            # Hashes are used in form building
            elsif letter.kind_of? Hash
              puts "FILLING #{field.name} with #{letter[ff.value]}"
              field.value = letter[ff.value]
            end
          end
        end
        
        begin
          form.submit
        rescue Timeout::Error
          save_after_error($!, options[:letter], delivery_attempt, save_states)

          delivery_attempt.save_after_browser_state(browser) if save_states
          
          return false
        rescue 
          save_after_error($!, options[:letter], delivery_attempt, save_states)

          delivery_attempt.save_after_browser_state(browser) if save_states
           
          return false
        end
        
        if formageddon_form.success_string.blank?
          if letter.kind_of? Formageddon::FormageddonLetter
            letter.status = 'WARNING: Confirmation message is blank. Unable to confirm delivery.'
            letter.save
          end
          
          if save_states
            delivery_attempt.save_after_browser_state(browser)

            delivery_attempt.result = 'WARNING: Confirmation message is blank. Unable to confirm delivery.'
            delivery_attempt.save            
          end
          
          return true
        elsif (browser.page.parser.to_s =~ /#{formageddon_form.success_string}/).nil?
          # save the browser state in the delivery attempt
          delivery_attempt.save_after_browser_state(browser) if save_states
          
          if letter.status == 'TRYING_CAPTCHA'
            # assume that the captcha was wrong?
            letter.status = 'CAPTCHA_REQUIRED'
            save_captcha_image(browser, letter)
            
            if save_states
              delivery_attempt.result = 'CAPTCHA_WRONG'
              delivery_attempt.save
            end
          else
            letter.status = "WARNING: Confirmation message not found."
            
            delivery_attempt.status = "WARNING: Confirmation message not found." if save_states
          end
          
          letter.save
          delivery_attempt.save if save_states
          
          
          return false
        else
          if letter.kind_of? Formageddon::FormageddonLetter
            letter.status = 'SENT'
            letter.save
          end
          
          if save_states
            delivery_attempt.result = 'SUCCESS'<commit_msg>fixed bug<commit_after>module Formageddon
  class FormageddonContactStep < ActiveRecord::Base
    belongs_to :formageddon_recipient, :polymorphic => true
    has_one :formageddon_form, :dependent => :destroy
    
    accepts_nested_attributes_for :formageddon_form
    
    attr_accessor :error_msg, :captcha_image
    
    @@contact_fields = [ 
      :title, :first_name, :last_name, :email, :address1, :address2, :zip5, :zip4, :city, :state,                
      :phone, :issue_area, :subject, :message, :submit_button, :leave_blank, :captcha_solution
    ]
    def self.contact_fields
      @@contact_fields
    end
    
    def has_captcha?
      command =~ /submit_form/ && formageddon_form.has_captcha?
    end
    
    def execute(browser, options = {})
      raise "Browser is nil!" if browser.nil? 
      
      save_states = options[:save_states].nil? ? true : options[:save_states]
      
      if save_states
        delivery_attempt = options[:delivery_attempt]
        delivery_attempt.letter_contact_step = self.step_number unless delivery_attempt.nil?
      end
      
      case self.command
      when /^visit::/
        command, url = self.command.split(/::/)
        
        begin
          browser.get(url)
        rescue Timeout::Error
          save_after_error($!, options[:letter], delivery_attempt, save_states)
          
          return false
        rescue 
          save_after_error($!, options[:letter], delivery_attempt, save_states)
          
          return false
        end
        
        return true
      when /^submit_form/
        raise "Must submit :letter to execute!" if options[:letter].nil?
        letter = options[:letter]
        
        delivery_attempt.save_before_browser_state(browser) if save_states
        
        if formageddon_form.has_captcha? and letter.captcha_solution.nil?
          letter.status = 'CAPTCHA_REQUIRED'
          letter.save
          
          if save_states
             delivery_attempt.result = 'CAPTCHA_REQUIRED'
             delivery_attempt.save
          end
          
          save_captcha_image(browser, letter)
          return false
        end
        
        form = browser.page.forms[formageddon_form.form_number]
        form.fields.each_with_index do |field, field_index|
          puts "TRYING FIELD: #{field.name}"
          if formageddon_form.use_field_names?
            ff = formageddon_form.formageddon_form_fields.select{|f| f.name == field.name }.first     
          else
            ff = formageddon_form.formageddon_form_fields[field_index]
          end
          
          unless ff.nil?
            if letter.kind_of? FormageddonLetter
            
              if ff.value == 'email' and not Formageddon::configuration.reply_domain.nil?
                field.value = "formageddon+#{letter.formageddon_thread.id}@#{Formageddon::configuration.reply_domain}"
              else
                field.value = letter.value_for(ff.value) unless ff.not_changeable?
              end
            
            # Hashes are used in form building
            elsif letter.kind_of? Hash
              puts "FILLING #{field.name} with #{letter[ff.value]}"
              field.value = letter[ff.value]
            end
          end
        end
        
        begin
          form.submit
        rescue Timeout::Error
          save_after_error($!, options[:letter], delivery_attempt, save_states)

          delivery_attempt.save_after_browser_state(browser) if save_states
          
          return false
        rescue 
          save_after_error($!, options[:letter], delivery_attempt, save_states)

          delivery_attempt.save_after_browser_state(browser) if save_states
           
          return false
        end
        
        if formageddon_form.success_string.blank?
          if letter.kind_of? Formageddon::FormageddonLetter
            letter.status = 'WARNING: Confirmation message is blank. Unable to confirm delivery.'
            letter.save
          end
          
          if save_states
            delivery_attempt.save_after_browser_state(browser)

            delivery_attempt.result = 'WARNING: Confirmation message is blank. Unable to confirm delivery.'
            delivery_attempt.save            
          end
          
          return true
        elsif (browser.page.parser.to_s =~ /#{formageddon_form.success_string}/).nil?
          # save the browser state in the delivery attempt
          delivery_attempt.save_after_browser_state(browser) if save_states
          
          if letter.status == 'TRYING_CAPTCHA'
            # assume that the captcha was wrong?
            letter.status = 'CAPTCHA_REQUIRED'
            save_captcha_image(browser, letter)
            
            if save_states
              delivery_attempt.result = 'CAPTCHA_WRONG'
              delivery_attempt.save
            end
          else
            letter.status = "WARNING: Confirmation message not found."
            
            delivery_attempt.result = "WARNING: Confirmation message not found." if save_states
          end
          
          letter.save
          delivery_attempt.save if save_states
          
          
          return false
        else
          if letter.kind_of? Formageddon::FormageddonLetter
            letter.status = 'SENT'
            letter.save
          end
          
          if save_states
            delivery_attempt.result = 'SUCCESS'
{'fraction_non_alphanumeric': 0.07269046742730953, 'fraction_numerical': 0.000736105999263894, 'mean_word_length': 1.8482704402515724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6339362', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2937, 'n_words': 624}