PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  27. #include "llmessageconfig.h"
  28. #include "llfile.h"
  29. #include "lllivefile.h"
  30. #include "llsd.h"
  31. #include "llsdutil.h"
  32. #include "llsdserialize.h"
  33. #include "message.h"
  34. static const char messageConfigFileName[] = "message.xml";
  35. static const F32 messageConfigRefreshRate = 5.0; // seconds
  36. static std::string sServerName = "";
  37. static std::string sConfigDir = "";
  38. static std::string sServerDefault;
  39. static LLSD sMessages;
  40. class LLMessageConfigFile : public LLLiveFile
  41. {
  42. public:
  43. LLMessageConfigFile() :
  44. LLLiveFile(filename(), messageConfigRefreshRate),
  45. mMaxQueuedEvents(0)
  46. { }
  47. static std::string filename();
  48. LLSD mMessages;
  49. std::string mServerDefault;
  50. static LLMessageConfigFile& instance();
  51. // return the singleton configuration file
  52. /* virtual */ bool loadFile();
  53. void loadServerDefaults(const LLSD& data);
  54. void loadMaxQueuedEvents(const LLSD& data);
  55. void loadMessages(const LLSD& data);
  56. void loadCapBans(const LLSD& blacklist);
  57. void loadMessageBans(const LLSD& blacklist);
  58. bool isCapBanned(const std::string& cap_name) const;
  59. public:
  60. LLSD mCapBans;
  61. S32 mMaxQueuedEvents;
  62. private:
  63. static const S32 DEFAULT_MAX_QUEUED_EVENTS = 100;
  64. };
  65. std::string LLMessageConfigFile::filename()
  66. {
  67. std::ostringstream ostr;
  68. ostr << sConfigDir//gAppSimp->getOption("configdir").asString()
  69. << "/" << messageConfigFileName;
  70. return ostr.str();
  71. }
  72. LLMessageConfigFile& LLMessageConfigFile::instance()
  73. {
  74. static LLMessageConfigFile the_file;
  75. the_file.checkAndReload();
  76. return the_file;
  77. }
  78. // virtual
  79. bool LLMessageConfigFile::loadFile()
  80. {
  81. LLSD data;
  82. {
  83. llifstream file(filename());
  84. if (file.is_open())
  85. {
  86. LL_DEBUGS("AppInit") << "Loading message.xml file at " << filename() << LL_ENDL;
  87. LLSDSerialize::fromXML(data, file);
  88. }
  89. if (data.isUndefined())
  90. {
  91. LL_INFOS("AppInit") << "LLMessageConfigFile::loadFile: file missing,"
  92. " ill-formed, or simply undefined; not changing the"
  93. " file" << LL_ENDL;
  94. return false;
  95. }
  96. }
  97. loadServerDefaults(data);
  98. loadMaxQueuedEvents(data);
  99. loadMessages(data);
  100. loadCapBans(data);
  101. loadMessageBans(data);
  102. return true;
  103. }
  104. void LLMessageConfigFile::loadServerDefaults(const LLSD& data)
  105. {
  106. mServerDefault = data["serverDefaults"][sServerName].asString();
  107. }
  108. void LLMessageConfigFile::loadMaxQueuedEvents(const LLSD& data)
  109. {
  110. if (data.has("maxQueuedEvents"))
  111. {
  112. mMaxQueuedEvents = data["maxQueuedEvents"].asInteger();
  113. }
  114. else
  115. {
  116. mMaxQueuedEvents = DEFAULT_MAX_QUEUED_EVENTS;
  117. }
  118. }
  119. void LLMessageConfigFile::loadMessages(const LLSD& data)
  120. {
  121. mMessages = data["messages"];
  122. #ifdef DEBUG
  123. std::ostringstream out;
  124. LLSDXMLFormatter *formatter = new LLSDXMLFormatter;
  125. formatter->format(mMessages, out);
  126. llinfos << "loading ... " << out.str()
  127. << " LLMessageConfigFile::loadMessages loaded "
  128. << mMessages.size() << " messages" << llendl;
  129. #endif
  130. }
  131. void LLMessageConfigFile::loadCapBans(const LLSD& data)
  132. {
  133. LLSD bans = data["capBans"];
  134. if (!bans.isMap())
  135. {
  136. LL_INFOS("AppInit") << "LLMessageConfigFile::loadCapBans: missing capBans section"
  137. << LL_ENDL;
  138. return;
  139. }
  140. mCapBans = bans;
  141. LL_DEBUGS("AppInit") << "LLMessageConfigFile::loadCapBans: "
  142. << bans.size() << " ban tests" << LL_ENDL;
  143. }
  144. void LLMessageConfigFile::loadMessageBans(const LLSD& data)
  145. {
  146. LLSD bans = data["messageBans"];
  147. if (!bans.isMap())
  148. {
  149. LL_INFOS("AppInit") << "LLMessageConfigFile::loadMessageBans: missing messageBans section"
  150. << LL_ENDL;
  151. return;
  152. }
  153. gMessageSystem->setMessageBans(bans["trusted"], bans["untrusted"]);
  154. }
  155. bool LLMessageConfigFile::isCapBanned(const std::string& cap_name) const
  156. {
  157. lldebugs << "mCapBans is " << LLSDNotationStreamer(mCapBans) << llendl;
  158. return mCapBans[cap_name];
  159. }
  160. //---------------------------------------------------------------
  161. // LLMessageConfig
  162. //---------------------------------------------------------------
  163. //static
  164. void LLMessageConfig::initClass(const std::string& server_name,
  165. const std::string& config_dir)
  166. {
  167. sServerName = server_name;
  168. sConfigDir = config_dir;
  169. (void) LLMessageConfigFile::instance();
  170. LL_DEBUGS("AppInit") << "LLMessageConfig::initClass config file "
  171. << config_dir << "/" << messageConfigFileName << LL_ENDL;
  172. }
  173. //static
  174. void LLMessageConfig::useConfig(const LLSD& config)
  175. {
  176. LLMessageConfigFile &the_file = LLMessageConfigFile::instance();
  177. the_file.loadServerDefaults(config);
  178. the_file.loadMaxQueuedEvents(config);
  179. the_file.loadMessages(config);
  180. the_file.loadCapBans(config);
  181. the_file.loadMessageBans(config);
  182. }
  183. //static
  184. LLMessageConfig::Flavor LLMessageConfig::getServerDefaultFlavor()
  185. {
  186. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  187. if (file.mServerDefault == "llsd")
  188. {
  189. return LLSD_FLAVOR;
  190. }
  191. if (file.mServerDefault == "template")
  192. {
  193. return TEMPLATE_FLAVOR;
  194. }
  195. return NO_FLAVOR;
  196. }
  197. //static
  198. S32 LLMessageConfig::getMaxQueuedEvents()
  199. {
  200. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  201. return file.mMaxQueuedEvents;
  202. }
  203. //static
  204. LLMessageConfig::Flavor LLMessageConfig::getMessageFlavor(const std::string& msg_name)
  205. {
  206. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  207. LLSD config = file.mMessages[msg_name];
  208. if (config["flavor"].asString() == "llsd")
  209. {
  210. return LLSD_FLAVOR;
  211. }
  212. if (config["flavor"].asString() == "template")
  213. {
  214. return TEMPLATE_FLAVOR;
  215. }
  216. return NO_FLAVOR;
  217. }
  218. //static
  219. LLMessageConfig::SenderTrust LLMessageConfig::getSenderTrustedness(
  220. const std::string& msg_name)
  221. {
  222. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  223. LLSD config = file.mMessages[msg_name];
  224. if (config.has("trusted-sender"))
  225. {
  226. return config["trusted-sender"].asBoolean() ? TRUSTED : UNTRUSTED;
  227. }
  228. return NOT_SET;
  229. }
  230. //static
  231. bool LLMessageConfig::isValidMessage(const std::string& msg_name)
  232. {
  233. if (sServerName.empty())
  234. {
  235. llerrs << "LLMessageConfig::initClass() not called" << llendl;
  236. }
  237. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  238. return file.mMessages.has(msg_name);
  239. }
  240. //static
  241. bool LLMessageConfig::onlySendLatest(const std::string& msg_name)
  242. {
  243. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  244. LLSD config = file.mMessages[msg_name];
  245. return config["only-send-latest"].asBoolean();
  246. }
  247. bool LLMessageConfig::isCapBanned(const std::string& cap_name)
  248. {
  249. return LLMessageConfigFile::instance().isCapBanned(cap_name);
  250. }
  251. // return the web-service path to use for a given
  252. // message. This entry *should* match the entry
  253. // in simulator.xml!
  254. LLSD LLMessageConfig::getConfigForMessage(const std::string& msg_name)
  255. {
  256. if (sServerName.empty())
  257. {
  258. llerrs << "LLMessageConfig::isMessageTrusted(name) before"
  259. << " LLMessageConfig::initClass()" << llendl;
  260. }
  261. LLMessageConfigFile& file = LLMessageConfigFile::instance();
  262. // LLSD for the CamelCase message name
  263. LLSD config = file.mMessages[msg_name];
  264. return config;
  265. }