self.parse

in www/secretary/workbench/models/message.rb [432:525]


  def self.parse(message)

    
    mail = Mail.read_from_string(message.gsub(LF_ONLY, CRLF))

    
    from_value = mail[:from].value rescue ''
    begin
      from = liberal_email_parser(from_value).display_name
    rescue Exception
      from = from_value.sub(/\s+<.*?>$/, '')
    end

    
    begin
      cc = []
      cc = mail[:to].to_s.split(/,\s*/)  if mail[:to]
      cc += mail[:cc].to_s.split(/,\s*/) if mail[:cc]
    rescue
      cc = []
      cc = mail[:to].value.split(/,\s*/)  if mail[:to]
      cc += mail[:cc].value.split(/,\s*/) if mail[:cc]
    end

    
    cc.reject! do |email|
      begin
        address = liberal_email_parser(email).address
        next true if address == 'secretary@apache.org'
        next true if mail.from_addrs.include? address
      rescue Exception
        true
      end
    end

    
    headers = {
      envelope_from: mail.envelope_from,
      envelope_date: mail.envelope_date.to_s, 
      from: mail.from_addrs.first,
      name: from,
      time: (mail.date.to_time.gmtime.iso8601 rescue nil),
      cc: cc
    }

    
    headers.merge! Mailbox.headers(mail)

    
    if mail.attachments.length > 0

      attachments = mail.attachments.map do |attach|
        
        mime = attach.mime_type
        if mime == 'application/octet-stream'
          filename = attach.filename.downcase
          mime = 'application/pdf' if filename.end_with? '.pdf'
          mime = 'application/png' if filename.end_with? '.png'
          mime = 'application/gif' if filename.end_with? '.gif'
          mime = 'application/jpeg' if filename.end_with? '.jpg'
          mime = 'application/jpeg' if filename.end_with? '.jpeg'
        end

        description = {
          name: attach.filename,
          length: attach.body.to_s.length,
          mime: mime
        }

        if description[:name].empty? and attach['Content-ID']
          description[:name] = attach['Content-ID'].to_s
        end

        description.merge(Mailbox.headers(attach))
      end

      headers[:attachments] = attachments
    
    elsif headers['Subject']&.include?('CLA') &&
         !headers['Subject'].include?('ICLA') &&
         !headers['Subject'].include?('iCLA') &&
         !headers['Subject'].start_with?('Re: ') &&
         !headers['Subject'].start_with?('RE: ')

      headers[:attachments] = [
        {name: RAWMESSAGE_ATTACHMENT_NAME,
          length: message.size,
          mime: 'text/plain'}
      ]
    end

    headers
  end