Esempio n. 1
0
        public async Task <Post> CreatePostAsync(string message, ICaptcha captcha, OptionalPostArgs args)
        {
            uint id = await PostManager.CreatePost(Board, this, message, captcha, args).ConfigureAwait(false);

            var newPost = new Post()
            {
                Thread     = this,
                Message    = message,
                PostNumber = id,
                Name       = args.Name
            };

            AutoUpdater.SuscribePost(newPost);

            return(newPost);
        }
Esempio n. 2
0
 public Post CreatePost(string message, ICaptcha captcha, OptionalPostArgs args)
 {
     return(CreatePostAsync(message, captcha, args).GetAwaiter().GetResult());
 }
Esempio n. 3
0
        public static async Task <uint> CreatePost(Board board, Thread thread, string message, ICaptcha captcha, OptionalPostArgs args)
        {
            string url = string.Format("https://sys.4chan.org/{0}/post", board.BoardId);

            var postData = new Dictionary <string, object>();

            postData.Add("MAX_FILE_SIZE", "4194304");
            postData.Add("mode", "regist");
            postData.Add("name", args.Name);
            postData.Add("com", message);

            if (thread != null)
            {
                postData.Add("resto", thread.PostNumber.ToString());
            }

            if (!string.IsNullOrEmpty(args.Options))
            {
                postData.Add("email", args.Options);
            }

            if (!string.IsNullOrEmpty(args.Password))
            {
                postData.Add("pwd", args.Password);
            }
            else
            {
                var sb = new StringBuilder();
                var r  = new Random();

                for (var i = 0; i < 15; i++)
                {
                    sb.Append((char)(r.Next('a', 'z')));
                }

                postData.Add("pwd", sb.ToString());
            }

            if (!captcha.Solved)
            {
                throw new CaptchaException();
            }

            if (args.File != null)
            {
                var filename = "file";
                if (!string.IsNullOrEmpty(args.Filename))
                {
                    filename = args.Filename;
                }

                if (!mappings.ContainsKey(Path.GetExtension(filename).ToLower()))
                {
                    throw new UnsupportedFiletypeException(Path.GetExtension(filename));
                }

                var file = new byte[args.File.Length];

                await args.File.ReadAsync(file, 0, file.Length).ConfigureAwait(false);

                postData.Add("upfile",
                             new Internet.FileParameter(file, filename,
                                                        mappings[Path.GetExtension(filename).ToLower()]));
            }

            var request = WebRequest.Create(url) as HttpWebRequest;

            request.CookieContainer = new CookieContainer();

            string formDataBoundary = string.Format("----------{0:N}", Guid.NewGuid());

            byte[] formData = Internet.GetMultipartFormData(postData, formDataBoundary);

            request.Method      = "POST";
            request.ContentType = "multipart/form-data; boundary=" + formDataBoundary;

            request.Headers["Content-Length"] = formData.Length.ToString();
            request.Headers["User-Agent"]     = Internet.UserAgent;

            captcha.Authenticate(request, postData);

            using (Stream requeStream = await request.GetRequestStreamAsync().ConfigureAwait(false))
            {
                requeStream.Write(formData, 0, formData.Length);
            }

            var response = await request.GetResponseAsync() as HttpWebResponse;

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new ThreadNotFoundException();
            }

            using (var respStream = new StreamReader(response.GetResponseStream()))
            {
                string text = respStream.ReadToEnd();

                if (text.Contains("CAPTCHA"))
                {
                    throw new CaptchaException();
                }

                if (text.Contains("banned") || text.Contains("warn"))
                {
                    throw new BannedException();
                }

                try
                {
                    Match  postIdMatch = Regex.Match(text, "no:(\\d+)");
                    string postIdStr   = postIdMatch.Groups[1].Captures[0].Value;

                    return(uint.Parse(postIdStr));
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new UnknownPostException();
                }
            }
        }
Esempio n. 4
0
        public async Task <Thread> CreateThreadAsync(string message, ICaptcha captcha, OptionalPostArgs args)
        {
            uint resp = await PostManager.CreatePost(this, null, message, captcha, args).ConfigureAwait(false);

            var newThread = new Thread(this, resp);

            newThread.AutoUpdater.SuscribePost(newThread);

            return(newThread);
        }