<commit_before>class Mailman < ActionMailer::Base

  def get_body(email)
      body = nil

      if email.multipart? then
        email.parts.each do |m|
          puts m.content_type
          if m.content_type.downcase == "text/plain"
            body = m.body
          elsif m.multipart?
            body = get_body(m)
          end
        end
      end

      body ||= email.body
  end

  def receive(email)
    e = Email.new
    e.to = email.to.join(", ")
    e.from = email.from.join(", ")

    e.body = get_body(email)

    e.subject = email.subject

    company = nil
    email.to.each do |to|
      next unless to.include?("clockingit.com")
      subdomain = to.split('@')[1].split('.')[0]
      puts "Looking for #{subdomain}.."
      company = Company.find_by_subdomain(subdomain)
    end

    if company
      e.company = company
      e.user = User.find_by_email(e.from, :conditions => ["company_id = ?", company.id])
    end

    e.save

    return if(e.from.downcase.include? 'clockingit.com' || company.nil?)

    target = nil
    email.to.each do |to|
      if to.include?("task-")
        _, task_num = /task-(\d+).*@.*/.match(to).to_a
        if task_num.to_i > 0
          puts "Looking for task[#{task_num}] from [#{company.name}]"
          target = Task.find(:first, :conditions => ["company_id = ? AND task_num = ?", company.id, task_num])
        end
      end
    end

    if target && target.is_a?(Task)
      puts "Found target [#{target.name}]"
      notify_targets = target.project.users.collect{ |u| u.email.downcase }.flatten.uniq
      notify_targets += Task.find(:all, :conditions => ["project_id = ? AND notify_emails IS NOT NULL and notify_emails <> ''", target.project_id]).collect{ |t| t.notify_emails.split(',').collect{ |i| i.strip.downcase } }.flatten.uniq

      puts "Possible participants[#{notify_targets.join(', ')}]"

      if notify_targets.include?(email.from.first.downcase)
        if email.has_attachments?
          email.attachments.each do |attachment|

            puts "Attachement[#{attachment.original_filename}]"

            task_file = ProjectFile.new()
            task_file.company = e.user.company
            task_file.customer = target.project.customer
            task_file.project = target.project
            task_file.task = target<commit_msg>Don't hardcode clockingit.com as receive domain for emails<commit_after>class Mailman < ActionMailer::Base

  def get_body(email)
      body = nil

      if email.multipart? then
        email.parts.each do |m|
          puts m.content_type
          if m.content_type.downcase == "text/plain"
            body = m.body
          elsif m.multipart?
            body = get_body(m)
          end
        end
      end

      body ||= email.body
  end

  def receive(email)
    e = Email.new
    e.to = email.to.join(", ")
    e.from = email.from.join(", ")

    e.body = get_body(email)

    e.subject = email.subject

    company = nil
    email.to.each do |to|
      next unless to.include?($CONFIG[:domain])
      subdomain = to.split('@')[1].split('.')[0]
      puts "Looking for #{subdomain}.."
      company = Company.find_by_subdomain(subdomain)
    end

    if company
      e.company = company
      e.user = User.find_by_email(e.from, :conditions => ["company_id = ?", company.id])
    end

    e.save

    return if(e.from.downcase.include? $CONFIG[:domain] || company.nil?)

    target = nil
    email.to.each do |to|
      if to.include?("task-")
        _, task_num = /task-(\d+).*@.*/.match(to).to_a
        if task_num.to_i > 0
          puts "Looking for task[#{task_num}] from [#{company.name}]"
          target = Task.find(:first, :conditions => ["company_id = ? AND task_num = ?", company.id, task_num])
        end
      end
    end

    if target && target.is_a?(Task)
      puts "Found target [#{target.name}]"
      notify_targets = target.project.users.collect{ |u| u.email.downcase }.flatten.uniq
      notify_targets += Task.find(:all, :conditions => ["project_id = ? AND notify_emails IS NOT NULL and notify_emails <> ''", target.project_id]).collect{ |t| t.notify_emails.split(',').collect{ |i| i.strip.downcase } }.flatten.uniq

      puts "Possible participants[#{notify_targets.join(', ')}]"

      if notify_targets.include?(email.from.first.downcase)
        if email.has_attachments?
          email.attachments.each do |attachment|

            puts "Attachement[#{attachment.original_filename}]"

            task_file = ProjectFile.new()
            task_file.company = e.user.company
            task_file.customer = target.project.customer
            task_file.project = target.project
            task_file.task = target
{'fraction_non_alphanumeric': 0.13855035279025016, 'fraction_numerical': 0.0012828736369467607, 'mean_word_length': 2.339043540328337, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3266929', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1505, 'n_words': 314}