Exemplo n.º 1
0
        /// <summary>
        /// Gets specified folder quota info. Throws Exception if server doesn't support QUOTA.
        /// </summary>
        /// <param name="folder">Folder name.</param>
        /// <returns>Returns specified folder quota info.</returns>
        /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception>
        /// <exception cref="InvalidOperationException">Is raised when IMAP client is not connected and authenticated.</exception>
        public IMAP_Quota GetFolderQuota(string folder)
        {
            /* RFC 2087 4.3. GETQUOTAROOT Command

                    Arguments:  mailbox name

                    Data:       untagged responses: QUOTAROOT, QUOTA

                    Result:     OK - getquota completed
                                NO - getquota error: no such mailbox, permission denied
                                BAD - command unknown or arguments invalid

               The GETQUOTAROOT command takes the name of a mailbox and returns the
               list of quota roots for the mailbox in an untagged QUOTAROOT
               response.  For each listed quota root, it also returns the quota
               root's resource usage and limits in an untagged QUOTA response.

                   Example:    C: A003 GETQUOTAROOT INBOX
                               S: * QUOTAROOT INBOX ""
                               S: * QUOTA "" (STORAGE 10 512)
                               S: A003 OK Getquota completed
            */

            if (this.IsDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }
            if (!this.IsConnected)
            {
                throw new InvalidOperationException("You must connect first.");
            }
            if (!this.IsAuthenticated)
            {
                throw new InvalidOperationException("The command is only valid in authenticated state.");
            }

            IMAP_Quota retVal = null;

            // Ensure that we send right separator to server, we accept both \ and /.
            folder = folder.Replace('\\', this.PathSeparator).Replace('/', this.PathSeparator);

            string line = GetNextCmdTag() + " GETQUOTAROOT " + TextUtils.QuoteString(Core.Encode_IMAP_UTF7_String(folder));
            int countWritten = this.TcpStream.WriteLine(line);
            LogAddWrite(countWritten, line);

            // Must get lines with * and cmdTag + OK or cmdTag BAD/NO.
            while (true)
            {
                line = this.ReadLine();

                if (line.StartsWith("*"))
                {
                    // Get rid of *
                    line = line.Substring(1).Trim();

                    if (line.ToUpper().StartsWith("QUOTAROOT"))
                    {
                        // Skip QUOTAROOT
                    }
                    else if (line.ToUpper().StartsWith("QUOTA"))
                    {
                        StringReader r = new StringReader(line);
                        // Skip QUOTA word
                        r.ReadWord();

                        string qoutaRootName = r.ReadWord();
                        long storage = -1;
                        long maxStorage = -1;
                        long messages = -1;
                        long maxMessages = -1;

                        string limits = r.ReadParenthesized();
                        r = new StringReader(limits);
                        while (r.Available > 0)
                        {
                            string limitName = r.ReadWord();
                            // STORAGE usedBytes maximumAllowedBytes
                            if (limitName.ToUpper() == "STORAGE")
                            {
                                storage = Convert.ToInt64(r.ReadWord());
                                maxStorage = Convert.ToInt64(r.ReadWord());
                            }
                            // STORAGE messagesCount maximumAllowedMessages
                            else if (limitName.ToUpper() == "MESSAGE")
                            {
                                messages = Convert.ToInt64(r.ReadWord());
                                maxMessages = Convert.ToInt64(r.ReadWord());
                            }
                        }

                        retVal = new IMAP_Quota(qoutaRootName, messages, maxMessages, storage, maxStorage);
                    }
                }
                else
                {
                    break;
                }
            }

            line = RemoveCmdTag(line);
            if (!line.ToUpper().StartsWith("OK"))
            {
                throw new IMAP_ClientException(line);
            }

            return retVal;
        }
        /// <summary>
        /// Gets specified folder quota info. Throws Exception if server doesn't support QUOTA.
        /// </summary>
        /// <param name="folder">Folder name.</param>
        /// <returns></returns>
        public IMAP_Quota GetFolderQuota(string folder)
        {
            /* RFC 2087 4.3. GETQUOTAROOT Command

                    Arguments:  mailbox name

                    Data:       untagged responses: QUOTAROOT, QUOTA

                    Result:     OK - getquota completed
                                NO - getquota error: no such mailbox, permission denied
                                BAD - command unknown or arguments invalid

               The GETQUOTAROOT command takes the name of a mailbox and returns the
               list of quota roots for the mailbox in an untagged QUOTAROOT
               response.  For each listed quota root, it also returns the quota
               root's resource usage and limits in an untagged QUOTA response.

                   Example:    C: A003 GETQUOTAROOT INBOX
                               S: * QUOTAROOT INBOX ""
                               S: * QUOTA "" (STORAGE 10 512)
                               S: A003 OK Getquota completed
            */

            if(!m_Connected){
                throw new Exception("You must connect first !");
            }
            if(!m_Authenticated){
                throw new Exception("You must authenticate first !");
            }

            IMAP_Quota retVal = null;

            m_pSocket.WriteLine("a1 GETQUOTAROOT \"" + Core.Encode_IMAP_UTF7_String(folder) + "\"");

            // Must get lines with * and cmdTag + OK or cmdTag BAD/NO
            string reply = m_pSocket.ReadLine();
            if(reply.StartsWith("*")){
                // Read multiline response
                while(reply.StartsWith("*")){
                    // Get rid of *
                    reply = reply.Substring(1).Trim();

                    if(reply.ToUpper().StartsWith("QUOTAROOT")){
                        // Skip QUOTAROOT
                    }
                    else if(reply.ToUpper().StartsWith("QUOTA")){
                        StringReader r = new StringReader(reply);
                        // Skip QUOTA word
                        r.ReadWord();

                        string qoutaRootName = r.ReadWord();
                        long   storage       = -1;
                        long   maxStorage    = -1;
                        long   messages      = -1;
                        long   maxMessages   = -1;

                        string limits = r.ReadParenthesized();
                        r = new StringReader(limits);
                        while(r.Available > 0){
                            string limitName = r.ReadWord();
                            // STORAGE usedBytes maximumAllowedBytes
                            if(limitName.ToUpper() == "STORAGE"){
                                storage    = Convert.ToInt64(r.ReadWord());
                                maxStorage = Convert.ToInt64(r.ReadWord());
                            }
                            // STORAGE messagesCount maximumAllowedMessages
                            else if(limitName.ToUpper() == "MESSAGE"){
                                messages    = Convert.ToInt64(r.ReadWord());
                                maxMessages = Convert.ToInt64(r.ReadWord());
                            }
                        }

                        retVal = new IMAP_Quota(qoutaRootName,messages,maxMessages,storage,maxStorage);
                    }

                    reply = m_pSocket.ReadLine();
                }
            }

            reply = reply.Substring(reply.IndexOf(" ")).Trim(); // Remove Cmd tag

            if(!reply.ToUpper().StartsWith("OK")){
                throw new Exception("Server returned:" + reply);
            }

            return retVal;
        }