GroupsServiceHGConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.Linq;
  30. using System.Reflection;
  31. using System.Text;
  32. using OpenSim.Framework;
  33. using OpenSim.Server.Base;
  34. using OpenMetaverse;
  35. using log4net;
  36. namespace OpenSim.Groups
  37. {
  38. public class GroupsServiceHGConnector
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private string m_ServerURI;
  42. private object m_Lock = new object();
  43. public GroupsServiceHGConnector(string url)
  44. {
  45. m_ServerURI = url;
  46. if (!m_ServerURI.EndsWith("/"))
  47. m_ServerURI += "/";
  48. m_log.DebugFormat("[Groups.HGConnector]: Groups server at {0}", m_ServerURI);
  49. }
  50. public bool CreateProxy(string RequestingAgentID, string AgentID, string accessToken, UUID groupID, string url, string name, out string reason)
  51. {
  52. reason = string.Empty;
  53. Dictionary<string, object> sendData = new Dictionary<string,object>();
  54. sendData["RequestingAgentID"] = RequestingAgentID;
  55. sendData["AgentID"] = AgentID.ToString();
  56. sendData["AccessToken"] = accessToken;
  57. sendData["GroupID"] = groupID.ToString();
  58. sendData["Location"] = url;
  59. sendData["Name"] = name;
  60. Dictionary<string, object> ret = MakeRequest("POSTGROUP", sendData);
  61. if (ret == null)
  62. return false;
  63. if (!ret.ContainsKey("RESULT"))
  64. return false;
  65. if (ret["RESULT"].ToString().ToLower() != "true")
  66. {
  67. reason = ret["REASON"].ToString();
  68. return false;
  69. }
  70. return true;
  71. }
  72. public void RemoveAgentFromGroup(string AgentID, UUID GroupID, string token)
  73. {
  74. Dictionary<string, object> sendData = new Dictionary<string, object>();
  75. sendData["AgentID"] = AgentID;
  76. sendData["GroupID"] = GroupID.ToString();
  77. sendData["AccessToken"] = GroupsDataUtils.Sanitize(token);
  78. MakeRequest("REMOVEAGENTFROMGROUP", sendData);
  79. }
  80. public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, UUID GroupID, string GroupName, string token)
  81. {
  82. if (GroupID.IsZero() && string.IsNullOrEmpty(GroupName))
  83. return null;
  84. Dictionary<string, object> sendData = new Dictionary<string, object>();
  85. if (!GroupID.IsZero())
  86. sendData["GroupID"] = GroupID.ToString();
  87. if (!string.IsNullOrEmpty(GroupName))
  88. sendData["Name"] = GroupsDataUtils.Sanitize(GroupName);
  89. sendData["RequestingAgentID"] = RequestingAgentID;
  90. sendData["AccessToken"] = GroupsDataUtils.Sanitize(token);
  91. Dictionary<string, object> ret = MakeRequest("GETGROUP", sendData);
  92. if (ret == null)
  93. return null;
  94. if (!ret.ContainsKey("RESULT"))
  95. return null;
  96. if (ret["RESULT"].ToString() == "NULL")
  97. return null;
  98. return GroupsDataUtils.GroupRecord((Dictionary<string, object>)ret["RESULT"]);
  99. }
  100. public List<ExtendedGroupMembersData> GetGroupMembers(string RequestingAgentID, UUID GroupID, string token)
  101. {
  102. List<ExtendedGroupMembersData> members = new List<ExtendedGroupMembersData>();
  103. Dictionary<string, object> sendData = new Dictionary<string, object>();
  104. sendData["GroupID"] = GroupID.ToString();
  105. sendData["RequestingAgentID"] = RequestingAgentID;
  106. sendData["AccessToken"] = GroupsDataUtils.Sanitize(token);
  107. Dictionary<string, object> ret = MakeRequest("GETGROUPMEMBERS", sendData);
  108. if (ret == null)
  109. return members;
  110. if (!ret.ContainsKey("RESULT"))
  111. return members;
  112. if (ret["RESULT"].ToString() == "NULL")
  113. return members;
  114. foreach (object v in ((Dictionary<string, object>)ret["RESULT"]).Values)
  115. {
  116. ExtendedGroupMembersData m = GroupsDataUtils.GroupMembersData((Dictionary<string, object>)v);
  117. members.Add(m);
  118. }
  119. return members;
  120. }
  121. public List<GroupRolesData> GetGroupRoles(string RequestingAgentID, UUID GroupID, string token)
  122. {
  123. List<GroupRolesData> roles = new List<GroupRolesData>();
  124. Dictionary<string, object> sendData = new Dictionary<string, object>();
  125. sendData["GroupID"] = GroupID.ToString();
  126. sendData["RequestingAgentID"] = RequestingAgentID;
  127. sendData["AccessToken"] = GroupsDataUtils.Sanitize(token);
  128. Dictionary<string, object> ret = MakeRequest("GETGROUPROLES", sendData);
  129. if (ret == null)
  130. return roles;
  131. if (!ret.ContainsKey("RESULT"))
  132. return roles;
  133. if (ret["RESULT"].ToString() == "NULL")
  134. return roles;
  135. foreach (object v in ((Dictionary<string, object>)ret["RESULT"]).Values)
  136. {
  137. GroupRolesData m = GroupsDataUtils.GroupRolesData((Dictionary<string, object>)v);
  138. roles.Add(m);
  139. }
  140. return roles;
  141. }
  142. public List<ExtendedGroupRoleMembersData> GetGroupRoleMembers(string RequestingAgentID, UUID GroupID, string token)
  143. {
  144. List<ExtendedGroupRoleMembersData> rmembers = new List<ExtendedGroupRoleMembersData>();
  145. Dictionary<string, object> sendData = new Dictionary<string, object>();
  146. sendData["GroupID"] = GroupID.ToString();
  147. sendData["RequestingAgentID"] = RequestingAgentID;
  148. sendData["AccessToken"] = GroupsDataUtils.Sanitize(token);
  149. Dictionary<string, object> ret = MakeRequest("GETROLEMEMBERS", sendData);
  150. if (ret == null)
  151. return rmembers;
  152. if (!ret.ContainsKey("RESULT"))
  153. return rmembers;
  154. if (ret["RESULT"].ToString() == "NULL")
  155. return rmembers;
  156. foreach (object v in ((Dictionary<string, object>)ret["RESULT"]).Values)
  157. {
  158. ExtendedGroupRoleMembersData m = GroupsDataUtils.GroupRoleMembersData((Dictionary<string, object>)v);
  159. rmembers.Add(m);
  160. }
  161. return rmembers;
  162. }
  163. public bool AddNotice(string RequestingAgentID, UUID groupID, UUID noticeID, string fromName, string subject, string message,
  164. bool hasAttachment, byte attType, string attName, UUID attItemID, string attOwnerID)
  165. {
  166. Dictionary<string, object> sendData = new Dictionary<string, object>();
  167. sendData["GroupID"] = groupID.ToString();
  168. sendData["NoticeID"] = noticeID.ToString();
  169. sendData["FromName"] = GroupsDataUtils.Sanitize(fromName);
  170. sendData["Subject"] = GroupsDataUtils.Sanitize(subject);
  171. sendData["Message"] = GroupsDataUtils.Sanitize(message);
  172. sendData["HasAttachment"] = hasAttachment.ToString();
  173. if (hasAttachment)
  174. {
  175. sendData["AttachmentType"] = attType.ToString();
  176. sendData["AttachmentName"] = attName.ToString();
  177. sendData["AttachmentItemID"] = attItemID.ToString();
  178. sendData["AttachmentOwnerID"] = attOwnerID;
  179. }
  180. sendData["RequestingAgentID"] = RequestingAgentID;
  181. Dictionary<string, object> ret = MakeRequest("ADDNOTICE", sendData);
  182. if (ret == null)
  183. return false;
  184. if (!ret.ContainsKey("RESULT"))
  185. return false;
  186. if (ret["RESULT"].ToString().ToLower() != "true")
  187. return false;
  188. return true;
  189. }
  190. public bool VerifyNotice(UUID noticeID, UUID groupID)
  191. {
  192. Dictionary<string, object> sendData = new Dictionary<string, object>();
  193. sendData["NoticeID"] = noticeID.ToString();
  194. sendData["GroupID"] = groupID.ToString();
  195. Dictionary<string, object> ret = MakeRequest("VERIFYNOTICE", sendData);
  196. if (ret == null)
  197. return false;
  198. if (!ret.ContainsKey("RESULT"))
  199. return false;
  200. if (ret["RESULT"].ToString().ToLower() != "true")
  201. return false;
  202. return true;
  203. }
  204. //
  205. //
  206. //
  207. //
  208. //
  209. #region Make Request
  210. private Dictionary<string, object> MakeRequest(string method, Dictionary<string, object> sendData)
  211. {
  212. sendData["METHOD"] = method;
  213. string reply = string.Empty;
  214. try
  215. {
  216. lock (m_Lock)
  217. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  218. m_ServerURI + "hg-groups",
  219. ServerUtils.BuildQueryString(sendData));
  220. }
  221. catch
  222. {
  223. return null;
  224. }
  225. //m_log.DebugFormat("[XXX]: reply was {0}", reply);
  226. if (string.IsNullOrEmpty(reply))
  227. return null;
  228. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  229. return replyData;
  230. }
  231. #endregion
  232. }
  233. }