예제 #1
0
        /// <summary>
        /// Gets IMAP server namespaces info.
        /// </summary>
        /// <returns>Returns user namespaces.</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_NamespacesInfo GetNamespacesInfo()
        {
            /* RFC 2342 5. NAMESPACE Command.
               Arguments: none

               Response:  an untagged NAMESPACE response that contains the prefix
                          and hierarchy delimiter to the server's Personal
                          Namespace(s), Other Users' Namespace(s), and Shared
                          Namespace(s) that the server wishes to expose. The
                          response will contain a NIL for any namespace class
                          that is not available. Namespace_Response_Extensions
                          MAY be included in the response.
                          Namespace_Response_Extensions which are not on the IETF
                          standards track, MUST be prefixed with an "X-".

               Result:    OK - Command completed
                          NO - Error: Can't complete command
                          BAD - argument invalid

                Example:
                    < A server that supports a single personal namespace.  No leading
                    prefix is used on personal mailboxes and "/" is the hierarchy
                    delimiter.>

                    C: A001 NAMESPACE
                    S: * NAMESPACE (("" "/")) NIL NIL
                    S: A001 OK NAMESPACE command 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 NAMESPACE command is only valid in authenticated state.");
            }

            string line = GetNextCmdTag() + " NAMESPACE";
            int countWritten = this.TcpStream.WriteLine(line);
            LogAddWrite(countWritten, line);

            IMAP_NamespacesInfo namespacesInfo = new IMAP_NamespacesInfo(null, null, null);
            while (true)
            {
                line = this.ReadLine();

                if (line.StartsWith("*"))
                {
                    line = RemoveCmdTag(line);

                    if (line.ToUpper().StartsWith("NAMESPACE"))
                    {
                        namespacesInfo = IMAP_NamespacesInfo.Parse(line);
                    }
                }
                else
                {
                    break;
                }
            }

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

            return namespacesInfo;
        }
        /// <summary>
        /// Gets IMAP server namespaces info.
        /// </summary>
        /// <returns></returns>
        public IMAP_NamespacesInfo GetNamespacesInfo()
        {
            /* RFC 2342 5. NAMESPACE Command.
               Arguments: none

               Response:  an untagged NAMESPACE response that contains the prefix
                          and hierarchy delimiter to the server's Personal
                          Namespace(s), Other Users' Namespace(s), and Shared
                          Namespace(s) that the server wishes to expose. The
                          response will contain a NIL for any namespace class
                          that is not available. Namespace_Response_Extensions
                          MAY be included in the response.
                          Namespace_Response_Extensions which are not on the IETF
                          standards track, MUST be prefixed with an "X-".

               Result:    OK - Command completed
                          NO - Error: Can't complete command
                          BAD - argument invalid

                Example:
                    < A server that supports a single personal namespace.  No leading
                    prefix is used on personal mailboxes and "/" is the hierarchy
                    delimiter.>

                    C: A001 NAMESPACE
                    S: * NAMESPACE (("" "/")) NIL NIL
                    S: A001 OK NAMESPACE command completed
            */

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

            m_pSocket.WriteLine("a1 NAMESPACE");

            IMAP_NamespacesInfo namespacesInfo = new IMAP_NamespacesInfo(null,null,null);
            string reply = m_pSocket.ReadLine();
            while(reply.StartsWith("*")){
                reply = RemoveCmdTag(reply);

                if(reply.ToUpper().StartsWith("NAMESPACE")){
                    namespacesInfo = IMAP_NamespacesInfo.Parse(reply);
                }

                reply = m_pSocket.ReadLine();
            }

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

            return namespacesInfo;
        }