2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Justin Crawford <Justasic@Gmail.com>
5 * Copyright (C) 2013-2014, 2017-2019 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013-2014, 2016 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2012, 2014 Adam <Adam@anope.org>
9 * Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12 * Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
14 * This file is part of InspIRCd. InspIRCd is free software: you can
15 * redistribute it and/or modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation, version 2.
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 /** Handles the +P channel mode
35 class PermChannel : public ModeHandler
38 PermChannel(Module* Creator)
39 : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL)
44 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
46 if (adding == channel->IsModeSet(this))
47 return MODEACTION_DENY;
49 channel->SetMode(this, adding);
51 channel->CheckDestroy();
53 return MODEACTION_ALLOW;
57 // Not in a class due to circular dependency hell.
58 static std::string permchannelsconf;
59 static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes)
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\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
76 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), 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 const chan_hash& chans = ServerInstance->GetChans();
84 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
86 Channel* chan = i->second;
87 if (!chan->IsModeSet(permchanmode))
90 std::string chanmodes = chan->ChanModes(true);
96 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
97 for (ModeParser::ListModeList::const_iterator j = listmodes.begin(); j != listmodes.end(); ++j)
99 ListModeBase* lm = *j;
100 ListModeBase::ModeList* list = lm->GetList(chan);
101 if (!list || list->empty())
105 // Append the parameters
106 for (ListModeBase::ModeList::const_iterator k = list->begin(); k != list->end(); ++k, n++)
112 // Append the mode letters (for example "IIII", "gg")
113 modes.append(n, lm->GetModeChar());
118 // Remove the last space
119 params.erase(params.end()-1);
121 // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
122 // insert the listmode mode letters before the space. Otherwise just append them.
123 std::string::size_type p = chanmodes.find(' ');
124 if (p == std::string::npos)
127 chanmodes.insert(p, modes);
129 // Append the listmode parameters (the masks themselves)
135 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
136 << "\" ts=\"" << chan->age
137 << "\" topic=\"" << ServerConfig::Escape(chan->topic)
138 << "\" topicts=\"" << chan->topicset
139 << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
140 << "\" modes=\"" << ServerConfig::Escape(chanmodes)
141 << "\">" << std::endl;
146 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database \"%s\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
147 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
153 remove(permchannelsconf.c_str());
155 // Use rename to move temporary to new db - this is guaranteed not to fuck up, even in case of a crash.
156 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
158 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot replace old database \"%s\" with new database \"%s\"! %s (%d)", permchannelsconf.c_str(), permchannelsnewconf.c_str(), strerror(errno), errno);
159 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old permchan db \"%s\" with new db \"%s\": %s (%d)", permchannelsconf.c_str(), permchannelsnewconf.c_str(), strerror(errno), errno);
166 class ModulePermanentChannels
177 ModulePermanentChannels()
185 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
187 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
188 permchannelsconf = tag->getString("filename");
189 save_listmodes = tag->getBool("listmodes");
190 SetInterval(tag->getDuration("saveperiod", 5));
192 if (!permchannelsconf.empty())
193 permchannelsconf = ServerInstance->Config->Paths.PrependConfig(permchannelsconf);
199 * Process config-defined list of permanent channels.
202 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
203 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
205 ConfigTag* tag = i->second;
206 std::string channel = tag->getString("channel");
207 std::string modes = tag->getString("modes");
209 if (!ServerInstance->IsChannel(channel))
211 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with invalid channel name (\"" + channel + "\")");
215 Channel *c = ServerInstance->FindChan(channel);
219 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
220 c = new Channel(channel, TS);
222 time_t topicset = tag->getInt("topicts", 0);
223 std::string topic = tag->getString("topic");
225 if ((topicset != 0) || (!topic.empty()))
228 topicset = ServerInstance->Time();
229 std::string topicsetby = tag->getString("topicsetby");
230 if (topicsetby.empty())
231 topicsetby = ServerInstance->Config->ServerName;
232 c->SetTopic(ServerInstance->FakeClient, topic, topicset, &topicsetby);
235 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
240 irc::spacesepstream list(modes);
244 list.GetToken(modeseq);
246 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
247 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
249 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
252 if (mode->NeedsParam(true))
257 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
261 // We always apply the permchannels mode to permanent channels.
263 p.OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
268 ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE
270 if (chan && (chan->IsModeSet(p) || mh == &p))
273 return MOD_RES_PASSTHRU;
276 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
282 bool Tick(time_t) CXX11_OVERRIDE
285 WriteDatabase(p, this, save_listmodes);
290 void Prioritize() CXX11_OVERRIDE
292 // XXX: Load the DB here because the order in which modules are init()ed at boot is
293 // alphabetical, this means we must wait until all modules have done their init()
294 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
295 // Prioritize() is called after all module initialization is complete, consequently
296 // all modes are available now
302 // Load only when there are no linked servers - we set the TS of the channels we
303 // create to the current time, this can lead to desync because spanningtree has
304 // no way of knowing what we do
305 ProtocolInterface::ServerList serverlist;
306 ServerInstance->PI->GetServerList(serverlist);
307 if (serverlist.size() < 2)
313 catch (CoreException& e)
315 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
320 Version GetVersion() CXX11_OVERRIDE
322 return Version("Adds channel mode P (permanent) which prevents the channel from being deleted when the last user leaves.", VF_VENDOR);
325 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
330 return MOD_RES_PASSTHRU;
334 MODULE_INIT(ModulePermanentChannels)