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/>.
32 /** Handles the +P channel mode
34 class PermChannel : public ModeHandler
37 PermChannel(Module* Creator)
38 : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL)
43 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
45 if (adding == channel->IsModeSet(this))
46 return MODEACTION_DENY;
48 channel->SetMode(this, adding);
50 channel->CheckDestroy();
52 return MODEACTION_ALLOW;
56 // Not in a class due to circular dependancy hell.
57 static std::string permchannelsconf;
58 static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes)
60 ChanModeReference ban(mod, "ban");
62 * We need to perform an atomic write so as not to fuck things up.
63 * So, let's write to a temporary file, flush it, then rename the file..
67 // If the user has not specified a configuration file then we don't write one.
68 if (permchannelsconf.empty())
71 std::string permchannelsnewconf = permchannelsconf + ".tmp";
72 std::ofstream stream(permchannelsnewconf.c_str());
73 if (!stream.is_open())
75 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno);
76 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
80 stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
81 << "<config format=\"xml\">" << std::endl;
83 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
85 Channel* chan = i->second;
86 if (!chan->IsModeSet(permchanmode))
89 std::string chanmodes = chan->ChanModes(true);
94 // Bans are managed by the core, so we have to process them separately
95 static_cast<ListModeBase*>(*ban)->DoSyncChannel(chan, mod, &lm);
97 // All other listmodes are managed by modules, so we need to ask them (call their
98 // OnSyncChannel() handler) to give our ProtoSendMode() a list of modes that are
99 // set on the channel. The ListModeData struct is passed as an opaque pointer
100 // that will be passed back to us by the module handling the mode.
101 FOREACH_MOD(OnSyncChannel, (chan, mod, &lm));
103 if (!lm.modes.empty())
105 // Remove the last space
106 lm.params.erase(lm.params.end()-1);
108 // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
109 // insert the listmode mode letters before the space. Otherwise just append them.
110 std::string::size_type p = chanmodes.find(' ');
111 if (p == std::string::npos)
112 chanmodes += lm.modes;
114 chanmodes.insert(p, lm.modes);
116 // Append the listmode parameters (the masks themselves)
118 chanmodes += lm.params;
122 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
123 << "\" ts=\"" << chan->age
124 << "\" topic=\"" << ServerConfig::Escape(chan->topic)
125 << "\" topicts=\"" << chan->topicset
126 << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
127 << "\" modes=\"" << ServerConfig::Escape(chanmodes)
128 << "\">" << std::endl;
133 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno);
134 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
140 if (remove(permchannelsconf.c_str()))
142 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno);
143 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
147 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
148 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
150 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno);
151 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
158 class ModulePermanentChannels : public Module
165 ModulePermanentChannels() : p(this), dirty(false)
172 * DelMode can't remove the +P mode on empty channels, or it will break
173 * merging modes with remote servers. Remove the empty channels now as
174 * we know this is not the case.
176 chan_hash::iterator iter = ServerInstance->chanlist->begin();
177 while (iter != ServerInstance->chanlist->end())
179 Channel* c = iter->second;
180 if (c->GetUserCounter() == 0)
182 chan_hash::iterator at = iter;
184 FOREACH_MOD(OnChannelDelete, (c));
185 ServerInstance->chanlist->erase(at);
186 ServerInstance->GlobalCulls.AddItem(c);
191 ServerInstance->Modes->DelMode(&p);
192 return Module::cull();
195 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
197 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
198 permchannelsconf = tag->getString("filename");
199 save_listmodes = tag->getBool("listmodes");
205 * Process config-defined list of permanent channels.
208 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
209 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
211 ConfigTag* tag = i->second;
212 std::string channel = tag->getString("channel");
213 std::string modes = tag->getString("modes");
215 if ((channel.empty()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
217 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
221 Channel *c = ServerInstance->FindChan(channel);
225 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
226 c = new Channel(channel, TS);
228 unsigned int topicset = tag->getInt("topicts");
229 c->topic = tag->getString("topic");
231 if ((topicset != 0) || (!c->topic.empty()))
234 topicset = ServerInstance->Time();
235 c->topicset = topicset;
236 c->setby = tag->getString("topicsetby");
237 if (c->setby.empty())
238 c->setby = ServerInstance->Config->ServerName;
241 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
246 irc::spacesepstream list(modes);
250 list.GetToken(modeseq);
252 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
253 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
255 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
258 if (mode->GetNumParams(true))
263 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
270 ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt) CXX11_OVERRIDE
272 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
275 return MOD_RES_PASSTHRU;
278 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
284 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
287 WriteDatabase(p, this, save_listmodes);
293 // XXX: Load the DB here because the order in which modules are init()ed at boot is
294 // alphabetical, this means we must wait until all modules have done their init()
295 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
296 // Prioritize() is called after all module initialization is complete, consequently
297 // all modes are available now
299 static bool loaded = false;
305 // Load only when there are no linked servers - we set the TS of the channels we
306 // create to the current time, this can lead to desync because spanningtree has
307 // no way of knowing what we do
308 ProtocolInterface::ServerList serverlist;
309 ServerInstance->PI->GetServerList(serverlist);
310 if (serverlist.size() < 2)
316 catch (CoreException& e)
318 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
323 void ProtoSendMode(void* opaque, TargetTypeFlags type, void* target, const std::vector<std::string>& modes, const std::vector<TranslateType>& translate)
325 // We never pass an empty modelist but better be sure
329 ListModeData* lm = static_cast<ListModeData*>(opaque);
331 // Append the mode letters without the trailing '+' (for example "IIII", "gg")
332 lm->modes.append(modes[0].begin()+1, modes[0].end());
334 // Append the parameters
335 for (std::vector<std::string>::const_iterator i = modes.begin()+1; i != modes.end(); ++i)
342 Version GetVersion() CXX11_OVERRIDE
344 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
347 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
352 return MOD_RES_PASSTHRU;
356 MODULE_INIT(ModulePermanentChannels)