2007-04-28 20:54:02 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2009-07-15 23:23:11 +00:00
|
|
|
using System.Threading;
|
2007-04-28 20:54:02 +00:00
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Xml;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse;
|
|
|
|
|
using OpenMetaverse.Packets;
|
|
|
|
|
using OpenMetaverse.Utilities;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2008-07-21 21:12:59 +00:00
|
|
|
namespace OpenMetaverse.TestClient
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-07-21 21:12:59 +00:00
|
|
|
public class TestClient : GridClient
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-07-25 05:15:05 +00:00
|
|
|
public UUID GroupID = UUID.Zero;
|
|
|
|
|
public Dictionary<UUID, GroupMember> GroupMembers;
|
2008-08-21 01:19:06 +00:00
|
|
|
public Dictionary<UUID, AvatarAppearancePacket> Appearances = new Dictionary<UUID, AvatarAppearancePacket>();
|
|
|
|
|
public Dictionary<string, Command> Commands = new Dictionary<string, Command>();
|
|
|
|
|
public bool Running = true;
|
2008-02-07 17:32:06 +00:00
|
|
|
public bool GroupCommands = false;
|
2007-04-28 20:54:02 +00:00
|
|
|
public string MasterName = String.Empty;
|
2008-07-25 05:15:05 +00:00
|
|
|
public UUID MasterKey = UUID.Zero;
|
2008-07-30 06:40:47 +00:00
|
|
|
public bool AllowObjectMaster = false;
|
2008-08-21 01:19:06 +00:00
|
|
|
public ClientManager ClientManager;
|
2008-04-09 20:02:07 +00:00
|
|
|
public VoiceManager VoiceManager;
|
2008-05-11 09:02:59 +00:00
|
|
|
// Shell-like inventory commands need to be aware of the 'current' inventory folder.
|
|
|
|
|
public InventoryFolder CurrentDirectory = null;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
private System.Timers.Timer updateTimer;
|
2009-06-28 19:13:05 +00:00
|
|
|
private UUID GroupMembersRequestID;
|
2009-07-15 23:23:11 +00:00
|
|
|
public Dictionary<UUID, Group> GroupsCache = null;
|
|
|
|
|
private ManualResetEvent GroupsEvent = new ManualResetEvent(false);
|
2007-07-11 09:17:46 +00:00
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TestClient(ClientManager manager)
|
|
|
|
|
{
|
2008-08-21 01:19:06 +00:00
|
|
|
ClientManager = manager;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2007-08-10 20:16:19 +00:00
|
|
|
updateTimer = new System.Timers.Timer(500);
|
2007-04-28 20:54:02 +00:00
|
|
|
updateTimer.Elapsed += new System.Timers.ElapsedEventHandler(updateTimer_Elapsed);
|
|
|
|
|
|
|
|
|
|
RegisterAllCommands(Assembly.GetExecutingAssembly());
|
|
|
|
|
|
2008-05-08 16:58:09 +00:00
|
|
|
Settings.LOG_LEVEL = Helpers.LogLevel.Debug;
|
2007-11-06 09:26:10 +00:00
|
|
|
Settings.LOG_RESENDS = false;
|
2007-04-28 20:54:02 +00:00
|
|
|
Settings.STORE_LAND_PATCHES = true;
|
2007-08-20 09:26:21 +00:00
|
|
|
Settings.ALWAYS_DECODE_OBJECTS = true;
|
2007-04-28 20:54:02 +00:00
|
|
|
Settings.ALWAYS_REQUEST_OBJECTS = true;
|
2007-08-10 20:16:19 +00:00
|
|
|
Settings.SEND_AGENT_UPDATES = true;
|
2009-07-19 03:50:09 +00:00
|
|
|
Settings.USE_ASSET_CACHE = true;
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
Network.RegisterCallback(PacketType.AgentDataUpdate, new NetworkManager.PacketCallback(AgentDataUpdateHandler));
|
2008-05-11 09:02:59 +00:00
|
|
|
Network.OnLogin += new NetworkManager.LoginCallback(LoginHandler);
|
2009-10-16 02:53:53 +00:00
|
|
|
Self.IM += Self_IM;
|
2007-04-28 20:54:02 +00:00
|
|
|
Groups.OnGroupMembers += new GroupManager.GroupMembersCallback(GroupMembersHandler);
|
2007-11-06 09:26:10 +00:00
|
|
|
Inventory.OnObjectOffered += new InventoryManager.ObjectOfferedCallback(Inventory_OnInventoryObjectReceived);
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
Network.RegisterCallback(PacketType.AvatarAppearance, new NetworkManager.PacketCallback(AvatarAppearanceHandler));
|
2007-08-10 20:16:19 +00:00
|
|
|
Network.RegisterCallback(PacketType.AlertMessage, new NetworkManager.PacketCallback(AlertMessageHandler));
|
2008-04-09 20:02:07 +00:00
|
|
|
|
|
|
|
|
VoiceManager = new VoiceManager(this);
|
2008-08-21 01:19:06 +00:00
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
updateTimer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-16 02:53:53 +00:00
|
|
|
void Self_IM(object sender, InstantMessageEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
bool groupIM = e.IM.GroupIM && GroupMembers != null && GroupMembers.ContainsKey(e.IM.FromAgentID) ? true : false;
|
|
|
|
|
|
|
|
|
|
if (e.IM.FromAgentID == MasterKey || (GroupCommands && groupIM))
|
|
|
|
|
{
|
|
|
|
|
// Received an IM from someone that is authenticated
|
|
|
|
|
Console.WriteLine("<{0} ({1})> {2}: {3} (@{4}:{5})", e.IM.GroupIM ? "GroupIM" : "IM", e.IM.Dialog, e.IM.FromAgentName, e.IM.Message,
|
|
|
|
|
e.IM.RegionID, e.IM.Position);
|
|
|
|
|
|
|
|
|
|
if (e.IM.Dialog == InstantMessageDialog.RequestTeleport)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Accepting teleport lure.");
|
|
|
|
|
Self.TeleportLureRespond(e.IM.FromAgentID, true);
|
|
|
|
|
}
|
|
|
|
|
else if (
|
|
|
|
|
e.IM.Dialog == InstantMessageDialog.MessageFromAgent ||
|
|
|
|
|
e.IM.Dialog == InstantMessageDialog.MessageFromObject)
|
|
|
|
|
{
|
|
|
|
|
ClientManager.Instance.DoCommandAll(e.IM.Message, e.IM.FromAgentID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Received an IM from someone that is not the bot's master, ignore
|
|
|
|
|
Console.WriteLine("<{0} ({1})> {2} (not master): {3} (@{4}:{5})", e.IM.GroupIM ? "GroupIM" : "IM", e.IM.Dialog, e.IM.FromAgentName, e.IM.Message,
|
|
|
|
|
e.IM.RegionID, e.IM.Position);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-11 09:02:59 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initialize everything that needs to be initialized once we're logged in.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="login">The status of the login</param>
|
|
|
|
|
/// <param name="message">Error message on failure, MOTD on success.</param>
|
2008-08-21 01:19:06 +00:00
|
|
|
public void LoginHandler(LoginStatus login, string message)
|
2008-05-11 09:02:59 +00:00
|
|
|
{
|
|
|
|
|
if (login == LoginStatus.Success)
|
|
|
|
|
{
|
2008-08-21 01:19:06 +00:00
|
|
|
// Start in the inventory root folder.
|
|
|
|
|
CurrentDirectory = Inventory.Store.RootFolder;
|
2008-05-11 09:02:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
public void RegisterAllCommands(Assembly assembly)
|
|
|
|
|
{
|
|
|
|
|
foreach (Type t in assembly.GetTypes())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (t.IsSubclassOf(typeof(Command)))
|
|
|
|
|
{
|
|
|
|
|
ConstructorInfo info = t.GetConstructor(new Type[] { typeof(TestClient) });
|
|
|
|
|
Command command = (Command)info.Invoke(new object[] { this });
|
|
|
|
|
RegisterCommand(command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterCommand(Command command)
|
|
|
|
|
{
|
2008-08-21 01:19:06 +00:00
|
|
|
command.Client = this;
|
|
|
|
|
if (!Commands.ContainsKey(command.Name.ToLower()))
|
|
|
|
|
{
|
2007-07-11 09:17:46 +00:00
|
|
|
Commands.Add(command.Name.ToLower(), command);
|
2008-08-21 01:19:06 +00:00
|
|
|
}
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 23:23:11 +00:00
|
|
|
public void ReloadGroupsCache()
|
|
|
|
|
{
|
|
|
|
|
GroupManager.CurrentGroupsCallback callback =
|
|
|
|
|
new GroupManager.CurrentGroupsCallback(Groups_OnCurrentGroups);
|
|
|
|
|
Groups.OnCurrentGroups += callback;
|
|
|
|
|
Groups.RequestCurrentGroups();
|
|
|
|
|
GroupsEvent.WaitOne(10000, false);
|
|
|
|
|
Groups.OnCurrentGroups -= callback;
|
|
|
|
|
GroupsEvent.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UUID GroupName2UUID(String groupName)
|
|
|
|
|
{
|
|
|
|
|
UUID tryUUID;
|
|
|
|
|
if (UUID.TryParse(groupName,out tryUUID))
|
|
|
|
|
return tryUUID;
|
|
|
|
|
if (null == GroupsCache) {
|
|
|
|
|
ReloadGroupsCache();
|
|
|
|
|
if (null == GroupsCache)
|
|
|
|
|
return UUID.Zero;
|
|
|
|
|
}
|
|
|
|
|
lock(GroupsCache) {
|
|
|
|
|
if (GroupsCache.Count > 0) {
|
|
|
|
|
foreach (Group currentGroup in GroupsCache.Values)
|
|
|
|
|
if (currentGroup.Name.ToLower() == groupName.ToLower())
|
|
|
|
|
return currentGroup.ID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return UUID.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Groups_OnCurrentGroups(Dictionary<UUID, Group> pGroups)
|
|
|
|
|
{
|
|
|
|
|
if (null == GroupsCache)
|
|
|
|
|
GroupsCache = pGroups;
|
|
|
|
|
else
|
|
|
|
|
lock(GroupsCache) { GroupsCache = pGroups; }
|
|
|
|
|
GroupsEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
private void updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (Command c in Commands.Values)
|
|
|
|
|
if (c.Active)
|
|
|
|
|
c.Think();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AgentDataUpdateHandler(Packet packet, Simulator sim)
|
|
|
|
|
{
|
|
|
|
|
AgentDataUpdatePacket p = (AgentDataUpdatePacket)packet;
|
2007-11-06 09:26:10 +00:00
|
|
|
if (p.AgentData.AgentID == sim.Client.Self.AgentID)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
|
|
|
|
GroupID = p.AgentData.ActiveGroupID;
|
|
|
|
|
|
2009-06-28 19:13:05 +00:00
|
|
|
GroupMembersRequestID = sim.Client.Groups.RequestGroupMembers(GroupID);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 21:37:35 +00:00
|
|
|
private void GroupMembersHandler(UUID requestID, UUID groupID, Dictionary<UUID, GroupMember> members)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2009-06-30 21:37:35 +00:00
|
|
|
if (requestID != GroupMembersRequestID) return;
|
2009-06-28 19:13:05 +00:00
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
GroupMembers = members;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AvatarAppearanceHandler(Packet packet, Simulator simulator)
|
|
|
|
|
{
|
|
|
|
|
AvatarAppearancePacket appearance = (AvatarAppearancePacket)packet;
|
|
|
|
|
|
|
|
|
|
lock (Appearances) Appearances[appearance.Sender.ID] = appearance;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-10 20:16:19 +00:00
|
|
|
private void AlertMessageHandler(Packet packet, Simulator simulator)
|
|
|
|
|
{
|
|
|
|
|
AlertMessagePacket message = (AlertMessagePacket)packet;
|
|
|
|
|
|
2008-08-12 22:38:02 +00:00
|
|
|
Logger.Log("[AlertMessage] " + Utils.BytesToString(message.AlertData.Message), Helpers.LogLevel.Info, this);
|
2007-08-10 20:16:19 +00:00
|
|
|
}
|
2009-10-16 02:53:53 +00:00
|
|
|
|
2008-08-21 01:19:06 +00:00
|
|
|
private bool Inventory_OnInventoryObjectReceived(InstantMessage offer, AssetType type,
|
2008-07-25 05:15:05 +00:00
|
|
|
UUID objectID, bool fromTask)
|
2007-07-13 14:49:36 +00:00
|
|
|
{
|
2008-07-25 05:15:05 +00:00
|
|
|
if (MasterKey != UUID.Zero)
|
2007-07-13 14:49:36 +00:00
|
|
|
{
|
2008-05-06 00:31:03 +00:00
|
|
|
if (offer.FromAgentID != MasterKey)
|
2008-08-21 01:19:06 +00:00
|
|
|
return false;
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
2008-05-06 00:31:03 +00:00
|
|
|
else if (GroupMembers != null && !GroupMembers.ContainsKey(offer.FromAgentID))
|
2007-07-13 14:49:36 +00:00
|
|
|
{
|
2008-08-21 01:19:06 +00:00
|
|
|
return false;
|
2008-07-30 06:40:47 +00:00
|
|
|
}
|
2007-07-13 14:49:36 +00:00
|
|
|
|
2008-08-21 01:19:06 +00:00
|
|
|
return true;
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
2008-08-21 01:19:06 +00:00
|
|
|
}
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|