PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/Platform/Channels/Entities/SourceAddress.cs

http://github.com/waseems/inbox2_desktop
C# | 245 lines | 195 code | 43 blank | 7 comment | 27 complexity | 648af4e90a7370f1fb84363b3b6ba31d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.CodeDom;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Net.Mail;
  8. using System.Runtime.Serialization;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Web;
  12. using System.Xml.Serialization;
  13. using Inbox2.Platform.Channels.Configuration;
  14. using LumiSoft.Net.Mime;
  15. namespace Inbox2.Platform.Channels.Entities
  16. {
  17. [Serializable]
  18. [DataContract]
  19. public class SourceAddress : IComparable, IComparable<SourceAddress>, IEquatable<SourceAddress>
  20. {
  21. private string displayName;
  22. /// <summary>
  23. /// Helper for serialization.
  24. /// </summary>
  25. [DataMember(Order = 1)]
  26. public string SerializedAddress
  27. {
  28. get { return ToString(true); }
  29. set { Parse(value); }
  30. }
  31. [XmlIgnore]
  32. public string Address { get; private set; }
  33. [XmlIgnore]
  34. public string DisplayName
  35. {
  36. get { return displayName; }
  37. private set
  38. {
  39. displayName = value != null && value.StartsWith("=?") ?
  40. MimeUtils.DecodeWords(value) : value;
  41. }
  42. }
  43. [XmlIgnore]
  44. public string AvatarUrl { get; private set; }
  45. [XmlIgnore]
  46. public string ChannelName { get; private set; }
  47. [XmlIgnore]
  48. public string ProfileUrl { get; set; }
  49. public SourceAddress()
  50. {
  51. Address = String.Empty;
  52. DisplayName = String.Empty;
  53. }
  54. public SourceAddress(MailAddress mailaddress)
  55. {
  56. Address = mailaddress.Address;
  57. DisplayName = mailaddress.DisplayName;
  58. }
  59. public SourceAddress(string rawAddress, string displayName, string avatarUrl)
  60. : this(rawAddress, displayName)
  61. {
  62. this.AvatarUrl = avatarUrl;
  63. }
  64. public SourceAddress(string rawAddress, string displayName, string avatarUrl, string profileUrl)
  65. : this(rawAddress, displayName, avatarUrl)
  66. {
  67. this.ProfileUrl = profileUrl;
  68. }
  69. public SourceAddress(string rawAddress, string displayName)
  70. : this(rawAddress)
  71. {
  72. this.DisplayName = displayName;
  73. }
  74. public SourceAddress(string rawAddress)
  75. {
  76. Parse(rawAddress);
  77. }
  78. private void Parse(string rawAddress)
  79. {
  80. if (rawAddress == null)
  81. throw new ArgumentNullException("rawAddress");
  82. rawAddress = rawAddress.Trim();
  83. var extendedPartIndex = rawAddress.IndexOf("|", StringComparison.InvariantCultureIgnoreCase);
  84. if (extendedPartIndex > -1)
  85. {
  86. var extendedPart = rawAddress.Substring(extendedPartIndex);
  87. var parts = extendedPart.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  88. if (parts.Length > 0)
  89. AvatarUrl = parts[0];
  90. if (parts.Length > 1)
  91. ChannelName = parts[1];
  92. // Remove extended part from rawAddress and continue parsing
  93. rawAddress = rawAddress.Substring(0, extendedPartIndex);
  94. }
  95. if (rawAddress.Contains("<") && rawAddress.Contains(">"))
  96. {
  97. string[] parts = rawAddress.Split(new[] {'<', '>'}, StringSplitOptions.RemoveEmptyEntries);
  98. if (parts.Length == 1)
  99. {
  100. this.Address = parts[0];
  101. this.DisplayName = parts[0];
  102. // See if the displayname is quoted, if so, remove the quotes
  103. if (DisplayName.StartsWith("\"") && DisplayName.EndsWith("\""))
  104. DisplayName = DisplayName.Substring(1, DisplayName.Length - 2);
  105. }
  106. if (parts.Length == 2)
  107. {
  108. this.DisplayName = parts[0].Trim();
  109. this.Address = parts[1].Trim();
  110. // Can happen when you receive addresses like " <waseem@sadiq.nl>" (space in the beginning)
  111. if (String.IsNullOrEmpty(this.DisplayName))
  112. this.DisplayName = this.Address;
  113. // See if the displayname is quoted, if so, remove the quotes
  114. if (DisplayName.StartsWith("\"") && DisplayName.EndsWith("\""))
  115. DisplayName = DisplayName.Substring(1, DisplayName.Length - 2);
  116. return;
  117. }
  118. }
  119. else
  120. {
  121. this.DisplayName = rawAddress;
  122. this.Address = rawAddress;
  123. }
  124. if (String.IsNullOrEmpty(Address))
  125. this.Address = rawAddress;
  126. }
  127. public int CompareTo(object obj)
  128. {
  129. return CompareTo((SourceAddress)obj);
  130. }
  131. public int CompareTo(SourceAddress other)
  132. {
  133. return Address.CompareTo(other.Address);
  134. }
  135. public bool Equals(SourceAddress other)
  136. {
  137. return CompareTo(other) == 0;
  138. }
  139. public MailAddress ToMailAddress()
  140. {
  141. return new MailAddress(Address, DisplayName);
  142. }
  143. public void SetChannel(ChannelConfiguration channel)
  144. {
  145. if (channel != null)
  146. ChannelName = channel.DisplayName;
  147. }
  148. public override string ToString()
  149. {
  150. return ToString(false);
  151. }
  152. public string ToEncodedString()
  153. {
  154. return HttpUtility.HtmlEncode(ToString());
  155. }
  156. public string ToString(bool renderProperties)
  157. {
  158. StringBuilder sb = new StringBuilder();
  159. bool hasDisplayName = !String.IsNullOrEmpty(DisplayName) && !DisplayName.Equals(Address);
  160. if (hasDisplayName)
  161. {
  162. sb.Append(DisplayName);
  163. sb.AppendFormat(" <{0}>", Address);
  164. }
  165. else
  166. {
  167. sb.Append(Address);
  168. }
  169. if (renderProperties)
  170. {
  171. sb.Append("|");
  172. if (!String.IsNullOrEmpty(AvatarUrl))
  173. sb.AppendFormat("{0}", AvatarUrl);
  174. sb.Append("|");
  175. if (!String.IsNullOrEmpty(ChannelName))
  176. sb.AppendFormat("{0}", ChannelName);
  177. }
  178. return sb.ToString();
  179. }
  180. public static bool IsValidEmail(string email)
  181. {
  182. string _EmailRegexPattern = @"(['\""]{1,}.+['\""]{1,}\s+)?<?[\w\.\-]+@([\w\-]+\.)+[A-Za-z]{2,}>?";
  183. return Regex.IsMatch(email, _EmailRegexPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  184. }
  185. public static bool IsValidEmail(SourceAddress address)
  186. {
  187. return IsValidEmail(address.Address);
  188. }
  189. public SourceAddressCollection ToList()
  190. {
  191. return new SourceAddressCollection { this };
  192. }
  193. public static SourceAddress Empty
  194. {
  195. get { return new SourceAddress("noreply@inbox2.com", "Inbox2 dummy user"); }
  196. }
  197. }
  198. }