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'))
61 std::string chants = ConvToStr(chan->age);
62 std::string topicts = ConvToStr(chan->topicset);
65 "<permchannels channel=",
76 chan->ChanModes(true),
81 int item = 0, ipos = 0;
84 char c = items[item][ipos++];
87 // end of this string; hop to next string, insert a quote
92 else if (c == '\\' || c == '"')
100 line.erase(line.end()-1);
101 fputs(line.c_str(), f);
105 write_error = ferror(f);
106 write_error |= fclose(f);
109 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
110 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
115 if (remove(permchannelsconf.c_str()))
117 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot remove old database! %s (%d)", strerror(errno), errno);
118 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
122 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
123 if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
125 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
126 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
135 /** Handles the +P channel mode
137 class PermChannel : public ModeHandler
140 PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
142 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
146 if (!channel->IsModeSet('P'))
148 channel->SetMode('P',true);
149 return MODEACTION_ALLOW;
154 if (channel->IsModeSet('P'))
156 channel->SetMode(this,false);
157 if (channel->GetUserCounter() == 0)
159 channel->DelUser(ServerInstance->FakeClient);
161 return MODEACTION_ALLOW;
165 return MODEACTION_DENY;
169 class ModulePermanentChannels : public Module
175 ModulePermanentChannels() : p(this), dirty(false)
181 ServerInstance->Modules->AddService(p);
182 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
183 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
191 * DelMode can't remove the +P mode on empty channels, or it will break
192 * merging modes with remote servers. Remove the empty channels now as
193 * we know this is not the case.
195 chan_hash::iterator iter = ServerInstance->chanlist->begin();
196 while (iter != ServerInstance->chanlist->end())
198 Channel* c = iter->second;
199 if (c->GetUserCounter() == 0)
201 chan_hash::iterator at = iter;
203 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
204 ServerInstance->chanlist->erase(at);
205 ServerInstance->GlobalCulls.AddItem(c);
210 ServerInstance->Modes->DelMode(&p);
211 return Module::cull();
214 virtual void OnRehash(User *user)
216 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
222 * Process config-defined list of permanent channels.
225 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
226 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
228 ConfigTag* tag = i->second;
229 std::string channel = tag->getString("channel");
230 std::string topic = tag->getString("topic");
231 std::string modes = tag->getString("modes");
233 if ((channel.empty()) || (channel.length() > ServerInstance->Config->Limits.ChanMax))
235 ServerInstance->Logs->Log("m_permchannels", DEFAULT, "Ignoring permchannels tag with empty or too long channel name (\"" + channel + "\")");
239 Channel *c = ServerInstance->FindChan(channel);
243 time_t TS = tag->getInt("ts");
244 c = new Channel(channel, ((TS > 0) ? TS : ServerInstance->Time()));
246 c->SetTopic(NULL, topic, true);
247 c->setby = tag->getString("topicsetby");
248 unsigned int topicset = tag->getInt("topicts");
249 // SetTopic() sets the topic TS to now, if there was no topicts saved then don't overwrite that with a 0
251 c->topicset = topicset;
253 ServerInstance->Logs->Log("m_permchannels", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
258 irc::spacesepstream list(modes);
262 list.GetToken(modeseq);
264 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
265 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
267 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
270 if (mode->GetNumParams(true))
275 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
282 virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt)
284 if (chan && (chan->IsModeSet('P') || mode == 'P'))
287 return MOD_RES_PASSTHRU;
290 virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
292 if (c->IsModeSet('P'))
296 void OnBackgroundTimer(time_t)
305 // XXX: Load the DB here because the order in which modules are init()ed at boot is
306 // alphabetical, this means we must wait until all modules have done their init()
307 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
308 // Prioritize() is called after all module initialization is complete, consequently
309 // all modes are available now
311 static bool loaded = false;
317 // Load only when there are no linked servers - we set the TS of the channels we
318 // create to the current time, this can lead to desync because spanningtree has
319 // no way of knowing what we do
320 ProtoServerList serverlist;
321 ServerInstance->PI->GetServerList(serverlist);
322 if (serverlist.size() < 2)
328 catch (CoreException& e)
330 ServerInstance->Logs->Log("m_permchannels", DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
335 virtual Version GetVersion()
337 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
340 virtual ModResult OnChannelPreDelete(Channel *c)
342 if (c->IsModeSet('P'))
345 return MOD_RES_PASSTHRU;
349 MODULE_INIT(ModulePermanentChannels)