Class: Dubhe::Sources::Mail

Inherits:
Object
  • Object
show all
Includes:
Dubhe::Source
Defined in:
lib/dubhe/sources/mail.rb

Instance Method Summary collapse

Constructor Details

#initialize(path:, store: Store.new, policy: Policy.new(profile: :mail)) ⇒ Mail

Returns a new instance of Mail.



12
13
14
15
16
17
# File 'lib/dubhe/sources/mail.rb', line 12

def initialize(path:, store: Store.new, policy: Policy.new(profile: :mail))
  @path = File.expand_path(path.to_s)
  @store = store
  @policy = policy
  @messages = {}
end

Instance Method Details

#entries(filter: nil) ⇒ Object



43
44
45
46
47
# File 'lib/dubhe/sources/mail.rb', line 43

def entries(filter: nil)
  result = @store.entries(source_id: id)
  result = refresh if result.empty?
  filter ? result.select { |entry| filter.call(entry) } : result
end

#fetch_body(entry) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dubhe/sources/mail.rb', line 49

def fetch_body(entry)
  message = @messages[entry.id]
  return @store.body(entry.id) unless message

  if (part = message.html_part)
    html = message.decoded(part)
    message.inline_parts.each do |content_id, inline_part|
      next if content_id.start_with?("<")

      mime = inline_part.content_type.to_s
      data = Base64.strict_encode64(inline_part.body.to_s)
      html = html.gsub("cid:#{content_id}", "data:#{mime};base64,#{data}")
    end
    result = @policy.apply(html)
    Body.new(kind: :html, content: result.html, base_url: nil,
      attachments: message.attachments.map(&:filename).compact, headers: {"blocked_count" => result.blocked_count})
  elsif (part = message.text_part)
    Body.new(kind: :text, content: message.decoded(part), base_url: nil,
      attachments: message.attachments.map(&:filename).compact, headers: {})
  else
    Body.new(kind: :text, content: "", base_url: nil, attachments: [], headers: {})
  end
end

#icon ⇒ Object



21
# File 'lib/dubhe/sources/mail.rb', line 21

def icon = "mail"

#id ⇒ Object



19
# File 'lib/dubhe/sources/mail.rb', line 19

def id = "mail:#{Digest::SHA256.hexdigest(@path)}"

#refresh ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dubhe/sources/mail.rb', line 23

def refresh
  messages = if File.extname(@path).downcase == ".mbox"
               Ukdah::Mbox.each(File.binread(@path)).to_a
             else
               [Ukdah::Message.parse(File.binread(@path))]
             end
  entries = messages.map do |message|
    message_id = message.message_id || Digest::SHA256.hexdigest(message.raw.to_s)
    entry = Entry.new(id: "#{id}:#{message_id}", source_id: id, title: message.subject.to_s,
      author: message.from.map { |address| address.respond_to?(:email) ? address.email : address.to_s }.join(", "),
      published_at: message.date, updated_at: message.date, url: nil, summary: message.header("x-snippet"),
      tags: [], state: :unread, parent_id: nil)
    @messages[entry.id] = message
    @store.save_entry(entry)
    @store.save_body(entry.id, fetch_body(entry))
    entry
  end
  entries
end

#supports?(capability) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dubhe/sources/mail.rb', line 73

def supports?(capability)
  %i[refresh search unread].include?(capability)
end

#title ⇒ Object



20
# File 'lib/dubhe/sources/mail.rb', line 20

def title = File.basename(@path)