1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides support for channel mode +P to provide permanent channels */
18 // Not in a class due to circular dependancy hell.
19 static std::string permchannelsconf;
20 static bool WriteDatabase()
24 if (permchannelsconf.empty())
30 std::string tempname = permchannelsconf + ".tmp";
33 * We need to perform an atomic write so as not to fuck things up.
34 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
37 f = fopen(tempname.c_str(), "w");
40 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno);
41 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
46 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
48 Channel* chan = i->second;
49 if (!chan->IsModeSet('P'))
55 "<permchannels channel=",
60 chan->ChanModes(true),
64 int lpos = 0, item = 0, ipos = 0;
65 while (lpos < 1022 && item < 7)
67 char c = items[item][ipos++];
70 // end of this string; hop to next string, insert a quote
75 else if (c == '\\' || c == '"')
86 write_error = ferror(f);
87 write_error |= fclose(f);
90 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
91 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
95 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
96 if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
98 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
99 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
108 /** Handles the +P channel mode
110 class PermChannel : public ModeHandler
113 PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
115 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
119 if (!channel->IsModeSet('P'))
121 channel->SetMode('P',true);
123 // Save permchannels db if needed.
125 return MODEACTION_ALLOW;
130 if (channel->IsModeSet('P'))
132 if (channel->GetUserCounter() == 0 && !IS_SERVER(source))
137 * We can't delete this channel the way things work at the moment,
138 * because of the following scenario:
141 * s1 has a user in #c, s2 does not. s2 has +P set. s2 has a losing TS.
143 * On netmerge, s2 loses, so s2 removes all modes (including +P) which
144 * would subsequently delete the channel here causing big fucking problems.
146 * I don't think there's really a way around this, so just deny -P on a 0 user chan.
151 return MODEACTION_DENY;
154 /* for servers, remove +P (to avoid desyncs) but don't bother trying to delete. */
155 channel->SetMode('P',false);
157 // Save permchannels db if needed.
159 return MODEACTION_ALLOW;
163 return MODEACTION_DENY;
167 class ModulePermanentChannels : public Module
172 ModulePermanentChannels() : p(this)
174 if (!ServerInstance->Modes->AddMode(&p))
175 throw ModuleException("Could not add new modes!");
176 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash };
177 ServerInstance->Modules->Attach(eventlist, this, 4);
185 * DelMode can't remove the +P mode on empty channels, or it will break
186 * merging modes with remote servers. Remove the empty channels now as
187 * we know this is not the case.
189 chan_hash::iterator iter = ServerInstance->chanlist->begin();
190 while (iter != ServerInstance->chanlist->end())
192 Channel* c = iter->second;
193 if (c->GetUserCounter() == 0)
195 chan_hash::iterator at = iter;
197 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
198 ServerInstance->chanlist->erase(at);
199 ServerInstance->GlobalCulls.AddItem(c);
204 ServerInstance->Modes->DelMode(&p);
205 return Module::cull();
208 virtual void OnRehash(User *user)
211 * Process config-defined list of permanent channels.
215 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
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("blah", 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("blah", 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'))
282 return MOD_RES_PASSTHRU;
285 virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
287 if (c->IsModeSet('P'))
291 virtual Version GetVersion()
293 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
296 virtual ModResult OnChannelPreDelete(Channel *c)
298 if (c->IsModeSet('P'))
301 return MOD_RES_PASSTHRU;
305 MODULE_INIT(ModulePermanentChannels)