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) CXX11_OVERRIDE
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)
55 * We need to perform an atomic write so as not to fuck things up.
56 * So, let's write to a temporary file, flush it, then rename the file..
60 // If the user has not specified a configuration file then we don't write one.
61 if (permchannelsconf.empty())
64 std::string permchannelsnewconf = permchannelsconf + ".tmp";
65 std::ofstream stream(permchannelsnewconf.c_str());
66 if (!stream.is_open())
68 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database \"%s\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
69 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
73 stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
74 << "<config format=\"xml\">" << std::endl;
76 const chan_hash& chans = ServerInstance->GetChans();
77 for (chan_hash::const_iterator i = chans.begin(); i != chans.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\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
140 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
146 remove(permchannelsconf.c_str());
148 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
149 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
151 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);
152 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);
159 class ModulePermanentChannels
170 ModulePermanentChannels()
178 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
180 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
181 permchannelsconf = tag->getString("filename");
182 save_listmodes = tag->getBool("listmodes");
183 SetInterval(tag->getDuration("saveperiod", 5));
185 if (!permchannelsconf.empty())
186 permchannelsconf = ServerInstance->Config->Paths.PrependConfig(permchannelsconf);
192 * Process config-defined list of permanent channels.
195 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
196 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
198 ConfigTag* tag = i->second;
199 std::string channel = tag->getString("channel");
200 std::string modes = tag->getString("modes");
202 if (!ServerInstance->IsChannel(channel))
204 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with invalid channel name (\"" + channel + "\")");
208 Channel *c = ServerInstance->FindChan(channel);
212 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
213 c = new Channel(channel, TS);
215 time_t topicset = tag->getInt("topicts", 0);
216 std::string topic = tag->getString("topic");
218 if ((topicset != 0) || (!topic.empty()))
221 topicset = ServerInstance->Time();
222 std::string topicsetby = tag->getString("topicsetby");
223 if (topicsetby.empty())
224 topicsetby = ServerInstance->Config->ServerName;
225 c->SetTopic(ServerInstance->FakeClient, topic, topicset, &topicsetby);
228 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
233 irc::spacesepstream list(modes);
237 list.GetToken(modeseq);
239 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
240 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
242 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
245 if (mode->NeedsParam(true))
250 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
254 // We always apply the permchannels mode to permanent channels.
256 p.OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
261 ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE
263 if (chan && (chan->IsModeSet(p) || mh == &p))
266 return MOD_RES_PASSTHRU;
269 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
275 bool Tick(time_t) CXX11_OVERRIDE
278 WriteDatabase(p, this, save_listmodes);
283 void Prioritize() CXX11_OVERRIDE
285 // XXX: Load the DB here because the order in which modules are init()ed at boot is
286 // alphabetical, this means we must wait until all modules have done their init()
287 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
288 // Prioritize() is called after all module initialization is complete, consequently
289 // all modes are available now
295 // Load only when there are no linked servers - we set the TS of the channels we
296 // create to the current time, this can lead to desync because spanningtree has
297 // no way of knowing what we do
298 ProtocolInterface::ServerList serverlist;
299 ServerInstance->PI->GetServerList(serverlist);
300 if (serverlist.size() < 2)
306 catch (CoreException& e)
308 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
313 Version GetVersion() CXX11_OVERRIDE
315 return Version("Provides channel mode +P to provide permanent channels", VF_VENDOR);
318 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
323 return MOD_RES_PASSTHRU;
327 MODULE_INIT(ModulePermanentChannels)