diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Hypergrid/HGStandaloneInventoryService.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Hypergrid/HGStandaloneInventoryService.cs | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Hypergrid/HGStandaloneInventoryService.cs b/OpenSim/Region/CoreModules/Hypergrid/HGStandaloneInventoryService.cs new file mode 100644 index 0000000..dfc859e --- /dev/null +++ b/OpenSim/Region/CoreModules/Hypergrid/HGStandaloneInventoryService.cs | |||
@@ -0,0 +1,314 @@ | |||
1 | /** | ||
2 | * Copyright (c) 2008, Contributors. All rights reserved. | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without modification, | ||
6 | * are permitted provided that the following conditions are met: | ||
7 | * | ||
8 | * * Redistributions of source code must retain the above copyright notice, | ||
9 | * this list of conditions and the following disclaimer. | ||
10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
11 | * this list of conditions and the following disclaimer in the documentation | ||
12 | * and/or other materials provided with the distribution. | ||
13 | * * Neither the name of the Organizations nor the names of Individual | ||
14 | * Contributors may be used to endorse or promote products derived from | ||
15 | * this software without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
20 | * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
22 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
24 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
25 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Net; | ||
32 | using System.Reflection; | ||
33 | using log4net; | ||
34 | using Nini.Config; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Framework.Communications; | ||
38 | using OpenSim.Framework.Communications.Cache; | ||
39 | using OpenSim.Framework.Servers; | ||
40 | using OpenSim.Region.Framework.Interfaces; | ||
41 | using OpenSim.Region.Framework.Scenes; | ||
42 | |||
43 | namespace OpenSim.Region.CoreModules.Hypergrid | ||
44 | { | ||
45 | public class HGStandaloneInventoryService : IRegionModule | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | private static bool initialized = false; | ||
49 | private static bool enabled = false; | ||
50 | |||
51 | Scene m_scene; | ||
52 | //InventoryService m_inventoryService; | ||
53 | |||
54 | #region IRegionModule interface | ||
55 | |||
56 | public void Initialise(Scene scene, IConfigSource config) | ||
57 | { | ||
58 | if (!initialized) | ||
59 | { | ||
60 | initialized = true; | ||
61 | m_scene = scene; | ||
62 | |||
63 | // This module is only on for standalones | ||
64 | enabled = !config.Configs["Startup"].GetBoolean("gridmode", true) && config.Configs["Startup"].GetBoolean("hypergrid", false); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | public void PostInitialise() | ||
69 | { | ||
70 | if (enabled) | ||
71 | { | ||
72 | m_log.Info("[HGStandaloneInvService]: Starting..."); | ||
73 | //m_inventoryService = new InventoryService(m_scene); | ||
74 | new InventoryService(m_scene); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | public void Close() | ||
79 | { | ||
80 | } | ||
81 | |||
82 | public string Name | ||
83 | { | ||
84 | get { return "HGStandaloneInventoryService"; } | ||
85 | } | ||
86 | |||
87 | public bool IsSharedModule | ||
88 | { | ||
89 | get { return true; } | ||
90 | } | ||
91 | |||
92 | #endregion | ||
93 | } | ||
94 | |||
95 | public class InventoryService | ||
96 | { | ||
97 | private static readonly ILog m_log | ||
98 | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
99 | |||
100 | private InventoryServiceBase m_inventoryService; | ||
101 | private IUserService m_userService; | ||
102 | private bool m_doLookup = false; | ||
103 | |||
104 | public bool DoLookup | ||
105 | { | ||
106 | get { return m_doLookup; } | ||
107 | set { m_doLookup = value; } | ||
108 | } | ||
109 | |||
110 | public InventoryService(Scene m_scene) | ||
111 | { | ||
112 | m_inventoryService = (InventoryServiceBase)m_scene.CommsManager.SecureInventoryService; | ||
113 | m_userService = m_scene.CommsManager.UserService; | ||
114 | AddHttpHandlers(m_scene); | ||
115 | } | ||
116 | |||
117 | protected void AddHttpHandlers(Scene m_scene) | ||
118 | { | ||
119 | BaseHttpServer httpServer = m_scene.CommsManager.HttpServer; | ||
120 | |||
121 | httpServer.AddStreamHandler( | ||
122 | new RestDeserialiseSecureHandler<Guid, InventoryCollection>( | ||
123 | "POST", "/GetInventory/", GetUserInventory, CheckAuthSession)); | ||
124 | |||
125 | httpServer.AddStreamHandler( | ||
126 | new RestDeserialiseSecureHandler<InventoryFolderBase, bool>( | ||
127 | "POST", "/NewFolder/", m_inventoryService.AddFolder, CheckAuthSession)); | ||
128 | |||
129 | httpServer.AddStreamHandler( | ||
130 | new RestDeserialiseSecureHandler<InventoryFolderBase, bool>( | ||
131 | "POST", "/UpdateFolder/", m_inventoryService.UpdateFolder, CheckAuthSession)); | ||
132 | |||
133 | httpServer.AddStreamHandler( | ||
134 | new RestDeserialiseSecureHandler<InventoryFolderBase, bool>( | ||
135 | "POST", "/MoveFolder/", m_inventoryService.MoveFolder, CheckAuthSession)); | ||
136 | |||
137 | httpServer.AddStreamHandler( | ||
138 | new RestDeserialiseSecureHandler<InventoryFolderBase, bool>( | ||
139 | "POST", "/PurgeFolder/", m_inventoryService.PurgeFolder, CheckAuthSession)); | ||
140 | |||
141 | httpServer.AddStreamHandler( | ||
142 | new RestDeserialiseSecureHandler<InventoryItemBase, bool>( | ||
143 | "POST", "/NewItem/", m_inventoryService.AddItem, CheckAuthSession)); | ||
144 | |||
145 | httpServer.AddStreamHandler( | ||
146 | new RestDeserialiseSecureHandler<InventoryItemBase, bool>( | ||
147 | "POST", "/DeleteItem/", m_inventoryService.DeleteItem, CheckAuthSession)); | ||
148 | |||
149 | //// WARNING: Root folders no longer just delivers the root and immediate child folders (e.g | ||
150 | //// system folders such as Objects, Textures), but it now returns the entire inventory skeleton. | ||
151 | //// It would have been better to rename this request, but complexities in the BaseHttpServer | ||
152 | //// (e.g. any http request not found is automatically treated as an xmlrpc request) make it easier | ||
153 | //// to do this for now. | ||
154 | //m_scene.AddStreamHandler( | ||
155 | // new RestDeserialiseTrustedHandler<Guid, List<InventoryFolderBase>> | ||
156 | // ("POST", "/RootFolders/", GetInventorySkeleton, CheckTrustSource)); | ||
157 | |||
158 | //// for persistent active gestures | ||
159 | //m_scene.AddStreamHandler( | ||
160 | // new RestDeserialiseTrustedHandler<Guid, List<InventoryItemBase>> | ||
161 | // ("POST", "/ActiveGestures/", GetActiveGestures, CheckTrustSource)); | ||
162 | } | ||
163 | |||
164 | |||
165 | ///// <summary> | ||
166 | ///// Check that the source of an inventory request is one that we trust. | ||
167 | ///// </summary> | ||
168 | ///// <param name="peer"></param> | ||
169 | ///// <returns></returns> | ||
170 | //public bool CheckTrustSource(IPEndPoint peer) | ||
171 | //{ | ||
172 | // if (m_doLookup) | ||
173 | // { | ||
174 | // m_log.InfoFormat("[GRID AGENT INVENTORY]: Checking trusted source {0}", peer); | ||
175 | // UriBuilder ub = new UriBuilder(m_userserver_url); | ||
176 | // IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host); | ||
177 | // foreach (IPAddress uaddr in uaddrs) | ||
178 | // { | ||
179 | // if (uaddr.Equals(peer.Address)) | ||
180 | // { | ||
181 | // return true; | ||
182 | // } | ||
183 | // } | ||
184 | |||
185 | // m_log.WarnFormat( | ||
186 | // "[GRID AGENT INVENTORY]: Rejecting request since source {0} was not in the list of trusted sources", | ||
187 | // peer); | ||
188 | |||
189 | // return false; | ||
190 | // } | ||
191 | // else | ||
192 | // { | ||
193 | // return true; | ||
194 | // } | ||
195 | //} | ||
196 | |||
197 | /// <summary> | ||
198 | /// Check that the source of an inventory request for a particular agent is a current session belonging to | ||
199 | /// that agent. | ||
200 | /// </summary> | ||
201 | /// <param name="session_id"></param> | ||
202 | /// <param name="avatar_id"></param> | ||
203 | /// <returns></returns> | ||
204 | public bool CheckAuthSession(string session_id, string avatar_id) | ||
205 | { | ||
206 | if (m_doLookup) | ||
207 | { | ||
208 | m_log.InfoFormat("[HGStandaloneInvService]: checking authed session {0} {1}", session_id, avatar_id); | ||
209 | UUID userID = UUID.Zero; | ||
210 | UUID sessionID = UUID.Zero; | ||
211 | UUID.TryParse(avatar_id, out userID); | ||
212 | UUID.TryParse(session_id, out sessionID); | ||
213 | if (userID.Equals(UUID.Zero) || sessionID.Equals(UUID.Zero)) | ||
214 | { | ||
215 | m_log.Info("[HGStandaloneInvService]: Invalid user or session id " + avatar_id + "; " + session_id); | ||
216 | return false; | ||
217 | } | ||
218 | UserProfileData userProfile = m_userService.GetUserProfile(userID); | ||
219 | if (userProfile != null && userProfile.CurrentAgent != null && | ||
220 | userProfile.CurrentAgent.SessionID == sessionID) | ||
221 | { | ||
222 | m_log.Info("[HGStandaloneInvService]: user is logged in and session is valid. Authorizing access."); | ||
223 | return true; | ||
224 | } | ||
225 | |||
226 | m_log.Warn("[HGStandaloneInvService]: unknown user or session_id, request rejected"); | ||
227 | return false; | ||
228 | } | ||
229 | else | ||
230 | { | ||
231 | return true; | ||
232 | } | ||
233 | } | ||
234 | |||
235 | |||
236 | /// <summary> | ||
237 | /// Return a user's entire inventory | ||
238 | /// </summary> | ||
239 | /// <param name="rawUserID"></param> | ||
240 | /// <returns>The user's inventory. If an inventory cannot be found then an empty collection is returned.</returns> | ||
241 | public InventoryCollection GetUserInventory(Guid rawUserID) | ||
242 | { | ||
243 | UUID userID = new UUID(rawUserID); | ||
244 | |||
245 | m_log.Info("[HGStandaloneInvService]: Processing request for inventory of " + userID); | ||
246 | |||
247 | // Uncomment me to simulate a slow responding inventory server | ||
248 | //Thread.Sleep(16000); | ||
249 | |||
250 | InventoryCollection invCollection = new InventoryCollection(); | ||
251 | |||
252 | List<InventoryFolderBase> allFolders = ((InventoryServiceBase)m_inventoryService).GetInventorySkeleton(userID); | ||
253 | |||
254 | if (null == allFolders) | ||
255 | { | ||
256 | m_log.WarnFormat("[HGStandaloneInvService]: No inventory found for user {0}", rawUserID); | ||
257 | |||
258 | return invCollection; | ||
259 | } | ||
260 | |||
261 | List<InventoryItemBase> allItems = new List<InventoryItemBase>(); | ||
262 | |||
263 | foreach (InventoryFolderBase folder in allFolders) | ||
264 | { | ||
265 | List<InventoryItemBase> items = ((InventoryServiceBase)m_inventoryService).RequestFolderItems(folder.ID); | ||
266 | |||
267 | if (items != null) | ||
268 | { | ||
269 | allItems.InsertRange(0, items); | ||
270 | } | ||
271 | } | ||
272 | |||
273 | invCollection.UserID = userID; | ||
274 | invCollection.Folders = allFolders; | ||
275 | invCollection.Items = allItems; | ||
276 | |||
277 | // foreach (InventoryFolderBase folder in invCollection.Folders) | ||
278 | // { | ||
279 | // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back folder {0} {1}", folder.Name, folder.ID); | ||
280 | // } | ||
281 | // | ||
282 | // foreach (InventoryItemBase item in invCollection.Items) | ||
283 | // { | ||
284 | // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back item {0} {1}, folder {2}", item.Name, item.ID, item.Folder); | ||
285 | // } | ||
286 | |||
287 | m_log.InfoFormat( | ||
288 | "[HGStandaloneInvService]: Sending back inventory response to user {0} containing {1} folders and {2} items", | ||
289 | invCollection.UserID, invCollection.Folders.Count, invCollection.Items.Count); | ||
290 | |||
291 | return invCollection; | ||
292 | } | ||
293 | |||
294 | /// <summary> | ||
295 | /// Guid to UUID wrapper for same name IInventoryServices method | ||
296 | /// </summary> | ||
297 | /// <param name="rawUserID"></param> | ||
298 | /// <returns></returns> | ||
299 | public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID) | ||
300 | { | ||
301 | UUID userID = new UUID(rawUserID); | ||
302 | return ((InventoryServiceBase)m_inventoryService).GetInventorySkeleton(userID); | ||
303 | } | ||
304 | |||
305 | public List<InventoryItemBase> GetActiveGestures(Guid rawUserID) | ||
306 | { | ||
307 | UUID userID = new UUID(rawUserID); | ||
308 | |||
309 | m_log.InfoFormat("[HGStandaloneInvService]: fetching active gestures for user {0}", userID); | ||
310 | |||
311 | return ((InventoryServiceBase)m_inventoryService).GetActiveGestures(userID); | ||
312 | } | ||
313 | } | ||
314 | } | ||