imap.rb/lib/imap_server/client_handler/fetch.rb
2022-12-11 13:53:41 +01:00

46 lines
1.4 KiB
Ruby

class ImapServer
class ClientHandler
module Fetch
# C: A654 FETCH 2:4 (FLAGS BODY[HEADER.FIELDS (DATE FROM)])
# S: * 2 FETCH ....
# S: * 3 FETCH ....
# S: * 4 FETCH ....
# S: A654 OK FETCH completed
def handle_fetch(msg:, user:)
if !(mailbox = msg.handler.selected)
msg.respond ServerMessage.new("BAD requires to select a mailbox first",
tag: msg.tag)
end
msg.respond ServerMessage.new("BAD require an id argument", tag: msg.tag) if !(id = msg.args.first)
flags = msg.args[1..]
handle_fetch_mailbox(msg, mailbox, id, flags)
end
# BODY[HEADER.FIELDS (MESSAGE-ID)]
TOKENS = %w[
ALL FAST FULL BODY ENVELOPE UID
BODY[TEXT] RFC822 RFC822.HEADER
]
def handle_fetch_mailbox(msg, mailbox, id, _flags)
ids =
if id.include?(":")
Range.new id.split(":").map(&:to_i)
elsif id.include?(",")
id.split(",").map(&:to_i)
else
[id.to_i]
end
mails = Mail.where(mailbox: mailbox, uid: ids)
mails.each do |mail|
body = mail.raw
msg.respond ServerMessage.new("#{mail.uid} FETCH (RFC822 {#{body.size}}\r\n#{body})")
end
msg.respond ServerMessage.new("OK FETCH completed", tag: msg.tag)
end
end
end
end