Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System / net / System / Net / mail / MailHeaderInfo.cs
1 using System;
2 using System.Collections.Specialized;
3 using System.Net.Mail;
4 using System.Globalization;
5 using System.Collections.Generic;
6
7 namespace System.Net.Mail {
8
9     // Enumeration of the well-known headers.
10     // If you add to this enum you MUST also add the appropriate initializer in m_HeaderInfo below.
11     internal enum MailHeaderID {
12         Bcc = 0,
13         Cc,
14         Comments,
15         ContentDescription,
16         ContentDisposition,
17         ContentID,
18         ContentLocation,
19         ContentTransferEncoding,
20         ContentType,
21         Date,
22         From,
23         Importance,
24         InReplyTo,
25         Keywords,
26         Max,
27         MessageID,
28         MimeVersion,
29         Priority,
30         References,
31         ReplyTo,
32         ResentBcc,
33         ResentCc,
34         ResentDate,
35         ResentFrom,
36         ResentMessageID,
37         ResentSender,
38         ResentTo,
39         Sender,
40         Subject,
41         To,
42         XPriority,
43         XReceiver,
44         XSender,
45         ZMaxEnumValue = XSender,  // Keep this to equal to the last "known" enum entry if you add to the end
46         Unknown = -1
47     }
48
49     internal static class MailHeaderInfo {
50
51         // Structure that wraps information about a single mail header
52         private struct HeaderInfo {
53             public readonly string NormalizedName;
54             public readonly bool IsSingleton;
55             public readonly MailHeaderID ID;
56             public readonly bool IsUserSettable;
57             public readonly bool AllowsUnicode;
58             public HeaderInfo(MailHeaderID id, string name, bool isSingleton, bool isUserSettable, bool allowsUnicode) {
59                 ID = id;
60                 NormalizedName = name;
61                 IsSingleton = isSingleton;
62                 IsUserSettable = isUserSettable;
63                 AllowsUnicode = allowsUnicode;
64             }
65         }
66
67
68
69         // Table of well-known mail headers.
70         // Keep the initializers in sync with the enum above.
71         private static readonly HeaderInfo[] m_HeaderInfo = {
72             //             ID                                     NormalizedString             IsSingleton      IsUserSettable      AllowsUnicode
73             new HeaderInfo(MailHeaderID.Bcc,                      "Bcc",                       true,            false,              true),
74             new HeaderInfo(MailHeaderID.Cc,                       "Cc",                        true,            false,              true),
75             new HeaderInfo(MailHeaderID.Comments,                 "Comments",                  false,           true,               true),
76             new HeaderInfo(MailHeaderID.ContentDescription,       "Content-Description",       true,            true,               true),
77             new HeaderInfo(MailHeaderID.ContentDisposition,       "Content-Disposition",       true,            true,               true),
78             new HeaderInfo(MailHeaderID.ContentID,                "Content-ID",                true,            false,              false),
79             new HeaderInfo(MailHeaderID.ContentLocation,          "Content-Location",          true,            false,              true),
80             new HeaderInfo(MailHeaderID.ContentTransferEncoding,  "Content-Transfer-Encoding", true,            false,              false),
81             new HeaderInfo(MailHeaderID.ContentType,              "Content-Type",              true,            false,              false),
82             new HeaderInfo(MailHeaderID.Date,                     "Date",                      true,            false,              false),
83             new HeaderInfo(MailHeaderID.From,                     "From",                      true,            false,              true),
84             new HeaderInfo(MailHeaderID.Importance,               "Importance",                true,            false,              false),
85             new HeaderInfo(MailHeaderID.InReplyTo,                "In-Reply-To",               true,            true,               false),
86             new HeaderInfo(MailHeaderID.Keywords,                 "Keywords",                  false,           true,               true),
87             new HeaderInfo(MailHeaderID.Max,                      "Max",                       false,           true,               false),
88             new HeaderInfo(MailHeaderID.MessageID,                "Message-ID",                true,            true,               false),
89             new HeaderInfo(MailHeaderID.MimeVersion,              "MIME-Version",              true,            false,              false),
90             new HeaderInfo(MailHeaderID.Priority,                 "Priority",                  true,            false,              false),
91             new HeaderInfo(MailHeaderID.References,               "References",                true,            true,               false),
92             new HeaderInfo(MailHeaderID.ReplyTo,                  "Reply-To",                  true,            false,              true),
93             new HeaderInfo(MailHeaderID.ResentBcc,                "Resent-Bcc",                false,           true,               true),
94             new HeaderInfo(MailHeaderID.ResentCc,                 "Resent-Cc",                 false,           true,               true),
95             new HeaderInfo(MailHeaderID.ResentDate,               "Resent-Date",               false,           true,               false),
96             new HeaderInfo(MailHeaderID.ResentFrom,               "Resent-From",               false,           true,               true),
97             new HeaderInfo(MailHeaderID.ResentMessageID,          "Resent-Message-ID",         false,           true,               false),
98             new HeaderInfo(MailHeaderID.ResentSender,             "Resent-Sender",             false,           true,               true),
99             new HeaderInfo(MailHeaderID.ResentTo,                 "Resent-To",                 false,           true,               true),
100             new HeaderInfo(MailHeaderID.Sender,                   "Sender",                    true,            false,              true),
101             new HeaderInfo(MailHeaderID.Subject,                  "Subject",                   true,            false,              true),
102             new HeaderInfo(MailHeaderID.To,                       "To",                        true,            false,              true),
103             new HeaderInfo(MailHeaderID.XPriority,                "X-Priority",                true,            false,              false),
104             new HeaderInfo(MailHeaderID.XReceiver,                "X-Receiver",                false,           true,               true),
105             new HeaderInfo(MailHeaderID.XSender,                  "X-Sender",                  true,            true,               true)
106         };
107
108         private static readonly Dictionary<string, int> m_HeaderDictionary;
109
110         static MailHeaderInfo() {
111
112 #if DEBUG
113             // Check that enum and header info array are in sync
114             for(int i = 0; i < m_HeaderInfo.Length; i++) {
115                 if((int)m_HeaderInfo[i].ID != i) {
116                     throw new Exception("Header info data structures are not in sync");
117                 }
118             }
119 #endif
120
121             // Create dictionary for string-to-enum lookup.  Ordinal and IgnoreCase are intentional.
122             m_HeaderDictionary = new Dictionary<string, int>((int)MailHeaderID.ZMaxEnumValue + 1, StringComparer.OrdinalIgnoreCase);
123             for(int i = 0; i < m_HeaderInfo.Length; i++) {
124                 m_HeaderDictionary.Add(m_HeaderInfo[i].NormalizedName, i);
125             }
126         }
127
128         internal static string GetString(MailHeaderID id) {
129             switch(id) {
130                 case MailHeaderID.Unknown:
131                 case MailHeaderID.ZMaxEnumValue+1:
132                     return null;
133                 default:
134                     return m_HeaderInfo[(int)id].NormalizedName;
135             }
136         }
137
138         internal static MailHeaderID GetID(string name) {
139             int id;
140             if(m_HeaderDictionary.TryGetValue(name, out id)) {
141                 return (MailHeaderID)id;
142             }
143             return MailHeaderID.Unknown;
144         }
145
146         internal static bool IsWellKnown(string name) {
147             int dummy;
148             return m_HeaderDictionary.TryGetValue(name, out dummy);
149         }
150
151         internal static bool IsUserSettable(string name) {
152             int index;
153             if (m_HeaderDictionary.TryGetValue(name, out index)) {
154                 return m_HeaderInfo[index].IsUserSettable;
155             }
156             //values not in the list of well-known headers are always user-settable
157             return true;
158         }
159
160         internal static bool IsSingleton(string name) {
161             int index;
162             if(m_HeaderDictionary.TryGetValue(name, out index)) {
163                 return m_HeaderInfo[index].IsSingleton;
164             }
165             return false;
166         }
167
168         internal static string NormalizeCase(string name) {
169             int index;
170             if(m_HeaderDictionary.TryGetValue(name, out index)) {
171                 return m_HeaderInfo[index].NormalizedName;
172             }
173             return name;
174         }
175
176         internal static bool IsMatch(string name, MailHeaderID header) {
177             int index;
178             if(m_HeaderDictionary.TryGetValue(name, out index) && (MailHeaderID)index == header) {
179                 return true;
180             }
181             return false;
182         }
183
184         internal static bool AllowsUnicode(string name) {
185             int index;
186             if (m_HeaderDictionary.TryGetValue(name, out index)) {
187                 return m_HeaderInfo[index].AllowsUnicode;
188             }
189             return true;
190         }
191     }
192 }