示例#1
0
        public void ComposePrivateMessage(string subject, string body, string to, string captchaId = "", string captchaAnswer = "")
        {
            if (User == null)
            {
                throw new Exception("User can not be null.");
            }
            var request = _webAgent.CreatePost(ComposeMessageUrl);

            _webAgent.WritePostBody(request.GetRequestStream(), new
            {
                api_type = "json",
                subject,
                text = body,
                to,
                uh      = User.Modhash,
                iden    = captchaId,
                captcha = captchaAnswer
            });
            var response = request.GetResponse();
            var result   = _webAgent.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(result);

            ICaptchaSolver solver = CaptchaSolver; // Prevent race condition

            if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "BAD_CAPTCHA" && solver != null)
            {
                captchaId = json["json"]["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(captchaId));

                if (!captchaResponse.Cancel) // Keep trying until we are told to cancel
                {
                    ComposePrivateMessage(subject, body, to, captchaId, captchaResponse.Answer);
                }
            }
        }
示例#2
0
        private Post Submit(SubmitData data)
        {
            if (Reddit.User == null)
            {
                throw new RedditException("No user logged in.");
            }
            var request = WebAgent.CreatePost(SubmitLinkUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), data);

            var            json   = WebAgent.ExecuteRequest(request);
            ICaptchaSolver solver = Reddit.CaptchaSolver;

            if (json["errors"].Any() && json["errors"][0][0].ToString() == "BAD_CAPTCHA" &&
                solver != null)
            {
                data.Iden = json["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));

                // We throw exception due to this method being expected to return a valid Post object, but we cannot
                // if we got a Captcha error.
                if (captchaResponse.Cancel)
                {
                    throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post");
                }

                data.Captcha = captchaResponse.Answer;
                return(Submit(data));
            }

            return(new Post(Reddit, json, WebAgent));
        }
 public CaptchaResponse HandleCaptcha(Captcha captcha)
 {
     Console.WriteLine("Captcha required! The captcha ID is {0}", captcha.Id);
     Console.WriteLine("You can find the captcha image at this url: {0}", captcha.Url);
     Console.WriteLine("Please input your captcha response or empty string to cancel:");
     var response = Console.ReadLine();
     CaptchaResponse captchaResponse = new CaptchaResponse(string.IsNullOrEmpty(response) ? null : response);
     return captchaResponse;
 }
示例#4
0
        /// <summary>
        ///

        /// Compose a private message.
        /// </summary>
        /// <param name="subject">message subject</param>
        /// <param name="body">markdown body</param>
        /// <param name="to">target author or subreddit</param>
        /// <param name="fromSubReddit">The subreddit to send the message as (optional).</param>
        /// <param name="captchaId"></param>
        /// <param name="captchaAnswer"></param>
        /// <remarks>If <paramref name="fromSubReddit"/> is passed in then the message is sent from the subreddit. the sender must be a mod of the specified subreddit.</remarks>
        /// <exception cref="AuthenticationException">Thrown when a subreddit is passed in and the user is not a mod of that sub.</exception>
        public void ComposePrivateMessage(string subject, string body, string to, string fromSubReddit = "", string captchaId = "", string captchaAnswer = "")
        {
            if (User == null)
            {
                throw new Exception("User can not be null.");
            }

            if (!string.IsNullOrWhiteSpace(fromSubReddit))
            {
                var subReddit   = this.GetSubreddit(fromSubReddit);
                var modNameList = subReddit.Moderators.Select(b => b.Name).ToList();

                if (!modNameList.Contains(User.Name))
                {
                    throw new AuthenticationException(
                              string.Format(
                                  @"User {0} is not a moderator of subreddit {1}.",
                                  User.Name,
                                  subReddit.Name));
                }
            }

            var request = WebAgent.CreatePost(ComposeMessageUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), new
            {
                api_type = "json",
                subject,
                text = body,
                to,
                from_sr = fromSubReddit,
                uh      = User.Modhash,
                iden    = captchaId,
                captcha = captchaAnswer
            });
            var response = request.GetResponse();
            var result   = WebAgent.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(result);

            ICaptchaSolver solver = CaptchaSolver; // Prevent race condition

            if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "BAD_CAPTCHA" && solver != null)
            {
                captchaId = json["json"]["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(captchaId));

                if (!captchaResponse.Cancel) // Keep trying until we are told to cancel
                {
                    ComposePrivateMessage(subject, body, to, fromSubReddit, captchaId, captchaResponse.Answer);
                }
            }
            else if (json["json"]["errors"].Any())
            {
                throw new Exception("Error when composing message. Error: " + json["json"]["errors"][0][0].ToString());
            }
        }
        public CaptchaResponse HandleCaptcha(Captcha captcha)
        {
            Console.WriteLine("Captcha required! The captcha ID is {0}", captcha.Id);
            Console.WriteLine("You can find the captcha image at this url: {0}", captcha.Url);
            Console.WriteLine("Please input your captcha response or empty string to cancel:");
            var             response        = Console.ReadLine();
            CaptchaResponse captchaResponse = new CaptchaResponse(string.IsNullOrEmpty(response) ? null : response);

            return(captchaResponse);
        }
        private Post Submit(SubmitData data)
        {
            if (Reddit.User == null)
            {
                throw new RedditException("No user logged in.");
            }
            var request = WebAgent.CreatePost(SubmitLinkUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), data);

            var response = request.GetResponse();
            var result   = WebAgent.GetResponseString(response.GetResponseStream());
            var json     = JToken.Parse(result);

            ICaptchaSolver solver = Reddit.CaptchaSolver;

            if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "BAD_CAPTCHA" &&
                solver != null)
            {
                data.Iden = json["json"]["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));

                // We throw exception due to this method being expected to return a valid Post object, but we cannot
                // if we got a Captcha error.
                if (captchaResponse.Cancel)
                {
                    throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post");
                }

                data.Captcha = captchaResponse.Answer;
                return(Submit(data));
            }
            else if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "ALREADY_SUB")
            {
                throw new DuplicateLinkException(String.Format("Post failed when submitting.  The following link has already been submitted: {0}", SubmitLinkUrl));
            }

            return(new Post(Reddit, json["json"], WebAgent));
        }