aboutsummaryrefslogtreecommitdiff
path: root/src/configreader.cpp
diff options
context:
space:
mode:
authorGravatar Sadie Powell2023-07-03 17:26:40 +0100
committerGravatar Sadie Powell2023-07-03 18:43:58 +0100
commit386f0dafd55897723356c75299cd02ed0b882f5b (patch)
treebe31de75870a0e1da815cf5f4203fed793eb66ba /src/configreader.cpp
parentFix a minor Doxygen comment issue. (diff)
Replace FileReader with something more sensible.
Diffstat (limited to 'src/configreader.cpp')
-rw-r--r--src/configreader.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/configreader.cpp b/src/configreader.cpp
index 363bc16c4..1c418eae7 100644
--- a/src/configreader.cpp
+++ b/src/configreader.cpp
@@ -40,6 +40,12 @@
#include "configparser.h"
#include "exitcodes.h"
+ServerConfig::ReadResult::ReadResult(const std::string& c, const std::string& e)
+ : contents(c)
+ , error(e)
+{
+}
+
ServerConfig::ServerLimits::ServerLimits(const std::shared_ptr<ConfigTag>& tag)
: MaxLine(tag->getNum<size_t>("maxline", 512, 512))
, MaxNick(tag->getNum<size_t>("maxnick", 30, 1, MaxLine))
@@ -88,6 +94,45 @@ ServerConfig::ServerConfig()
{
}
+ServerConfig::ReadResult ServerConfig::ReadFile(const std::string& file, bool invalidate)
+{
+ auto contents = filecontents.find(file);
+ if (invalidate)
+ filecontents.erase(contents);
+ else if (contents != filecontents.end())
+ return ReadResult(contents->second, {});
+
+ bool executable = false;
+ std::string name = file;
+ std::string path = file;
+
+ // If the caller specified a short name (e.g. <file motd="motd.txt">) then look it up.
+ auto source = filesources.find(file);
+ if (source != filesources.end())
+ {
+ name = source->first;
+ path = source->second.second;
+ executable = source->second.second;
+ }
+
+ // Try to open the file and error out if it fails.
+ auto fh = ParseStack::DoOpenFile(path, executable);
+ if (!fh)
+ return ReadResult({}, strerror(errno));
+
+ std::stringstream datastream;
+ char databuf[4096];
+ while (fgets(databuf, sizeof(databuf), fh.get()))
+ {
+ size_t len = strlen(databuf);
+ if (len)
+ datastream.write(databuf, len);
+ }
+
+ filecontents[name] = datastream.str();
+ return ReadResult(filecontents[name], {});
+}
+
void ServerConfig::CrossCheckOperBlocks()
{
std::unordered_map<std::string, std::shared_ptr<ConfigTag>> operclass;