예제 #1
0
        /// <summary>
        /// Sets the article cursor.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <returns></returns>
        private ArticleResponseIds SetArticleCursor(string direction)
        {
            if (direction == null)
            {
                throw new ArgumentNullException("direction");
            }

            if (!(direction.Equals("LAST", StringComparison.InvariantCultureIgnoreCase) || direction.Equals("NEXT", StringComparison.InvariantCultureIgnoreCase)))
            {
                throw new ArgumentException(Resource.ErrorMessage03, "direction");
            }

            if (!CurrentGroupSelected)
            {
                throw new NntpGroupNotSelectedException();
            }

            ValidateConnectionState();

            NntpReaderWriter.WriteCommand(direction);
            NntpReaderWriter.ReadResponse();
            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.ArticleRetrievedTextSeparate)
            {
                throw new NntpResponseException(Resource.ErrorMessage04, NntpReaderWriter.LastResponse);
            }
            return(ArticleResponseIds.Parse(NntpReaderWriter.LastResponse));
        }
예제 #2
0
        /// <summary>
        /// Does the basic command. In the NNTP protocol, a command is sent and the server
        /// possibly returns some text and finally is returns a response code. If a server
        /// returned line equal a single "." we are done and nothing more is returned. If
        /// the server returns a ".." (double period) the leading period is removed and the
        /// remaining string is returned.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="expectedResponseCode">The expected response code.</param>
        /// <returns></returns>
        protected IEnumerable <string> DoBasicCommand(string command, int expectedResponseCode)
        {
            ValidateConnectionState();
            NntpReaderWriter.WriteCommand(command);
            NntpReaderWriter.ReadResponse();

            if (NntpReaderWriter.LastResponseCode != expectedResponseCode)
            {
                throw new NntpResponseException(Resource.ErrorMessage12, NntpReaderWriter.LastResponse);
            }

            do
            {
                string line = NntpReaderWriter.ReadLine();
                if (line.Equals("."))
                {
                    break;
                }
                else if (line.StartsWith(".."))
                {
                    line = line.Substring(1);
                }
                yield return(line);
            } while (true);
        }
예제 #3
0
        /// <summary>
        /// Selects the newsgroup.
        /// </summary>
        /// <param name="group">The group.</param>
        public virtual void SelectNewsgroup(string group)
        {
            if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentNullException("group");
            }

            ValidateConnectionState();

            NntpReaderWriter.WriteCommand("GROUP " + group);

            string response = NntpReaderWriter.ReadResponse();

            if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.NewsgroupSelected)
            {
                string[]            parts = response.Split(' ');
                NewsgroupStatistics g     = new NewsgroupStatistics(group, ConvertToInt32(parts[1]), ConvertToInt32(parts[2]), ConvertToInt32(parts[3]));
                m_currentGroup = g;
            }
            else
            {
                m_currentGroup = null;
                if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.NoSuchNewsgroup)
                {
                    throw new NntpGroupNotSelectedException();
                }
                else
                {
                    throw new NntpResponseException(Resource.ErrorMessage02, NntpReaderWriter.LastResponse);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Posts the article.
        /// </summary>
        /// <param name="header">The header.</param>
        /// <param name="body">The body.</param>
        public virtual void PostArticle(IArticleHeaderEnumerator header, /*List*/ IEnumerable <string> body)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            if (body == null)
            {
                throw new ArgumentNullException("body");
            }

            ValidateConnectionState();

            NntpReaderWriter.WriteCommand("POST");
            NntpReaderWriter.ReadResponse();

            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.SendArticleToPost)
            {
                throw new NntpResponseException(Resource.ErrorMessage06, NntpReaderWriter.LastResponse);
            }

            foreach (string key in header.HeaderKeys)
            {
                int count = 0;
                foreach (string v in header[key])
                {
                    if (count > 0)
                    {
                        NntpReaderWriter.Write("\t");
                    }
                    else
                    {
                        NntpReaderWriter.Write(key);
                        NntpReaderWriter.Write(": ");
                    }
                    NntpReaderWriter.WriteLine(v);
                    count++;
                }
            }

            NntpReaderWriter.WriteLine("");

            foreach (string s in body)
            {
                if (s.Length > 0 && s[0] == '.')
                {
                    NntpReaderWriter.Write(".");
                }
                NntpReaderWriter.WriteLine(s);
            }

            NntpReaderWriter.WriteLine(".");
            NntpReaderWriter.ReadResponse();
            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.ArticlePostedOk)
            {
                Console.WriteLine("resp: {0}", NntpReaderWriter.LastResponse);
                throw new NntpResponseException(Resource.ErrorMessage07, NntpReaderWriter.LastResponse);
            }
        }
예제 #5
0
        /// <summary>
        /// Does the basic command. In the NNTP protocol, a command is sent and the server
        /// possibly returns some text and finally is returns a response code. If a server
        /// returned line equal a single "." we are done and nothing more is returned. If
        /// the server returns a ".." (double period) the leading period is removed and the
        /// remaining string is returned.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="expectedResponseCode">The expected response code.</param>
        /// <returns></returns>
        protected IEnumerable <string> DoBasicCommand(string command, int expectedResponseCode)
        {
            ValidateConnectionState();
            NntpReaderWriter.WriteCommand(command);
            NntpReaderWriter.ReadResponse();

            if (NntpReaderWriter.LastResponseCode != expectedResponseCode)
            {
                throw new NntpResponseException(NntpErrorMessages.ERROR_12, NntpReaderWriter.LastResponse);
            }

            do
            {
                string line = NntpReaderWriter.ReadLine();
                if (line.Equals("."))
                {
                    break;
                }
                else if (line.Length > 1)
                {
                    if (line[0].Equals('.') && line[1].Equals('.'))
                    {
                        line = line.Substring(1);
                    }
                }

                yield return(line);
            } while (true);
        }
예제 #6
0
        /// <summary>
        /// Posts the article.
        /// </summary>
        /// <param name="header">The header.</param>
        /// <param name="body">The body.</param>
        public virtual void PostArticle(IArticleHeaderEnumerator header, List <string> body)
        {
            sb.Clear();
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            if (body == null)
            {
                throw new ArgumentNullException("body");
            }

            ValidateConnectionState();

            NntpReaderWriter.WriteCommand("post");
            NntpReaderWriter.ReadResponse();

            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.SendArticleToPost)
            {
                throw new NntpResponseException(NntpErrorMessages.ERROR_6, NntpReaderWriter.LastResponse);
            }

            foreach (string key in header.HeaderKeys)
            {
                int count = 0;
                foreach (string v in header[key])
                {
                    if (count > 0)
                    {
                        sb.Append("\t");
                    }
                    else
                    {
                        sb.Append(key);
                        sb.Append(": ");
                    }
                    sb.Append(v + NEWLINE);
                    count++;
                }
            }
            sb.Append("" + NEWLINE);

            foreach (string s in body)
            {
                if (s.Length > 0 && s[0] == '.')
                {
                    sb.Append(".");
                }
                sb.Append(s + NEWLINE);
            }
            sb.Append("." + NEWLINE);
            NntpReaderWriter.Write(sb.ToString());
            NntpReaderWriter.ReadResponse();
            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.ArticlePostedOk)
            {
                throw new NntpResponseException(NntpErrorMessages.ERROR_7, NntpReaderWriter.LastResponse);
            }
        }
예제 #7
0
        /// <summary>
        /// Sends the slave command.
        /// </summary>
        public virtual void SendSlave()
        {
            ValidateConnectionState();

            NntpReaderWriter.WriteLine("SLAVE");
            NntpReaderWriter.ReadResponse();
            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.SlaveStatusNoted)
            {
                throw new NntpResponseException(Resource.ErrorMessage08, NntpReaderWriter.LastResponse);
            }
        }
 /// <summary>
 /// Sends the mode reader.
 /// </summary>
 public void SendModeReader()
 {
     NntpReaderWriter.WriteCommand("MODE READER");
     NntpReaderWriter.ReadResponse();
     if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.ServerReadyPostingAllowed)
     {
         PostingAllowed = true;
     }
     else if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.ServerReadyNoPostingAllowed)
     {
         PostingAllowed = false;
     }
     else if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.CommandUnavailable)
     {
         throw new NntpException();
     }
 }
예제 #9
0
        /// <summary>
        /// Retrieves the statistics for an article based on the command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns></returns>
        protected virtual ArticleResponseIds RetrieveStatisticsCore(string command)
        {
            ValidateConnectionState();

            if (!CurrentGroupSelected)
            {
                throw new NntpGroupNotSelectedException();
            }

            NntpReaderWriter.WriteCommand(command);
            NntpReaderWriter.ReadResponse();
            if (NntpReaderWriter.LastResponseCode != Rfc977ResponseCodes.ArticleRetrievedTextSeparate)
            {
                throw new NntpResponseException(Resource.ErrorMessage05, NntpReaderWriter.LastResponse);
            }
            return(ArticleResponseIds.Parse(NntpReaderWriter.LastResponse));
        }
 /// <summary>
 /// Authenticates the user.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="password">The password.</param>
 public virtual void AuthenticateUser(string userName, string password)
 {
     NntpReaderWriter.WriteCommand("AUTHINFO USER " + userName);
     NntpReaderWriter.ReadResponse();
     if (NntpReaderWriter.LastResponseCode == Rfc4643ResponseCodes.PasswordRequired)
     {
         NntpReaderWriter.WriteCommand("AUTHINFO PASS " + password);
         NntpReaderWriter.ReadResponse();
         if (NntpReaderWriter.LastResponseCode != Rfc4643ResponseCodes.AuthenticationAccepted)
         {
             throw new NntpNotAuthorizedException(Resource.ErrorMessage30);
         }
     }
     else
     {
         throw new NntpNotAuthorizedException(Resource.ErrorMessage31);
     }
 }
 /// <summary>
 /// Authenticates the user.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="password">The password.</param>
 public virtual void AuthenticateUser(string userName, string password)
 {
     NntpReaderWriter.WriteCommand("authinfo user " + userName);
     NntpReaderWriter.ReadResponse();
     if (NntpReaderWriter.LastResponseCode == Rfc4643ResponseCodes.PasswordRequired)
     {
         NntpReaderWriter.WriteCommand("authinfo pass " + password);
         NntpReaderWriter.ReadResponse();
         if (NntpReaderWriter.LastResponseCode != Rfc4643ResponseCodes.AuthenticationAccepted)
         {
             throw new NntpNotAuthorizedException(NntpErrorMessages.ERROR_30);
         }
     }
     else
     {
         throw new NntpNotAuthorizedException(NntpErrorMessages.ERROR_31);
     }
 }
예제 #12
0
        /// <summary>
        /// Connects using the specified host name and port number.
        /// </summary>
        /// <param name="hostName">Name of the host.</param>
        /// <param name="port">The port.</param>
        /// <param name="useSsl">Connect with SSL</param>
        public virtual void Connect(string hostName, int port, bool useSsl)
        {
            Open(hostName, port, useSsl);

            NntpReaderWriter.ReadResponse();
            if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.ServerReadyPostingAllowed)
            {
                m_postingIsAllowed = true;
            }
            else if (NntpReaderWriter.LastResponseCode == Rfc977ResponseCodes.ServerReadyNoPostingAllowed)
            {
                m_postingIsAllowed = false;
            }
            else
            {
                throw new NntpResponseException(Resource.ErrorMessage01, NntpReaderWriter.LastResponse);
            }
        }