SampleMoneyModule.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Services.Interfaces;
  44. namespace OpenSim.Region.OptionalModules.World.MoneyModule
  45. {
  46. /// <summary>
  47. /// This is only the functionality required to make the functionality associated with money work
  48. /// (such as land transfers). There is no money code here! Use FORGE as an example for money code.
  49. /// Demo Economy/Money Module. This is a purposely crippled module!
  50. /// // To land transfer you need to add:
  51. /// -helperuri http://serveraddress:port/
  52. /// to the command line parameters you use to start up your client
  53. /// This commonly looks like -helperuri http://127.0.0.1:9000/
  54. ///
  55. /// </summary>
  56. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SampleMoneyModule")]
  57. public class SampleMoneyModule : IMoneyModule, ISharedRegionModule
  58. {
  59. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  60. /// <summary>
  61. /// Where Stipends come from and Fees go to.
  62. /// </summary>
  63. // private UUID EconomyBaseAccount = UUID.Zero;
  64. private Dictionary<string, XmlRpcMethod> m_rpcHandlers;
  65. private string m_localEconomyURL;
  66. private float EnergyEfficiency = 1f;
  67. // private ObjectPaid handerOnObjectPaid;
  68. private bool m_enabled = true;
  69. private bool m_sellEnabled = true;
  70. private IConfigSource m_gConfig;
  71. /// <summary>
  72. /// Region UUIDS indexed by AgentID
  73. /// </summary>
  74. /// <summary>
  75. /// Scenes by Region Handle
  76. /// </summary>
  77. private Dictionary<ulong, Scene> m_scenel = new Dictionary<ulong, Scene>();
  78. // private int m_stipend = 1000;
  79. private int ObjectCount = 0;
  80. private int PriceEnergyUnit = 0;
  81. private int PriceGroupCreate = -1;
  82. private int PriceObjectClaim = 0;
  83. private float PriceObjectRent = 0f;
  84. private float PriceObjectScaleFactor = 10f;
  85. private int PriceParcelClaim = 0;
  86. private float PriceParcelClaimFactor = 1f;
  87. private int PriceParcelRent = 0;
  88. private int PricePublicObjectDecay = 0;
  89. private int PricePublicObjectDelete = 0;
  90. private int PriceRentLight = 0;
  91. private int PriceUpload = 0;
  92. private int TeleportMinPrice = 0;
  93. private float TeleportPriceExponent = 2f;
  94. #region IMoneyModule Members
  95. #pragma warning disable 0067
  96. public event ObjectPaid OnObjectPaid;
  97. #pragma warning restore 0067
  98. public int UploadCharge
  99. {
  100. get { return 0; }
  101. }
  102. public int GroupCreationCharge
  103. {
  104. get { return 0; }
  105. }
  106. /// <summary>
  107. /// Called on startup so the module can be configured.
  108. /// </summary>
  109. /// <param name="config">Configuration source.</param>
  110. public void Initialise(IConfigSource config)
  111. {
  112. m_gConfig = config;
  113. ReadConfigAndPopulate();
  114. }
  115. public void AddRegion(Scene scene)
  116. {
  117. if (m_enabled)
  118. {
  119. scene.RegisterModuleInterface<IMoneyModule>(this);
  120. IHttpServer httpServer = MainServer.Instance;
  121. lock (m_scenel)
  122. {
  123. if (m_scenel.Count == 0)
  124. {
  125. m_localEconomyURL = scene.RegionInfo.ServerURI;
  126. m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
  127. m_rpcHandlers.Add("getCurrencyQuote", quote_func);
  128. m_rpcHandlers.Add("buyCurrency", buy_func);
  129. m_rpcHandlers.Add("preflightBuyLandPrep", preflightBuyLandPrep_func);
  130. m_rpcHandlers.Add("buyLandPrep", landBuy_func);
  131. // add php
  132. MainServer.Instance.AddSimpleStreamHandler(new SimpleStreamHandler("/currency.php", processPHP));
  133. MainServer.Instance.AddSimpleStreamHandler(new SimpleStreamHandler("/landtool.php", processPHP));
  134. }
  135. if (m_scenel.ContainsKey(scene.RegionInfo.RegionHandle))
  136. {
  137. m_scenel[scene.RegionInfo.RegionHandle] = scene;
  138. }
  139. else
  140. {
  141. m_scenel.Add(scene.RegionInfo.RegionHandle, scene);
  142. }
  143. }
  144. scene.EventManager.OnNewClient += OnNewClient;
  145. scene.EventManager.OnMoneyTransfer += MoneyTransferAction;
  146. scene.EventManager.OnClientClosed += ClientClosed;
  147. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  148. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  149. scene.EventManager.OnValidateLandBuy += ValidateLandBuy;
  150. scene.EventManager.OnLandBuy += processLandBuy;
  151. }
  152. }
  153. public void RemoveRegion(Scene scene)
  154. {
  155. }
  156. public void RegionLoaded(Scene scene)
  157. {
  158. if (!m_enabled)
  159. return;
  160. if(scene.SceneGridInfo!= null && !string.IsNullOrEmpty(scene.SceneGridInfo.EconomyURL))
  161. return;
  162. ISimulatorFeaturesModule fm = scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  163. if (fm != null && !string.IsNullOrWhiteSpace(m_localEconomyURL))
  164. {
  165. if(fm.TryGetOpenSimExtraFeature("currency-base-uri", out OSD tmp))
  166. return;
  167. fm.AddOpenSimExtraFeature("currency-base-uri", Util.AppendEndSlash(m_localEconomyURL));
  168. }
  169. }
  170. public void processPHP(IOSHttpRequest request, IOSHttpResponse response)
  171. {
  172. MainServer.Instance.HandleXmlRpcRequests((OSHttpRequest)request, (OSHttpResponse)response, m_rpcHandlers);
  173. }
  174. // Please do not refactor these to be just one method
  175. // Existing implementations need the distinction
  176. //
  177. public void ApplyCharge(UUID agentID, int amount, MoneyTransactionType type, string extraData)
  178. {
  179. }
  180. public void ApplyCharge(UUID agentID, int amount, MoneyTransactionType type)
  181. {
  182. }
  183. public void ApplyUploadCharge(UUID agentID, int amount, string text)
  184. {
  185. }
  186. public bool ObjectGiveMoney(UUID objectID, UUID fromID, UUID toID, int amount, UUID txn, out string result)
  187. {
  188. result = String.Empty;
  189. string description = String.Format("Object {0} pays {1}", resolveObjectName(objectID), resolveAgentName(toID));
  190. bool give_result = doMoneyTransfer(fromID, toID, amount, 2, description);
  191. BalanceUpdate(fromID, toID, give_result, description);
  192. return give_result;
  193. }
  194. public void PostInitialise()
  195. {
  196. }
  197. public void Close()
  198. {
  199. }
  200. public Type ReplaceableInterface
  201. {
  202. get { return typeof(IMoneyModule); }
  203. }
  204. public string Name
  205. {
  206. get { return "BetaGridLikeMoneyModule"; }
  207. }
  208. #endregion
  209. /// <summary>
  210. /// Parse Configuration
  211. /// </summary>
  212. private void ReadConfigAndPopulate()
  213. {
  214. // we are enabled by default
  215. IConfig startupConfig = m_gConfig.Configs["Startup"];
  216. if(startupConfig == null) // should not happen
  217. return;
  218. IConfig economyConfig = m_gConfig.Configs["Economy"];
  219. // economymodule may be at startup or Economy (legacy)
  220. string mmodule = startupConfig.GetString("economymodule","");
  221. if(string.IsNullOrEmpty(mmodule))
  222. {
  223. if(economyConfig != null)
  224. {
  225. mmodule = economyConfig.GetString("economymodule", "");
  226. if (String.IsNullOrEmpty(mmodule))
  227. mmodule = economyConfig.GetString("EconomyModule", "");
  228. }
  229. }
  230. if (!string.IsNullOrEmpty(mmodule) && mmodule != Name)
  231. {
  232. // some other money module selected
  233. m_enabled = false;
  234. return;
  235. }
  236. if(economyConfig == null)
  237. return;
  238. PriceEnergyUnit = economyConfig.GetInt("PriceEnergyUnit", 0);
  239. PriceObjectClaim = economyConfig.GetInt("PriceObjectClaim", 0);
  240. PricePublicObjectDecay = economyConfig.GetInt("PricePublicObjectDecay", 4);
  241. PricePublicObjectDelete = economyConfig.GetInt("PricePublicObjectDelete", 0);
  242. PriceParcelClaim = economyConfig.GetInt("PriceParcelClaim", 0);
  243. PriceParcelClaimFactor = economyConfig.GetFloat("PriceParcelClaimFactor", 1f);
  244. PriceUpload = economyConfig.GetInt("PriceUpload", 0);
  245. PriceRentLight = economyConfig.GetInt("PriceRentLight", 0);
  246. TeleportMinPrice = economyConfig.GetInt("TeleportMinPrice", 0);
  247. TeleportPriceExponent = economyConfig.GetFloat("TeleportPriceExponent", 2f);
  248. EnergyEfficiency = economyConfig.GetFloat("EnergyEfficiency", 1);
  249. PriceObjectRent = economyConfig.GetFloat("PriceObjectRent", 0);
  250. PriceObjectScaleFactor = economyConfig.GetFloat("PriceObjectScaleFactor", 10);
  251. PriceParcelRent = economyConfig.GetInt("PriceParcelRent", 0);
  252. PriceGroupCreate = economyConfig.GetInt("PriceGroupCreate", -1);
  253. m_sellEnabled = economyConfig.GetBoolean("SellEnabled", true);
  254. }
  255. private void GetClientFunds(IClientAPI client)
  256. {
  257. CheckExistAndRefreshFunds(client.AgentId);
  258. }
  259. /// <summary>
  260. /// New Client Event Handler
  261. /// </summary>
  262. /// <param name="client"></param>
  263. private void OnNewClient(IClientAPI client)
  264. {
  265. GetClientFunds(client);
  266. // Subscribe to Money messages
  267. client.OnEconomyDataRequest += EconomyDataRequestHandler;
  268. client.OnMoneyBalanceRequest += SendMoneyBalance;
  269. client.OnRequestPayPrice += requestPayPrice;
  270. client.OnObjectBuy += ObjectBuy;
  271. client.OnLogout += ClientLoggedOut;
  272. }
  273. /// <summary>
  274. /// Transfer money
  275. /// </summary>
  276. /// <param name="Sender"></param>
  277. /// <param name="Receiver"></param>
  278. /// <param name="amount"></param>
  279. /// <returns></returns>
  280. private bool doMoneyTransfer(UUID Sender, UUID Receiver, int amount, int transactiontype, string description)
  281. {
  282. return true;
  283. }
  284. /// <summary>
  285. /// Sends the the stored money balance to the client
  286. /// </summary>
  287. /// <param name="client"></param>
  288. /// <param name="agentID"></param>
  289. /// <param name="SessionID"></param>
  290. /// <param name="TransactionID"></param>
  291. public void SendMoneyBalance(IClientAPI client, UUID agentID, UUID SessionID, UUID TransactionID)
  292. {
  293. if (client.AgentId == agentID && client.SessionId == SessionID)
  294. {
  295. int returnfunds = 0;
  296. try
  297. {
  298. returnfunds = GetFundsForAgentID(agentID);
  299. }
  300. catch (Exception e)
  301. {
  302. client.SendAlertMessage(e.Message + " ");
  303. }
  304. client.SendMoneyBalance(TransactionID, true, new byte[0], returnfunds, 0, UUID.Zero, false, UUID.Zero, false, 0, String.Empty);
  305. }
  306. else
  307. {
  308. client.SendAlertMessage("Unable to send your money balance to you!");
  309. }
  310. }
  311. private SceneObjectPart findPrim(UUID objectID)
  312. {
  313. lock (m_scenel)
  314. {
  315. foreach (Scene s in m_scenel.Values)
  316. {
  317. SceneObjectPart part = s.GetSceneObjectPart(objectID);
  318. if (part != null)
  319. {
  320. return part;
  321. }
  322. }
  323. }
  324. return null;
  325. }
  326. private string resolveObjectName(UUID objectID)
  327. {
  328. SceneObjectPart part = findPrim(objectID);
  329. if (part != null)
  330. {
  331. return part.Name;
  332. }
  333. return String.Empty;
  334. }
  335. private string resolveAgentName(UUID agentID)
  336. {
  337. // try avatar username surname
  338. Scene scene = GetRandomScene();
  339. UserAccount account = scene.UserAccountService.GetUserAccount(scene.RegionInfo.ScopeID, agentID);
  340. if (account != null)
  341. {
  342. string avatarname = account.FirstName + " " + account.LastName;
  343. return avatarname;
  344. }
  345. else
  346. {
  347. m_log.ErrorFormat(
  348. "[MONEY]: Could not resolve user {0}",
  349. agentID);
  350. }
  351. return String.Empty;
  352. }
  353. private void BalanceUpdate(UUID senderID, UUID receiverID, bool transactionresult, string description)
  354. {
  355. IClientAPI sender = LocateClientObject(senderID);
  356. IClientAPI receiver = LocateClientObject(receiverID);
  357. if (senderID != receiverID)
  358. {
  359. if (sender != null)
  360. {
  361. sender.SendMoneyBalance(UUID.Random(), transactionresult, Utils.StringToBytes(description), GetFundsForAgentID(senderID), 0, UUID.Zero, false, UUID.Zero, false, 0, String.Empty);
  362. }
  363. if (receiver != null)
  364. {
  365. receiver.SendMoneyBalance(UUID.Random(), transactionresult, Utils.StringToBytes(description), GetFundsForAgentID(receiverID), 0, UUID.Zero, false, UUID.Zero, false, 0, String.Empty);
  366. }
  367. }
  368. }
  369. /// <summary>
  370. /// XMLRPC handler to send alert message and sound to client
  371. /// </summary>
  372. public XmlRpcResponse UserAlert(XmlRpcRequest request, IPEndPoint remoteClient)
  373. {
  374. XmlRpcResponse ret = new XmlRpcResponse();
  375. Hashtable retparam = new Hashtable();
  376. Hashtable requestData = (Hashtable) request.Params[0];
  377. UUID agentId;
  378. UUID soundId;
  379. UUID regionId;
  380. UUID.TryParse((string) requestData["agentId"], out agentId);
  381. UUID.TryParse((string) requestData["soundId"], out soundId);
  382. UUID.TryParse((string) requestData["regionId"], out regionId);
  383. string text = (string) requestData["text"];
  384. string secret = (string) requestData["secret"];
  385. Scene userScene = GetSceneByUUID(regionId);
  386. if (userScene != null)
  387. {
  388. if (userScene.RegionInfo.regionSecret == secret)
  389. {
  390. IClientAPI client = LocateClientObject(agentId);
  391. if (client != null)
  392. {
  393. if (soundId != UUID.Zero)
  394. client.SendPlayAttachedSound(soundId, UUID.Zero, UUID.Zero, 1.0f, 0);
  395. client.SendBlueBoxMessage(UUID.Zero, "", text);
  396. retparam.Add("success", true);
  397. }
  398. else
  399. {
  400. retparam.Add("success", false);
  401. }
  402. }
  403. else
  404. {
  405. retparam.Add("success", false);
  406. }
  407. }
  408. ret.Value = retparam;
  409. return ret;
  410. }
  411. # region Standalone box enablers only
  412. public XmlRpcResponse quote_func(XmlRpcRequest request, IPEndPoint remoteClient)
  413. {
  414. // UUID agentId = UUID.Zero;
  415. int amount = 0;
  416. try
  417. {
  418. Hashtable requestData = (Hashtable)request.Params[0];
  419. amount = (int)requestData["currencyBuy"];
  420. }
  421. catch{ }
  422. Hashtable currencyResponse = new Hashtable();
  423. currencyResponse.Add("estimatedCost", 0);
  424. //currencyResponse.Add("estimatedLocalCost", " 0 Euros");
  425. currencyResponse.Add("currencyBuy", amount);
  426. Hashtable quoteResponse = new Hashtable();
  427. quoteResponse.Add("success", true);
  428. quoteResponse.Add("currency", currencyResponse);
  429. quoteResponse.Add("confirm", "asdfad9fj39ma9fj");
  430. //quoteResponse.Add("success", false);
  431. //quoteResponse.Add("errorMessage", "There is currency");
  432. //quoteResponse.Add("errorURI", "http://opensimulator.org");
  433. XmlRpcResponse returnval = new XmlRpcResponse();
  434. returnval.Value = quoteResponse;
  435. return returnval;
  436. }
  437. public XmlRpcResponse buy_func(XmlRpcRequest request, IPEndPoint remoteClient)
  438. {
  439. // Hashtable requestData = (Hashtable) request.Params[0];
  440. // UUID agentId = UUID.Zero;
  441. // int amount = 0;
  442. XmlRpcResponse returnval = new XmlRpcResponse();
  443. Hashtable returnresp = new Hashtable();
  444. returnresp.Add("success", true);
  445. returnval.Value = returnresp;
  446. return returnval;
  447. }
  448. public XmlRpcResponse preflightBuyLandPrep_func(XmlRpcRequest request, IPEndPoint remoteClient)
  449. {
  450. XmlRpcResponse ret = new XmlRpcResponse();
  451. Hashtable retparam = new Hashtable();
  452. Hashtable membershiplevels = new Hashtable();
  453. ArrayList levels = new ArrayList();
  454. Hashtable level = new Hashtable();
  455. level.Add("id", "00000000-0000-0000-0000-000000000000");
  456. level.Add("description", "some level");
  457. levels.Add(level);
  458. //membershiplevels.Add("levels",levels);
  459. Hashtable landuse = new Hashtable();
  460. landuse.Add("upgrade", false);
  461. landuse.Add("action", "http://invaliddomaininvalid.com/");
  462. Hashtable currency = new Hashtable();
  463. currency.Add("estimatedCost", 0);
  464. Hashtable membership = new Hashtable();
  465. membershiplevels.Add("upgrade", false);
  466. membershiplevels.Add("action", "http://invaliddomaininvalid.com/");
  467. membershiplevels.Add("levels", membershiplevels);
  468. retparam.Add("success", true);
  469. retparam.Add("currency", currency);
  470. retparam.Add("membership", membership);
  471. retparam.Add("landuse", landuse);
  472. retparam.Add("confirm", "asdfajsdkfjasdkfjalsdfjasdf");
  473. ret.Value = retparam;
  474. return ret;
  475. }
  476. public XmlRpcResponse landBuy_func(XmlRpcRequest request, IPEndPoint remoteClient)
  477. {
  478. XmlRpcResponse ret = new XmlRpcResponse();
  479. Hashtable retparam = new Hashtable();
  480. // Hashtable requestData = (Hashtable) request.Params[0];
  481. // UUID agentId = UUID.Zero;
  482. // int amount = 0;
  483. retparam.Add("success", true);
  484. ret.Value = retparam;
  485. return ret;
  486. }
  487. #endregion
  488. #region local Fund Management
  489. /// <summary>
  490. /// Ensures that the agent accounting data is set up in this instance.
  491. /// </summary>
  492. /// <param name="agentID"></param>
  493. private void CheckExistAndRefreshFunds(UUID agentID)
  494. {
  495. }
  496. /// <summary>
  497. /// Gets the amount of Funds for an agent
  498. /// </summary>
  499. /// <param name="AgentID"></param>
  500. /// <returns></returns>
  501. private int GetFundsForAgentID(UUID AgentID)
  502. {
  503. int returnfunds = 0;
  504. return returnfunds;
  505. }
  506. // private void SetLocalFundsForAgentID(UUID AgentID, int amount)
  507. // {
  508. // }
  509. #endregion
  510. #region Utility Helpers
  511. /// <summary>
  512. /// Locates a IClientAPI for the client specified
  513. /// </summary>
  514. /// <param name="AgentID"></param>
  515. /// <returns></returns>
  516. private IClientAPI LocateClientObject(UUID AgentID)
  517. {
  518. ScenePresence tPresence = null;
  519. IClientAPI rclient = null;
  520. lock (m_scenel)
  521. {
  522. foreach (Scene _scene in m_scenel.Values)
  523. {
  524. tPresence = _scene.GetScenePresence(AgentID);
  525. if (tPresence != null)
  526. {
  527. if (!tPresence.IsChildAgent)
  528. {
  529. rclient = tPresence.ControllingClient;
  530. }
  531. }
  532. if (rclient != null)
  533. {
  534. return rclient;
  535. }
  536. }
  537. }
  538. return null;
  539. }
  540. private Scene LocateSceneClientIn(UUID AgentId)
  541. {
  542. lock (m_scenel)
  543. {
  544. foreach (Scene _scene in m_scenel.Values)
  545. {
  546. ScenePresence tPresence = _scene.GetScenePresence(AgentId);
  547. if (tPresence != null)
  548. {
  549. if (!tPresence.IsChildAgent)
  550. {
  551. return _scene;
  552. }
  553. }
  554. }
  555. }
  556. return null;
  557. }
  558. /// <summary>
  559. /// Utility function Gets a Random scene in the instance. For when which scene exactly you're doing something with doesn't matter
  560. /// </summary>
  561. /// <returns></returns>
  562. public Scene GetRandomScene()
  563. {
  564. lock (m_scenel)
  565. {
  566. foreach (Scene rs in m_scenel.Values)
  567. return rs;
  568. }
  569. return null;
  570. }
  571. /// <summary>
  572. /// Utility function to get a Scene by RegionID in a module
  573. /// </summary>
  574. /// <param name="RegionID"></param>
  575. /// <returns></returns>
  576. public Scene GetSceneByUUID(UUID RegionID)
  577. {
  578. lock (m_scenel)
  579. {
  580. foreach (Scene rs in m_scenel.Values)
  581. {
  582. if (rs.RegionInfo.originRegionID == RegionID)
  583. {
  584. return rs;
  585. }
  586. }
  587. }
  588. return null;
  589. }
  590. #endregion
  591. #region event Handlers
  592. public void requestPayPrice(IClientAPI client, UUID objectID)
  593. {
  594. Scene scene = LocateSceneClientIn(client.AgentId);
  595. if (scene == null)
  596. return;
  597. SceneObjectPart task = scene.GetSceneObjectPart(objectID);
  598. if (task == null)
  599. return;
  600. SceneObjectGroup group = task.ParentGroup;
  601. SceneObjectPart root = group.RootPart;
  602. client.SendPayPrice(objectID, root.PayPrice);
  603. }
  604. /// <summary>
  605. /// When the client closes the connection we remove their accounting
  606. /// info from memory to free up resources.
  607. /// </summary>
  608. /// <param name="AgentID">UUID of agent</param>
  609. /// <param name="scene">Scene the agent was connected to.</param>
  610. /// <see cref="OpenSim.Region.Framework.Scenes.EventManager.ClientClosed"/>
  611. public void ClientClosed(UUID AgentID, Scene scene)
  612. {
  613. }
  614. /// <summary>
  615. /// Event called Economy Data Request handler.
  616. /// </summary>
  617. /// <param name="agentId"></param>
  618. public void EconomyDataRequestHandler(IClientAPI user)
  619. {
  620. Scene s = (Scene)user.Scene;
  621. user.SendEconomyData(EnergyEfficiency, s.RegionInfo.ObjectCapacity, ObjectCount, PriceEnergyUnit, PriceGroupCreate,
  622. PriceObjectClaim, PriceObjectRent, PriceObjectScaleFactor, PriceParcelClaim, PriceParcelClaimFactor,
  623. PriceParcelRent, PricePublicObjectDecay, PricePublicObjectDelete, PriceRentLight, PriceUpload,
  624. TeleportMinPrice, TeleportPriceExponent);
  625. }
  626. private void ValidateLandBuy(Object osender, EventManager.LandBuyArgs e)
  627. {
  628. lock (e)
  629. {
  630. e.economyValidated = true;
  631. }
  632. }
  633. private void processLandBuy(Object osender, EventManager.LandBuyArgs e)
  634. {
  635. }
  636. /// <summary>
  637. /// THis method gets called when someone pays someone else as a gift.
  638. /// </summary>
  639. /// <param name="osender"></param>
  640. /// <param name="e"></param>
  641. private void MoneyTransferAction(Object osender, EventManager.MoneyTransferArgs e)
  642. {
  643. }
  644. /// <summary>
  645. /// Event Handler for when a root agent becomes a child agent
  646. /// </summary>
  647. /// <param name="avatar"></param>
  648. private void MakeChildAgent(ScenePresence avatar)
  649. {
  650. }
  651. /// <summary>
  652. /// Event Handler for when the client logs out.
  653. /// </summary>
  654. /// <param name="AgentId"></param>
  655. private void ClientLoggedOut(IClientAPI client)
  656. {
  657. }
  658. /// <summary>
  659. /// Call this when the client disconnects.
  660. /// </summary>
  661. /// <param name="client"></param>
  662. public void ClientClosed(IClientAPI client)
  663. {
  664. ClientClosed(client.AgentId, null);
  665. }
  666. /// <summary>
  667. /// Event Handler for when an Avatar enters one of the parcels in the simulator.
  668. /// </summary>
  669. /// <param name="avatar"></param>
  670. /// <param name="localLandID"></param>
  671. /// <param name="regionID"></param>
  672. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
  673. {
  674. //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
  675. }
  676. public int GetBalance(UUID agentID)
  677. {
  678. return 0;
  679. }
  680. // Please do not refactor these to be just one method
  681. // Existing implementations need the distinction
  682. //
  683. public bool UploadCovered(UUID agentID, int amount)
  684. {
  685. return true;
  686. }
  687. public bool AmountCovered(UUID agentID, int amount)
  688. {
  689. return true;
  690. }
  691. #endregion
  692. public void ObjectBuy(IClientAPI remoteClient, UUID agentID,
  693. UUID sessionID, UUID groupID, UUID categoryID,
  694. uint localID, byte saleType, int salePrice)
  695. {
  696. if (!m_sellEnabled)
  697. {
  698. remoteClient.SendBlueBoxMessage(UUID.Zero, "", "Buying is not implemented in this version");
  699. return;
  700. }
  701. if (salePrice != 0)
  702. {
  703. remoteClient.SendBlueBoxMessage(UUID.Zero, "", "Buying anything for a price other than zero is not implemented");
  704. return;
  705. }
  706. Scene s = LocateSceneClientIn(remoteClient.AgentId);
  707. // Implmenting base sale data checking here so the default OpenSimulator implementation isn't useless
  708. // combined with other implementations. We're actually validating that the client is sending the data
  709. // that it should. In theory, the client should already know what to send here because it'll see it when it
  710. // gets the object data. If the data sent by the client doesn't match the object, the viewer probably has an
  711. // old idea of what the object properties are. Viewer developer Hazim informed us that the base module
  712. // didn't check the client sent data against the object do any. Since the base modules are the
  713. // 'crowning glory' examples of good practice..
  714. // Validate that the object exists in the scene the user is in
  715. SceneObjectPart part = s.GetSceneObjectPart(localID);
  716. if(!part.IsRoot) // silent ignore non root parts
  717. return;
  718. if (part == null || part.ParentGroup == null || part.ParentGroup.IsDeleted)
  719. {
  720. remoteClient.SendAgentAlertMessage("Unable to buy now. The object was not found.", false);
  721. return;
  722. }
  723. if (part.ObjectSaleType == (byte)SaleType.Not)
  724. {
  725. string e = string.Format("Object {0} is not for sale", part.Name);
  726. remoteClient.SendAgentAlertMessage(e, false);
  727. return;
  728. }
  729. // Validate that the client sent the price that the object is being sold for
  730. if (part.SalePrice != salePrice)
  731. {
  732. string e = string.Format("Object {0} price does not match selected price", part.Name);
  733. remoteClient.SendAgentAlertMessage(e, false);
  734. return;
  735. }
  736. // Validate that the client sent the proper sale type the object has set
  737. if (part.ObjectSaleType != saleType)
  738. {
  739. string e = string.Format("Object {0} sell type does not match selected type", part.Name);
  740. remoteClient.SendAgentAlertMessage(e, false);
  741. return;
  742. }
  743. IBuySellModule module = s.RequestModuleInterface<IBuySellModule>();
  744. if (module != null)
  745. module.BuyObject(remoteClient, categoryID, localID, saleType, salePrice);
  746. }
  747. public void MoveMoney(UUID fromUser, UUID toUser, int amount, string text)
  748. {
  749. }
  750. public bool MoveMoney(UUID fromUser, UUID toUser, int amount, MoneyTransactionType type, string text)
  751. {
  752. return true;
  753. }
  754. }
  755. public enum TransactionType : int
  756. {
  757. SystemGenerated = 0,
  758. RegionMoneyRequest = 1,
  759. Gift = 2,
  760. Purchase = 3
  761. }
  762. }