Finish revising messaging API

This commit is contained in:
Zontreck 2020-06-28 19:45:04 -07:00
parent 59c62e62e2
commit 4b07283a3d
8 changed files with 137 additions and 143 deletions

View file

@ -6,7 +6,7 @@ using System.Reflection;
[assembly: AssemblyCompany("ZNI")]
[assembly: AssemblyAlgorithmId(System.Configuration.Assemblies.AssemblyHashAlgorithm.MD5)]
[assembly: AssemblyCopyright("© 2020 Tara Piccari")]
[assembly: AssemblyFileVersion("5.3.091")]
[assembly: AssemblyFileVersion("5.3.912")]
[assembly: AssemblyDescription("Second Life Bot - BotCore5")]
@ -15,7 +15,7 @@ namespace Bot.Assemble
public class ASMInfo
{
public static string BotName = "ZBotCore";
public static string BotVer = "5.3.091";
public static string BotVer = "5.3.912";
public static string GitPassword
{
get

View file

@ -46,5 +46,7 @@ namespace Bot
public DateTime LaunchTime { get; set; } = DateTime.Now;
public bool WaitForFiveMinutes = false;
public MessageService MSGSVC { get; set; } = new MessageService();
public bool EnqueueExit = false;
public bool EnqueueGroupRefresh = false;
}
}

View file

@ -94,8 +94,8 @@ namespace Bot.CommandSystem
[CommandGroup("show_level", 0, 0, "This command shows your current auth level if any.", Destinations.DEST_AGENT | Destinations.DEST_LOCAL | Destinations.DEST_GROUP)]
public void show_level(UUID client, int level, GridClient grid, string[] additionalArgs,
[CommandGroup("show_level", 0, 0, "This command shows your current auth level if any.", Destinations.DEST_AGENT | Destinations.DEST_DISCORD | Destinations.DEST_LOCAL | Destinations.DEST_GROUP)]
public void show_level(UUID client, int level, string[] additionalArgs,
Destinations source,
UUID agentKey, string agentName)
{
@ -128,7 +128,7 @@ namespace Bot.CommandSystem
public void PerformExit(UUID client, int level, string[] additionalArgs, Destinations source, UUID agentKey, string agentName)
{
MHE(source, client, "Bot exit initiated.");
BotSession.Instance.LaunchTime = new DateTime(); // zero out date time to force a relog
BotSession.Instance.EnqueueExit = true;
}
// !!help
[CommandGroup("!help", 1, 0, "Prints the entire help registry", Destinations.DEST_AGENT |Destinations.DEST_LOCAL | Destinations.DEST_GROUP)]

View file

@ -239,8 +239,23 @@ namespace Bot.CommandSystem
//(UUID client, int level, string[] additionalArgs,
//Destinations source,
//UUID agentKey, string agentName)
Thread CommandThread = new Thread(() => cgX.AssignedMethod.Invoke(ovj, new object[] { user, level, additionalArgs, source, agentKey, agentName }));
CommandThread.Start();
try
{
Thread CMDThread = new Thread(() =>
{
try
{
cgX.AssignedMethod.Invoke(ovj, new object[] { user,level,additionalArgs, source,agentKey, agentName });
}catch(Exception e)
{
MessageFactory.Post(Destinations.DEST_LOCAL, "Exception caught when executing a command\n" + e.Message + "\nStacktrace: " + e.StackTrace, UUID.Zero);
}
});
CMDThread.Start();
}catch(Exception e)
{
MessageFactory.Post(Destinations.DEST_LOCAL, "EXCEPTION CAUGHT WHEN EXECUTING COMMAND\n\n" + e.Message + "\nSTACK\n" + e.StackTrace, UUID.Zero);
}
}
}
}

View file

@ -11,7 +11,7 @@ namespace Bot
{
void run(); // Define the run command since a thread needs a entry point
string getTick(); // Run every second to check for queued data. If queue exists, then it will be returned as a JSON string.
void getTick(); // Run every second to check for queued data. If queue exists, then it will be returned as a JSON string.
// getTick can reply with data for the serializer for instance.
void passArguments(string data); // json!!

View file

@ -7,7 +7,7 @@ using OpenMetaverse;
using Newtonsoft.Json;
using System.IO;
using System.Threading;
using Microsoft.VisualBasic;
namespace Bot
{
@ -114,11 +114,72 @@ namespace Bot
}
}
public class DiscordMessage : Message
{
private string Msg;
public string ServerName;
public string ChannelName;
private UUID SenderID;
public override int GetChannel()
{
return 0;
}
public override string GetMessage()
{
return Msg;
}
public override Destinations GetMessageSource()
{
return Destinations.DEST_DISCORD;
}
public override UUID GetSender()
{
// This is always UUID.Zero with the bot's discord plugin
return SenderID;
}
public override string GetSenderName()
{
return "";
}
public override UUID GetTarget()
{
return UUID.Zero;
}
internal override void set(Destinations dest, string msg, UUID agentID, string senderName, int channel)
{
Msg = msg;
return;
}
public DiscordMessage(string Msg, UUID Sender)
{
this.Msg = Msg;
ServerName = "MAP_NOT_KNOWN";
ChannelName = "MAP_NOT_KNOWN";
SenderID = Sender;
}
public DiscordMessage(string Msg, string Server, string Channel, UUID Sender)
{
this.Msg = Msg;
ServerName = Server;
ChannelName = Channel;
SenderID = Sender;
}
}
public class MessageFactory
{
public static void Post(Destinations dest, string Msg, UUID destID, int chn = 0)
public static void Post(Destinations dest, string Msg, UUID destID, int chn = 0,string ServerName="MAP_NOT_KNOWN", string ChannelName="MAP_NOT_KNOWN")
{
Message m = null;
@ -129,7 +190,7 @@ namespace Bot
m = new GroupMessage(destID);
break;
case Destinations.DEST_DISCORD:
m = new ChatMessage(UUID.Zero);
m = new DiscordMessage(Msg,ServerName, ChannelName,destID);
break;
default:
m = new ChatMessage(destID);
@ -148,18 +209,26 @@ namespace Bot
/// </summary>
public class MessageService
{
public List<MessageEventArgs> QUEUE = new List<MessageEventArgs>();
public MessageService()
{
}
public void DoSend(MessageEventArgs args)
{
OnMessageEvent(args);
}
public static void Dispatch(Message M)
{
MessageEventArgs MEA = new MessageEventArgs();
MEA.Timestamp = DateTime.Now;
MEA.Msg = M;
BotSession.Instance.MSGSVC.OnMessageEvent(MEA);
BotSession.Instance.MSGSVC.QUEUE.Add(MEA);
//BotSession.Instance.MSGSVC.OnMessageEvent(MEA);
}
protected virtual void OnMessageEvent(MessageEventArgs e)
@ -171,6 +240,15 @@ namespace Bot
}
}
public event EventHandler<MessageEventArgs> MessageEvent;
public void PopMessage()
{
if (QUEUE.Count == 0) return;
DoSend(QUEUE.First());
QUEUE.Remove(QUEUE.First());
}
}
@ -182,122 +260,4 @@ namespace Bot
public class MessageHandler_old // keep the old structure for now
{
private List<MessageQueuePacket> MSGQueue = new List<MessageQueuePacket>();
private List<ActionPacket> ActionQueue = new List<ActionPacket>();
private List<DiscordAction> DiscordQueue = new List<DiscordAction>();
public ManualResetEvent GroupJoinWaiter = new ManualResetEvent(false);
private Logger Log = BotSession.Instance.Logger;
public struct MessageQueuePacket
{
public Destinations Dest;
public UUID DestID;
public string Msg;
public int channel;
}
public struct ActionPacket
{
public Destinations Dest;
public string ActionStr;
}
public struct DiscordAction
{
public string Action;
}
public delegate void MessageHandleEvent(Destinations DType, UUID AgentOrSession, string MSG, int channel = 0);
public volatile MessageHandleEvent callbacks;
public void MessageHandle(Destinations DType, UUID AgentOrSession, string MSG, int channel = 0)
{
if (DType == Destinations.DEST_DISCORD)
{
DiscordAction DA = new DiscordAction();
DA.Action = MSG;
DiscordQueue.Add(DA);
return; // Do nothing
}
MessageQueuePacket pkt = new MessageQueuePacket();
pkt.channel = channel;
pkt.Dest = DType;
pkt.DestID = AgentOrSession;
pkt.Msg = MSG;
if (MSGQueue != null)
MSGQueue.Add(pkt);
}
public void ClearQueues()
{
MSGQueue = new List<MessageQueuePacket>();
DiscordQueue = new List<DiscordAction>();
}
public void run(GridClient client)
{
// Execute one queue item
if (MSGQueue.Count == 0) return;
MessageQueuePacket pkt = MSGQueue.First();
MSGQueue.RemoveAt(MSGQueue.IndexOf(pkt));
if (pkt.Dest == Destinations.DEST_AGENT)
{
client.Self.InstantMessage(pkt.DestID, "[" + MSGQueue.Count.ToString() + "] " + pkt.Msg);
}
else if (pkt.Dest == Destinations.DEST_GROUP)
{
if (client.Self.GroupChatSessions.ContainsKey(pkt.DestID))
client.Self.InstantMessageGroup(pkt.DestID, "[" + MSGQueue.Count.ToString() + "] " + pkt.Msg);
else
{
GroupJoinWaiter.Reset();
client.Groups.ActivateGroup(pkt.DestID);
client.Self.RequestJoinGroupChat(pkt.DestID);
//callbacks(Destinations.DEST_LOCAL, UUID.Zero, "Attempting to join group chat for secondlife:///app/group/" + pkt.DestID.ToString() + "/about");
if (GroupJoinWaiter.WaitOne(TimeSpan.FromSeconds(20), false))
{
client.Self.InstantMessageGroup(pkt.DestID, "[" + MSGQueue.Count.ToString() + "] " + pkt.Msg);
}
else
{
MSGQueue.Add(pkt); // Because we failed to join the group chat we'll tack this onto the end of the queue and try again
}
}
}
else if (pkt.Dest == Destinations.DEST_LOCAL)
{
client.Self.Chat("[" + MSGQueue.Count.ToString() + "] " + pkt.Msg, pkt.channel, ChatType.Normal);
}
}
public string CheckActions()
{
string RETURNStr = "";
if (ActionQueue.Count == 0) return "NONE";
else
{
RETURNStr = ActionQueue.First().ActionStr;
ActionQueue.Remove(ActionQueue.First());
return RETURNStr;
}
}
public string CheckDiscordActions()
{
if (DiscordQueue.Count == 0) return "NONE";
else
{
string RET = DiscordQueue.First().Action;
DiscordQueue.Remove(DiscordQueue.First());
return RET;
}
}
}
}

View file

@ -241,9 +241,6 @@ namespace Bot
{
Console.WriteLine("Check Creds:\n \nFirst Name: '" + fna + "'\nLast Name: '" + lna + "'\nPWD: '" + pwd + "'\nBotStr: '" + BotStr + "'\nBotVer: " + BotVer.ToString()+"\n \nLogin Message: "+client.Network.LoginMessage);
if(args[0] == "-x") // debug launch
Console.ReadKey();
}
if (LoggedIn)
{
@ -339,7 +336,7 @@ namespace Bot
client.Self.RetrieveInstantMessages();
if (client.Network.Connected == false) g_iIsRunning = false; // Quit the program and restart immediately!
Thread.Sleep(2000);
Thread.Sleep(1000);
if (conf.ConfigFor == "Main")
@ -434,6 +431,16 @@ namespace Bot
// onSimChange(null, new SimChangedEventArgs(client.Network.CurrentSim));
//}
if (BotSession.Instance.EnqueueExit) g_iIsRunning = false;
if (BotSession.Instance.EnqueueGroupRefresh)
{
BotSession.Instance.EnqueueGroupRefresh = false;
ReloadGroupsCache();
}
BotSession.Instance.MSGSVC.PopMessage();
}
prompter.Interrupt();
@ -454,22 +461,33 @@ namespace Bot
private static ManualResetEvent GroupJoinWaiter = new ManualResetEvent(false);
private static void MSGSVC_onGroupMessage(object sender, MessageEventArgs e)
{
throw new NotImplementedException();
// not implemented, yet, but do not throw error
switch (e.Msg.GetMessageSource())
{
case Destinations.DEST_GROUP:
BotSession.Instance.grid.Self.InstantMessageGroup(e.Msg.GetTarget(), e.Msg.GetMessage());
break;
default:
return;
}
}
private static void MSGSVC_onIM(object sender, MessageEventArgs e)
{
throw new NotImplementedException();
switch (e.Msg.GetMessageSource())
{
case Destinations.DEST_AGENT:
BotSession.Instance.grid.Self.InstantMessage(e.Msg.GetTarget(), e.Msg.GetMessage());
break;
default:
return;
}
}
private static void MSGSVC_onChat(object sender, MessageEventArgs e)
{
switch (e.Msg.GetMessageSource())
{
case Destinations.DEST_AGENT:
// send as IM
BotSession.Instance.grid.Self.InstantMessage(e.Msg.GetTarget(), e.Msg.GetMessage());
break;
case Destinations.DEST_LOCAL:
BotSession.Instance.grid.Self.Chat(e.Msg.GetMessage(), e.Msg.GetChannel(), ChatType.Normal);
break;

View file

@ -35,10 +35,9 @@ namespace Bot.WebHookServer
get { return 1.7f; }
}
public string getTick()
public void getTick()
{
return "";
return;
}
public void passArguments(string data)