[HttpListenre] Avoid multiple calls to Close()
[mono.git] / mcs / class / System / System.Net.Mail / MailAddress.cs
1 //
2 // System.Net.Mail.MailAddress.cs
3 //
4 // Author:
5 //      Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System.Text;
34
35 namespace System.Net.Mail {
36         public class MailAddress 
37         {
38                 #region Fields
39
40                 string address;
41                 string displayName;
42                 string host;
43                 string user;
44                 string to_string;
45                 //Encoding displayNameEncoding;
46
47                 #endregion // Fields
48
49                 #region Constructors
50
51                 public MailAddress (string address) : this (address, null)
52                 {
53                 }
54
55                 public MailAddress (string address, string displayName) : this (address, displayName, Encoding.UTF8)
56                 {
57                 }
58
59                 [MonoTODO ("We don't do anything with displayNameEncoding")]
60                 public MailAddress (string address, string displayName, Encoding displayNameEncoding)
61                 {
62                         if (address == null)
63                                 throw new ArgumentNullException ("address");
64                         if (address.Length == 0)
65                                 throw new ArgumentException ("address");
66
67                         if (displayName != null)
68                                 this.displayName = displayName.Trim ();
69                         ParseAddress (address);
70                 }
71
72                 void ParseAddress (string address)
73                 {
74                         // 1. Quotes for display name
75                         address = address.Trim ();
76                         int idx = address.IndexOf ('"');
77                         if (idx != -1) {
78                                 if (idx != 0 || address.Length == 1)
79                                         throw CreateFormatException ();
80
81                                 int closing = address.LastIndexOf ('"');
82                                 if (closing == idx)
83                                         throw CreateFormatException ();
84
85                                 if (this.displayName == null)
86                                         this.displayName = address.Substring (idx + 1, closing - idx - 1).Trim ();
87                                 address = address.Substring (closing + 1).Trim ();
88                         }
89
90                         // 2. <email>
91                         idx = address.IndexOf ('<');
92                         if (idx >= 0) {
93                                 if (this.displayName == null)
94                                         this.displayName = address.Substring (0, idx).Trim ();
95                                 if (address.Length - 1 == idx)
96                                         throw CreateFormatException ();
97
98                                 int end = address.IndexOf ('>', idx + 1);
99                                 if (end == -1)
100                                         throw CreateFormatException ();
101
102                                 address = address.Substring (idx + 1, end - idx - 1).Trim ();
103                         }
104                         this.address = address;
105                         // 3. email
106                         idx = address.IndexOf ('@');
107                         if (idx <= 0)
108                                 throw CreateFormatException ();
109                         if (idx != address.LastIndexOf ('@'))
110                                 throw CreateFormatException ();
111
112                         this.user = address.Substring (0, idx).Trim ();
113                         if (user.Length == 0)
114                                 throw CreateFormatException ();
115                         this.host = address.Substring (idx + 1).Trim ();
116                         if (host.Length == 0)
117                                 throw CreateFormatException ();
118                 }
119
120                 #endregion // Constructors
121
122 #region Properties
123
124                 public string Address {
125                         get { return address; }
126                 }
127
128                 public string DisplayName {
129                         get {
130                                 if (displayName == null)
131                                         return string.Empty;
132                                 return displayName;
133                         }
134                 }
135
136                 public string Host {
137                         get { return host; }
138                 }
139
140                 public string User {
141                         get { return user; }
142                 }
143
144 #endregion // Properties
145
146 #region Methods
147                 
148                 public override bool Equals (object obj)
149                 {
150                         if (obj == null)
151                                 return false;
152
153                         return (0 == String.Compare (ToString (), obj.ToString (), StringComparison.OrdinalIgnoreCase));
154                 }
155
156                 public override int GetHashCode ()
157                 {
158                         return ToString ().GetHashCode ();
159                 }
160
161                 public override string ToString ()
162                 {
163                         if (to_string != null)
164                                 return to_string;
165
166                         if (!String.IsNullOrEmpty (displayName))
167                                 to_string = String.Format ("\"{0}\" <{1}>", DisplayName, Address);
168                         else
169                                 to_string = address;
170
171                         return to_string;
172                 }
173
174                 private static FormatException CreateFormatException () {
175                         return new FormatException ("The specified string is not in the "
176                                 + "form required for an e-mail address.");
177                 }
178
179 #endregion // Methods
180         }
181 }
182
183 #endif // NET_2_0