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)
169 void init() CXX11_OVERRIDE
171 ServerInstance->Modules->AddService(p);
177 * DelMode can't remove the +P mode on empty channels, or it will break
178 * merging modes with remote servers. Remove the empty channels now as
179 * we know this is not the case.
181 chan_hash::iterator iter = ServerInstance->chanlist->begin();
182 while (iter != ServerInstance->chanlist->end())
184 Channel* c = iter->second;
185 if (c->GetUserCounter() == 0)
187 chan_hash::iterator at = iter;
189 FOREACH_MOD(OnChannelDelete, (c));
190 ServerInstance->chanlist->erase(at);
191 ServerInstance->GlobalCulls.AddItem(c);
196 ServerInstance->Modes->DelMode(&p);
197 return Module::cull();
200 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
202 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
203 permchannelsconf = tag->getString("filename");
204 save_listmodes = tag->getBool("listmodes");
210 * Process config-defined list of permanent channels.
213 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
214 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
216 ConfigTag* tag = i->second;
217 std::string channel = tag->getString("channel");
218 std::string modes = tag->getString("modes");
220 if ((channel.empty()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
222 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
226 Channel *c = ServerInstance->FindChan(channel);
230 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
231 c = new Channel(channel, TS);
233 unsigned int topicset = tag->getInt("topicts");
234 c->topic = tag->getString("topic");
236 if ((topicset != 0) || (!c->topic.empty()))
239 topicset = ServerInstance->Time();
240 c->topicset = topicset;
241 c->setby = tag->getString("topicsetby");
242 if (c->setby.empty())
243 c->setby = ServerInstance->Config->ServerName;
246 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
251 irc::spacesepstream list(modes);
255 list.GetToken(modeseq);
257 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
258 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
260 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
263 if (mode->GetNumParams(true))
268 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
275 ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt) CXX11_OVERRIDE
277 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
280 return MOD_RES_PASSTHRU;
283 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
289 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
292 WriteDatabase(p, this, save_listmodes);
298 // XXX: Load the DB here because the order in which modules are init()ed at boot is
299 // alphabetical, this means we must wait until all modules have done their init()
300 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
301 // Prioritize() is called after all module initialization is complete, consequently
302 // all modes are available now
304 static bool loaded = false;
310 // Load only when there are no linked servers - we set the TS of the channels we
311 // create to the current time, this can lead to desync because spanningtree has
312 // no way of knowing what we do
313 ProtocolInterface::ServerList serverlist;
314 ServerInstance->PI->GetServerList(serverlist);
315 if (serverlist.size() < 2)
321 catch (CoreException& e)
323 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
328 void ProtoSendMode(void* opaque, TargetTypeFlags type, void* target, const std::vector<std::string>& modes, const std::vector<TranslateType>& translate)
330 // We never pass an empty modelist but better be sure
334 ListModeData* lm = static_cast<ListModeData*>(opaque);
336 // Append the mode letters without the trailing '+' (for example "IIII", "gg")
337 lm->modes.append(modes[0].begin()+1, modes[0].end());
339 // Append the parameters
340 for (std::vector<std::string>::const_iterator i = modes.begin()+1; i != modes.end(); ++i)
347 Version GetVersion() CXX11_OVERRIDE
349 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
352 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
357 return MOD_RES_PASSTHRU;
361 MODULE_INIT(ModulePermanentChannels)