diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs new file mode 100644 index 0000000..b61e848 --- /dev/null +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/XMuteModule.cs | |||
@@ -0,0 +1,239 @@ | |||
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 System.Text; | ||
31 | using log4net; | ||
32 | using Nini.Config; | ||
33 | using OpenMetaverse; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Region.Framework.Interfaces; | ||
36 | using OpenSim.Region.Framework.Scenes; | ||
37 | using Mono.Addins; | ||
38 | using OpenSim.Data.MySQL; | ||
39 | using MySql.Data.MySqlClient; | ||
40 | |||
41 | |||
42 | namespace OpenSim.Region.CoreModules.Avatar.InstantMessage | ||
43 | { | ||
44 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XMute")] | ||
45 | public class XMuteModule : ISharedRegionModule | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger( | ||
48 | MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | protected bool m_Enabled = true; | ||
51 | protected List<Scene> m_SceneList = new List<Scene>(); | ||
52 | protected MuteTableHandler m_MuteTable; | ||
53 | protected string m_DatabaseConnect; | ||
54 | |||
55 | public void Initialise(IConfigSource config) | ||
56 | { | ||
57 | IConfig cnf = config.Configs["Messaging"]; | ||
58 | if (cnf == null) | ||
59 | { | ||
60 | m_Enabled = false; | ||
61 | return; | ||
62 | } | ||
63 | |||
64 | if (cnf.GetString("MuteListModule", "None") != | ||
65 | "XMute") | ||
66 | { | ||
67 | m_Enabled = false; | ||
68 | return; | ||
69 | } | ||
70 | |||
71 | m_DatabaseConnect = cnf.GetString("MuteDatabaseConnect", String.Empty); | ||
72 | if (m_DatabaseConnect == String.Empty) | ||
73 | { | ||
74 | m_log.Debug("[XMute]: MuteDatabaseConnect missing or empty"); | ||
75 | m_Enabled = false; | ||
76 | return; | ||
77 | } | ||
78 | |||
79 | m_MuteTable = new MuteTableHandler( | ||
80 | m_DatabaseConnect, "XMute", String.Empty); | ||
81 | } | ||
82 | |||
83 | public void AddRegion(Scene scene) | ||
84 | { | ||
85 | if (!m_Enabled) | ||
86 | return; | ||
87 | |||
88 | lock (m_SceneList) | ||
89 | { | ||
90 | m_SceneList.Add(scene); | ||
91 | |||
92 | scene.EventManager.OnNewClient += OnNewClient; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | public void RegionLoaded(Scene scene) | ||
97 | { | ||
98 | } | ||
99 | |||
100 | public void RemoveRegion(Scene scene) | ||
101 | { | ||
102 | if (!m_Enabled) | ||
103 | return; | ||
104 | |||
105 | lock (m_SceneList) | ||
106 | { | ||
107 | m_SceneList.Remove(scene); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | public void PostInitialise() | ||
112 | { | ||
113 | if (!m_Enabled) | ||
114 | return; | ||
115 | |||
116 | m_log.Debug("[XMute]: Mute list enabled"); | ||
117 | } | ||
118 | |||
119 | public string Name | ||
120 | { | ||
121 | get { return "XMuteModule"; } | ||
122 | } | ||
123 | |||
124 | public Type ReplaceableInterface | ||
125 | { | ||
126 | get { return null; } | ||
127 | } | ||
128 | |||
129 | public void Close() | ||
130 | { | ||
131 | } | ||
132 | |||
133 | private void OnNewClient(IClientAPI client) | ||
134 | { | ||
135 | client.OnMuteListRequest += OnMuteListRequest; | ||
136 | client.OnUpdateMuteListEntry += OnUpdateMuteListEntry; | ||
137 | client.OnRemoveMuteListEntry += OnRemoveMuteListEntry; | ||
138 | } | ||
139 | |||
140 | private void OnMuteListRequest(IClientAPI client, uint crc) | ||
141 | { | ||
142 | string filename = "mutes"+client.AgentId.ToString(); | ||
143 | |||
144 | IXfer xfer = client.Scene.RequestModuleInterface<IXfer>(); | ||
145 | if (xfer != null) | ||
146 | { | ||
147 | MuteData[] data = m_MuteTable.Get("AgentID", client.AgentId.ToString()); | ||
148 | if (data == null || data.Length == 0) | ||
149 | { | ||
150 | xfer.AddNewFile(filename, new Byte[0]); | ||
151 | } | ||
152 | else | ||
153 | { | ||
154 | StringBuilder sb = new StringBuilder(1024); | ||
155 | |||
156 | foreach (MuteData d in data) | ||
157 | sb.AppendFormat("{0} {1} {2}|{3}\n", | ||
158 | d.MuteType, | ||
159 | d.MuteID.ToString(), | ||
160 | d.MuteName, | ||
161 | d.MuteFlags); | ||
162 | |||
163 | Byte[] filedata = Util.UTF8.GetBytes(sb.ToString()); | ||
164 | |||
165 | uint dataCrc = Crc32.Compute(filedata); | ||
166 | |||
167 | if (dataCrc == crc) | ||
168 | { | ||
169 | client.SendUseCachedMuteList(); | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | xfer.AddNewFile(filename, filedata); | ||
174 | } | ||
175 | |||
176 | client.SendMuteListUpdate(filename); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | private void OnUpdateMuteListEntry(IClientAPI client, UUID muteID, string muteName, int muteType, uint muteFlags) | ||
181 | { | ||
182 | MuteData mute = new MuteData(); | ||
183 | |||
184 | mute.AgentID = client.AgentId; | ||
185 | mute.MuteID = muteID; | ||
186 | mute.MuteName = muteName; | ||
187 | mute.MuteType = muteType; | ||
188 | mute.MuteFlags = (int)muteFlags; | ||
189 | mute.Stamp = Util.UnixTimeSinceEpoch(); | ||
190 | |||
191 | m_MuteTable.Store(mute); | ||
192 | } | ||
193 | |||
194 | private void OnRemoveMuteListEntry(IClientAPI client, UUID muteID, string muteName) | ||
195 | { | ||
196 | m_MuteTable.Delete(new string[] { "AgentID", | ||
197 | "MuteID", | ||
198 | "MuteName" }, | ||
199 | new string[] { client.AgentId.ToString(), | ||
200 | muteID.ToString(), | ||
201 | muteName }); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | public class MuteTableHandler : MySQLGenericTableHandler<MuteData> | ||
206 | { | ||
207 | public MuteTableHandler(string conn, string realm, string m) : base(conn, realm, m) | ||
208 | { | ||
209 | } | ||
210 | |||
211 | public bool Delete(string[] fields, string[] val) | ||
212 | { | ||
213 | if (fields.Length != val.Length) | ||
214 | return false; | ||
215 | |||
216 | using (MySqlCommand cmd = new MySqlCommand()) | ||
217 | { | ||
218 | string text = String.Format("delete from {0} where ", m_Realm); | ||
219 | |||
220 | List<string> terms = new List<string>(); | ||
221 | |||
222 | for (int i = 0 ; i < fields.Length ; i++) | ||
223 | { | ||
224 | terms.Add(String.Format("{0} = ?{0}", fields[i])); | ||
225 | cmd.Parameters.AddWithValue("?" + fields[i], val[i]); | ||
226 | } | ||
227 | |||
228 | text += string.Join(" and ", terms.ToArray()); | ||
229 | |||
230 | cmd.CommandText = text; | ||
231 | |||
232 | if (ExecuteNonQuery(cmd) > 0) | ||
233 | return true; | ||
234 | return false; | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | } | ||
239 | |||