]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
Merge pull request #92 from Robby-/insp20-headers
[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
23 /* $ModDesc: Provides support for channel mode +P to provide permanent channels */
24
25 // Not in a class due to circular dependancy hell.
26 static std::string permchannelsconf;
27 static bool WriteDatabase()
28 {
29         FILE *f;
30
31         if (permchannelsconf.empty())
32         {
33                 // Fake success.
34                 return true;
35         }
36
37         std::string tempname = permchannelsconf + ".tmp";
38
39         /*
40          * We need to perform an atomic write so as not to fuck things up.
41          * So, let's write to a temporary file, flush and sync the FD, then rename the file..
42          *              -- w00t
43          */
44         f = fopen(tempname.c_str(), "w");
45         if (!f)
46         {
47                 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot create database! %s (%d)", strerror(errno), errno);
48                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
49                 return false;
50         }
51
52         fputs("# Permchannels DB\n# This file is autogenerated; any changes will be overwritten!\n<config format=\"compat\">\n", f);
53         // Now, let's write.
54         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
55         {
56                 Channel* chan = i->second;
57                 if (!chan->IsModeSet('P'))
58                         continue;
59
60                 char line[1024];
61                 const char* items[] =
62                 {
63                         "<permchannels channel=",
64                         chan->name.c_str(),
65                         " topic=",
66                         chan->topic.c_str(),
67                         " modes=",
68                         chan->ChanModes(true),
69                         ">\n"
70                 };
71
72                 int lpos = 0, item = 0, ipos = 0;
73                 while (lpos < 1022 && item < 7)
74                 {
75                         char c = items[item][ipos++];
76                         if (c == 0)
77                         {
78                                 // end of this string; hop to next string, insert a quote
79                                 item++;
80                                 ipos = 0;
81                                 c = '"';
82                         }
83                         else if (c == '\\' || c == '"')
84                         {
85                                 line[lpos++] = '\\';
86                         }
87                         line[lpos++] = c;
88                 }
89                 line[--lpos] = 0;
90                 fputs(line, f);
91         }
92
93         int write_error = 0;
94         write_error = ferror(f);
95         write_error |= fclose(f);
96         if (write_error)
97         {
98                 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
99                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
100                 return false;
101         }
102
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(tempname.c_str(), permchannelsconf.c_str()) < 0)
105         {
106                 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: 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
115
116 /** Handles the +P channel mode
117  */
118 class PermChannel : public ModeHandler
119 {
120  public:
121         PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
122
123         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
124         {
125                 if (adding)
126                 {
127                         if (!channel->IsModeSet('P'))
128                         {
129                                 channel->SetMode('P',true);
130                                 return MODEACTION_ALLOW;
131                         }
132                 }
133                 else
134                 {
135                         if (channel->IsModeSet('P'))
136                         {
137                                 channel->SetMode(this,false);
138                                 if (channel->GetUserCounter() == 0)
139                                 {
140                                         channel->DelUser(ServerInstance->FakeClient);
141                                 }
142                                 return MODEACTION_ALLOW;
143                         }
144                 }
145
146                 return MODEACTION_DENY;
147         }
148 };
149
150 class ModulePermanentChannels : public Module
151 {
152         PermChannel p;
153         bool dirty;
154 public:
155
156         ModulePermanentChannels() : p(this), dirty(false)
157         {
158         }
159
160         void init()
161         {
162                 ServerInstance->Modules->AddService(p);
163                 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
164                 ServerInstance->Modules->Attach(eventlist, this, 5);
165
166                 OnRehash(NULL);
167         }
168
169         CullResult cull()
170         {
171                 /*
172                  * DelMode can't remove the +P mode on empty channels, or it will break
173                  * merging modes with remote servers. Remove the empty channels now as
174                  * we know this is not the case.
175                  */
176                 chan_hash::iterator iter = ServerInstance->chanlist->begin();
177                 while (iter != ServerInstance->chanlist->end())
178                 {
179                         Channel* c = iter->second;
180                         if (c->GetUserCounter() == 0)
181                         {
182                                 chan_hash::iterator at = iter;
183                                 iter++;
184                                 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
185                                 ServerInstance->chanlist->erase(at);
186                                 ServerInstance->GlobalCulls.AddItem(c);
187                         }
188                         else
189                                 iter++;
190                 }
191                 ServerInstance->Modes->DelMode(&p);
192                 return Module::cull();
193         }
194
195         virtual void OnRehash(User *user)
196         {
197                 /*
198                  * Process config-defined list of permanent channels.
199                  * -- w00t
200                  */
201
202                 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
203
204                 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
205                 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
206                 {
207                         ConfigTag* tag = i->second;
208                         std::string channel = tag->getString("channel");
209                         std::string topic = tag->getString("topic");
210                         std::string modes = tag->getString("modes");
211
212                         if (channel.empty())
213                         {
214                                 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
215                                 continue;
216                         }
217
218                         Channel *c = ServerInstance->FindChan(channel);
219
220                         if (!c)
221                         {
222                                 c = new Channel(channel, ServerInstance->Time());
223                                 if (!topic.empty())
224                                 {
225                                         c->SetTopic(NULL, topic, true);
226
227                                         /*
228                                          * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
229                                          * topic will always win over others.
230                                          *
231                                          * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
232                                          */
233                                         c->topicset = 42;
234                                 }
235                                 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
236
237                                 if (modes.empty())
238                                         continue;
239
240                                 irc::spacesepstream list(modes);
241                                 std::string modeseq;
242                                 std::string par;
243
244                                 list.GetToken(modeseq);
245
246                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
247                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
248                                 {
249                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
250                                         if (mode)
251                                         {
252                                                 if (mode->GetNumParams(true))
253                                                         list.GetToken(par);
254                                                 else
255                                                         par.clear();
256
257                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
258                                         }
259                                 }
260                         }
261                 }
262         }
263
264         virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt)
265         {
266                 if (chan && (chan->IsModeSet('P') || mode == 'P'))
267                         dirty = true;
268
269                 return MOD_RES_PASSTHRU;
270         }
271
272         virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
273         {
274                 if (c->IsModeSet('P'))
275                         dirty = true;
276         }
277
278         void OnBackgroundTimer(time_t)
279         {
280                 if (dirty)
281                         WriteDatabase();
282                 dirty = false;
283         }
284
285         virtual Version GetVersion()
286         {
287                 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
288         }
289
290         virtual ModResult OnChannelPreDelete(Channel *c)
291         {
292                 if (c->IsModeSet('P'))
293                         return MOD_RES_DENY;
294
295                 return MOD_RES_PASSTHRU;
296         }
297 };
298
299 MODULE_INIT(ModulePermanentChannels)