llmessageconfig.cpp

Go to the documentation of this file.
00001 
00032 #include "linden_common.h"
00033 
00034 #include "llmessageconfig.h"
00035 #include "llfile.h"
00036 #include "lllivefile.h"
00037 #include "llsd.h"
00038 #include "llsdutil.h"
00039 #include "llsdserialize.h"
00040 #include "message.h"
00041 
00042 static const char messageConfigFileName[] = "message.xml";
00043 static const F32 messageConfigRefreshRate = 5.0; // seconds
00044 
00045 static std::string sServerName = "";
00046 static std::string sConfigDir = "";
00047 
00048 static std::string sServerDefault;
00049 static LLSD sMessages;
00050 
00051 
00052 class LLMessageConfigFile : public LLLiveFile
00053 {
00054 public:
00055         LLMessageConfigFile()
00056         : LLLiveFile(filename(), messageConfigRefreshRate)
00057             { }
00058 
00059     static std::string filename();
00060 
00061     LLSD mMessages;
00062         std::string mServerDefault;
00063         
00064         static LLMessageConfigFile& instance();
00065                 // return the singleton configuration file
00066 
00067         /* virtual */ void loadFile();
00068         void loadServerDefaults(const LLSD& data);
00069         void loadMessages(const LLSD& data);
00070         void loadCapBans(const LLSD& blacklist);
00071         void loadMessageBans(const LLSD& blacklist);
00072         bool isCapBanned(const std::string& cap_name) const;
00073 
00074 public:
00075         LLSD mCapBans;
00076 };
00077 
00078 std::string LLMessageConfigFile::filename()
00079 {
00080     std::ostringstream ostr;
00081         ostr << sConfigDir//gAppSimp->getOption("configdir").asString()
00082                 << "/" << messageConfigFileName;
00083         return ostr.str();
00084 }
00085 
00086 LLMessageConfigFile& LLMessageConfigFile::instance()
00087 {
00088         static LLMessageConfigFile the_file;
00089         the_file.checkAndReload();
00090         return the_file;
00091 }
00092 
00093 // virtual
00094 void LLMessageConfigFile::loadFile()
00095 {
00096         LLSD data;
00097     {
00098         llifstream file(filename().c_str());
00099         
00100         if (file.is_open())
00101         {
00102                         llinfos << "Loading message.xml file at " << filename() << llendl;
00103             LLSDSerialize::fromXML(data, file);
00104         }
00105 
00106         if (data.isUndefined())
00107         {
00108             llinfos << "LLMessageConfigFile::loadFile: file missing,"
00109                                 " ill-formed, or simply undefined; not changing the"
00110                                 " file" << llendl;
00111             return;
00112         }
00113     }
00114         loadServerDefaults(data);
00115         loadMessages(data);
00116         loadCapBans(data);
00117         loadMessageBans(data);
00118 }
00119 
00120 void LLMessageConfigFile::loadServerDefaults(const LLSD& data)
00121 {
00122         mServerDefault = data["serverDefaults"][sServerName].asString();
00123 }
00124 
00125 void LLMessageConfigFile::loadMessages(const LLSD& data)
00126 {
00127         mMessages = data["messages"];
00128 
00129 #ifdef DEBUG
00130         std::ostringstream out;
00131         LLSDXMLFormatter *formatter = new LLSDXMLFormatter;
00132         formatter->format(mMessages, out);
00133         llinfos << "loading ... " << out.str()
00134                         << " LLMessageConfigFile::loadMessages loaded "
00135                         << mMessages.size() << " messages" << llendl;
00136 #endif
00137 }
00138 
00139 void LLMessageConfigFile::loadCapBans(const LLSD& data)
00140 {
00141     LLSD bans = data["capBans"];
00142     if (!bans.isMap())
00143     {
00144         llinfos << "LLMessageConfigFile::loadCapBans: missing capBans section"
00145             << llendl;
00146         return;
00147     }
00148     
00149         mCapBans = bans;
00150     
00151     llinfos << "LLMessageConfigFile::loadCapBans: "
00152         << bans.size() << " ban tests" << llendl;
00153 }
00154 
00155 void LLMessageConfigFile::loadMessageBans(const LLSD& data)
00156 {
00157     LLSD bans = data["messageBans"];
00158     if (!bans.isMap())
00159     {
00160         llinfos << "LLMessageConfigFile::loadMessageBans: missing messageBans section"
00161             << llendl;
00162         return;
00163     }
00164     
00165         gMessageSystem->setMessageBans(bans["trusted"], bans["untrusted"]);
00166 }
00167 
00168 bool LLMessageConfigFile::isCapBanned(const std::string& cap_name) const
00169 {
00170         lldebugs << "mCapBans is " << LLSDNotationStreamer(mCapBans) << llendl;
00171     return mCapBans[cap_name];
00172 }
00173 
00174 //---------------------------------------------------------------
00175 // LLMessageConfig
00176 //---------------------------------------------------------------
00177 
00178 //static
00179 void LLMessageConfig::initClass(const std::string& server_name,
00180                                                                 const std::string& config_dir)
00181 {
00182         sServerName = server_name;
00183         sConfigDir = config_dir;
00184         (void) LLMessageConfigFile::instance();
00185         llinfos << "LLMessageConfig::initClass config file "
00186                         << config_dir << "/" << messageConfigFileName << llendl;
00187 }
00188 
00189 //static
00190 void LLMessageConfig::useConfig(const LLSD& config)
00191 {
00192         LLMessageConfigFile &the_file = LLMessageConfigFile::instance();
00193         the_file.loadServerDefaults(config);
00194         the_file.loadMessages(config);
00195         the_file.loadCapBans(config);
00196         the_file.loadMessageBans(config);
00197 
00198 }
00199 
00200 //static
00201 LLMessageConfig::Flavor LLMessageConfig::getServerDefaultFlavor()
00202 {
00203         LLMessageConfigFile& file = LLMessageConfigFile::instance();
00204         if (file.mServerDefault == "llsd")
00205         {
00206                 return LLSD_FLAVOR;
00207         }
00208         if (file.mServerDefault == "template")
00209         {
00210                 return TEMPLATE_FLAVOR;
00211         }
00212         return NO_FLAVOR;
00213 }
00214 
00215 //static
00216 LLMessageConfig::Flavor LLMessageConfig::getMessageFlavor(const std::string& msg_name)
00217 {
00218         LLMessageConfigFile& file = LLMessageConfigFile::instance();
00219         LLSD config = file.mMessages[msg_name];
00220         if (config["flavor"].asString() == "llsd")
00221         {
00222                 return LLSD_FLAVOR;
00223         }
00224         if (config["flavor"].asString() == "template")
00225         {
00226                 return TEMPLATE_FLAVOR;
00227         }
00228         return NO_FLAVOR;
00229 }
00230 
00231 //static
00232 LLMessageConfig::SenderTrust LLMessageConfig::getSenderTrustedness(
00233         const std::string& msg_name)
00234 {
00235         LLMessageConfigFile& file = LLMessageConfigFile::instance();
00236         LLSD config = file.mMessages[msg_name];
00237         if (config.has("trusted-sender"))
00238         {
00239                 return config["trusted-sender"].asBoolean() ? TRUSTED : UNTRUSTED;
00240         }
00241         return NOT_SET;
00242 }
00243 
00244 //static
00245 bool LLMessageConfig::isValidMessage(const std::string& msg_name)
00246 {
00247         if (sServerName.empty())
00248         {
00249                 llerrs << "LLMessageConfig::initClass() not called" << llendl;
00250         }
00251         LLMessageConfigFile& file = LLMessageConfigFile::instance();
00252         return file.mMessages.has(msg_name);
00253 }
00254 
00255 bool LLMessageConfig::isCapBanned(const std::string& cap_name)
00256 {
00257         return LLMessageConfigFile::instance().isCapBanned(cap_name);
00258 }
00259 
00260 // return the web-service path to use for a given
00261 // message. This entry *should* match the entry
00262 // in simulator.xml!
00263 LLSD LLMessageConfig::getConfigForMessage(const std::string& msg_name)
00264 {
00265         if (sServerName.empty())
00266         {
00267                 llerrs << "LLMessageConfig::isMessageTrusted(name) before"
00268                                 << " LLMessageConfig::initClass()" << llendl;
00269         }
00270         LLMessageConfigFile& file = LLMessageConfigFile::instance();
00271         // LLSD for the CamelCase message name
00272         LLSD config = file.mMessages[msg_name];
00273         return config;
00274 }

Generated on Thu Jul 1 06:08:53 2010 for Second Life Viewer by  doxygen 1.4.7