1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, 'P', PARAM_NONE, MODETYPE_CHANNEL) { }
115 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
117 if (!source->HasPrivPermission("channels/set-permanent"))
119 source->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges", source->nick.c_str());
120 return MODEACTION_DENY;
125 if (!channel->IsModeSet('P'))
127 channel->SetMode('P',true);
129 // Save permchannels db if needed.
131 return MODEACTION_ALLOW;
136 if (channel->IsModeSet('P'))
138 if (channel->GetUserCounter() == 0 && !IS_FAKE(source))
143 * We can't delete this channel the way things work at the moment,
144 * because of the following scenario:
147 * s1 has a user in #c, s2 does not. s2 has +P set. s2 has a losing TS.
149 * On netmerge, s2 loses, so s2 removes all modes (including +P) which
150 * would subsequently delete the channel here causing big fucking problems.
152 * I don't think there's really a way around this, so just deny -P on a 0 user chan.
157 return MODEACTION_DENY;
160 /* for servers, remove +P (to avoid desyncs) but don't bother trying to delete. */
161 channel->SetMode('P',false);
163 // Save permchannels db if needed.
165 return MODEACTION_ALLOW;
169 return MODEACTION_DENY;
173 class ModulePermanentChannels : public Module
178 ModulePermanentChannels(InspIRCd* Me) : Module(Me), p(Me, this)
180 if (!ServerInstance->Modes->AddMode(&p))
181 throw ModuleException("Could not add new modes!");
182 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode };
183 ServerInstance->Modules->Attach(eventlist, this, 3);
188 virtual ~ModulePermanentChannels()
190 ServerInstance->Modes->DelMode(&p);
192 * DelMode can't remove the +P mode on empty channels, or it will break
193 * merging modes with remote servers. Remove the empty channels now as
194 * we know this is not the case.
196 chan_hash::iterator iter = ServerInstance->chanlist->begin();
197 while (iter != ServerInstance->chanlist->end())
199 Channel* c = iter->second;
200 if (c->GetUserCounter() == 0)
202 chan_hash::iterator at = iter;
204 ServerInstance->chanlist->erase(at);
212 virtual void OnRehash(User *user)
215 * Process config-defined list of permanent channels.
218 ConfigReader MyConf(ServerInstance);
220 permchannelsconf = MyConf.ReadValue("permchanneldb", "filename", "", 0, false);
222 for (int i = 0; i < MyConf.Enumerate("permchannels"); i++)
224 std::string channel = MyConf.ReadValue("permchannels", "channel", i);
225 std::string topic = MyConf.ReadValue("permchannels", "topic", i);
226 std::string modes = MyConf.ReadValue("permchannels", "modes", i);
230 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
234 Channel *c = ServerInstance->FindChan(channel);
238 c = new Channel(ServerInstance, channel, ServerInstance->Time());
241 c->SetTopic(NULL, topic, true);
244 * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
245 * topic will always win over others.
247 * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
251 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
256 irc::spacesepstream list(modes);
260 list.GetToken(modeseq);
262 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
263 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
265 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
268 if (mode->GetNumParams(true))
273 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
280 virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt)
282 if (chan && chan->IsModeSet('P'))
285 return MOD_RES_PASSTHRU;
288 virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
290 if (c->IsModeSet('P'))
294 virtual Version GetVersion()
296 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
299 virtual ModResult OnChannelPreDelete(Channel *c)
301 if (c->IsModeSet('P'))
304 return MOD_RES_PASSTHRU;
308 MODULE_INIT(ModulePermanentChannels)