2007-04-28 20:54:02 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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;
|
|
|
|
|
public Dictionary<UUID, AvatarAppearancePacket> Appearances = new Dictionary<UUID, AvatarAppearancePacket>();
|
2007-04-28 20:54:02 +00:00
|
|
|
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;
|
2007-04-28 20:54:02 +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
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
private Quaternion bodyRotation = Quaternion.Identity;
|
|
|
|
|
private Vector3 forward = new Vector3(0, 0.9999f, 0);
|
|
|
|
|
private Vector3 left = new Vector3(0.9999f, 0, 0);
|
|
|
|
|
private Vector3 up = new Vector3(0, 0, 0.9999f);
|
2007-04-28 20:54:02 +00:00
|
|
|
private System.Timers.Timer updateTimer;
|
2007-07-11 09:17:46 +00:00
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TestClient(ClientManager manager)
|
|
|
|
|
{
|
|
|
|
|
ClientManager = manager;
|
|
|
|
|
|
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;
|
2008-04-07 15:10:01 +00:00
|
|
|
Settings.USE_TEXTURE_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);
|
2007-11-06 09:26:10 +00:00
|
|
|
Self.OnInstantMessage += new AgentManager.InstantMessageCallback(Self_OnInstantMessage);
|
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);
|
2007-04-28 20:54:02 +00:00
|
|
|
updateTimer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
|
|
|
|
public void LoginHandler(LoginStatus login, string message)
|
|
|
|
|
{
|
|
|
|
|
if (login == LoginStatus.Success)
|
|
|
|
|
{
|
2008-07-29 21:36:53 +00:00
|
|
|
// Create the stores:
|
|
|
|
|
InventoryStore = new Inventory(Inventory, Inventory.InventorySkeleton);
|
|
|
|
|
LibraryStore = new Inventory(Inventory, Inventory.LibrarySkeleton);
|
|
|
|
|
|
|
|
|
|
// Start in the inventory root folder:
|
|
|
|
|
CurrentDirectory = InventoryStore.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)
|
|
|
|
|
{
|
|
|
|
|
command.Client = this;
|
|
|
|
|
if (!Commands.ContainsKey(command.Name.ToLower()))
|
|
|
|
|
{
|
2007-07-11 09:17:46 +00:00
|
|
|
Commands.Add(command.Name.ToLower(), command);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//breaks up large responses to deal with the max IM size
|
2008-07-25 05:15:05 +00:00
|
|
|
private void SendResponseIM(GridClient client, UUID fromAgentID, string data)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
|
|
|
|
for ( int i = 0 ; i < data.Length ; i += 1024 ) {
|
|
|
|
|
int y;
|
|
|
|
|
if ((i + 1023) > data.Length)
|
|
|
|
|
{
|
|
|
|
|
y = data.Length - i;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
y = 1023;
|
|
|
|
|
}
|
|
|
|
|
string message = data.Substring(i, y);
|
2007-12-21 05:31:13 +00:00
|
|
|
client.Self.InstantMessage(fromAgentID, message);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
public void DoCommand(string cmd, UUID fromAgentID)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2007-08-29 08:55:53 +00:00
|
|
|
string[] tokens;
|
|
|
|
|
|
|
|
|
|
try { tokens = Parsing.ParseArguments(cmd); }
|
|
|
|
|
catch (FormatException ex) { Console.WriteLine(ex.Message); return; }
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
if (tokens.Length == 0)
|
|
|
|
|
return;
|
2007-08-29 08:55:53 +00:00
|
|
|
|
2007-04-28 20:54:02 +00:00
|
|
|
string firstToken = tokens[0].ToLower();
|
|
|
|
|
|
|
|
|
|
// "all balance" will send the balance command to all currently logged in bots
|
|
|
|
|
if (firstToken == "all" && tokens.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
cmd = String.Empty;
|
|
|
|
|
|
|
|
|
|
// Reserialize all of the arguments except for "all"
|
|
|
|
|
for (int i = 1; i < tokens.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
cmd += tokens[i] + " ";
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-21 05:31:13 +00:00
|
|
|
ClientManager.DoCommandAll(cmd, fromAgentID);
|
2007-04-28 20:54:02 +00:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Commands.ContainsKey(firstToken))
|
|
|
|
|
{
|
|
|
|
|
string[] args = new string[tokens.Length - 1];
|
|
|
|
|
Array.Copy(tokens, 1, args, 0, args.Length);
|
2007-05-15 17:52:58 +00:00
|
|
|
string response = Commands[firstToken].Execute(args, fromAgentID);
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2007-11-06 09:26:10 +00:00
|
|
|
if (!String.IsNullOrEmpty(response))
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
|
|
|
|
Console.WriteLine(response);
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
if (fromAgentID != UUID.Zero && Network.Connected)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
|
|
|
|
// IMs don't like \r\n line endings, clean them up first
|
2007-12-21 05:31:13 +00:00
|
|
|
response = response.Replace("\r", String.Empty);
|
|
|
|
|
SendResponseIM(this, fromAgentID, response);
|
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
|
|
|
{
|
|
|
|
|
Console.WriteLine("Got the group ID for " + sim.Client.ToString() + ", requesting group members...");
|
|
|
|
|
GroupID = p.AgentData.ActiveGroupID;
|
|
|
|
|
|
2007-08-10 20:16:19 +00:00
|
|
|
sim.Client.Groups.RequestGroupMembers(GroupID);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
private void GroupMembersHandler(Dictionary<UUID, GroupMember> members)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
|
|
|
|
Console.WriteLine("Got " + members.Count + " group members.");
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2007-08-22 18:16:38 +00:00
|
|
|
private void Self_OnInstantMessage(InstantMessage im, Simulator simulator)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-02-07 17:32:06 +00:00
|
|
|
|
|
|
|
|
bool groupIM = im.GroupIM && GroupMembers != null && GroupMembers.ContainsKey(im.FromAgentID) ? true : false;
|
|
|
|
|
|
2008-07-30 06:40:47 +00:00
|
|
|
if ((im.Dialog == InstantMessageDialog.MessageFromObject) && !AllowObjectMaster)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (im.FromAgentID == MasterKey || im.FromAgentName == MasterName || (GroupCommands && groupIM))
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-02-07 17:32:06 +00:00
|
|
|
// Received an IM from someone that is authenticated
|
|
|
|
|
Console.WriteLine("<{0} ({1})> {2}: {3} (@{4}:{5})", im.GroupIM ? "GroupIM" : "IM", im.Dialog, im.FromAgentName, im.Message, im.RegionID, im.Position);
|
|
|
|
|
|
|
|
|
|
if (im.Dialog == InstantMessageDialog.RequestTeleport)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Accepting teleport lure.");
|
|
|
|
|
Self.TeleportLureRespond(im.FromAgentID, true);
|
|
|
|
|
}
|
|
|
|
|
else if (
|
|
|
|
|
im.Dialog == InstantMessageDialog.MessageFromAgent ||
|
|
|
|
|
im.Dialog == InstantMessageDialog.MessageFromObject)
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-02-07 17:32:06 +00:00
|
|
|
DoCommand(im.Message, im.FromAgentID);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-02-07 17:32:06 +00:00
|
|
|
|
|
|
|
|
else
|
2007-04-28 20:54:02 +00:00
|
|
|
{
|
2008-02-07 17:32:06 +00:00
|
|
|
// Received an IM from someone that is not the bot's master, ignore
|
|
|
|
|
Console.WriteLine("<{0} ({1})> {2} (not master): {3} (@{4}:{5})", im.GroupIM ? "GroupIM" : "IM", im.Dialog, im.FromAgentName, im.Message,
|
|
|
|
|
im.RegionID, im.Position);
|
2007-07-13 14:49:36 +00:00
|
|
|
return;
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
2007-07-13 14:49:36 +00:00
|
|
|
}
|
2007-04-28 20:54:02 +00:00
|
|
|
|
2008-07-29 21:36:53 +00:00
|
|
|
private UUID 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-07-29 21:36:53 +00:00
|
|
|
return UUID.Zero;
|
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-07-29 21:36:53 +00:00
|
|
|
return UUID.Zero;
|
2007-07-13 14:49:36 +00:00
|
|
|
}
|
2008-07-30 06:40:47 +00:00
|
|
|
else if (MasterName != String.Empty)
|
|
|
|
|
{
|
|
|
|
|
if (offer.FromAgentName != MasterName)
|
|
|
|
|
return UUID.Zero;
|
|
|
|
|
}
|
|
|
|
|
else if (fromTask && !AllowObjectMaster)
|
|
|
|
|
{
|
|
|
|
|
return UUID.Zero;
|
|
|
|
|
}
|
2007-07-13 14:49:36 +00:00
|
|
|
|
2008-07-29 21:36:53 +00:00
|
|
|
return Inventory.FindFolderForType(type);
|
2007-04-28 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|