diff --git a/Core/Commands/Group/getGroupList.cs b/Core/Commands/Group/getGroupList.cs index 91b9c47..3c8653c 100644 --- a/Core/Commands/Group/getGroupList.cs +++ b/Core/Commands/Group/getGroupList.cs @@ -7,20 +7,14 @@ namespace BSB.Commands.Group { public class GetGroupList : CoreGroupCommand_SmartReply_auto { - public override string Helpfile { get { return "Returns a csv of known groups NAME=UUID,NAME=UUID... "; } } + public override string Helpfile { get { return "Gets a list of groups Json encoded"; } } protected override string RunFunction() { - StringBuilder reply = new StringBuilder(); - string addon = ""; foreach (KeyValuePair groupdata in bot.MyGroups) { - reply.Append(addon); - addon = ","; - reply.Append(groupdata.Value.Name); - reply.Append("="); - reply.Append(groupdata.Key); + collection.Add(groupdata.Value.Name, groupdata.Key.ToString()); } - return reply.ToString(); + return "grouplist=loaded"; } } } diff --git a/Core/Commands/Group/getGroupRoles.cs b/Core/Commands/Group/getGroupRoles.cs index 11afdfb..3c702a7 100644 --- a/Core/Commands/Group/getGroupRoles.cs +++ b/Core/Commands/Group/getGroupRoles.cs @@ -7,7 +7,7 @@ namespace BSB.Commands.Group { public class GetGroupRoles : CoreGroupCommand_SmartReply_Group { - public override string Helpfile { get { return "Returns group=UUID@@@rolename=UUID,rolename=UUID ... "; } } + public override string Helpfile { get { return "Gets a list of group roles Json encoded "; } } public override bool CallFunction(string[] args) { if (base.CallFunction(args) == true) @@ -36,23 +36,14 @@ namespace BSB.Commands.Group public override void Callback(string[] args, EventArgs e) { GroupRolesDataReplyEventArgs rolesData = (GroupRolesDataReplyEventArgs)e; - StringBuilder reply = new StringBuilder(); - reply.Append("group="); - reply.Append(args[1]); - reply.Append("@@@"); - string addon = ""; foreach(KeyValuePair data in rolesData.Roles) { if (data.Key != UUID.Zero) { - reply.Append(addon); - addon = ","; - reply.Append(data.Value.Name); - reply.Append("="); - reply.Append(data.Key.ToString()); + collection.Add(data.Value.Name, data.Key.ToString()); } } - bot.GetCommandsInterface.SmartCommandReply(args[0], reply.ToString(), CommandName); + bot.GetCommandsInterface.SmartCommandReply(true,args[0], "group=" + args[1], CommandName,collection); base.Callback(args, e); } } diff --git a/Core/Commands/Group/group.cs b/Core/Commands/Group/group.cs index c6f3232..5ea61cd 100644 --- a/Core/Commands/Group/group.cs +++ b/Core/Commands/Group/group.cs @@ -1,4 +1,5 @@ using OpenMetaverse; +using System.Collections.Generic; namespace BSB.Commands.Group { @@ -12,14 +13,15 @@ namespace BSB.Commands.Group { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], RunFunction(), CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true, args[0], RunFunction(), CommandName, collection); } - return false; + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "", CommandName); } } public abstract class CoreGroupCommand_SmartReply_Group : CoreCommand_2arg { + protected Dictionary collection = new Dictionary(); public override string[] ArgTypes { get { return new[] { "Mixed", "UUID" }; } } public override string[] ArgHints { get { return new[] { "Smart reply [Channel|IM uuid|http url]", "Group" }; } } } @@ -38,7 +40,7 @@ namespace BSB.Commands.Group { if (bot.MyGroups.ContainsKey(targetgroup) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], RunFunction(targetgroup), CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], RunFunction(targetgroup), CommandName); } else { diff --git a/Core/Commands/Group/groupchatAnyUnread.cs b/Core/Commands/Group/groupchatAnyUnread.cs index d7971f5..a4fb6b6 100644 --- a/Core/Commands/Group/groupchatAnyUnread.cs +++ b/Core/Commands/Group/groupchatAnyUnread.cs @@ -5,7 +5,7 @@ public override string Helpfile { get { return "Returns True|False via the smart reply target if there are any unread group ims"; } } protected override string RunFunction() { - return bot.HasUnreadGroupchats().ToString(); + return "groupchatunread="+bot.HasUnreadGroupchats().ToString(); } } } \ No newline at end of file diff --git a/Core/Commands/Group/groupchatGroupHasUnread.cs b/Core/Commands/Group/groupchatGroupHasUnread.cs index 2a3a2e8..db7551a 100644 --- a/Core/Commands/Group/groupchatGroupHasUnread.cs +++ b/Core/Commands/Group/groupchatGroupHasUnread.cs @@ -8,7 +8,7 @@ namespace BSB.Commands.Group protected override string RunFunction(UUID targetgroup) { - return bot.GroupHasUnread(targetgroup).ToString(); + return ""+targetgroup.ToString()+"=" + bot.GroupHasUnread(targetgroup).ToString(); } } diff --git a/Core/Commands/Group/groupchatHistory.cs b/Core/Commands/Group/groupchatHistory.cs index 4f79fdb..557a7c8 100644 --- a/Core/Commands/Group/groupchatHistory.cs +++ b/Core/Commands/Group/groupchatHistory.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using Microsoft.VisualBasic.CompilerServices; using OpenMetaverse; namespace BSB.Commands.Group @@ -8,7 +10,13 @@ namespace BSB.Commands.Group public override string Helpfile { get { return "Returns the chat history for the selected group [ARG 2] via smart reply [ARG 1]"; } } protected override string RunFunction(UUID targetgroup) { - return String.Join("###", bot.GetGroupchat(targetgroup)); + int entry = 1; + foreach(string A in bot.GetGroupchat(targetgroup)) + { + collection.Add(entry.ToString(),A); + entry++; + } + return "group="+targetgroup.ToString()+""; } } } diff --git a/Core/Commands/Group/groupchatListAllUnreadGroups.cs b/Core/Commands/Group/groupchatListAllUnreadGroups.cs index 2ab9b81..e62dbed 100644 --- a/Core/Commands/Group/groupchatListAllUnreadGroups.cs +++ b/Core/Commands/Group/groupchatListAllUnreadGroups.cs @@ -5,19 +5,17 @@ namespace BSB.Commands.Group { public class GroupchatListAllUnreadGroups : CoreGroupCommand_SmartReply_auto { - public override string Helpfile { get { return "Returns a csv of groups with unread messages: UUID,UUID... "; } } + public override string Helpfile { get { return "Returns what groups have unread messages "; } } protected override string RunFunction() { - StringBuilder reply = new StringBuilder(); - string addon = ""; + int entry = 1; foreach (UUID group in bot.UnreadGroupchatGroups()) { - reply.Append(addon); - addon = ","; - reply.Append(group); + collection.Add(entry.ToString(),group.ToString()); + entry++; } - return reply.ToString(); + return "count="+collection.Count.ToString(); } } } diff --git a/Core/Commands/Info/Balance.cs b/Core/Commands/Info/Balance.cs index 50cb864..aaf8716 100644 --- a/Core/Commands/Info/Balance.cs +++ b/Core/Commands/Info/Balance.cs @@ -9,11 +9,11 @@ { if (bot.GetAllowFunds == true) { - bool status = bot.GetCommandsInterface.SmartCommandReply(args[0], bot.GetClient.Self.Balance.ToString(), CommandName); + bool status = bot.GetCommandsInterface.SmartCommandReply(true,args[0], bot.GetClient.Self.Balance.ToString(), CommandName); bot.GetClient.Self.RequestBalance(); return status; } - return bot.GetCommandsInterface.SmartCommandReply(args[0], "0", CommandName); + return bot.GetCommandsInterface.SmartCommandReply(false,args[0], "0", CommandName); } return false; } diff --git a/Core/Commands/Info/GetPosition.cs b/Core/Commands/Info/GetPosition.cs index 020bb05..bc45f7c 100644 --- a/Core/Commands/Info/GetPosition.cs +++ b/Core/Commands/Info/GetPosition.cs @@ -7,7 +7,7 @@ { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], bot.GetClient.Self.RelativePosition.ToString(), CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], bot.GetClient.Self.RelativePosition.ToString(), CommandName); } return false; } diff --git a/Core/Commands/Info/ParcelName.cs b/Core/Commands/Info/ParcelName.cs index 23e8f51..74af8c5 100644 --- a/Core/Commands/Info/ParcelName.cs +++ b/Core/Commands/Info/ParcelName.cs @@ -8,7 +8,7 @@ { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], targetparcel.Name, CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], targetparcel.Name, CommandName); } return false; } diff --git a/Core/Commands/Info/SimName.cs b/Core/Commands/Info/SimName.cs index 01dcfcb..f7b461a 100644 --- a/Core/Commands/Info/SimName.cs +++ b/Core/Commands/Info/SimName.cs @@ -7,7 +7,7 @@ { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], bot.GetClient.Network.CurrentSim.Name, CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], bot.GetClient.Network.CurrentSim.Name, CommandName); } return false; } diff --git a/Core/Commands/Info/UnixTimeNow.cs b/Core/Commands/Info/UnixTimeNow.cs index 799ac16..56e0645 100644 --- a/Core/Commands/Info/UnixTimeNow.cs +++ b/Core/Commands/Info/UnixTimeNow.cs @@ -9,7 +9,7 @@ namespace BSB.Commands.Info { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], helpers.UnixTimeNow().ToString(), CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], helpers.UnixTimeNow().ToString(), CommandName); } return false; } diff --git a/Core/Commands/Inventory/inventoryContents.cs b/Core/Commands/Inventory/inventoryContents.cs index 280d8d9..3134449 100644 --- a/Core/Commands/Inventory/inventoryContents.cs +++ b/Core/Commands/Inventory/inventoryContents.cs @@ -37,7 +37,7 @@ namespace BSB.Commands.Inventory } else { - return bot.GetCommandsInterface.SmartCommandReply(args[0], String.Join("~|~", content), CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true, args[0], String.Join("~|~", content), CommandName); } } else diff --git a/Core/Commands/Inventory/inventoryFolders.cs b/Core/Commands/Inventory/inventoryFolders.cs index 7acb665..2ed81f1 100644 --- a/Core/Commands/Inventory/inventoryFolders.cs +++ b/Core/Commands/Inventory/inventoryFolders.cs @@ -27,7 +27,7 @@ namespace BSB.Commands.Inventory else { string reply = HelperInventory.MapFolder(bot, true); - return bot.GetCommandsInterface.SmartCommandReply(args[0], reply, CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], reply, CommandName); } } return false; diff --git a/Core/Commands/Parcel/GetParcelDesc.cs b/Core/Commands/Parcel/GetParcelDesc.cs index d2c7a4b..d957cbc 100644 --- a/Core/Commands/Parcel/GetParcelDesc.cs +++ b/Core/Commands/Parcel/GetParcelDesc.cs @@ -8,7 +8,7 @@ { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], targetparcel.Desc, CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true, args[0], targetparcel.Desc, CommandName); } return false; } diff --git a/Core/Commands/Parcel/GetParcelFlags.cs b/Core/Commands/Parcel/GetParcelFlags.cs index eb9d536..9162a93 100644 --- a/Core/Commands/Parcel/GetParcelFlags.cs +++ b/Core/Commands/Parcel/GetParcelFlags.cs @@ -56,20 +56,15 @@ namespace BSB.Commands.CMD_Parcel if (bot.GetClient.Network.CurrentSim.Parcels.ContainsKey(localid) == true) { Parcel p = bot.GetClient.Network.CurrentSim.Parcels[localid]; - StringBuilder reply = new StringBuilder(); - string addon = ""; + Dictionary collection = new Dictionary(); foreach (string cfg in get_flags) { if (flags.ContainsKey(cfg) == true) { - reply.Append(addon); - reply.Append(cfg); - reply.Append("="); - reply.Append(p.Flags.HasFlag(flags[cfg]).ToString()); - addon = ","; + collection.Add(cfg, p.Flags.HasFlag(flags[cfg]).ToString()); } } - return bot.GetCommandsInterface.SmartCommandReply(target, reply.ToString(), CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,target, p.Name, CommandName, collection); } else { diff --git a/Core/Commands/Parcel/ParcelPrimCountAvatar.cs b/Core/Commands/Parcel/ParcelPrimCountAvatar.cs index b2e711c..e9d6717 100644 --- a/Core/Commands/Parcel/ParcelPrimCountAvatar.cs +++ b/Core/Commands/Parcel/ParcelPrimCountAvatar.cs @@ -5,7 +5,7 @@ using System.Text; namespace BSB.Commands.CMD_Parcel { - class ParcelPrimCountAvatar : CoreCommand_2arg + class ParcelPrimCountAvatar : CoreCommand_SmartReply_2arg { public override string[] ArgTypes { get { return new[] { "Smart","Avatar" }; } } public override string[] ArgHints { get { return new[] { "Smart reply target: Channel,Avatar,HTTP url","the avatar (UUID or Firstname Lastname) we want the count of back" }; } } @@ -25,25 +25,18 @@ namespace BSB.Commands.CMD_Parcel { if (UUID.TryParse(args[1], out UUID avatar) == true) { - StringBuilder reply = new StringBuilder(); int localid = bot.GetClient.Parcels.GetParcelLocalID(bot.GetClient.Network.CurrentSim, bot.GetClient.Self.SimPosition); - reply.Append("DATA="); - reply.Append(bot.GetClient.Network.CurrentSim.Name); - reply.Append(","); - reply.Append(localid.ToString()); - reply.Append(","); - reply.Append(avatar.ToString()); - reply.Append(","); + collection.Add("sim", bot.GetClient.Network.CurrentSim.Name); if (owner_uuids.Contains(avatar) == true) { int owner_count = owner_counts[owner_uuids.IndexOf(avatar)]; - reply.Append(owner_count.ToString()); + collection.Add("count", owner_count.ToString()); } else { - reply.Append("0"); + collection.Add("count", "0"); } - base.Callback(args, e, bot.GetCommandsInterface.SmartCommandReply(args[0], reply.ToString(), CommandName)); + base.Callback(args, e, bot.GetCommandsInterface.SmartCommandReply(true,args[0], "avatar="+ avatar.ToString(), CommandName,collection)); } else { diff --git a/Core/Commands/Parcel/ParcelPrimCountTotal.cs b/Core/Commands/Parcel/ParcelPrimCountTotal.cs index ca7d2d6..d89db74 100644 --- a/Core/Commands/Parcel/ParcelPrimCountTotal.cs +++ b/Core/Commands/Parcel/ParcelPrimCountTotal.cs @@ -4,10 +4,8 @@ using System.Text; namespace BSB.Commands.CMD_Parcel { - class ParcelPrimCountTotal : CoreCommand_1arg + class ParcelPrimCountTotal : CoreCommand_SmartReply_1arg { - public override string[] ArgTypes { get { return new[] { "Smart" }; } } - public override string[] ArgHints { get { return new[] { "Smart reply target: Channel,Avatar,HTTP url" }; } } public override string Helpfile { get { return "Returns the total number of prims on the current parcel as follows: DATA=SIMNAME,PARCELID,COUNT"; } } public override void Callback(string[] args, EventArgs e) @@ -18,15 +16,11 @@ namespace BSB.Commands.CMD_Parcel { total_count += owner.Count; } - StringBuilder reply = new StringBuilder(); int localid = bot.GetClient.Parcels.GetParcelLocalID(bot.GetClient.Network.CurrentSim, bot.GetClient.Self.SimPosition); - reply.Append("DATA="); - reply.Append(bot.GetClient.Network.CurrentSim.Name); - reply.Append(","); - reply.Append(localid.ToString()); - reply.Append(","); - reply.Append(total_count.ToString()); - base.Callback(args, e, bot.GetCommandsInterface.SmartCommandReply(args[0], reply.ToString(), CommandName)); + collection.Add("sim", bot.GetClient.Network.CurrentSim.Name); + collection.Add("parcelid", localid.ToString()); + collection.Add("count", total_count.ToString()); + base.Callback(args, e, bot.GetCommandsInterface.SmartCommandReply(true,args[0], "ok", CommandName,collection)); } public override bool CallFunction(string[] args) diff --git a/Core/Commands/Self/GetLastCommands.cs b/Core/Commands/Self/GetLastCommands.cs index 714930b..040b88a 100644 --- a/Core/Commands/Self/GetLastCommands.cs +++ b/Core/Commands/Self/GetLastCommands.cs @@ -10,7 +10,13 @@ namespace BSB.Commands.Self { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], String.Join(',',bot.GetLastCommands(5)), CommandName); + int entry = 1; + foreach(string a in bot.GetLastCommands(5)) + { + collection.Add(entry.ToString(), a); + entry++; + } + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], "last=5", CommandName,collection); } return false; } diff --git a/Core/Commands/Self/botVersion.cs b/Core/Commands/Self/botVersion.cs index e1525c2..d468fa8 100644 --- a/Core/Commands/Self/botVersion.cs +++ b/Core/Commands/Self/botVersion.cs @@ -7,7 +7,7 @@ { if (base.CallFunction(args) == true) { - return bot.GetCommandsInterface.SmartCommandReply(args[0], bot.MyVersion, CommandName); + return bot.GetCommandsInterface.SmartCommandReply(true,args[0], bot.MyVersion, CommandName); } return false; } diff --git a/Core/Commands/commandlib.cs b/Core/Commands/commandlib.cs index 04dd80f..3d5464c 100644 --- a/Core/Commands/commandlib.cs +++ b/Core/Commands/commandlib.cs @@ -8,6 +8,7 @@ using BSB.bottypes; using BetterSecondBotShared.API; using BetterSecondBotShared.Static; using BetterSecondBotShared.logs; +using Newtonsoft.Json; namespace BSB.Commands { @@ -132,11 +133,13 @@ namespace BSB.Commands } protected HttpClient HTTPclient = new HttpClient(); - public bool SmartCommandReply(string target, string output) + + public bool SmartCommandReply(bool run_status, string target, string output, string command) { - return SmartCommandReply(target, output, ""); + return SmartCommandReply(run_status, target, output, command, null); } - public bool SmartCommandReply(string target, string output, string command) + + public bool SmartCommandReply(bool run_status, string target, string output, string command, Dictionary collection) { string mode = "CHAT"; UUID target_avatar = UUID.Zero; @@ -151,12 +154,41 @@ namespace BSB.Commands } else { - int.TryParse(target, out target_channel); + if (int.TryParse(target, out target_channel) == false) + { + run_status = false; + } + } + + if (collection != null) + { + var reply_object = new CoreCommand_json_reply_wit_collection(); + reply_object.status = run_status; + reply_object.command = command; + reply_object.output = output; + reply_object.collection = collection; + output = JsonConvert.SerializeObject(reply_object); + } + else + { + var reply_object = new CoreCommand_json_reply(); + reply_object.status = run_status; + reply_object.command = command; + reply_object.output = output; + output = JsonConvert.SerializeObject(reply_object); } if (mode == "CHAT") { - bot.GetClient.Self.Chat(output, target_channel, ChatType.Normal); + if (target_channel >= 0) + { + bot.GetClient.Self.Chat(output, target_channel, ChatType.Normal); + } + else + { + run_status = false; + ConsoleLog.Crit("[SmartReply] output Channel must be zero or higher"); + } } else if (mode == "IM") { @@ -167,13 +199,8 @@ namespace BSB.Commands Dictionary values = new Dictionary { { "reply", output }, - { "unixtime", helpers.UnixTimeNow().ToString() } }; - if (command != "") - { - values.Add("command", command); - } var content = new FormUrlEncodedContent(values); try { @@ -181,14 +208,16 @@ namespace BSB.Commands } catch (Exception e) { + run_status = false; ConsoleLog.Crit("[SmartReply] HTTP failed: " + e.Message + ""); } } else { + ConsoleLog.Crit("[SmartReply] Unsupported output target"); return false; } - return true; + return run_status; } protected List suppress_warnings = new List(); @@ -266,6 +295,17 @@ namespace BSB.Commands return new KeyValuePair(false, "--"); } } + class CoreCommand_json_reply_wit_collection : CoreCommand_json_reply + { + public Dictionary collection = new Dictionary(); + } + class CoreCommand_json_reply + { + public bool status; + public string command; + public string output; + } + public abstract class CoreCommand_1arg : CoreCommand { @@ -283,11 +323,22 @@ namespace BSB.Commands { public override int Min_Required_args { get { return 4; } } } + public abstract class CoreCommand_SmartReply_1arg : CoreCommand_1arg { + protected Dictionary collection = new Dictionary(); public override string[] ArgTypes { get { return new[] { "Smart" }; } } public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]" }; } } } + public abstract class CoreCommand_SmartReply_2arg : CoreCommand_SmartReply_1arg + { + public override int Min_Required_args { get { return 2; } } + } + public abstract class CoreCommand_SmartReply_3arg : CoreCommand_SmartReply_1arg + { + public override int Min_Required_args { get { return 3; } } + } + public abstract class CoreCommand : API_interface { diff --git a/Core/bottypes/DialogsBot.cs b/Core/bottypes/DialogsBot.cs index 4412326..bdb68d2 100644 --- a/Core/bottypes/DialogsBot.cs +++ b/Core/bottypes/DialogsBot.cs @@ -97,17 +97,17 @@ namespace BSB.bottypes string DialogMessage = "NewDialog###" + dialogID.ToString() + "###"+ Dmess + "###" + String.Join("~|~",e.ButtonLabels) + ""; if (RelayDialogsToChannel != 0) { - CommandsInterface.SmartCommandReply(RelayDialogsToChannel.ToString(), DialogMessage,"NewDialogNotice"); + CommandsInterface.SmartCommandReply(true,RelayDialogsToChannel.ToString(), DialogMessage,"NewDialogNotice"); } if(RelayDialogsToAvatar != UUID.Zero) { - CommandsInterface.SmartCommandReply(RelayDialogsToAvatar.ToString(), DialogMessage, "NewDialogNotice"); + CommandsInterface.SmartCommandReply(true, RelayDialogsToAvatar.ToString(), DialogMessage, "NewDialogNotice"); } if(RelayDialogsToHttp != null) { if (RelayDialogsToHttp.StartsWith("http") == true) { - CommandsInterface.SmartCommandReply(RelayDialogsToAvatar.ToString(), DialogMessage, "NewDialogNotice"); + CommandsInterface.SmartCommandReply(true, RelayDialogsToAvatar.ToString(), DialogMessage, "NewDialogNotice"); } } }