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/>.
26 /** Handles the +P channel mode
28 class PermChannel : public ModeHandler
31 PermChannel(Module* Creator)
32 : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL)
37 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
39 if (adding == channel->IsModeSet(this))
40 return MODEACTION_DENY;
42 channel->SetMode(this, adding);
44 channel->CheckDestroy();
46 return MODEACTION_ALLOW;
50 // Not in a class due to circular dependancy hell.
51 static std::string permchannelsconf;
52 static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes)
54 ChanModeReference ban(mod, "ban");
56 * We need to perform an atomic write so as not to fuck things up.
57 * So, let's write to a temporary file, flush it, then rename the file..
61 // If the user has not specified a configuration file then we don't write one.
62 if (permchannelsconf.empty())
65 std::string permchannelsnewconf = permchannelsconf + ".tmp";
66 std::ofstream stream(permchannelsnewconf.c_str());
67 if (!stream.is_open())
69 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno);
70 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
74 stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
75 << "<config format=\"xml\">" << std::endl;
77 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
79 Channel* chan = i->second;
80 if (!chan->IsModeSet(permchanmode))
83 std::string chanmodes = chan->ChanModes(true);
89 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
90 for (ModeParser::ListModeList::const_iterator j = listmodes.begin(); j != listmodes.end(); ++j)
92 ListModeBase* lm = *j;
93 ListModeBase::ModeList* list = lm->GetList(chan);
94 if (!list || list->empty())
98 // Append the parameters
99 for (ListModeBase::ModeList::const_iterator k = list->begin(); k != list->end(); ++k, n++)
105 // Append the mode letters (for example "IIII", "gg")
106 modes.append(n, lm->GetModeChar());
111 // Remove the last space
112 params.erase(params.end()-1);
114 // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
115 // insert the listmode mode letters before the space. Otherwise just append them.
116 std::string::size_type p = chanmodes.find(' ');
117 if (p == std::string::npos)
120 chanmodes.insert(p, modes);
122 // Append the listmode parameters (the masks themselves)
128 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
129 << "\" ts=\"" << chan->age
130 << "\" topic=\"" << ServerConfig::Escape(chan->topic)
131 << "\" topicts=\"" << chan->topicset
132 << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
133 << "\" modes=\"" << ServerConfig::Escape(chanmodes)
134 << "\">" << std::endl;
139 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno);
140 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
146 if (remove(permchannelsconf.c_str()))
148 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno);
149 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
153 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
154 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
156 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno);
157 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
164 class ModulePermanentChannels : public Module
172 ModulePermanentChannels()
173 : p(this), dirty(false), loaded(false)
180 * DelMode can't remove the +P mode on empty channels, or it will break
181 * merging modes with remote servers. Remove the empty channels now as
182 * we know this is not the case.
184 chan_hash::iterator iter = ServerInstance->chanlist->begin();
185 while (iter != ServerInstance->chanlist->end())
187 Channel* c = iter->second;
188 if (c->GetUserCounter() == 0)
190 chan_hash::iterator at = iter;
192 FOREACH_MOD(OnChannelDelete, (c));
193 ServerInstance->chanlist->erase(at);
194 ServerInstance->GlobalCulls.AddItem(c);
199 ServerInstance->Modes->DelMode(&p);
200 return Module::cull();
203 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
205 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
206 permchannelsconf = tag->getString("filename");
207 save_listmodes = tag->getBool("listmodes");
213 * Process config-defined list of permanent channels.
216 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
217 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
219 ConfigTag* tag = i->second;
220 std::string channel = tag->getString("channel");
221 std::string modes = tag->getString("modes");
223 if ((channel.empty()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
225 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
229 Channel *c = ServerInstance->FindChan(channel);
233 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
234 c = new Channel(channel, TS);
236 unsigned int topicset = tag->getInt("topicts");
237 c->topic = tag->getString("topic");
239 if ((topicset != 0) || (!c->topic.empty()))
242 topicset = ServerInstance->Time();
243 c->topicset = topicset;
244 c->setby = tag->getString("topicsetby");
245 if (c->setby.empty())
246 c->setby = ServerInstance->Config->ServerName;
249 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
254 irc::spacesepstream list(modes);
258 list.GetToken(modeseq);
260 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
261 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
263 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
266 if (mode->GetNumParams(true))
271 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
278 ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt) CXX11_OVERRIDE
280 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
283 return MOD_RES_PASSTHRU;
286 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
292 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
295 WriteDatabase(p, this, save_listmodes);
301 // XXX: Load the DB here because the order in which modules are init()ed at boot is
302 // alphabetical, this means we must wait until all modules have done their init()
303 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
304 // Prioritize() is called after all module initialization is complete, consequently
305 // all modes are available now
311 // Load only when there are no linked servers - we set the TS of the channels we
312 // create to the current time, this can lead to desync because spanningtree has
313 // no way of knowing what we do
314 ProtocolInterface::ServerList serverlist;
315 ServerInstance->PI->GetServerList(serverlist);
316 if (serverlist.size() < 2)
322 catch (CoreException& e)
324 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
329 Version GetVersion() CXX11_OVERRIDE
331 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
334 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
339 return MOD_RES_PASSTHRU;
343 MODULE_INIT(ModulePermanentChannels)