]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
Clean up the protocol interface
[user/henk/code/inspircd.git] / src / modules / m_permchannels.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 #include <fstream>
23
24
25 /** Handles the +P channel mode
26  */
27 class PermChannel : public ModeHandler
28 {
29  public:
30         PermChannel(Module* Creator)
31                 : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL)
32         {
33                 oper = true;
34         }
35
36         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
37         {
38                 if (adding == channel->IsModeSet(this))
39                         return MODEACTION_DENY;
40
41                 channel->SetMode(this, adding);
42                 if (!adding)
43                         channel->CheckDestroy();
44
45                 return MODEACTION_ALLOW;
46         }
47 };
48
49 // Not in a class due to circular dependancy hell.
50 static std::string permchannelsconf;
51 static bool WriteDatabase(PermChannel& permchanmode)
52 {
53         /*
54          * We need to perform an atomic write so as not to fuck things up.
55          * So, let's write to a temporary file, flush it, then rename the file..
56          *     -- w00t
57          */
58         
59         // If the user has not specified a configuration file then we don't write one.
60         if (permchannelsconf.empty())
61                 return true;
62
63         std::string permchannelsnewconf = permchannelsconf + ".tmp";
64         std::ofstream stream(permchannelsnewconf.c_str());
65         if (!stream.is_open())
66         {
67                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database! %s (%d)", strerror(errno), errno);
68                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
69                 return false;
70         }
71         
72         stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
73                 << "<config format=\"xml\">" << std::endl;
74
75         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
76         {
77                 Channel* chan = i->second;
78                 if (!chan->IsModeSet(permchanmode))
79                         continue;
80
81                 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
82                         << "\" topic=\"" << ServerConfig::Escape(chan->topic)
83                         << "\" modes=\"" << ServerConfig::Escape(chan->ChanModes(true))
84                         << "\">" << std::endl;
85         }
86
87         if (stream.fail())
88         {
89                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database! %s (%d)", strerror(errno), errno);
90                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
91                 return false;
92         }
93         stream.close();
94
95 #ifdef _WIN32
96         if (remove(permchannelsconf.c_str()))
97         {
98                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot remove old database! %s (%d)", strerror(errno), errno);
99                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
100                 return false;
101         }
102 #endif
103         // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
104         if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
105         {
106                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot move new to old database! %s (%d)", strerror(errno), errno);
107                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
108                 return false;
109         }
110
111         return true;
112 }
113
114 class ModulePermanentChannels : public Module
115 {
116         PermChannel p;
117         bool dirty;
118 public:
119
120         ModulePermanentChannels() : p(this), dirty(false)
121         {
122         }
123
124         void init() CXX11_OVERRIDE
125         {
126                 ServerInstance->Modules->AddService(p);
127
128                 OnRehash(NULL);
129         }
130
131         CullResult cull()
132         {
133                 /*
134                  * DelMode can't remove the +P mode on empty channels, or it will break
135                  * merging modes with remote servers. Remove the empty channels now as
136                  * we know this is not the case.
137                  */
138                 chan_hash::iterator iter = ServerInstance->chanlist->begin();
139                 while (iter != ServerInstance->chanlist->end())
140                 {
141                         Channel* c = iter->second;
142                         if (c->GetUserCounter() == 0)
143                         {
144                                 chan_hash::iterator at = iter;
145                                 iter++;
146                                 FOREACH_MOD(OnChannelDelete, (c));
147                                 ServerInstance->chanlist->erase(at);
148                                 ServerInstance->GlobalCulls.AddItem(c);
149                         }
150                         else
151                                 iter++;
152                 }
153                 ServerInstance->Modes->DelMode(&p);
154                 return Module::cull();
155         }
156
157         void OnRehash(User *user) CXX11_OVERRIDE
158         {
159                 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
160         }
161
162         void LoadDatabase()
163         {
164                 /*
165                  * Process config-defined list of permanent channels.
166                  * -- w00t
167                  */
168                 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
169                 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
170                 {
171                         ConfigTag* tag = i->second;
172                         std::string channel = tag->getString("channel");
173                         std::string topic = tag->getString("topic");
174                         std::string modes = tag->getString("modes");
175
176                         if (channel.empty())
177                         {
178                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed permchannels tag with empty channel name.");
179                                 continue;
180                         }
181
182                         Channel *c = ServerInstance->FindChan(channel);
183
184                         if (!c)
185                         {
186                                 c = new Channel(channel, ServerInstance->Time());
187                                 if (!topic.empty())
188                                 {
189                                         c->SetTopic(ServerInstance->FakeClient, topic);
190
191                                         /*
192                                          * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
193                                          * topic will always win over others.
194                                          *
195                                          * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
196                                          */
197                                         c->topicset = 42;
198                                 }
199                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
200
201                                 if (modes.empty())
202                                         continue;
203
204                                 irc::spacesepstream list(modes);
205                                 std::string modeseq;
206                                 std::string par;
207
208                                 list.GetToken(modeseq);
209
210                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
211                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
212                                 {
213                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
214                                         if (mode)
215                                         {
216                                                 if (mode->GetNumParams(true))
217                                                         list.GetToken(par);
218                                                 else
219                                                         par.clear();
220
221                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
222                                         }
223                                 }
224                         }
225                 }
226         }
227
228         ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt) CXX11_OVERRIDE
229         {
230                 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
231                         dirty = true;
232
233                 return MOD_RES_PASSTHRU;
234         }
235
236         void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
237         {
238                 if (c->IsModeSet(p))
239                         dirty = true;
240         }
241
242         void OnBackgroundTimer(time_t) CXX11_OVERRIDE
243         {
244                 if (dirty)
245                         WriteDatabase(p);
246                 dirty = false;
247         }
248
249         void Prioritize()
250         {
251                 // XXX: Load the DB here because the order in which modules are init()ed at boot is
252                 // alphabetical, this means we must wait until all modules have done their init()
253                 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
254                 // Prioritize() is called after all module initialization is complete, consequently
255                 // all modes are available now
256
257                 static bool loaded = false;
258                 if (loaded)
259                         return;
260
261                 loaded = true;
262
263                 // Load only when there are no linked servers - we set the TS of the channels we
264                 // create to the current time, this can lead to desync because spanningtree has
265                 // no way of knowing what we do
266                 ProtocolInterface::ServerList serverlist;
267                 ServerInstance->PI->GetServerList(serverlist);
268                 if (serverlist.size() < 2)
269                 {
270                         try
271                         {
272                                 LoadDatabase();
273                         }
274                         catch (CoreException& e)
275                         {
276                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
277                         }
278                 }
279         }
280
281         Version GetVersion() CXX11_OVERRIDE
282         {
283                 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
284         }
285
286         ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
287         {
288                 if (c->IsModeSet(p))
289                         return MOD_RES_DENY;
290
291                 return MOD_RES_PASSTHRU;
292         }
293 };
294
295 MODULE_INIT(ModulePermanentChannels)