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",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);
55 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
57 Channel* chan = i->second;
58 if (!chan->IsModeSet('P'))
63 "<permchannels channel=",
68 chan->ChanModes(true),
73 int item = 0, ipos = 0;
76 char c = items[item][ipos++];
79 // end of this string; hop to next string, insert a quote
84 else if (c == '\\' || c == '"')
92 line.erase(line.end()-1);
93 fputs(line.c_str(), f);
97 write_error = ferror(f);
98 write_error |= fclose(f);
101 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
102 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
107 if (remove(permchannelsconf.c_str()))
109 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot remove old database! %s (%d)", strerror(errno), errno);
110 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
114 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
115 if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
117 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
118 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
127 /** Handles the +P channel mode
129 class PermChannel : public ModeHandler
132 PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
134 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
138 if (!channel->IsModeSet('P'))
140 channel->SetMode('P',true);
141 return MODEACTION_ALLOW;
146 if (channel->IsModeSet('P'))
148 channel->SetMode(this,false);
149 if (channel->GetUserCounter() == 0)
151 channel->DelUser(ServerInstance->FakeClient);
153 return MODEACTION_ALLOW;
157 return MODEACTION_DENY;
161 class ModulePermanentChannels : public Module
167 ModulePermanentChannels() : p(this), dirty(false)
173 ServerInstance->Modules->AddService(p);
174 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
175 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
183 * DelMode can't remove the +P mode on empty channels, or it will break
184 * merging modes with remote servers. Remove the empty channels now as
185 * we know this is not the case.
187 chan_hash::iterator iter = ServerInstance->chanlist->begin();
188 while (iter != ServerInstance->chanlist->end())
190 Channel* c = iter->second;
191 if (c->GetUserCounter() == 0)
193 chan_hash::iterator at = iter;
195 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
196 ServerInstance->chanlist->erase(at);
197 ServerInstance->GlobalCulls.AddItem(c);
202 ServerInstance->Modes->DelMode(&p);
203 return Module::cull();
206 virtual void OnRehash(User *user)
208 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
214 * Process config-defined list of permanent channels.
217 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
218 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
220 ConfigTag* tag = i->second;
221 std::string channel = tag->getString("channel");
222 std::string topic = tag->getString("topic");
223 std::string modes = tag->getString("modes");
227 ServerInstance->Logs->Log("m_permchannels", DEBUG, "Malformed permchannels tag with empty channel name.");
231 Channel *c = ServerInstance->FindChan(channel);
235 c = new Channel(channel, ServerInstance->Time());
238 c->SetTopic(NULL, topic, true);
241 * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
242 * topic will always win over others.
244 * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
248 ServerInstance->Logs->Log("m_permchannels", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
253 irc::spacesepstream list(modes);
257 list.GetToken(modeseq);
259 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
260 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
262 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
265 if (mode->GetNumParams(true))
270 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
277 virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt)
279 if (chan && (chan->IsModeSet('P') || mode == 'P'))
282 return MOD_RES_PASSTHRU;
285 virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
287 if (c->IsModeSet('P'))
291 void OnBackgroundTimer(time_t)
300 // XXX: Load the DB here because the order in which modules are init()ed at boot is
301 // alphabetical, this means we must wait until all modules have done their init()
302 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
303 // Prioritize() is called after all module initialization is complete, consequently
304 // all modes are available now
306 static bool loaded = false;
312 // Load only when there are no linked servers - we set the TS of the channels we
313 // create to the current time, this can lead to desync because spanningtree has
314 // no way of knowing what we do
315 ProtoServerList serverlist;
316 ServerInstance->PI->GetServerList(serverlist);
317 if (serverlist.size() < 2)
323 catch (CoreException& e)
325 ServerInstance->Logs->Log("m_permchannels", DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
330 virtual Version GetVersion()
332 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
335 virtual ModResult OnChannelPreDelete(Channel *c)
337 if (c->IsModeSet('P'))
340 return MOD_RES_PASSTHRU;
344 MODULE_INIT(ModulePermanentChannels)