Exemplo n.º 1
0
        protected override CompletionResponse Behaviour()
        {
            uint          commandId = base._dispatcher.SendCommand(string.Format("APPEND \"{0}\" {1}{2}{3}", this.mailbox, "{" + message.Length + "}", Environment.NewLine, message));
            IMAP4Response response  = null;

            try
            {
                response = base._dispatcher.GetResponse(commandId);
            }
            catch
            {
                base._dispatcher.SendCommand(this.message);
                response = base._dispatcher.GetResponse(commandId);
            }
            if (!response.IsCompletionResponse())
            {
                throw new UnexpectedResponseException("Unexpected response");
            }
            return(new CompletionResponse(response.Response));
        }
Exemplo n.º 2
0
        public virtual IMAP4Response GetResponse(uint commandId)
        {
            IMAP4Response response;
            bool          flag = false;

            do
            {
                if (this._queues[commandId].Count == 0)
                {
                    lock (this)
                    {
                        if (!this._receiveInProgress)
                        {
                            this.GetServerData();
                        }
                    }
                    if (this._receiveInProgress)
                    {
                        Task.Run(async() => await Task.Delay(this._sleepTime)).Wait();
                    }
                }
                else
                {
                    flag = true;
                }
            }while (!flag);
            lock (this)
            {
                response = new IMAP4Response(this._queues[commandId].Dequeue().Replace("\r\n", ""));
            }
            if (response.IsCompletionResponse())
            {
                this.DeleteCommand(commandId);
            }
            return(response);
        }
Exemplo n.º 3
0
        protected override CompletionResponse Behaviour()
        {
            Exception     exception        = null;
            IPart         partByAttachment = this._message.GetPartByAttachment(this._attachmentDescription);
            uint          commandId        = base._dispatcher.SendCommand(string.Format("UID FETCH {0} (BODY.PEEK[{1}] BODY.PEEK[{1}.MIME])", this._message.Uid, this._message.GetPartIndex(partByAttachment)), base.filter);
            IMAP4Response response         = base._dispatcher.GetResponse(commandId);

            while (!response.IsCompletionResponse())
            {
                if (response.Name != "FETCH")
                {
                    throw new UnexpectedResponseException("Unexpected response");
                }
                ulong             size   = base.GetSize(response);
                FileContentWriter writer = new FileContentWriter(this._attachmentDirectory);
                try
                {
                    writer.Open();
                    string str = string.Empty;
                    while (size > 0L)
                    {
                        ulong  num3  = (size > 0x1000L) ? ((ulong)0x1000L) : size;
                        byte[] bytes = base._dispatcher.GetRawData(num3);
                        byte[] data  = bytes;
                        if (partByAttachment.Header.ContentTransferEncoding == EContentTransferEncoding.Base64)
                        {
                            string str2 = str + Encoding.UTF8.GetString(bytes, 0, bytes.Length).Replace("\r\n", "");
                            data = Convert.FromBase64String(str2.Substring(0, str2.Length - (str2.Length % 4)));
                            str  = str2.Substring(str2.Length - (str2.Length % 4));
                        }
                        writer.Write(data);
                        size -= num3;
                    }
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    base._dispatcher.GetRawData(size);
                }
                finally
                {
                    writer.Close();
                }
                this._attachmentDescription.DiskFilename = writer.Filename;
                byte[] rawData = base._dispatcher.GetRawData();
                string s       = Encoding.UTF8.GetString(rawData, 0, rawData.Length) + "\r\n";
                rawData = Encoding.UTF8.GetBytes(s);
                Match match = new Regex("{(?<size>[0-9]+)}|(?<size>NIL)").Match(s);
                if (match.Success && (match.Groups["size"].Value != "NIL"))
                {
                    size = ulong.Parse(match.Groups["size"].Value);
                    byte[] sourceArray      = base._dispatcher.GetRawData(size);
                    byte[] destinationArray = new byte[rawData.Length + sourceArray.Length];
                    Array.Copy(rawData, destinationArray, rawData.Length);
                    Array.Copy(sourceArray, 0, destinationArray, rawData.Length, sourceArray.Length);
                    this._message = new MessageDescriptionParser().Parse(this._message, destinationArray, this._message.Uid, this._attachmentDirectory);
                    base._dispatcher.GetRawData();
                }
                response = base._dispatcher.GetResponse(commandId);
            }
            if (exception != null)
            {
                throw exception;
            }
            return(new CompletionResponse(response.Response));
        }
Exemplo n.º 4
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));
        }