Пример #1
0
        public async Task <bool> BanUser(Models.BannedEntity user)
        {
            var newBan = await bbDAL.BanUser(new Models.BannedEntity[] { user });

            if (!newBan)
            {
                return(false);
            }

            string reason = $"Banned {user.UserName} : {user.BanReason}";

            //only used if changing to allowing banning of multiple users at a time.
            //if (reason.Length > 255) reason = $"Banned {string.Join(",", userEntities.Select(e => e.EntityString))} for {string.Join(",", userEntities.Select(e => e.BanReason).Distinct())} by {string.Join(",", userEntities.Select(e => e.BannedBy).Distinct())}";
            //if (reason.Length > 255) reason = "Banned lots of things and the summary is too long for the description.. RIP";

            bool done  = false;
            int  count = 1;

            var ident = await userManager.FindByNameAsync(user.BannedBy);

            var webAgent = await agentPool.GetOrCreateWebAgentAsync(user.BannedBy, (uname, uagent, rlimit) =>
            {
                return(Task.FromResult <RedditSharp.RefreshTokenPoolEntry>(new RedditSharp.RefreshTokenPoolEntry(uname, ident.RefreshToken, rlimit, uagent)));
            });

            if (!ident.HasWiki)
            {
                throw new UnauthorizedAccessException("Need Wiki Permissions");
            }
            if (!ident.HasConfig)
            {
                throw new UnauthorizedAccessException("Need Config Permissions");
            }
            RedditSharp.Reddit rd = new RedditSharp.Reddit(webAgent, true);

            var wiki = new RedditSharp.Wiki(webAgent, user.SubName);

            //var wiki = new RedditSharp.WikiPage()
            while (!done && count < 5)
            {
                try
                {
                    done = await SaveAutoModConfig(reason, wiki);
                }
                catch (WebException ex)
                {
                    if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.Forbidden)
                    {
                        throw;
                    }
                    else
                    {
                        count++;
                    }
                    await Task.Delay(100);
                }
            }

            return(true);
        }
Пример #2
0
        public async Task <bool> SaveAutoModConfig(string editReason, RedditSharp.Wiki wiki)
        {
            RedditSharp.WikiPage automodWiki;
            string wikiContent = "";

            try
            {
                automodWiki = await wiki.GetPageAsync(AUTOMOD_WIKI_PAGE);

                wikiContent = WebUtility.HtmlDecode(automodWiki.MarkdownContent);
            }
            catch (WebException ex)
            {
                if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
                {
                    wikiContent = WIKI_SECTION_START_IDENTIFIER + DefaultBotConfigSection + WIKI_SECTION_END_IDENTIFIER;
                }
                else
                {
                    throw;
                }
            }
            string botConfigSection = "";
            bool   noStart          = false;
            bool   noEnd            = false;

            if (!wikiContent.Contains(WIKI_SECTION_START_IDENTIFIER))
            {
                noStart = true;
            }
            if (!wikiContent.Contains(WIKI_SECTION_END_IDENTIFIER))
            {
                noEnd = true;
            }

            if (noStart && noEnd)
            {
                wikiContent += $@"
{WIKI_SECTION_START_IDENTIFIER + DefaultBotConfigSection + WIKI_SECTION_END_IDENTIFIER}
";
            }

            else if (noStart || noEnd)
            {
                throw new Exception("Wiki contains a start or an end section, but not both");
            }

            string updatedWiki = wikiContent;

            int startBotSection  = wikiContent.IndexOf(WIKI_SECTION_START_IDENTIFIER) + WIKI_SECTION_START_IDENTIFIER.Length;
            int botSectionLength = wikiContent.IndexOf(WIKI_SECTION_END_IDENTIFIER, startBotSection) - startBotSection;

            if (botSectionLength < 0)
            {
                throw new Exception("End section identifier is before the start identifier");
            }
            botConfigSection = wikiContent.Substring(startBotSection, botSectionLength);


            var ents = await bbDAL.GetBannedUserNames(wiki.SubredditName);

            string entsString = string.Join(", ", ents.Select(e => "\"" + e + "\""));

            updatedWiki = updatedWiki.Remove(startBotSection, botSectionLength);
            if (ents.Count() > 0)
            {
                //Writing an empty match set to automod means it removes literally everything...
                updatedWiki = updatedWiki.Insert(startBotSection, String.Format(DefaultBotConfigSection, entsString));
            }
            await wiki.EditPageAsync(AUTOMOD_WIKI_PAGE, updatedWiki, reason : editReason);

            return(true);
        }