# frozen_string_literal: false
# Author:: Akira Yamada <akira@ruby-lang.org>
# License:: You can redistribute it and/or modify it under the same term as Ruby.
# See URI for general documentation
require_relative 'generic'
# RFC6068, the mailto URL scheme.
# A Default port of nil for URI::MailTo.
# An Array of the available components for URI::MailTo.
COMPONENT = [ :scheme, :to, :headers ].freeze
# "hname" and "hvalue" are encodings of an RFC 822 header name and
# value, respectively. As with "to", all URL reserved characters must
# "#mailbox" is as specified in RFC 822 [RFC822]. This means that it
# consists of zero or more comma-separated mail addresses, possibly
# including "phrase" and "comment" components. Note that all URL
# reserved characters in "to" must be encoded: in particular,
# parentheses, commas, and the percent sign ("%"), which commonly occur
# in the "mailbox" syntax.
# Within mailto URLs, the characters "?", "=", "&" are reserved.
# hfields = "?" hfield *( "&" hfield )
# hfield = hfname "=" hfvalue
# qchar = unreserved / pct-encoded / some-delims
# some-delims = "!" / "$" / "'" / "(" / ")" / "*"
# / "+" / "," / ";" / ":" / "@"
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
# pct-encoded = "%" HEXDIG HEXDIG
HEADER_REGEXP = /\A(?<hfield>(?:%\h\h|[!$'-.0-;@-Z_a-z~])*=(?:%\h\h|[!$'-.0-;@-Z_a-z~])*)(?:&\g<hfield>)*\z/
# practical regexp for email address
# https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
EMAIL_REGEXP = /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/
# Creates a new URI::MailTo object from components, with syntax checking.
# Components can be provided as an Array or Hash. If an Array is used,
# the components must be supplied as <code>[to, headers]</code>.
# If a Hash is used, the keys are the component names preceded by colons.
# The headers can be supplied as a pre-encoded string, such as
# <code>"subject=subscribe&cc=address"</code>, or as an Array of Arrays
# like <code>[['subject', 'subscribe'], ['cc', 'address']]</code>.
# m1 = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
# m1.to_s # => "mailto:joe@example.com?subject=Ruby"
# m2 = URI::MailTo.build(['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]])
# m2.to_s # => "mailto:john@example.com?Subject=Ruby&Cc=jack@example.com"
# m3 = URI::MailTo.build({:to => 'listman@example.com', :headers => [['subject', 'subscribe']]})
# m3.to_s # => "mailto:listman@example.com?subject=subscribe"
tmp = Util.make_components_hash(self, args)
tmp[:opaque] = tmp[:to].join(',')
tmp[:opaque] = tmp[:to].dup
tmp[:headers].collect { |x|
x[0] + '=' + x[1..-1].join
tmp[:headers].collect { |h,v|
tmp[:opaque] << '?' << query
# Creates a new URI::MailTo object from generic URL components with
# This method is usually called from URI::parse, which checks
# the validity of each component.
# The RFC3986 parser does not normally populate opaque
@opaque = "?#{@query}" if @query && !@opaque
raise InvalidComponentError,
"missing opaque part for mailto URL"
to, header = @opaque.split('?', 2)
# allow semicolon as a addr-spec separator
# http://support.microsoft.com/kb/820868
unless /\A(?:[^@,;]+@[^@,;]+(?:\z|[,;]))*\z/ =~ to
raise InvalidComponentError,
"unrecognised opaque part for mailtoURL: #{@opaque}"
# The primary e-mail address of the URL, as a String.
# E-mail headers set by the URL, as an Array of Arrays.
# Checks the to +v+ component.
return true if v.size == 0
v.split(/[,;]/).each do |addr|
# check url safety as path-rootless
if /\A(?:%\h\h|[!$&-.0-;=@-Z_a-z~])*\z/ !~ addr
raise InvalidComponentError,
"an address in 'to' is invalid as URI #{addr.dump}"
addr.gsub!(/%\h\h/, URI::TBLDECWWWCOMP_)
raise InvalidComponentError,
"an address in 'to' is invalid as uri-escaped addr-spec #{addr.dump}"
# Private setter for to +v+.
# Checks the headers +v+ component against either
return true if v.size == 0
raise InvalidComponentError,
"bad component(expected opaque component): #{v}"
# Private setter for headers +v+.
@headers << x.split(/=/, 2)
# Setter for headers +v+.
# Constructs String from URI.
'?' + @headers.collect{|x| x.join('=')}.join('&')
# Returns the RFC822 e-mail text equivalent of the URL, as a String.
# uri = URI.parse("mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr")
# # => "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n"
to = URI.decode_www_form_component(@to)
body = URI.decode_www_form_component(x[1])
to << ', ' + URI.decode_www_form_component(x[1])
head << URI.decode_www_form_component(x[0]).capitalize + ': ' +
URI.decode_www_form_component(x[1]) + "\n"
alias to_rfc822text to_mailtext
@@schemes['MAILTO'] = MailTo