GroupsServiceRobustConnector.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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.Reflection;
  29. using System.Text;
  30. using System.Xml;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Framework.ServiceAuth;
  39. using OpenSim.Server.Handlers.Base;
  40. using log4net;
  41. using OpenMetaverse;
  42. namespace OpenSim.Groups
  43. {
  44. public class GroupsServiceRobustConnector : ServiceConnector
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private GroupsService m_GroupsService;
  48. private string m_ConfigName = "Groups";
  49. public GroupsServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) :
  50. base(config, server, configName)
  51. {
  52. string key = string.Empty;
  53. if (configName != String.Empty)
  54. m_ConfigName = configName;
  55. m_log.DebugFormat("[Groups.RobustConnector]: Starting with config name {0}", m_ConfigName);
  56. IConfig groupsConfig = config.Configs[m_ConfigName];
  57. if (groupsConfig != null)
  58. {
  59. key = groupsConfig.GetString("SecretKey", string.Empty);
  60. m_log.DebugFormat("[Groups.RobustConnector]: Starting with secret key {0}", key);
  61. }
  62. // else
  63. // m_log.DebugFormat("[Groups.RobustConnector]: Unable to find {0} section in configuration", m_ConfigName);
  64. m_GroupsService = new GroupsService(config);
  65. IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName);
  66. server.AddStreamHandler(new GroupsServicePostHandler(m_GroupsService, auth));
  67. }
  68. }
  69. public class GroupsServicePostHandler : BaseStreamHandler
  70. {
  71. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  72. private GroupsService m_GroupsService;
  73. public GroupsServicePostHandler(GroupsService service, IServiceAuth auth) :
  74. base("POST", "/groups", auth)
  75. {
  76. m_GroupsService = service;
  77. }
  78. protected override byte[] ProcessRequest(string path, Stream requestData,
  79. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  80. {
  81. StreamReader sr = new StreamReader(requestData);
  82. string body = sr.ReadToEnd();
  83. sr.Close();
  84. body = body.Trim();
  85. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  86. try
  87. {
  88. Dictionary<string, object> request =
  89. ServerUtils.ParseQueryString(body);
  90. if (!request.ContainsKey("METHOD"))
  91. return FailureResult();
  92. string method = request["METHOD"].ToString();
  93. request.Remove("METHOD");
  94. // m_log.DebugFormat("[Groups.Handler]: {0}", method);
  95. switch (method)
  96. {
  97. case "PUTGROUP":
  98. return HandleAddOrUpdateGroup(request);
  99. case "GETGROUP":
  100. return HandleGetGroup(request);
  101. case "ADDAGENTTOGROUP":
  102. return HandleAddAgentToGroup(request);
  103. case "REMOVEAGENTFROMGROUP":
  104. return HandleRemoveAgentFromGroup(request);
  105. case "GETMEMBERSHIP":
  106. return HandleGetMembership(request);
  107. case "GETGROUPMEMBERS":
  108. return HandleGetGroupMembers(request);
  109. case "PUTROLE":
  110. return HandlePutRole(request);
  111. case "REMOVEROLE":
  112. return HandleRemoveRole(request);
  113. case "GETGROUPROLES":
  114. return HandleGetGroupRoles(request);
  115. case "GETROLEMEMBERS":
  116. return HandleGetRoleMembers(request);
  117. case "AGENTROLE":
  118. return HandleAgentRole(request);
  119. case "GETAGENTROLES":
  120. return HandleGetAgentRoles(request);
  121. case "SETACTIVE":
  122. return HandleSetActive(request);
  123. case "UPDATEMEMBERSHIP":
  124. return HandleUpdateMembership(request);
  125. case "INVITE":
  126. return HandleInvite(request);
  127. case "ADDNOTICE":
  128. return HandleAddNotice(request);
  129. case "GETNOTICES":
  130. return HandleGetNotices(request);
  131. case "FINDGROUPS":
  132. return HandleFindGroups(request);
  133. }
  134. m_log.DebugFormat("[GROUPS HANDLER]: unknown method request: {0}", method);
  135. }
  136. catch (Exception e)
  137. {
  138. m_log.Error(string.Format("[GROUPS HANDLER]: Exception {0} ", e.Message), e);
  139. }
  140. return FailureResult();
  141. }
  142. byte[] HandleAddOrUpdateGroup(Dictionary<string, object> request)
  143. {
  144. Dictionary<string, object> result = new Dictionary<string, object>();
  145. ExtendedGroupRecord grec = GroupsDataUtils.GroupRecord(request);
  146. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("OP"))
  147. NullResult(result, "Bad network data");
  148. else
  149. {
  150. string RequestingAgentID = request["RequestingAgentID"].ToString();
  151. string reason = string.Empty;
  152. string op = request["OP"].ToString();
  153. if (op == "ADD")
  154. {
  155. grec.GroupID = m_GroupsService.CreateGroup(RequestingAgentID, grec.GroupName, grec.Charter, grec.ShowInList, grec.GroupPicture, grec.MembershipFee,
  156. grec.OpenEnrollment, grec.AllowPublish, grec.MaturePublish, grec.FounderID, out reason);
  157. }
  158. else if (op == "UPDATE")
  159. {
  160. m_GroupsService.UpdateGroup(RequestingAgentID, grec.GroupID, grec.Charter, grec.ShowInList, grec.GroupPicture, grec.MembershipFee,
  161. grec.OpenEnrollment, grec.AllowPublish, grec.MaturePublish);
  162. }
  163. if (grec.GroupID != UUID.Zero)
  164. {
  165. grec = m_GroupsService.GetGroupRecord(RequestingAgentID, grec.GroupID);
  166. if (grec == null)
  167. NullResult(result, "Internal Error");
  168. else
  169. result["RESULT"] = GroupsDataUtils.GroupRecord(grec);
  170. }
  171. else
  172. NullResult(result, reason);
  173. }
  174. string xmlString = ServerUtils.BuildXmlResponse(result);
  175. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  176. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  177. }
  178. byte[] HandleGetGroup(Dictionary<string, object> request)
  179. {
  180. Dictionary<string, object> result = new Dictionary<string, object>();
  181. if (!request.ContainsKey("RequestingAgentID"))
  182. NullResult(result, "Bad network data");
  183. else
  184. {
  185. string RequestingAgentID = request["RequestingAgentID"].ToString();
  186. ExtendedGroupRecord grec = null;
  187. if (request.ContainsKey("GroupID"))
  188. {
  189. UUID groupID = new UUID(request["GroupID"].ToString());
  190. grec = m_GroupsService.GetGroupRecord(RequestingAgentID, groupID);
  191. }
  192. else if (request.ContainsKey("Name"))
  193. {
  194. string name = request["Name"].ToString();
  195. grec = m_GroupsService.GetGroupRecord(RequestingAgentID, name);
  196. }
  197. if (grec == null)
  198. NullResult(result, "Group not found");
  199. else
  200. result["RESULT"] = GroupsDataUtils.GroupRecord(grec);
  201. }
  202. string xmlString = ServerUtils.BuildXmlResponse(result);
  203. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  204. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  205. }
  206. byte[] HandleAddAgentToGroup(Dictionary<string, object> request)
  207. {
  208. Dictionary<string, object> result = new Dictionary<string, object>();
  209. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AgentID") ||
  210. !request.ContainsKey("GroupID") || !request.ContainsKey("RoleID"))
  211. NullResult(result, "Bad network data");
  212. else
  213. {
  214. UUID groupID = new UUID(request["GroupID"].ToString());
  215. UUID roleID = new UUID(request["RoleID"].ToString());
  216. string agentID = request["AgentID"].ToString();
  217. string requestingAgentID = request["RequestingAgentID"].ToString();
  218. string token = string.Empty;
  219. string reason = string.Empty;
  220. if (request.ContainsKey("AccessToken"))
  221. token = request["AccessToken"].ToString();
  222. if (!m_GroupsService.AddAgentToGroup(requestingAgentID, agentID, groupID, roleID, token, out reason))
  223. NullResult(result, reason);
  224. else
  225. {
  226. GroupMembershipData membership = m_GroupsService.GetAgentGroupMembership(requestingAgentID, agentID, groupID);
  227. if (membership == null)
  228. NullResult(result, "Internal error");
  229. else
  230. result["RESULT"] = GroupsDataUtils.GroupMembershipData((ExtendedGroupMembershipData)membership);
  231. }
  232. }
  233. string xmlString = ServerUtils.BuildXmlResponse(result);
  234. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  235. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  236. }
  237. byte[] HandleRemoveAgentFromGroup(Dictionary<string, object> request)
  238. {
  239. Dictionary<string, object> result = new Dictionary<string, object>();
  240. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AgentID") || !request.ContainsKey("GroupID"))
  241. NullResult(result, "Bad network data");
  242. else
  243. {
  244. UUID groupID = new UUID(request["GroupID"].ToString());
  245. string agentID = request["AgentID"].ToString();
  246. string requestingAgentID = request["RequestingAgentID"].ToString();
  247. if (!m_GroupsService.RemoveAgentFromGroup(requestingAgentID, agentID, groupID))
  248. NullResult(result, string.Format("Insufficient permissions. {0}", agentID));
  249. else
  250. result["RESULT"] = "true";
  251. }
  252. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  253. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  254. }
  255. byte[] HandleGetMembership(Dictionary<string, object> request)
  256. {
  257. Dictionary<string, object> result = new Dictionary<string, object>();
  258. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AgentID"))
  259. NullResult(result, "Bad network data");
  260. else
  261. {
  262. string agentID = request["AgentID"].ToString();
  263. UUID groupID = UUID.Zero;
  264. if (request.ContainsKey("GroupID"))
  265. groupID = new UUID(request["GroupID"].ToString());
  266. string requestingAgentID = request["RequestingAgentID"].ToString();
  267. bool all = request.ContainsKey("ALL");
  268. if (!all)
  269. {
  270. ExtendedGroupMembershipData membership = null;
  271. if (groupID == UUID.Zero)
  272. {
  273. membership = m_GroupsService.GetAgentActiveMembership(requestingAgentID, agentID);
  274. }
  275. else
  276. {
  277. membership = m_GroupsService.GetAgentGroupMembership(requestingAgentID, agentID, groupID);
  278. }
  279. if (membership == null)
  280. NullResult(result, "No such membership");
  281. else
  282. result["RESULT"] = GroupsDataUtils.GroupMembershipData(membership);
  283. }
  284. else
  285. {
  286. List<GroupMembershipData> memberships = m_GroupsService.GetAgentGroupMemberships(requestingAgentID, agentID);
  287. if (memberships == null || (memberships != null && memberships.Count == 0))
  288. {
  289. NullResult(result, "No memberships");
  290. }
  291. else
  292. {
  293. Dictionary<string, object> dict = new Dictionary<string, object>();
  294. int i = 0;
  295. foreach (GroupMembershipData m in memberships)
  296. dict["m-" + i++] = GroupsDataUtils.GroupMembershipData((ExtendedGroupMembershipData)m);
  297. result["RESULT"] = dict;
  298. }
  299. }
  300. }
  301. string xmlString = ServerUtils.BuildXmlResponse(result);
  302. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  303. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  304. }
  305. byte[] HandleGetGroupMembers(Dictionary<string, object> request)
  306. {
  307. Dictionary<string, object> result = new Dictionary<string, object>();
  308. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID"))
  309. NullResult(result, "Bad network data");
  310. else
  311. {
  312. UUID groupID = new UUID(request["GroupID"].ToString());
  313. string requestingAgentID = request["RequestingAgentID"].ToString();
  314. List<ExtendedGroupMembersData> members = m_GroupsService.GetGroupMembers(requestingAgentID, groupID);
  315. if (members == null || (members != null && members.Count == 0))
  316. {
  317. NullResult(result, "No members");
  318. }
  319. else
  320. {
  321. Dictionary<string, object> dict = new Dictionary<string, object>();
  322. int i = 0;
  323. foreach (ExtendedGroupMembersData m in members)
  324. {
  325. dict["m-" + i++] = GroupsDataUtils.GroupMembersData(m);
  326. }
  327. result["RESULT"] = dict;
  328. }
  329. }
  330. string xmlString = ServerUtils.BuildXmlResponse(result);
  331. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  332. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  333. }
  334. byte[] HandlePutRole(Dictionary<string, object> request)
  335. {
  336. Dictionary<string, object> result = new Dictionary<string, object>();
  337. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("RoleID") ||
  338. !request.ContainsKey("Name") || !request.ContainsKey("Description") || !request.ContainsKey("Title") ||
  339. !request.ContainsKey("Powers") || !request.ContainsKey("OP"))
  340. NullResult(result, "Bad network data");
  341. else
  342. {
  343. string op = request["OP"].ToString();
  344. string reason = string.Empty;
  345. bool success = false;
  346. if (op == "ADD")
  347. success = m_GroupsService.AddGroupRole(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  348. new UUID(request["RoleID"].ToString()), request["Name"].ToString(), request["Description"].ToString(),
  349. request["Title"].ToString(), UInt64.Parse(request["Powers"].ToString()), out reason);
  350. else if (op == "UPDATE")
  351. success = m_GroupsService.UpdateGroupRole(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  352. new UUID(request["RoleID"].ToString()), request["Name"].ToString(), request["Description"].ToString(),
  353. request["Title"].ToString(), UInt64.Parse(request["Powers"].ToString()));
  354. result["RESULT"] = success.ToString();
  355. }
  356. string xmlString = ServerUtils.BuildXmlResponse(result);
  357. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  358. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  359. }
  360. byte[] HandleRemoveRole(Dictionary<string, object> request)
  361. {
  362. Dictionary<string, object> result = new Dictionary<string, object>();
  363. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("RoleID"))
  364. NullResult(result, "Bad network data");
  365. else
  366. {
  367. m_GroupsService.RemoveGroupRole(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  368. new UUID(request["RoleID"].ToString()));
  369. result["RESULT"] = "true";
  370. }
  371. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  372. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  373. }
  374. byte[] HandleGetGroupRoles(Dictionary<string, object> request)
  375. {
  376. Dictionary<string, object> result = new Dictionary<string, object>();
  377. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID"))
  378. NullResult(result, "Bad network data");
  379. else
  380. {
  381. UUID groupID = new UUID(request["GroupID"].ToString());
  382. string requestingAgentID = request["RequestingAgentID"].ToString();
  383. List<GroupRolesData> roles = m_GroupsService.GetGroupRoles(requestingAgentID, groupID);
  384. if (roles == null || (roles != null && roles.Count == 0))
  385. {
  386. NullResult(result, "No members");
  387. }
  388. else
  389. {
  390. Dictionary<string, object> dict = new Dictionary<string, object>();
  391. int i = 0;
  392. foreach (GroupRolesData r in roles)
  393. dict["r-" + i++] = GroupsDataUtils.GroupRolesData(r);
  394. result["RESULT"] = dict;
  395. }
  396. }
  397. string xmlString = ServerUtils.BuildXmlResponse(result);
  398. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  399. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  400. }
  401. byte[] HandleGetRoleMembers(Dictionary<string, object> request)
  402. {
  403. Dictionary<string, object> result = new Dictionary<string, object>();
  404. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID"))
  405. NullResult(result, "Bad network data");
  406. else
  407. {
  408. UUID groupID = new UUID(request["GroupID"].ToString());
  409. string requestingAgentID = request["RequestingAgentID"].ToString();
  410. List<ExtendedGroupRoleMembersData> rmembers = m_GroupsService.GetGroupRoleMembers(requestingAgentID, groupID);
  411. if (rmembers == null || (rmembers != null && rmembers.Count == 0))
  412. {
  413. NullResult(result, "No members");
  414. }
  415. else
  416. {
  417. Dictionary<string, object> dict = new Dictionary<string, object>();
  418. int i = 0;
  419. foreach (ExtendedGroupRoleMembersData rm in rmembers)
  420. dict["rm-" + i++] = GroupsDataUtils.GroupRoleMembersData(rm);
  421. result["RESULT"] = dict;
  422. }
  423. }
  424. string xmlString = ServerUtils.BuildXmlResponse(result);
  425. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  426. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  427. }
  428. byte[] HandleAgentRole(Dictionary<string, object> request)
  429. {
  430. Dictionary<string, object> result = new Dictionary<string, object>();
  431. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("RoleID") ||
  432. !request.ContainsKey("AgentID") || !request.ContainsKey("OP"))
  433. NullResult(result, "Bad network data");
  434. else
  435. {
  436. string op = request["OP"].ToString();
  437. bool success = false;
  438. if (op == "ADD")
  439. success = m_GroupsService.AddAgentToGroupRole(request["RequestingAgentID"].ToString(), request["AgentID"].ToString(),
  440. new UUID(request["GroupID"].ToString()), new UUID(request["RoleID"].ToString()));
  441. else if (op == "DELETE")
  442. success = m_GroupsService.RemoveAgentFromGroupRole(request["RequestingAgentID"].ToString(), request["AgentID"].ToString(),
  443. new UUID(request["GroupID"].ToString()), new UUID(request["RoleID"].ToString()));
  444. result["RESULT"] = success.ToString();
  445. }
  446. string xmlString = ServerUtils.BuildXmlResponse(result);
  447. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  448. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  449. }
  450. byte[] HandleGetAgentRoles(Dictionary<string, object> request)
  451. {
  452. Dictionary<string, object> result = new Dictionary<string, object>();
  453. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AgentID"))
  454. NullResult(result, "Bad network data");
  455. else
  456. {
  457. UUID groupID = new UUID(request["GroupID"].ToString());
  458. string agentID = request["AgentID"].ToString();
  459. string requestingAgentID = request["RequestingAgentID"].ToString();
  460. List<GroupRolesData> roles = m_GroupsService.GetAgentGroupRoles(requestingAgentID, agentID, groupID);
  461. if (roles == null || (roles != null && roles.Count == 0))
  462. {
  463. NullResult(result, "No members");
  464. }
  465. else
  466. {
  467. Dictionary<string, object> dict = new Dictionary<string, object>();
  468. int i = 0;
  469. foreach (GroupRolesData r in roles)
  470. dict["r-" + i++] = GroupsDataUtils.GroupRolesData(r);
  471. result["RESULT"] = dict;
  472. }
  473. }
  474. string xmlString = ServerUtils.BuildXmlResponse(result);
  475. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  476. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  477. }
  478. byte[] HandleSetActive(Dictionary<string, object> request)
  479. {
  480. Dictionary<string, object> result = new Dictionary<string, object>();
  481. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") ||
  482. !request.ContainsKey("AgentID") || !request.ContainsKey("OP"))
  483. {
  484. NullResult(result, "Bad network data");
  485. string xmlString = ServerUtils.BuildXmlResponse(result);
  486. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  487. }
  488. else
  489. {
  490. string op = request["OP"].ToString();
  491. if (op == "GROUP")
  492. {
  493. ExtendedGroupMembershipData group = m_GroupsService.SetAgentActiveGroup(request["RequestingAgentID"].ToString(),
  494. request["AgentID"].ToString(), new UUID(request["GroupID"].ToString()));
  495. if (group == null)
  496. NullResult(result, "Internal error");
  497. else
  498. result["RESULT"] = GroupsDataUtils.GroupMembershipData(group);
  499. string xmlString = ServerUtils.BuildXmlResponse(result);
  500. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  501. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  502. }
  503. else if (op == "ROLE" && request.ContainsKey("RoleID"))
  504. {
  505. m_GroupsService.SetAgentActiveGroupRole(request["RequestingAgentID"].ToString(), request["AgentID"].ToString(),
  506. new UUID(request["GroupID"].ToString()), new UUID(request["RoleID"].ToString()));
  507. result["RESULT"] = "true";
  508. }
  509. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  510. }
  511. }
  512. byte[] HandleUpdateMembership(Dictionary<string, object> request)
  513. {
  514. Dictionary<string, object> result = new Dictionary<string, object>();
  515. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("AgentID") || !request.ContainsKey("GroupID") ||
  516. !request.ContainsKey("AcceptNotices") || !request.ContainsKey("ListInProfile"))
  517. NullResult(result, "Bad network data");
  518. else
  519. {
  520. m_GroupsService.UpdateMembership(request["RequestingAgentID"].ToString(), request["AgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  521. bool.Parse(request["AcceptNotices"].ToString()), bool.Parse(request["ListInProfile"].ToString()));
  522. result["RESULT"] = "true";
  523. }
  524. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  525. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  526. }
  527. byte[] HandleInvite(Dictionary<string, object> request)
  528. {
  529. Dictionary<string, object> result = new Dictionary<string, object>();
  530. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("InviteID"))
  531. {
  532. NullResult(result, "Bad network data");
  533. string xmlString = ServerUtils.BuildXmlResponse(result);
  534. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  535. }
  536. else
  537. {
  538. string op = request["OP"].ToString();
  539. if (op == "ADD" && request.ContainsKey("GroupID") && request.ContainsKey("RoleID") && request.ContainsKey("AgentID"))
  540. {
  541. bool success = m_GroupsService.AddAgentToGroupInvite(request["RequestingAgentID"].ToString(),
  542. new UUID(request["InviteID"].ToString()), new UUID(request["GroupID"].ToString()),
  543. new UUID(request["RoleID"].ToString()), request["AgentID"].ToString());
  544. result["RESULT"] = success.ToString();
  545. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  546. }
  547. else if (op == "DELETE")
  548. {
  549. m_GroupsService.RemoveAgentToGroupInvite(request["RequestingAgentID"].ToString(), new UUID(request["InviteID"].ToString()));
  550. result["RESULT"] = "true";
  551. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  552. }
  553. else if (op == "GET")
  554. {
  555. GroupInviteInfo invite = m_GroupsService.GetAgentToGroupInvite(request["RequestingAgentID"].ToString(),
  556. new UUID(request["InviteID"].ToString()));
  557. if (invite != null)
  558. result["RESULT"] = GroupsDataUtils.GroupInviteInfo(invite);
  559. else
  560. result["RESULT"] = "NULL";
  561. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  562. }
  563. NullResult(result, "Bad OP in request");
  564. return Util.UTF8NoBomEncoding.GetBytes(ServerUtils.BuildXmlResponse(result));
  565. }
  566. }
  567. byte[] HandleAddNotice(Dictionary<string, object> request)
  568. {
  569. Dictionary<string, object> result = new Dictionary<string, object>();
  570. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("NoticeID") ||
  571. !request.ContainsKey("FromName") || !request.ContainsKey("Subject") || !request.ContainsKey("Message") ||
  572. !request.ContainsKey("HasAttachment"))
  573. NullResult(result, "Bad network data");
  574. else
  575. {
  576. bool hasAtt = bool.Parse(request["HasAttachment"].ToString());
  577. byte attType = 0;
  578. string attName = string.Empty;
  579. string attOwner = string.Empty;
  580. UUID attItem = UUID.Zero;
  581. if (request.ContainsKey("AttachmentType"))
  582. attType = byte.Parse(request["AttachmentType"].ToString());
  583. if (request.ContainsKey("AttachmentName"))
  584. attName = request["AttachmentName"].ToString();
  585. if (request.ContainsKey("AttachmentItemID"))
  586. attItem = new UUID(request["AttachmentItemID"].ToString());
  587. if (request.ContainsKey("AttachmentOwnerID"))
  588. attOwner = request["AttachmentOwnerID"].ToString();
  589. bool success = m_GroupsService.AddGroupNotice(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()),
  590. new UUID(request["NoticeID"].ToString()), request["FromName"].ToString(), request["Subject"].ToString(),
  591. request["Message"].ToString(), hasAtt, attType, attName, attItem, attOwner);
  592. result["RESULT"] = success.ToString();
  593. }
  594. string xmlString = ServerUtils.BuildXmlResponse(result);
  595. //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
  596. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  597. }
  598. byte[] HandleGetNotices(Dictionary<string, object> request)
  599. {
  600. Dictionary<string, object> result = new Dictionary<string, object>();
  601. if (!request.ContainsKey("RequestingAgentID"))
  602. NullResult(result, "Bad network data");
  603. else if (request.ContainsKey("NoticeID")) // just one
  604. {
  605. GroupNoticeInfo notice = m_GroupsService.GetGroupNotice(request["RequestingAgentID"].ToString(), new UUID(request["NoticeID"].ToString()));
  606. if (notice == null)
  607. NullResult(result, "NO such notice");
  608. else
  609. result["RESULT"] = GroupsDataUtils.GroupNoticeInfo(notice);
  610. }
  611. else if (request.ContainsKey("GroupID")) // all notices for group
  612. {
  613. List<ExtendedGroupNoticeData> notices = m_GroupsService.GetGroupNotices(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()));
  614. if (notices == null || (notices != null && notices.Count == 0))
  615. NullResult(result, "No notices");
  616. else
  617. {
  618. Dictionary<string, object> dict = new Dictionary<string, object>();
  619. int i = 0;
  620. foreach (ExtendedGroupNoticeData n in notices)
  621. dict["n-" + i++] = GroupsDataUtils.GroupNoticeData(n);
  622. result["RESULT"] = dict;
  623. }
  624. }
  625. else
  626. NullResult(result, "Bad OP in request");
  627. string xmlString = ServerUtils.BuildXmlResponse(result);
  628. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  629. }
  630. byte[] HandleFindGroups(Dictionary<string, object> request)
  631. {
  632. Dictionary<string, object> result = new Dictionary<string, object>();
  633. if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("Query"))
  634. NullResult(result, "Bad network data");
  635. List<DirGroupsReplyData> hits = m_GroupsService.FindGroups(request["RequestingAgentID"].ToString(), request["Query"].ToString());
  636. if (hits == null || (hits != null && hits.Count == 0))
  637. NullResult(result, "No hits");
  638. else
  639. {
  640. Dictionary<string, object> dict = new Dictionary<string, object>();
  641. int i = 0;
  642. foreach (DirGroupsReplyData n in hits)
  643. dict["n-" + i++] = GroupsDataUtils.DirGroupsReplyData(n);
  644. result["RESULT"] = dict;
  645. }
  646. string xmlString = ServerUtils.BuildXmlResponse(result);
  647. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  648. }
  649. #region Helpers
  650. private void NullResult(Dictionary<string, object> result, string reason)
  651. {
  652. result["RESULT"] = "NULL";
  653. result["REASON"] = reason;
  654. }
  655. private byte[] FailureResult()
  656. {
  657. Dictionary<string, object> result = new Dictionary<string, object>();
  658. NullResult(result, "Unknown method");
  659. string xmlString = ServerUtils.BuildXmlResponse(result);
  660. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  661. }
  662. private byte[] FailureResult(string reason)
  663. {
  664. Dictionary<string, object> result = new Dictionary<string, object>();
  665. NullResult(result, reason);
  666. string xmlString = ServerUtils.BuildXmlResponse(result);
  667. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  668. }
  669. #endregion
  670. }
  671. }