4 # Author:: Akira Yamada <akira@ruby-lang.org>
5 # License:: You can redistribute it and/or modify it under the same term as Ruby.
6 # Revision:: $Id: mailto.rb 14565 2007-12-24 01:51:49Z drbrain $
14 # RFC2368, The mailto URL scheme
16 class MailTo < Generic
21 COMPONENT = [ :scheme, :to, :headers ].freeze
24 # "hname" and "hvalue" are encodings of an RFC 822 header name and
25 # value, respectively. As with "to", all URL reserved characters must
28 # "#mailbox" is as specified in RFC 822 [RFC822]. This means that it
29 # consists of zero or more comma-separated mail addresses, possibly
30 # including "phrase" and "comment" components. Note that all URL
31 # reserved characters in "to" must be encoded: in particular,
32 # parentheses, commas, and the percent sign ("%"), which commonly occur
33 # in the "mailbox" syntax.
35 # Within mailto URLs, the characters "?", "=", "&" are reserved.
39 # header = hname "=" hvalue
40 HEADER_PATTERN = "(?:[^?=&]*=[^?=&]*)".freeze
41 HEADER_REGEXP = Regexp.new(HEADER_PATTERN, 'N').freeze
42 # headers = "?" header *( "&" header )
44 # mailtoURL = "mailto:" [ to ] [ headers ]
45 MAILBOX_PATTERN = "(?:#{PATTERN::ESCAPED}|[^(),%?=&])".freeze
46 MAILTO_REGEXP = Regexp.new(" # :nodoc:
48 (#{MAILBOX_PATTERN}*?) (?# 1: to)
51 (#{HEADER_PATTERN}(?:\\&#{HEADER_PATTERN})*) (?# 2: headers)
55 (#{PATTERN::FRAGMENT}) (?# 3: fragment)
58 ", Regexp::EXTENDED).freeze
64 # Creates a new URI::MailTo object from components, with syntax checking.
66 # Components can be provided as an Array or Hash. If an Array is used,
67 # the components must be supplied as [to, headers].
69 # If a Hash is used, the keys are the component names preceded by colons.
71 # The headers can be supplied as a pre-encoded string, such as
72 # "subject=subscribe&cc=address", or as an Array of Arrays like
73 # [['subject', 'subscribe'], ['cc', 'address']]
79 # m1 = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
80 # puts m1.to_s -> mailto:joe@example.com?subject=Ruby
82 # m2 = URI::MailTo.build(['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]])
83 # puts m2.to_s -> mailto:john@example.com?Subject=Ruby&Cc=jack@example.com
85 # m3 = URI::MailTo.build({:to => 'listman@example.com', :headers => [['subject', 'subscribe']]})
86 # puts m3.to_s -> mailto:listman@example.com?subject=subscribe
89 tmp = Util::make_components_hash(self, args)
92 tmp[:opaque] = tmp[:to]
100 if tmp[:headers].kind_of?(Array)
101 tmp[:opaque] << tmp[:headers].collect { |x|
103 x[0] + '=' + x[1..-1].to_s
109 elsif tmp[:headers].kind_of?(Hash)
110 tmp[:opaque] << tmp[:headers].collect { |h,v|
115 tmp[:opaque] << tmp[:headers].to_s
125 # Creates a new URI::MailTo object from generic URL components with
126 # no syntax checking.
128 # This method is usually called from URI::parse, which checks
129 # the validity of each component.
137 if MAILTO_REGEXP =~ @opaque
147 raise InvalidComponentError,
148 "unrecognised opaque part for mailtoURL: #{@opaque}"
152 # The primary e-mail address of the URL, as a String
155 # E-mail headers set by the URL, as an Array of Arrays
160 return true if v.size == 0
162 if OPAQUE !~ v || /\A#{MAILBOX_PATTERN}*\z/o !~ v
163 raise InvalidComponentError,
164 "bad component(expected opaque component): #{v}"
184 return true if v.size == 0
187 /\A(#{HEADER_PATTERN}(?:\&#{HEADER_PATTERN})*)\z/o !~ v
188 raise InvalidComponentError,
189 "bad component(expected opaque component): #{v}"
194 private :check_headers
199 v.scan(HEADER_REGEXP) do |x|
200 @headers << x.split(/=/o, 2)
204 protected :set_headers
220 '?' + @headers.collect{|x| x.join('=')}.join('&')
231 # Returns the RFC822 e-mail text equivalent of the URL, as a String.
237 # uri = URI.parse("mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr")
239 # # => "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n"
242 to = URI::unescape(@to)
248 body = URI::unescape(x[1])
250 to << ', ' + URI::unescape(x[1])
252 head << URI::unescape(x[0]).capitalize + ': ' +
253 URI::unescape(x[1]) + "\n"
262 alias to_rfc822text to_mailtext
265 @@schemes['MAILTO'] = MailTo