PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llmessageconfig.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 304 lines | 223 code | 39 blank | 42 comment | 16 complexity | 0983513a421d3ac298b1d148519bd83b MD5 | raw file
  1/** 
  2 * @file llmessageconfig.cpp
  3 * @brief Live file handling for messaging
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "llmessageconfig.h"
 30#include "llfile.h"
 31#include "lllivefile.h"
 32#include "llsd.h"
 33#include "llsdutil.h"
 34#include "llsdserialize.h"
 35#include "message.h"
 36
 37static const char messageConfigFileName[] = "message.xml";
 38static const F32 messageConfigRefreshRate = 5.0; // seconds
 39
 40static std::string sServerName = "";
 41static std::string sConfigDir = "";
 42
 43static std::string sServerDefault;
 44static LLSD sMessages;
 45
 46
 47class LLMessageConfigFile : public LLLiveFile
 48{
 49public:
 50	LLMessageConfigFile() :
 51		LLLiveFile(filename(), messageConfigRefreshRate),
 52		mMaxQueuedEvents(0)
 53            { }
 54
 55	static std::string filename();
 56
 57	LLSD mMessages;
 58	std::string mServerDefault;
 59	
 60	static LLMessageConfigFile& instance();
 61		// return the singleton configuration file
 62
 63	/* virtual */ bool loadFile();
 64	void loadServerDefaults(const LLSD& data);
 65	void loadMaxQueuedEvents(const LLSD& data);
 66	void loadMessages(const LLSD& data);
 67	void loadCapBans(const LLSD& blacklist);
 68	void loadMessageBans(const LLSD& blacklist);
 69	bool isCapBanned(const std::string& cap_name) const;
 70
 71public:
 72	LLSD mCapBans;
 73	S32 mMaxQueuedEvents;
 74
 75private:
 76	static const S32 DEFAULT_MAX_QUEUED_EVENTS = 100;
 77};
 78
 79std::string LLMessageConfigFile::filename()
 80{
 81    std::ostringstream ostr;
 82	ostr << sConfigDir//gAppSimp->getOption("configdir").asString()
 83		<< "/" << messageConfigFileName;
 84	return ostr.str();
 85}
 86
 87LLMessageConfigFile& LLMessageConfigFile::instance()
 88{
 89	static LLMessageConfigFile the_file;
 90	the_file.checkAndReload();
 91	return the_file;
 92}
 93
 94// virtual
 95bool LLMessageConfigFile::loadFile()
 96{
 97	LLSD data;
 98    {
 99        llifstream file(filename());
100        
101        if (file.is_open())
102        {
103			LL_DEBUGS("AppInit") << "Loading message.xml file at " << filename() << LL_ENDL;
104            LLSDSerialize::fromXML(data, file);
105        }
106
107        if (data.isUndefined())
108        {
109            LL_INFOS("AppInit") << "LLMessageConfigFile::loadFile: file missing,"
110				" ill-formed, or simply undefined; not changing the"
111				" file" << LL_ENDL;
112            return false;
113        }
114    }
115	loadServerDefaults(data);
116	loadMaxQueuedEvents(data);
117	loadMessages(data);
118	loadCapBans(data);
119	loadMessageBans(data);
120	return true;
121}
122
123void LLMessageConfigFile::loadServerDefaults(const LLSD& data)
124{
125	mServerDefault = data["serverDefaults"][sServerName].asString();
126}
127
128void LLMessageConfigFile::loadMaxQueuedEvents(const LLSD& data)
129{
130	 if (data.has("maxQueuedEvents"))
131	 {
132		  mMaxQueuedEvents = data["maxQueuedEvents"].asInteger();
133	 }
134	 else
135	 {
136		  mMaxQueuedEvents = DEFAULT_MAX_QUEUED_EVENTS;
137	 }
138}
139
140void LLMessageConfigFile::loadMessages(const LLSD& data)
141{
142	mMessages = data["messages"];
143
144#ifdef DEBUG
145	std::ostringstream out;
146	LLSDXMLFormatter *formatter = new LLSDXMLFormatter;
147	formatter->format(mMessages, out);
148	llinfos << "loading ... " << out.str()
149			<< " LLMessageConfigFile::loadMessages loaded "
150			<< mMessages.size() << " messages" << llendl;
151#endif
152}
153
154void LLMessageConfigFile::loadCapBans(const LLSD& data)
155{
156    LLSD bans = data["capBans"];
157    if (!bans.isMap())
158    {
159        LL_INFOS("AppInit") << "LLMessageConfigFile::loadCapBans: missing capBans section"
160            << LL_ENDL;
161        return;
162    }
163    
164	mCapBans = bans;
165    
166    LL_DEBUGS("AppInit") << "LLMessageConfigFile::loadCapBans: "
167        << bans.size() << " ban tests" << LL_ENDL;
168}
169
170void LLMessageConfigFile::loadMessageBans(const LLSD& data)
171{
172    LLSD bans = data["messageBans"];
173    if (!bans.isMap())
174    {
175        LL_INFOS("AppInit") << "LLMessageConfigFile::loadMessageBans: missing messageBans section"
176            << LL_ENDL;
177        return;
178    }
179    
180	gMessageSystem->setMessageBans(bans["trusted"], bans["untrusted"]);
181}
182
183bool LLMessageConfigFile::isCapBanned(const std::string& cap_name) const
184{
185	lldebugs << "mCapBans is " << LLSDNotationStreamer(mCapBans) << llendl;
186    return mCapBans[cap_name];
187}
188
189//---------------------------------------------------------------
190// LLMessageConfig
191//---------------------------------------------------------------
192
193//static
194void LLMessageConfig::initClass(const std::string& server_name,
195								const std::string& config_dir)
196{
197	sServerName = server_name;
198	sConfigDir = config_dir;
199	(void) LLMessageConfigFile::instance();
200	LL_DEBUGS("AppInit") << "LLMessageConfig::initClass config file "
201			<< config_dir << "/" << messageConfigFileName << LL_ENDL;
202}
203
204//static
205void LLMessageConfig::useConfig(const LLSD& config)
206{
207	LLMessageConfigFile &the_file = LLMessageConfigFile::instance();
208	the_file.loadServerDefaults(config);
209	the_file.loadMaxQueuedEvents(config);
210	the_file.loadMessages(config);
211	the_file.loadCapBans(config);
212	the_file.loadMessageBans(config);
213}
214
215//static
216LLMessageConfig::Flavor LLMessageConfig::getServerDefaultFlavor()
217{
218	LLMessageConfigFile& file = LLMessageConfigFile::instance();
219	if (file.mServerDefault == "llsd")
220	{
221		return LLSD_FLAVOR;
222	}
223	if (file.mServerDefault == "template")
224	{
225		return TEMPLATE_FLAVOR;
226	}
227	return NO_FLAVOR;
228}
229
230//static
231S32 LLMessageConfig::getMaxQueuedEvents()
232{
233	LLMessageConfigFile& file = LLMessageConfigFile::instance();
234	return file.mMaxQueuedEvents;
235}
236
237//static
238LLMessageConfig::Flavor LLMessageConfig::getMessageFlavor(const std::string& msg_name)
239{
240	LLMessageConfigFile& file = LLMessageConfigFile::instance();
241	LLSD config = file.mMessages[msg_name];
242	if (config["flavor"].asString() == "llsd")
243	{
244		return LLSD_FLAVOR;
245	}
246	if (config["flavor"].asString() == "template")
247	{
248		return TEMPLATE_FLAVOR;
249	}
250	return NO_FLAVOR;
251}
252
253//static
254LLMessageConfig::SenderTrust LLMessageConfig::getSenderTrustedness(
255	const std::string& msg_name)
256{
257	LLMessageConfigFile& file = LLMessageConfigFile::instance();
258	LLSD config = file.mMessages[msg_name];
259	if (config.has("trusted-sender"))
260	{
261		return config["trusted-sender"].asBoolean() ? TRUSTED : UNTRUSTED;
262	}
263	return NOT_SET;
264}
265
266//static
267bool LLMessageConfig::isValidMessage(const std::string& msg_name)
268{
269	if (sServerName.empty())
270	{
271		llerrs << "LLMessageConfig::initClass() not called" << llendl;
272	}
273	LLMessageConfigFile& file = LLMessageConfigFile::instance();
274	return file.mMessages.has(msg_name);
275}
276
277//static
278bool LLMessageConfig::onlySendLatest(const std::string& msg_name)
279{
280	LLMessageConfigFile& file = LLMessageConfigFile::instance();
281	LLSD config = file.mMessages[msg_name];
282	return config["only-send-latest"].asBoolean();
283}
284
285bool LLMessageConfig::isCapBanned(const std::string& cap_name)
286{
287	return LLMessageConfigFile::instance().isCapBanned(cap_name);
288}
289
290// return the web-service path to use for a given
291// message. This entry *should* match the entry
292// in simulator.xml!
293LLSD LLMessageConfig::getConfigForMessage(const std::string& msg_name)
294{
295	if (sServerName.empty())
296	{
297		llerrs << "LLMessageConfig::isMessageTrusted(name) before"
298				<< " LLMessageConfig::initClass()" << llendl;
299	}
300	LLMessageConfigFile& file = LLMessageConfigFile::instance();
301	// LLSD for the CamelCase message name
302	LLSD config = file.mMessages[msg_name];
303	return config;
304}