home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1997 December / Internet_Info_CD-ROM_Walnut_Creek_December_1997.iso / drafts / draft_n_r / draft-newman-url-imap-08.txt < prev    next >
Text File  |  1997-05-07  |  30KB  |  845 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                          C. Newman
  8. Internet Draft: IMAP URL Scheme                                 Innosoft
  9. Document: draft-newman-url-imap-08.txt                          May 1997
  10.                                                    Expires in six months
  11.  
  12.  
  13.                             IMAP URL Scheme
  14.  
  15.  
  16. Status of this memo
  17.  
  18.      This document is an Internet Draft.  Internet Drafts are working
  19.      documents of the Internet Engineering Task Force (IETF), its Areas,
  20.      and its Working Groups.  Note that other groups may also distribute
  21.      working documents as Internet Drafts.
  22.  
  23.      Internet Drafts are draft documents valid for a maximum of six
  24.      months.  Internet Drafts may be updated, replaced, or obsoleted by
  25.      other documents at any time.  It is not appropriate to use Internet
  26.      Drafts as reference material or to cite them other than as a
  27.      ``working draft'' or ``work in progress``.
  28.  
  29.      To learn the current status of any Internet-Draft, please check the
  30.      1id-abstracts.txt listing contained in the Internet-Drafts Shadow
  31.      Directories on ds.internic.net, nic.nordu.net, ftp.isi.edu, or
  32.      munnari.oz.au.
  33.  
  34.      A revised version of this draft document will be submitted to the
  35.      RFC editor as a Proposed Standard for the Internet Community.
  36.      Discussion and suggestions for improvement are requested.  This
  37.      document will expire six months after publication.  Distribution of
  38.      this draft is unlimited.
  39.  
  40.  
  41. Abstract
  42.  
  43.      IMAP [IMAP4] is a rich protocol for accessing remote message
  44.      stores.  It provides an ideal mechanism for accessing public
  45.      mailing list archives as well as private and shared message stores.
  46.      This document defines a URL scheme for referencing objects on an
  47.      IMAP server.
  48.  
  49.  
  50. 1. Conventions used in this document
  51.  
  52.      The key words "MUST", "MUST NOT", "SHOULD", "SHOULD NOT", and "MAY"
  53.      in this document are to be interpreted as defined in "Key words for
  54.      use in RFCs to Indicate Requirement Levels" [KEYWORDS].
  55.  
  56.  
  57.  
  58. Newman                                                          [Page 1]
  59.  
  60. Internet Draft              IMAP URL Scheme                     May 1997
  61.  
  62.  
  63. 2. IMAP scheme
  64.  
  65.      The IMAP URL scheme is used to designate IMAP servers, mailboxes,
  66.      messages, MIME bodies [MIME], and search programs on Internet hosts
  67.      accessible using the IMAP protocol.
  68.  
  69.      The IMAP URL follows the common Internet scheme syntax as defined
  70.      in RFC 1738 [BASIC-URL] except that clear text passwords are not
  71.      permitted.  If :<port> is omitted, the port defaults to 143.
  72.  
  73.      An IMAP URL takes one of the following forms:
  74.  
  75.          imap://<iserver>/
  76.          imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  77.          imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  78.          imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  79.  
  80.      The first form is used to refer to an IMAP server, the second form
  81.      refers to a list of mailboxes, the third form refers to the
  82.      contents of a mailbox or a set of messages resulting from a search,
  83.      and the final form refers to a specific message or message part.
  84.  
  85.  
  86. 3. IMAP User Name and Authentication Mechanism
  87.  
  88.      A user name and/or authentication mechanism may be supplied.  They
  89.      are used in the "LOGIN" or "AUTHENTICATE" commands after making the
  90.      connection to the IMAP server.  If no user name or authentication
  91.      mechanism is supplied, the user name "anonymous" is used with the
  92.      "LOGIN" command and the password is supplied as the Internet e-mail
  93.      address of the end user accessing the resource.  If the URL
  94.      supplies a user name, the program interpreting the IMAP URL SHOULD
  95.      request one from the user if necessary.
  96.  
  97.      An authentication mechanism can be expressed by adding
  98.      ";AUTH=<enc_auth_type>" to the end of the user name.  When such an
  99.      <enc_auth_type> is indicated, the client SHOULD request appropriate
  100.      credentials from that mechanism and use the "AUTHENTICATE" command
  101.      instead of the "LOGIN" command.  If no user name is specified, one
  102.      SHOULD be obtained from the mechanism or requested from the user as
  103.      appropriate.
  104.  
  105.      The string ";AUTH=*" indicates that the client SHOULD select an
  106.      appropriate authentication mechanism.  It MAY use any mechanism
  107.      listed in the CAPABILITY command or use an out of band security
  108.      service resulting in a PREAUTH connection.  If no user name is
  109.      specified and no appropriate authentication mechanisms are
  110.      available, the client SHOULD fall back to anonymous login as
  111.  
  112.  
  113.  
  114. Newman                                                          [Page 2]
  115.  
  116. Internet Draft              IMAP URL Scheme                     May 1997
  117.  
  118.  
  119.      described above.  This allows a URL which grants read-write access
  120.      to authorized users, and read-only anonymous access to other users.
  121.  
  122.      Note that if unsafe or reserved characters such as " " or ";" are
  123.      present in the user name or authentication mechanism, they MUST be
  124.      encoded as described in RFC 1738 [BASIC-URL].
  125.  
  126.  
  127. 4. IMAP server
  128.  
  129.      An IMAP URL referring to an IMAP server has the following form:
  130.  
  131.          imap://<iserver>/
  132.  
  133.      A program interpreting this URL would issue the standard set of
  134.      commands it uses to present a view of the contents of an IMAP
  135.      server.  It is likely to be semanticly equivalent to one of the
  136.      following URLs:
  137.  
  138.          imap://<iserver>/;TYPE=LIST
  139.          imap://<iserver>/;TYPE=LSUB
  140.  
  141.      The program interpreting this URL SHOULD use the LSUB form if it
  142.      supports mailbox subscriptions.
  143.  
  144.  
  145. 5. Lists of mailboxes
  146.  
  147.      An IMAP URL referring to a list of mailboxes has the following
  148.      form:
  149.  
  150.          imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  151.  
  152.      The <list_type> may be either "LIST" or "LSUB", and is case
  153.      insensitive.  The field ";TYPE=<list_type>" MUST be included.
  154.  
  155.      The <enc_list_mailbox> is any argument suitable for the
  156.      list_mailbox field of the IMAP [IMAP4] LIST or LSUB commands.  The
  157.      field <enc_list_mailbox> may be omitted, in which case the program
  158.      interpreting the IMAP URL may use "*" or "%" as the
  159.      <enc_list_mailbox>.  The program SHOULD use "%" if it supports a
  160.      hierarchical view, otherwise it SHOULD use "*".
  161.  
  162.      Note that if unsafe or reserved characters such as " " or "%" are
  163.      present in <enc_list_mailbox> they MUST be encoded as described in
  164.      RFC 1738 [BASIC-URL].  If the character "/" is present in
  165.      enc_list_mailbox, it SHOULD NOT be encoded.
  166.  
  167.  
  168.  
  169.  
  170. Newman                                                          [Page 3]
  171.  
  172. Internet Draft              IMAP URL Scheme                     May 1997
  173.  
  174.  
  175. 6. Lists of messages
  176.  
  177.      An IMAP URL referring to a list of messages has the following form:
  178.  
  179.          imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  180.  
  181.      The <enc_mailbox> field is used as the argument to the IMAP4
  182.      "SELECT" command.  Note that if unsafe or reserved characters such
  183.      as " ", ";", or "?" are present in <enc_mailbox> they MUST be
  184.      encoded as described in RFC 1738 [BASIC-URL].  If the character "/"
  185.      is present in enc_mailbox, it SHOULD NOT be encoded.
  186.  
  187.      The [uidvalidity] field is optional.  If it is present, it MUST be
  188.      the argument to the IMAP4 UIDVALIDITY status response at the time
  189.      the URL was created.  This SHOULD be used by the program
  190.      interpreting the IMAP URL to determine if the URL is stale.
  191.  
  192.      The [?<enc_search>] field is optional.  If it is not present, the
  193.      contents of the mailbox SHOULD be presented by the program
  194.      interpreting the URL.  If it is present, it SHOULD be used as the
  195.      arguments following an IMAP4 SEARCH command with unsafe characters
  196.      such as " " (which are likely to be present in the <enc_search>)
  197.      encoded as described in RFC 1738 [BASIC-URL].
  198.  
  199.  
  200. 7. A specific message or message part
  201.  
  202.      An IMAP URL referring to a specific message or message part has the
  203.      following form:
  204.  
  205.          imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  206.  
  207.      The <enc_mailbox> and [uidvalidity] are as defined above.
  208.  
  209.      If [uidvalidity] is present in this form, it SHOULD be used by the
  210.      program interpreting the URL to determine if the URL is stale.
  211.  
  212.      The <uid> refers to an IMAP4 message UID, and SHOULD be used as the
  213.      <set> argument to the IMAP4 "UID FETCH" command.
  214.  
  215.      The [isection] field is optional.  If not present, the URL refers
  216.      to the entire Internet message as returned by the IMAP command "UID
  217.      FETCH <uid> BODY.PEEK[]".  If present, the URL refers to the object
  218.      returned by a "UID FETCH <uid> BODY.PEEK[<section>]" command.  The
  219.      type of the object may be determined with a "UID FETCH <uid>
  220.      BODYSTRUCTURE" command and locating the appropriate part in the
  221.      resulting BODYSTRUCTURE.  Note that unsafe characters in [isection]
  222.      MUST be encoded as described in [BASIC-URL].
  223.  
  224.  
  225.  
  226. Newman                                                          [Page 4]
  227.  
  228. Internet Draft              IMAP URL Scheme                     May 1997
  229.  
  230.  
  231. 8. Relative IMAP URLs
  232.  
  233.      Relative IMAP URLs are permitted and are resolved according to the
  234.      rules defined in RFC 1808 [REL-URL] with one exception.  In IMAP
  235.      URLs, parameters are treated as part of the normal path with
  236.      respect to relative URL resolution.  This is believed to be the
  237.      behavior of the installed base and is likely to be documented in a
  238.      future revision of the relative URL specification.
  239.  
  240.      The following observations are also important:
  241.  
  242.      The <iauth> grammar element is considered part of the user name for
  243.      purposes of resolving relative IMAP URLs.  This means that unless a
  244.      new login/server specification is included in the relative URL, the
  245.      authentication mechanism is inherited from a base IMAP URL.
  246.  
  247.      URLs always use "/" as the hierarchy delimiter for the purpose of
  248.      resolving paths in relative URLs.  IMAP4 permits the use of any
  249.      hierarchy delimiter in mailbox names.  For this reason, relative
  250.      mailbox paths will only work if the mailbox uses "/" as the
  251.      hierarchy delimiter.  Relative URLs may be used on mailboxes which
  252.      use other delimiters, but in that case, the entire mailbox name
  253.      MUST be specified in the relative URL or inherited as a whole from
  254.      the base URL.
  255.  
  256.      The base URL for a list of mailboxes or messages which was referred
  257.      to by an IMAP URL is always the referring IMAP URL itself.  The
  258.      base URL for a message or message part which was referred to by an
  259.      IMAP URL may be more complicated to determine.  The program
  260.      interpreting the relative URL will have to check the headers of the
  261.      MIME entity and any enclosing MIME entities in order to locate the
  262.      "Content-Base" and "Content-Location" headers.  These headers are
  263.      used to determine the base URL as defined in [HTTP].  For example,
  264.      if the referring IMAP URL contains a "/;SECTION=1.2" parameter,
  265.      then the MIME headers for section 1.2, for section 1, and for the
  266.      enclosing message itself SHOULD be checked in that order for
  267.      "Content-Base" or "Content-Location" headers.
  268.  
  269.  
  270. 9. Multinational Considerations
  271.  
  272.      IMAP4 [IMAP4] section 5.1.3 includes a convention for encoding
  273.      non-US-ASCII characters in IMAP mailbox names.  Because this
  274.      convention is private to IMAP, it is necessary to convert IMAP's
  275.      encoding to one that can be more easily interpreted by a URL
  276.      display program.  For this reason, IMAP's modified UTF-7 encoding
  277.      for mailboxes MUST be converted to UTF-8 [UTF8].  Since 8-bit
  278.      characters are not permitted in URLs, the UTF-8 characters are
  279.  
  280.  
  281.  
  282. Newman                                                          [Page 5]
  283.  
  284. Internet Draft              IMAP URL Scheme                     May 1997
  285.  
  286.  
  287.      encoded as required by the URL specification [BASIC-URL].  Sample
  288.      code is included in Appendix A to demonstrate this conversion.
  289.  
  290.  
  291. 10. Examples
  292.  
  293.      The following examples demonstrate how an IMAP4 client program
  294.      might translate various IMAP4 URL into a series of IMAP4 commands.
  295.      Commands sent from the client to the server are prefixed with "C:",
  296.      and responses sent from the server to the client are prefixed with
  297.      "S:".
  298.  
  299.      The URL:
  300.  
  301.       <imap://minbari.org/gray-council;UIDVALIDITY=385759045/;UID=20>
  302.  
  303.      Results in the following client commands:
  304.  
  305.          <connect to minbari.org, port 143>
  306.          C: A001 LOGIN ANONYMOUS sheridan@babylon5.org
  307.          C: A002 SELECT gray-council
  308.          <client verifies the UIDVALIDITY matches>
  309.          C: A003 UID FETCH 20 BODY.PEEK[]
  310.  
  311.      The URL:
  312.  
  313.       <imap://michael@minbari.org/users.*;type=list>
  314.  
  315.      Results in the following client commands:
  316.  
  317.          <client requests password from user>
  318.          <connect to minbari.org, port 143>
  319.          C: A001 LOGIN MICHAEL zipper
  320.          C: A002 LIST "" users.*
  321.  
  322.      The URL:
  323.  
  324.       <imap://psy.earth/~peter/%E6%97%A5%E6%9C%AC%E8%AA%9E/%E5%8F%B0%E5%8C%97>
  325.  
  326.      Results in the following client commands:
  327.  
  328.          <connect to psy.earth, port 143>
  329.          C: A001 LOGIN ANONYMOUS bester@psycop.psy.earth
  330.          C: A002 SELECT ~peter/&ZeVnLIqe-/&U,BTFw-
  331.          <commands the client uses for viewing the contents of a mailbox>
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. Newman                                                          [Page 6]
  339.  
  340. Internet Draft              IMAP URL Scheme                     May 1997
  341.  
  342.  
  343.      The URL:
  344.  
  345.       <imap://;AUTH=KERBEROS_V4@minbari.org/gray-council/;uid=20/;section=1.2>
  346.  
  347.      Results in the following client commands:
  348.  
  349.          <connect to minbari.org, port 143>
  350.          C: A001 AUTHENTICATE KERBEROS_V4
  351.          <authentication exchange>
  352.          C: A002 SELECT gray-council
  353.          C: A003 UID FETCH 20 BODY.PEEK[1.2]
  354.  
  355.      If the following relative URL is located in that body part:
  356.  
  357.       <;section=1.4>
  358.  
  359.      This could result in the following client commands:
  360.  
  361.          C: A004 UID FETCH 20 (BODY.PEEK[1.2.MIME]
  362.                  BODY.PEEK[1.MIME]
  363.                  BODY.PEEK[HEADER.FIELDS (Content-Base Content-Location)])
  364.          <Client looks for Content-Base or Content-Location headers in
  365.           result.  If no such headers, then it does the following>
  366.          C: A005 UID FETCH 20 BODY.PEEK[1.4]
  367.  
  368.      The URL:
  369.  
  370.       <imap://;AUTH=*@minbari.org/gray%20council?SUBJECT%20shadows>
  371.  
  372.      Could result in the following:
  373.  
  374.          <connect to minbari.org, port 143>
  375.          C: A001 CAPABILITY
  376.          S: * CAPABILITY IMAP4rev1 AUTH=GSSAPI
  377.          S: A001 OK
  378.          C: A002 AUTHENTICATE GSSAPI
  379.          <authentication exchange>
  380.          S: A002 OK user lennier authenticated
  381.          C: A003 SELECT "gray council"
  382.          ...
  383.          C: A004 SEARCH SUBJECT shadows
  384.          S: * SEARCH 8 10 13 14 15 16
  385.          S: A004 OK SEARCH completed
  386.          C: A005 FETCH 8,10,13:16 ALL
  387.          ...
  388.  
  389.      NOTE: In this final example, the client has implementation dependent
  390.      choices.  The authentication mechanism could be anything, including
  391.  
  392.  
  393.  
  394. Newman                                                          [Page 7]
  395.  
  396. Internet Draft              IMAP URL Scheme                     May 1997
  397.  
  398.  
  399.      PREAUTH.  And the final FETCH command could fetch more or less
  400.      information about the messages, depending on what it wishes to display
  401.      to the user.
  402.  
  403.  
  404. 11. ABNF for IMAP URL scheme
  405.  
  406.      This uses ABNF as defined in RFC 822 [IMAIL].  Terminals from the
  407.      BNF for IMAP [IMAP4] and URLs [BASIC-URL] are also used.  Strings
  408.      are not case sensitive and free insertion of linear-white-space is
  409.      not permitted.
  410.  
  411.      achar            = uchar / "&" / "=" / "~"
  412.                              ; see [BASIC-URL] for "uchar" definition
  413.  
  414.      bchar            = achar / ":" / "@" / "/"
  415.  
  416.      enc_auth_type    = 1*achar
  417.                              ; encoded version of [IMAP-AUTH] "auth_type"
  418.  
  419.      enc_list_mailbox = 1*bchar
  420.                              ; encoded version of [IMAP4] "list_mailbox"
  421.  
  422.      enc_mailbox      = 1*bchar
  423.                              ; encoded version of [IMAP4] "mailbox"
  424.  
  425.      enc_search       = 1*bchar
  426.                              ; encoded version of search_program below
  427.  
  428.      enc_section      = 1*bchar
  429.                              ; encoded version of section below
  430.  
  431.      enc_user         = *achar
  432.                              ; encoded version of [IMAP4] "userid"
  433.  
  434.      imapurl          = "imap://" iserver "/" [ icommand ]
  435.  
  436.      iauth            = ";AUTH=" ( "*" / enc_auth_type )
  437.  
  438.      icommand         = imailboxlist / ipath / isearch
  439.  
  440.      imailboxlist     = [enc_list_mailbox] ";TYPE=" list_type
  441.  
  442.      ipath            = enc_mailbox [uidvalidity] iuid [isection]
  443.  
  444.      isearch          = enc_mailbox [ "?" enc_search ] [uidvalidity]
  445.  
  446.      isection         = "/;SECTION=" enc_section
  447.  
  448.  
  449.  
  450. Newman                                                          [Page 8]
  451.  
  452. Internet Draft              IMAP URL Scheme                     May 1997
  453.  
  454.  
  455.      iserver          = [enc_user [ iauth ] "@"] hostport
  456.                              ; See [BASIC-URL] for "hostport" definition
  457.  
  458.      iuid             = "/;UID=" nz_number
  459.                              ; See [IMAP4] for "nz_number" definition
  460.  
  461.      list_type        = "LIST" / "LSUB"
  462.  
  463.      search_program   = ["CHARSET" SPACE astring SPACE] 1#search_key
  464.                              ; IMAP4 literals may not be used
  465.                              ; See [IMAP4] for "astring" and "search_key"
  466.  
  467.      section          = section_text / (nz_number *["." nz_number]
  468.                          ["." (section_text / "MIME")])
  469.                              ; See [IMAP4] for "section_text" and "nz_number"
  470.  
  471.      uidvalidity      = ";UIDVALIDITY=" nz_number
  472.                              ; See [IMAP4] for "nz_number" definition
  473.  
  474.  
  475. 12. References
  476.  
  477.      [BASIC-URL] Berners-Lee, Masinter, McCahill, "Uniform Resource
  478.      Locators (URL)", RFC 1738, CERN, Xerox Corporation, University of
  479.      Minnesota, December 1994.
  480.  
  481.          <ftp://ds.internic.net/rfc/rfc1738.txt>
  482.  
  483.      [IMAP4] Crispin, M., "Internet Message Access Protocol - Version
  484.      4rev1", RFC 2060, University of Washington, December 1996.
  485.  
  486.          <ftp://ds.internic.net/rfc/rfc2060.txt>
  487.  
  488.      [IMAP-AUTH] Myers, J., "IMAP4 Authentication Mechanism", RFC 1731,
  489.      Carnegie-Mellon University, December 1994.
  490.  
  491.          <ftp://ds.internic.net/rfc/rfc1731.txt>
  492.  
  493.      [HTTP] Fielding, Gettys, Mogul, Frystyk, Berners-Lee, "Hypertext
  494.      Transfer Protocol -- HTTP/1.1", RFC 2068, UC Irvine, DEC, MIT/LCS,
  495.      January 1997.
  496.  
  497.          <ftp://ds.internic.net/rfc/rfc2068.txt>
  498.  
  499.      [IMAIL] Crocker, "Standard for the Format of ARPA Internet Text
  500.      Messages", STD 11, RFC 822, University of Delaware, August 1982.
  501.  
  502.          <ftp://ds.internic.net/rfc/rfc822.txt>
  503.  
  504.  
  505.  
  506. Newman                                                          [Page 9]
  507.  
  508. Internet Draft              IMAP URL Scheme                     May 1997
  509.  
  510.  
  511.      [KEYWORDS] Bradner, "Key words for use in RFCs to Indicate
  512.      Requirement Levels", RFC 2119, Harvard University, March 1997.
  513.  
  514.          <ftp://ds.internic.net/rfc/rfc2119.txt>
  515.  
  516.      [MIME] Freed, N., Borenstein, N., "Multipurpose Internet Mail
  517.      Extensions", RFC 2045, Innosoft, First Virtual, November 1996.
  518.  
  519.         <ftp://ds.internic.net/rfc/rfc2045.txt>
  520.  
  521.      [REL-URL] Fielding, "Relative Uniform Resource Locators", RFC 1808,
  522.      UC Irvine, June 1995.
  523.  
  524.          <ftp://ds.internic.net/rfc/rfc1808.txt>
  525.  
  526.      [UTF8] Yergeau, F. "UTF-8, a transformation format of Unicode and
  527.      ISO 10646", RFC 2044, Alis Technologies, October 1996.
  528.  
  529.          <ftp://ds.internic.net/rfc/rfc2044.txt>
  530.  
  531.  
  532. 13. Security Considerations
  533.  
  534.      Security considerations discussed in the IMAP specification [IMAP4]
  535.      and the URL specification [BASIC-URL] are relevant.
  536.  
  537.      Client authors SHOULD be careful when selecting an authentication
  538.      mechanism if ";AUTH=*" is specified without a user name.  Clients
  539.      SHOULD NOT fall back to the "LOGIN" command with a user other than
  540.      "anonymous".  A client which violates this rule is vulnerable to an
  541.      active attacker which spoofs the server and does not declare
  542.      support for any AUTHENTICATE mechanisms.
  543.  
  544.      Many email clients store the plain text password for later use
  545.      after logging into an IMAP server.  Such clients MUST NOT use a
  546.      stored password in response to an IMAP URL without explicit
  547.      permission from the user to supply that password to the specified
  548.      host name.
  549.  
  550.  
  551. 14. Author's Address
  552.  
  553.      Chris Newman
  554.      Innosoft International, Inc.
  555.      1050 East Garvey Ave. South
  556.      West Covina, CA 91790 USA
  557.  
  558.      Email: chris.newman@innosoft.com
  559.  
  560.  
  561.  
  562. Newman                                                         [Page 10]
  563.  
  564. Internet Draft              IMAP URL Scheme                     May 1997
  565.  
  566.  
  567. Appendix A.  Sample code
  568.  
  569. Here is sample C source code to convert between URL paths and IMAP
  570. mailbox names, taking into account mapping between IMAP's modified UTF-7
  571. [IMAP4] and hex-encoded UTF-8 which is more appropriate for URLs.  This
  572. code has not been rigorously tested nor does it necessarily behave
  573. reasonably with invalid input, but it should serve as a useful example.
  574. This code just converts the mailbox portion of the URL and does not deal
  575. with parameters, query or server components of the URL.
  576.  
  577. #include <stdio.h>
  578. #include <string.h>
  579.  
  580. /* hexadecimal lookup table */
  581. static char hex[] = "0123456789ABCDEF";
  582.  
  583. /* URL unsafe printable characters */
  584. static char urlunsafe[] = " \"#%&+:;<=>?@[\\]^`{|}";
  585.  
  586. /* UTF7 modified base64 alphabet */
  587. static char base64chars[] =
  588.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
  589. #define UNDEFINED 64
  590.  
  591. /* UTF16 definitions */
  592. #define UTF16MASK       0x03FFUL
  593. #define UTF16SHIFT      10
  594. #define UTF16HIGHSTART  0xD800UL
  595. #define UTF16HIGHEND    0xDBFFUL
  596. #define UTF16LOSTART    0xDC00UL
  597. #define UTF16LOEND      0xDFFFUL
  598.  
  599. /* Convert an IMAP mailbox to a URL path
  600.  *  dst needs to have roughly 4 times the storage space of src
  601.  *    Hex encoding can triple the size of the input
  602.  *    UTF-7 can be slightly denser than UTF-8
  603.  *     (worst case: 8 octets UTF-7 becomes 9 octets UTF-8)
  604.  */
  605. void MailboxToURL(char *dst, char *src)
  606. {
  607.     unsigned char c, i, bitcount;
  608.     unsigned long ucs4, utf16, bitbuf;
  609.     unsigned char base64[256], utf8[6];
  610.  
  611.     /* initialize modified base64 decoding table */
  612.     memset(base64, UNDEFINED, sizeof (base64));
  613.     for (i = 0; i < sizeof (base64chars); ++i) {
  614.         base64[base64chars[i]] = i;
  615.  
  616.  
  617.  
  618. Newman                                                         [Page 11]
  619.  
  620. Internet Draft              IMAP URL Scheme                     May 1997
  621.  
  622.  
  623.     }
  624.  
  625.     /* loop until end of string */
  626.     while (*src != '\0') {
  627.         c = *src++;
  628.         /* deal with literal characters and &- */
  629.         if (c != '&' || *src == '-') {
  630.             if (c < ' ' || c > '~' || strchr(urlunsafe, c) != NULL) {
  631.                 /* hex encode if necessary */
  632.                 dst[0] = '%';
  633.                 dst[1] = hex[c >> 4];
  634.                 dst[2] = hex[c & 0x0f];
  635.                 dst += 3;
  636.             } else {
  637.                 /* encode literally */
  638.                 *dst++ = c;
  639.             }
  640.             /* skip over the '-' if this is an &- sequence */
  641.             if (c == '&') ++src;
  642.         } else {
  643.             /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */
  644.             bitbuf = 0;
  645.             bitcount = 0;
  646.             ucs4 = 0;
  647.             while ((c = base64[(unsigned char) *src]) != UNDEFINED) {
  648.                 ++src;
  649.                 bitbuf = (bitbuf << 6) | c;
  650.                 bitcount += 6;
  651.                 /* enough bits for a UTF-16 character? */
  652.                 if (bitcount >= 16) {
  653.                     bitcount -= 16;
  654.                     utf16 = (bitcount ? bitbuf >> bitcount : bitbuf) & 0xffff;
  655.                     /* convert UTF16 to UCS4 */
  656.                     if (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) {
  657.                         ucs4 = (utf16 & UTF16MASK) << UTF16SHIFT;
  658.                         continue;
  659.                     } else if (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) {
  660.                         ucs4 |= utf16 & UTF16MASK;
  661.                     } else {
  662.                         ucs4 = utf16;
  663.                     }
  664.                     /* convert UTF-16 range of UCS4 to UTF-8 */
  665.                     if (ucs4 <= 0x7fUL) {
  666.                         utf8[0] = ucs4;
  667.                         i = 1;
  668.                     } else if (ucs4 <= 0x7ffUL) {
  669.                         utf8[0] = 0xc0 | (ucs4 >> 6);
  670.                         utf8[1] = 0x80 | (ucs4 & 0x3f);
  671.  
  672.  
  673.  
  674. Newman                                                         [Page 12]
  675.  
  676. Internet Draft              IMAP URL Scheme                     May 1997
  677.  
  678.  
  679.                         i = 2;
  680.                     } else if (ucs4 <= 0xffffUL) {
  681.                         utf8[0] = 0xe0 | (ucs4 >> 12);
  682.                         utf8[1] = 0x80 | ((ucs4 >> 6) & 0x3f);
  683.                         utf8[2] = 0x80 | (ucs4 & 0x3f);
  684.                         i = 3;
  685.                     } else {
  686.                         utf8[0] = 0xf0 | (ucs4 >> 18);
  687.                         utf8[1] = 0x80 | ((ucs4 >> 12) & 0x3f);
  688.                         utf8[2] = 0x80 | ((ucs4 >> 6) & 0x3f);
  689.                         utf8[3] = 0x80 | (ucs4 & 0x3f);
  690.                         i = 4;
  691.                     }
  692.                     /* convert utf8 to hex */
  693.                     for (c = 0; c < i; ++c) {
  694.                         dst[0] = '%';
  695.                         dst[1] = hex[utf8[c] >> 4];
  696.                         dst[2] = hex[utf8[c] & 0x0f];
  697.                         dst += 3;
  698.                     }
  699.                 }
  700.             }
  701.             /* skip over trailing '-' in modified UTF-7 encoding */
  702.             if (*src == '-') ++src;
  703.         }
  704.     }
  705.     /* terminate destination string */
  706.     *dst = '\0';
  707. }
  708.  
  709. /* Convert hex coded UTF-8 URL path to modified UTF-7 IMAP mailbox
  710.  *  dst should be about twice the length of src to deal with non-hex coded URLs
  711.  */
  712. void URLtoMailbox(char *dst, char *src)
  713. {
  714.     unsigned int utf8pos, utf8total, i, c, utf7mode, bitstogo, utf16flag;
  715.     unsigned long ucs4, bitbuf;
  716.     unsigned char hextab[256];
  717.  
  718.     /* initialize hex lookup table */
  719.     memset(hextab, 0, sizeof (hextab));
  720.     for (i = 0; i < sizeof (hex); ++i) {
  721.         hextab[hex[i]] = i;
  722.         if (isupper(hex[i])) hextab[tolower(hex[i])] = i;
  723.     }
  724.  
  725.     utf7mode = 0;
  726.     utf8total = 0;
  727.  
  728.  
  729.  
  730. Newman                                                         [Page 13]
  731.  
  732. Internet Draft              IMAP URL Scheme                     May 1997
  733.  
  734.  
  735.     bitstogo = 0;
  736.     while ((c = *src) != '\0') {
  737.         ++src;
  738.         /* undo hex-encoding */
  739.         if (c == '%' && src[0] != '\0' && src[1] != '\0') {
  740.             c = (hextab[src[0]] << 4) | hextab[src[1]];
  741.             src += 2;
  742.         }
  743.         /* normal character? */
  744.         if (c >= ' ' && c <= '~') {
  745.             /* switch out of UTF-7 mode */
  746.             if (utf7mode) {
  747.                 if (bitstogo) {
  748.                     *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
  749.                 }
  750.                 *dst++ = '-';
  751.                 utf7mode = 0;
  752.             }
  753.             *dst++ = c;
  754.             /* encode '&' as '&-' */
  755.             if (c == '&') {
  756.                 *dst++ = '-';
  757.             }
  758.             continue;
  759.         }
  760.         /* switch to UTF-7 mode */
  761.         if (!utf7mode) {
  762.             *dst++ = '&';
  763.             utf7mode = 1;
  764.         }
  765.         /* Encode US-ASCII characters as themselves */
  766.         if (c < 0x80) {
  767.             ucs4 = c;
  768.             utf8total = 1;
  769.         } else if (utf8total) {
  770.             /* save UTF8 bits into UCS4 */
  771.             ucs4 = (ucs4 << 6) | (c & 0x3FUL);
  772.             if (++utf8pos < utf8total) {
  773.                 continue;
  774.             }
  775.         } else {
  776.             utf8pos = 1;
  777.             if (c < 0xE0) {
  778.                 utf8total = 2;
  779.                 ucs4 = c & 0x1F;
  780.             } else if (c < 0xF0) {
  781.                 utf8total = 3;
  782.                 ucs4 = c & 0x0F;
  783.  
  784.  
  785.  
  786. Newman                                                         [Page 14]
  787.  
  788. Internet Draft              IMAP URL Scheme                     May 1997
  789.  
  790.  
  791.             } else {
  792.                 /* NOTE: can't convert UTF8 sequences longer than 4 */
  793.                 utf8total = 4;
  794.                 ucs4 = c & 0x03;
  795.             }
  796.             continue;
  797.         }
  798.         /* loop to split ucs4 into two utf16 chars if necessary */
  799.         utf8total = 0;
  800.         do {
  801.             if (ucs4 > 0xffffUL) {
  802.                 bitbuf = (bitbuf << 16) | ((ucs4 >> UTF16SHIFT)
  803.                                            + UTF16HIGHSTART);
  804.                 ucs4 = (ucs4 & UTF16MASK) + UTF16LOSTART;
  805.                 utf16flag = 1;
  806.             } else {
  807.                 bitbuf = (bitbuf << 16) | ucs4;
  808.                 utf16flag = 0;
  809.             }
  810.             bitstogo += 16;
  811.             /* spew out base64 */
  812.             while (bitstogo >= 6) {
  813.                 bitstogo -= 6;
  814.                 *dst++ = base64chars[(bitstogo ? (bitbuf >> bitstogo) : bitbuf)
  815.                                      & 0x3F];
  816.             }
  817.         } while (utf16flag);
  818.     }
  819.     /* if in UTF-7 mode, finish in ASCII */
  820.     if (utf7mode) {
  821.         if (bitstogo) {
  822.             *dst++ = base64chars[(bitbuf << (6 - bitstogo)) & 0x3F];
  823.         }
  824.         *dst++ = '-';
  825.     }
  826.     /* tie off string */
  827.     *dst = '\0';
  828. }
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842. Newman                                                         [Page 15]
  843.  
  844.  
  845.