opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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.Reflection; |
||
30 | using log4net; |
||
31 | using Mono.Addins; |
||
32 | using Nini.Config; |
||
33 | using OpenMetaverse; |
||
34 | using OpenSim.Framework; |
||
35 | using OpenSim.Framework.Communications; |
||
36 | using OpenSim.Framework.Servers; |
||
37 | using OpenSim.Framework.Client; |
||
38 | using OpenSim.Region.Framework.Interfaces; |
||
39 | using OpenSim.Region.Framework.Scenes; |
||
40 | |||
41 | namespace OpenSim.Region.CoreModules.Avatar.InstantMessage |
||
42 | { |
||
43 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "OfflineMessageModule")] |
||
44 | public class OfflineMessageModule : ISharedRegionModule |
||
45 | { |
||
46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
47 | |||
48 | private bool enabled = true; |
||
49 | private List<Scene> m_SceneList = new List<Scene>(); |
||
50 | private string m_RestURL = String.Empty; |
||
51 | IMessageTransferModule m_TransferModule = null; |
||
52 | private bool m_ForwardOfflineGroupMessages = true; |
||
53 | |||
54 | public void Initialise(IConfigSource config) |
||
55 | { |
||
56 | IConfig cnf = config.Configs["Messaging"]; |
||
57 | if (cnf == null) |
||
58 | { |
||
59 | enabled = false; |
||
60 | return; |
||
61 | } |
||
62 | if (cnf != null && cnf.GetString("OfflineMessageModule", "None") != |
||
63 | "OfflineMessageModule") |
||
64 | { |
||
65 | enabled = false; |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | m_RestURL = cnf.GetString("OfflineMessageURL", ""); |
||
70 | if (m_RestURL == "") |
||
71 | { |
||
72 | m_log.Error("[OFFLINE MESSAGING] Module was enabled, but no URL is given, disabling"); |
||
73 | enabled = false; |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", m_ForwardOfflineGroupMessages); |
||
78 | } |
||
79 | |||
80 | public void AddRegion(Scene scene) |
||
81 | { |
||
82 | if (!enabled) |
||
83 | return; |
||
84 | |||
85 | lock (m_SceneList) |
||
86 | { |
||
87 | m_SceneList.Add(scene); |
||
88 | |||
89 | scene.EventManager.OnNewClient += OnNewClient; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public void RegionLoaded(Scene scene) |
||
94 | { |
||
95 | if (!enabled) |
||
96 | return; |
||
97 | |||
98 | if (m_TransferModule == null) |
||
99 | { |
||
100 | m_TransferModule = scene.RequestModuleInterface<IMessageTransferModule>(); |
||
101 | if (m_TransferModule == null) |
||
102 | { |
||
103 | scene.EventManager.OnNewClient -= OnNewClient; |
||
104 | |||
105 | enabled = false; |
||
106 | m_SceneList.Clear(); |
||
107 | |||
108 | m_log.Error("[OFFLINE MESSAGING] No message transfer module is enabled. Diabling offline messages"); |
||
109 | } |
||
110 | m_TransferModule.OnUndeliveredMessage += UndeliveredMessage; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | public void RemoveRegion(Scene scene) |
||
115 | { |
||
116 | if (!enabled) |
||
117 | return; |
||
118 | |||
119 | lock (m_SceneList) |
||
120 | { |
||
121 | m_SceneList.Remove(scene); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | public void PostInitialise() |
||
126 | { |
||
127 | if (!enabled) |
||
128 | return; |
||
129 | |||
130 | m_log.Debug("[OFFLINE MESSAGING] Offline messages enabled"); |
||
131 | } |
||
132 | |||
133 | public string Name |
||
134 | { |
||
135 | get { return "OfflineMessageModule"; } |
||
136 | } |
||
137 | |||
138 | public Type ReplaceableInterface |
||
139 | { |
||
140 | get { return null; } |
||
141 | } |
||
142 | |||
143 | public void Close() |
||
144 | { |
||
145 | } |
||
146 | |||
147 | private Scene FindScene(UUID agentID) |
||
148 | { |
||
149 | foreach (Scene s in m_SceneList) |
||
150 | { |
||
151 | ScenePresence presence = s.GetScenePresence(agentID); |
||
152 | if (presence != null && !presence.IsChildAgent) |
||
153 | return s; |
||
154 | } |
||
155 | return null; |
||
156 | } |
||
157 | |||
158 | private IClientAPI FindClient(UUID agentID) |
||
159 | { |
||
160 | foreach (Scene s in m_SceneList) |
||
161 | { |
||
162 | ScenePresence presence = s.GetScenePresence(agentID); |
||
163 | if (presence != null && !presence.IsChildAgent) |
||
164 | return presence.ControllingClient; |
||
165 | } |
||
166 | return null; |
||
167 | } |
||
168 | |||
169 | private void OnNewClient(IClientAPI client) |
||
170 | { |
||
171 | client.OnRetrieveInstantMessages += RetrieveInstantMessages; |
||
172 | } |
||
173 | |||
174 | private void RetrieveInstantMessages(IClientAPI client) |
||
175 | { |
||
176 | if (m_RestURL != "") |
||
177 | { |
||
178 | m_log.DebugFormat("[OFFLINE MESSAGING]: Retrieving stored messages for {0}", client.AgentId); |
||
179 | |||
180 | List<GridInstantMessage> msglist |
||
181 | = SynchronousRestObjectRequester.MakeRequest<UUID, List<GridInstantMessage>>( |
||
182 | "POST", m_RestURL + "/RetrieveMessages/", client.AgentId); |
||
183 | |||
184 | if (msglist == null) |
||
185 | { |
||
186 | m_log.WarnFormat("[OFFLINE MESSAGING]: WARNING null message list."); |
||
187 | return; |
||
188 | } |
||
189 | |||
190 | foreach (GridInstantMessage im in msglist) |
||
191 | { |
||
192 | if (im.dialog == (byte)InstantMessageDialog.InventoryOffered) |
||
193 | // send it directly or else the item will be given twice |
||
194 | client.SendInstantMessage(im); |
||
195 | else |
||
196 | { |
||
197 | // Send through scene event manager so all modules get a chance |
||
198 | // to look at this message before it gets delivered. |
||
199 | // |
||
200 | // Needed for proper state management for stored group |
||
201 | // invitations |
||
202 | // |
||
203 | Scene s = FindScene(client.AgentId); |
||
204 | if (s != null) |
||
205 | s.EventManager.TriggerIncomingInstantMessage(im); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | private void UndeliveredMessage(GridInstantMessage im) |
||
212 | { |
||
213 | if (im.dialog != (byte)InstantMessageDialog.MessageFromObject && |
||
214 | im.dialog != (byte)InstantMessageDialog.MessageFromAgent && |
||
215 | im.dialog != (byte)InstantMessageDialog.GroupNotice && |
||
216 | im.dialog != (byte)InstantMessageDialog.GroupInvitation && |
||
217 | im.dialog != (byte)InstantMessageDialog.InventoryOffered) |
||
218 | { |
||
219 | return; |
||
220 | } |
||
221 | |||
222 | if (!m_ForwardOfflineGroupMessages) |
||
223 | { |
||
224 | if (im.dialog == (byte)InstantMessageDialog.GroupNotice || |
||
225 | im.dialog == (byte)InstantMessageDialog.GroupInvitation) |
||
226 | return; |
||
227 | } |
||
228 | |||
229 | Scene scene = FindScene(new UUID(im.fromAgentID)); |
||
230 | if (scene == null) |
||
231 | scene = m_SceneList[0]; |
||
232 | |||
233 | bool success = SynchronousRestObjectRequester.MakeRequest<GridInstantMessage, bool>( |
||
234 | "POST", m_RestURL+"/SaveMessage/", im); |
||
235 | |||
236 | if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent) |
||
237 | { |
||
238 | IClientAPI client = FindClient(new UUID(im.fromAgentID)); |
||
239 | if (client == null) |
||
240 | return; |
||
241 | |||
242 | client.SendInstantMessage(new GridInstantMessage( |
||
243 | null, new UUID(im.toAgentID), |
||
244 | "System", new UUID(im.fromAgentID), |
||
245 | (byte)InstantMessageDialog.MessageFromAgent, |
||
246 | "User is not logged in. "+ |
||
247 | (success ? "Message saved." : "Message not saved"), |
||
248 | false, new Vector3())); |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 |