Wikipedia:Requests for adminship/TawkerbotTorA/code

From Wikipedia, the free encyclopedia

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using WikiFunctions;

namespace TorList
{
    class Program
    {
        static void Main(string[] args)
        {
            string TorDirectory = "";
            WebClient wc = new WebClient();
            string Output = "";
            Editor ed = new Editor();
            //path to the wiki :)
            ed.IndexPath = "http://wiki.wikipedia.org/wiki";
            ed.LogIn("TawkerbotTorA", "GUESSWHAT - A PASSWORD GOES HERE.......");

            TorDirectory = wc.DownloadString("http://tor.noreply.org/tor/");

            Regex RouterRegex = new Regex(@"router\ [^\s]+\ ([12]?\d?\d.[12]?\d?\d.[12]?\d?\d.[12]?\d?\d)\ \d+\ " +
                @"\d+ \d+\r?\n.+?(\r?\n(accept|reject)\ (([12]?\d?\d.[12]?\d?\d.[12]?\d?\d.[12]?\d?\d)|\*)\:(\d+" +
                @"(-\d+)?|\*))+",
                RegexOptions.Singleline);

            MatchCollection matches = RouterRegex.Matches(TorDirectory);

            int counter;

            foreach (Match m in matches)
            {
                counter++;

                if (counter % 3 == 0) {
                    if (ed.GetWikiText("User:Werdna/StopTBTA").Contains("Stop"))
                        return; //Shit hit the fan
                }

                string IP;
                string AccessPolicy = "";

                //Steal the access policy and IP.
                Regex ServerIPRegex = new Regex(@"router\ [^\s]+\ ([12]?\d?\d.[12]?\d?\d.[12]?\d?\d.[12]?\d?\d)");
                Regex ExitPolicyRegex = new Regex(@"(accept|reject)\ (([12]?\d?\d.[12]?\d?\d.[12]?\d?\d.[12]?\d?\d)|\*)" +
                    @":(\d+(-\d+)?|\*)");

                Match ServerIPMatch;
                MatchCollection ExitPolicyMatches;

                ServerIPMatch = ServerIPRegex.Match(m.Value);
                ExitPolicyMatches = ExitPolicyRegex.Matches(m.Value);

                foreach (Match p in ExitPolicyMatches)
                {
                    AccessPolicy += "*" + p.Value + "\r\n";
                }

                IP = ServerIPMatch.Groups[1].Value;

                if ( (AccessPolicy.Contains("accept *:*") || AccessPolicy.Contains("accept *:80") ) &&
                    !AccessPolicy.Contains("reject *:80"))
                {
                    Output += "==Tor Server {{subst:vandal|" + IP + "}}==\r\n;Access Policy:\r\n";
                    Output += AccessPolicy;

                    ed.Unblock(IP, "Automatically unblocking Tor node" + " for migration to AnonOnly NoCreate" + ".");
                    ed.Block(IP, "1 month", true, true, "Tor ''(Anonymous only, with account creation disabled)''");
                }
            }

            ed.EditPage("User:Werdna/Tor List", Output, "Automatically listing current Tor nodes", false);
        }
    }
}