CallingCardModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. using Mono.Addins;
  38. namespace OpenSim.Region.CoreModules.Avatar.Friends
  39. {
  40. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XCallingCard")]
  41. public class CallingCardModule : ISharedRegionModule, ICallingCardModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. protected List<Scene> m_Scenes = new List<Scene>();
  45. protected bool m_Enabled = true;
  46. public void Initialise(IConfigSource source)
  47. {
  48. IConfig ccConfig = source.Configs["XCallingCard"];
  49. if (ccConfig != null)
  50. m_Enabled = ccConfig.GetBoolean("Enabled", true);
  51. }
  52. public void AddRegion(Scene scene)
  53. {
  54. if (!m_Enabled)
  55. return;
  56. m_Scenes.Add(scene);
  57. scene.RegisterModuleInterface<ICallingCardModule>(this);
  58. }
  59. public void RemoveRegion(Scene scene)
  60. {
  61. if (!m_Enabled)
  62. return;
  63. m_Scenes.Remove(scene);
  64. scene.EventManager.OnNewClient -= OnNewClient;
  65. scene.EventManager.OnIncomingInstantMessage +=
  66. OnIncomingInstantMessage;
  67. scene.UnregisterModuleInterface<ICallingCardModule>(this);
  68. }
  69. public void RegionLoaded(Scene scene)
  70. {
  71. if (!m_Enabled)
  72. return;
  73. scene.EventManager.OnNewClient += OnNewClient;
  74. }
  75. public void PostInitialise()
  76. {
  77. }
  78. public void Close()
  79. {
  80. }
  81. public Type ReplaceableInterface
  82. {
  83. get { return null; }
  84. }
  85. public string Name
  86. {
  87. get { return "XCallingCardModule"; }
  88. }
  89. private void OnNewClient(IClientAPI client)
  90. {
  91. client.OnOfferCallingCard += OnOfferCallingCard;
  92. client.OnAcceptCallingCard += OnAcceptCallingCard;
  93. client.OnDeclineCallingCard += OnDeclineCallingCard;
  94. }
  95. private void OnOfferCallingCard(IClientAPI client, UUID destID, UUID transactionID)
  96. {
  97. ScenePresence sp = GetClientPresence(client.AgentId);
  98. if (sp != null)
  99. {
  100. // If we're in god mode, we reverse the meaning. Offer
  101. // calling card becomes "Take a calling card" for that
  102. // person, no matter if they agree or not.
  103. if (sp.GodLevel >= 200)
  104. {
  105. CreateCallingCard(client.AgentId, destID, UUID.Zero, true);
  106. return;
  107. }
  108. }
  109. IClientAPI dest = FindClientObject(destID);
  110. if (dest != null)
  111. {
  112. DoCallingCardOffer(dest, client.AgentId);
  113. return;
  114. }
  115. IMessageTransferModule transferModule =
  116. m_Scenes[0].RequestModuleInterface<IMessageTransferModule>();
  117. if (transferModule != null)
  118. {
  119. transferModule.SendInstantMessage(new GridInstantMessage(
  120. client.Scene, client.AgentId,
  121. client.FirstName+" "+client.LastName,
  122. destID, (byte)211, false,
  123. String.Empty,
  124. transactionID, false, new Vector3(), new byte[0], true),
  125. delegate(bool success) {} );
  126. }
  127. }
  128. private void DoCallingCardOffer(IClientAPI dest, UUID from)
  129. {
  130. UUID itemID = CreateCallingCard(dest.AgentId, from, UUID.Zero, false);
  131. dest.SendOfferCallingCard(from, itemID);
  132. }
  133. // Create a calling card in the user's inventory. This is called
  134. // from direct calling card creation, when the offer is forwarded,
  135. // and from the friends module when the friend is confirmed.
  136. // Because of the latter, it will send a bulk inventory update
  137. // if the receiving user is in the same simulator.
  138. public UUID CreateCallingCard(UUID userID, UUID creatorID, UUID folderID)
  139. {
  140. return CreateCallingCard(userID, creatorID, folderID, false);
  141. }
  142. private UUID CreateCallingCard(UUID userID, UUID creatorID, UUID folderID, bool isGod)
  143. {
  144. IUserAccountService userv = m_Scenes[0].UserAccountService;
  145. if (userv == null)
  146. return UUID.Zero;
  147. UserAccount info = userv.GetUserAccount(UUID.Zero, creatorID);
  148. if (info == null)
  149. return UUID.Zero;
  150. IInventoryService inv = m_Scenes[0].InventoryService;
  151. if (inv == null)
  152. return UUID.Zero;
  153. if (folderID == UUID.Zero)
  154. {
  155. InventoryFolderBase folder = inv.GetFolderForType(userID,
  156. AssetType.CallingCard);
  157. if (folder == null) // Nowhere to put it
  158. return UUID.Zero;
  159. folderID = folder.ID;
  160. }
  161. m_log.DebugFormat("[XCALLINGCARD]: Creating calling card for {0} in inventory of {1}", info.Name, userID);
  162. InventoryItemBase item = new InventoryItemBase();
  163. item.AssetID = UUID.Zero;
  164. item.AssetType = (int)AssetType.CallingCard;
  165. item.BasePermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  166. if (isGod)
  167. item.BasePermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer | PermissionMask.Move);
  168. item.EveryOnePermissions = (uint)PermissionMask.None;
  169. item.CurrentPermissions = item.BasePermissions;
  170. item.NextPermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  171. item.ID = UUID.Random();
  172. item.CreatorId = creatorID.ToString();
  173. item.Owner = userID;
  174. item.GroupID = UUID.Zero;
  175. item.GroupOwned = false;
  176. item.Folder = folderID;
  177. item.CreationDate = Util.UnixTimeSinceEpoch();
  178. item.InvType = (int)InventoryType.CallingCard;
  179. item.Flags = 0;
  180. item.Name = info.Name;
  181. item.Description = "";
  182. item.SalePrice = 10;
  183. item.SaleType = (byte)SaleType.Not;
  184. inv.AddItem(item);
  185. IClientAPI client = FindClientObject(userID);
  186. if (client != null)
  187. client.SendBulkUpdateInventory(item);
  188. return item.ID;
  189. }
  190. private void OnAcceptCallingCard(IClientAPI client, UUID transactionID, UUID folderID)
  191. {
  192. }
  193. private void OnDeclineCallingCard(IClientAPI client, UUID transactionID)
  194. {
  195. IInventoryService invService = m_Scenes[0].InventoryService;
  196. InventoryFolderBase trashFolder =
  197. invService.GetFolderForType(client.AgentId, AssetType.TrashFolder);
  198. InventoryItemBase item = new InventoryItemBase(transactionID, client.AgentId);
  199. item = invService.GetItem(item);
  200. if (item != null && trashFolder != null)
  201. {
  202. item.Folder = trashFolder.ID;
  203. List<UUID> uuids = new List<UUID>();
  204. uuids.Add(item.ID);
  205. invService.DeleteItems(item.Owner, uuids);
  206. m_Scenes[0].AddInventoryItem(client, item);
  207. }
  208. }
  209. public IClientAPI FindClientObject(UUID agentID)
  210. {
  211. Scene scene = GetClientScene(agentID);
  212. if (scene == null)
  213. return null;
  214. ScenePresence presence = scene.GetScenePresence(agentID);
  215. if (presence == null)
  216. return null;
  217. return presence.ControllingClient;
  218. }
  219. private Scene GetClientScene(UUID agentId)
  220. {
  221. lock (m_Scenes)
  222. {
  223. foreach (Scene scene in m_Scenes)
  224. {
  225. ScenePresence presence = scene.GetScenePresence(agentId);
  226. if (presence != null)
  227. {
  228. if (!presence.IsChildAgent)
  229. return scene;
  230. }
  231. }
  232. }
  233. return null;
  234. }
  235. private ScenePresence GetClientPresence(UUID agentId)
  236. {
  237. lock (m_Scenes)
  238. {
  239. foreach (Scene scene in m_Scenes)
  240. {
  241. ScenePresence presence = scene.GetScenePresence(agentId);
  242. if (presence != null)
  243. {
  244. if (!presence.IsChildAgent)
  245. return presence;
  246. }
  247. }
  248. }
  249. return null;
  250. }
  251. private void OnIncomingInstantMessage(GridInstantMessage msg)
  252. {
  253. if (msg.dialog == (uint)211)
  254. {
  255. IClientAPI client = FindClientObject(new UUID(msg.toAgentID));
  256. if (client == null)
  257. return;
  258. DoCallingCardOffer(client, new UUID(msg.fromAgentID));
  259. }
  260. }
  261. }
  262. }