+ 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()
+ {
+ /*
+ * DelMode can't remove the +P mode on empty channels, or it will break
+ * merging modes with remote servers. Remove the empty channels now as
+ * we know this is not the case.
+ */
+ chan_hash::iterator iter = ServerInstance->chanlist->begin();
+ while (iter != ServerInstance->chanlist->end())
+ {
+ Channel* c = iter->second;
+ if (c->GetUserCounter() == 0)
+ {
+ chan_hash::iterator at = iter;
+ iter++;
+ FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
+ ServerInstance->chanlist->erase(at);
+ ServerInstance->GlobalCulls.AddItem(c);
+ }
+ else
+ iter++;
+ }
+ ServerInstance->Modes->DelMode(&p);
+ return Module::cull();
+ }
+
+ virtual void OnRehash(User *user)
+ {
+ 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.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
+
+ static bool loaded = false;
+ 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)