PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Rusty/Core/Network.cs

http://github.com/polyethene/IronAHK
C# | 217 lines | 142 code | 33 blank | 42 comment | 23 complexity | e10826c0bfa4903a352059070b2c1ca2 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Cache;
  8. using System.Net.Mail;
  9. namespace IronAHK.Rusty
  10. {
  11. partial class Core
  12. {
  13. /// <summary>
  14. /// Resolves a host name or IP address.
  15. /// </summary>
  16. /// <param name="name">The host name.</param>
  17. /// <returns>A dictionary with the following key/value pairs:
  18. /// <list type="bullet">
  19. /// <item><term>Host</term>: <description>the host name.</description></item>
  20. /// <item><term>Addresses</term>: <description>the list of IP addresses.</description></item>
  21. /// </list>
  22. /// </returns>
  23. public static Dictionary<string, object> GetHostEntry(string name)
  24. {
  25. var entry = Dns.GetHostEntry(name);
  26. var ips = new string[entry.AddressList.Length];
  27. for (int i = 0; i < ips.Length; i++)
  28. ips[i] = entry.AddressList[0].ToString();
  29. var info = new Dictionary<string, object>();
  30. info.Add("Host", entry.HostName);
  31. info.Add("Addresses", ips);
  32. return info;
  33. }
  34. /// <summary>
  35. /// Sends an email.
  36. /// </summary>
  37. /// <param name="recipients">A list of receivers of the message.</param>
  38. /// <param name="subject">Subject of the message.</param>
  39. /// <param name="message">Message body.</param>
  40. /// <param name="options">A dictionary with any the following optional key/value pairs:
  41. /// <list type="bullet">
  42. /// <item><term>Attachments</term>: <description>a list of file paths to send as attachments.</description></item>
  43. /// <item><term>Bcc</term>: <description>a list of blind carbon copy recipients.</description></item>
  44. /// <item><term>CC</term>: <description>a list of carbon copy recipients.</description></item>
  45. /// <item><term>From</term>: <description>the from address.</description></item>
  46. /// <item><term>ReplyTo</term>: <description>the reply address.</description></item>
  47. /// <item><term>Host</term>: <description>the SMTP client hostname and port.</description></item>
  48. /// <item><term>(Header)</term>: <description>any additional header and corresponding value.</description></item>
  49. /// </list>
  50. /// </param>
  51. /// <remarks><see cref="ErrorLevel"/> is set to <c>1</c> if there was a problem, <c>0</c> otherwise.</remarks>
  52. public static void Mail(object recipients, string subject, string message, IDictionary options = null)
  53. {
  54. ErrorLevel = 1;
  55. var msg = new MailMessage { Subject = subject, Body = message };
  56. msg.From = new MailAddress(string.Concat(Environment.UserName, "@", Environment.UserDomainName));
  57. if (recipients is string)
  58. msg.To.Add(new MailAddress((string)recipients));
  59. else if (recipients is IEnumerable)
  60. {
  61. foreach (var item in (IEnumerable)recipients)
  62. if (!string.IsNullOrEmpty(item as string))
  63. msg.To.Add((string)item);
  64. }
  65. else
  66. return;
  67. var smtpHost = "localhost";
  68. int? smtpPort = null;
  69. if (options == null)
  70. goto send;
  71. #region Options
  72. foreach (var key in options.Keys)
  73. {
  74. var item = key as string;
  75. if (string.IsNullOrEmpty(item))
  76. continue;
  77. string[] value;
  78. if (options[key] is string)
  79. value = new[] { (string)options[key] };
  80. else if (options[key] is string[])
  81. value = (string[])options[key];
  82. else if (options[key] is object[])
  83. {
  84. var block = (object[])options[key];
  85. value = new string[block.Length];
  86. for (int i = 0; i < block.Length; i++)
  87. value[i] = block[i] as string;
  88. }
  89. else
  90. continue;
  91. switch (item.ToLowerInvariant())
  92. {
  93. case Keyword_Attachments:
  94. foreach (var entry in value)
  95. if (File.Exists(entry))
  96. msg.Attachments.Add(new Attachment(entry));
  97. break;
  98. case Keyword_Bcc:
  99. foreach (var entry in value)
  100. msg.Bcc.Add(entry);
  101. break;
  102. case Keyword_CC:
  103. foreach (var entry in value)
  104. msg.CC.Add(entry);
  105. break;
  106. case Keyword_From:
  107. msg.From = new MailAddress(value[0]);
  108. break;
  109. case Keyword_ReplyTo:
  110. msg.ReplyTo = new MailAddress(value[0]);
  111. break;
  112. case Keyword_Host:
  113. {
  114. smtpHost = value[0];
  115. var z = smtpHost.LastIndexOf(Keyword_Port);
  116. if (z != -1)
  117. {
  118. var port = smtpHost.Substring(z + 1);
  119. smtpHost = smtpHost.Substring(0, z);
  120. int n;
  121. if (int.TryParse(port, out n))
  122. smtpPort = n;
  123. }
  124. }
  125. break;
  126. default:
  127. msg.Headers.Add(item, value[0]);
  128. break;
  129. }
  130. }
  131. #endregion
  132. send:
  133. var client = smtpPort == null ? new SmtpClient(smtpHost) : new SmtpClient(smtpHost, (int)smtpPort);
  134. try
  135. {
  136. client.Send(msg);
  137. ErrorLevel = 0;
  138. }
  139. catch (SmtpException)
  140. {
  141. if (Debug)
  142. throw;
  143. }
  144. }
  145. /// <summary>
  146. /// Downloads a resource from the internet to a file.
  147. /// </summary>
  148. /// <param name="url">The URL from which to download data.</param>
  149. /// <param name="filename">The file path to receive the data.
  150. /// Any existing file will be overwritten.</param>
  151. [Obsolete]
  152. public static void URLDownloadToFile(string url, string filename)
  153. {
  154. UriDownload(url, filename);
  155. }
  156. /// <summary>
  157. /// Downloads a resource from the internet.
  158. /// </summary>
  159. /// <param name="address">The URI (or URL) of the resource.</param>
  160. /// <param name="filename">The file path to receive the downloaded data. An existing file will be overwritten.
  161. /// Leave blank to return the data as a string.
  162. /// </param>
  163. /// <returns>The downloaded data if <paramref name="filename"/> is blank, otherwise an empty string.</returns>
  164. public static string UriDownload(string address, string filename = null)
  165. {
  166. ErrorLevel = 0;
  167. var flags = ParseFlags(ref address);
  168. var http = new WebClient();
  169. if (flags.Contains("0"))
  170. http.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
  171. try
  172. {
  173. if (string.IsNullOrEmpty(filename))
  174. return http.DownloadString(address);
  175. http.DownloadFile(address, filename);
  176. }
  177. catch (WebException)
  178. {
  179. ErrorLevel = 1;
  180. }
  181. return string.Empty;
  182. }
  183. }
  184. }