User:Petrb/Petan Bot

// wikibot.cpp : main project file.


 * 1) include "stdafx.h"

using namespace System; using namespace System::Text; using namespace System::IO; using namespace DotNetWikiBot; using namespace System::Threading; using namespace System::Collections;

bool OK = true;

int Log (System::String^ message) {	message=message->Insert(0, "SYSTEM: "); String^ path = "petanbot_log1.txt"; Console::WriteLine(message); System::IO::File::AppendAllText( path, message + "\n" );

return 0; }

bool TLog (int id, System::String^ message) {	String^ path = "petanbot_log1.txt"; message=message->Insert(0, "TASK: "); //System::IO::File::AppendAllText( path, message + "\n" ); Console::WriteLine(message); return false; }

bool LogP ( System::String^ message) {	Console::WriteLine(message); return true; }

public ref class t { public: DotNetWikiBot::Site^ testwp; DotNetWikiBot::Site^ dewiki; DotNetWikiBot::Site^ meta; DotNetWikiBot::Site^ co; DotNetWikiBot::Site^ enwiki; bool active; t {		active = false; }	int Do {		TLog(1, "error: nothing to do"); return 1; }

void Init {	} };

public ref class task_4 : t { public: void Init {		TLog(4, "Loading task #4"); TLog(4, "Loaded task #4"); active = true; }	int Do2 {		if (active == false) {			return 2; }		String^ data = ""; String^ d = ""; String^ line; Page^ l = gcnew Page(dewiki, "Project:Huggle/Whitelist"); l->Load; data = l->text; ArrayList^ li = gcnew ArrayList; StringReader^ r = gcnew StringReader(data); while (line = r->ReadLine) {		if (li->Contains(line->ToLower) != true) {d = d + line + "\n"; } li->Add(line->ToLower); }	l->text=d; if ( data != d ) { l->Save(d, "Removing duplicate entries (bot)", true); } delete l;	delete data; return 1; }	int Do {		if (active == false) {			return 2; }		String^ data = ""; String^ d = ""; String^ line; Page^ l = gcnew Page(enwiki, "Project:Huggle/Whitelist"); l->Load; data = l->text; ArrayList^ li = gcnew ArrayList; StringReader^ r = gcnew StringReader(data); while (line = r->ReadLine) {		if (li->Contains(line->ToLower) != true) {d = d + line + "\n"; } li->Add(line->ToLower); }	l->text=d; if ( data != d ) { l->Save(d, "Removing duplicate entries (bot)", true); } //l.Save("Cleaning the whitelist"); delete l;	delete data; //Do2; Thread::Sleep(3800000); return 1; } };

public ref class task_1 : t { public: void Init {		TLog(1, "Loading task #1"); TLog(1, "Loaded #1"); active = true; }	int Do {		return 0; } };

public ref class task_2 : t { public: int Check {	DotNetWikiBot::Page^ shut = gcnew DotNetWikiBot::Page(enwiki, "User:Petan-Bot/Shutdown"); shut->Load; if (shut->text->Contains("if you want to shutdown this bot change this to the shutdown reason and bot will be terminated") == false) {	Log("Terminated with: " + shut->text); active = false; String^ path = "petanbot_log1.txt"; String^ s = ""; System::IO::StreamReader^ sw = System::IO::File::OpenText(path); DotNetWikiBot::Page^ bp = gcnew DotNetWikiBot::Page(testwp, "User:Petan-Bot/Status"); bp->Load; bp->text = "System log:\n\n "; while (s = sw->ReadLine ) {        bp->text = bp->text + "\n" + s;	} bp->text = bp->text + " "; delete sw; bp->Save; OK = false; }	//Slee12000; return 2; }	int Do {	Check; String^ path = "petanbot_log1.txt"; System::IO::StreamReader^ sw = System::IO::File::OpenText(path); String^ s = ""; DotNetWikiBot::Page^ bp = gcnew DotNetWikiBot::Page(testwp, "User:Petan-Bot/Status"); bp->Load; bp->text = "System log:\n\n "; while (s = sw->ReadLine ) {        bp->text = bp->text + "\n" + s;	} bp->text = bp->text + " "; delete sw; bp->Save; if (active == false) {		 OK = false; }	Thread::Sleep(25000); return 0; }	void Init {		TLog(2, "Loading task #2"); //System::Threading::ThreadStart^ tl = gcnew System::Threading::ThreadStart(this, Chec); //System::Threading::Thread^ term = gcnew System::Threading::Thread(tl); active = true; }

};

public ref class task_3 : t { public: array^ src; array^ tm; int Init {		//String^ pagetext = ""; int curr = 0; TLog(3, "Loading task #3"); System::IO::StreamReader^ sw = System::IO::File::OpenText("task"); src = gcnew array(32); tm = gcnew array(32); tm->SetValue("Template:Huggle/warn-delete-1", 0); src->SetValue("Template:Uw-delete1",0); src->SetValue("Template:Uw-delete2",1); src->SetValue("Template:Uw-delete3",2); src->SetValue("Template:Uw-delete4",3); src->SetValue("Template:Uw-spam1", 4); src->SetValue("Template:Uw-spam2", 5); src->SetValue("Template:Uw-spam3", 6); src->SetValue("Template:Uw-spam4", 7); src->SetValue("Template:Uw-biog1", 8); src->SetValue("Template:Uw-biog2", 9); src->SetValue("Template:Uw-biog3", 10); src->SetValue("Template:Uw-biog4", 11); src->SetValue("Template:Uw-vandalism1", 12 ); src->SetValue("Template:Uw-vandalism2", 13 ); src->SetValue("Template:Uw-vandalism3", 14 ); src->SetValue("Template:Uw-vandalism4", 15 ); src->SetValue("Template:Uw-npa1", 16 ); src->SetValue("Template:Uw-npa2", 17 ); src->SetValue("Template:Uw-blank1", 18 ); src->SetValue("Template:Uw-blank2", 19 ); src->SetValue("Template:Uw-blank3", 20 ); src->SetValue("Template:Uw-blank4", 21 ); src->SetValue("Template:Uw-error1", 22 ); src->SetValue("Template:Uw-error2", 23 ); src->SetValue("Template:Uw-error3", 24 ); src->SetValue("Template:Uw-test1", 25 ); src->SetValue("Template:Uw-test2", 26 ); src->SetValue("Template:Uw-npov1", 27 ); src->SetValue("Template:Uw-npov2", 28 ); src->SetValue("Template:Uw-npov3", 29 ); src->SetValue("Template", 30 );

tm->SetValue("Template:Huggle/warn-spam-1", 4 ); tm->SetValue("Template:Huggle/warn-spam-2", 5 ); tm->SetValue("Template:Huggle/warn-spam-3", 6 ); tm->SetValue("Template:Huggle/warn-spam-4", 7); tm->SetValue("Template:Huggle/warn-delete-2", 1); tm->SetValue("Template:Huggle/warn-delete-3", 2); tm->SetValue("Template:Huggle/warn-delete-4", 3); tm->SetValue("Template:Huggle/warn-bio-1", 8 ); tm->SetValue("Template:Huggle/warn-bio-2", 9 ); tm->SetValue("Template:Huggle/warn-bio-3", 10 ); tm->SetValue("Template:Huggle/warn-bio-4", 11 ); tm->SetValue("Template:Huggle/warn-1", 12 ); tm->SetValue("Template:Huggle/warn-2", 13 ); tm->SetValue("Template:Huggle/warn-3", 14 ); tm->SetValue("Template:Huggle/warn-4", 15 ); tm->SetValue("Template:Huggle/warn-attack-1", 16 ); tm->SetValue("Template:Huggle/warn-attack-2", 17 ); tm->SetValue("Template:Huggle/warn-blank-1", 18 ); tm->SetValue("Template:Huggle/warn-blank-2", 19 ); tm->SetValue("Template:Huggle/warn-blank-3", 20 ); tm->SetValue("Template:Huggle/warn-blank-4", 21 ); tm->SetValue("Template:Huggle/warn-error-1", 22 ); tm->SetValue("Template:Huggle/warn-error-2", 23 ); tm->SetValue("Template:Huggle/warn-error-3", 24 ); tm->SetValue("Template:Huggle/warn-test-1", 25 ); tm->SetValue("Template:Huggle/warn-test-2", 26 ); tm->SetValue("Template:Huggle/warn-npov-1", 27 ); tm->SetValue("Template:Huggle/warn-npov-2", 28 ); tm->SetValue("Template:Huggle/warn-npov-3", 29 ); tm->SetValue("Template:Huggle/warn-npov-4", 30 ); active = true; return 0; }	int Do {		if (active == false) {		 return 0; }		int curr = 0; Page^ x;		String^ line; String^ pagetext = ""; while (curr < 30) {		x = gcnew Page(enwiki, src->GetValue(curr)->ToString); String^ link; link = ""; System::Text::RegularExpressions::Regex^ rege = gcnew System::Text::RegularExpressions::Regex("\\[\\[simp.*:.*\\]\\]|\\[\\[..:.*\\]\\]"); pagetext = " "; x->Load; pagetext = pagetext + x->text; delete x;		x = gcnew Page(enwiki, tm->GetValue(curr)->ToString); //x = gcnew Page(testwp, "Template:Hugglev1"); x->Load; StringReader^ r = gcnew StringReader(x->text); if ((pagetext->Contains("page=") != true) && (pagetext->Contains("#REDIRE") == false)) {		pagetext = pagetext->Replace("", "Thank you. "); pagetext = pagetext->Replace("", "Thank you, "); pagetext = pagetext->Replace(":", ": with [ this edit]"); pagetext = pagetext->Replace("Templatesnotice", "Huggle/TemplateNotice"); pagetext = pagetext->Replace("", "Thank you. "); link = " \n"; while (line = r->ReadLine) {			if (rege->IsMatch(line) == true) { link = link + line + "\n"; } }		if ( link == " \n" ) { link =""; } else {				link = link + "\n "; }		pagetext = rege->Replace(pagetext, ""); pagetext = pagetext + link; delete rege; delete x;		x->Save(pagetext, "Updating the template according to project user templates (bot)", false); }else { if (pagetext->Contains("#REDIRECT") != true) {			pagetext = pagetext->Replace("only>Uw3", "only>Huggle/uw-3"); pagetext = pagetext->Replace("only>Uw4", "only>Huggle/uw-4"); pagetext = pagetext->Replace("", ": with [ this edit]"); pagetext = pagetext->Replace("", " "); link = " \n"; while (line = r->ReadLine) {				if (rege->IsMatch(line) == true) { link = link + line + "\n"; } }			if ( link == " \n" ) { link =""; } else {			link = link + "\n "; }			pagetext = rege->Replace(pagetext, ""); pagetext = pagetext + link; delete rege; delete x;			pagetext = pagetext->Replace("Templatesnotice", "Huggle/TemplateNotice"); x->Save(pagetext, "Updating the template according to project user templates (bot)", false); }		} 		delete x;		delete link; delete r;		delete rege; System::Threading::Thread::Sleep(8000); curr = 1 + curr; }		Thread::Sleep(36000000); return 2; } };

public ref class task_5 : t { public: void Init {		TLog(5, "Initializing task #5"); active = true; }	int Do {		//Page^ na = gcnew Page(enwiki, "Wikipedia:Requests for feedback/navigation"); //na->Load; DateTime^ month = gcnew DateTime; DateTime^ x;		x = month->Now; String^ cm; String^ year = "2011"; int a = x->Month; switch (a) {		case 1: cm = "January"; case 2: cm = "February"; case 3: cm = "March"; case 4: cm = "April"; case 5: cm = "May"; case 6: cm = "June"; case 7: cm = "July"; case 8: cm = "Aug"; case 9: cm = "September"; case 10: cm = "November"; }		String^ s1 = "1·"; Thread::Sleep(10000000); return 2; } };

public ref class task_6 : t { public: void Init {		TLog(6, "Initialising task #6"); active = true; }	int Do {		String^ td = "t4"; String^ pt; String^ page_name = ""; Page^ x;		System::IO::StreamReader^ sw = System::IO::File::OpenText(td); while ( page_name = sw->ReadLine ) {		x = gcnew DotNetWikiBot::Page(enwiki, page_name); x->Load; if (x->text->Contains("{{WikiProject United States") == false) {			pt = " \n"; pt = pt + x->text; x->Save(pt, "Inserting tag (bot)", true); }			delete x;			Thread::Sleep(10000); }		delete sw; return 2; } };

public ref class PBot : Bot { public: String^ login; String^ pw; bool started; DotNetWikiBot::Site^ testwp; DotNetWikiBot::Site^ dewiki; DotNetWikiBot::Site^ meta; DotNetWikiBot::Site^ commons; DotNetWikiBot::Site^ enwiki; DotNetWikiBot::Site^ cswiki; System::Threading::Thread^ t1; System::Threading::Thread^ t2; System::Threading::Thread^ t3; System::Threading::Thread^ t4; System::Threading::Thread^ t5; System::Threading::Thread^ t6; task_1^ job_x1; task_2^ job_x2; task_3^ job_x3; task_4^ job_x4; task_5^ job_x5; task_6^ job_x6; void d1 {	while (OK == true && job_x1->active == true) {		job_x1->Do; Thread::Sleep(500000); }	}	void d2 {	while (OK == true && job_x2->active == true) {		job_x2->Do; }	}	void d3 {	while (OK == true) {		job_x3->Do; }	}	void d4 {	while (OK == true) {		job_x4->Do; }	}	void d5 {	while (OK) {			job_x5->Do; }	}	void d6 {		while (OK) {			job_x6->Do; }	}

void Initialise {		Log("Started petan-bot version 1.0.6, timeout disabled"); pw = gcnew String(""); login = gcnew String("Petan-Bot"); Log(L"Connecting to english wp..."); enwiki = gcnew Site("en.wikipedia.org", login, pw); Log(L"Connecting to german wiki..."); dewiki = gcnew Site("de.wikipedia.org", login, pw); Log(L"Connecting to test wp..."); testwp = gcnew Site("test.wikipedia.org", login, pw); Log(L"Tasks..."); //System::ServerClass^ main = gcnew ServerClass; job_x1 = gcnew task_1; job_x1->enwiki = enwiki; job_x1->testwp = testwp; job_x2 = gcnew task_2; job_x2->testwp = testwp; job_x2->enwiki = enwiki; job_x3 = gcnew task_3; job_x3->enwiki = enwiki; job_x3->testwp = testwp; job_x4 = gcnew task_4; job_x4->enwiki = enwiki; job_x4->dewiki = dewiki; job_x4->testwp = testwp; job_x5 = gcnew task_5; job_x5->enwiki = enwiki; job_x6 = gcnew task_6; job_x6->enwiki = enwiki; //job_x2->meta = meta; //job_x2->dewiki = dewiki; //job_x2->cswiki = cswiki; job_x1->Init; job_x2->Init; job_x3->Init; job_x4->Init; job_x5->Init;

started = true; }

};

public ref class Kernel_Main { public: static void Start {       PBot^ Pb = gcnew PBot;

// Create the thread object, passing in the // serverObject.InstanceMethod method using a       // ThreadStart delegate. try {			Pb->Initialise;

Pb->t1 = gcnew Thread(           gcnew ThreadStart(Pb, &PBot::d1)); Pb->t2 = gcnew Thread(gcnew ThreadStart(Pb, &PBot::d2)); Pb->t3 = gcnew Thread(gcnew ThreadStart(Pb, &PBot::d3)); Pb->t4 = gcnew Thread(gcnew ThreadStart(Pb, &PBot::d4)); Pb->t5 = gcnew Thread(gcnew ThreadStart(Pb, &PBot::d5)); Pb->t6 = gcnew Thread(gcnew ThreadStart(Pb, &PBot::d6));

Pb->t1->Start; Pb->t2->Start; Pb->t3->Start; Pb->t4->Start; Pb->t5->Start;

} catch (String^ em) {		Log(" error " + em); }

while (OK != false) {			Thread::Sleep(10000); }		Pb->t1->Abort; Pb->t2->Abort; Pb->t3->Abort; Pb->t4->Abort; Pb->t5->Abort; delete Pb; } };

int main(array ^args) {   Log("Loading data"); Kernel_Main^ main; main = gcnew Kernel_Main; Log("Starting..."); main->Start; /*pb->t1 = gcnew Thread(ThreadStart(pb, &PBot::d1)); pb->t2 = gcnew Thread(ThreadStart(pb, &PBot::d2)); pb->t3 = gcnew Thread(ThreadStart(pb, &PBot::d3)); pb->t4 = gcnew Thread(ThreadStart(pb, &PBot::d4));*/ while (OK) {		System::Threading::Thread::Sleep(10200000); }

return 0; }