Pop3Response.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.IO;
  3. namespace Ant.Service.Utilities
  4. {
  5. /// <summary>
  6. /// This class represents a Pop3 response message and
  7. /// is intended to be used as a base class for all other
  8. /// Pop3Response types.
  9. /// </summary>
  10. internal class Pop3Response
  11. {
  12. private byte[] _responseContents;
  13. /// <summary>
  14. /// Gets the response contents.
  15. /// </summary>
  16. /// <value>The response contents.</value>
  17. internal byte[] ResponseContents
  18. {
  19. get
  20. {
  21. return _responseContents;
  22. }
  23. }
  24. private bool _statusIndicator;
  25. /// <summary>
  26. /// Gets a value indicating whether message was <c>true</c> +OK or <c>false</c> -ERR
  27. /// </summary>
  28. /// <value><c>true</c> if [status indicator]; otherwise, <c>false</c>.</value>
  29. public bool StatusIndicator
  30. {
  31. get { return _statusIndicator; }
  32. }
  33. private string _hostMessage;
  34. /// <summary>
  35. /// Gets the host message.
  36. /// </summary>
  37. /// <value>The host message.</value>
  38. public string HostMessage
  39. {
  40. get { return _hostMessage; }
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Pop3Response"/> class.
  44. /// </summary>
  45. /// <param name="responseContents">The response contents.</param>
  46. /// <param name="hostMessage">The host message.</param>
  47. /// <param name="statusIndicator">if set to <c>true</c> [status indicator].</param>
  48. public Pop3Response(byte[] responseContents, string hostMessage, bool statusIndicator)
  49. {
  50. if (responseContents == null)
  51. {
  52. throw new ArgumentNullException("responseBuffer");
  53. }
  54. if (string.IsNullOrEmpty(hostMessage))
  55. {
  56. throw new ArgumentNullException("hostMessage");
  57. }
  58. _responseContents = responseContents;
  59. _hostMessage = hostMessage;
  60. _statusIndicator = statusIndicator;
  61. }
  62. /// <summary>
  63. /// Creates the response.
  64. /// </summary>
  65. /// <param name="responseContents">The response contents.</param>
  66. /// <returns></returns>
  67. public static Pop3Response CreateResponse(byte[] responseContents)
  68. {
  69. string hostMessage;
  70. MemoryStream stream = new MemoryStream(responseContents);
  71. using (StreamReader reader = new StreamReader(stream))
  72. {
  73. hostMessage = reader.ReadLine();
  74. if (hostMessage == null)
  75. {
  76. return null;
  77. }
  78. bool success = hostMessage.StartsWith(Pop3Responses.Ok);
  79. return new Pop3Response(responseContents, hostMessage, success);
  80. }
  81. }
  82. }
  83. }