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. using PermissionMask = OpenSim.Framework.PermissionMask;
  39. namespace OpenSim.Region.CoreModules.Avatar.Friends
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XCallingCard")]
  42. public class CallingCardModule : ISharedRegionModule, ICallingCardModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. protected List<Scene> m_Scenes = new List<Scene>();
  46. protected bool m_Enabled = true;
  47. public void Initialise(IConfigSource source)
  48. {
  49. IConfig ccConfig = source.Configs["XCallingCard"];
  50. if (ccConfig != null)
  51. m_Enabled = ccConfig.GetBoolean("Enabled", true);
  52. }
  53. public void AddRegion(Scene scene)
  54. {
  55. if (!m_Enabled)
  56. return;
  57. m_Scenes.Add(scene);
  58. scene.RegisterModuleInterface<ICallingCardModule>(this);
  59. }
  60. public void RemoveRegion(Scene scene)
  61. {
  62. if (!m_Enabled)
  63. return;
  64. m_Scenes.Remove(scene);
  65. scene.EventManager.OnNewClient -= OnNewClient;
  66. scene.EventManager.OnIncomingInstantMessage +=
  67. OnIncomingInstantMessage;
  68. scene.UnregisterModuleInterface<ICallingCardModule>(this);
  69. }
  70. public void RegionLoaded(Scene scene)
  71. {
  72. if (!m_Enabled)
  73. return;
  74. scene.EventManager.OnNewClient += OnNewClient;
  75. }
  76. public void PostInitialise()
  77. {
  78. }
  79. public void Close()
  80. {
  81. }
  82. public Type ReplaceableInterface
  83. {
  84. get { return null; }
  85. }
  86. public string Name
  87. {
  88. get { return "XCallingCardModule"; }
  89. }
  90. private void OnNewClient(IClientAPI client)
  91. {
  92. client.OnOfferCallingCard += OnOfferCallingCard;
  93. client.OnAcceptCallingCard += OnAcceptCallingCard;
  94. client.OnDeclineCallingCard += OnDeclineCallingCard;
  95. }
  96. private void OnOfferCallingCard(IClientAPI client, UUID destID, UUID transactionID)
  97. {
  98. ScenePresence sp = GetClientPresence(client.AgentId);
  99. if (sp != null)
  100. {
  101. // If we're in god mode, we reverse the meaning. Offer
  102. // calling card becomes "Take a calling card" for that
  103. // person, no matter if they agree or not.
  104. if (sp.IsViewerUIGod)
  105. {
  106. CreateCallingCard(client.AgentId, destID, UUID.Zero, true);
  107. return;
  108. }
  109. }
  110. IClientAPI dest = FindClientObject(destID);
  111. if (dest != null)
  112. {
  113. DoCallingCardOffer(dest, client.AgentId);
  114. return;
  115. }
  116. IMessageTransferModule transferModule =
  117. m_Scenes[0].RequestModuleInterface<IMessageTransferModule>();
  118. if (transferModule != null)
  119. {
  120. transferModule.SendInstantMessage(new GridInstantMessage(
  121. client.Scene, client.AgentId,
  122. client.FirstName+" "+client.LastName,
  123. destID, (byte)211, false,
  124. String.Empty,
  125. transactionID, false, new Vector3(), new byte[0], true),
  126. delegate(bool success) {} );
  127. }
  128. }
  129. private void DoCallingCardOffer(IClientAPI dest, UUID from)
  130. {
  131. UUID itemID = CreateCallingCard(dest.AgentId, from, UUID.Zero, false);
  132. dest.SendOfferCallingCard(from, itemID);
  133. }
  134. // Create a calling card in the user's inventory. This is called
  135. // from direct calling card creation, when the offer is forwarded,
  136. // and from the friends module when the friend is confirmed.
  137. // Because of the latter, it will send a bulk inventory update
  138. // if the receiving user is in the same simulator.
  139. public UUID CreateCallingCard(UUID userID, UUID creatorID, UUID folderID)
  140. {
  141. return CreateCallingCard(userID, creatorID, folderID, false);
  142. }
  143. private UUID CreateCallingCard(UUID userID, UUID creatorID, UUID folderID, bool isGod)
  144. {
  145. IUserAccountService userv = m_Scenes[0].UserAccountService;
  146. if (userv == null)
  147. return UUID.Zero;
  148. UserAccount info = userv.GetUserAccount(UUID.Zero, creatorID);
  149. if (info == null)
  150. return UUID.Zero;
  151. IInventoryService inv = m_Scenes[0].InventoryService;
  152. if (inv == null)
  153. return UUID.Zero;
  154. if (folderID == UUID.Zero)
  155. {
  156. InventoryFolderBase folder = inv.GetFolderForType(userID,
  157. FolderType.CallingCard);
  158. if (folder == null) // Nowhere to put it
  159. return UUID.Zero;
  160. folderID = folder.ID;
  161. }
  162. m_log.DebugFormat("[XCALLINGCARD]: Creating calling card for {0} in inventory of {1}", info.Name, userID);
  163. InventoryItemBase item = new InventoryItemBase();
  164. item.AssetID = UUID.Zero;
  165. item.AssetType = (int)AssetType.CallingCard;
  166. item.BasePermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  167. if (isGod)
  168. item.BasePermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer | PermissionMask.Move);
  169. item.EveryOnePermissions = (uint)PermissionMask.None;
  170. item.CurrentPermissions = item.BasePermissions;
  171. item.NextPermissions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  172. item.ID = UUID.Random();
  173. item.CreatorId = creatorID.ToString();
  174. item.Owner = userID;
  175. item.GroupID = UUID.Zero;
  176. item.GroupOwned = false;
  177. item.Folder = folderID;
  178. item.CreationDate = Util.UnixTimeSinceEpoch();
  179. item.InvType = (int)InventoryType.CallingCard;
  180. item.Flags = 0;
  181. item.Name = info.Name;
  182. item.Description = "";
  183. item.SalePrice = 10;
  184. item.SaleType = (byte)SaleType.Not;
  185. inv.AddItem(item);
  186. IClientAPI client = FindClientObject(userID);
  187. if (client != null)
  188. client.SendBulkUpdateInventory(item);
  189. return item.ID;
  190. }
  191. private void OnAcceptCallingCard(IClientAPI client, UUID transactionID, UUID folderID)
  192. {
  193. }
  194. private void OnDeclineCallingCard(IClientAPI client, UUID transactionID)
  195. {
  196. IInventoryService invService = m_Scenes[0].InventoryService;
  197. InventoryFolderBase trashFolder =
  198. invService.GetFolderForType(client.AgentId, FolderType.Trash);
  199. InventoryItemBase item = invService.GetItem(client.AgentId, transactionID);
  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. }