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);
45 fputs("# Permchannels DB\n# This file is autogenerated; any changes will be overwritten!\n<config format=\"compat\">\n", f);
47 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
49 Channel* chan = i->second;
50 if (!chan->IsModeSet('P'))
56 "<permchannels channel=",
61 chan->ChanModes(true),
65 int lpos = 0, item = 0, ipos = 0;
66 while (lpos < 1022 && item < 7)
68 char c = items[item][ipos++];
71 // end of this string; hop to next string, insert a quote
76 else if (c == '\\' || c == '"')
87 write_error = ferror(f);
88 write_error |= fclose(f);
91 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
92 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
96 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
97 if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
99 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
100 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
109 /** Handles the +P channel mode
111 class PermChannel : public ModeHandler
114 PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
116 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
120 if (!channel->IsModeSet('P'))
122 channel->SetMode('P',true);
123 return MODEACTION_ALLOW;
128 if (channel->IsModeSet('P'))
130 channel->SetMode(this,false);
131 if (channel->GetUserCounter() == 0)
133 channel->DelUser(ServerInstance->FakeClient);
135 return MODEACTION_ALLOW;
139 return MODEACTION_DENY;
143 class ModulePermanentChannels : public Module
149 ModulePermanentChannels() : p(this), dirty(false)
155 ServerInstance->Modules->AddService(p);
156 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
157 ServerInstance->Modules->Attach(eventlist, this, 5);
165 * DelMode can't remove the +P mode on empty channels, or it will break
166 * merging modes with remote servers. Remove the empty channels now as
167 * we know this is not the case.
169 chan_hash::iterator iter = ServerInstance->chanlist->begin();
170 while (iter != ServerInstance->chanlist->end())
172 Channel* c = iter->second;
173 if (c->GetUserCounter() == 0)
175 chan_hash::iterator at = iter;
177 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
178 ServerInstance->chanlist->erase(at);
179 ServerInstance->GlobalCulls.AddItem(c);
184 ServerInstance->Modes->DelMode(&p);
185 return Module::cull();
188 virtual void OnRehash(User *user)
191 * Process config-defined list of permanent channels.
195 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
197 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
198 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
200 ConfigTag* tag = i->second;
201 std::string channel = tag->getString("channel");
202 std::string topic = tag->getString("topic");
203 std::string modes = tag->getString("modes");
207 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
211 Channel *c = ServerInstance->FindChan(channel);
215 c = new Channel(channel, ServerInstance->Time());
218 c->SetTopic(NULL, topic, true);
221 * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
222 * topic will always win over others.
224 * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
228 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
233 irc::spacesepstream list(modes);
237 list.GetToken(modeseq);
239 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
240 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
242 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
245 if (mode->GetNumParams(true))
250 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
257 virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt)
259 if (chan && (chan->IsModeSet('P') || mode == 'P'))
262 return MOD_RES_PASSTHRU;
265 virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
267 if (c->IsModeSet('P'))
271 void OnBackgroundTimer(time_t)
278 virtual Version GetVersion()
280 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
283 virtual ModResult OnChannelPreDelete(Channel *c)
285 if (c->IsModeSet('P'))
288 return MOD_RES_PASSTHRU;
292 MODULE_INIT(ModulePermanentChannels)