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/>.
23 /* $ModDesc: Provides support for channel mode +P to provide permanent channels */
25 // Not in a class due to circular dependancy hell.
26 static std::string permchannelsconf;
27 static bool WriteDatabase()
31 if (permchannelsconf.empty())
37 std::string tempname = permchannelsconf + ".tmp";
40 * We need to perform an atomic write so as not to fuck things up.
41 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
44 f = fopen(tempname.c_str(), "w");
47 ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno);
48 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
52 fputs("# Permchannels DB\n# This file is autogenerated; any changes will be overwritten!\n<config format=\"compat\">\n", f);
54 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
56 Channel* chan = i->second;
57 if (!chan->IsModeSet('P'))
63 "<permchannels channel=",
68 chan->ChanModes(true),
72 int lpos = 0, item = 0, ipos = 0;
73 while (lpos < 1022 && item < 7)
75 char c = items[item][ipos++];
78 // end of this string; hop to next string, insert a quote
83 else if (c == '\\' || c == '"')
94 write_error = ferror(f);
95 write_error |= fclose(f);
98 ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
99 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
104 if (remove(permchannelsconf.c_str()))
106 ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot remove old database! %s (%d)", strerror(errno), errno);
107 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
111 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
112 if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
114 ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
115 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
124 /** Handles the +P channel mode
126 class PermChannel : public ModeHandler
129 PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
131 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
135 if (!channel->IsModeSet('P'))
137 channel->SetMode('P',true);
138 return MODEACTION_ALLOW;
143 if (channel->IsModeSet('P'))
145 channel->SetMode(this,false);
146 channel->CheckDestroy();
147 return MODEACTION_ALLOW;
151 return MODEACTION_DENY;
155 class ModulePermanentChannels : public Module
161 ModulePermanentChannels() : p(this), dirty(false)
165 void init() CXX11_OVERRIDE
167 ServerInstance->Modules->AddService(p);
168 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
169 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
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(I_OnChannelDelete, OnChannelDelete(c));
190 ServerInstance->chanlist->erase(at);
191 ServerInstance->GlobalCulls.AddItem(c);
196 ServerInstance->Modes->DelMode(&p);
197 return Module::cull();
200 void OnRehash(User *user) CXX11_OVERRIDE
202 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
208 * Process config-defined list of permanent channels.
211 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
212 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
214 ConfigTag* tag = i->second;
215 std::string channel = tag->getString("channel");
216 std::string topic = tag->getString("topic");
217 std::string modes = tag->getString("modes");
221 ServerInstance->Logs->Log("m_permchannels", LOG_DEBUG, "Malformed permchannels tag with empty channel name.");
225 Channel *c = ServerInstance->FindChan(channel);
229 c = new Channel(channel, ServerInstance->Time());
232 c->SetTopic(ServerInstance->FakeClient, topic, true);
235 * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
236 * topic will always win over others.
238 * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
242 ServerInstance->Logs->Log("m_permchannels", LOG_DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
247 irc::spacesepstream list(modes);
251 list.GetToken(modeseq);
253 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
254 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
256 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
259 if (mode->GetNumParams(true))
264 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
271 ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt) CXX11_OVERRIDE
273 if (chan && (chan->IsModeSet('P') || mode == 'P'))
276 return MOD_RES_PASSTHRU;
279 void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
281 if (c->IsModeSet('P'))
285 void OnBackgroundTimer(time_t) CXX11_OVERRIDE
294 // XXX: Load the DB here because the order in which modules are init()ed at boot is
295 // alphabetical, this means we must wait until all modules have done their init()
296 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
297 // Prioritize() is called after all module initialization is complete, consequently
298 // all modes are available now
300 static bool loaded = false;
306 // Load only when there are no linked servers - we set the TS of the channels we
307 // create to the current time, this can lead to desync because spanningtree has
308 // no way of knowing what we do
309 ProtoServerList serverlist;
310 ServerInstance->PI->GetServerList(serverlist);
311 if (serverlist.size() < 2)
317 catch (CoreException& e)
319 ServerInstance->Logs->Log("m_permchannels", LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
324 Version GetVersion() CXX11_OVERRIDE
326 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
329 ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
331 if (c->IsModeSet('P'))
334 return MOD_RES_PASSTHRU;
338 MODULE_INIT(ModulePermanentChannels)