199 lines
9.2 KiB
C#
199 lines
9.2 KiB
C#
using Newtonsoft.Json;
|
|
using OpenMetaverse;
|
|
using OpenMetaverse.Assets;
|
|
using SecondBotEvents.Services;
|
|
|
|
|
|
namespace SecondBotEvents.Commands
|
|
{
|
|
[ClassInfo("Why this is not a built in script command I will never know, but its due to needing this that the bot was made")]
|
|
public class Notecard : CommandsAPI
|
|
{
|
|
public Notecard(EventsSecondBot setmaster) : base(setmaster)
|
|
{
|
|
}
|
|
|
|
[About("Reads the notecards content, if it is able to read the notecard will give a json reply of notecarduuid name and contents")]
|
|
[ReturnHintsFailure("Not a vaild UUID")]
|
|
[ReturnHintsFailure("Unable to find inventory item")]
|
|
[ReturnHintsFailure("Inventory item is not a notecard")]
|
|
[ReturnHintsFailure("replytarget not set")]
|
|
[ReturnHints("Making request to get data from notecard")]
|
|
[ArgHints("notecardInventoryUUID", "The inventory UUID of the notecard to read")]
|
|
[ArgHints("replytarget", "where to send the results to (smartreply target Avatar UUID, HTTP url, chat channel")]
|
|
public object NotecardRead(string notecardInventoryUUID, string replytarget)
|
|
{
|
|
if(SecondbotHelpers.isempty(replytarget) == true)
|
|
{
|
|
return Failure("replytarget not set");
|
|
}
|
|
NotecardReadReply reply = new NotecardReadReply();
|
|
reply.uuid = notecardInventoryUUID;
|
|
reply.name = "?";
|
|
reply.status = false;
|
|
reply.content = "Not a vaild UUID";
|
|
if (UUID.TryParse(notecardInventoryUUID, out var uuid) == false)
|
|
{
|
|
master.CommandsService.SmartCommandReply(replytarget, JsonConvert.SerializeObject(reply), "NotecardRead");
|
|
return Failure(reply.content, new[] { notecardInventoryUUID });
|
|
}
|
|
reply.content = "Unable to find inventory item";
|
|
InventoryItem item = GetClient().Inventory.FetchItem(uuid, GetClient().Self.AgentID, new System.TimeSpan(0, 1, 30));
|
|
if (item == null)
|
|
{
|
|
master.CommandsService.SmartCommandReply(replytarget, JsonConvert.SerializeObject(reply), "NotecardRead");
|
|
return Failure(reply.content, new[] { notecardInventoryUUID });
|
|
}
|
|
reply.content = "Inventory item is not a notecard";
|
|
if (item.InventoryType != InventoryType.Notecard)
|
|
{
|
|
master.CommandsService.SmartCommandReply(replytarget, JsonConvert.SerializeObject(reply), "NotecardRead");
|
|
return Failure(reply.content, new[] { notecardInventoryUUID }); ;
|
|
}
|
|
InventoryNotecard notecard = (InventoryNotecard)item;
|
|
GetClient().Assets.RequestInventoryAsset(notecard, true, UUID.Random(), (AssetDownload transfer, Asset asset) =>
|
|
{
|
|
if (transfer.Success == false)
|
|
{
|
|
reply.content = "!ERROR! - unable to read notecard";
|
|
master.CommandsService.SmartCommandReply(replytarget, JsonConvert.SerializeObject(reply), "NotecardRead");
|
|
}
|
|
else
|
|
{
|
|
AssetNotecard note = (AssetNotecard)asset;
|
|
note.Decode();
|
|
string contents = "";
|
|
for (int index = 0; index < note.BodyText.Length; index++)
|
|
{
|
|
char c = note.BodyText[index];
|
|
if ((int)c == 0xdbc0)
|
|
{
|
|
contents = "[ATTACHMENT]";
|
|
}
|
|
else
|
|
{
|
|
contents += c;
|
|
}
|
|
}
|
|
reply.content = contents;
|
|
reply.name = notecard.Name;
|
|
reply.status = true;
|
|
master.CommandsService.SmartCommandReply(replytarget, JsonConvert.SerializeObject(reply), "NotecardRead");
|
|
}
|
|
}
|
|
);
|
|
return BasicReply("Making request to get data from notecard");
|
|
}
|
|
|
|
[About("Adds content to the Collection<br/> Also creates the Collection if it does not exist")]
|
|
[ReturnHintsFailure("Collection value is empty")]
|
|
[ReturnHintsFailure("Content value is empty")]
|
|
[ReturnHints("ok")]
|
|
[ArgHints("collection", "The name of the collection")]
|
|
[ArgHints("content", "The text to add to the collection")]
|
|
public object NotecardAdd(string collection, string content)
|
|
{
|
|
if (SecondbotHelpers.notempty(collection) == false)
|
|
{
|
|
return Failure("Collection value is empty", new [] { collection, content });
|
|
}
|
|
if (SecondbotHelpers.notempty(content) == false)
|
|
{
|
|
return Failure("Content value is empty", new [] { collection, content });
|
|
}
|
|
master.DataStoreService.AppendKeyValue("notecard_"+collection, content);
|
|
return BasicReply("ok", new [] { collection, master.DataStoreService.GetKeyValue("notecard_"+collection).Length.ToString() });
|
|
}
|
|
|
|
[About("Clears the contents of a collection")]
|
|
[ReturnHintsFailure("Collection value is empty")]
|
|
[ReturnHints("ok")]
|
|
[ArgHints("collection", "The name of the collection")]
|
|
public object NotecardClear(string collection)
|
|
{
|
|
if (SecondbotHelpers.notempty(collection) == false)
|
|
{
|
|
return Failure("Collection value is empty", new [] { collection });
|
|
}
|
|
master.DataStoreService.ClearKeyValue("notecard_" + collection);
|
|
return BasicReply("ok");
|
|
}
|
|
|
|
[About("Sends a notecard to a avatar using the text in the prebuilt collection [see NotecardAdd] and also clears the collection just before sending [see NotecardClear]")]
|
|
[ReturnHintsFailure("Collection value is empty")]
|
|
[ReturnHintsFailure("Notecardname value is empty")]
|
|
[ReturnHintsFailure("Invaild avatar uuid")]
|
|
[ReturnHintsFailure("No content in notecard storage ?")]
|
|
[ReturnHints("ok")]
|
|
[ArgHints("avatar", "The UUID or Name of an avatar")]
|
|
[ArgHints("collection", "The name of the collection")]
|
|
[ArgHints("notecardname", "What to call the created notecard")]
|
|
public object NotecardSend(string avatar, string collection, string notecardname)
|
|
{
|
|
if (SecondbotHelpers.notempty(collection) == false)
|
|
{
|
|
return Failure("Collection value is empty", new [] { avatar, collection, notecardname });
|
|
}
|
|
if (SecondbotHelpers.notempty(notecardname) == false)
|
|
{
|
|
return Failure("Notecardname value is empty", new [] { avatar, collection, notecardname });
|
|
}
|
|
ProcessAvatar(avatar);
|
|
if(avataruuid == UUID.Zero)
|
|
{
|
|
return Failure("Invaild avatar uuid", new [] { avatar, collection, notecardname });
|
|
}
|
|
string content = master.DataStoreService.GetKeyValue("notecard_" + collection);
|
|
if(content.Length <= 3)
|
|
{
|
|
return Failure("No content in notecard storage ?", new[] { avatar, collection, notecardname });
|
|
}
|
|
bool result = master.BotClient.SendNotecard(notecardname, content, avataruuid);
|
|
if (result == false)
|
|
{
|
|
return Failure("Failed to create/send notecard", new[] { avatar, content, notecardname });
|
|
}
|
|
master.DataStoreService.ClearKeyValue("notecard_" + collection);
|
|
return BasicReply("ok");
|
|
}
|
|
|
|
[About("Creates and sends a notecard in one command good if you are using HTTP otherwise see [NotecardSend]")]
|
|
[ReturnHintsFailure("notecardname value is empty")]
|
|
[ReturnHintsFailure("Content value is empty")]
|
|
[ReturnHintsFailure("Invaild avatar uuid")]
|
|
[ReturnHints("ok")]
|
|
[ArgHints("avatar", "The UUID or Name of an avatar")]
|
|
[ArgHints("content", "The text to add to the collection")]
|
|
[ArgHints("notecardname", "What to call the created notecard")]
|
|
public object NotecardDirectSend(string avatar, string content, string notecardname)
|
|
{
|
|
if (SecondbotHelpers.notempty(notecardname) == false)
|
|
{
|
|
return Failure("notecardname value is empty", new[] { avatar, content, notecardname });
|
|
}
|
|
if (SecondbotHelpers.notempty(content) == false)
|
|
{
|
|
return Failure("content value is empty", new[] { avatar, content, notecardname });
|
|
}
|
|
ProcessAvatar(avatar);
|
|
if (avataruuid == UUID.Zero)
|
|
{
|
|
return Failure("Invaild avatar uuid", new[] { avatar, content, notecardname });
|
|
}
|
|
bool result = master.BotClient.SendNotecard(notecardname, content, avataruuid);
|
|
if(result == false)
|
|
{
|
|
return Failure("Failed to create/send notecard", new[] { avatar, content, notecardname });
|
|
}
|
|
return BasicReply("ok");
|
|
}
|
|
}
|
|
|
|
public class NotecardReadReply
|
|
{
|
|
public string uuid;
|
|
public string name;
|
|
public string content;
|
|
public bool status;
|
|
}
|
|
}
|