Ejemplo n.º 1
0
        protected override CompletionResponse Behaviour()
        {
            MessageDescriptionParser parser  = new MessageDescriptionParser();
            StringBuilder            builder = new StringBuilder();
            int           num         = 0;
            List <string> subPartList = (this._partDescription == null ? new List <string>()
            {
                ""
            } : this.GetSubPartList(this._partDescription));

            for (int i = 0; i < subPartList.Count; i++)
            {
                if (i > 0)
                {
                    builder.Append(" ");
                }
                builder.Append(string.Format("BODY.PEEK[{0}]", subPartList[i]));
            }
            uint          commandId = base._dispatcher.SendCommand(string.Format("UID FETCH {0} ({1})", this._message.Uid, builder), base.filter);
            IMAP4Response response  = base._dispatcher.GetResponse(commandId);

            if (!response.IsCompletionResponse())
            {
                if (response.Name != "FETCH")
                {
                    throw new UnexpectedResponseException("Unexpected response");
                }
                string input = response.Data.Substring(response.Data.IndexOf("(") + 1);
                while (num < subPartList.Count)
                {
                    Regex regex = new Regex(@"[\}]*{(?<size>[0-9]+)}|(?<size>NIL)");
                    if (!regex.IsMatch(input))
                    {
                        throw new Exception("Unexpected response");
                    }
                    string s = regex.Match(input).Groups["size"].Value;
                    num++;
                    if (!s.Equals("NIL", StringComparison.OrdinalIgnoreCase))
                    {
                        ulong  size             = ulong.Parse(s);
                        byte[] rawData          = base._dispatcher.GetRawData(size);
                        byte[] bytes            = Encoding.UTF8.GetBytes(input + "\r\n");
                        byte[] destinationArray = new byte[bytes.Length + rawData.Length];
                        Array.Copy(bytes, destinationArray, bytes.Length);
                        Array.Copy(rawData, 0, destinationArray, bytes.Length, rawData.Length);
                        this._message = parser.Parse(this._message, destinationArray, this._message.Uid, this._attachmentDirectory);
                        byte[] rawDataBytes = base._dispatcher.GetRawData();
                        input = Encoding.UTF8.GetString(rawDataBytes, 0, rawDataBytes.Length);
                    }
                }
                response = base._dispatcher.GetResponse(commandId);
            }
            return(new CompletionResponse(response.Response));
        }
Ejemplo n.º 2
0
        protected override CompletionResponse Behaviour()
        {
            uint          commandId = base._dispatcher.SendCommand(string.Format("{0}FETCH {1} (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER] BODY)", this._uidMode ? "UID " : "", this._sequence), base.filter);
            IMAP4Response response  = base._dispatcher.GetResponse(commandId);

            if (!response.IsCompletionResponse())
            {
                if (response.Name != "FETCH")
                {
                    throw new UnexpectedResponseException("Unexpected response");
                }
                while (!response.IsCompletionResponse())
                {
                    try
                    {
                        List <byte> list = new List <byte>();
                        if (!response.IsCompletionResponse())
                        {
                            ulong size = base.GetSize(response);
                            list.AddRange(Encoding.UTF8.GetBytes(response.Data + "\r\n"));
                            list.AddRange(base._dispatcher.GetRawData(size));
                            list.AddRange(base._dispatcher.GetRawData());
                            int  sourceIndex = -1;
                            int  num4        = -1;
                            byte num5        = Encoding.UTF8.GetBytes("(")[0];
                            byte num6        = Encoding.UTF8.GetBytes(")")[0];
                            for (int i = 0; i < list.Count; i++)
                            {
                                if ((sourceIndex == -1) && (list[i] == num5))
                                {
                                    sourceIndex = i;
                                }
                                if (list[i] == num6)
                                {
                                    num4 = i;
                                }
                            }
                            sourceIndex++;
                            byte[] destinationArray = new byte[num4 - sourceIndex];
                            Array.Copy(list.ToArray(), sourceIndex, destinationArray, 0, num4 - sourceIndex);
                            ImapMessage item = new MessageDescriptionParser().Parse(destinationArray, _sequence[0].ToString(), this._attachmentDirectory);
                            this._fetchedItems.Add(item);
                            if (this.MessageReceived != null)
                            {
                                this.MessageReceived(this, new MessageReceivedEventArgs(item));
                            }
                        }
                    }
                    catch (BadAttachmentDirectoryException)
                    {
                        throw;
                    }
                    catch (Exception exception)
                    {
                        if (this.BrokenMessage != null)
                        {
                            this.BrokenMessage(this, new BrokenMessageInfoArgs(exception.Message));
                        }
                    }
                    response = base._dispatcher.GetResponse(commandId);
                }
            }
            return(new CompletionResponse(response.Response));
        }