DialogModule.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 Mono.Addins;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. namespace OpenSim.Region.CoreModules.Avatar.Dialog
  39. {
  40. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DialogModule")]
  41. public class DialogModule : IDialogModule, INonSharedRegionModule
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. protected Scene m_scene;
  45. public void Initialise(IConfigSource source) { }
  46. public Type ReplaceableInterface { get { return null; } }
  47. public void AddRegion(Scene scene)
  48. {
  49. m_scene = scene;
  50. m_scene.RegisterModuleInterface<IDialogModule>(this);
  51. }
  52. public void RegionLoaded(Scene scene)
  53. {
  54. if (scene != m_scene)
  55. return;
  56. m_scene.AddCommand(
  57. "Users", this, "alert", "alert <message>",
  58. "Send an alert to everyone",
  59. HandleAlertConsoleCommand);
  60. m_scene.AddCommand(
  61. "Users", this, "alert-user",
  62. "alert-user <first> <last> <message>",
  63. "Send an alert to a user",
  64. HandleAlertConsoleCommand);
  65. }
  66. public void RemoveRegion(Scene scene)
  67. {
  68. if (scene != m_scene)
  69. return;
  70. m_scene.UnregisterModuleInterface<IDialogModule>(this);
  71. }
  72. public void Close() { }
  73. public string Name { get { return "Dialog Module"; } }
  74. public void SendAlertToUser(IClientAPI client, string message)
  75. {
  76. SendAlertToUser(client, message, false);
  77. }
  78. public void SendAlertToUser(IClientAPI client, string message,
  79. bool modal)
  80. {
  81. client.SendAgentAlertMessage(message, modal);
  82. }
  83. public void SendAlertToUser(UUID agentID, string message)
  84. {
  85. SendAlertToUser(agentID, message, false);
  86. }
  87. public void SendAlertToUser(UUID agentID, string message, bool modal)
  88. {
  89. ScenePresence sp = m_scene.GetScenePresence(agentID);
  90. if (sp != null)
  91. sp.ControllingClient.SendAgentAlertMessage(message, modal);
  92. }
  93. public void SendAlertToUser(string firstName, string lastName,
  94. string message, bool modal)
  95. {
  96. ScenePresence presence = m_scene.GetScenePresence(firstName,
  97. lastName);
  98. if (presence != null)
  99. {
  100. presence.ControllingClient.SendAgentAlertMessage(message,
  101. modal);
  102. }
  103. }
  104. public void SendGeneralAlert(string message)
  105. {
  106. m_scene.ForEachRootClient(delegate(IClientAPI client)
  107. {
  108. client.SendAlertMessage(message);
  109. });
  110. }
  111. private void GetOwnerName(UUID ownerID, out string ownerFirstName, out string ownerLastName)
  112. {
  113. ownerFirstName = string.Empty;
  114. ownerLastName = string.Empty;
  115. string username = m_scene.UserManagementModule.GetUserName(ownerID);
  116. if (!string.IsNullOrEmpty(username) && !username.StartsWith("UnknownUMM2", StringComparison.InvariantCulture))
  117. {
  118. string[] parts = username.Split(' ');
  119. ownerFirstName = parts[0];
  120. if (parts.Length > 1)
  121. ownerLastName = parts[1];
  122. }
  123. else
  124. {
  125. IGroupsModule groups = m_scene.RequestModuleInterface<IGroupsModule>();
  126. if (groups != null)
  127. {
  128. GroupRecord grprec = groups.GetGroupRecord(ownerID);
  129. if (grprec != null && !string.IsNullOrEmpty(grprec.GroupName))
  130. ownerFirstName = grprec.GroupName;
  131. }
  132. }
  133. if(string.IsNullOrEmpty(ownerFirstName))
  134. {
  135. ownerFirstName = "(unknown)";
  136. ownerLastName = string.Empty;
  137. }
  138. }
  139. public void SendDialogToUser(UUID avatarID, string objectName,
  140. UUID objectID, UUID ownerID, string message, UUID textureID,
  141. int ch, string[] buttonlabels)
  142. {
  143. ScenePresence sp = m_scene.GetScenePresence(avatarID);
  144. if (sp != null)
  145. {
  146. string ownerFirstName;
  147. string ownerLastName;
  148. GetOwnerName(ownerID, out ownerFirstName, out ownerLastName);
  149. sp.ControllingClient.SendDialog(objectName, objectID, ownerID,
  150. ownerFirstName, ownerLastName, message, textureID, ch,
  151. buttonlabels);
  152. }
  153. }
  154. public void SendUrlToUser(UUID avatarID, string objectName,
  155. UUID objectID, UUID ownerID, bool groupOwned, string message,
  156. string url)
  157. {
  158. ScenePresence sp = m_scene.GetScenePresence(avatarID);
  159. if (sp != null)
  160. {
  161. sp.ControllingClient.SendLoadURL(objectName, objectID,
  162. ownerID, groupOwned, message, url);
  163. }
  164. }
  165. public void SendTextBoxToUser(UUID avatarid, string message,
  166. int chatChannel, string name, UUID objectid, UUID ownerID)
  167. {
  168. ScenePresence sp = m_scene.GetScenePresence(avatarid);
  169. if (sp != null)
  170. {
  171. string ownerFirstName;
  172. string ownerLastName;
  173. GetOwnerName(ownerID, out ownerFirstName, out ownerLastName);
  174. sp.ControllingClient.SendTextBoxRequest(message, chatChannel,
  175. name, ownerID, ownerFirstName, ownerLastName,
  176. objectid);
  177. }
  178. }
  179. public void SendNotificationToUsersInRegion(UUID fromAvatarID,
  180. string fromAvatarName, string message)
  181. {
  182. m_scene.ForEachRootClient(delegate(IClientAPI client)
  183. {
  184. client.SendAgentAlertMessage(
  185. message, false);
  186. });
  187. }
  188. /// <summary>
  189. /// Handle an alert command from the console.
  190. /// </summary>
  191. /// <param name="module"></param>
  192. /// <param name="cmdparams"></param>
  193. public void HandleAlertConsoleCommand(string module,
  194. string[] cmdparams)
  195. {
  196. if (m_scene.ConsoleScene() != null &&
  197. m_scene.ConsoleScene() != m_scene)
  198. {
  199. return;
  200. }
  201. string message = string.Empty;
  202. if (cmdparams[0].ToLower().Equals("alert"))
  203. {
  204. message = CombineParams(cmdparams, 1);
  205. m_log.InfoFormat("[DIALOG]: Sending general alert in region {0} with message {1}",
  206. m_scene.RegionInfo.RegionName, message);
  207. SendGeneralAlert(message);
  208. }
  209. else if (cmdparams.Length > 3)
  210. {
  211. string firstName = cmdparams[1];
  212. string lastName = cmdparams[2];
  213. message = CombineParams(cmdparams, 3);
  214. m_log.InfoFormat("[DIALOG]: Sending alert in region {0} to {1} {2} with message {3}",
  215. m_scene.RegionInfo.RegionName, firstName, lastName,
  216. message);
  217. SendAlertToUser(firstName, lastName, message, false);
  218. }
  219. else
  220. {
  221. MainConsole.Instance.Output(
  222. "Usage: alert <message> | alert-user <first> <last> <message>");
  223. return;
  224. }
  225. }
  226. private string CombineParams(string[] commandParams, int pos)
  227. {
  228. string result = string.Empty;
  229. for (int i = pos; i < commandParams.Length; i++)
  230. {
  231. result += commandParams[i] + " ";
  232. }
  233. return result;
  234. }
  235. }
  236. }