From b9f4836d3edbeac98d416901d88fd3427b04981e Mon Sep 17 00:00:00 2001 From: Melanie Date: Wed, 28 Mar 2012 23:51:44 +0200 Subject: [PATCH] Committing the Avination calling card module --- .../Avatar/Friends/CallingCardModule.cs | 286 ++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 OpenSim/Region/CoreModules/Avatar/Friends/CallingCardModule.cs diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/CallingCardModule.cs b/OpenSim/Region/CoreModules/Avatar/Friends/CallingCardModule.cs new file mode 100644 index 0000000000..5e2a651715 --- /dev/null +++ b/OpenSim/Region/CoreModules/Avatar/Friends/CallingCardModule.cs @@ -0,0 +1,286 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using log4net; +using Nini.Config; +using OpenMetaverse; +using OpenSim.Framework; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; +using OpenSim.Services.Interfaces; +using Mono.Addins; + +namespace Careminster.XCallingCard.Modules +{ + [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XCallingCard")] + public class CallingCardModule : ISharedRegionModule, ICallingCardModule + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + protected List m_Scenes = new List(); + protected bool m_Enabled = true; + + public void Initialise(IConfigSource source) + { + IConfig ccConfig = source.Configs["XCallingCard"]; + if (ccConfig != null) + m_Enabled = ccConfig.GetBoolean("Enabled", true); + } + + public void AddRegion(Scene scene) + { + if (!m_Enabled) + return; + + m_Scenes.Add(scene); + + scene.RegisterModuleInterface(this); + } + + public void RemoveRegion(Scene scene) + { + if (!m_Enabled) + return; + + m_Scenes.Remove(scene); + + scene.EventManager.OnNewClient -= OnNewClient; + scene.EventManager.OnIncomingInstantMessage += + OnIncomingInstantMessage; + + scene.UnregisterModuleInterface(this); + } + + public void RegionLoaded(Scene scene) + { + if (!m_Enabled) + return; + scene.EventManager.OnNewClient += OnNewClient; + } + + public void PostInitialise() + { + } + + public void Close() + { + } + + public Type ReplaceableInterface + { + get { return null; } + } + + public string Name + { + get { return "XCallingCardModule"; } + } + + private void OnNewClient(IClientAPI client) + { + client.OnOfferCallingCard += OnOfferCallingCard; + client.OnAcceptCallingCard += OnAcceptCallingCard; + client.OnDeclineCallingCard += OnDeclineCallingCard; + } + + private void OnOfferCallingCard(IClientAPI client, UUID destID, UUID transactionID) + { + ScenePresence sp = GetClientPresence(client.AgentId); + if (sp != null) + { + // If we're in god mode, we reverse the meaning. Offer + // calling card becomes "Take a calling card" for that + // person, no matter if they agree or not. + if (sp.GodLevel >= 200) + { + CreateCallingCard(client.AgentId, destID, UUID.Zero, true); + return; + } + } + + IClientAPI dest = FindClientObject(destID); + if (dest != null) + { + DoCallingCardOffer(dest, client.AgentId); + return; + } + + IMessageTransferModule transferModule = + m_Scenes[0].RequestModuleInterface(); + + if (transferModule != null) + { + transferModule.SendInstantMessage(new GridInstantMessage( + client.Scene, client.AgentId, + client.FirstName+" "+client.LastName, + destID, (byte)211, false, + String.Empty, + transactionID, false, new Vector3(), new byte[0]), + delegate(bool success) {} ); + } + } + + private void DoCallingCardOffer(IClientAPI dest, UUID from) + { + UUID itemID = CreateCallingCard(dest.AgentId, from, UUID.Zero, false); + + dest.SendOfferCallingCard(from, itemID); + } + + // Create a calling card in the user's inventory. This is called + // from direct calling card creation, when the offer is forwarded, + // and from the friends module when the friend is confirmed. + // Because of the latter, it will send a bulk inventory update + // if the receiving user is in the same simulator. + public UUID CreateCallingCard(UUID userID, UUID creatorID, UUID folderID) + { + return CreateCallingCard(userID, creatorID, folderID, false); + } + + private UUID CreateCallingCard(UUID userID, UUID creatorID, UUID folderID, bool isGod) + { + IUserAccountService userv = m_Scenes[0].UserAccountService; + if (userv == null) + return UUID.Zero; + + UserAccount info = userv.GetUserAccount(UUID.Zero, creatorID); + if (info == null) + return UUID.Zero; + + IInventoryService inv = m_Scenes[0].InventoryService; + if (inv == null) + return UUID.Zero; + + if (folderID == UUID.Zero) + { + InventoryFolderBase folder = inv.GetFolderForType(userID, + AssetType.CallingCard); + + if (folder == null) // Nowhere to put it + return UUID.Zero; + + folderID = folder.ID; + } + + m_log.DebugFormat("[XCALLINGCARD]: Creating calling card for {0} in inventory of {1}", info.Name, userID); + + InventoryItemBase item = new InventoryItemBase(); + item.AssetID = UUID.Zero; + item.AssetType = (int)AssetType.CallingCard; + item.BasePermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify); + if (isGod) + item.BasePermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer | PermissionMask.Move); + + item.EveryOnePermissions = (uint)PermissionMask.None; + item.CurrentPermissions = item.BasePermissions; + item.NextPermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify); + + item.ID = UUID.Random(); + item.CreatorId = creatorID.ToString(); + item.Owner = userID; + item.GroupID = UUID.Zero; + item.GroupOwned = false; + item.Folder = folderID; + + item.CreationDate = Util.UnixTimeSinceEpoch(); + item.InvType = (int)InventoryType.CallingCard; + item.Flags = 0; + + item.Name = info.Name; + item.Description = ""; + + item.SalePrice = 10; + item.SaleType = (byte)SaleType.Not; + + inv.AddItem(item); + + IClientAPI client = FindClientObject(userID); + if (client != null) + client.SendBulkUpdateInventory(item); + + return item.ID; + } + + private void OnAcceptCallingCard(IClientAPI client, UUID transactionID, UUID folderID) + { + } + + private void OnDeclineCallingCard(IClientAPI client, UUID transactionID) + { + IInventoryService invService = m_Scenes[0].InventoryService; + + InventoryFolderBase trashFolder = + invService.GetFolderForType(client.AgentId, AssetType.TrashFolder); + + InventoryItemBase item = new InventoryItemBase(transactionID, client.AgentId); + item = invService.GetItem(item); + + if (item != null && trashFolder != null) + { + item.Folder = trashFolder.ID; + List uuids = new List(); + uuids.Add(item.ID); + invService.DeleteItems(item.Owner, uuids); + m_Scenes[0].AddInventoryItem(client, item); + } + } + + public IClientAPI FindClientObject(UUID agentID) + { + Scene scene = GetClientScene(agentID); + if (scene == null) + return null; + + ScenePresence presence = scene.GetScenePresence(agentID); + if (presence == null) + return null; + + return presence.ControllingClient; + } + + private Scene GetClientScene(UUID agentId) + { + lock (m_Scenes) + { + foreach (Scene scene in m_Scenes) + { + ScenePresence presence = scene.GetScenePresence(agentId); + if (presence != null) + { + if (!presence.IsChildAgent) + return scene; + } + } + } + return null; + } + + private ScenePresence GetClientPresence(UUID agentId) + { + lock (m_Scenes) + { + foreach (Scene scene in m_Scenes) + { + ScenePresence presence = scene.GetScenePresence(agentId); + if (presence != null) + { + if (!presence.IsChildAgent) + return presence; + } + } + } + return null; + } + + private void OnIncomingInstantMessage(GridInstantMessage msg) + { + if (msg.dialog == (uint)211) + { + IClientAPI client = FindClientObject(new UUID(msg.toAgentID)); + if (client == null) + return; + + DoCallingCardOffer(client, new UUID(msg.fromAgentID)); + } + } + } +}