SampleMoneyModule.cs 56 KB

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