+ ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
+ permchannelsconf = tag->getString("filename");
+ save_listmodes = tag->getBool("listmodes");
+ }
+
+ void LoadDatabase()
+ {
+ /*
+ * Process config-defined list of permanent channels.
+ * -- w00t
+ */
+ ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
+ for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
+ {
+ 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()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
+ {
+ ServerInstance->Logs->Log("m_permchannels", DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
+ continue;
+ }
+
+ Channel *c = ServerInstance->FindChan(channel);
+
+ if (!c)
+ {
+ time_t TS = tag->getInt("ts");
+ c = new Channel(channel, ((TS > 0) ? TS : ServerInstance->Time()));
+
+ c->SetTopic(NULL, topic, true);
+ c->setby = tag->getString("topicsetby");
+ if (c->setby.empty())
+ c->setby = ServerInstance->Config->ServerName;
+ unsigned int topicset = tag->getInt("topicts");
+ // SetTopic() sets the topic TS to now, if there was no topicts saved then don't overwrite that with a 0
+ if (topicset > 0)
+ c->topicset = topicset;
+
+ ServerInstance->Logs->Log("m_permchannels", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
+
+ if (modes.find('P') == std::string::npos)
+ {
+ ServerInstance->Logs->Log("m_permchannels", DEFAULT, "%s (%s) does not have +P set in <permchannels:modes>; it will be deleted when empty!",
+ c->name.c_str(), tag->getTagLocation().c_str());
+ }
+
+ if (modes.empty())
+ continue;
+
+ irc::spacesepstream list(modes);
+ std::string modeseq;
+ std::string par;
+
+ list.GetToken(modeseq);
+
+ // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
+ for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
+ {
+ ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
+ if (mode)
+ {
+ if (mode->GetNumParams(true))
+ list.GetToken(par);
+ else
+ par.clear();
+
+ mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
+ }
+ }
+ }
+ }
+ }
+
+ virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt)
+ {
+ if (chan && (chan->IsModeSet('P') || mode == 'P'))
+ dirty = true;
+
+ return MOD_RES_PASSTHRU;
+ }
+
+ virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
+ {
+ if (c->IsModeSet('P'))
+ dirty = true;
+ }
+
+ void OnBackgroundTimer(time_t)
+ {
+ if (dirty)
+ WriteDatabase(this, save_listmodes);
+ dirty = false;
+ }
+
+ void Prioritize()
+ {
+ // XXX: Load the DB here because the order in which modules are init()ed at boot is
+ // alphabetical, this means we must wait until all modules have done their init()
+ // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
+ // Prioritize() is called after all module initialization is complete, consequently
+ // all modes are available now
+ if (loaded)
+ return;
+
+ loaded = true;
+
+ // 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;
+ ServerInstance->PI->GetServerList(serverlist);
+ if (serverlist.size() < 2)
+ {
+ try
+ {
+ LoadDatabase();
+ }
+ catch (CoreException& e)
+ {
+ ServerInstance->Logs->Log("m_permchannels", 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 += ' ';
+ }