2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Justin Crawford <Justasic@Gmail.com>
5 * Copyright (C) 2013-2014, 2017-2020 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);
81 << "# This file was automatically generated by the " << INSPIRCD_VERSION << " permchannels module on " << InspIRCd::TimeString(ServerInstance->Time()) << "." << std::endl
82 << "# Any changes to this file will be automatically overwritten." << std::endl
84 << "<config format=\"xml\">" << std::endl;
86 const chan_hash& chans = ServerInstance->GetChans();
87 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
89 Channel* chan = i->second;
90 if (!chan->IsModeSet(permchanmode))
93 std::string chanmodes = chan->ChanModes(true);
99 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
100 for (ModeParser::ListModeList::const_iterator j = listmodes.begin(); j != listmodes.end(); ++j)
102 ListModeBase* lm = *j;
103 ListModeBase::ModeList* list = lm->GetList(chan);
104 if (!list || list->empty())
108 // Append the parameters
109 for (ListModeBase::ModeList::const_iterator k = list->begin(); k != list->end(); ++k, n++)
115 // Append the mode letters (for example "IIII", "gg")
116 modes.append(n, lm->GetModeChar());
121 // Remove the last space
122 params.erase(params.end()-1);
124 // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
125 // insert the listmode mode letters before the space. Otherwise just append them.
126 std::string::size_type p = chanmodes.find(' ');
127 if (p == std::string::npos)
130 chanmodes.insert(p, modes);
132 // Append the listmode parameters (the masks themselves)
138 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
139 << "\" ts=\"" << chan->age
140 << "\" topic=\"" << ServerConfig::Escape(chan->topic)
141 << "\" topicts=\"" << chan->topicset
142 << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
143 << "\" modes=\"" << ServerConfig::Escape(chanmodes)
144 << "\">" << std::endl;
149 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database \"%s\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
150 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
156 remove(permchannelsconf.c_str());
158 // Use rename to move temporary to new db - this is guaranteed not to fuck up, even in case of a crash.
159 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
161 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);
162 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);
169 class ModulePermanentChannels
180 ModulePermanentChannels()
188 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
190 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
191 permchannelsconf = tag->getString("filename");
192 save_listmodes = tag->getBool("listmodes", true);
193 SetInterval(tag->getDuration("saveperiod", 5));
195 if (!permchannelsconf.empty())
196 permchannelsconf = ServerInstance->Config->Paths.PrependConfig(permchannelsconf);
202 * Process config-defined list of permanent channels.
205 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
206 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
208 ConfigTag* tag = i->second;
209 std::string channel = tag->getString("channel");
210 std::string modes = tag->getString("modes");
212 if (!ServerInstance->IsChannel(channel))
214 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with invalid channel name (\"" + channel + "\")");
218 Channel *c = ServerInstance->FindChan(channel);
222 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
223 c = new Channel(channel, TS);
225 time_t topicset = tag->getInt("topicts", 0);
226 std::string topic = tag->getString("topic");
228 if ((topicset != 0) || (!topic.empty()))
231 topicset = ServerInstance->Time();
232 std::string topicsetby = tag->getString("topicsetby");
233 if (topicsetby.empty())
234 topicsetby = ServerInstance->Config->GetServerName();
235 c->SetTopic(ServerInstance->FakeClient, topic, topicset, &topicsetby);
238 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
243 irc::spacesepstream list(modes);
247 list.GetToken(modeseq);
249 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
250 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
252 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
255 if (mode->NeedsParam(true))
260 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
264 // We always apply the permchannels mode to permanent channels.
266 p.OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
271 ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE
273 if (chan && (chan->IsModeSet(p) || mh == &p))
276 return MOD_RES_PASSTHRU;
279 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
285 bool Tick(time_t) CXX11_OVERRIDE
288 WriteDatabase(p, this, save_listmodes);
293 void Prioritize() CXX11_OVERRIDE
295 // XXX: Load the DB here because the order in which modules are init()ed at boot is
296 // alphabetical, this means we must wait until all modules have done their init()
297 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
298 // Prioritize() is called after all module initialization is complete, consequently
299 // all modes are available now
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 Version GetVersion() CXX11_OVERRIDE
325 return Version("Adds channel mode P (permanent) which prevents the channel from being deleted when the last user leaves.", VF_VENDOR);
328 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
333 return MOD_RES_PASSTHRU;
337 MODULE_INIT(ModulePermanentChannels)