SIPVoiceModule.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Framework.Communications.Capabilities;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Environment.Scenes;
  39. using Caps=OpenSim.Framework.Communications.Capabilities.Caps;
  40. namespace OpenSim.Region.Environment.Modules.Avatar.Voice.SIPVoice
  41. {
  42. public class SIPVoiceModule : IRegionModule
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private static readonly string m_parcelVoiceInfoRequestPath = "0007/";
  47. private static readonly string m_provisionVoiceAccountRequestPath = "0008/";
  48. private IConfig m_config;
  49. private Scene m_scene;
  50. private string m_sipDomain;
  51. #region IRegionModule Members
  52. public void Initialise(Scene scene, IConfigSource config)
  53. {
  54. m_scene = scene;
  55. m_config = config.Configs["Voice"];
  56. if (null == m_config || !m_config.GetBoolean("enabled", false))
  57. {
  58. m_log.Info("[VOICE] plugin disabled");
  59. return;
  60. }
  61. m_log.Info("[VOICE] plugin enabled");
  62. m_sipDomain = m_config.GetString("sip_domain", String.Empty);
  63. if (String.IsNullOrEmpty(m_sipDomain))
  64. {
  65. m_log.Error("[VOICE] plugin mis-configured: missing sip_domain configuration");
  66. m_log.Info("[VOICE] plugin disabled");
  67. return;
  68. }
  69. m_log.InfoFormat("[VOICE] using SIP domain {0}", m_sipDomain);
  70. scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  71. }
  72. public void PostInitialise()
  73. {
  74. }
  75. public void Close()
  76. {
  77. }
  78. public string Name
  79. {
  80. get { return "VoiceModule"; }
  81. }
  82. public bool IsSharedModule
  83. {
  84. get { return false; }
  85. }
  86. #endregion
  87. public void OnRegisterCaps(UUID agentID, Caps caps)
  88. {
  89. m_log.DebugFormat("[VOICE] OnRegisterCaps: agentID {0} caps {1}", agentID, caps);
  90. string capsBase = "/CAPS/" + caps.CapsObjectPath;
  91. caps.RegisterHandler("ParcelVoiceInfoRequest",
  92. new RestStreamHandler("POST", capsBase + m_parcelVoiceInfoRequestPath,
  93. delegate(string request, string path, string param,
  94. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  95. {
  96. return ParcelVoiceInfoRequest(request, path, param,
  97. agentID, caps);
  98. }));
  99. caps.RegisterHandler("ProvisionVoiceAccountRequest",
  100. new RestStreamHandler("POST", capsBase + m_provisionVoiceAccountRequestPath,
  101. delegate(string request, string path, string param,
  102. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  103. {
  104. return ProvisionVoiceAccountRequest(request, path, param,
  105. agentID, caps);
  106. }));
  107. }
  108. /// <summary>
  109. /// Callback for a client request for ParcelVoiceInfo
  110. /// </summary>
  111. /// <param name="request"></param>
  112. /// <param name="path"></param>
  113. /// <param name="param"></param>
  114. /// <param name="agentID"></param>
  115. /// <param name="caps"></param>
  116. /// <returns></returns>
  117. public string ParcelVoiceInfoRequest(string request, string path, string param,
  118. UUID agentID, Caps caps)
  119. {
  120. try
  121. {
  122. m_log.DebugFormat("[VOICE][PARCELVOICE]: request: {0}, path: {1}, param: {2}", request, path, param);
  123. // FIXME: get the creds from region file or from config
  124. Hashtable creds = new Hashtable();
  125. creds["channel_uri"] = String.Format("sip:{0}@{1}", agentID, m_sipDomain);
  126. string regionName = m_scene.RegionInfo.RegionName;
  127. ScenePresence avatar = m_scene.GetScenePresence(agentID);
  128. if (null == m_scene.LandChannel) throw new Exception("land data not yet available");
  129. LandData land = m_scene.GetLandData(avatar.AbsolutePosition.X, avatar.AbsolutePosition.Y);
  130. LLSDParcelVoiceInfoResponse parcelVoiceInfo =
  131. new LLSDParcelVoiceInfoResponse(regionName, land.LocalID, creds);
  132. string r = LLSDHelpers.SerialiseLLSDReply(parcelVoiceInfo);
  133. m_log.DebugFormat("[VOICE][PARCELVOICE]: {0}", r);
  134. return r;
  135. }
  136. catch (Exception e)
  137. {
  138. m_log.ErrorFormat("[CAPS]: {0}, try again later", e.ToString());
  139. }
  140. return null;
  141. }
  142. /// <summary>
  143. /// Callback for a client request for Voice Account Details
  144. /// </summary>
  145. /// <param name="request"></param>
  146. /// <param name="path"></param>
  147. /// <param name="param"></param>
  148. /// <param name="agentID"></param>
  149. /// <param name="caps"></param>
  150. /// <returns></returns>
  151. public string ProvisionVoiceAccountRequest(string request, string path, string param,
  152. UUID agentID, Caps caps)
  153. {
  154. try
  155. {
  156. m_log.DebugFormat("[VOICE][PROVISIONVOICE]: request: {0}, path: {1}, param: {2}",
  157. request, path, param);
  158. string voiceUser = "x" + Convert.ToBase64String(agentID.GetBytes());
  159. voiceUser = voiceUser.Replace('+', '-').Replace('/', '_');
  160. CachedUserInfo userInfo = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(agentID);
  161. if (null == userInfo) throw new Exception("cannot get user details");
  162. LLSDVoiceAccountResponse voiceAccountResponse =
  163. new LLSDVoiceAccountResponse(voiceUser, "$1$" + userInfo.UserProfile.PasswordHash);
  164. string r = LLSDHelpers.SerialiseLLSDReply(voiceAccountResponse);
  165. m_log.DebugFormat("[CAPS][PROVISIONVOICE]: {0}", r);
  166. return r;
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.ErrorFormat("[CAPS][PROVISIONVOICE]: {0}, retry later", e.Message);
  171. }
  172. return null;
  173. }
  174. }
  175. }