]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
704745f572539f5838ccba4f6ae60459af1e2162
[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                 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
128                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
129
130                 OnRehash(NULL);
131         }
132
133         CullResult cull()
134         {
135                 /*
136                  * DelMode can't remove the +P mode on empty channels, or it will break
137                  * merging modes with remote servers. Remove the empty channels now as
138                  * we know this is not the case.
139                  */
140                 chan_hash::iterator iter = ServerInstance->chanlist->begin();
141                 while (iter != ServerInstance->chanlist->end())
142                 {
143                         Channel* c = iter->second;
144                         if (c->GetUserCounter() == 0)
145                         {
146                                 chan_hash::iterator at = iter;
147                                 iter++;
148                                 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
149                                 ServerInstance->chanlist->erase(at);
150                                 ServerInstance->GlobalCulls.AddItem(c);
151                         }
152                         else
153                                 iter++;
154                 }
155                 ServerInstance->Modes->DelMode(&p);
156                 return Module::cull();
157         }
158
159         void OnRehash(User *user) CXX11_OVERRIDE
160         {
161                 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
162         }
163
164         void LoadDatabase()
165         {
166                 /*
167                  * Process config-defined list of permanent channels.
168                  * -- w00t
169                  */
170                 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
171                 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
172                 {
173                         ConfigTag* tag = i->second;
174                         std::string channel = tag->getString("channel");
175                         std::string topic = tag->getString("topic");
176                         std::string modes = tag->getString("modes");
177
178                         if (channel.empty())
179                         {
180                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed permchannels tag with empty channel name.");
181                                 continue;
182                         }
183
184                         Channel *c = ServerInstance->FindChan(channel);
185
186                         if (!c)
187                         {
188                                 c = new Channel(channel, ServerInstance->Time());
189                                 if (!topic.empty())
190                                 {
191                                         c->SetTopic(ServerInstance->FakeClient, topic);
192
193                                         /*
194                                          * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
195                                          * topic will always win over others.
196                                          *
197                                          * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
198                                          */
199                                         c->topicset = 42;
200                                 }
201                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
202
203                                 if (modes.empty())
204                                         continue;
205
206                                 irc::spacesepstream list(modes);
207                                 std::string modeseq;
208                                 std::string par;
209
210                                 list.GetToken(modeseq);
211
212                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
213                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
214                                 {
215                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
216                                         if (mode)
217                                         {
218                                                 if (mode->GetNumParams(true))
219                                                         list.GetToken(par);
220                                                 else
221                                                         par.clear();
222
223                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
224                                         }
225                                 }
226                         }
227                 }
228         }
229
230         ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt) CXX11_OVERRIDE
231         {
232                 if (chan && (chan->IsModeSet(p) || mode == p.GetModeChar()))
233                         dirty = true;
234
235                 return MOD_RES_PASSTHRU;
236         }
237
238         void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
239         {
240                 if (c->IsModeSet(p))
241                         dirty = true;
242         }
243
244         void OnBackgroundTimer(time_t) CXX11_OVERRIDE
245         {
246                 if (dirty)
247                         WriteDatabase(p);
248                 dirty = false;
249         }
250
251         void Prioritize()
252         {
253                 // XXX: Load the DB here because the order in which modules are init()ed at boot is
254                 // alphabetical, this means we must wait until all modules have done their init()
255                 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
256                 // Prioritize() is called after all module initialization is complete, consequently
257                 // all modes are available now
258
259                 static bool loaded = false;
260                 if (loaded)
261                         return;
262
263                 loaded = true;
264
265                 // Load only when there are no linked servers - we set the TS of the channels we
266                 // create to the current time, this can lead to desync because spanningtree has
267                 // no way of knowing what we do
268                 ProtoServerList serverlist;
269                 ServerInstance->PI->GetServerList(serverlist);
270                 if (serverlist.size() < 2)
271                 {
272                         try
273                         {
274                                 LoadDatabase();
275                         }
276                         catch (CoreException& e)
277                         {
278                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
279                         }
280                 }
281         }
282
283         Version GetVersion() CXX11_OVERRIDE
284         {
285                 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
286         }
287
288         ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
289         {
290                 if (c->IsModeSet(p))
291                         return MOD_RES_DENY;
292
293                 return MOD_RES_PASSTHRU;
294         }
295 };
296
297 MODULE_INIT(ModulePermanentChannels)