Skip to content

Commit

Permalink
Added chatting from web interface
Browse files Browse the repository at this point in the history
  • Loading branch information
codecat committed Oct 20, 2012
1 parent 55cf5ac commit dd9a860
Show file tree
Hide file tree
Showing 6 changed files with 447 additions and 340 deletions.
57 changes: 53 additions & 4 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,29 @@ public void RTSThread()

try {
string line = reader.ReadLine();

while (reader.ReadLine() != "") { }

if (line != null) {
string request = line.Split(' ')[1].Substring(1);
string[] requestParts = line.Split(' ')[1].Substring(1).Split(new char[] { '?' }, 2);
string request = requestParts[0];
string data = requestParts.Length == 2 ? Uri.UnescapeDataString(requestParts[1]).Replace('+', ' ') : "";
string[] parse = request.Split('/');

string roomID = "";
Room r = null;

string responseBuffer = "";
switch (parse[0]) {
case "l":
if (IP != ServerConfig.Get("RTS_Trusted")) {
responseBuffer = "[]";
break;
}

responseBuffer = "[";
foreach (Room room in Rooms) {
if (!room.Secret && !room.Owner.ShadowBanned) {
if (!room.Secret && (room.Owner != null && !room.Owner.ShadowBanned)) {
responseBuffer += "[";
responseBuffer += "\"" + room.ID + "\",";
responseBuffer += "\"" + JsonSafe(room.Name) + "\",";
Expand All @@ -286,8 +299,8 @@ public void RTSThread()
break;

case "g":
string roomID = parse[1];
Room r = null;
roomID = parse[1];
r = null;
foreach (Room room in Rooms) {
if (room.ID == roomID) {
r = room;
Expand Down Expand Up @@ -327,6 +340,42 @@ public void RTSThread()
}
}
break;

case "c":
if (IP != ServerConfig.Get("RTS_Trusted")) {
responseBuffer = "[]";
break;
}

roomID = parse[1];

Hashtable[] userRes = Sql.Query("SELECT * FROM \"users\" WHERE \"Username\"='" + Sql.AddSlashes(parse[2]) + "'");
if (userRes.Length != 1) {
break;
}

Hashtable u = userRes[0];

r = null;
foreach (Room room in Rooms) {
if (room.ID == roomID) {
r = room;
break;
}
}

if (r != null && !r.Secret) {
string strName = u["Username"].ToString();

for (int i = 0; i < Scripting.WebFormatHooks.Count; i++) {
strName = Scripting.WebFormatHooks[i](u, strName);
}

SendChatAll(strName + ": " + data, r);
}

responseBuffer = "OK";
break;
}

writer.WriteLine("HTTP/1.1 200 OK");
Expand Down
10 changes: 10 additions & 0 deletions Scripting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Collections;

namespace OpenSMO
{
Expand All @@ -24,13 +25,15 @@ public class Scripting
public delegate bool ChatHookCall(User user, string message);
public delegate bool ChatCommandHookCall(User user, string args);
public delegate string NameFormatHookCall(User user, string current);
public delegate string WebFormatHookCall(Hashtable user, string current);
public delegate int PlayerXPHookCall(User user, int current);

public Dictionary<NSCommand, List<PacketHookCall>> PacketHooks;
public List<UpdateHookCall> UpdateHooks;
public List<ChatHookCall> ChatHooks;
public Dictionary<string, List<ChatCommandHookCall>> ChatCommandHooks;
public List<NameFormatHookCall> NameFormatHooks;
public List<WebFormatHookCall> WebFormatHooks;
public List<PlayerXPHookCall> PlayerXPHooks;

public Scripting()
Expand All @@ -44,6 +47,7 @@ public Scripting()
ChatHooks = new List<ChatHookCall>();
ChatCommandHooks = new Dictionary<string, List<ChatCommandHookCall>>();
NameFormatHooks = new List<NameFormatHookCall>();
WebFormatHooks = new List<WebFormatHookCall>();
PlayerXPHooks = new List<PlayerXPHookCall>();
}

Expand Down Expand Up @@ -83,6 +87,12 @@ public void HookNameFormat(NameFormatHookCall call, bool priority = false)
else NameFormatHooks.Add(call);
}

public void HookWebFormat(WebFormatHookCall call, bool priority = false)
{
if (priority) WebFormatHooks.Insert(0, call);
else WebFormatHooks.Add(call);
}

public void HookPlayerXP(PlayerXPHookCall call, bool priority = false)
{
if (priority) PlayerXPHooks.Insert(0, call);
Expand Down
Loading

0 comments on commit dd9a860

Please sign in to comment.