Exemple #1
0
            public ApiResult PostEdit(Page page, string text, string reason, string section = null, bool minor = false, bool watch = false, bool SuppressSummary = false, bool Create = true)
            {
                ApiResult result = new ApiResult(null, null, null);

                string token = null;
                bool InvalidToken = false;

                while (!InvalidToken)
                {
                    while (token == null)
                    {
                        if (section != null || Variables.EditToken == null)
                        {
                            result = ApiRequest("action=query&prop=info&intoken=edit&titles=" + System.Web.HttpUtility.UrlEncode(page.Name));
                            if (result.ResultInError)
                            {
                                return result;
                            }
                            token = GetParameter(result.Text, "edittoken");
                        }
                    }
                }

                return result;
            }
Exemple #2
0
        public static LoginResult DoLogin()
        {
            Core.History("DoLogin()");
            try
            {
                //Get the result of the api login request
                ApiResult result = new ApiResult();
                result = ApiRequest("action=login", "lgname=" + System.Web.HttpUtility.UrlEncode(Config.Username), Config.Project);
                //If this returns as null then the login has failed
                if (result == null || result.Text == null)
                {
                    Core.DebugLog("The request returned a null value");
                    return LoginResult.Failed;
                }

                //If no token is found (doesnt match regex) then the login has failed
                if (System.Text.RegularExpressions.Regex.Match(result.Text, "token=\"[0-9A-Za-z]*\"").Success == false)
                {
                    Core.DebugLog("Request did not match the login regex");
                    return LoginResult.Failed;
                }

                //This means that there must be a token, So lets get this token
                Login.Token = System.Text.RegularExpressions.Regex.Match(result.Text, "token=\"[0-9A-Za-z]*\"").Value;
                //And format it properly
                Login.Token = Login.Token.Replace("\"", "");
                Login.Token = Login.Token.Replace("token=", "");

                //Now we will do a request with our new token
                result = ApiRequest("action=login", "lgname=" + System.Web.HttpUtility.UrlEncode(Config.Username) + "&lgpassword="******"&lgtoken=" + Login.Token, Config.Project);

                //As this has returned as null the login has probably failed
                if (result.Text == null)
                {
                    Core.WriteLog("Request to login (with token) returned a null");
                    return LoginResult.Failed;
                }

                //Now we will try and match all of the other possible values
                if (result.Text.Contains("result=\"Success\""))
                {
                    // Logged in
                    return LoginResult.Success;
                }
                if (result.Text.Contains("result=\"Illegal\""))
                {
                    return LoginResult.Illegal;
                }
                if (result.Text.Contains("result=\"NotExists\""))
                {
                    return LoginResult.NotExists;
                }
                if (result.Text.Contains("result=\"WrongPass\""))
                {
                    return LoginResult.WrongPass;
                }
                if (result.Text.Contains("result=\"NoName\""))
                {
                    return LoginResult.NoName;
                }
                if (result.Text.Contains("result=\"EmptyPass\""))
                {
                    return LoginResult.EmptyPass;
                }
                if (result.Text.Contains("result=\"Throttled\""))
                {
                    return LoginResult.Throttled;
                }
                if (result.Text.Contains("result=\"Blocked\""))
                {
                    return LoginResult.Blocked;
                }
                if (result.Text.Contains("result=\"NeedToken\""))
                {
                    return LoginResult.NeedToken;
                }
            }
            catch (Exception x)
            {
                Core.ExceptionHandler(x);
            }

            //TODO: This result we have not yet accounted for (see list below)
            //WrongPluginPass
            //CreateBlocked
            //Throttled
            //Blocked
            //mustbeposted
            //NeedToken
            return LoginResult.None;
        }
Exemple #3
0
            /// <summary>
            /// Perform API request
            /// </summary>
            /// <param name="Query">Query data</param>
            /// <param name="Post">Post string</param>
            /// <param name="CurrentProject"></param>
            /// <returns></returns>
            public static ApiResult ApiRequest(string Query, string Post = "", string CurrentProject = "")
            {
                Core.History("Request.ApiRequest()");
                if (CurrentProject == "")
                {
                    // retrieve project in case we don't know it
                    CurrentProject = Config.Project;
                }
                string URL; // url of request
                if (CurrentProject == "meta")
                {
                    URL = Config.Metawiki; // meta has specific url so we need to put it from config file
                }
                else
                {
                    URL = Config.Projects[CurrentProject];
                }

                ApiResult return_value = new ApiResult();

                string Result = "";
                int Retries = Config.RequestAttempts;

                while (Retries > 0 && (Result == ""))
                {
                    if (Retries < Config.RequestAttempts)
                    {
                        System.Threading.Thread.Sleep(Config.RequestRetryInterval); // timeout before requests to save resources
                    }
                    Retries--;

                    try
                    {
                        // Perform a request now and catch all exceptions caused by connection
                        Result = DoWebRequest(URL + Config.WikiPath + "api.php?format=xml&" + Query, Post);
                    }
                    catch (System.Net.WebException Connectionx)
                    {
                        if (Connectionx.Status == System.Net.WebExceptionStatus.Timeout)
                        {
                            return new ApiResult(null, "error-timeout", Languages.Get("error"));
                        }
                        else
                        {
                            return new ApiResult(null, "error-unknown", Languages.Get("error"));
                        }
                    }

                    if (Result == null)
                    {
                        return new ApiResult("", "error-unknown", "error when processing api");
                    }
                    if (Result == "")
                    {
                        return new ApiResult("", "null", Languages.Get("error-noresponse"));
                    }
                    if (Result.StartsWith("MediaWiki API is not enabled"))
                    {
                        return new ApiResult("", "error-api-disabled", "");
                    }
                    if (Core.FindString(Result, "<error") != "")
                    {
                        return new ApiResult(Result, System.Web.HttpUtility.HtmlDecode(Core.FindString(Result, "<error")));
                    }
                    return new ApiResult(Result, "", "");

                }

                return return_value;
            }