From b5e43662f0fa76771ba4e164ca4aea7e71a52e99 Mon Sep 17 00:00:00 2001 From: Shank Date: Mon, 30 May 2011 01:14:58 -0600 Subject: [PATCH] Kick + Ban --- TShockAPI/TShock.cs | 161 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 3 deletions(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index af0ffe76..040147af 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.IO; using Terraria; using TerrariaAPI; using TerrariaAPI.Hooks; @@ -11,6 +12,8 @@ namespace TShockAPI { public class TShock : TerrariaPlugin { + public static string saveDir = "./tshock/"; + public override string Name { get { return "TShock"; } @@ -32,7 +35,39 @@ namespace TShockAPI GameHooks.OnPostInitialize += OnPostInit; GameHooks.OnUpdate += new Action(OnUpdate); GameHooks.OnLoadContent += new Action(OnLoadContent); - ServerHooks.Chat += OnChat; + ServerHooks.OnChat += new Action(OnChat); + } + + /* + * Hooks: + * */ + + void OnChat(int ply, string msg, HandledEventArgs handler) + { + if (IsAdmin(ply)) + { + if (msg.Length > 5 && msg.Substring(0, 5) == "/kick") + { + string plStr = msg.Remove(0, 5).Trim(); + if (FindPlayer(plStr) == -1 || plStr == "") + { + Kick(FindPlayer(plStr), "You were kicked."); + Broadcast(plStr + " was kicked by " + FindPlayer(ply)); + } + handler.Handled = true; + } + + if (msg.Length > 4 && msg.Substring(0, 4) == "/ban") + { + string plStr = msg.Remove(0, 4).Trim(); + if (!(FindPlayer(plStr) == -1 || plStr == "")) + { + WriteBan(FindPlayer(plStr)); + Kick(FindPlayer(plStr), "You were banned."); + } + handler.Handled = true; + } + } } void OnLoadContent(Microsoft.Xna.Framework.Content.ContentManager obj) @@ -55,9 +90,129 @@ namespace TShockAPI } - void OnChat(int ply, string msg) - { + /* + * Useful stuff: + * */ + public static void Kick(int ply, string reason) + { + NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f); + Netplay.serverSock[ply].kill = true; + NetMessage.syncPlayers(); + } + + public static bool IsAdmin(string ply) + { + string remoteEndPoint = Convert.ToString((Netplay.serverSock[FindPlayer(ply)].tcpClient.Client.RemoteEndPoint)); + string[] remoteEndPointIP = remoteEndPoint.Split(':'); + TextReader tr = new StreamReader(saveDir + "admins.txt"); + string adminlist = tr.ReadToEnd(); + tr.Close(); + if (adminlist.Contains(remoteEndPointIP[0])) + { + return true; + } + return false; + } + + public static bool IsAdmin(int ply) + { + string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + string[] remoteEndPointIP = remoteEndPoint.Split(':'); + TextReader tr = new StreamReader(saveDir + "admins.txt"); + string adminlist = tr.ReadToEnd(); + tr.Close(); + if (adminlist.Contains(remoteEndPointIP[0])) + { + return true; + } + return false; + } + + public static int FindPlayer(string ply) + { + int pl = -1; + for (int i = 0; i < Main.player.Length; i++) + { + if ((ply.ToLower()) == Main.player[i].name.ToLower()) + { + pl = i; + break; + } + } + return pl; + } + + public static string FindPlayer(int ply) + { + for (int i = 0; i < Main.player.Length; i++) + { + if (i == ply) + { + return Main.player[i].name; + } + } + return "null"; + } + + public static void Broadcast(string msg) + { + for (int i = 0; i < Main.player.Length; i++) + { + SendMessage(i, msg); + } + } + + public static string GetRealIP(string mess) + { + return mess.Split(':')[0]; + } + + public static void SendMessage(int ply, string msg, float[] color) + { + NetMessage.SendData(0x19, ply, -1, msg, 8, color[0], color[1], color[2]); + } + + public static void SendMessage(int ply, string message) + { + NetMessage.SendData(0x19, ply, -1, message, 8, 0f, 255f, 0f); + } + + private static void WriteGrief(int ply) + { + TextWriter tw = new StreamWriter(saveDir + "grief.txt", true); + tw.WriteLine("[" + Main.player[ply].name + "] [" + GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]"); + tw.Close(); + } + + private static void WriteError(string err) + { + if (System.IO.File.Exists(saveDir + "errors.txt")) + { + TextWriter tw = new StreamWriter(saveDir + "errors.txt", true); + tw.WriteLine(err); + tw.Close(); + } + else + { + CreateFile(saveDir + "errors.txt"); + TextWriter tw = new StreamWriter(saveDir + "errors.txt", true); + tw.WriteLine(err); + tw.Close(); + } + } + + public static void WriteBan(int ply) + { + string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + TextWriter tw = new StreamWriter(saveDir + "bans.txt", true); + tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); + tw.Close(); + } + + private static void CreateFile(string file) + { + using (FileStream fs = File.Create(file)) { } } } } \ No newline at end of file