forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintenance: Improves email drivers.
- Added POP3 driver test. - Improved IMAP driver test. - Unified repeating code into a base class. - Structural code improvements, increasing maintainability.
- Loading branch information
Showing
17 changed files
with
1,471 additions
and
877 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/models/channel/driver/base_email_inbound/message_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/ | ||
|
||
class Channel::Driver::BaseEmailInbound | ||
class MessageValidator | ||
attr_reader :headers, :size | ||
|
||
# @param headers [Hash] in key-value format | ||
# @param size [Integer] in bytes | ||
def initialize(headers, size = nil) | ||
@headers = headers | ||
@size = size | ||
end | ||
|
||
# Checks if email is not too big for processing | ||
# | ||
# This method is used by IMAP and MicrosoftGraphInbound only | ||
# It may be possible to reuse them with POP3 too, but it needs further refactoring | ||
def too_large? | ||
max_message_size = Setting.get('postmaster_max_size').to_f | ||
real_message_size = size.to_f / 1024 / 1024 | ||
if real_message_size > max_message_size | ||
return [real_message_size, max_message_size] | ||
end | ||
|
||
false | ||
end | ||
|
||
# Checks if a message with the given headers is a Zammad verify message | ||
# | ||
# This method is used by IMAP and MicrosoftGraphInbound only | ||
# It may be possible to reuse them with POP3 too, but it needs further refactoring | ||
def verify_message? | ||
headers['X-Zammad-Verify'] == 'true' | ||
end | ||
|
||
# Checks if a message with the given headers marked to be ignored by Zammad | ||
# | ||
# This method is used by IMAP and MicrosoftGraphInbound only | ||
# It may be possible to reuse them with POP3 too, but it needs further refactoring | ||
def ignore? | ||
headers['X-Zammad-Ignore'] == 'true' | ||
end | ||
|
||
# Checks if a message is a new Zammad verify message | ||
# | ||
# Returns false only if a verify message is less than 30 minutes old | ||
# | ||
# This method is used by IMAP and MicrosoftGraphInbound only | ||
# It may be possible to reuse them with POP3 too, but it needs further refactoring | ||
def fresh_verify_message? | ||
return false if !verify_message? | ||
return false if headers['X-Zammad-Verify-Time'].blank? | ||
|
||
begin | ||
verify_time = Time.zone.parse(headers['X-Zammad-Verify-Time']) | ||
rescue => e | ||
Rails.logger.error e | ||
return false | ||
end | ||
|
||
verify_time > 30.minutes.ago | ||
end | ||
|
||
# Checks if a message is already imported in a given channel | ||
# This check is skipped for channels which do not keep messages on the server | ||
# | ||
# This method is used by IMAP and MicrosoftGraphInbound only | ||
# It may be possible to reuse them with POP3 too, but it needs further refactoring | ||
def already_imported?(keep_on_server, channel) | ||
return false if !keep_on_server | ||
|
||
return false if !headers | ||
|
||
local_message_id = headers['Message-ID'] | ||
return false if local_message_id.blank? | ||
|
||
local_message_id_md5 = Digest::MD5.hexdigest(local_message_id) | ||
article = Ticket::Article.where(message_id_md5: local_message_id_md5).reorder('created_at DESC, id DESC').limit(1).first | ||
return false if !article | ||
|
||
# verify if message is already imported via same channel, if not, import it again | ||
ticket = article.ticket | ||
return false if ticket&.preferences && ticket.preferences[:channel_id].present? && channel.present? && ticket.preferences[:channel_id] != channel[:id] | ||
|
||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.