Class: PecRuby::Message
- Inherits:
-
Object
- Object
- PecRuby::Message
- Defined in:
- lib/pec_ruby/message.rb
Instance Attribute Summary collapse
-
#bodystructure ⇒ Object
readonly
Returns the value of attribute bodystructure.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#envelope ⇒ Object
readonly
Returns the value of attribute envelope.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
Instance Method Summary collapse
-
#all_postacert_messages ⇒ Object
Get a flattened view of all postacert messages (original + nested) Returns array with the original message first, followed by nested ones.
-
#attachments ⇒ Object
Get attachments - preferring postacert.eml if available, otherwise direct message.
- #date ⇒ Object
- #from ⇒ Object
-
#has_nested_postacerts? ⇒ Boolean
Check if original message has nested postacert.eml files.
-
#has_postacert? ⇒ Boolean
Check if message contains postacert.eml.
-
#initialize(client, fetch_data) ⇒ Message
constructor
A new instance of Message.
-
#nested_postacert_messages ⇒ Object
Get all nested postacert messages parsed and ready to use.
-
#nested_postacerts ⇒ Object
Get nested postacert.eml files from postacert message attachments.
-
#original_attachments ⇒ Object
Legacy methods for backward compatibility.
- #original_body ⇒ Object
- #original_body_html ⇒ Object
- #original_body_text ⇒ Object
- #original_date ⇒ Object
- #original_from ⇒ Object
- #original_regular_attachments ⇒ Object
-
#original_subject ⇒ Object
Original message envelope information (the outer PEC container).
- #original_to ⇒ Object
-
#postacert_attachments ⇒ Object
Get postacert message attachments (with memoization).
-
#postacert_body ⇒ Object
Get postacert message body with format information.
-
#postacert_body_html ⇒ Object
Get postacert message body as HTML only.
-
#postacert_body_text ⇒ Object
Get postacert message body as plain text only.
-
#postacert_message ⇒ Object
Extract and return the original message from postacert.eml.
-
#postacert_regular_attachments ⇒ Object
Get postacert message attachments that are NOT postacert.eml files.
-
#raw_body ⇒ Object
Get message body - preferring postacert.eml if available, otherwise direct message.
-
#raw_body_html ⇒ Object
Get message body as HTML - preferring postacert.eml if available, otherwise direct message.
-
#raw_body_text ⇒ Object
Get message body as plain text - preferring postacert.eml if available, otherwise direct message.
-
#regular_attachments ⇒ Object
Get regular attachments (non-postacert.eml).
-
#subject ⇒ Object
Basic envelope information - now points to postacert.eml when available.
-
#summary ⇒ Object
Summary information.
- #to ⇒ Object
Constructor Details
#initialize(client, fetch_data) ⇒ Message
Returns a new instance of Message.
9 10 11 12 13 14 15 16 |
# File 'lib/pec_ruby/message.rb', line 9 def initialize(client, fetch_data) @client = client @uid = fetch_data.attr["UID"] @envelope = fetch_data.attr["ENVELOPE"] @bodystructure = fetch_data.attr["BODYSTRUCTURE"] @postacert_mail = nil @postacert_extracted = false end |
Instance Attribute Details
#bodystructure ⇒ Object (readonly)
Returns the value of attribute bodystructure.
7 8 9 |
# File 'lib/pec_ruby/message.rb', line 7 def bodystructure @bodystructure end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/pec_ruby/message.rb', line 7 def client @client end |
#envelope ⇒ Object (readonly)
Returns the value of attribute envelope.
7 8 9 |
# File 'lib/pec_ruby/message.rb', line 7 def envelope @envelope end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
7 8 9 |
# File 'lib/pec_ruby/message.rb', line 7 def uid @uid end |
Instance Method Details
#all_postacert_messages ⇒ Object
Get a flattened view of all postacert messages (original + nested) Returns array with the original message first, followed by nested ones
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/pec_ruby/message.rb', line 271 def = [] # Add the main postacert message (this message) if has_postacert? << { level: 0, message: self, type: :main_postacert } end # Add nested postacert messages .each_with_index do |nested_msg, index| << { level: 1, message: nested_msg, type: :nested_postacert, index: index } # Check for deeper nesting (postacert within postacert within postacert) if nested_msg.has_nested_postacerts? nested_msg.nested_postacerts.each_with_index do |deep_nested, deep_index| deep_nested_msg = deep_nested. if deep_nested_msg << { level: 2, message: deep_nested_msg, type: :deep_nested_postacert, parent_index: index, index: deep_index } end end end end end |
#attachments ⇒ Object
Get attachments - preferring postacert.eml if available, otherwise direct message
216 217 218 219 220 221 222 |
# File 'lib/pec_ruby/message.rb', line 216 def if has_postacert? else [] # Direct messages typically don't have attachments via IMAP end end |
#date ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/pec_ruby/message.rb', line 43 def date if has_postacert? &.date else original_date end end |
#from ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/pec_ruby/message.rb', line 27 def from if has_postacert? &.from&.first else original_from end end |
#has_nested_postacerts? ⇒ Boolean
Check if original message has nested postacert.eml files
260 261 262 |
# File 'lib/pec_ruby/message.rb', line 260 def has_nested_postacerts? !nested_postacerts.empty? end |
#has_postacert? ⇒ Boolean
Check if message contains postacert.eml
78 79 80 |
# File 'lib/pec_ruby/message.rb', line 78 def has_postacert? !find_postacert_part_ids.empty? end |
#nested_postacert_messages ⇒ Object
Get all nested postacert messages parsed and ready to use
265 266 267 |
# File 'lib/pec_ruby/message.rb', line 265 def nested_postacerts.map(&:as_postacert_message).compact end |
#nested_postacerts ⇒ Object
Get nested postacert.eml files from postacert message attachments
255 256 257 |
# File 'lib/pec_ruby/message.rb', line 255 def nested_postacerts @nested_postacerts ||= .select(&:postacert?) end |
#original_attachments ⇒ Object
Legacy methods for backward compatibility
234 235 236 |
# File 'lib/pec_ruby/message.rb', line 234 def end |
#original_body ⇒ Object
242 243 244 |
# File 'lib/pec_ruby/message.rb', line 242 def original_body postacert_body end |
#original_body_html ⇒ Object
250 251 252 |
# File 'lib/pec_ruby/message.rb', line 250 def original_body_html postacert_body_html end |
#original_body_text ⇒ Object
246 247 248 |
# File 'lib/pec_ruby/message.rb', line 246 def original_body_text postacert_body_text end |
#original_date ⇒ Object
73 74 75 |
# File 'lib/pec_ruby/message.rb', line 73 def original_date @envelope.date ? Time.parse(@envelope.date.to_s) : nil end |
#original_from ⇒ Object
60 61 62 63 64 65 |
# File 'lib/pec_ruby/message.rb', line 60 def original_from return nil unless @envelope.from&.first from_addr = @envelope.from.first extract_real_sender(from_addr) end |
#original_regular_attachments ⇒ Object
238 239 240 |
# File 'lib/pec_ruby/message.rb', line 238 def end |
#original_subject ⇒ Object
Original message envelope information (the outer PEC container)
52 53 54 55 56 57 58 |
# File 'lib/pec_ruby/message.rb', line 52 def original_subject return nil unless @envelope.subject decoded = Mail::Encodings.value_decode(@envelope.subject) decoded = decoded.gsub("POSTA CERTIFICATA:", "") if decoded.start_with?("POSTA CERTIFICATA:") decoded.strip end |
#original_to ⇒ Object
67 68 69 70 71 |
# File 'lib/pec_ruby/message.rb', line 67 def original_to return [] unless @envelope.to @envelope.to.map { |addr| "#{addr.mailbox}@#{addr.host}" } end |
#postacert_attachments ⇒ Object
Get postacert message attachments (with memoization)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/pec_ruby/message.rb', line 192 def @postacert_attachments ||= begin mail = = [] # Add attachments from the postacert message if mail&. += mail..map { |att| Attachment.new(att) } end # Also check for postacert.eml attachments in the outer message structure # This handles cases where postacert.eml files are forwarded as attachments += end end |
#postacert_body ⇒ Object
Get postacert message body with format information
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/pec_ruby/message.rb', line 106 def postacert_body mail = return nil unless mail text_part = extract_text_part(mail, "text/plain") html_part = extract_text_part(mail, "text/html") # Prefer text/plain, but return HTML if that's all we have selected_part = text_part || html_part return nil unless selected_part raw_body = selected_part.body.decoded charset = selected_part.charset || selected_part.content_type_parameters&.[]("charset") || "UTF-8" content = raw_body.dup.force_encoding(charset).encode("UTF-8") { content: content, content_type: selected_part.mime_type, charset: charset } end |
#postacert_body_html ⇒ Object
Get postacert message body as HTML only
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/pec_ruby/message.rb', line 149 def postacert_body_html mail = return nil unless mail html_part = extract_text_part(mail, "text/html") return nil unless html_part raw_body = html_part.body.decoded charset = html_part.charset || html_part.content_type_parameters&.[]("charset") || "UTF-8" raw_body.dup.force_encoding(charset).encode("UTF-8") end |
#postacert_body_text ⇒ Object
Get postacert message body as plain text only
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/pec_ruby/message.rb', line 133 def postacert_body_text mail = return nil unless mail text_part = extract_text_part(mail, "text/plain") return nil unless text_part raw_body = text_part.body.decoded charset = text_part.charset || text_part.content_type_parameters&.[]("charset") || "UTF-8" raw_body.dup.force_encoding(charset).encode("UTF-8") end |
#postacert_message ⇒ Object
Extract and return the original message from postacert.eml
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pec_ruby/message.rb', line 83 def return @postacert_mail if @postacert_extracted @postacert_extracted = true part_ids = find_postacert_part_ids if part_ids.empty? @postacert_mail = nil return nil end begin part_id = part_ids.first raw_data = @client.fetch_body_part(@uid, part_id) @postacert_mail = Mail.read_from_string(raw_data) rescue => e raise Error, "Failed to extract postacert.eml: #{e.}" end @postacert_mail end |
#postacert_regular_attachments ⇒ Object
Get postacert message attachments that are NOT postacert.eml files
211 212 213 |
# File 'lib/pec_ruby/message.rb', line 211 def .reject(&:postacert?) end |
#raw_body ⇒ Object
Get message body - preferring postacert.eml if available, otherwise direct message
165 166 167 168 169 170 171 |
# File 'lib/pec_ruby/message.rb', line 165 def raw_body if has_postacert? postacert_body else end end |
#raw_body_html ⇒ Object
Get message body as HTML - preferring postacert.eml if available, otherwise direct message
183 184 185 186 187 188 189 |
# File 'lib/pec_ruby/message.rb', line 183 def raw_body_html if has_postacert? postacert_body_html else end end |
#raw_body_text ⇒ Object
Get message body as plain text - preferring postacert.eml if available, otherwise direct message
174 175 176 177 178 179 180 |
# File 'lib/pec_ruby/message.rb', line 174 def raw_body_text if has_postacert? postacert_body_text else end end |
#regular_attachments ⇒ Object
Get regular attachments (non-postacert.eml)
225 226 227 228 229 230 231 |
# File 'lib/pec_ruby/message.rb', line 225 def if has_postacert? else [] # Direct messages typically don't have attachments via IMAP end end |
#subject ⇒ Object
Basic envelope information - now points to postacert.eml when available
19 20 21 22 23 24 25 |
# File 'lib/pec_ruby/message.rb', line 19 def subject if has_postacert? &.subject else original_subject end end |
#summary ⇒ Object
Summary information
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/pec_ruby/message.rb', line 313 def summary { uid: @uid, subject: subject, from: from, to: to, date: date, has_postacert: has_postacert?, original_subject: original_subject, original_from: original_from, original_to: original_to, original_date: original_date, attachments_count: .size, regular_attachments_count: .size, nested_postacerts_count: nested_postacerts.size, has_nested_postacerts: has_nested_postacerts?, total_postacert_messages: .size } end |
#to ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/pec_ruby/message.rb', line 35 def to if has_postacert? &.to || [] else original_to end end |