2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /** Handles the +P channel mode
27 class PermChannel : public ModeHandler
30 PermChannel(Module* Creator)
31 : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL)
36 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
38 if (adding == channel->IsModeSet(this))
39 return MODEACTION_DENY;
41 channel->SetMode(this, adding);
43 channel->CheckDestroy();
45 return MODEACTION_ALLOW;
49 // Not in a class due to circular dependancy hell.
50 static std::string permchannelsconf;
51 static bool WriteDatabase(PermChannel& permchanmode)
54 * We need to perform an atomic write so as not to fuck things up.
55 * So, let's write to a temporary file, flush it, then rename the file..
59 // If the user has not specified a configuration file then we don't write one.
60 if (permchannelsconf.empty())
63 std::string permchannelsnewconf = permchannelsconf + ".tmp";
64 std::ofstream stream(permchannelsnewconf.c_str());
65 if (!stream.is_open())
67 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno);
68 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
72 stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
73 << "<config format=\"xml\">" << std::endl;
75 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
77 Channel* chan = i->second;
78 if (!chan->IsModeSet(permchanmode))
81 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
82 << "\" topic=\"" << ServerConfig::Escape(chan->topic)
83 << "\" modes=\"" << ServerConfig::Escape(chan->ChanModes(true))
84 << "\">" << std::endl;
89 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno);
90 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
96 if (remove(permchannelsconf.c_str()))
98 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno);
99 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
103 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
104 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
106 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno);
107 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
114 class ModulePermanentChannels : public Module
120 ModulePermanentChannels() : p(this), dirty(false)
124 void init() CXX11_OVERRIDE
126 ServerInstance->Modules->AddService(p);
134 * DelMode can't remove the +P mode on empty channels, or it will break
135 * merging modes with remote servers. Remove the empty channels now as
136 * we know this is not the case.
138 chan_hash::iterator iter = ServerInstance->chanlist->begin();
139 while (iter != ServerInstance->chanlist->end())
141 Channel* c = iter->second;
142 if (c->GetUserCounter() == 0)
144 chan_hash::iterator at = iter;
146 FOREACH_MOD(OnChannelDelete, (c));
147 ServerInstance->chanlist->erase(at);
148 ServerInstance->GlobalCulls.AddItem(c);
153 ServerInstance->Modes->DelMode(&p);
154 return Module::cull();
157 void OnRehash(User *user) CXX11_OVERRIDE
159 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
165 * Process config-defined list of permanent channels.
168 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
169 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
171 ConfigTag* tag = i->second;
172 std::string channel = tag->getString("channel");
173 std::string topic = tag->getString("topic");
174 std::string modes = tag->getString("modes");
178 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed permchannels tag with empty channel name.");
182 Channel *c = ServerInstance->FindChan(channel);
186 c = new Channel(channel, ServerInstance->Time());
189 c->SetTopic(ServerInstance->FakeClient, topic);
192 * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
193 * topic will always win over others.
195 * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
199 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
204 irc::spacesepstream list(modes);
208 list.GetToken(modeseq);
210 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
211 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
213 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
216 if (mode->GetNumParams(true))
221 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
228 ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt) CXX11_OVERRIDE
230 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
233 return MOD_RES_PASSTHRU;
236 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
242 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
251 // XXX: Load the DB here because the order in which modules are init()ed at boot is
252 // alphabetical, this means we must wait until all modules have done their init()
253 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
254 // Prioritize() is called after all module initialization is complete, consequently
255 // all modes are available now
257 static bool loaded = false;
263 // Load only when there are no linked servers - we set the TS of the channels we
264 // create to the current time, this can lead to desync because spanningtree has
265 // no way of knowing what we do
266 ProtoServerList serverlist;
267 ServerInstance->PI->GetServerList(serverlist);
268 if (serverlist.size() < 2)
274 catch (CoreException& e)
276 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
281 Version GetVersion() CXX11_OVERRIDE
283 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
286 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
291 return MOD_RES_PASSTHRU;
295 MODULE_INIT(ModulePermanentChannels)