Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using log4net;
  4. using Nini.Config;
  5. using OpenSim.Framework;
  6. using OpenSim.Framework.Servers.HttpServer;
  7. using OpenSim.Region.Framework.Interfaces;
  8. using OpenSim.Region.Framework.Scenes;
  9. using OpenMetaverse;
  10. using System.Collections.Generic;
  11.  
  12. namespace MumbleVoice
  13. {
  14. /// Offer Mumble server information via REST method.
  15. /// Request headers:
  16. /// - avatar_uuid ... The uuid of the avatar requesting voice
  17. ///
  18. /// @todo Return xml insted of reponse headers
  19. /// @todo Access control using eq. user specifig passwords
  20. public class MumbleVoiceModule : IRegionModule
  21. {
  22. private static readonly ILog m_Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  23.  
  24. public Type ReplaceableInterface { get { return null; } }
  25.  
  26. private string m_server_address = null;
  27. private string m_server_password = "";
  28. private string m_server_version = "";
  29. private string m_default_channel = "Root";
  30. private string m_context = "Mumbe voice system";
  31. private static string SERVICE_REST_URL = "/mumble_server_info";
  32. private bool m_enabled = false;
  33.  
  34. private List<Scene> m_scenes = new List<Scene>();
  35.  
  36. public MumbleVoiceModule()
  37. {
  38. }
  39.  
  40. public void Initialise(Scene scene, IConfigSource source)
  41. {
  42. ReadConfig(source);
  43.  
  44. m_scenes.Add(scene);
  45.  
  46. //scene.EventManager.OnMakeChildAgent += OnMakeChildAgent;
  47. scene.EventManager.OnMakeRootAgent += OnMakeRootAgent;
  48.  
  49. m_Log.Info("[MUMBLE VOIP]: MumbleVoiceModule initialized");
  50. }
  51.  
  52. private void ReadConfig(IConfigSource source)
  53. {
  54. try
  55. {
  56. if (source.Configs["mumblevoice"] != null)
  57. {
  58. m_enabled = source.Configs["mumblevoice"].GetBoolean("enabled", false);
  59. m_server_address = source.Configs["mumblevoice"].GetString("server_address", "");
  60. m_server_password = source.Configs["mumblevoice"].GetString("server_password", "");
  61. m_server_version = source.Configs["mumblevoice"].GetString("server_version", "");
  62. if (source.Configs["mumblevoice"].Contains("channel"))
  63. m_default_channel = source.Configs["mumblevoice"].GetString("channel", "Root");
  64. else
  65. m_default_channel = null;
  66. }
  67. }
  68. catch (Exception)
  69. {
  70. m_Log.Error("[MUMBLE VOIP]: Cannot find server configuration");
  71. }
  72. }
  73.  
  74. public void PostInitialise()
  75. {
  76. if (m_enabled)
  77. {
  78. foreach (Scene s in m_scenes)
  79. {
  80. s.EventManager.OnRegisterCaps += new EventManager.RegisterCapsEvent(EventManager_OnRegisterCaps);
  81. }
  82. }
  83. }
  84.  
  85. void EventManager_OnRegisterCaps(OpenMetaverse.UUID agentID, OpenSim.Framework.Capabilities.Caps caps)
  86. {
  87. UUID capID = UUID.Random();
  88. caps.RegisterHandler("mumble_server_info", new RestStreamHandler("GET", "/CAPS/" + capID, RestGetMumbleServerInfo));
  89. }
  90.  
  91. /// <summary>
  92. /// Returns information about a mumble server via a REST Request
  93. /// </summary>
  94. /// <param name="request"></param>
  95. /// <param name="path"></param>
  96. /// <param name="param">A string representing the sim's UUID</param>
  97. /// <param name="httpRequest">HTTP request header object</param>
  98. /// <param name="httpResponse">HTTP response header object</param>
  99. /// <returns>Information about the mumble server in http response headers</returns>
  100. public string RestGetMumbleServerInfo(string request, string path, string param,
  101. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  102. {
  103. if (m_server_address == null)
  104. {
  105. httpResponse.StatusCode = 404;
  106. httpResponse.StatusDescription = "Not Found";
  107.  
  108. string message = "[MUMBLE VOIP]: Server info request from " + httpRequest.RemoteIPEndPoint.Address + ". Cannot send response, module is not configured properly.";
  109. m_Log.Warn(message);
  110. return "Mumble server info is not available.";
  111. }
  112. if (httpRequest.Headers.GetValues("avatar_uuid") == null)
  113. {
  114. httpResponse.StatusCode = 400;
  115. httpResponse.StatusDescription = "Bad Request";
  116.  
  117. string message = "[MUMBLE VOIP]: Invalid server info request from " + httpRequest.RemoteIPEndPoint.Address +"";
  118. m_Log.Warn(message);
  119. return "avatar_uuid header is missing";
  120. }
  121.  
  122. string avatar_uuid = httpRequest.Headers.GetValues("avatar_uuid")[0];
  123. string responseBody = String.Empty;
  124. UUID avatarId;
  125. if (UUID.TryParse(avatar_uuid, out avatarId))
  126. {
  127. string channel;
  128. string regionId = GetRegionUuid(avatarId);
  129. if (regionId == null)
  130. {
  131. //user has not logged in?
  132. regionId = "Root";
  133. }
  134.  
  135. if (m_default_channel == null)
  136. channel = regionId;
  137. else
  138. channel = m_default_channel;
  139.  
  140. httpResponse.AddHeader("Mumble-Server", m_server_address);
  141. httpResponse.AddHeader("Mumble-Version", m_server_version);
  142. httpResponse.AddHeader("Mumble-Channel", channel);
  143. httpResponse.AddHeader("Mumble-User", avatar_uuid);
  144. httpResponse.AddHeader("Mumble-Password", m_server_password);
  145. httpResponse.AddHeader("Mumble-Avatar-Id", avatar_uuid);
  146. httpResponse.AddHeader("Mumble-Context-Id", m_context);
  147.  
  148. responseBody += "Mumble-Server: " + m_server_address + "\n";
  149. responseBody += "Mumble-Version: " + m_server_version + "\n";
  150. responseBody += "Mumble-Channel: " + channel + "\n";
  151. responseBody += "Mumble-User: " + avatar_uuid + "\n";
  152. responseBody += "Mumble-Password: " + m_server_password + "\n";
  153. responseBody += "Mumble-Avatar-Id: " + avatar_uuid + "\n";
  154. responseBody += "Mumble-Context-Id: " + m_context + "\n";
  155.  
  156. string log_message = "[MUMBLE VOIP]: Server info request handled for " + httpRequest.RemoteIPEndPoint.Address + "";
  157. m_Log.Info(log_message);
  158. }
  159. else
  160. {
  161. httpResponse.StatusCode = 400;
  162. httpResponse.StatusDescription = "Bad Request";
  163.  
  164. m_Log.Warn("[MUMBLE VOIP]: Could not parse avatar uuid from request");
  165. return "could not parse avatar_uuid header";
  166. }
  167.  
  168. return responseBody;
  169. }
  170.  
  171. string GetRegionUuid(UUID userId)
  172. {
  173. foreach (Scene s in m_scenes)
  174. {
  175. ScenePresence avatar = s.GetScenePresence(userId);
  176. if (avatar != null && !(avatar.IsChildAgent))
  177. {
  178. return s.RegionInfo.RegionID.ToString();
  179. }
  180. }
  181.  
  182. return null;
  183. }
  184.  
  185. public void Close()
  186. {
  187. if (m_enabled)
  188. {
  189. foreach (Scene s in m_scenes)
  190. {
  191. s.EventManager.OnRegisterCaps -= new EventManager.RegisterCapsEvent(EventManager_OnRegisterCaps);
  192. }
  193. }
  194. }
  195.  
  196. public string Name
  197. {
  198. get { return "Mumble Voice Module"; }
  199. }
  200.  
  201. public bool IsSharedModule
  202. {
  203. get { return true; }
  204. }
  205.  
  206. private void OnMakeRootAgent(ScenePresence presence)
  207. {
  208. m_Log.Info("[MUMBLE VOIP]: new root agent.");
  209. }
  210.  
  211. private void OnMakeChildAgent(ScenePresence presence)
  212. {
  213. m_Log.Info("[MUMBLE VOIP]: new child agent.");
  214. }
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement