SecondBot/SecondBotEvents/Services/InteractionService.cs
2022-05-29 17:38:28 +01:00

225 lines
8.3 KiB
C#

using Newtonsoft.Json;
using OpenMetaverse;
using SecondBotEvents.Config;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace SecondBotEvents.Services
{
public class InteractionService : Services
{
protected InteractionConfig myConfig;
protected bool botConnected = false;
public InteractionService(EventsSecondBot setMaster) : base(setMaster)
{
myConfig = new InteractionConfig(master.fromEnv,master.fromFolder);
if (myConfig.GetEnabled() == false)
{
Console.WriteLine(Status());
return;
}
}
public override string Status()
{
if(myConfig == null)
{
return "Interaction Service [Config status broken]";
}
if (myConfig.GetEnabled() == false)
{
return "Interaction Service [- Not requested -]";
}
return "Interaction Service [Waiting for bot]";
}
protected bool processRequest(bool enabledByConfig, string avatarName, string sourceName)
{
if(myConfig.GetEnabled() == false)
{
jsonOuput(false, sourceName, avatarName, "Interactions hooks disabled");
return false;
}
if (enabledByConfig == false)
{
jsonOuput(false, sourceName, avatarName, "disabled by config");
return false;
}
if (master.CommandsService.myConfig.GetOnlyMasterAvs() == false)
{
// master avatars list is not enabled. unable to continue
jsonOuput(false, sourceName, avatarName, "masters AV list is disabled");
return false;
}
if (master.CommandsService.myConfig.GetMastersCSV().Contains(avatarName) == false)
{
jsonOuput(false, sourceName, avatarName, "Not from accepted master");
return false;
}
return true;
}
protected void BotClientRestart(object o, BotClientNotice e)
{
botConnected = false;
Console.WriteLine("Interaction Service [Attached to new client]");
master.botClient.client.Network.LoggedOut += BotLoggedOut;
master.botClient.client.Network.SimConnected += BotLoggedIn;
}
protected void jsonOuput(bool status, string eventype, string from, string why="", Dictionary<string, string> setMisc = null)
{
if (myConfig.GetEnableJsonOutputEvents() == false)
{
return;
}
master.CommandsService.SmartCommandReply(
status,
myConfig.GetJsonOutputEventsTarget(),
JsonConvert.SerializeObject(new interactionEvent(from, eventype, status, why, setMisc)),
"interactions"
);
}
protected void BotTeleportOffer(object o, InstantMessageEventArgs e)
{
if((e.IM.Dialog != InstantMessageDialog.RequestTeleport) && (e.IM.Dialog != InstantMessageDialog.RequestLure))
{
return;
}
string mode = "Teleport Bot to Av";
if (e.IM.Dialog == InstantMessageDialog.RequestLure)
{
mode = "Teleport Av to Bot";
}
if (processRequest(myConfig.GetAcceptTeleports(), e.IM.FromAgentName, mode) == false)
{
if (e.IM.Dialog != InstantMessageDialog.RequestLure)
{
return;
}
master.botClient.client.Self.TeleportLureRespond(e.IM.FromAgentID, e.IM.IMSessionID, false);
return;
}
jsonOuput(true, mode, e.IM.FromAgentName);
if (e.IM.Dialog == InstantMessageDialog.RequestLure)
{
master.botClient.client.Self.SendTeleportLure(e.IM.FromAgentID);
return;
}
master.botClient.client.Self.TeleportLureRespond(e.IM.FromAgentID, e.IM.IMSessionID, true);
}
protected void BotGroupInviteOffer(object o, GroupInvitationEventArgs e)
{
if (processRequest(myConfig.GetAcceptGroupInvites(), e.FromName, "GroupInvite") == false)
{
return;
}
e.Accept = true;
jsonOuput(true, "GroupInvite", e.FromName);
}
protected void BotInventoryOffer(object o, InventoryObjectOfferedEventArgs e)
{
if(processRequest(myConfig.GetAcceptInventory(),e.Offer.FromAgentName, "InventoryUpdate") == false)
{
return;
}
e.Accept = true;
Dictionary<string, string> details = new Dictionary<string, string>();
details.Add("itemuuid", e.ObjectID.ToString());
details.Add("itemtype", e.AssetType.ToString());
InventoryItem itm = master.botClient.client.Inventory.FetchItem(e.ObjectID, master.botClient.client.Self.AgentID, 2000);
if(itm != null)
{
details.Add("itemname", itm.Name);
}
jsonOuput(true, "InventoryUpdate", e.Offer.FromAgentName, "see misc", details);
}
protected void BotFriendRequested(object o, FriendshipOfferedEventArgs e)
{
if (processRequest(myConfig.GetAcceptGroupInvites(), e.AgentName, "FriendRequest") == false)
{
return;
}
master.botClient.client.Friends.AcceptFriendship(e.AgentID, e.SessionID);
jsonOuput(true, "FriendRequest", e.AgentName);
}
protected void BotLoggedOut(object o, LoggedOutEventArgs e)
{
botConnected = false;
master.botClient.client.Network.SimConnected += BotLoggedIn;
Console.WriteLine("Interaction Service [Standby]");
}
protected void BotLoggedIn(object o, SimConnectedEventArgs e)
{
master.botClient.client.Network.SimConnected -= BotLoggedIn;
master.botClient.client.Friends.FriendshipOffered += BotFriendRequested;
master.botClient.client.Inventory.InventoryObjectOffered += BotInventoryOffer;
master.botClient.client.Groups.GroupInvitation += BotGroupInviteOffer;
master.botClient.client.Self.IM += BotTeleportOffer;
botConnected = true;
Console.WriteLine("Interaction Service [Active]");
}
public override void Start()
{
if (myConfig.GetEnabled() == false)
{
Console.WriteLine("Interaction Service [- Not requested -]");
return;
}
Stop();
master.BotClientNoticeEvent += BotClientRestart;
Console.WriteLine("Interaction Service [Starting]");
}
public override void Stop()
{
if (myConfig.GetEnabled() == false)
{
return;
}
master.BotClientNoticeEvent -= BotClientRestart;
if (master.botClient != null)
{
if (master.botClient.client != null)
{
master.botClient.client.Friends.FriendshipOffered -= BotFriendRequested;
master.botClient.client.Inventory.InventoryObjectOffered -= BotInventoryOffer;
master.botClient.client.Groups.GroupInvitation -= BotGroupInviteOffer;
}
}
Console.WriteLine("Interaction Service [Stopping]");
}
}
public class interactionEvent
{
public string from = "?";
public string eventType = "?";
public bool accepted = false;
public string info = "";
public Dictionary<string, string> misc = new Dictionary<string, string>();
public interactionEvent(string setFrom, string setType, bool setAccepted, string setInfo, Dictionary<string, string> setMisc = null)
{
accepted = setAccepted;
from = setFrom;
eventType = setType;
info = setInfo;
if(setMisc != null)
{
misc = setMisc;
}
}
}
}