Module: Mailmate::MidUrl

Defined in:
lib/mailmate/mid_url.rb

Overview

Build a MailMate ‘mid:` URL from a Message-ID. MailMate registers the `mid:` scheme (RFC 2392) and resolves it to a specific message. The angle brackets that bracket the Message-ID in headers must be percent-encoded.

Class Method Summary collapse

Class Method Details

.for(message_id) ⇒ Object

Returns ‘mid:%3C<message-id>%3E`. Accepts the Message-ID with or without surrounding angle brackets — strips them either way before encoding. URL-encodes characters that would break the URL parser, notably `[` and `]` (which appear in Message-IDs containing IPv4 literals, e.g. `<id@>`). Other URL-reserved characters (`@`, `.`, `-`, `_`, etc.) are preserved — MailMate’s parser accepts them as-is.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/mailmate/mid_url.rb', line 16

def self.for(message_id)
  raise ArgumentError, "Message-ID required" if message_id.nil? || message_id.to_s.empty?
  id = message_id.to_s.sub(/\A</, "").sub(/>\z/, "")
  encoded = id.gsub(/[\[\]<>\s]/) { |c| "%%%02X" % c.ord }
  "mid:%3C#{encoded}%3E"
end