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-07.txt < prev    next >
Text File  |  1997-05-02  |  29KB  |  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-07.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 mailboxes, messages, MIME
  66.      bodies [MIME], and search programs on Internet hosts accessible
  67.      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>/<enc_list_mailbox>;TYPE=<list_type>
  76.          imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  77.          imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  78.  
  79.      The first form is used to refer to a list of mailboxes, the second
  80.      form refers to the contents of a mailbox or a set of messages
  81.      resulting from a search, and the final form refers to a specific
  82.      message or message part.
  83.  
  84.  
  85. 3. IMAP User Name and Authentication Mechanism
  86.  
  87.      A user name and/or authentication mechanism may be supplied.  They
  88.      are used in the "LOGIN" or "AUTHENTICATE" commands after making the
  89.      connection to the IMAP server.  If no user name or authentication
  90.      mechanism is supplied, the user name "anonymous" is used with the
  91.      "LOGIN" command and the password is supplied as the Internet e-mail
  92.      address of the end user accessing the resource.  If the URL
  93.      supplies a user name, the program interpreting the IMAP URL SHOULD
  94.      request one from the user if necessary.
  95.  
  96.      An authentication mechanism can be expressed by adding
  97.      ";AUTH=<enc_auth_type>" to the end of the user name.  When such an
  98.      <enc_auth_type> is indicated, the client SHOULD request appropriate
  99.      credentials from that mechanism and use the "AUTHENTICATE" command
  100.      instead of the "LOGIN" command.  If no user name is specified, one
  101.      SHOULD be obtained from the mechanism or requested from the user as
  102.      appropriate.
  103.  
  104.      The string ";AUTH=*" indicates that the client SHOULD select an
  105.      appropriate authentication mechanism.  It MAY use any mechanism
  106.      listed in the CAPABILITY command or use an out of band security
  107.      service resulting in a PREAUTH connection.  If no user name is
  108.      specified and no appropriate authentication mechanisms are
  109.      available, the client SHOULD fall back to anonymous login as
  110.      described above.  This allows a URL which grants read-write access
  111.  
  112.  
  113.  
  114. Newman                                                          [Page 2]
  115.  
  116. Internet Draft              IMAP URL Scheme                     May 1997
  117.  
  118.  
  119.      to authorized users, and read-only anonymous access to other users.
  120.  
  121.      Note that if unsafe or reserved characters such as " " or ";" are
  122.      present in the user name or authentication mechanism, they MUST be
  123.      encoded as described in RFC 1738 [BASIC-URL].
  124.  
  125.  
  126. 4. Lists of mailboxes
  127.  
  128.      An IMAP URL referring to a list of mailboxes has the following
  129.      form:
  130.  
  131.          imap://<iserver>/<enc_list_mailbox>;TYPE=<list_type>
  132.  
  133.      The <list_type> may be either "LIST" or "LSUB", and is case
  134.      insensitive.  The field ";TYPE=<list_type>" MUST be included.
  135.  
  136.      The <enc_list_mailbox> is any argument suitable for the
  137.      list_mailbox field of the IMAP [IMAP4] LIST or LSUB commands.  The
  138.      field <enc_list_mailbox> may be omitted, in which case the program
  139.      interpreting the IMAP URL may use "*" or "%" as the
  140.      <enc_list_mailbox>.  The program SHOULD use "%" if it supports a
  141.      hierarchical view, otherwise it SHOULD use "*".
  142.  
  143.      Note that if unsafe or reserved characters such as " " or "%" are
  144.      present in <enc_list_mailbox> they MUST be encoded as described in
  145.      RFC 1738 [BASIC-URL].  If the character "/" is present in
  146.      enc_list_mailbox, it SHOULD NOT be encoded.
  147.  
  148.  
  149. 5. Lists of messages
  150.  
  151.      An IMAP URL referring to a list of messages has the following form:
  152.  
  153.          imap://<iserver>/<enc_mailbox>[uidvalidity][?<enc_search>]
  154.  
  155.      The <enc_mailbox> field is used as the argument to the IMAP4
  156.      "SELECT" command.  Note that if unsafe or reserved characters such
  157.      as " ", ";", or "?" are present in <enc_mailbox> they MUST be
  158.      encoded as described in RFC 1738 [BASIC-URL].  If the character "/"
  159.      is present in enc_mailbox, it SHOULD NOT be encoded.
  160.  
  161.      The [uidvalidity] field is optional.  If it is present, it MUST be
  162.      the argument to the IMAP4 UIDVALIDITY status response at the time
  163.      the URL was created.  This SHOULD be used by the program
  164.      interpreting the IMAP URL to determine if the URL is stale.
  165.  
  166.      The [?<enc_search>] field is optional.  If it is not present, the
  167.  
  168.  
  169.  
  170. Newman                                                          [Page 3]
  171.  
  172. Internet Draft              IMAP URL Scheme                     May 1997
  173.  
  174.  
  175.      contents of the mailbox SHOULD be presented by the program
  176.      interpreting the URL.  If it is present, it SHOULD be used as the
  177.      arguments following an IMAP4 SEARCH command with unsafe characters
  178.      such as " " (which are likely to be present in the <enc_search>)
  179.      encoded as described in RFC 1738 [BASIC-URL].
  180.  
  181.  
  182. 6. A specific message or message part
  183.  
  184.      An IMAP URL referring to a specific message or message part has the
  185.      following form:
  186.  
  187.          imap://<iserver>/<enc_mailbox>[uidvalidity]<iuid>[isection]
  188.  
  189.      The <enc_mailbox> and [uidvalidity] are as defined above.
  190.  
  191.      If [uidvalidity] is present in this form, it SHOULD be used by the
  192.      program interpreting the URL to determine if the URL is stale.
  193.  
  194.      The <uid> refers to an IMAP4 message UID, and SHOULD be used as the
  195.      <set> argument to the IMAP4 "UID FETCH" command.
  196.  
  197.      The [isection] field is optional.  If not present, the URL refers
  198.      to the entire Internet message as returned by the IMAP command "UID
  199.      FETCH <uid> BODY.PEEK[]".  If present, the URL refers to the object
  200.      returned by a "UID FETCH <uid> BODY.PEEK[<section>]" command.  The
  201.      type of the object may be determined with a "UID FETCH <uid>
  202.      BODYSTRUCTURE" command and locating the appropriate part in the
  203.      resulting BODYSTRUCTURE.  Note that unsafe characters in
  204.      [isection], such as " " MUST be encoded as described in RFC 1738
  205.      [BASIC-URL].
  206.  
  207.  
  208. 7. Relative IMAP URLs
  209.  
  210.      Relative IMAP URLs are permitted and are resolved according to the
  211.      rules defined in RFC 1808 [REL-URL] with one exception.  In IMAP
  212.      URLs, parameters are treated as part of the normal path with
  213.      respect to relative URL resolution.  This is believed to be the
  214.      behavior of the installed base and is likely to be documented in a
  215.      future revision of the relative URL specification.
  216.  
  217.      The following observations are also important:
  218.  
  219.      The <iauth> grammar element is considered part of the user name for
  220.      purposes of resolving relative IMAP URLs.  This means that unless a
  221.      new login/server specification is included in the relative URL, the
  222.      authentication mechanism is inherited from a base IMAP URL.
  223.  
  224.  
  225.  
  226. Newman                                                          [Page 4]
  227.  
  228. Internet Draft              IMAP URL Scheme                     May 1997
  229.  
  230.  
  231.      URLs always use "/" as the hierarchy delimiter for the purpose of
  232.      resolving paths in relative URLs.  IMAP4 permits the use of any
  233.      hierarchy delimiter in mailbox names.  For this reason, relative
  234.      mailbox paths will only work if the mailbox uses "/" as the
  235.      hierarchy delimiter.  Relative URLs may be used on mailboxes which
  236.      use other delimiters, but in that case, the entire mailbox name
  237.      MUST be specified in the relative URL or inherited as a whole from
  238.      the base URL.
  239.  
  240.      The base URL for a list of mailboxes or messages which was referred
  241.      to by an IMAP URL is always the referring IMAP URL itself.  The
  242.      base URL for a message or message part which was referred to by an
  243.      IMAP URL may be more complicated to determine.  The program
  244.      interpreting the relative URL will have to check the headers of the
  245.      MIME entity and any enclosing MIME entities in order to locate the
  246.      "Content-Base" and "Content-Location" headers.  These headers are
  247.      used to determine the base URL as defined in [HTTP].  For example,
  248.      if the referring IMAP URL contains a "/;SECTION=1.2" parameter,
  249.      then the MIME headers for section 1.2, for section 1, and for the
  250.      enclosing message itself SHOULD be checked in that order for
  251.      "Content-Base" or "Content-Location" headers.
  252.  
  253.  
  254. 8. Multinational Considerations
  255.  
  256.      IMAP4 [IMAP4] section 5.1.3 includes a convention for encoding
  257.      non-US-ASCII characters in IMAP mailbox names.  Because this
  258.      convention is private to IMAP, it is necessary to convert IMAP's
  259.      encoding to one that can be more easily interpreted by a URL
  260.      display program.  For this reason, IMAP's modified UTF-7 encoding
  261.      for mailboxes MUST be converted to UTF-8 [UTF8].  Since 8-bit
  262.      characters are not permitted in URLs, the UTF-8 characters are
  263.      encoded as required by the URL specification [BASIC-URL].  Sample
  264.      code is included in Appendix A to demonstrate this conversion.
  265.  
  266.  
  267. 9. Examples
  268.  
  269.      The following examples demonstrate how an IMAP4 client program
  270.      might translate various IMAP4 URL into a series of IMAP4 commands.
  271.      Commands sent from the client to the server are prefixed with "C:",
  272.      and responses sent from the server to the client are prefixed with
  273.      "S:".
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. Newman                                                          [Page 5]
  283.  
  284. Internet Draft              IMAP URL Scheme                     May 1997
  285.  
  286.  
  287.      The URL:
  288.  
  289.       <imap://minbari.org/gray-council;UIDVALIDITY=385759045/;UID=20>
  290.  
  291.      Results in the following client commands:
  292.  
  293.          <connect to minbari.org, port 143>
  294.          C: A001 LOGIN ANONYMOUS sheridan@babylon5.org
  295.          C: A002 SELECT gray-council
  296.          <client verifies the UIDVALIDITY matches>
  297.          C: A003 UID FETCH 20 BODY.PEEK[]
  298.  
  299.      The URL:
  300.  
  301.       <imap://michael@minbari.org/users.*;type=list>
  302.  
  303.      Results in the following client commands:
  304.  
  305.          <client requests password from user>
  306.          <connect to minbari.org, port 143>
  307.          C: A001 LOGIN MICHAEL zipper
  308.          C: A002 LIST "" users.*
  309.  
  310.      The URL:
  311.  
  312.       <imap://psy.earth/~peter/%E6%97%A5%E6%9C%AC%E8%AA%9E/%E5%8F%B0%E5%8C%97>
  313.  
  314.      Results in the following client commands:
  315.  
  316.          <connect to psy.earth, port 143>
  317.          C: A001 LOGIN ANONYMOUS bester@psycop.psy.earth
  318.          C: A002 SELECT ~peter/&ZeVnLIqe-/&U,BTFw-
  319.          <commands the client uses for viewing the contents of a mailbox>
  320.  
  321.      The URL:
  322.  
  323.       <imap://;AUTH=KERBEROS_V4@minbari.org/gray-council/;uid=20/;section=1.2>
  324.  
  325.      Results in the following client commands:
  326.  
  327.          <connect to minbari.org, port 143>
  328.          C: A001 AUTHENTICATE KERBEROS_V4
  329.          <authentication exchange>
  330.          C: A002 SELECT gray-council
  331.          C: A003 UID FETCH 20 BODY.PEEK[1.2]
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338. Newman                                                          [Page 6]
  339.  
  340. Internet Draft              IMAP URL Scheme                     May 1997
  341.  
  342.  
  343.      If the following relative URL is located in that body part:
  344.  
  345.       <;section=1.4>
  346.  
  347.      This could result in the following client commands:
  348.  
  349.          C: A004 UID FETCH 20 (BODY.PEEK[1.2.MIME]
  350.                  BODY.PEEK[1.MIME]
  351.                  BODY.PEEK[HEADER.FIELDS (Content-Base Content-Location)])
  352.          <Client looks for Content-Base or Content-Location headers in
  353.           result.  If no such headers, then it does the following>
  354.          C: A005 UID FETCH 20 BODY.PEEK[1.4]
  355.  
  356.      The URL:
  357.  
  358.       <imap://;AUTH=*@minbari.org/gray%20council?SUBJECT%20shadows>
  359.  
  360.      Could result in the following:
  361.  
  362.          <connect to minbari.org, port 143>
  363.          C: A001 CAPABILITY
  364.          S: * CAPABILITY IMAP4rev1 AUTH=GSSAPI
  365.          S: A001 OK
  366.          C: A002 AUTHENTICATE GSSAPI
  367.          <authentication exchange>
  368.          S: A002 OK user lennier authenticated
  369.          C: A003 SELECT "gray council"
  370.          ...
  371.          C: A004 SEARCH SUBJECT shadows
  372.          S: * SEARCH 8 10 13 14 15 16
  373.          S: A004 OK SEARCH completed
  374.          C: A005 FETCH 8,10,13:16 ALL
  375.          ...
  376.  
  377.      NOTE: In this final example, the client has implementation dependent
  378.      choices.  The authentication mechanism could be anything, including
  379.      PREAUTH.  And the final FETCH command could fetch more or less
  380.      information about the messages, depending on what it wishes to display
  381.      to the user.
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. Newman                                                          [Page 7]
  395.  
  396. Internet Draft              IMAP URL Scheme                     May 1997
  397.  
  398.  
  399. 10. ABNF for IMAP URL scheme
  400.  
  401.      This uses ABNF as defined in RFC 822 [IMAIL].  Terminals from the
  402.      BNF for IMAP [IMAP] and URLs [BASIC-URL] are also used.  Strings
  403.      are not case sensitive and free insertion of linear-white-space is
  404.      not permitted.
  405.  
  406.      achar            = uchar / "&" / "=" / "~"
  407.                              ; see [BASIC-URL] for "uchar" definition
  408.  
  409.      bchar            = achar / ":" / "@" / "/"
  410.  
  411.      enc_auth_type    = 1*achar
  412.                              ; encoded version of [IMAP-AUTH] "auth_type"
  413.  
  414.      enc_list_mailbox = 1*bchar
  415.                              ; encoded version of [IMAP4] "list_mailbox"
  416.  
  417.      enc_mailbox      = 1*bchar
  418.                              ; encoded version of [IMAP4] "mailbox"
  419.  
  420.      enc_search       = 1*bchar
  421.                              ; encoded version of search_program below
  422.  
  423.      enc_section      = 1*bchar
  424.                              ; encoded version of section below
  425.  
  426.      enc_user         = *achar
  427.                              ; encoded version of [IMAP4] "userid"
  428.  
  429.      imapurl          = "imap://" iserver "/" [ icommand ] [ iauth ]
  430.  
  431.      iauth            = ";AUTH=" ( "*" / enc_auth_type )
  432.  
  433.      icommand         = imailboxlist / ipath / isearch
  434.  
  435.      imailboxlist     = [enc_list_mailbox] [ ";TYPE=" list_type ]
  436.  
  437.      ipath            = enc_mailbox [uidvalidity] iuid [isection]
  438.  
  439.      isearch          = enc_mailbox [ "?" enc_search ] [uidvalidity]
  440.  
  441.      isection         = "/;SECTION=" enc_section
  442.  
  443.      iserver          = [enc_user [ iauth ] "@"] hostport
  444.                              ; See [BASIC-URL] for "hostport" definition
  445.  
  446.  
  447.  
  448.  
  449.  
  450. Newman                                                          [Page 8]
  451.  
  452. Internet Draft              IMAP URL Scheme                     May 1997
  453.  
  454.  
  455.      iuid             = "/;UID=" nz_number
  456.                              ; See [IMAP4] for "nz_number" definition
  457.  
  458.      list_type        = "LIST" / "LSUB"
  459.  
  460.      search_program   = ["CHARSET" SPACE astring SPACE] 1#search_key
  461.                              ; IMAP4 literals may not be used
  462.                              ; See [IMAP4] for "astring" and "search_key"
  463.  
  464.      section          = section_text / (nz_number *["." nz_number]
  465.                          ["." (section_text / "MIME")])
  466.                              ; See [IMAP4] for "section_text" and "nz_number"
  467.  
  468.      uidvalidity      = ";UIDVALIDITY=" nz_number
  469.                              ; See [IMAP4] for "nz_number" definition
  470.  
  471.  
  472. 11. References
  473.  
  474.      [BASIC-URL] Berners-Lee, Masinter, McCahill, "Uniform Resource
  475.      Locators (URL)", RFC 1738, CERN, Xerox Corporation, University of
  476.      Minnesota, December 1994.
  477.  
  478.          <ftp://ds.internic.net/rfc/rfc1738.txt>
  479.  
  480.      [IMAP4] Crispin, M., "Internet Message Access Protocol - Version
  481.      4rev1", RFC 2060, University of Washington, December 1996.
  482.  
  483.          <ftp://ds.internic.net/rfc/rfc2060.txt>
  484.  
  485.      [IMAP-AUTH] Myers, J., "IMAP4 Authentication Mechanism", RFC 1731,
  486.      Carnegie-Mellon University, December 1994.
  487.  
  488.          <ftp://ds.internic.net/rfc/rfc1731.txt>
  489.  
  490.      [HTTP] Fielding, Gettys, Mogul, Frystyk, Berners-Lee, "Hypertext
  491.      Transfer Protocol -- HTTP/1.1", RFC 2068, UC Irvine, DEC, MIT/LCS,
  492.      January 1997.
  493.  
  494.          <ftp://ds.internic.net/rfc/rfc2068.txt>
  495.  
  496.      [IMAIL] Crocker, "Standard for the Format of ARPA Internet Text
  497.      Messages", STD 11, RFC 822, University of Delaware, August 1982.
  498.  
  499.          <ftp://ds.internic.net/rfc/rfc822.txt>
  500.  
  501.  
  502.  
  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. 12. 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. 13. 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. [IMAP] 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.