using System; using System.Collections.Generic; using System.IO; namespace Ant.Service.Utilities { /// /// This class represents both the multiline and single line Pop3 LIST command. /// internal sealed class ListCommand : Pop3Command { // the id of the message on the server to retrieve. int _messageId; public ListCommand(Stream stream) : base(stream, true, Pop3State.Transaction) { } /// /// Initializes a new instance of the class. /// /// The stream. /// The message id. public ListCommand(Stream stream, int messageId) : this(stream) { if (messageId < 0) { throw new ArgumentOutOfRangeException("messageId"); } _messageId = messageId; base.IsMultiline = false; } /// /// Creates the LIST request message. /// /// The byte[] containing the LIST request message. protected override byte[] CreateRequestMessage() { string requestMessage = Pop3Commands.List; if (!IsMultiline) { requestMessage += _messageId.ToString(); } // Append the message id to perform the LIST command for. return GetRequestMessage(requestMessage, Pop3Commands.Crlf); } /// /// Creates the response. /// /// The buffer. /// A ListResponse containing the results of the Pop3 LIST command. protected override ListResponse CreateResponse(byte[] buffer) { Pop3Response response = Pop3Response.CreateResponse(buffer); List items; if (IsMultiline) { items = new List(); string[] values; string[] lines = GetResponseLines(StripPop3HostMessage(buffer, response.HostMessage)); foreach (string line in lines) { //each line should consist of 'n m' where n is the message number and m is the number of octets values = line.Split(' '); if (values.Length < 2) { throw new Pop3Exception(string.Concat("Invalid line in multiline response: ", line)); } items.Add(new Pop3ListItem(Convert.ToInt32(values[0]), Convert.ToInt64(values[1]))); } } //Parse the multiline response. else { items = new List(1); string[] values = response.HostMessage.Split(' '); //should consist of '+OK messageNumber octets' if (values.Length < 3) { throw new Pop3Exception(string.Concat("Invalid response message: ", response.HostMessage)); } items.Add(new Pop3ListItem(Convert.ToInt32(values[1]), Convert.ToInt64(values[2]))); } //Parse the single line results. return new ListResponse(response, items); } } }