Class: PecRuby::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/pec_ruby/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodystructureObject (readonly)

Returns the value of attribute bodystructure.



7
8
9
# File 'lib/pec_ruby/message.rb', line 7

def bodystructure
  @bodystructure
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/pec_ruby/message.rb', line 7

def client
  @client
end

#envelopeObject (readonly)

Returns the value of attribute envelope.



7
8
9
# File 'lib/pec_ruby/message.rb', line 7

def envelope
  @envelope
end

#uidObject (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_messagesObject

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 all_postacert_messages
  messages = []
  
  # Add the main postacert message (this message)
  if has_postacert?
    messages << {
      level: 0,
      message: self,
      type: :main_postacert
    }
  end
  
  # Add nested postacert messages
  nested_postacert_messages.each_with_index do |nested_msg, index|
    messages << {
      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.as_postacert_message
        if deep_nested_msg
          messages << {
            level: 2,
            message: deep_nested_msg,
            type: :deep_nested_postacert,
            parent_index: index,
            index: deep_index
          }
        end
      end
    end
  end
  
  messages
end

#attachmentsObject

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 attachments
  if has_postacert?
    postacert_attachments
  else
    [] # Direct messages typically don't have attachments via IMAP
  end
end

#dateObject



43
44
45
46
47
48
49
# File 'lib/pec_ruby/message.rb', line 43

def date
  if has_postacert?
    postacert_message&.date
  else
    original_date
  end
end

#fromObject



27
28
29
30
31
32
33
# File 'lib/pec_ruby/message.rb', line 27

def from
  if has_postacert?
    postacert_message&.from&.first
  else
    original_from
  end
end

#has_nested_postacerts?Boolean

Check if original message has nested postacert.eml files

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


78
79
80
# File 'lib/pec_ruby/message.rb', line 78

def has_postacert?
  !find_postacert_part_ids.empty?
end

#nested_postacert_messagesObject

Get all nested postacert messages parsed and ready to use



265
266
267
# File 'lib/pec_ruby/message.rb', line 265

def nested_postacert_messages
  nested_postacerts.map(&:as_postacert_message).compact
end

#nested_postacertsObject

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 ||= postacert_attachments.select(&:postacert?)
end

#original_attachmentsObject

Legacy methods for backward compatibility



234
235
236
# File 'lib/pec_ruby/message.rb', line 234

def original_attachments
  postacert_attachments
end

#original_bodyObject



242
243
244
# File 'lib/pec_ruby/message.rb', line 242

def original_body
  postacert_body
end

#original_body_htmlObject



250
251
252
# File 'lib/pec_ruby/message.rb', line 250

def original_body_html
  postacert_body_html
end

#original_body_textObject



246
247
248
# File 'lib/pec_ruby/message.rb', line 246

def original_body_text
  postacert_body_text
end

#original_dateObject



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_fromObject



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_attachmentsObject



238
239
240
# File 'lib/pec_ruby/message.rb', line 238

def original_regular_attachments
  postacert_regular_attachments
end

#original_subjectObject

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_toObject



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_attachmentsObject

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
  @postacert_attachments ||= begin
    mail = postacert_message
    attachments = []
    
    # Add attachments from the postacert message
    if mail&.attachments
      attachments += mail.attachments.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
    attachments += nested_postacert_attachments
    
    attachments
  end
end

#postacert_bodyObject

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 = postacert_message
  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_htmlObject

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 = postacert_message
  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_textObject

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 = postacert_message
  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_messageObject

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 postacert_message
  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.message}"
  end

  @postacert_mail
end

#postacert_regular_attachmentsObject

Get postacert message attachments that are NOT postacert.eml files



211
212
213
# File 'lib/pec_ruby/message.rb', line 211

def postacert_regular_attachments
  postacert_attachments.reject(&:postacert?)
end

#raw_bodyObject

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
    direct_message_body
  end
end

#raw_body_htmlObject

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
    direct_message_body_html
  end
end

#raw_body_textObject

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
    direct_message_body_text
  end
end

#regular_attachmentsObject

Get regular attachments (non-postacert.eml)



225
226
227
228
229
230
231
# File 'lib/pec_ruby/message.rb', line 225

def regular_attachments
  if has_postacert?
    postacert_regular_attachments
  else
    [] # Direct messages typically don't have attachments via IMAP
  end
end

#subjectObject

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?
    postacert_message&.subject
  else
    original_subject
  end
end

#summaryObject

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: original_attachments.size,
    regular_attachments_count: original_regular_attachments.size,
    nested_postacerts_count: nested_postacerts.size,
    has_nested_postacerts: has_nested_postacerts?,
    total_postacert_messages: all_postacert_messages.size
  }
end

#toObject



35
36
37
38
39
40
41
# File 'lib/pec_ruby/message.rb', line 35

def to
  if has_postacert?
    postacert_message&.to || []
  else
    original_to
  end
end