opensim – Blame information for rev 1

Subversion Repositories:
Rev:
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  
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Text;
33 using log4net;
34 using OpenMetaverse;
35 using OpenSim.Framework;
36 using OpenSim.Data;
37  
38 namespace OpenSim.Data.Null
39 {
40 public class NullUserAccountData : IUserAccountData
41 {
42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43  
44 private Dictionary<UUID, UserAccountData> m_DataByUUID = new Dictionary<UUID, UserAccountData>();
45 private Dictionary<string, UserAccountData> m_DataByName = new Dictionary<string, UserAccountData>();
46 private Dictionary<string, UserAccountData> m_DataByEmail = new Dictionary<string, UserAccountData>();
47  
48 public NullUserAccountData(string connectionString, string realm)
49 {
50 // m_log.DebugFormat(
51 // "[NULL USER ACCOUNT DATA]: Initializing new NullUserAccountData with connectionString [{0}], realm [{1}]",
52 // connectionString, realm);
53 }
54  
55 /// <summary>
56 /// Tries to implement the Get [] semantics, but it cuts corners like crazy.
57 /// Specifically, it relies on the knowledge that the only Gets used are
58 /// keyed on PrincipalID, Email, and FirstName+LastName.
59 /// </summary>
60 /// <param name="fields"></param>
61 /// <param name="values"></param>
62 /// <returns></returns>
63 public UserAccountData[] Get(string[] fields, string[] values)
64 {
65 // if (m_log.IsDebugEnabled)
66 // {
67 // m_log.DebugFormat(
68 // "[NULL USER ACCOUNT DATA]: Called Get with fields [{0}], values [{1}]",
69 // string.Join(", ", fields), string.Join(", ", values));
70 // }
71  
72 UserAccountData[] userAccounts = new UserAccountData[0];
73  
74 List<string> fieldsLst = new List<string>(fields);
75 if (fieldsLst.Contains("PrincipalID"))
76 {
77 int i = fieldsLst.IndexOf("PrincipalID");
78 UUID id = UUID.Zero;
79 if (UUID.TryParse(values[i], out id))
80 if (m_DataByUUID.ContainsKey(id))
81 userAccounts = new UserAccountData[] { m_DataByUUID[id] };
82 }
83 else if (fieldsLst.Contains("FirstName") && fieldsLst.Contains("LastName"))
84 {
85 int findex = fieldsLst.IndexOf("FirstName");
86 int lindex = fieldsLst.IndexOf("LastName");
87 if (m_DataByName.ContainsKey(values[findex] + " " + values[lindex]))
88 {
89 userAccounts = new UserAccountData[] { m_DataByName[values[findex] + " " + values[lindex]] };
90 }
91 }
92 else if (fieldsLst.Contains("Email"))
93 {
94 int i = fieldsLst.IndexOf("Email");
95 if (m_DataByEmail.ContainsKey(values[i]))
96 userAccounts = new UserAccountData[] { m_DataByEmail[values[i]] };
97 }
98  
99 // if (m_log.IsDebugEnabled)
100 // {
101 // StringBuilder sb = new StringBuilder();
102 // foreach (UserAccountData uad in userAccounts)
103 // sb.AppendFormat("({0} {1} {2}) ", uad.FirstName, uad.LastName, uad.PrincipalID);
104 //
105 // m_log.DebugFormat(
106 // "[NULL USER ACCOUNT DATA]: Returning {0} user accounts out of {1}: [{2}]", userAccounts.Length, m_DataByName.Count, sb);
107 // }
108  
109 return userAccounts;
110 }
111  
112 public bool Store(UserAccountData data)
113 {
114 if (data == null)
115 return false;
116  
117 m_log.DebugFormat(
118 "[NULL USER ACCOUNT DATA]: Storing user account {0} {1} {2} {3}",
119 data.FirstName, data.LastName, data.PrincipalID, this.GetHashCode());
120  
121 m_DataByUUID[data.PrincipalID] = data;
122 m_DataByName[data.FirstName + " " + data.LastName] = data;
123 if (data.Data.ContainsKey("Email") && data.Data["Email"] != null && data.Data["Email"] != string.Empty)
124 m_DataByEmail[data.Data["Email"]] = data;
125  
126 // m_log.DebugFormat("m_DataByUUID count is {0}, m_DataByName count is {1}", m_DataByUUID.Count, m_DataByName.Count);
127  
128 return true;
129 }
130  
131 public UserAccountData[] GetUsers(UUID scopeID, string query)
132 {
133 // m_log.DebugFormat(
134 // "[NULL USER ACCOUNT DATA]: Called GetUsers with scope [{0}], query [{1}]", scopeID, query);
135  
136 string[] words = query.Split(new char[] { ' ' });
137  
138 for (int i = 0; i < words.Length; i++)
139 {
140 if (words[i].Length < 3)
141 {
142 if (i != words.Length - 1)
143 Array.Copy(words, i + 1, words, i, words.Length - i - 1);
144 Array.Resize(ref words, words.Length - 1);
145 }
146 }
147  
148 if (words.Length == 0)
149 return new UserAccountData[0];
150  
151 if (words.Length > 2)
152 return new UserAccountData[0];
153  
154 List<string> lst = new List<string>(m_DataByName.Keys);
155 if (words.Length == 1)
156 {
157 lst = lst.FindAll(delegate(string s) { return s.StartsWith(words[0]); });
158 }
159 else
160 {
161 lst = lst.FindAll(delegate(string s) { return s.Contains(words[0]) || s.Contains(words[1]); });
162 }
163  
164 if (lst == null || (lst != null && lst.Count == 0))
165 return new UserAccountData[0];
166  
167 UserAccountData[] result = new UserAccountData[lst.Count];
168 int n = 0;
169 foreach (string key in lst)
170 result[n++] = m_DataByName[key];
171  
172 return result;
173 }
174  
175 public bool Delete(string field, string val)
176 {
177 // Only delete by PrincipalID
178 if (field.Equals("PrincipalID"))
179 {
180 UUID uuid = UUID.Zero;
181 if (UUID.TryParse(val, out uuid) && m_DataByUUID.ContainsKey(uuid))
182 {
183 UserAccountData account = m_DataByUUID[uuid];
184 m_DataByUUID.Remove(uuid);
185 if (m_DataByName.ContainsKey(account.FirstName + " " + account.LastName))
186 m_DataByName.Remove(account.FirstName + " " + account.LastName);
187 if (account.Data.ContainsKey("Email") && account.Data["Email"] != string.Empty && m_DataByEmail.ContainsKey(account.Data["Email"]))
188 m_DataByEmail.Remove(account.Data["Email"]);
189  
190 return true;
191 }
192 }
193  
194 return false;
195 }
196 }
197 }