Skip to content

Commit

Permalink
Merge branch 'imap_agent_scrub_strings'
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Mar 23, 2015
2 parents 1093275 + 5211378 commit 49eb434
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
28 changes: 21 additions & 7 deletions app/models/agents/imap_folder_agent.rb
Expand Up @@ -240,7 +240,7 @@ def check
when 'subject'
value.present? or next true
re = Regexp.new(value)
if m = re.match(mail.subject)
if m = re.match(mail.scrubbed(:subject))
m.names.each { |name|
matches[name] = m[name]
}
Expand All @@ -252,7 +252,7 @@ def check
value.present? or next true
re = Regexp.new(value)
matched_part = body_parts.find { |part|
if m = re.match(part.decoded)
if m = re.match(part.scrubbed(:decoded))
m.names.each { |name|
matches[name] = m[name]
}
Expand Down Expand Up @@ -285,7 +285,7 @@ def check

if matched_part
mime_type = matched_part.mime_type
body = matched_part.decoded
body = matched_part.scrubbed(:decoded)
else
mime_type = 'text/plain'
body = ''
Expand All @@ -295,7 +295,7 @@ def check

create_event :payload => {
'folder' => mail.folder,
'subject' => mail.subject,
'subject' => mail.scrubbed(:subject),
'from' => mail.from_addrs.first,
'to' => mail.to_addrs,
'cc' => mail.cc_addrs,
Expand Down Expand Up @@ -504,6 +504,15 @@ class Message < SimpleDelegator

attr_reader :uid, :folder, :uidvalidity

module Scrubbed
def scrubbed(method)
(@scrubbed ||= {})[method.to_sym] ||=
__send__(method).scrub { |bytes| "<#{bytes.unpack('H*')[0]}>" }
end
end

include Scrubbed

def initialize(client, fetch_data, props = {})
@client = client
props.each { |key, value|
Expand Down Expand Up @@ -538,9 +547,14 @@ def body_parts(mime_types = DEFAULT_BODY_MIME_TYPES)
mail.all_parts
else
[mail]
end.reject { |part|
part.multipart? || part.attachment? || !part.text? ||
!mime_types.include?(part.mime_type)
end.select { |part|
if part.multipart? || part.attachment? || !part.text? ||
!mime_types.include?(part.mime_type)
false
else
part.extend(Scrubbed)
true
end
}
end

Expand Down
31 changes: 30 additions & 1 deletion spec/models/agents/imap_folder_agent_spec.rb
Expand Up @@ -36,8 +36,12 @@ def body_parts(mime_types = %[text/plain text/enriched text/html])
all_parts.find { |part|
part.mime_type == type
}
}.compact
}.compact.map! { |part|
part.extend(Agents::ImapFolderAgent::Message::Scrubbed)
}
end

include Agents::ImapFolderAgent::Message::Scrubbed
}

@mails = [
Expand Down Expand Up @@ -255,4 +259,29 @@ def body_parts(mime_types = %[text/plain text/enriched text/html])
end
end
end

describe 'Agents::ImapFolderAgent::Message::Scrubbed' do
before do
@class = Class.new do
def subject
"broken\xB7subject\xB6"
end

def body
"broken\xB7body\xB6"
end

include Agents::ImapFolderAgent::Message::Scrubbed
end

@object = @class.new
end

describe '#scrubbed' do
it 'should return a scrubbed string' do
expect(@object.scrubbed(:subject)).to eq("broken<b7>subject<b6>")
expect(@object.scrubbed(:body)).to eq("broken<b7>body<b6>")
end
end
end
end

0 comments on commit 49eb434

Please sign in to comment.