SampleMoneyModule.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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 OpenSim 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.Net.Sockets;
  32. using System.Reflection;
  33. using System.Xml;
  34. using libsecondlife;
  35. using log4net;
  36. using Nini.Config;
  37. using Nwc.XmlRpc;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Environment.Interfaces;
  40. using OpenSim.Region.Environment.Scenes;
  41. namespace OpenSim.Region.Environment.Modules.Avatar.Currency.SampleMoney
  42. {
  43. /// <summary>
  44. /// Demo Economy/Money Module. This is not a production quality money/economy module!
  45. /// This is a demo for you to use when making one that works for you.
  46. /// // To use the following you need to add:
  47. /// -helperuri <ADDRESS TO HERE OR grid MONEY SERVER>
  48. /// to the command line parameters you use to start up your client
  49. /// This commonly looks like -helperuri http://127.0.0.1:9000/
  50. ///
  51. /// Centralized grid structure example using OpenSimWi Redux revision 9+
  52. /// svn co https://opensimwiredux.svn.sourceforge.net/svnroot/opensimwiredux
  53. /// </summary>
  54. public class SampleMoneyModule : IMoneyModule
  55. {
  56. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. /// <summary>
  58. /// Where Stipends come from and Fees go to.
  59. /// </summary>
  60. private LLUUID EconomyBaseAccount = LLUUID.Zero;
  61. private float EnergyEfficiency = 0f;
  62. private bool gridmode = false;
  63. private ObjectPaid handerOnObjectPaid;
  64. private bool m_enabled = true;
  65. private IConfigSource m_gConfig;
  66. private bool m_keepMoneyAcrossLogins = true;
  67. private Dictionary<LLUUID, int> m_KnownClientFunds = new Dictionary<LLUUID, int>();
  68. private string m_LandAddress = String.Empty;
  69. private int m_minFundsBeforeRefresh = 100;
  70. private string m_MoneyAddress = String.Empty;
  71. /// <summary>
  72. /// Region UUIDS indexed by AgentID
  73. /// </summary>
  74. private Dictionary<LLUUID, LLUUID> m_rootAgents = new Dictionary<LLUUID, LLUUID>();
  75. /// <summary>
  76. /// Scenes by Region Handle
  77. /// </summary>
  78. private Dictionary<ulong, Scene> m_scenel = new Dictionary<ulong, Scene>();
  79. private int m_stipend = 1000;
  80. private int ObjectCapacity = 45000;
  81. private int ObjectCount = 0;
  82. private int PriceEnergyUnit = 0;
  83. private int PriceGroupCreate = 0;
  84. private int PriceObjectClaim = 0;
  85. private float PriceObjectRent = 0f;
  86. private float PriceObjectScaleFactor = 0f;
  87. private int PriceParcelClaim = 0;
  88. private float PriceParcelClaimFactor = 0f;
  89. private int PriceParcelRent = 0;
  90. private int PricePublicObjectDecay = 0;
  91. private int PricePublicObjectDelete = 0;
  92. private int PriceRentLight = 0;
  93. private int PriceUpload = 0;
  94. private int TeleportMinPrice = 0;
  95. private float TeleportPriceExponent = 0f;
  96. private int UserLevelPaysFees = 2;
  97. private Scene XMLRPCHandler;
  98. #region IMoneyModule Members
  99. public event ObjectPaid OnObjectPaid;
  100. /// <summary>
  101. /// Startup
  102. /// </summary>
  103. /// <param name="scene"></param>
  104. /// <param name="config"></param>
  105. public void Initialise(Scene scene, IConfigSource config)
  106. {
  107. m_gConfig = config;
  108. IConfig startupConfig = m_gConfig.Configs["Startup"];
  109. IConfig economyConfig = m_gConfig.Configs["Economy"];
  110. ReadConfigAndPopulate(scene, startupConfig, "Startup");
  111. ReadConfigAndPopulate(scene, economyConfig, "Economy");
  112. if (m_enabled)
  113. {
  114. scene.RegisterModuleInterface<IMoneyModule>(this);
  115. lock (m_scenel)
  116. {
  117. if (m_scenel.Count == 0)
  118. {
  119. XMLRPCHandler = scene;
  120. // To use the following you need to add:
  121. // -helperuri <ADDRESS TO HERE OR grid MONEY SERVER>
  122. // to the command line parameters you use to start up your client
  123. // This commonly looks like -helperuri http://127.0.0.1:9000/
  124. if (m_MoneyAddress.Length > 0)
  125. {
  126. // Centralized grid structure using OpenSimWi Redux revision 9+
  127. // https://opensimwiredux.svn.sourceforge.net/svnroot/opensimwiredux
  128. scene.AddXmlRPCHandler("balanceUpdateRequest", GridMoneyUpdate);
  129. scene.AddXmlRPCHandler("userAlert", UserAlert);
  130. }
  131. else
  132. {
  133. // Local Server.. enables functionality only.
  134. scene.AddXmlRPCHandler("getCurrencyQuote", quote_func);
  135. scene.AddXmlRPCHandler("buyCurrency", buy_func);
  136. scene.AddXmlRPCHandler("preflightBuyLandPrep", preflightBuyLandPrep_func);
  137. scene.AddXmlRPCHandler("buyLandPrep", landBuy_func);
  138. }
  139. }
  140. if (m_scenel.ContainsKey(scene.RegionInfo.RegionHandle))
  141. {
  142. m_scenel[scene.RegionInfo.RegionHandle] = scene;
  143. }
  144. else
  145. {
  146. m_scenel.Add(scene.RegionInfo.RegionHandle, scene);
  147. }
  148. }
  149. scene.EventManager.OnNewClient += OnNewClient;
  150. scene.EventManager.OnMoneyTransfer += MoneyTransferAction;
  151. scene.EventManager.OnClientClosed += ClientClosed;
  152. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  153. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  154. scene.EventManager.OnClientClosed += ClientLoggedOut;
  155. scene.EventManager.OnValidateLandBuy += ValidateLandBuy;
  156. scene.EventManager.OnLandBuy += processLandBuy;
  157. scene.EventManager.OnAvatarKilled += KillAvatar;
  158. }
  159. }
  160. public void ApplyUploadCharge(LLUUID agentID)
  161. {
  162. }
  163. public bool ObjectGiveMoney(LLUUID objectID, LLUUID fromID, LLUUID toID, int amount)
  164. {
  165. string description = String.Format("Object {0} pays {1}", resolveObjectName(objectID), resolveAgentName(toID));
  166. bool give_result = doMoneyTransfer(fromID, toID, amount, 2, description);
  167. if (m_MoneyAddress.Length == 0)
  168. BalanceUpdate(fromID, toID, give_result, description);
  169. return give_result;
  170. }
  171. public void PostInitialise()
  172. {
  173. }
  174. public void Close()
  175. {
  176. }
  177. public string Name
  178. {
  179. get { return "BetaGridLikeMoneyModule"; }
  180. }
  181. public bool IsSharedModule
  182. {
  183. get { return true; }
  184. }
  185. #endregion
  186. /// <summary>
  187. /// Parse Configuration
  188. /// </summary>
  189. /// <param name="scene"></param>
  190. /// <param name="startupConfig"></param>
  191. /// <param name="config"></param>
  192. private void ReadConfigAndPopulate(Scene scene, IConfig startupConfig, string config)
  193. {
  194. if (config == "Startup" && startupConfig != null)
  195. {
  196. gridmode = startupConfig.GetBoolean("gridmode", false);
  197. m_enabled = (startupConfig.GetString("economymodule", "BetaGridLikeMoneyModule") == "BetaGridLikeMoneyModule");
  198. }
  199. if (config == "Economy" && startupConfig != null)
  200. {
  201. ObjectCapacity = startupConfig.GetInt("ObjectCapacity", 45000);
  202. PriceEnergyUnit = startupConfig.GetInt("PriceEnergyUnit", 100);
  203. PriceObjectClaim = startupConfig.GetInt("PriceObjectClaim", 10);
  204. PricePublicObjectDecay = startupConfig.GetInt("PricePublicObjectDecay", 4);
  205. PricePublicObjectDelete = startupConfig.GetInt("PricePublicObjectDelete", 4);
  206. PriceParcelClaim = startupConfig.GetInt("PriceParcelClaim", 1);
  207. PriceParcelClaimFactor = startupConfig.GetFloat("PriceParcelClaimFactor", 1f);
  208. PriceUpload = startupConfig.GetInt("PriceUpload", 0);
  209. PriceRentLight = startupConfig.GetInt("PriceRentLight", 5);
  210. TeleportMinPrice = startupConfig.GetInt("TeleportMinPrice", 2);
  211. TeleportPriceExponent = startupConfig.GetFloat("TeleportPriceExponent", 2f);
  212. EnergyEfficiency = startupConfig.GetFloat("EnergyEfficiency", 1);
  213. PriceObjectRent = startupConfig.GetFloat("PriceObjectRent", 1);
  214. PriceObjectScaleFactor = startupConfig.GetFloat("PriceObjectScaleFactor", 10);
  215. PriceParcelRent = startupConfig.GetInt("PriceParcelRent", 1);
  216. PriceGroupCreate = startupConfig.GetInt("PriceGroupCreate", -1);
  217. string EBA = startupConfig.GetString("EconomyBaseAccount", LLUUID.Zero.ToString());
  218. Helpers.TryParse(EBA, out EconomyBaseAccount);
  219. UserLevelPaysFees = startupConfig.GetInt("UserLevelPaysFees", -1);
  220. m_stipend = startupConfig.GetInt("UserStipend", 500);
  221. m_minFundsBeforeRefresh = startupConfig.GetInt("IssueStipendWhenClientIsBelowAmount", 10);
  222. m_keepMoneyAcrossLogins = startupConfig.GetBoolean("KeepMoneyAcrossLogins", true);
  223. m_MoneyAddress = startupConfig.GetString("CurrencyServer", String.Empty);
  224. m_LandAddress = startupConfig.GetString("LandServer", String.Empty);
  225. }
  226. // Send ObjectCapacity to Scene.. Which sends it to the SimStatsReporter.
  227. scene.SetObjectCapacity(ObjectCapacity);
  228. }
  229. /// <summary>
  230. /// New Client Event Handler
  231. /// </summary>
  232. /// <param name="client"></param>
  233. private void OnNewClient(IClientAPI client)
  234. {
  235. // Here we check if we're in grid mode
  236. // I imagine that the 'check balance'
  237. // function for the client should be here or shortly after
  238. if (gridmode)
  239. {
  240. if (m_MoneyAddress.Length == 0)
  241. {
  242. CheckExistAndRefreshFunds(client.AgentId);
  243. }
  244. else
  245. {
  246. bool childYN = true;
  247. ScenePresence agent = null;
  248. //client.SecureSessionId;
  249. Scene s = LocateSceneClientIn(client.AgentId);
  250. if (s != null)
  251. {
  252. agent = s.GetScenePresence(client.AgentId);
  253. if (agent != null)
  254. childYN = agent.IsChildAgent;
  255. }
  256. if (s != null && agent != null && childYN == false)
  257. {
  258. //s.RegionInfo.RegionHandle;
  259. LLUUID agentID = LLUUID.Zero;
  260. int funds = 0;
  261. Hashtable hbinfo =
  262. GetBalanceForUserFromMoneyServer(client.AgentId, client.SecureSessionId, s.RegionInfo.originRegionID.ToString(),
  263. s.RegionInfo.regionSecret);
  264. if ((bool) hbinfo["success"] == true)
  265. {
  266. Helpers.TryParse((string) hbinfo["agentId"], out agentID);
  267. try
  268. {
  269. funds = (Int32) hbinfo["funds"];
  270. }
  271. catch (ArgumentException)
  272. {
  273. }
  274. catch (FormatException)
  275. {
  276. }
  277. catch (OverflowException)
  278. {
  279. m_log.ErrorFormat("[MONEY]: While getting the Currency for user {0}, the return funds overflowed.", agentID);
  280. client.SendAlertMessage("Unable to get your money balance, money operations will be unavailable");
  281. }
  282. catch (InvalidCastException)
  283. {
  284. funds = 0;
  285. }
  286. m_KnownClientFunds[agentID] = funds;
  287. }
  288. else
  289. {
  290. m_log.WarnFormat("[MONEY]: Getting Money for user {0} failed with the following message:{1}", agentID,
  291. (string) hbinfo["errorMessage"]);
  292. client.SendAlertMessage((string) hbinfo["errorMessage"]);
  293. }
  294. SendMoneyBalance(client, agentID, client.SessionId, LLUUID.Zero);
  295. }
  296. }
  297. }
  298. else
  299. {
  300. CheckExistAndRefreshFunds(client.AgentId);
  301. }
  302. // Subscribe to Money messages
  303. client.OnEconomyDataRequest += EconomyDataRequestHandler;
  304. client.OnMoneyBalanceRequest += SendMoneyBalance;
  305. client.OnRequestPayPrice += requestPayPrice;
  306. client.OnLogout += ClientClosed;
  307. }
  308. /// <summary>
  309. /// Transfer money
  310. /// </summary>
  311. /// <param name="Sender"></param>
  312. /// <param name="Receiver"></param>
  313. /// <param name="amount"></param>
  314. /// <returns></returns>
  315. private bool doMoneyTransfer(LLUUID Sender, LLUUID Receiver, int amount, int transactiontype, string description)
  316. {
  317. bool result = false;
  318. if (amount >= 0)
  319. {
  320. lock (m_KnownClientFunds)
  321. {
  322. // If we don't know about the sender, then the sender can't
  323. // actually be here and therefore this is likely fraud or outdated.
  324. if (m_MoneyAddress.Length == 0)
  325. {
  326. if (m_KnownClientFunds.ContainsKey(Sender))
  327. {
  328. // Does the sender have enough funds to give?
  329. if (m_KnownClientFunds[Sender] >= amount)
  330. {
  331. // Subtract the funds from the senders account
  332. m_KnownClientFunds[Sender] -= amount;
  333. // do we know about the receiver?
  334. if (!m_KnownClientFunds.ContainsKey(Receiver))
  335. {
  336. // Make a record for them so they get the updated balance when they login
  337. CheckExistAndRefreshFunds(Receiver);
  338. }
  339. if (m_enabled)
  340. {
  341. //Add the amount to the Receiver's funds
  342. m_KnownClientFunds[Receiver] += amount;
  343. result = true;
  344. }
  345. }
  346. else
  347. {
  348. // These below are redundant to make this clearer to read
  349. result = false;
  350. }
  351. }
  352. else
  353. {
  354. result = false;
  355. }
  356. }
  357. else
  358. {
  359. result = TransferMoneyonMoneyServer(Sender, Receiver, amount, transactiontype, description);
  360. }
  361. }
  362. }
  363. return result;
  364. }
  365. /// <summary>
  366. /// Sends the the stored money balance to the client
  367. /// </summary>
  368. /// <param name="client"></param>
  369. /// <param name="agentID"></param>
  370. /// <param name="SessionID"></param>
  371. /// <param name="TransactionID"></param>
  372. public void SendMoneyBalance(IClientAPI client, LLUUID agentID, LLUUID SessionID, LLUUID TransactionID)
  373. {
  374. if (client.AgentId == agentID && client.SessionId == SessionID)
  375. {
  376. int returnfunds = 0;
  377. try
  378. {
  379. returnfunds = GetFundsForAgentID(agentID);
  380. }
  381. catch (Exception e)
  382. {
  383. client.SendAlertMessage(e.Message + " ");
  384. }
  385. client.SendMoneyBalance(TransactionID, true, new byte[0], returnfunds);
  386. }
  387. else
  388. {
  389. client.SendAlertMessage("Unable to send your money balance to you!");
  390. }
  391. }
  392. /// <summary>
  393. /// Gets the current balance for the user from the Grid Money Server
  394. /// </summary>
  395. /// <param name="agentId"></param>
  396. /// <param name="secureSessionID"></param>
  397. /// <param name="regionId"></param>
  398. /// <param name="regionSecret"></param>
  399. /// <returns></returns>
  400. public Hashtable GetBalanceForUserFromMoneyServer(LLUUID agentId, LLUUID secureSessionID, LLUUID regionId, string regionSecret)
  401. {
  402. Hashtable MoneyBalanceRequestParams = new Hashtable();
  403. MoneyBalanceRequestParams["agentId"] = agentId.ToString();
  404. MoneyBalanceRequestParams["secureSessionId"] = secureSessionID.ToString();
  405. MoneyBalanceRequestParams["regionId"] = regionId.ToString();
  406. MoneyBalanceRequestParams["secret"] = regionSecret;
  407. MoneyBalanceRequestParams["currencySecret"] = ""; // per - region/user currency secret gotten from the money system
  408. Hashtable MoneyRespData = genericCurrencyXMLRPCRequest(MoneyBalanceRequestParams, "simulatorUserBalanceRequest");
  409. return MoneyRespData;
  410. }
  411. /// <summary>
  412. /// Generic XMLRPC client abstraction
  413. /// </summary>
  414. /// <param name="ReqParams">Hashtable containing parameters to the method</param>
  415. /// <param name="method">Method to invoke</param>
  416. /// <returns>Hashtable with success=>bool and other values</returns>
  417. public Hashtable genericCurrencyXMLRPCRequest(Hashtable ReqParams, string method)
  418. {
  419. ArrayList SendParams = new ArrayList();
  420. SendParams.Add(ReqParams);
  421. // Send Request
  422. XmlRpcResponse MoneyResp;
  423. try
  424. {
  425. XmlRpcRequest BalanceRequestReq = new XmlRpcRequest(method, SendParams);
  426. MoneyResp = BalanceRequestReq.Send(m_MoneyAddress, 30000);
  427. }
  428. catch (WebException ex)
  429. {
  430. m_log.ErrorFormat(
  431. "[MONEY]: Unable to connect to Money Server {0}. Exception {1}",
  432. m_MoneyAddress, ex);
  433. Hashtable ErrorHash = new Hashtable();
  434. ErrorHash["success"] = false;
  435. ErrorHash["errorMessage"] = "Unable to manage your money at this time. Purchases may be unavailable";
  436. ErrorHash["errorURI"] = "";
  437. return ErrorHash;
  438. //throw (ex);
  439. }
  440. catch (SocketException ex)
  441. {
  442. m_log.ErrorFormat(
  443. "[MONEY]: Unable to connect to Money Server {0}. Exception {1}",
  444. m_MoneyAddress, ex);
  445. Hashtable ErrorHash = new Hashtable();
  446. ErrorHash["success"] = false;
  447. ErrorHash["errorMessage"] = "Unable to manage your money at this time. Purchases may be unavailable";
  448. ErrorHash["errorURI"] = "";
  449. return ErrorHash;
  450. //throw (ex);
  451. }
  452. catch (XmlException ex)
  453. {
  454. m_log.ErrorFormat(
  455. "[MONEY]: Unable to connect to Money Server {0}. Exception {1}",
  456. m_MoneyAddress, ex);
  457. Hashtable ErrorHash = new Hashtable();
  458. ErrorHash["success"] = false;
  459. ErrorHash["errorMessage"] = "Unable to manage your money at this time. Purchases may be unavailable";
  460. ErrorHash["errorURI"] = "";
  461. return ErrorHash;
  462. }
  463. if (MoneyResp.IsFault)
  464. {
  465. Hashtable ErrorHash = new Hashtable();
  466. ErrorHash["success"] = false;
  467. ErrorHash["errorMessage"] = "Unable to manage your money at this time. Purchases may be unavailable";
  468. ErrorHash["errorURI"] = "";
  469. return ErrorHash;
  470. }
  471. Hashtable MoneyRespData = (Hashtable) MoneyResp.Value;
  472. return MoneyRespData;
  473. }
  474. /// <summary>
  475. /// This informs the Money Grid Server that the avatar is in this simulator
  476. /// </summary>
  477. /// <param name="agentId"></param>
  478. /// <param name="secureSessionID"></param>
  479. /// <param name="regionId"></param>
  480. /// <param name="regionSecret"></param>
  481. /// <returns></returns>
  482. public Hashtable claim_user(LLUUID agentId, LLUUID secureSessionID, LLUUID regionId, string regionSecret)
  483. {
  484. Hashtable MoneyBalanceRequestParams = new Hashtable();
  485. MoneyBalanceRequestParams["agentId"] = agentId.ToString();
  486. MoneyBalanceRequestParams["secureSessionId"] = secureSessionID.ToString();
  487. MoneyBalanceRequestParams["regionId"] = regionId.ToString();
  488. MoneyBalanceRequestParams["secret"] = regionSecret;
  489. Hashtable MoneyRespData = genericCurrencyXMLRPCRequest(MoneyBalanceRequestParams, "simulatorClaimUserRequest");
  490. IClientAPI sendMoneyBal = LocateClientObject(agentId);
  491. if (sendMoneyBal != null)
  492. {
  493. SendMoneyBalance(sendMoneyBal, agentId, sendMoneyBal.SessionId, LLUUID.Zero);
  494. }
  495. return MoneyRespData;
  496. }
  497. private SceneObjectPart findPrim(LLUUID objectID)
  498. {
  499. lock (m_scenel)
  500. {
  501. foreach (Scene s in m_scenel.Values)
  502. {
  503. SceneObjectPart part = s.GetSceneObjectPart(objectID);
  504. if (part != null)
  505. {
  506. return part;
  507. }
  508. }
  509. }
  510. return null;
  511. }
  512. private string resolveObjectName(LLUUID objectID)
  513. {
  514. SceneObjectPart part = findPrim(objectID);
  515. if (part != null)
  516. {
  517. return part.Name;
  518. }
  519. return String.Empty;
  520. }
  521. private string resolveAgentName(LLUUID agentID)
  522. {
  523. // try avatar username surname
  524. Scene scene = GetRandomScene();
  525. UserProfileData profile = scene.CommsManager.UserService.GetUserProfile(agentID);
  526. if (profile != null)
  527. {
  528. string avatarname = profile.FirstName + " " + profile.SurName;
  529. return avatarname;
  530. }
  531. return String.Empty;
  532. }
  533. private void BalanceUpdate(LLUUID senderID, LLUUID receiverID, bool transactionresult, string description)
  534. {
  535. IClientAPI sender = LocateClientObject(senderID);
  536. IClientAPI receiver = LocateClientObject(receiverID);
  537. if (senderID != receiverID)
  538. {
  539. if (sender != null)
  540. {
  541. sender.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(description), GetFundsForAgentID(senderID));
  542. }
  543. if (receiver != null)
  544. {
  545. receiver.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(description), GetFundsForAgentID(receiverID));
  546. }
  547. }
  548. }
  549. /// <summary>
  550. /// Informs the Money Grid Server of a transfer.
  551. /// </summary>
  552. /// <param name="sourceId"></param>
  553. /// <param name="destId"></param>
  554. /// <param name="amount"></param>
  555. /// <returns></returns>
  556. public bool TransferMoneyonMoneyServer(LLUUID sourceId, LLUUID destId, int amount, int transactiontype, string description)
  557. {
  558. int aggregatePermInventory = 0;
  559. int aggregatePermNextOwner = 0;
  560. int flags = 0;
  561. bool rvalue = false;
  562. IClientAPI cli = LocateClientObject(sourceId);
  563. if (cli != null)
  564. {
  565. Scene userScene = null;
  566. lock (m_rootAgents)
  567. {
  568. userScene = GetSceneByUUID(m_rootAgents[sourceId]);
  569. }
  570. if (userScene != null)
  571. {
  572. Hashtable ht = new Hashtable();
  573. ht["agentId"] = sourceId.ToString();
  574. ht["secureSessionId"] = cli.SecureSessionId.ToString();
  575. ht["regionId"] = userScene.RegionInfo.originRegionID.ToString();
  576. ht["secret"] = userScene.RegionInfo.regionSecret;
  577. ht["currencySecret"] = " ";
  578. ht["destId"] = destId.ToString();
  579. ht["cash"] = amount;
  580. ht["aggregatePermInventory"] = aggregatePermInventory;
  581. ht["aggregatePermNextOwner"] = aggregatePermNextOwner;
  582. ht["flags"] = flags;
  583. ht["transactionType"] = transactiontype;
  584. ht["description"] = description;
  585. Hashtable hresult = genericCurrencyXMLRPCRequest(ht, "regionMoveMoney");
  586. if ((bool) hresult["success"] == true)
  587. {
  588. int funds1 = 0;
  589. int funds2 = 0;
  590. try
  591. {
  592. funds1 = (Int32) hresult["funds"];
  593. }
  594. catch (InvalidCastException)
  595. {
  596. funds1 = 0;
  597. }
  598. SetLocalFundsForAgentID(sourceId, funds1);
  599. if (m_KnownClientFunds.ContainsKey(destId))
  600. {
  601. try
  602. {
  603. funds2 = (Int32) hresult["funds2"];
  604. }
  605. catch (InvalidCastException)
  606. {
  607. funds2 = 0;
  608. }
  609. SetLocalFundsForAgentID(destId, funds2);
  610. }
  611. rvalue = true;
  612. }
  613. else
  614. {
  615. cli.SendAgentAlertMessage((string) hresult["errorMessage"], true);
  616. }
  617. }
  618. }
  619. else
  620. {
  621. m_log.ErrorFormat("[MONEY]: Client {0} not found", sourceId.ToString());
  622. }
  623. return rvalue;
  624. }
  625. public int GetRemoteBalance(LLUUID agentId)
  626. {
  627. int funds = 0;
  628. IClientAPI aClient = LocateClientObject(agentId);
  629. if (aClient != null)
  630. {
  631. Scene s = LocateSceneClientIn(agentId);
  632. if (s != null)
  633. {
  634. if (m_MoneyAddress.Length > 0)
  635. {
  636. Hashtable hbinfo =
  637. GetBalanceForUserFromMoneyServer(aClient.AgentId, aClient.SecureSessionId, s.RegionInfo.originRegionID.ToString(),
  638. s.RegionInfo.regionSecret);
  639. if ((bool) hbinfo["success"] == true)
  640. {
  641. try
  642. {
  643. funds = (Int32) hbinfo["funds"];
  644. }
  645. catch (ArgumentException)
  646. {
  647. }
  648. catch (FormatException)
  649. {
  650. }
  651. catch (OverflowException)
  652. {
  653. m_log.ErrorFormat("[MONEY]: While getting the Currency for user {0}, the return funds overflowed.", agentId);
  654. aClient.SendAlertMessage("Unable to get your money balance, money operations will be unavailable");
  655. }
  656. catch (InvalidCastException)
  657. {
  658. funds = 0;
  659. }
  660. }
  661. else
  662. {
  663. m_log.WarnFormat("[MONEY]: Getting Money for user {0} failed with the following message:{1}", agentId,
  664. (string) hbinfo["errorMessage"]);
  665. aClient.SendAlertMessage((string) hbinfo["errorMessage"]);
  666. }
  667. }
  668. SetLocalFundsForAgentID(agentId, funds);
  669. SendMoneyBalance(aClient, agentId, aClient.SessionId, LLUUID.Zero);
  670. }
  671. else
  672. {
  673. m_log.Debug("[MONEY]: Got balance request update for agent that is here, but couldn't find which scene.");
  674. }
  675. }
  676. else
  677. {
  678. m_log.Debug("[MONEY]: Got balance request update for agent that isn't here.");
  679. }
  680. return funds;
  681. }
  682. public XmlRpcResponse GridMoneyUpdate(XmlRpcRequest request)
  683. {
  684. m_log.Debug("[MONEY]: Dynamic balance update called.");
  685. Hashtable requestData = (Hashtable) request.Params[0];
  686. if (requestData.ContainsKey("agentId"))
  687. {
  688. LLUUID agentId = LLUUID.Zero;
  689. Helpers.TryParse((string) requestData["agentId"], out agentId);
  690. if (agentId != LLUUID.Zero)
  691. {
  692. GetRemoteBalance(agentId);
  693. }
  694. else
  695. {
  696. m_log.Debug("[MONEY]: invalid agentId specified, dropping.");
  697. }
  698. }
  699. else
  700. {
  701. m_log.Debug("[MONEY]: no agentId specified, dropping.");
  702. }
  703. XmlRpcResponse r = new XmlRpcResponse();
  704. Hashtable rparms = new Hashtable();
  705. rparms["success"] = true;
  706. r.Value = rparms;
  707. return r;
  708. }
  709. /// <summary>
  710. /// XMLRPC handler to send alert message and sound to client
  711. /// </summary>
  712. public XmlRpcResponse UserAlert(XmlRpcRequest request)
  713. {
  714. XmlRpcResponse ret = new XmlRpcResponse();
  715. Hashtable retparam = new Hashtable();
  716. Hashtable requestData = (Hashtable) request.Params[0];
  717. LLUUID agentId = LLUUID.Zero;
  718. LLUUID soundId = LLUUID.Zero;
  719. Helpers.TryParse((string) requestData["agentId"], out agentId);
  720. Helpers.TryParse((string) requestData["soundId"], out soundId);
  721. string text = (string) requestData["text"];
  722. string secret = (string) requestData["secret"];
  723. Scene userScene = GetRandomScene();
  724. if (userScene.RegionInfo.regionSecret.ToString() == secret)
  725. {
  726. IClientAPI client = LocateClientObject(agentId);
  727. if (client != null)
  728. {
  729. if (soundId != LLUUID.Zero)
  730. client.SendPlayAttachedSound(soundId, LLUUID.Zero, LLUUID.Zero, 1.0f, 0);
  731. client.SendBlueBoxMessage(LLUUID.Zero, LLUUID.Zero, "", text);
  732. retparam.Add("success", true);
  733. }
  734. else
  735. {
  736. retparam.Add("success", false);
  737. }
  738. }
  739. else
  740. {
  741. retparam.Add("success", false);
  742. }
  743. ret.Value = retparam;
  744. return ret;
  745. }
  746. # region Standalone box enablers only
  747. public XmlRpcResponse quote_func(XmlRpcRequest request)
  748. {
  749. Hashtable requestData = (Hashtable) request.Params[0];
  750. LLUUID agentId = LLUUID.Zero;
  751. int amount = 0;
  752. Hashtable quoteResponse = new Hashtable();
  753. XmlRpcResponse returnval = new XmlRpcResponse();
  754. if (requestData.ContainsKey("agentId") && requestData.ContainsKey("currencyBuy"))
  755. {
  756. Helpers.TryParse((string) requestData["agentId"], out agentId);
  757. try
  758. {
  759. amount = (Int32) requestData["currencyBuy"];
  760. }
  761. catch (InvalidCastException)
  762. {
  763. }
  764. Hashtable currencyResponse = new Hashtable();
  765. currencyResponse.Add("estimatedCost", 0);
  766. currencyResponse.Add("currencyBuy", amount);
  767. quoteResponse.Add("success", true);
  768. quoteResponse.Add("currency", currencyResponse);
  769. quoteResponse.Add("confirm", "asdfad9fj39ma9fj");
  770. returnval.Value = quoteResponse;
  771. return returnval;
  772. }
  773. quoteResponse.Add("success", false);
  774. quoteResponse.Add("errorMessage", "Invalid parameters passed to the quote box");
  775. quoteResponse.Add("errorURI", "http://www.opensimulator.org/wiki");
  776. returnval.Value = quoteResponse;
  777. return returnval;
  778. }
  779. public XmlRpcResponse buy_func(XmlRpcRequest request)
  780. {
  781. Hashtable requestData = (Hashtable) request.Params[0];
  782. LLUUID agentId = LLUUID.Zero;
  783. int amount = 0;
  784. if (requestData.ContainsKey("agentId") && requestData.ContainsKey("currencyBuy"))
  785. {
  786. Helpers.TryParse((string) requestData["agentId"], out agentId);
  787. try
  788. {
  789. amount = (Int32) requestData["currencyBuy"];
  790. }
  791. catch (InvalidCastException)
  792. {
  793. }
  794. if (agentId != LLUUID.Zero)
  795. {
  796. lock (m_KnownClientFunds)
  797. {
  798. if (m_KnownClientFunds.ContainsKey(agentId))
  799. {
  800. m_KnownClientFunds[agentId] += amount;
  801. }
  802. else
  803. {
  804. m_KnownClientFunds.Add(agentId, amount);
  805. }
  806. }
  807. IClientAPI client = LocateClientObject(agentId);
  808. if (client != null)
  809. {
  810. SendMoneyBalance(client, agentId, client.SessionId, LLUUID.Zero);
  811. }
  812. }
  813. }
  814. XmlRpcResponse returnval = new XmlRpcResponse();
  815. Hashtable returnresp = new Hashtable();
  816. returnresp.Add("success", true);
  817. returnval.Value = returnresp;
  818. return returnval;
  819. }
  820. public XmlRpcResponse preflightBuyLandPrep_func(XmlRpcRequest request)
  821. {
  822. XmlRpcResponse ret = new XmlRpcResponse();
  823. Hashtable retparam = new Hashtable();
  824. Hashtable membershiplevels = new Hashtable();
  825. ArrayList levels = new ArrayList();
  826. Hashtable level = new Hashtable();
  827. level.Add("id", "00000000-0000-0000-0000-000000000000");
  828. level.Add("description", "some level");
  829. levels.Add(level);
  830. //membershiplevels.Add("levels",levels);
  831. Hashtable landuse = new Hashtable();
  832. landuse.Add("upgrade", false);
  833. landuse.Add("action", "http://invaliddomaininvalid.com/");
  834. Hashtable currency = new Hashtable();
  835. currency.Add("estimatedCost", 0);
  836. Hashtable membership = new Hashtable();
  837. membershiplevels.Add("upgrade", false);
  838. membershiplevels.Add("action", "http://invaliddomaininvalid.com/");
  839. membershiplevels.Add("levels", membershiplevels);
  840. retparam.Add("success", true);
  841. retparam.Add("currency", currency);
  842. retparam.Add("membership", membership);
  843. retparam.Add("landuse", landuse);
  844. retparam.Add("confirm", "asdfajsdkfjasdkfjalsdfjasdf");
  845. ret.Value = retparam;
  846. return ret;
  847. }
  848. public XmlRpcResponse landBuy_func(XmlRpcRequest request)
  849. {
  850. XmlRpcResponse ret = new XmlRpcResponse();
  851. Hashtable retparam = new Hashtable();
  852. Hashtable requestData = (Hashtable) request.Params[0];
  853. LLUUID agentId = LLUUID.Zero;
  854. int amount = 0;
  855. if (requestData.ContainsKey("agentId") && requestData.ContainsKey("currencyBuy"))
  856. {
  857. Helpers.TryParse((string) requestData["agentId"], out agentId);
  858. try
  859. {
  860. amount = (Int32) requestData["currencyBuy"];
  861. }
  862. catch (InvalidCastException)
  863. {
  864. }
  865. if (agentId != LLUUID.Zero)
  866. {
  867. lock (m_KnownClientFunds)
  868. {
  869. if (m_KnownClientFunds.ContainsKey(agentId))
  870. {
  871. m_KnownClientFunds[agentId] += amount;
  872. }
  873. else
  874. {
  875. m_KnownClientFunds.Add(agentId, amount);
  876. }
  877. }
  878. IClientAPI client = LocateClientObject(agentId);
  879. if (client != null)
  880. {
  881. SendMoneyBalance(client, agentId, client.SessionId, LLUUID.Zero);
  882. }
  883. }
  884. }
  885. retparam.Add("success", true);
  886. ret.Value = retparam;
  887. return ret;
  888. }
  889. #endregion
  890. #region local Fund Management
  891. /// <summary>
  892. /// Ensures that the agent accounting data is set up in this instance.
  893. /// </summary>
  894. /// <param name="agentID"></param>
  895. private void CheckExistAndRefreshFunds(LLUUID agentID)
  896. {
  897. lock (m_KnownClientFunds)
  898. {
  899. if (!m_KnownClientFunds.ContainsKey(agentID))
  900. {
  901. m_KnownClientFunds.Add(agentID, m_stipend);
  902. }
  903. else
  904. {
  905. if (m_KnownClientFunds[agentID] <= m_minFundsBeforeRefresh)
  906. {
  907. m_KnownClientFunds[agentID] = m_stipend;
  908. }
  909. }
  910. }
  911. }
  912. /// <summary>
  913. /// Gets the amount of Funds for an agent
  914. /// </summary>
  915. /// <param name="AgentID"></param>
  916. /// <returns></returns>
  917. private int GetFundsForAgentID(LLUUID AgentID)
  918. {
  919. int returnfunds = 0;
  920. lock (m_KnownClientFunds)
  921. {
  922. if (m_KnownClientFunds.ContainsKey(AgentID))
  923. {
  924. returnfunds = m_KnownClientFunds[AgentID];
  925. }
  926. else
  927. {
  928. //throw new Exception("Unable to get funds.");
  929. }
  930. }
  931. return returnfunds;
  932. }
  933. private void SetLocalFundsForAgentID(LLUUID AgentID, int amount)
  934. {
  935. lock (m_KnownClientFunds)
  936. {
  937. if (m_KnownClientFunds.ContainsKey(AgentID))
  938. {
  939. m_KnownClientFunds[AgentID] = amount;
  940. }
  941. else
  942. {
  943. m_KnownClientFunds.Add(AgentID, amount);
  944. }
  945. }
  946. }
  947. #endregion
  948. #region Utility Helpers
  949. /// <summary>
  950. /// Locates a IClientAPI for the client specified
  951. /// </summary>
  952. /// <param name="AgentID"></param>
  953. /// <returns></returns>
  954. private IClientAPI LocateClientObject(LLUUID AgentID)
  955. {
  956. ScenePresence tPresence = null;
  957. IClientAPI rclient = null;
  958. lock (m_scenel)
  959. {
  960. foreach (Scene _scene in m_scenel.Values)
  961. {
  962. tPresence = _scene.GetScenePresence(AgentID);
  963. if (tPresence != null)
  964. {
  965. if (!tPresence.IsChildAgent)
  966. {
  967. rclient = tPresence.ControllingClient;
  968. }
  969. }
  970. if (rclient != null)
  971. {
  972. return rclient;
  973. }
  974. }
  975. }
  976. return null;
  977. }
  978. private Scene LocateSceneClientIn(LLUUID AgentId)
  979. {
  980. lock (m_scenel)
  981. {
  982. foreach (Scene _scene in m_scenel.Values)
  983. {
  984. ScenePresence tPresence = _scene.GetScenePresence(AgentId);
  985. if (tPresence != null)
  986. {
  987. if (!tPresence.IsChildAgent)
  988. {
  989. return _scene;
  990. }
  991. }
  992. }
  993. }
  994. return null;
  995. }
  996. /// <summary>
  997. /// Utility function Gets a Random scene in the instance. For when which scene exactly you're doing something with doesn't matter
  998. /// </summary>
  999. /// <returns></returns>
  1000. public Scene GetRandomScene()
  1001. {
  1002. lock (m_scenel)
  1003. {
  1004. foreach (Scene rs in m_scenel.Values)
  1005. return rs;
  1006. }
  1007. return null;
  1008. }
  1009. /// <summary>
  1010. /// Utility function to get a Scene by RegionID in a module
  1011. /// </summary>
  1012. /// <param name="RegionID"></param>
  1013. /// <returns></returns>
  1014. public Scene GetSceneByUUID(LLUUID RegionID)
  1015. {
  1016. lock (m_scenel)
  1017. {
  1018. foreach (Scene rs in m_scenel.Values)
  1019. {
  1020. if (rs.RegionInfo.originRegionID == RegionID)
  1021. {
  1022. return rs;
  1023. }
  1024. }
  1025. }
  1026. return null;
  1027. }
  1028. #endregion
  1029. #region event Handlers
  1030. public void requestPayPrice(IClientAPI client, LLUUID objectID)
  1031. {
  1032. Scene scene = LocateSceneClientIn(client.AgentId);
  1033. if (scene == null)
  1034. return;
  1035. SceneObjectPart task = scene.GetSceneObjectPart(objectID);
  1036. if (task == null)
  1037. return;
  1038. SceneObjectGroup group = task.ParentGroup;
  1039. SceneObjectPart root = group.RootPart;
  1040. client.SendPayPrice(objectID, root.PayPrice);
  1041. }
  1042. /// <summary>
  1043. /// When the client closes the connection we remove their accounting info from memory to free up resources.
  1044. /// </summary>
  1045. /// <param name="AgentID"></param>
  1046. public void ClientClosed(LLUUID AgentID)
  1047. {
  1048. lock (m_KnownClientFunds)
  1049. {
  1050. if (m_keepMoneyAcrossLogins && m_MoneyAddress.Length == 0)
  1051. {
  1052. }
  1053. else
  1054. {
  1055. m_KnownClientFunds.Remove(AgentID);
  1056. }
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// Event called Economy Data Request handler.
  1061. /// </summary>
  1062. /// <param name="agentId"></param>
  1063. public void EconomyDataRequestHandler(LLUUID agentId)
  1064. {
  1065. IClientAPI user = LocateClientObject(agentId);
  1066. if (user != null)
  1067. {
  1068. user.SendEconomyData(EnergyEfficiency, ObjectCapacity, ObjectCount, PriceEnergyUnit, PriceGroupCreate,
  1069. PriceObjectClaim, PriceObjectRent, PriceObjectScaleFactor, PriceParcelClaim, PriceParcelClaimFactor,
  1070. PriceParcelRent, PricePublicObjectDecay, PricePublicObjectDelete, PriceRentLight, PriceUpload,
  1071. TeleportMinPrice, TeleportPriceExponent);
  1072. }
  1073. }
  1074. private void ValidateLandBuy(Object osender, EventManager.LandBuyArgs e)
  1075. {
  1076. if (m_MoneyAddress.Length == 0)
  1077. {
  1078. lock (m_KnownClientFunds)
  1079. {
  1080. if (m_KnownClientFunds.ContainsKey(e.agentId))
  1081. {
  1082. // Does the sender have enough funds to give?
  1083. if (m_KnownClientFunds[e.agentId] >= e.parcelPrice)
  1084. {
  1085. lock (e)
  1086. {
  1087. e.economyValidated = true;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. else
  1094. {
  1095. if (GetRemoteBalance(e.agentId) >= e.parcelPrice)
  1096. {
  1097. lock (e)
  1098. {
  1099. e.economyValidated = true;
  1100. }
  1101. }
  1102. }
  1103. }
  1104. private void processLandBuy(Object osender, EventManager.LandBuyArgs e)
  1105. {
  1106. lock (e)
  1107. {
  1108. if (e.economyValidated == true && e.transactionID == 0)
  1109. {
  1110. e.transactionID = Util.UnixTimeSinceEpoch();
  1111. if (doMoneyTransfer(e.agentId, e.parcelOwnerID, e.parcelPrice, 0, "Land purchase"))
  1112. {
  1113. lock (e)
  1114. {
  1115. e.amountDebited = e.parcelPrice;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. /// <summary>
  1122. /// THis method gets called when someone pays someone else as a gift.
  1123. /// </summary>
  1124. /// <param name="osender"></param>
  1125. /// <param name="e"></param>
  1126. private void MoneyTransferAction(Object osender, EventManager.MoneyTransferArgs e)
  1127. {
  1128. IClientAPI sender = null;
  1129. IClientAPI receiver = null;
  1130. if (m_MoneyAddress.Length > 0) // Handled on server
  1131. e.description = String.Empty;
  1132. if (e.transactiontype == 5008) // Object gets paid
  1133. {
  1134. sender = LocateClientObject(e.sender);
  1135. if (sender != null)
  1136. {
  1137. SceneObjectPart part = findPrim(e.receiver);
  1138. if (part == null)
  1139. return;
  1140. string name = resolveAgentName(part.OwnerID);
  1141. if (name == String.Empty)
  1142. name = "(hippos)";
  1143. receiver = LocateClientObject(part.OwnerID);
  1144. string description = String.Format("Paid {0} via object {1}", name, e.description);
  1145. bool transactionresult = doMoneyTransfer(e.sender, part.OwnerID, e.amount, e.transactiontype, description);
  1146. if (transactionresult)
  1147. {
  1148. ObjectPaid handlerOnObjectPaid = OnObjectPaid;
  1149. if (handlerOnObjectPaid != null)
  1150. {
  1151. handlerOnObjectPaid(e.receiver, e.sender, e.amount);
  1152. }
  1153. }
  1154. if (e.sender != e.receiver)
  1155. {
  1156. sender.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(e.description), GetFundsForAgentID(e.sender));
  1157. }
  1158. if (receiver != null)
  1159. {
  1160. receiver.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(e.description), GetFundsForAgentID(part.OwnerID));
  1161. }
  1162. }
  1163. return;
  1164. }
  1165. sender = LocateClientObject(e.sender);
  1166. if (sender != null)
  1167. {
  1168. receiver = LocateClientObject(e.receiver);
  1169. bool transactionresult = doMoneyTransfer(e.sender, e.receiver, e.amount, e.transactiontype, e.description);
  1170. if (e.sender != e.receiver)
  1171. {
  1172. if (sender != null)
  1173. {
  1174. sender.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(e.description), GetFundsForAgentID(e.sender));
  1175. }
  1176. }
  1177. if (receiver != null)
  1178. {
  1179. receiver.SendMoneyBalance(LLUUID.Random(), transactionresult, Helpers.StringToField(e.description), GetFundsForAgentID(e.receiver));
  1180. }
  1181. }
  1182. else
  1183. {
  1184. m_log.Warn("[MONEY]: Potential Fraud Warning, got money transfer request for avatar that isn't in this simulator - Details; Sender:" +
  1185. e.sender.ToString() + " Receiver: " + e.receiver.ToString() + " Amount: " + e.amount.ToString());
  1186. }
  1187. }
  1188. /// <summary>
  1189. /// Event Handler for when a root agent becomes a child agent
  1190. /// </summary>
  1191. /// <param name="avatar"></param>
  1192. private void MakeChildAgent(ScenePresence avatar)
  1193. {
  1194. lock (m_rootAgents)
  1195. {
  1196. if (m_rootAgents.ContainsKey(avatar.UUID))
  1197. {
  1198. if (m_rootAgents[avatar.UUID] == avatar.Scene.RegionInfo.originRegionID)
  1199. {
  1200. m_rootAgents.Remove(avatar.UUID);
  1201. m_log.Info("[MONEY]: Removing " + avatar.Firstname + " " + avatar.Lastname + " as a root agent");
  1202. }
  1203. }
  1204. }
  1205. }
  1206. /// <summary>
  1207. /// Event Handler for when the client logs out.
  1208. /// </summary>
  1209. /// <param name="AgentId"></param>
  1210. private void ClientLoggedOut(LLUUID AgentId)
  1211. {
  1212. lock (m_rootAgents)
  1213. {
  1214. if (m_rootAgents.ContainsKey(AgentId))
  1215. {
  1216. m_rootAgents.Remove(AgentId);
  1217. //m_log.Info("[MONEY]: Removing " + AgentId + ". Agent logged out.");
  1218. }
  1219. }
  1220. }
  1221. /// <summary>
  1222. /// Call this when the client disconnects.
  1223. /// </summary>
  1224. /// <param name="client"></param>
  1225. public void ClientClosed(IClientAPI client)
  1226. {
  1227. ClientClosed(client.AgentId);
  1228. }
  1229. /// <summary>
  1230. /// Event Handler for when an Avatar enters one of the parcels in the simulator.
  1231. /// </summary>
  1232. /// <param name="avatar"></param>
  1233. /// <param name="localLandID"></param>
  1234. /// <param name="regionID"></param>
  1235. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, LLUUID regionID)
  1236. {
  1237. lock (m_rootAgents)
  1238. {
  1239. if (m_rootAgents.ContainsKey(avatar.UUID))
  1240. {
  1241. if (avatar.Scene.RegionInfo.originRegionID != m_rootAgents[avatar.UUID])
  1242. {
  1243. m_rootAgents[avatar.UUID] = avatar.Scene.RegionInfo.originRegionID;
  1244. //m_log.Info("[MONEY]: Claiming " + avatar.Firstname + " " + avatar.Lastname + " in region:" + avatar.RegionHandle + ".");
  1245. // Claim User! my user! Mine mine mine!
  1246. if (m_MoneyAddress.Length > 0)
  1247. {
  1248. Scene RegionItem = GetSceneByUUID(regionID);
  1249. if (RegionItem != null)
  1250. {
  1251. Hashtable hresult =
  1252. claim_user(avatar.UUID, avatar.ControllingClient.SecureSessionId, regionID, RegionItem.RegionInfo.regionSecret);
  1253. if ((bool)hresult["success"] == true)
  1254. {
  1255. int funds = 0;
  1256. try
  1257. {
  1258. funds = (Int32)hresult["funds"];
  1259. }
  1260. catch (InvalidCastException)
  1261. {
  1262. }
  1263. SetLocalFundsForAgentID(avatar.UUID, funds);
  1264. }
  1265. else
  1266. {
  1267. avatar.ControllingClient.SendAgentAlertMessage((string)hresult["errorMessage"], true);
  1268. }
  1269. }
  1270. }
  1271. }
  1272. else
  1273. {
  1274. ILandObject obj = avatar.Scene.LandChannel.GetLandObject(avatar.AbsolutePosition.X, avatar.AbsolutePosition.Y);
  1275. if ((obj.landData.landFlags & (uint)Parcel.ParcelFlags.AllowDamage) != 0)
  1276. {
  1277. avatar.Invulnerable = false;
  1278. }
  1279. else
  1280. {
  1281. avatar.Invulnerable = true;
  1282. }
  1283. }
  1284. }
  1285. else
  1286. {
  1287. lock (m_rootAgents)
  1288. {
  1289. m_rootAgents.Add(avatar.UUID, avatar.Scene.RegionInfo.originRegionID);
  1290. }
  1291. if (m_MoneyAddress.Length > 0)
  1292. {
  1293. Scene RegionItem = GetSceneByUUID(regionID);
  1294. if (RegionItem != null)
  1295. {
  1296. Hashtable hresult = claim_user(avatar.UUID, avatar.ControllingClient.SecureSessionId, regionID, RegionItem.RegionInfo.regionSecret);
  1297. if ((bool) hresult["success"] == true)
  1298. {
  1299. int funds = 0;
  1300. try
  1301. {
  1302. funds = (Int32) hresult["funds"];
  1303. }
  1304. catch (InvalidCastException)
  1305. {
  1306. }
  1307. SetLocalFundsForAgentID(avatar.UUID, funds);
  1308. }
  1309. else
  1310. {
  1311. avatar.ControllingClient.SendAgentAlertMessage((string) hresult["errorMessage"], true);
  1312. }
  1313. }
  1314. }
  1315. //m_log.Info("[MONEY]: Claiming " + avatar.Firstname + " " + avatar.Lastname + " in region:" + avatar.RegionHandle + ".");
  1316. }
  1317. }
  1318. //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
  1319. }
  1320. private void KillAvatar(uint killerObjectLocalID, ScenePresence DeadAvatar)
  1321. {
  1322. if (killerObjectLocalID == 0)
  1323. DeadAvatar.ControllingClient.SendAgentAlertMessage("You committed suicide!", true);
  1324. else
  1325. {
  1326. bool foundResult = false;
  1327. string resultstring = "";
  1328. List<ScenePresence> allav = DeadAvatar.Scene.GetScenePresences();
  1329. try
  1330. {
  1331. foreach (ScenePresence av in allav)
  1332. {
  1333. if (av.LocalId == killerObjectLocalID)
  1334. {
  1335. av.ControllingClient.SendAlertMessage("You fragged " + DeadAvatar.Firstname + " " + DeadAvatar.Lastname);
  1336. resultstring = av.Firstname + " " + av.Lastname;
  1337. foundResult = true;
  1338. }
  1339. }
  1340. } catch (System.InvalidOperationException)
  1341. {
  1342. }
  1343. if (!foundResult)
  1344. {
  1345. SceneObjectPart part = DeadAvatar.Scene.GetSceneObjectPart(killerObjectLocalID);
  1346. if (part != null)
  1347. {
  1348. ScenePresence av = DeadAvatar.Scene.GetScenePresence(part.OwnerID);
  1349. if (av != null)
  1350. {
  1351. av.ControllingClient.SendAlertMessage("You fragged " + DeadAvatar.Firstname + " " + DeadAvatar.Lastname);
  1352. resultstring = av.Firstname + " " + av.Lastname;
  1353. DeadAvatar.ControllingClient.SendAgentAlertMessage("You got killed by " + resultstring + "!", true);
  1354. }
  1355. else
  1356. {
  1357. string killer = DeadAvatar.Scene.CommsManager.UUIDNameRequestString(part.OwnerID);
  1358. DeadAvatar.ControllingClient.SendAgentAlertMessage("You impailed yourself on " + part.Name + " owned by " + killer +"!", true);
  1359. }
  1360. //DeadAvatar.Scene. part.ObjectOwner
  1361. }
  1362. else
  1363. {
  1364. DeadAvatar.ControllingClient.SendAgentAlertMessage("You died!", true);
  1365. }
  1366. }
  1367. }
  1368. DeadAvatar.Health = 100;
  1369. DeadAvatar.Scene.TeleportClientHome(DeadAvatar.UUID, DeadAvatar.ControllingClient);
  1370. }
  1371. #endregion
  1372. }
  1373. public enum TransactionType : int
  1374. {
  1375. SystemGenerated = 0,
  1376. RegionMoneyRequest = 1,
  1377. Gift = 2,
  1378. Purchase = 3
  1379. }
  1380. }