SIPVoiceModule.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 libsecondlife;
  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(LLUUID 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. {
  95. return ParcelVoiceInfoRequest(request, path, param,
  96. agentID, caps);
  97. }));
  98. caps.RegisterHandler("ProvisionVoiceAccountRequest",
  99. new RestStreamHandler("POST", capsBase + m_provisionVoiceAccountRequestPath,
  100. delegate(string request, string path, string param)
  101. {
  102. return ProvisionVoiceAccountRequest(request, path, param,
  103. agentID, caps);
  104. }));
  105. }
  106. /// <summary>
  107. /// Callback for a client request for ParcelVoiceInfo
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <param name="path"></param>
  111. /// <param name="param"></param>
  112. /// <param name="agentID"></param>
  113. /// <param name="caps"></param>
  114. /// <returns></returns>
  115. public string ParcelVoiceInfoRequest(string request, string path, string param,
  116. LLUUID agentID, Caps caps)
  117. {
  118. try
  119. {
  120. m_log.DebugFormat("[VOICE][PARCELVOICE]: request: {0}, path: {1}, param: {2}", request, path, param);
  121. // FIXME: get the creds from region file or from config
  122. Hashtable creds = new Hashtable();
  123. creds["channel_uri"] = String.Format("sip:{0}@{1}", agentID, m_sipDomain);
  124. string regionName = m_scene.RegionInfo.RegionName;
  125. ScenePresence avatar = m_scene.GetScenePresence(agentID);
  126. if (null == m_scene.LandChannel) throw new Exception("land data not yet available");
  127. LandData land = m_scene.GetLandData(avatar.AbsolutePosition.X, avatar.AbsolutePosition.Y);
  128. LLSDParcelVoiceInfoResponse parcelVoiceInfo =
  129. new LLSDParcelVoiceInfoResponse(regionName, land.localID, creds);
  130. string r = LLSDHelpers.SerialiseLLSDReply(parcelVoiceInfo);
  131. m_log.DebugFormat("[VOICE][PARCELVOICE]: {0}", r);
  132. return r;
  133. }
  134. catch (Exception e)
  135. {
  136. m_log.ErrorFormat("[CAPS]: {0}, try again later", e.ToString());
  137. }
  138. return null;
  139. }
  140. /// <summary>
  141. /// Callback for a client request for Voice Account Details
  142. /// </summary>
  143. /// <param name="request"></param>
  144. /// <param name="path"></param>
  145. /// <param name="param"></param>
  146. /// <param name="agentID"></param>
  147. /// <param name="caps"></param>
  148. /// <returns></returns>
  149. public string ProvisionVoiceAccountRequest(string request, string path, string param,
  150. LLUUID agentID, Caps caps)
  151. {
  152. try
  153. {
  154. m_log.DebugFormat("[VOICE][PROVISIONVOICE]: request: {0}, path: {1}, param: {2}",
  155. request, path, param);
  156. string voiceUser = "x" + Convert.ToBase64String(agentID.GetBytes());
  157. voiceUser = voiceUser.Replace('+', '-').Replace('/', '_');
  158. CachedUserInfo userInfo = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(agentID);
  159. if (null == userInfo) throw new Exception("cannot get user details");
  160. LLSDVoiceAccountResponse voiceAccountResponse =
  161. new LLSDVoiceAccountResponse(voiceUser, "$1$" + userInfo.UserProfile.PasswordHash);
  162. string r = LLSDHelpers.SerialiseLLSDReply(voiceAccountResponse);
  163. m_log.DebugFormat("[CAPS][PROVISIONVOICE]: {0}", r);
  164. return r;
  165. }
  166. catch (Exception e)
  167. {
  168. m_log.ErrorFormat("[CAPS][PROVISIONVOICE]: {0}, retry later", e.Message);
  169. }
  170. return null;
  171. }
  172. }
  173. }