Skip to content

Commit

Permalink
Merge pull request #2515 from huginn/imap_delete
Browse files Browse the repository at this point in the history
Add a `delete` option to ImapFolderAgent
  • Loading branch information
knu committed Mar 29, 2019
2 parents 6662ec6 + 000da0e commit 8757d2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/models/agents/imap_folder_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ImapFolderAgent < Agent
If this key is unspecified or set to null, it is ignored.
Set `mark_as_read` to true to mark found mails as read.
Set `delete` to true to delete found mails.
Set `event_headers` to a list of header names you want to include in a `headers` hash in each created event, either in an array of string or in a comma-separated string.
Expand Down Expand Up @@ -128,7 +129,7 @@ def validate_options
errors.add(:base, "port must be a positive integer") unless is_positive_integer?(options['port'])
end

%w[ssl mark_as_read include_raw_mail].each { |key|
%w[ssl mark_as_read delete include_raw_mail].each { |key|
if options[key].present?
if boolify(options[key]).nil?
errors.add(:base, '%s must be a boolean value' % key)
Expand Down Expand Up @@ -315,6 +316,11 @@ def check
log 'Marking as read'
mail.mark_as_read unless dry_run?
end

if boolify(interpolated['delete'])
log 'Deleting'
mail.delete unless dry_run?
end
}
end

Expand Down Expand Up @@ -568,6 +574,11 @@ def mark_as_read
@client.uid_store(@uid, '+FLAGS', [:Seen])
end

def delete
@client.uid_store(@uid, '+FLAGS', [:Deleted])
@client.expunge
end

private

def struct_has_attachment?(struct)
Expand Down
8 changes: 8 additions & 0 deletions spec/models/agents/imap_folder_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ def body_parts(mime_types = %[text/plain text/enriched text/html])
expect { @checker.check }.to change { Event.count }.by(1)
end

it 'should delete mails if delete is true' do
@checker.options['delete'] = true
mails.each { |mail|
stub(mail).delete.once
}
expect { @checker.check }.to change { Event.count }.by(2)
end

describe 'processing mails with a broken From header value' do
before do
# "from" patterns work against mail addresses and not
Expand Down

0 comments on commit 8757d2a

Please sign in to comment.