From d1545c00f7c937932a652debcea19f8807f46233 Mon Sep 17 00:00:00 2001 From: MallocNull Date: Tue, 24 Jun 2014 10:34:48 -0500 Subject: [PATCH] base work pt 2 woaher --- bot/bot/Bot.cs | 13 ++++++++++--- bot/bot/Query.cs | 28 ++++++++++++++++++++++------ bot/bot/ResponseCaller.cs | 4 ++-- bot/bot/StringSanitizer.cs | 13 +++++++++++++ bot/bot/bot.csproj | 1 + bot/bot/responses/jumble.cs | 11 ++++++++++- bot/bot/responses/replace.cs | 11 ++++++++++- 7 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 bot/bot/StringSanitizer.cs diff --git a/bot/bot/Bot.cs b/bot/bot/Bot.cs index 53dac8d..7724d8c 100644 --- a/bot/bot/Bot.cs +++ b/bot/bot/Bot.cs @@ -14,20 +14,27 @@ using MySql.Data.MySqlClient; using System.Threading; namespace bot { - class Bot { + class Bot : StringSanitizer { public static MySqlConnection conn; + static string tmp; static void Main(string[] args) { conn = new MySqlConnection("SERVER="+ _G.serveraddr +";DATABASE="+ _G.dbname +";UID="+ _G.dbuser +";PASSWORD="+ _G.dbpass +";"); conn.Open(); + tmp = "DELETE FROM resptypes WHERE "; foreach(Type t in ResponseCaller.getResponseTypes()) { string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null); - try { (new MySqlCommand("UPDATE `resptypes` SET friendlyname='" + typeInfo[1].Replace("'", "\\'") + "',description='" + typeInfo[2].Replace("'", "\\'") + "' WHERE name='" + typeInfo[0] + "'", conn)).ExecuteNonQuery(); } catch(Exception e) { } - try { (new MySqlCommand("INSERT INTO `resptypes` (name,friendlyname,description) VALUES ('" + typeInfo[0] + "','" + typeInfo[1].Replace("'", "\\'") + "','" + typeInfo[2].Replace("'", "\\'") + "')", conn)).ExecuteNonQuery(); } catch(Exception e) { } + tmp += "name<>'"+ typeInfo[0] +"' AND "; + Query.Quiet("UPDATE `resptypes` SET friendlyname='" + Sanitize(typeInfo[1]) + "',description='" + Sanitize(typeInfo[2]) + "' WHERE name='" + typeInfo[0] + "'", conn); + Query.Quiet("INSERT INTO `resptypes` (name,friendlyname,description) VALUES ('" + typeInfo[0] + "','" + Sanitize(typeInfo[1]) + "','" + Sanitize(typeInfo[2]) + "')", conn); } + + tmp = tmp.Substring(0, tmp.Length - 5); + Query.Quiet(tmp, conn); + while(true) ; } } } diff --git a/bot/bot/Query.cs b/bot/bot/Query.cs index 4795fe6..fc35c4f 100644 --- a/bot/bot/Query.cs +++ b/bot/bot/Query.cs @@ -7,16 +7,32 @@ using MySql.Data.MySqlClient; namespace bot { static class Query { - static object Scalar(string query, MySqlConnection conn) { - return (new MySqlCommand(query, conn)).ExecuteScalar(); + static public object Scalar(string query, MySqlConnection conn) { + try { + return (new MySqlCommand(query, conn)).ExecuteScalar(); + } catch(Exception e) { + Console.WriteLine(e.Message); + return null; + } } - static void Quiet(string query, MySqlConnection conn) { - (new MySqlCommand(query, conn)).ExecuteNonQuery(); + static public bool Quiet(string query, MySqlConnection conn) { + try { + (new MySqlCommand(query, conn)).ExecuteNonQuery(); + return true; + } catch(Exception e) { + Console.WriteLine(e.Message); + return false; + } } - static MySqlDataReader Reader(string query, MySqlConnection conn) { - // TODO write this + static public MySqlDataReader Reader(string query, MySqlConnection conn) { + try { + return (new MySqlCommand(query, conn)).ExecuteReader(); + } catch(Exception e) { + Console.WriteLine(e.Message); + return null; + } } } } diff --git a/bot/bot/ResponseCaller.cs b/bot/bot/ResponseCaller.cs index a09bebf..54ea84f 100644 --- a/bot/bot/ResponseCaller.cs +++ b/bot/bot/ResponseCaller.cs @@ -14,9 +14,9 @@ namespace bot { responseTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.responses", StringComparison.Ordinal)).ToArray(); } - public static void callResponse(String responseName) { + public static void callResponse(String responseName, string parameters, Message msg) { loadResponseTypes(); - //responseTypes[0].GetMethod("test").Invoke(null, ""); + responseTypes.First(t => t.Name == responseName).GetMethod("performOperation").Invoke(null, new Object[]{(object)parameters, (object)msg}); } public static Type[] getResponseTypes() { diff --git a/bot/bot/StringSanitizer.cs b/bot/bot/StringSanitizer.cs new file mode 100644 index 0000000..9078808 --- /dev/null +++ b/bot/bot/StringSanitizer.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace bot { + class StringSanitizer { + public static String Sanitize(String str) { + return str.Replace("'", "\\'"); + } + } +} diff --git a/bot/bot/bot.csproj b/bot/bot/bot.csproj index 74d6944..f11bed8 100644 --- a/bot/bot/bot.csproj +++ b/bot/bot/bot.csproj @@ -94,6 +94,7 @@ + diff --git a/bot/bot/responses/jumble.cs b/bot/bot/responses/jumble.cs index 5d675ae..f603996 100644 --- a/bot/bot/responses/jumble.cs +++ b/bot/bot/responses/jumble.cs @@ -3,8 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Reflection; namespace bot.responses { class jumble { + static public string[] getInfo() { + return new string[] {"junble"/*typeof(jumble).Name*/, "Jumble Message", + "Takes all words in the sentence and rearranges them randomly, sending the result to the chat."}; + } + + static public void performOperation(string parameters, Message msg) { + + } } -} +} \ No newline at end of file diff --git a/bot/bot/responses/replace.cs b/bot/bot/responses/replace.cs index d6df557..6fb01bf 100644 --- a/bot/bot/responses/replace.cs +++ b/bot/bot/responses/replace.cs @@ -3,8 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Reflection; namespace bot.responses { class replace { + static public string[] getInfo() { + return new string[] {typeof(replace).Name, "Replace Phrase", + "Takes a message, replaces all instances of the specified phrase, and sends it to the chat."}; + } + + static public void performOperation(string parameters, Message msg) { + + } } -} +} \ No newline at end of file