From 2cfef93e01a41343fddc07be9b2bbdfa5476a12c Mon Sep 17 00:00:00 2001 From: MallocNull Date: Fri, 11 Jul 2014 16:03:57 -0500 Subject: [PATCH] why is javascript so bad who knows --- .gitignore | 2 +- bot/bot/Bot.cs | 26 +++++- bot/bot/ConditionChecker.cs | 4 +- bot/bot/NavigationNode.cs | 58 +++++++++++- bot/bot/Pulse.cs | 46 ++++++++++ bot/bot/{_GG.cs => _G.cs} | 72 ++++++++------- bot/bot/bot.csproj | 9 +- bot/bot/conditions/contains.cs | 17 ---- bot/bot/conditions/msgcontains.cs | 17 ++++ bot/bot/dbinfo_generic.txt | 4 + www/config.php | 142 +++++++++++++++++++++++------- www/header.php | 1 + www/img/add.png | Bin 0 -> 733 bytes www/index.php | 13 ++- www/jews.php | 16 +++- www/resp.php | 129 +++++++++++++++++++++++++++ www/style.css | 10 +++ 17 files changed, 465 insertions(+), 101 deletions(-) create mode 100644 bot/bot/Pulse.cs rename bot/bot/{_GG.cs => _G.cs} (60%) delete mode 100644 bot/bot/conditions/contains.cs create mode 100644 bot/bot/conditions/msgcontains.cs create mode 100644 bot/bot/dbinfo_generic.txt create mode 100644 www/img/add.png create mode 100644 www/resp.php diff --git a/.gitignore b/.gitignore index e6f5bf4..2291f41 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ ## Temporary Exclusions ################# conn.php -_G.cs +dbinfo.txt .idea/ ################# diff --git a/bot/bot/Bot.cs b/bot/bot/Bot.cs index d4702ae..7dab118 100644 --- a/bot/bot/Bot.cs +++ b/bot/bot/Bot.cs @@ -17,8 +17,22 @@ namespace bot { class Bot { static string tmp; + static List navigationList = new List(); static List responseList = new List(); + public static void loadNavigationList() { + navigationList = new List(); + var r = Query.Reader("SELECT * FROM `navigate`", _G.conn); + while(r.Read()) { + navigationList.Add(new NavigationNode( + r.GetInt32("findtype"), + r.GetString("locator"), + r.GetInt32("action"), + r.GetString("parameter"))); + } + r.Close(); + } + public static void loadResponseList() { responseList = new List(); var r = Query.Reader("SELECT * FROM `responses`", _G.conn); @@ -33,10 +47,12 @@ namespace bot { } static void Main(string[] args) { - _G.conn = new MySqlConnection("SERVER="+ _G.serveraddr +";DATABASE="+ _G.dbname +";UID="+ _G.dbuser +";PASSWORD="+ _G.dbpass +";"); - _G.conn.Open(); + _G.loadDatabaseInfo(); + _G.conn = _G.spawnNewConnection(); + _G.errconn = _G.spawnNewConnection(); _G.loadConfig(); + loadResponseList(); tmp = "DELETE FROM resptypes WHERE "; foreach(Type t in ResponseCaller.getResponseTypes()) { @@ -62,9 +78,11 @@ namespace bot { tmp = tmp.Substring(0, tmp.Length - 5); Query.Quiet(tmp, _G.conn); - _G.updateHeartbeat(); + _G.driver = new FirefoxDriver(); + foreach(NavigationNode node in navigationList) + node.performNavigation(_G.driver); - _G.startThread(_G.pulseThread); + _G.startThread(Pulse.pulseThread); Console.WriteLine(_G.propername +" has started successfully."); diff --git a/bot/bot/ConditionChecker.cs b/bot/bot/ConditionChecker.cs index 020469e..e5b766f 100644 --- a/bot/bot/ConditionChecker.cs +++ b/bot/bot/ConditionChecker.cs @@ -14,9 +14,9 @@ namespace bot { conditionTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.conditions", StringComparison.Ordinal)).ToArray(); } - public static bool checkCondition(String conditionName, string check, string parameter) { + public static bool checkCondition(String conditionName, Message msg, string parameter) { loadConditionTypes(); - return (bool)conditionTypes.First(t => t.Name == conditionName).GetMethod("performCheck").Invoke(null, new Object[] { (object)parameter }); + return (bool)conditionTypes.First(t => t.Name == conditionName).GetMethod("performCheck").Invoke(null, new Object[] { (object)msg, (object)parameter }); } public static Type[] getConditions() { diff --git a/bot/bot/NavigationNode.cs b/bot/bot/NavigationNode.cs index e1ecaf8..a75b978 100644 --- a/bot/bot/NavigationNode.cs +++ b/bot/bot/NavigationNode.cs @@ -27,12 +27,66 @@ namespace bot { INDEXSELECT }; - + findTypes findtype; + string locator; + actionTypes action; + string parameter; public NavigationNode() { } - public void performNavigation(FirefoxDriver d) { + public NavigationNode(int ft, string l, int a, string p) { + findtype = (findTypes)ft; + locator = l; + action = (actionTypes)a; + parameter = p; + } + public bool performNavigation(FirefoxDriver d) { + IWebElement e = null; + try { + switch(findtype) { + case findTypes.GOTOURL: + d.Navigate().GoToUrl(locator); + break; + case findTypes.BYLINKTEXT: + e = (new WebDriverWait(d, new TimeSpan(0, 0, 900)).Until(ExpectedConditions.ElementExists(By.LinkText(locator)))); + break; + case findTypes.BYID: + e = (new WebDriverWait(d, new TimeSpan(0, 0, 900)).Until(ExpectedConditions.ElementExists(By.Id(locator)))); + break; + case findTypes.BYNAME: + e = (new WebDriverWait(d, new TimeSpan(0, 0, 900)).Until(ExpectedConditions.ElementExists(By.Name(locator)))); + break; + case findTypes.BYCLASS: + e = (new WebDriverWait(d, new TimeSpan(0, 0, 900)).Until(ExpectedConditions.ElementExists(By.ClassName(locator)))); + break; + case findTypes.BYTAG: + e = (new WebDriverWait(d, new TimeSpan(0, 0, 900)).Until(ExpectedConditions.ElementExists(By.TagName(locator)))); + break; + } + } catch(Exception err) { + _G.logError("Failed to find element "+ locator +" "+ findtype.ToString()); + return false; + } + + if(e != null) { + switch(action) { + case actionTypes.CLICK: + e.Click(); + break; + case actionTypes.TYPE: + e.SendKeys(parameter); + break; + case actionTypes.TEXTSELECT: + (new SelectElement(e)).SelectByText(parameter); + break; + case actionTypes.INDEXSELECT: + (new SelectElement(e)).SelectByIndex(Int32.Parse(parameter)); + break; + } + } + + return true; } } } diff --git a/bot/bot/Pulse.cs b/bot/bot/Pulse.cs new file mode 100644 index 0000000..527ea60 --- /dev/null +++ b/bot/bot/Pulse.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace bot { + class Pulse { + public static void updateHeartbeat() { + string beat = _G.getLocalTimeFromUTC().ToString(); + Query.Quiet("UPDATE `updater` SET `heartbeat`='" + beat + "' WHERE `id`=1", _G.conn); + } + + public static void checkUpdates() { + var r = Query.Reader("SELECT * FROM `updater` WHERE `id`=1", _G.conn); + r.Read(); + bool[] tmp = new bool[] { r.GetBoolean("responses"), r.GetBoolean("autonomous"), r.GetBoolean("config") }; + r.Close(); + if(tmp[0]) { + Bot.loadResponseList(); + Query.Quiet("UPDATE `updater` SET `responses`=0 WHERE `id`=1", _G.conn); + } + if(tmp[1]) { + // TODO implement update when autonomous is added + Query.Quiet("UPDATE `updater` SET `autonomous`=0 WHERE `id`=1", _G.conn); + } + if(tmp[2]) { + _G.loadConfig(); + Query.Quiet("UPDATE `updater` SET `config`=0 WHERE `id`=1", _G.conn); + } + } + + public static void pulseThread() { + DateTime t = new DateTime(0); + + while(true) { + if((DateTime.Now - t).TotalSeconds > 30) { + updateHeartbeat(); + checkUpdates(); + t = DateTime.Now; + Console.WriteLine("pulsed"); + } + } + } + } +} diff --git a/bot/bot/_GG.cs b/bot/bot/_G.cs similarity index 60% rename from bot/bot/_GG.cs rename to bot/bot/_G.cs index da5f9a5..7dc511c 100644 --- a/bot/bot/_GG.cs +++ b/bot/bot/_G.cs @@ -1,8 +1,4 @@ -/* - * MODIFY THIS FILE TO FIT YOUR SERVER'S CONFIGURATION! - */ - -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -18,14 +14,16 @@ using MySql.Data.MySqlClient; using System.Threading; namespace bot { - static class _GG { - const string serveraddr = "ADDR"; - const string dbuser = "NAME"; - const string dbpass = "PWD"; - const string dbname = "DATABASE_NAME"; + static class _G { + static string[] dbinfo = new string[4]; static List runningThreads = new List(); + public static FirefoxDriver driver; + /* + * TODO cross-thread driver protection + */ + public static string timezone = "+00:00"; public static bool observeDST = false; public static string username = ""; @@ -34,6 +32,19 @@ namespace bot { public static int defaultCooldown = 300; public static MySqlConnection conn; + public static MySqlConnection errconn; + + public static bool loadDatabaseInfo() { + try { + System.IO.StreamReader r = new System.IO.StreamReader("dbinfo.txt"); + for(int i = 0; i < 4; i++) + dbinfo[i] = r.ReadLine(); + } catch(Exception e) { + Environment.FailFast("Error attempting to read from dbinfo.txt: "+e.Message+"\n\nProper format:\nSERVER ADDRESS\nUSERNAME\nPASSWORD\nDATABASE_NAME"); + return false; + } + return true; + } public static bool isDaylightSavings() { return (observeDST) ? TimeZoneInfo.GetSystemTimeZones().First(o => o.DisplayName.ToLower().Contains("central time")).IsDaylightSavingTime(DateTime.UtcNow) : false; @@ -47,11 +58,6 @@ namespace bot { return (new DateTimeOffset(utcTime)).ToOffset(TimeSpan.Parse(timezone).Add(TimeSpan.FromHours(isDaylightSavings() ? 1 : 0))).DateTime; } - public static void updateHeartbeat() { - string beat = getLocalTimeFromUTC().ToString(); - Query.Quiet("UPDATE `updater` SET `heartbeat`='" + beat + "' WHERE `id`=1", conn); - } - public static void loadConfig() { var r = Query.Reader("SELECT * FROM `config` WHERE `id`=1", conn); r.Read(); @@ -61,31 +67,23 @@ namespace bot { timezone = r.GetString("timezone"); observeDST = r.GetBoolean("dst"); r.Close(); + Bot.loadNavigationList(); } - public static void checkUpdates() { - var r = Query.Reader("SELECT * FROM `updater` WHERE `id`=1", conn); - r.Read(); - if(r.GetBoolean("responses")) - Bot.loadResponseList(); - //if(r.GetBoolean("autonomous")) TODO implement with autonomous - - if(r.GetBoolean("config")) - loadConfig(); - r.Close(); - } - - public static void pulseThread() { - DateTime t = new DateTime(0); - - while(true) { - if((DateTime.Now - t).TotalSeconds > 30) { - updateHeartbeat(); - checkUpdates(); - t = DateTime.Now; - Console.WriteLine("pulsed"); - } + public static MySqlConnection spawnNewConnection() { + MySqlConnection tmp; + try { + tmp = new MySqlConnection("SERVER=" + dbinfo[0] + ";DATABASE=" + dbinfo[3] + ";UID=" + dbinfo[1] + ";PASSWORD=" + dbinfo[2] + ";"); + tmp.Open(); + } catch(Exception e) { + Environment.FailFast("Could not open database connection!"); + return null; } + return tmp; + } + + public static void logError(string err) { + (new MySqlCommand("INSERT INTO `error` (`time`,`msg`) VALUES ('"+ getLocalTimeFromUTC() +" UTC"+ timezone +"','"+err+"')", errconn)).ExecuteNonQuery(); } public delegate void threadFunc(); diff --git a/bot/bot/bot.csproj b/bot/bot/bot.csproj index 048aa25..1ffd3e8 100644 --- a/bot/bot/bot.csproj +++ b/bot/bot/bot.csproj @@ -92,8 +92,9 @@ - + + @@ -104,12 +105,14 @@ - - + + + +