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("m_permchannels", LOG_DEFAULT, "permchannels: 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("m_permchannels", LOG_DEFAULT, "permchannels: 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("m_permchannels", LOG_DEFAULT, "permchannels: 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("m_permchannels", LOG_DEFAULT, "permchannels: 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);
127 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
128 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
136 * DelMode can't remove the +P mode on empty channels, or it will break
137 * merging modes with remote servers. Remove the empty channels now as
138 * we know this is not the case.
140 chan_hash::iterator iter = ServerInstance->chanlist->begin();
141 while (iter != ServerInstance->chanlist->end())
143 Channel* c = iter->second;
144 if (c->GetUserCounter() == 0)
146 chan_hash::iterator at = iter;
148 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
149 ServerInstance->chanlist->erase(at);
150 ServerInstance->GlobalCulls.AddItem(c);
155 ServerInstance->Modes->DelMode(&p);
156 return Module::cull();
159 void OnRehash(User *user) CXX11_OVERRIDE
161 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
167 * Process config-defined list of permanent channels.
170 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
171 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
173 ConfigTag* tag = i->second;
174 std::string channel = tag->getString("channel");
175 std::string topic = tag->getString("topic");
176 std::string modes = tag->getString("modes");
180 ServerInstance->Logs->Log("m_permchannels", LOG_DEBUG, "Malformed permchannels tag with empty channel name.");
184 Channel *c = ServerInstance->FindChan(channel);
188 c = new Channel(channel, ServerInstance->Time());
191 c->SetTopic(ServerInstance->FakeClient, topic);
194 * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
195 * topic will always win over others.
197 * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
201 ServerInstance->Logs->Log("m_permchannels", LOG_DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
206 irc::spacesepstream list(modes);
210 list.GetToken(modeseq);
212 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
213 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
215 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
218 if (mode->GetNumParams(true))
223 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
230 ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt) CXX11_OVERRIDE
232 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
235 return MOD_RES_PASSTHRU;
238 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
244 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
253 // XXX: Load the DB here because the order in which modules are init()ed at boot is
254 // alphabetical, this means we must wait until all modules have done their init()
255 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
256 // Prioritize() is called after all module initialization is complete, consequently
257 // all modes are available now
259 static bool loaded = false;
265 // Load only when there are no linked servers - we set the TS of the channels we
266 // create to the current time, this can lead to desync because spanningtree has
267 // no way of knowing what we do
268 ProtoServerList serverlist;
269 ServerInstance->PI->GetServerList(serverlist);
270 if (serverlist.size() < 2)
276 catch (CoreException& e)
278 ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
283 Version GetVersion() CXX11_OVERRIDE
285 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
288 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
293 return MOD_RES_PASSTHRU;
297 MODULE_INIT(ModulePermanentChannels)