<commit_before># encoding: UTF-8
# Receive and handle emails sent to tasks

class Mailman < ActionMailer::Base
  # The marker in the email body that shows where the new content ends
  BODY_SPLIT = "o------ please reply above this line ------o"

  # helper method to remove email reply noise from the body
  def self.clean_body(body)
    new_body_end = body.to_s.index(Mailman::BODY_SPLIT) || body.to_s.length
    body = body.to_s[0, new_body_end].strip

    lines = body.to_s.split("\n")
    while lines.any?
      line = lines.last.strip

      if line.blank? or line.match(/^[<>]+$/) or line.match(/.* wrote:/)
        lines.pop
      else
        break
      end
    end

    return lines.join("\n")
  end

  def get_body(email)
    body = nil

    if email.multipart? then
      email.parts.each do |m|
        next if body

        if m.content_type =~ /text\/plain/i
          body = m.body.to_s.force_encoding(m.charset || "US_ASCII").encode(Encoding.default_internal)
        elsif m.multipart?
          body = get_body(m)
        end
      end
    end

    body ||= email.body.to_s.force_encoding(email.charset || "US-ASCII").encode(Encoding.default_internal)
    body = Mailman.clean_body(body)
    body = CGI::escapeHTML(body)
    return body
  end

  def bad_subject?(sub)
    return false if sub.nil?
    arr = YAML.load_file(File.join(Rails.root, '/config/bad_subjects.yml'))<commit_msg>Fix mailman encoding convertation code: it will work only on Ruby 1.9<commit_after># encoding: UTF-8
# Receive and handle emails sent to tasks

class Mailman < ActionMailer::Base
  # The marker in the email body that shows where the new content ends
  BODY_SPLIT = "o------ please reply above this line ------o"

  # helper method to remove email reply noise from the body
  def self.clean_body(body)
    new_body_end = body.to_s.index(Mailman::BODY_SPLIT) || body.to_s.length
    body = body.to_s[0, new_body_end].strip

    lines = body.to_s.split("\n")
    while lines.any?
      line = lines.last.strip

      if line.blank? or line.match(/^[<>]+$/) or line.match(/.* wrote:/)
        lines.pop
      else
        break
      end
    end

    return lines.join("\n")
  end

  def get_body(email)
    body = nil

    if email.multipart? then
      email.parts.each do |m|
        next if body

        if m.content_type =~ /text\/plain/i
          if RUBY_VERSION >= '1.9'
            body = m.body.to_s.force_encoding(m.charset || "US_ASCII").encode(Encoding.default_internal)
          else
            body = m.body.to_s
          end
        elsif m.multipart?
          body = get_body(m)
        end
      end
    end
    if RUBY_VERSION >= '1.9'
      body ||= email.body.to_s.force_encoding(email.charset || "US-ASCII").encode(Encoding.default_internal)
    else
      body ||= email.body.to_s
    end
    body = Mailman.clean_body(body)
    body = CGI::escapeHTML(body)
    return body
  end

  def bad_subject?(sub)
    return false if sub.nil?
    arr = YAML.load_file(File.join(Rails.root, '/config/bad_subjects.yml'))
{'fraction_non_alphanumeric': 0.11927512355848435, 'fraction_numerical': 0.0032948929159802307, 'mean_word_length': 2.418918918918919, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1962064', 'n_tokens_mistral': 1046, 'n_tokens_neox': 1009, 'n_words': 260}