llmessageconfig.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @file llmessageconfig.cpp
  3. * @brief Live file handling for messaging
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llmessageconfig.h"
  34. #include "llmessage.h"
  35. #include "llsdutil.h"
  36. #include "llsdserialize.h"
  37. static const char MESSAGE_CONFIG_FILENAME[] = "message.xml";
  38. static const char SERVER_NAME[] = "viewer";
  39. //---------------------------------------------------------------
  40. // LLMessageConfigFile class
  41. //---------------------------------------------------------------
  42. class LLMessageConfigFile
  43. {
  44. protected:
  45. LOG_CLASS(LLMessageConfigFile);
  46. private:
  47. // Use createInstance() only
  48. LLMessageConfigFile()
  49. {
  50. sInstance = this;
  51. }
  52. public:
  53. ~LLMessageConfigFile()
  54. {
  55. sInstance = NULL;
  56. }
  57. // Used to instantiate the singleton class
  58. static void createInstance(const std::string& config_dir);
  59. static LLMessageConfigFile* getInstance();
  60. bool loadFile(const std::string& filename);
  61. void loadServerDefaults(const LLSD& data);
  62. void loadMessages(const LLSD& data);
  63. void loadMessageBans(const LLSD& blacklist);
  64. public:
  65. LLSD mMessages;
  66. std::string mServerDefault;
  67. private:
  68. static LLMessageConfigFile* sInstance;
  69. };
  70. LLMessageConfigFile* LLMessageConfigFile::sInstance = NULL;
  71. //static
  72. void LLMessageConfigFile::createInstance(const std::string& config_dir)
  73. {
  74. if (sInstance)
  75. {
  76. llerrs << "Instance already exists !" << llendl;
  77. }
  78. LL_DEBUGS("AppInit") << "Config file: " << config_dir << "/"
  79. << MESSAGE_CONFIG_FILENAME << LL_ENDL;
  80. sInstance = new LLMessageConfigFile;
  81. sInstance->loadFile(config_dir + LL_DIR_DELIM_STR +
  82. MESSAGE_CONFIG_FILENAME);
  83. }
  84. LLMessageConfigFile* LLMessageConfigFile::getInstance()
  85. {
  86. if (!sInstance)
  87. {
  88. llerrs << "Call done before class initialization !" << llendl;
  89. }
  90. return sInstance;
  91. }
  92. bool LLMessageConfigFile::loadFile(const std::string& filename)
  93. {
  94. LLSD data;
  95. {
  96. llifstream file(filename.c_str());
  97. if (file.is_open())
  98. {
  99. LL_DEBUGS("AppInit") << "Loading: " << filename << LL_ENDL;
  100. LLSDSerialize::fromXML(data, file);
  101. }
  102. if (data.isUndefined())
  103. {
  104. llwarns << "File missing, ill-formed, or simply undefined; not changing the file."
  105. << llendl;
  106. return false;
  107. }
  108. }
  109. loadServerDefaults(data);
  110. loadMessages(data);
  111. loadMessageBans(data);
  112. return true;
  113. }
  114. void LLMessageConfigFile::loadServerDefaults(const LLSD& data)
  115. {
  116. mServerDefault = data["serverDefaults"][SERVER_NAME].asString();
  117. }
  118. void LLMessageConfigFile::loadMessages(const LLSD& data)
  119. {
  120. mMessages = data["messages"];
  121. LL_DEBUGS("AppInit") << "Loading...\n";
  122. std::ostringstream out;
  123. LLSDXMLFormatter* formatter = new LLSDXMLFormatter;
  124. formatter->format(mMessages, out);
  125. LL_CONT << out.str() << "\nLoaded: " << mMessages.size() << " messages."
  126. << LL_ENDL;
  127. }
  128. void LLMessageConfigFile::loadMessageBans(const LLSD& data)
  129. {
  130. LLSD bans = data["messageBans"];
  131. if (!bans.isMap())
  132. {
  133. llwarns << "Missing messageBans section" << llendl;
  134. return;
  135. }
  136. gMessageSystemp->setMessageBans(bans["trusted"], bans["untrusted"]);
  137. }
  138. //---------------------------------------------------------------
  139. // LLMessageConfig class
  140. //---------------------------------------------------------------
  141. //static
  142. void LLMessageConfig::initClass(const std::string& config_dir)
  143. {
  144. LLMessageConfigFile::createInstance(config_dir);
  145. }
  146. //static
  147. LLMessageConfig::Flavor LLMessageConfig::getServerDefaultFlavor()
  148. {
  149. LLMessageConfigFile* file = LLMessageConfigFile::getInstance();
  150. if (file->mServerDefault == "llsd")
  151. {
  152. return LLSD_FLAVOR;
  153. }
  154. if (file->mServerDefault == "template")
  155. {
  156. return TEMPLATE_FLAVOR;
  157. }
  158. return NO_FLAVOR;
  159. }
  160. //static
  161. LLMessageConfig::Flavor LLMessageConfig::getMessageFlavor(const std::string& msg_name)
  162. {
  163. LLSD config = LLMessageConfigFile::getInstance()->mMessages[msg_name];
  164. if (config["flavor"].asString() == "llsd")
  165. {
  166. return LLSD_FLAVOR;
  167. }
  168. if (config["flavor"].asString() == "template")
  169. {
  170. return TEMPLATE_FLAVOR;
  171. }
  172. return NO_FLAVOR;
  173. }
  174. //static
  175. bool LLMessageConfig::isValidMessage(const std::string& msg_name)
  176. {
  177. return LLMessageConfigFile::getInstance()->mMessages.has(msg_name);
  178. }