]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
sslinfo: use the SSL certificate API to get user SSL certificates.
[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 "listmode.h"
23 #include <fstream>
24
25
26 /** Handles the +P channel mode
27  */
28 class PermChannel : public ModeHandler
29 {
30  public:
31         PermChannel(Module* Creator)
32                 : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL)
33         {
34                 oper = true;
35         }
36
37         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
38         {
39                 if (adding == channel->IsModeSet(this))
40                         return MODEACTION_DENY;
41
42                 channel->SetMode(this, adding);
43                 if (!adding)
44                         channel->CheckDestroy();
45
46                 return MODEACTION_ALLOW;
47         }
48 };
49
50 // Not in a class due to circular dependancy hell.
51 static std::string permchannelsconf;
52 static bool WriteDatabase(PermChannel& permchanmode, Module* mod, bool save_listmodes)
53 {
54         /*
55          * We need to perform an atomic write so as not to fuck things up.
56          * So, let's write to a temporary file, flush it, then rename the file..
57          *     -- w00t
58          */
59
60         // If the user has not specified a configuration file then we don't write one.
61         if (permchannelsconf.empty())
62                 return true;
63
64         std::string permchannelsnewconf = permchannelsconf + ".tmp";
65         std::ofstream stream(permchannelsnewconf.c_str());
66         if (!stream.is_open())
67         {
68                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot create database \"%s\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
69                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
70                 return false;
71         }
72
73         stream << "# This file is automatically generated by m_permchannels. Any changes will be overwritten." << std::endl
74                 << "<config format=\"xml\">" << std::endl;
75
76         const chan_hash& chans = ServerInstance->GetChans();
77         for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
78         {
79                 Channel* chan = i->second;
80                 if (!chan->IsModeSet(permchanmode))
81                         continue;
82
83                 std::string chanmodes = chan->ChanModes(true);
84                 if (save_listmodes)
85                 {
86                         std::string modes;
87                         std::string params;
88
89                         const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
90                         for (ModeParser::ListModeList::const_iterator j = listmodes.begin(); j != listmodes.end(); ++j)
91                         {
92                                 ListModeBase* lm = *j;
93                                 ListModeBase::ModeList* list = lm->GetList(chan);
94                                 if (!list || list->empty())
95                                         continue;
96
97                                 size_t n = 0;
98                                 // Append the parameters
99                                 for (ListModeBase::ModeList::const_iterator k = list->begin(); k != list->end(); ++k, n++)
100                                 {
101                                         params += k->mask;
102                                         params += ' ';
103                                 }
104
105                                 // Append the mode letters (for example "IIII", "gg")
106                                 modes.append(n, lm->GetModeChar());
107                         }
108
109                         if (!params.empty())
110                         {
111                                 // Remove the last space
112                                 params.erase(params.end()-1);
113
114                                 // If there is at least a space in chanmodes (that is, a non-listmode has a parameter)
115                                 // insert the listmode mode letters before the space. Otherwise just append them.
116                                 std::string::size_type p = chanmodes.find(' ');
117                                 if (p == std::string::npos)
118                                         chanmodes += modes;
119                                 else
120                                         chanmodes.insert(p, modes);
121
122                                 // Append the listmode parameters (the masks themselves)
123                                 chanmodes += ' ';
124                                 chanmodes += params;
125                         }
126                 }
127
128                 stream << "<permchannels channel=\"" << ServerConfig::Escape(chan->name)
129                         << "\" ts=\"" << chan->age
130                         << "\" topic=\"" << ServerConfig::Escape(chan->topic)
131                         << "\" topicts=\"" << chan->topicset
132                         << "\" topicsetby=\"" << ServerConfig::Escape(chan->setby)
133                         << "\" modes=\"" << ServerConfig::Escape(chanmodes)
134                         << "\">" << std::endl;
135         }
136
137         if (stream.fail())
138         {
139                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot write to new database \"%s\"! %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
140                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new permchan db \"%s\": %s (%d)", permchannelsnewconf.c_str(), strerror(errno), errno);
141                 return false;
142         }
143         stream.close();
144
145 #ifdef _WIN32
146         remove(permchannelsconf.c_str());
147 #endif
148         // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
149         if (rename(permchannelsnewconf.c_str(), permchannelsconf.c_str()) < 0)
150         {
151                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Cannot replace old database \"%s\" with new database \"%s\"! %s (%d)", permchannelsconf.c_str(), permchannelsnewconf.c_str(), strerror(errno), errno);
152                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old permchan db \"%s\" with new db \"%s\": %s (%d)", permchannelsconf.c_str(), permchannelsnewconf.c_str(), strerror(errno), errno);
153                 return false;
154         }
155
156         return true;
157 }
158
159 class ModulePermanentChannels
160         : public Module
161         , public Timer
162
163 {
164         PermChannel p;
165         bool dirty;
166         bool loaded;
167         bool save_listmodes;
168 public:
169
170         ModulePermanentChannels()
171                 : Timer(0, true)
172                 , p(this)
173                 , dirty(false)
174                 , loaded(false)
175         {
176         }
177
178         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
179         {
180                 ConfigTag* tag = ServerInstance->Config->ConfValue("permchanneldb");
181                 permchannelsconf = tag->getString("filename");
182                 save_listmodes = tag->getBool("listmodes");
183                 SetInterval(tag->getDuration("saveperiod", 5));
184
185                 if (!permchannelsconf.empty())
186                         permchannelsconf = ServerInstance->Config->Paths.PrependConfig(permchannelsconf);
187         }
188
189         void LoadDatabase()
190         {
191                 /*
192                  * Process config-defined list of permanent channels.
193                  * -- w00t
194                  */
195                 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
196                 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
197                 {
198                         ConfigTag* tag = i->second;
199                         std::string channel = tag->getString("channel");
200                         std::string modes = tag->getString("modes");
201
202                         if (!ServerInstance->IsChannel(channel))
203                         {
204                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Ignoring permchannels tag with invalid channel name (\"" + channel + "\")");
205                                 continue;
206                         }
207
208                         Channel *c = ServerInstance->FindChan(channel);
209
210                         if (!c)
211                         {
212                                 time_t TS = tag->getInt("ts", ServerInstance->Time(), 1);
213                                 c = new Channel(channel, TS);
214
215                                 time_t topicset = tag->getInt("topicts", 0);
216                                 std::string topic = tag->getString("topic");
217
218                                 if ((topicset != 0) || (!topic.empty()))
219                                 {
220                                         if (topicset == 0)
221                                                 topicset = ServerInstance->Time();
222                                         std::string topicsetby = tag->getString("topicsetby");
223                                         if (topicsetby.empty())
224                                                 topicsetby = ServerInstance->Config->ServerName;
225                                         c->SetTopic(ServerInstance->FakeClient, topic, topicset, &topicsetby);
226                                 }
227
228                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Added %s with topic %s", channel.c_str(), c->topic.c_str());
229
230                                 if (modes.empty())
231                                         continue;
232
233                                 irc::spacesepstream list(modes);
234                                 std::string modeseq;
235                                 std::string par;
236
237                                 list.GetToken(modeseq);
238
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)
241                                 {
242                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
243                                         if (mode)
244                                         {
245                                                 if (mode->NeedsParam(true))
246                                                         list.GetToken(par);
247                                                 else
248                                                         par.clear();
249
250                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
251                                         }
252                                 }
253
254                                 // We always apply the permchannels mode to permanent channels.
255                                 par.clear();
256                                 p.OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
257                         }
258                 }
259         }
260
261         ModResult OnRawMode(User* user, Channel* chan, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE
262         {
263                 if (chan && (chan->IsModeSet(p) || mh == &p))
264                         dirty = true;
265
266                 return MOD_RES_PASSTHRU;
267         }
268
269         void OnPostTopicChange(User*, Channel *c, const std::string&) CXX11_OVERRIDE
270         {
271                 if (c->IsModeSet(p))
272                         dirty = true;
273         }
274
275         bool Tick(time_t) CXX11_OVERRIDE
276         {
277                 if (dirty)
278                         WriteDatabase(p, this, save_listmodes);
279                 dirty = false;
280                 return true;
281         }
282
283         void Prioritize() CXX11_OVERRIDE
284         {
285                 // XXX: Load the DB here because the order in which modules are init()ed at boot is
286                 // alphabetical, this means we must wait until all modules have done their init()
287                 // to be able to set the modes they provide (e.g.: m_stripcolor is inited after us)
288                 // Prioritize() is called after all module initialization is complete, consequently
289                 // all modes are available now
290                 if (loaded)
291                         return;
292
293                 loaded = true;
294
295                 // Load only when there are no linked servers - we set the TS of the channels we
296                 // create to the current time, this can lead to desync because spanningtree has
297                 // no way of knowing what we do
298                 ProtocolInterface::ServerList serverlist;
299                 ServerInstance->PI->GetServerList(serverlist);
300                 if (serverlist.size() < 2)
301                 {
302                         try
303                         {
304                                 LoadDatabase();
305                         }
306                         catch (CoreException& e)
307                         {
308                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error loading permchannels database: " + std::string(e.GetReason()));
309                         }
310                 }
311         }
312
313         Version GetVersion() CXX11_OVERRIDE
314         {
315                 return Version("Provides channel mode +P to provide permanent channels", VF_VENDOR);
316         }
317
318         ModResult OnChannelPreDelete(Channel *c) CXX11_OVERRIDE
319         {
320                 if (c->IsModeSet(p))
321                         return MOD_RES_DENY;
322
323                 return MOD_RES_PASSTHRU;
324         }
325 };
326
327 MODULE_INIT(ModulePermanentChannels)