DialogModule.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Region.CoreModules.Avatar.Dialog
  38. {
  39. public class DialogModule : IRegionModule, IDialogModule
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. protected Scene m_scene;
  43. public void Initialise(Scene scene, IConfigSource source)
  44. {
  45. m_scene = scene;
  46. m_scene.RegisterModuleInterface<IDialogModule>(this);
  47. m_scene.AddCommand(
  48. this, "alert", "alert <first> <last> <message>",
  49. "Send an alert to a user",
  50. HandleAlertConsoleCommand);
  51. m_scene.AddCommand(
  52. this, "alert general", "alert [general] <message>",
  53. "Send an alert to everyone",
  54. "If keyword 'general' is omitted, then <message> must be surrounded by quotation marks.",
  55. HandleAlertConsoleCommand);
  56. }
  57. public void PostInitialise() {}
  58. public void Close() {}
  59. public string Name { get { return "Dialog Module"; } }
  60. public bool IsSharedModule { get { return false; } }
  61. public void SendAlertToUser(IClientAPI client, string message)
  62. {
  63. SendAlertToUser(client, message, false);
  64. }
  65. public void SendAlertToUser(IClientAPI client, string message, bool modal)
  66. {
  67. client.SendAgentAlertMessage(message, modal);
  68. }
  69. public void SendAlertToUser(UUID agentID, string message)
  70. {
  71. SendAlertToUser(agentID, message, false);
  72. }
  73. public void SendAlertToUser(UUID agentID, string message, bool modal)
  74. {
  75. ScenePresence sp = m_scene.GetScenePresence(agentID);
  76. if (sp != null)
  77. sp.ControllingClient.SendAgentAlertMessage(message, modal);
  78. }
  79. public void SendAlertToUser(string firstName, string lastName, string message, bool modal)
  80. {
  81. ScenePresence presence = m_scene.GetScenePresence(firstName, lastName);
  82. if (presence != null)
  83. presence.ControllingClient.SendAgentAlertMessage(message, modal);
  84. }
  85. public void SendGeneralAlert(string message)
  86. {
  87. m_scene.ForEachScenePresence(delegate(ScenePresence presence)
  88. {
  89. if (!presence.IsChildAgent)
  90. presence.ControllingClient.SendAlertMessage(message);
  91. });
  92. }
  93. public void SendDialogToUser(
  94. UUID avatarID, string objectName, UUID objectID, UUID ownerID,
  95. string message, UUID textureID, int ch, string[] buttonlabels)
  96. {
  97. UserAccount account = m_scene.UserAccountService.GetUserAccount(m_scene.RegionInfo.ScopeID, ownerID);
  98. string ownerFirstName, ownerLastName;
  99. if (account != null)
  100. {
  101. ownerFirstName = account.FirstName;
  102. ownerLastName = account.LastName;
  103. }
  104. else
  105. {
  106. ownerFirstName = "(unknown";
  107. ownerLastName = "user)";
  108. }
  109. ScenePresence sp = m_scene.GetScenePresence(avatarID);
  110. if (sp != null)
  111. sp.ControllingClient.SendDialog(objectName, objectID, ownerFirstName, ownerLastName, message, textureID, ch, buttonlabels);
  112. }
  113. public void SendUrlToUser(
  114. UUID avatarID, string objectName, UUID objectID, UUID ownerID, bool groupOwned, string message, string url)
  115. {
  116. ScenePresence sp = m_scene.GetScenePresence(avatarID);
  117. if (sp != null)
  118. sp.ControllingClient.SendLoadURL(objectName, objectID, ownerID, groupOwned, message, url);
  119. }
  120. public void SendTextBoxToUser(UUID avatarid, string message, int chatChannel, string name, UUID objectid, UUID ownerid)
  121. {
  122. UserAccount account = m_scene.UserAccountService.GetUserAccount(m_scene.RegionInfo.ScopeID, ownerid);
  123. string ownerFirstName, ownerLastName;
  124. if (account != null)
  125. {
  126. ownerFirstName = account.FirstName;
  127. ownerLastName = account.LastName;
  128. }
  129. else
  130. {
  131. ownerFirstName = "(unknown";
  132. ownerLastName = "user)";
  133. }
  134. ScenePresence sp = m_scene.GetScenePresence(avatarid);
  135. if (sp != null)
  136. sp.ControllingClient.SendTextBoxRequest(message, chatChannel, name, ownerFirstName, ownerLastName, objectid);
  137. }
  138. public void SendNotificationToUsersInRegion(
  139. UUID fromAvatarID, string fromAvatarName, string message)
  140. {
  141. m_scene.ForEachScenePresence(delegate(ScenePresence presence)
  142. {
  143. if (!presence.IsChildAgent)
  144. presence.ControllingClient.SendBlueBoxMessage(fromAvatarID, fromAvatarName, message);
  145. });
  146. }
  147. /// <summary>
  148. /// Handle an alert command from the console.
  149. /// </summary>
  150. /// <param name="module"></param>
  151. /// <param name="cmdparams"></param>
  152. public void HandleAlertConsoleCommand(string module, string[] cmdparams)
  153. {
  154. if (m_scene.ConsoleScene() != null && m_scene.ConsoleScene() != m_scene)
  155. return;
  156. bool isGeneral = false;
  157. string firstName = string.Empty;
  158. string lastName = string.Empty;
  159. string message = string.Empty;
  160. if (cmdparams.Length > 1)
  161. {
  162. firstName = cmdparams[1];
  163. isGeneral = firstName.ToLower().Equals("general");
  164. }
  165. if (cmdparams.Length == 2 && !isGeneral)
  166. {
  167. // alert "message"
  168. message = cmdparams[1];
  169. isGeneral = true;
  170. }
  171. else if (cmdparams.Length > 2 && isGeneral)
  172. {
  173. // alert general <message>
  174. message = CombineParams(cmdparams, 2);
  175. }
  176. else if (cmdparams.Length > 3)
  177. {
  178. // alert <first> <last> <message>
  179. lastName = cmdparams[2];
  180. message = CombineParams(cmdparams, 3);
  181. }
  182. else
  183. {
  184. OpenSim.Framework.Console.MainConsole.Instance.Output(
  185. "Usage: alert \"message\" | alert general <message> | alert <first> <last> <message>");
  186. return;
  187. }
  188. if (isGeneral)
  189. {
  190. m_log.InfoFormat(
  191. "[DIALOG]: Sending general alert in region {0} with message {1}",
  192. m_scene.RegionInfo.RegionName, message);
  193. SendGeneralAlert(message);
  194. }
  195. else
  196. {
  197. m_log.InfoFormat(
  198. "[DIALOG]: Sending alert in region {0} to {1} {2} with message {3}",
  199. m_scene.RegionInfo.RegionName, firstName, lastName, message);
  200. SendAlertToUser(firstName, lastName, message, false);
  201. }
  202. }
  203. private string CombineParams(string[] commandParams, int pos)
  204. {
  205. string result = string.Empty;
  206. for (int i = pos; i < commandParams.Length; i++)
  207. {
  208. result += commandParams[i] + " ";
  209. }
  210. return result;
  211. }
  212. }
  213. }