gia la mensa e preparata

non mi voglio divertir
This commit is contained in:
MallocNull 2014-07-09 16:55:46 -05:00
parent d1545c00f7
commit 72cfd18bfe
13 changed files with 217 additions and 18 deletions

View file

@ -14,25 +14,59 @@ using MySql.Data.MySqlClient;
using System.Threading;
namespace bot {
class Bot : StringSanitizer {
public static MySqlConnection conn;
class Bot {
static string tmp;
static List<Response> responseList = new List<Response>();
public static void loadResponseList() {
responseList = new List<Response>();
var r = Query.Reader("SELECT * FROM `responses`", _G.conn);
while(r.Read()) {
responseList.Add(new Response(
r.GetString("conditions"),
r.GetInt32("respid"),
r.GetString("parameters"),
r.GetInt32("cooldown")));
}
r.Close();
}
static void Main(string[] args) {
conn = new MySqlConnection("SERVER="+ _G.serveraddr +";DATABASE="+ _G.dbname +";UID="+ _G.dbuser +";PASSWORD="+ _G.dbpass +";");
conn.Open();
_G.conn = new MySqlConnection("SERVER="+ _G.serveraddr +";DATABASE="+ _G.dbname +";UID="+ _G.dbuser +";PASSWORD="+ _G.dbpass +";");
_G.conn.Open();
_G.loadConfig();
tmp = "DELETE FROM resptypes WHERE ";
foreach(Type t in ResponseCaller.getResponseTypes()) {
string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null);
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 += "name<>'" + typeInfo[0] + "' AND ";
if((Int64)Query.Scalar("SELECT COUNT(*) FROM `resptypes` WHERE `name`='" + typeInfo[0] + "'", _G.conn) > 0)
Query.Quiet("UPDATE `resptypes` SET friendlyname='" + Sanitizer.Sanitize(typeInfo[1]) + "',description='" + Sanitizer.Sanitize(typeInfo[2]) + "' WHERE name='" + typeInfo[0] + "'", _G.conn);
else
Query.Quiet("INSERT INTO `resptypes` (name,friendlyname,description) VALUES ('" + typeInfo[0] + "','" + Sanitizer.Sanitize(typeInfo[1]) + "','" + Sanitizer.Sanitize(typeInfo[2]) + "')", _G.conn);
}
tmp = tmp.Substring(0, tmp.Length - 5);
Query.Quiet(tmp, conn);
Query.Quiet(tmp, _G.conn);
tmp = "DELETE FROM conditions WHERE ";
foreach(Type t in ConditionChecker.getConditions()) {
string[] typeInfo = (string[])t.GetMethod("getInfo").Invoke(null, null);
tmp += "name<>'" + typeInfo[0] + "' AND ";
if((Int64)Query.Scalar("SELECT COUNT(*) FROM `conditions` WHERE `name`='" + typeInfo[0] + "'", _G.conn) > 0)
Query.Quiet("UPDATE `conditions` SET friendlyname='" + Sanitizer.Sanitize(typeInfo[1]) + "' WHERE name='" + typeInfo[0] + "'", _G.conn);
else
Query.Quiet("INSERT INTO `conditions` (name,friendlyname) VALUES ('" + typeInfo[0] + "','" + Sanitizer.Sanitize(typeInfo[1]) + "')", _G.conn);
}
tmp = tmp.Substring(0, tmp.Length - 5);
Query.Quiet(tmp, _G.conn);
_G.updateHeartbeat();
_G.startThread(_G.pulseThread);
Console.WriteLine(_G.propername +" has started successfully.");
while(true) ;
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace bot {
class ConditionChecker {
static Type[] conditionTypes = null;
static void loadConditionTypes() {
if(conditionTypes == null)
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) {
loadConditionTypes();
return (bool)conditionTypes.First(t => t.Name == conditionName).GetMethod("performCheck").Invoke(null, new Object[] { (object)parameter });
}
public static Type[] getConditions() {
loadConditionTypes();
return conditionTypes;
}
}
}

View file

@ -24,7 +24,7 @@ namespace bot {
public Response(string conditions, int responseId, string parameters, int cooldown) {
this.conditions = conditions;
string typeName = (string)(new MySqlCommand("SELECT `name` FROM `resptypes` WHERE `id`=" + responseId, Bot.conn)).ExecuteScalar();
string typeName = (string)(new MySqlCommand("SELECT `name` FROM `resptypes` WHERE `id`=" + responseId, _G.conn)).ExecuteScalar();
this.responseType = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "bot.responses", StringComparison.Ordinal) && String.Equals(t.Name, typeName, StringComparison.Ordinal)).ToArray()[0];
this.parameters = parameters;
this.cooldown = cooldown;

View file

@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
namespace bot {
class StringSanitizer {
class Sanitizer {
public static String Sanitize(String str) {
return str.Replace("'", "\\'");
}

View file

@ -91,10 +91,12 @@
<ItemGroup>
<Compile Include="Bot.cs" />
<Compile Include="Condition.cs" />
<Compile Include="ConditionChecker.cs" />
<Compile Include="conditions\contains.cs" />
<Compile Include="Query.cs" />
<Compile Include="responses\jumble.cs" />
<Compile Include="responses\replace.cs" />
<Compile Include="StringSanitizer.cs" />
<Compile Include="Sanitizer.cs" />
<Compile Include="_G.cs" />
<Compile Include="Message.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@ -106,9 +108,7 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="conditions\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View file

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bot.conditions {
class contains {
static public string[] getInfo() {
return new string[] {typeof(contains).Name, "contains"};
}
static public bool performCheck(string check, string parameter) {
return check.Contains(parameter);
}
}
}

View file

@ -8,7 +8,7 @@ using System.Reflection;
namespace bot.responses {
class jumble {
static public string[] getInfo() {
return new string[] {"junble"/*typeof(jumble).Name*/, "Jumble Message",
return new string[] {typeof(jumble).Name, "Jumble Message",
"Takes all words in the sentence and rearranges them randomly, sending the result to the chat."};
}

30
www/config.php Normal file
View file

@ -0,0 +1,30 @@
<?php include("conn.php");
include("header.php"); ?>
<center>
<fieldset class="normal" style="padding-bottom: 0;">
<legend>General Configuration</legend>
<center>
<form method="post" action="">
<table border="0">
<tr><td style="text-align: right;">Default Cooldown:</td><td><input type="text" name="cooldown" size="6" value="<?php echo $config->cooldown; ?>" /> seconds</td></tr>
<tr><td style="text-align: right;">Parse ChatBot Messages:</td><td><select name="chatbot"><option value="1">Yes</option><option value="0"<?php if(!$config->parsechatbot) { ?> selected="true"<?php } ?>>No</option></select></td></tr>
<tr><td style="text-align: right;vertical-align: top;">Timezone:</td><td>
UTC
<select name="tzSign">
<option value="+">+</option>
<option value="-"<?php if(substr($config->timezone, 0, 1) == "-") { ?> selected="true"<?php } ?>>-</option>
</select>
<input type="text" name="tzHour" maxlength="2" value="<?php echo substr($config->timezone, 1, 2); ?>" style="width:20px;" />
:
<input type="text" name="tzMins" maxlength="2" value="<?php echo substr($config->timezone, 4); ?>" style="width:20px;" />
<br />DST? <input type="checkbox" name="dst"<?php if($config->dst) { ?> checked="true"<?php } ?> />
</td></tr>
<tr><td style="text-align: right;">Chat Username:</td><td><input type="text" name="username" value="<?php echo $config->username; ?>" /></td></tr>
<tr><td style="text-align: right;">Bot Name:</td><td><input type="text" name="username" value="<?php echo $config->name; ?>" /></td></tr>
<tr><td></td><td><input type="submit" name="changeConfig" value="Modify" /></td></tr>
</table>
</form>
</center>
</fieldset>
</center>
<?php include("footer.php"); ?>

2
www/footer.php Normal file
View file

@ -0,0 +1,2 @@
</body>
</html>

22
www/header.php Normal file
View file

@ -0,0 +1,22 @@
<html>
<head>
<title>AJAX Bot Administration</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<h1>Control Panel</h1>
<h4>
<?php
$request = substr($_SERVER["REQUEST_URI"], strrpos($_SERVER["REQUEST_URI"], "/")+1);
$request = strtolower(substr($request, 0, strrpos($request, ".")));
if(!checkIfLoggedIn()) header("Location: index.php?err=Session invalid.");
?>
<?php if($request == "" || $request == "index") { ?>Home<?php } else { ?><a href="index.php">Home</a><?php } ?> |
<?php if($request == "config") { ?>Configuration<?php } else { ?><a href="config.php">Configuration</a><?php } ?> |
<?php if($request == "resp") { ?>Responses<?php } else { ?><a href="resp.php">Responses</a><?php } ?> |
<?php if($request == "auto") { ?>Autonomous<?php } else { ?><a href="auto.php">Autonomous</a><?php } ?> |
<a href="jews.php">Logout</a>
</h4>
</center>

View file

@ -1,8 +1,51 @@
<?php
include("conn.php");
$err = $_GET["err"];
if($_POST["loginAttempt"]) {
if(mysql_num_rows(mysql_query("SELECT * FROM `admin` WHERE `username`='". mysql_real_escape_string($_POST['name']) ."' AND `password`='". hash('sha256',$_POST['pwd']) ."'")) > 0) {
$_SESSION["user"] = $_POST["name"];
$_SESSION["pwd"] = hash('sha256',$_POST['pwd']);
} else $err = "Failed to log in.";
}
if($_GET["jew"] == "true")
session_destroy();
?>
<?php if(!checkIfLoggedIn()) { ?>
<html>
<head>
<title>AJAX Bot Administration</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
</html>
<body>
<center>
<h1>Admin Login</h1>
<?php if($err) { ?><h3 style="color: red;"><?php echo $err; ?></h3><?php } ?>
<form method="post" action="index.php">
<table border="0">
<tr><td style="text-align: right;">Username:</td><td><input type="text" name="name" /></td></tr>
<tr><td style="text-align: right;">Password:</td><td><input type="password" name="pwd" /></td></tr>
<tr><td></td><td><input type="submit" value="Login" name="loginAttempt" /></td></tr>
</table>
</form>
</center>
</body>
</html>
<?php } else { ?>
<?php include("header.php"); ?>
<center>
<fieldset class="narrow">
<legend>Pulse</legend>
Last hearbeat sent
<?php
echo mysql_fetch_object(mysql_query("SELECT `heartbeat` FROM `updater` WHERE `id`=1"))->heartbeat;
echo " UTC". $config->timezone ."". (($config->dst)?" in accordance to daylight savings.":"disregarding daylight savings.");
?>
</fieldset>
<?php
?>
</center>
<?php include("footer.php"); ?>
<?php } ?>

5
www/jews.php Normal file
View file

@ -0,0 +1,5 @@
<?php
session_start();
session_destroy();
header("Location: index.php");
?>

19
www/style.css Normal file
View file

@ -0,0 +1,19 @@
.narrow {
width: 400px;
text-align: left;
}
.normal {
width: 600px;
text-align: left;
}
.wide {
width: 800px;
text-align: left;
}
body {
font-family: Times New Roman;
font-size: 16px;
}