private string GetArticleBody <T>(T msg)
        {
            var    conn                 = (msg as Messages.GetArticleBody).Connection;
            uint   articleId            = (msg as Messages.GetArticleBody).ArticleID;
            string newsGroup            = (msg as Messages.GetArticleBody).NewsGroup;
            bool   BodyCommandSupported = (bool)conn.SupportedCommands[NNTPMessages.Body];

            if (!BodyCommandSupported)
            {
                throw new Exception(); //TODO:
            }
            string response = SelectNewsGroup(conn, newsGroup);

            LogResponse(response);

            Write(conn, NNTPMessages.GetArticleBody(articleId.ToString()));
            response = Response(conn, NNTPMessages.GetArticleBody(articleId.ToString()));

            LogResponse(response);
            if (!NNTPResponseCodes.IsResposeCode(response, NNTPResponseCodes.ArticleRetrievedBodyToFollow))
            {
                if (response.Length <= 0)
                {
                    throw new Exception();// TODO
                }
            }

            if (NNTPResponseCodes.IsResposeCode(response, NNTPResponseCodes.NoSuchArticleNumberInGroup))
            {
                throw new Exception(); //TODO:
            }

            string articleBody = "";

            bool storedAuth = conn.Authenticate;

            conn.Authenticate = false;

            while (true)
            {
                response = Response(conn, NNTPMessages.GetArticleBody(articleId.ToString()));
                if (response == ".\r\n")
                {
                    break;
                }

                if (response == ".\n")
                {
                    break;
                }

                //if (article.Length < 1024)
                articleBody += response;
            }

            conn.Authenticate = storedAuth;

            return(articleBody);
        }
Exemplo n.º 2
0
        public override bool OnMessageRecieved <T>(T msg)
        {
            if (msg.GetType() != typeof(Messages.Authenticate))
            {
                return(false);
            }

            string        authResponse = "";
            var           conn         = (msg as Messages.Authenticate).Connection;
            TcpClient     connection   = conn.Connection;
            NetworkStream stream       = conn.Stream;
            string        Username     = conn.UserName;
            string        Password     = conn.Password;

            System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding();

            if ((null == Username) || (null == Password))
            {
                return(false);
            }

            //DO Authetication Here!
            bool   success     = true;
            string message     = NNTPMessages.GetAuthInfoUser(Username);
            var    writeBuffer = en.GetBytes(message);

            stream.Write(writeBuffer, 0, writeBuffer.Length);

            string requiresPassword = Response(conn, message);

            //TODO: check the supported commands for the correct command to authenticate with.
            authResponse = requiresPassword;

            if (NNTPResponseCodes.IsResposeCode(requiresPassword, NNTPResponseCodes.PasswordRequired))
            {
                message     = NNTPMessages.GetAuthInfoPass(Password);
                writeBuffer = en.GetBytes(message);
                stream.Write(writeBuffer, 0, writeBuffer.Length);

                string response = Response(conn, message);

                if (NNTPResponseCodes.IsResposeCode(response, NNTPResponseCodes.AuthenticationOK))
                {
                    success = true;
                }

                if (NNTPResponseCodes.IsResposeCode(response, NNTPResponseCodes.PermissionDenied))
                {
                    throw new Exception(response); //NNTPAuthenticationDeniedException
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public override bool OnMessageRecieved <T>(T msg)
        {
            if (msg.GetType() != typeof(Messages.PopulateCommandList))
            {
                return(false);
            }

            var conn = (msg as Messages.PopulateCommandList).Connection;

            conn.ResetSupportedCommandList();
            Write(conn, NNTPMessages.Help);

            string response = Response(conn, NNTPMessages.Help);

            Telegraph.Instance.Tell(response);

            if (!NNTPResponseCodes.IsResposeCode(response, NNTPResponseCodes.HelpTextFollows))
            {
                //LibrayMessageLogger.Instance.WriteError(this, NNTPErrorTypes.NNTPResponseWasNotTheOneExpected, response);
                throw new Exception(); // throw new NntpException(response);
            }

            bool storeadAuth = conn.Authenticate;

            conn.Authenticate = false;

            while (true)
            {
                response = Response(conn, NNTPMessages.Help);
                if (response == ".\r\n")
                {
                    break;
                }

                if (response == ".\n")
                {
                    break;
                }

                Telegraph.Instance.Tell(response);
                AddValidCommand(conn, response);
            }

            conn.Authenticate = storeadAuth;
            return(true);
        }
Exemplo n.º 4
0
        private bool Connect <T>(T msg)
        {
            /// We call the Connect method of our base TcpClient class with the server name and port 119. Port 119 is the well-known port for NNTP servers.
            /// The server should respond with a 200 status-code indicating that connection was successful.
            /// When you are finished calling methods to the Nntp client object, then you should call the Disconnect method to terminate the connection.

            var       connection = (msg as Messages.ConnectToServer).Connection;
            string    server     = connection.ServerName;
            TcpClient client     = new TcpClient();

            System.Diagnostics.Debug.WriteLine("Connecting:" + server);
            client.Connect(server, 119);
            NetworkStream stream = client.GetStream();

            stream.ReadTimeout = 30 * 1000;

            connection.Connection = client;
            connection.Stream     = stream;

            System.Diagnostics.Debug.WriteLine("Connected asking for response:" + server);
            Task <IActorMessage> t = Telegraph.Instance.Ask(new Messages.GetReponse(connection, ""));

            t.Wait();

            string response = (t.Result.ProcessingResult as string);

            Telegraph.Instance.Tell(response);

            if (NNTPResponseCodes.IsResposeCode(response, NNTPResponseCodes.ServerReadyNoPostingAllowed))
            {
                connection.SupportedCommands[NNTPMessages.Post] = false;
            }
            else if (response.Substring(0, 3) != NNTPResponseCodes.ServerReadyPostingAllowed)
            {
                throw new Exception(); // TODO:throw new NntpException(response);
            }
            else
            {
                connection.SupportedCommands[NNTPMessages.Post] = true;
            }

            return(true);
        }