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 const chan_hash& chans = ServerInstance->GetChans();
78 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
80 Channel* chan = i->second;
81 if (!chan->IsModeSet(permchanmode))
84 std::string chanmodes = chan->ChanModes(true);
90 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
91 for (ModeParser::ListModeList::const_iterator j = listmodes.begin(); j != listmodes.end(); ++j)
93 ListModeBase* lm = *j;
94 ListModeBase::ModeList* list = lm->GetList(chan);
95 if (!list || list->empty())
99 // Append the parameters
100 for (ListModeBase::ModeList::const_iterator k = list->begin(); k != list->end(); ++k, n++)
106 // Append the mode letters (for example "IIII", "gg")
107 modes.append(n, lm->GetModeChar());
112 // Remove the last space
113 params.erase(params.end()-1);
115 // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
116 // insert the listmode mode letters before the space. Otherwise just append them.
117 std::string::size_type p = chanmodes.find(' ');
118 if (p == std::string::npos)
121 chanmodes.insert(p, modes);
123 // Append the listmode parameters (the masks themselves)
129 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
130 << "\" ts=\"" << chan->age
131 << "\" topic=\"" << ServerConfig::Escape(chan->topic)
132 << "\" topicts=\"" << chan->topicset
133 << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
134 << "\" modes=\"" << ServerConfig::Escape(chanmodes)
135 << "\">" << std::endl;
140 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno);
141 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
147 if (remove(permchannelsconf.c_str()))
149 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno);
150 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
154 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
155 if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
157 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno);
158 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
165 class ModulePermanentChannels : public Module
173 ModulePermanentChannels()
174 : p(this), dirty(false), loaded(false)
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");
184 if (!permchannelsconf.empty())
185 permchannelsconf = ServerInstance->Config->Paths.PrependConfig(permchannelsconf);
191 * Process config-defined list of permanent channels.
194 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
195 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
197 ConfigTag* tag = i->second;
198 std::string channel = tag->getString("channel");
199 std::string modes = tag->getString("modes");
201 if ((channel.empty()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
203 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
207 Channel *c = ServerInstance->FindChan(channel);
211 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
212 c = new Channel(channel, TS);
214 unsigned int topicset = tag->getInt("topicts");
215 c->topic = tag->getString("topic");
217 if ((topicset != 0) || (!c->topic.empty()))
220 topicset = ServerInstance->Time();
221 c->topicset = topicset;
222 c->setby = tag->getString("topicsetby");
223 if (c->setby.empty())
224 c->setby = ServerInstance->Config->ServerName;
227 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
232 irc::spacesepstream list(modes);
236 list.GetToken(modeseq);
238 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
239 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
241 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
244 if (mode->GetNumParams(true))
249 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
256 ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE
258 if (chan && (chan->IsModeSet(p) || mh == &p))
261 return MOD_RES_PASSTHRU;
264 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
270 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
273 WriteDatabase(p, this, save_listmodes);
279 // XXX: Load the DB here because the order in which modules are init()ed at boot is
280 // alphabetical, this means we must wait until all modules have done their init()
281 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
282 // Prioritize() is called after all module initialization is complete, consequently
283 // all modes are available now
289 // Load only when there are no linked servers - we set the TS of the channels we
290 // create to the current time, this can lead to desync because spanningtree has
291 // no way of knowing what we do
292 ProtocolInterface::ServerList serverlist;
293 ServerInstance->PI->GetServerList(serverlist);
294 if (serverlist.size() < 2)
300 catch (CoreException& e)
302 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
307 Version GetVersion() CXX11_OVERRIDE
309 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
312 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
317 return MOD_RES_PASSTHRU;
321 MODULE_INIT(ModulePermanentChannels)