ListCommand.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Ant.Service.Utilities
  5. {
  6. /// <summary>
  7. /// This class represents both the multiline and single line Pop3 LIST command.
  8. /// </summary>
  9. internal sealed class ListCommand : Pop3Command<ListResponse>
  10. {
  11. // the id of the message on the server to retrieve.
  12. int _messageId;
  13. public ListCommand(Stream stream)
  14. : base(stream, true, Pop3State.Transaction)
  15. {
  16. }
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="ListCommand"/> class.
  19. /// </summary>
  20. /// <param name="stream">The stream.</param>
  21. /// <param name="messageId">The message id.</param>
  22. public ListCommand(Stream stream, int messageId)
  23. : this(stream)
  24. {
  25. if (messageId < 0)
  26. {
  27. throw new ArgumentOutOfRangeException("messageId");
  28. }
  29. _messageId = messageId;
  30. base.IsMultiline = false;
  31. }
  32. /// <summary>
  33. /// Creates the LIST request message.
  34. /// </summary>
  35. /// <returns>The byte[] containing the LIST request message.</returns>
  36. protected override byte[] CreateRequestMessage()
  37. {
  38. string requestMessage = Pop3Commands.List;
  39. if (!IsMultiline)
  40. {
  41. requestMessage += _messageId.ToString();
  42. } // Append the message id to perform the LIST command for.
  43. return GetRequestMessage(requestMessage, Pop3Commands.Crlf);
  44. }
  45. /// <summary>
  46. /// Creates the response.
  47. /// </summary>
  48. /// <param name="buffer">The buffer.</param>
  49. /// <returns>A <c>ListResponse</c> containing the results of the Pop3 LIST command.</returns>
  50. protected override ListResponse CreateResponse(byte[] buffer)
  51. {
  52. Pop3Response response = Pop3Response.CreateResponse(buffer);
  53. List<Pop3ListItem> items;
  54. if (IsMultiline)
  55. {
  56. items = new List<Pop3ListItem>();
  57. string[] values;
  58. string[] lines = GetResponseLines(StripPop3HostMessage(buffer, response.HostMessage));
  59. foreach (string line in lines)
  60. {
  61. //each line should consist of 'n m' where n is the message number and m is the number of octets
  62. values = line.Split(' ');
  63. if (values.Length < 2)
  64. {
  65. throw new Pop3Exception(string.Concat("Invalid line in multiline response: ", line));
  66. }
  67. items.Add(new Pop3ListItem(Convert.ToInt32(values[0]),
  68. Convert.ToInt64(values[1])));
  69. }
  70. } //Parse the multiline response.
  71. else
  72. {
  73. items = new List<Pop3ListItem>(1);
  74. string[] values = response.HostMessage.Split(' ');
  75. //should consist of '+OK messageNumber octets'
  76. if (values.Length < 3)
  77. {
  78. throw new Pop3Exception(string.Concat("Invalid response message: ", response.HostMessage));
  79. }
  80. items.Add(new Pop3ListItem(Convert.ToInt32(values[1]), Convert.ToInt64(values[2])));
  81. } //Parse the single line results.
  82. return new ListResponse(response, items);
  83. }
  84. }
  85. }