Exemplo n.º 1
0
        public List <Cookie> Authenticate(string username, string password, int timeout = DefaultTimeoutInMilliseconds)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                throw new LoginFailedException("The network is unavailable. Check your network settings and please try again.");
            }

            var request = AwfulWebRequest.CreatePostRequest(LOGIN_URL);

            var signal = new AutoResetEvent(false);
            var result = request.BeginGetRequestStream(callback => { signal.Set(); },
                                                       request);

            signal.WaitOne();

            bool uploaded = UploadLoginData(result, username, password);

            if (!uploaded)
            {
                throw new LoginFailedException();
            }

            List <Cookie> success = ProcessLoginResults(result.AsyncState as HttpWebRequest,
                                                        timeout);

            if (success != null)
            {
                OnLoginSuccessful(this, username, success);
            }

            return(success);
        }
Exemplo n.º 2
0
 public static void Logout(this UserMetadata user)
 {
     if (user.IsActive())
     {
         AwfulWebRequest.ClearCredentials();
     }
 }
Exemplo n.º 3
0
        public static bool ClearMarkedPosts(ThreadMetadata thread, int timeout = CoreConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)
        {
            // create request
            HttpWebRequest request = AwfulWebRequest.CreateFormDataPostRequest(
                "http://forums.somethingawful.com/showthread.php",
                "application/x-www-form-urlencoded");

            // begin request stream creation and wait...
            var signal = new AutoResetEvent(false);
            var result = request.BeginGetRequestStream(callback =>
                                                       SendClearMarkedPostRequest(callback, signal, thread),
                                                       request);

            signal.WaitOne();

            // begin response stream and wait...
            request = result.AsyncState as HttpWebRequest;
            result  = request.BeginGetResponse(callback => { signal.Set(); }, request);
            signal.WaitOne(timeout);

            if (!result.IsCompleted)
            {
                throw new TimeoutException();
            }

            // process the response and return status
            bool success = ProcessClearMarkedPostResponse(result);

            return(success);
        }
Exemplo n.º 4
0
 public static void MakeActive(this UserMetadata user)
 {
     if (!user.IsActive())
     {
         AwfulWebRequest.SetCredentials(user);
     }
 }
Exemplo n.º 5
0
        private void FetchHtmlStateAsync(string url, Action <FetchHtmlState> callback, int timeout = DefaultTimeoutInMilliseconds)
        {
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, string.Format("START FetchHtml({0}, {1})", url, timeout));
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Performing authentication check...");

            Authenticate();

            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Authentication check complete.");
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, string.Format("Requesting html from url '{0}'...", url));

            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                throw new Exception("The network is unavailable. Check your network settings and please try again.");
            }

            if (SimulateTimeout)
            {
                AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "SimulateTimeout = true.");
                Random random = new Random();
                int    value  = random.Next(1, 101);
                if (value <= SimulateTimeoutChance)
                {
                    AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Timeout generated.");
                    throw new TimeoutException("Artificial timeout generated!");
                }
            }

            FetchHtmlState state = new FetchHtmlState(timeout);

            state.Callback = callback;
            state.Request  = AwfulWebRequest.CreateGetRequest(url);
            state.Request.BeginGetResponse(FetchHtmlAsyncResponse, state);
            state.StartTimer();
        }
Exemplo n.º 6
0
        private void Authenticate()
        {
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "START Authenticate");
            // first, check iso storage for cookies.
            // TODO: the above.

            // else, login using the supplied username and password.
            var login = new AwfulLoginClient();
            int tries = 0;

            while (!AwfulWebRequest.CanAuthenticate && tries < 3)
            {
                AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Authentication failed, attempting to try again...");

                var loginArgs = OnLoginRequired(this);
                if (loginArgs == null)
                {
                    throw new Exception("You need to attach an event listener for login required!");
                }

                loginArgs.Signal.WaitOne();

                if (loginArgs.Ignore)
                {
                    AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "Ignoring authentication request. Moving on...");
                    return;
                }

                else if (!loginArgs.Cancel)
                {
                    AwfulDebugger.AddLog(this, AwfulDebugger.Level.Info, "User has input credentials; retrying...");

                    Username = loginArgs.Username;
                    Password = loginArgs.Password;
                    AwfulWebRequest.SetCookieJar(login.Authenticate(Username, Password));
                    tries++;
                }

                // automatically fail on cancel
                else
                {
                    throw new Exception("User cancelled authentication.");
                }
            }

            // after too many tries throw the white flag.
            if (tries > 3)
            {
                throw new Exception("User failed to authenticate.");
            }

            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "END Authenticate()");
        }
Exemplo n.º 7
0
        public static bool Login(this UserMetadata user, string password)
        {
            var  login   = new AwfulLoginClient();
            var  cookies = login.Authenticate(user.Username, password);
            bool success = false;

            if (cookies != null)
            {
                success      = true;
                user.Cookies = cookies;
                AwfulWebRequest.SetCredentials(user);
            }

            return(success);
        }
Exemplo n.º 8
0
        private Uri InitiateReply(ThreadReplyData data, int timeout = DEFAULT_TIMEOUT)
        {
            // create request
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Creating Reply Request...");

            string url     = "http://forums.somethingawful.com/newreply.php";
            var    request = AwfulWebRequest.CreateFormDataPostRequest(url, REPLY_CONTENT_TYPE);

            if (request == null)
            {
                throw new NullReferenceException("request is not an http request.");
            }

            request.AllowAutoRedirect = true;

            // begin request stream
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Beginning Request Stream...");

            var signal = new AutoResetEvent(false);
            var result = request.BeginGetRequestStream(callback => { signal.Set(); }, request);

            signal.WaitOne();

            // process request
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Creating Form Data...");

            FormDataDelegate replyFormData = () => { return(GetReplyMultipartFormData(data)); };
            var success = ProcessReplyRequest(result, replyFormData);

            // begin response
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Waiting for response...");

            request = result.AsyncState as HttpWebRequest;
            result  = request.BeginGetResponse(callback => { signal.Set(); }, request);
            signal.WaitOne(timeout);

            if (!result.IsCompleted)
            {
                throw new TimeoutException("Thread Reply: timeout reached");
            }

            // process response and return status value
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Processing response...");

            Uri redirect = HandleGetResponse(result);

            return(redirect);
        }
Exemplo n.º 9
0
        private Uri InitiateEditRequest(PostEditData data, int timeout = CoreConstants.DEFAULT_TIMEOUT_IN_MILLISECONDS)
        {
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Begin Edit Request...");

            string url     = "http://forums.somethingawful.com/editpost.php";
            var    request = AwfulWebRequest.CreateFormDataPostRequest(url, EDIT_CONTENT_TYPE);

            if (request == null)
            {
                throw new NullReferenceException("request is not an http request.");
            }

            request.AllowAutoRedirect = true;

            // begin request stream
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Processing request stream...");

            var signal = new AutoResetEvent(false);
            var result = request.BeginGetRequestStream(callback => { signal.Set(); }, request);

            signal.WaitOne();

            // process request
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Creating form data...");

            FormDataDelegate editFormData = () => { return(GetEditMultipartFormData(data)); };
            var success = ProcessReplyRequest(result, editFormData);

            // begin response
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Waiting for server response...");

            request = result.AsyncState as HttpWebRequest;
            result  = request.BeginGetResponse(callback => { signal.Set(); }, request);
            signal.WaitOne(timeout);

            if (!result.IsCompleted)
            {
                throw new TimeoutException("The timeout was reached while trying to send an edit request.");
            }

            // process response and return status value
            AwfulDebugger.AddLog(this, AwfulDebugger.Level.Debug, "Processing server response...");

            Uri redirect = HandleGetResponse(result);

            return(redirect);
        }
Exemplo n.º 10
0
        private bool SendPost(string postData, string url)
        {
            bool result      = false;
            var  signal      = new AutoResetEvent(false);
            var  httpRequest = AwfulWebRequest.CreatePostRequest(url);

            //Logger.AddEntry("Send Post start...");

            // Synchronous Post Request!
            var state = httpRequest.BeginGetRequestStream(callback => { signal.Set(); }, httpRequest);

            signal.WaitOne();
            BeginPostRequest(postData, state);

            // Synchronous Post Response!
            var request = state.AsyncState as WebRequest;

            state = request.BeginGetResponse(callback => { signal.Set(); }, request);
            signal.WaitOne();

            result = ProcessPostResponse(state);
            return(result);
        }
Exemplo n.º 11
0
        private HttpWebRequest InitializePostRequest(string url)
        {
            HttpWebRequest request = AwfulWebRequest.CreateFormDataPostRequest(url, "application/x-www-form-urlencoded");

            return(request);
        }
Exemplo n.º 12
0
 public void SetUserAndProceed(UserMetadata user)
 {
     AwfulWebRequest.SetCredentials(user);
     this.Ignore = true;
 }