예제 #1
0
        protected override void Parse(NntpResponse response)
        {
            base.Parse(response);

            Headers = new Head(response);
            Body = new Body(response);
        }
예제 #2
0
        protected override void Parse(NntpResponse response)
        {
            ThrowIfUnexpectedResponseCode(response, NntpResponseCode.NflsGroupSelected);

            string count = null;
            string first = null;
            string last = null;
            string name = null;

            try
            {
                string statusLine = response.StatusLine;

                var parts = statusLine.Split(' ');

                count = parts[1];
                first = parts[2];
                last = parts[3];
                name = parts[4];
            }
            catch (Exception)
            {
                //Likely index out of range.. ignore
            }
            finally
            {
                Name = name;
                if(count != null)
                    Count = Convert.ToInt64(count);
                if(first != null)
                    First = Convert.ToInt64(first);
                if(last != null)
                    Last = Convert.ToInt64(last);
            }
        }
예제 #3
0
파일: Head.cs 프로젝트: aluitink/NntpClient
 protected override void Parse(NntpResponse response)
 {
     base.Parse(response);
     switch (response.ResponseCode)
     {
         case NntpResponseCode.ArticleRetrievedHeadAndBodyFollows:
             HandleHeadersAndBody(response);
             break;
         case NntpResponseCode.ArticleRetrievedHeadFollows:
             HandleHeaders(response);
             break;
     }
 }
예제 #4
0
        /// <summary>
        /// The <a href="https://tools.ietf.org/html/rfc4643#section-2.3">AUTHINFO USER and AUTHINFO PASS</a>
        /// (<a href="https://tools.ietf.org/html/rfc2980#section-3.1.1">ad 1</a>)
        /// commands are used to present clear text credentials to the server.
        /// </summary>
        /// <param name="username">The username to use.</param>
        /// <param name="password">The password to use.</param>
        /// <returns>true if the user was authenticated successfully; otherwise false.</returns>
        public bool Authenticate(string username, string password = null)
        {
            Guard.ThrowIfNullOrWhiteSpace(username, nameof(username));
            NntpResponse userResponse = connection.Command($"AUTHINFO USER {username}", new ResponseParser(281));

            if (userResponse.Success)
            {
                return(true);
            }
            if (userResponse.Code != 381 || string.IsNullOrWhiteSpace(password))
            {
                return(false);
            }
            NntpResponse passResponse = connection.Command($"AUTHINFO PASS {password}", new ResponseParser(281));

            return(passResponse.Success);
        }
예제 #5
0
파일: Body.cs 프로젝트: aluitink/NntpClient
 private void HandleBody(NntpResponse response)
 {
     using (var streamReader = new StreamReader(response.Payload, new UTF8Encoding(false)))
     {
         _tempFilePath = Path.GetTempFileName();
         using (var fileStream = new FileStream(_tempFilePath, FileMode.OpenOrCreate, FileAccess.Write))
         {
             var line = streamReader.ReadLine();
             while (line != null && !line.Equals(BlockTerminator))
             {
                 var lineBytes = Encoding.UTF8.GetBytes(line);
                 fileStream.Write(lineBytes, 0, lineBytes.Length);
                 line = streamReader.ReadLine();
             }
         }
     }
 }
예제 #6
0
        protected override void Parse(NntpResponse response)
        {
            ThrowIfUnexpectedResponseCode(response, NntpResponseCode.ArticleRetrievedHeadFollows);

            var statusLine = response.StatusLine;

            var statusParts = statusLine.Split(new[] {' '}, 3);

            try
            {
                Name = statusParts[1];
            }
            catch (Exception)
            {
                Logger.WarnFormat("Could not parse status line '{0}'", statusLine);
            }

            _tempFilePath = GetOwnedPayload(response);
        }
예제 #7
0
파일: Head.cs 프로젝트: aluitink/NntpClient
        private void HandleHeaders(NntpResponse response)
        {
            using (var streamReader = new StreamReader(response.Payload, new UTF8Encoding(false)))
            {
                var headers = new Dictionary<string, string>();
                var line = streamReader.ReadLine();
                var previousKey = string.Empty;
                while (line != null && !line.Equals(BlockTerminator))
                {
                    if (line.StartsWith("\t") || line.StartsWith(" "))//line continuation
                    {
                        string val = headers[previousKey];
                        val = val + line.TrimStart('\t');
                        headers[previousKey] = val;
                    }
                    else
                    {
                        var parts = line.Split(new[] {':'}, 2);
                        var key = parts[0];
                        previousKey = key;
                        var value = parts[1].TrimStart(' ');

                        try
                        {
                            headers.Add(key, value);
                        }
                        catch (ArgumentException ex)
                        {
                            //if key was already there.. concat the values
                            var val1 = headers[key];
                            var newVal = string.Format("{0} {1}", val1, value);
                            headers[key] = newVal;
                        }
                    }

                    line = streamReader.ReadLine();

                }

                _headers = new ReadOnlyDictionary<string, string>(headers);
            }
        }
예제 #8
0
        protected override void Parse(NntpResponse response)
        {
            try
            {
                ThrowIfUnexpectedResponseCode(response,
                NntpResponseCode.ArticleRetrievedHeadFollows,
                NntpResponseCode.ArticleRetrievedHeadAndBodyFollows,
                NntpResponseCode.ArticleRetrievedBodyFollows);

                string articleId = null;
                string messageId = null;

                try
                {
                    string statusLine = response.StatusLine;

                    var parts = statusLine.Split(new[] { ' ' }, 4);

                    articleId = parts[1];
                    messageId = parts[2];

                }
                catch (Exception)
                {
                    //Likely index out of range.. ignore
                }
                finally
                {
                    if (!string.IsNullOrWhiteSpace(articleId))
                        ArticleId = Convert.ToInt64(articleId);
                    if (!string.IsNullOrWhiteSpace(messageId))
                        MessageId = messageId;
                }
            }
            catch (Exception)
            {
                IsMissing = true;
            }
        }
예제 #9
0
 public AuthenticateResponse(NntpResponse response) : base(response.Code, response.Message)
 {
 }
예제 #10
0
 public HeaderCollection(NntpResponse response)
 {
     Parse(response);
 }
예제 #11
0
 protected void Answer(NntpResponse code)
 {
     Answer(new Response(code));
 }
예제 #12
0
 public Group(NntpResponse response)
 {
     Parse(response);
 }
예제 #13
0
 public Article(NntpResponse response)
 {
     Parse(response);
 }
예제 #14
0
 public Response(string description, NntpResponse code, object body, params object[] parameters) :
     this(description, (int)code, body, parameters)
 {
 }
예제 #15
0
 public ConnectResponse(NntpResponse response, bool canPost) : base(response.Code, response.Message)
 {
     this.CanPost = canPost;
 }
예제 #16
0
 public Response(NntpResponse code, object body, params object[] parameters) :
     this(null, code, body, parameters)
 {
 }
예제 #17
0
파일: Head.cs 프로젝트: aluitink/NntpClient
        private void HandleHeadersAndBody(NntpResponse response)
        {
            using (var streamReader = new StreamReader(response.Payload, new UTF8Encoding(false)))
            {
                var headers = new Dictionary<string, string>();
                var line = streamReader.ReadLine();

                while (line != null && !line.Equals(HeaderBodyDelimiter))
                {
                    var parts = line.Split(new[] {':'}, 2);
                    var key = parts[0];
                    var value = parts[1].TrimStart(' ');

                    headers.Add(key, value);
                    line = streamReader.ReadLine();
                }

                _headers = new ReadOnlyDictionary<string, string>(headers);
            }
        }
예제 #18
0
파일: Body.cs 프로젝트: aluitink/NntpClient
 public Body(NntpResponse response)
 {
     Parse(response);
 }
예제 #19
0
 public Response(NntpResponse code) : this(code, null)
 {
 }
예제 #20
0
파일: Head.cs 프로젝트: aluitink/NntpClient
 public Head(NntpResponse response)
 {
     Parse(response);
 }
예제 #21
0
 public static void Answer(NntpResponse code, Socket socket)
 {
     Answer((int)code, socket);
 }