]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_permchannels.cpp
Replace OnRehash() with ReadConfig() that is called on boot, on module load and on...
[user/henk/code/inspircd.git] / src / modules / m_permchannels.cpp
index 69c433d045b2dfa58e8dbdfe5109eb31d9a08312..ec04c7a4547b4b5e7a23c562b09a62841abf68eb 100644 (file)
 
 
 #include "inspircd.h"
+#include "listmode.h"
 #include <fstream>
 
 
+struct ListModeData
+{
+       std::string modes;
+       std::string params;
+};
+
 /** Handles the +P channel mode
  */
 class PermChannel : public ModeHandler
@@ -48,8 +55,9 @@ class PermChannel : public ModeHandler
 
 // Not in a class due to circular dependancy hell.
 static std::string permchannelsconf;
-static bool WriteDatabase(PermChannel& permchanmode)
+static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes)
 {
+       ChanModeReference ban(mod, "ban");
        /*
         * We need to perform an atomic write so as not to fuck things up.
         * So, let's write to a temporary file, flush it, then rename the file..
@@ -64,7 +72,7 @@ static bool WriteDatabase(PermChannel& permchanmode)
        std::ofstream stream(permchannelsnewconf.c_str());
        if (!stream.is_open())
        {
-               ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno);
+               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno);
                ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
                return false;
        }
@@ -78,15 +86,51 @@ static bool WriteDatabase(PermChannel& permchanmode)
                if (!chan->IsModeSet(permchanmode))
                        continue;
 
+               std::string chanmodes = chan->ChanModes(true);
+               if (save_listmodes)
+               {
+                       ListModeData lm;
+
+                       // Bans are managed by the core, so we have to process them separately
+                       static_cast<ListModeBase*>(*ban)->DoSyncChannel(chan, mod, &lm);
+
+                       // All other listmodes are managed by modules, so we need to ask them (call their
+                       // OnSyncChannel() handler) to give our ProtoSendMode() a list of modes that are
+                       // set on the channel. The ListModeData struct is passed as an opaque pointer
+                       // that will be passed back to us by the module handling the mode.
+                       FOREACH_MOD(OnSyncChannel, (chan, mod, &lm));
+
+                       if (!lm.modes.empty())
+                       {
+                               // Remove the last space
+                               lm.params.erase(lm.params.end()-1);
+
+                               // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
+                               // insert the listmode mode letters before the space. Otherwise just append them.
+                               std::string::size_type p = chanmodes.find(' ');
+                               if (p == std::string::npos)
+                                       chanmodes += lm.modes;
+                               else
+                                       chanmodes.insert(p, lm.modes);
+
+                               // Append the listmode parameters (the masks themselves)
+                               chanmodes += ' ';
+                               chanmodes += lm.params;
+                       }
+               }
+
                stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
+                       << "\" ts=\"" << chan->age
                        << "\" topic=\"" << ServerConfig::Escape(chan->topic)
-                       << "\" modes=\"" << ServerConfig::Escape(chan->ChanModes(true))
+                       << "\" topicts=\"" << chan->topicset
+                       << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
+                       << "\" modes=\"" << ServerConfig::Escape(chanmodes)
                        << "\">" << std::endl;
        }
 
        if (stream.fail())
        {
-               ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
+               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno);
                ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
                return false;
        }
@@ -95,7 +139,7 @@ static bool WriteDatabase(PermChannel& permchanmode)
 #ifdef _WIN32
        if (remove(permchannelsconf.c_str()))
        {
-               ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot remove old database! %s (%d)", strerror(errno), errno);
+               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno);
                ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
                return false;
        }
@@ -103,7 +147,7 @@ static bool WriteDatabase(PermChannel& permchanmode)
        // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
        if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
        {
-               ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
+               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno);
                ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
                return false;
        }
@@ -115,6 +159,7 @@ class ModulePermanentChannels : public Module
 {
        PermChannel p;
        bool dirty;
+       bool save_listmodes;
 public:
 
        ModulePermanentChannels() : p(this), dirty(false)
@@ -124,10 +169,6 @@ public:
        void init() CXX11_OVERRIDE
        {
                ServerInstance->Modules->AddService(p);
-               Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
-               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
-
-               OnRehash(NULL);
        }
 
        CullResult cull()
@@ -145,7 +186,7 @@ public:
                        {
                                chan_hash::iterator at = iter;
                                iter++;
-                               FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
+                               FOREACH_MOD(OnChannelDelete, (c));
                                ServerInstance->chanlist->erase(at);
                                ServerInstance->GlobalCulls.AddItem(c);
                        }
@@ -156,9 +197,11 @@ public:
                return Module::cull();
        }
 
-       void OnRehash(User *user) CXX11_OVERRIDE
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
-               permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
+               ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
+               permchannelsconf = tag->getString("filename");
+               save_listmodes = tag->getBool("listmodes");
        }
 
        void LoadDatabase()
@@ -172,12 +215,11 @@ public:
                {
                        ConfigTag* tag = i->second;
                        std::string channel = tag->getString("channel");
-                       std::string topic = tag->getString("topic");
                        std::string modes = tag->getString("modes");
 
-                       if (channel.empty())
+                       if ((channel.empty()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
                        {
-                               ServerInstance->Logs->Log("m_permchannels", LOG_DEBUG, "Malformed permchannels tag with empty channel name.");
+                               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
                                continue;
                        }
 
@@ -185,20 +227,23 @@ public:
 
                        if (!c)
                        {
-                               c = new Channel(channel, ServerInstance->Time());
-                               if (!topic.empty())
+                               time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
+                               c = new Channel(channel, TS);
+
+                               unsigned int topicset = tag->getInt("topicts");
+                               c->topic = tag->getString("topic");
+
+                               if ((topicset != 0) || (!c->topic.empty()))
                                {
-                                       c->SetTopic(ServerInstance->FakeClient, topic);
-
-                                       /*
-                                        * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
-                                        * topic will always win over others.
-                                        *
-                                        * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
-                                        */
-                                       c->topicset = 42;
+                                       if (topicset == 0)
+                                               topicset = ServerInstance->Time();
+                                       c->topicset = topicset;
+                                       c->setby = tag->getString("topicsetby");
+                                       if (c->setby.empty())
+                                               c->setby = ServerInstance->Config->ServerName;
                                }
-                               ServerInstance->Logs->Log("m_permchannels", LOG_DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
+
+                               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
 
                                if (modes.empty())
                                        continue;
@@ -244,7 +289,7 @@ public:
        void OnBackgroundTimer(time_t) CXX11_OVERRIDE
        {
                if (dirty)
-                       WriteDatabase(p);
+                       WriteDatabase(p, this, save_listmodes);
                dirty = false;
        }
 
@@ -265,7 +310,7 @@ public:
                // Load only when there are no linked servers - we set the TS of the channels we
                // create to the current time, this can lead to desync because spanningtree has
                // no way of knowing what we do
-               ProtoServerList serverlist;
+               ProtocolInterface::ServerList serverlist;
                ServerInstance->PI->GetServerList(serverlist);
                if (serverlist.size() < 2)
                {
@@ -275,11 +320,30 @@ public:
                        }
                        catch (CoreException& e)
                        {
-                               ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
+                               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
                        }
                }
        }
 
+       void ProtoSendMode(void* opaque, TargetTypeFlags type, void* target, const std::vector<std::string>& modes, const std::vector<TranslateType>& translate)
+       {
+               // We never pass an empty modelist but better be sure
+               if (modes.empty())
+                       return;
+
+               ListModeData* lm = static_cast<ListModeData*>(opaque);
+
+               // Append the mode letters without the trailing '+' (for example "IIII", "gg")
+               lm->modes.append(modes[0].begin()+1, modes[0].end());
+
+               // Append the parameters
+               for (std::vector<std::string>::const_iterator i = modes.begin()+1; i != modes.end(); ++i)
+               {
+                       lm->params += *i;
+                       lm->params += ' ';
+               }
+       }
+
        Version GetVersion() CXX11_OVERRIDE
        {
                return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);