Class: MailMCP::ImapClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_mcp/imap_client.rb

Defined Under Namespace

Classes: AuthError, ConnectionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imap) ⇒ ImapClient

Returns a new instance of ImapClient.



10
11
12
# File 'lib/mail_mcp/imap_client.rb', line 10

def initialize(imap)
  @imap = imap
end

Instance Attribute Details

#imapObject (readonly)

Returns the value of attribute imap.



8
9
10
# File 'lib/mail_mcp/imap_client.rb', line 8

def imap
  @imap
end

Class Method Details

.connect(config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mail_mcp/imap_client.rb', line 25

def self.connect(config)
  conn = Net::IMAP.new(config[:host], port: config[:port], ssl: config[:ssl])
  conn.(config[:username], config[:password])
  client = new(conn)
  yield client
rescue Net::IMAP::NoResponseError, Net::IMAP::BadResponseError => e
  raise AuthError, "IMAP authentication failed: #{e.message}"
rescue StandardError => e
  raise ConnectionError, "IMAP connection failed: #{e.message}"
ensure
  begin
    conn&.logout
  rescue StandardError
    nil
  end
  begin
    conn&.disconnect
  rescue StandardError
    nil
  end
end

.validate!(config) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/mail_mcp/imap_client.rb', line 14

def self.validate!(config)
  conn = Net::IMAP.new(config[:host], port: config[:port], ssl: config[:ssl])
  conn.(config[:username], config[:password])
  conn.logout
  conn.disconnect
rescue Net::IMAP::NoResponseError, Net::IMAP::BadResponseError => e
  raise AuthError, "IMAP authentication failed: #{e.message}"
rescue StandardError => e
  raise ConnectionError, "IMAP connection failed: #{e.message}"
end

Instance Method Details

#append_message(folder:, raw_message:) ⇒ Object



115
116
117
# File 'lib/mail_mcp/imap_client.rb', line 115

def append_message(folder:, raw_message:)
  @imap.append(folder, raw_message, [:Draft], Time.now)
end

#delete_message(folder:, uid:) ⇒ Object



92
93
94
95
96
# File 'lib/mail_mcp/imap_client.rb', line 92

def delete_message(folder:, uid:)
  @imap.select(folder)
  @imap.uid_store(uid.to_i, "+FLAGS", [:Deleted])
  @imap.expunge
end

#get_message(folder:, uid:) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mail_mcp/imap_client.rb', line 64

def get_message(folder:, uid:)
  @imap.examine(folder)
  data = @imap.uid_fetch([uid.to_i], %w[RFC822 FLAGS]).first
  return nil unless data

  raw = data.attr["RFC822"]
  flags = data.attr["FLAGS"]
  parsed = Mail.new(raw)
  attachments = extract_attachments(parsed)
  {
    uid: uid,
    subject: parsed.subject,
    from: parsed.from,
    to: parsed.to,
    cc: parsed.cc,
    date: parsed.date&.iso8601,
    text_body: parsed.text_part&.decoded,
    html_body: parsed.html_part&.decoded,
    flags: flags,
    attachments: attachments
  }
end

#list_mailboxesObject



47
48
49
# File 'lib/mail_mcp/imap_client.rb', line 47

def list_mailboxes
  @imap.list("", "*").map(&:name)
end

#list_messages(folder:, page: 1, per_page: 20) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mail_mcp/imap_client.rb', line 51

def list_messages(folder:, page: 1, per_page: 20)
  @imap.examine(folder)
  uids = @imap.uid_search(["ALL"]).reverse
  total = uids.length
  offset = (page - 1) * per_page
  page_uids = uids[offset, per_page] || []
  return { messages: [], total: total } if page_uids.empty?

  envelopes = @imap.uid_fetch(page_uids, ["ENVELOPE", "FLAGS", "RFC822.SIZE"])
  messages = (envelopes || []).map { |msg| format_envelope(msg) }
  { messages: messages, total: total, page: page, per_page: per_page }
end

#move_message(folder:, uid:, destination:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/mail_mcp/imap_client.rb', line 98

def move_message(folder:, uid:, destination:)
  @imap.select(folder)
  if @imap.capability.include?("MOVE")
    @imap.uid_move(uid.to_i, destination)
  else
    @imap.uid_copy(uid.to_i, destination)
    @imap.uid_store(uid.to_i, "+FLAGS", [:Deleted])
    @imap.expunge
  end
end

#search_messages(folder:, query:) ⇒ Object



87
88
89
90
# File 'lib/mail_mcp/imap_client.rb', line 87

def search_messages(folder:, query:)
  @imap.examine(folder)
  @imap.search(query.split)
end

#update_flags(folder:, uid:, add: [], remove: []) ⇒ Object



109
110
111
112
113
# File 'lib/mail_mcp/imap_client.rb', line 109

def update_flags(folder:, uid:, add: [], remove: [])
  @imap.select(folder)
  @imap.uid_store(uid.to_i, "+FLAGS", add) unless add.empty?
  @imap.uid_store(uid.to_i, "-FLAGS", remove) unless remove.empty?
end