]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
...because every now and again, i have to do a massive commit.
[user/henk/code/inspircd.git] / src / modules / m_permchannels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides support for channel mode +P to provide permanent channels */
17
18 // Not in a class due to circular dependancy hell.
19 static std::string permchannelsconf;
20 static bool WriteDatabase()
21 {
22         FILE *f;
23
24         if (permchannelsconf.empty())
25         {
26                 // Fake success.
27                 return true;
28         }
29
30         std::string tempname = permchannelsconf + ".tmp";
31
32         /*
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..
35          *              -- w00t
36          */
37         f = fopen(tempname.c_str(), "w");
38         if (!f)
39         {
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);
42                 return false;
43         }
44
45         // Now, let's write.
46         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
47         {
48                 Channel* chan = i->second;
49                 if (!chan->IsModeSet('P'))
50                         continue;
51
52                 char line[1024];
53                 const char* items[] =
54                 {
55                         "<permchannels channel=",
56                         chan->name.c_str(),
57                         " topic=",
58                         chan->topic.c_str(),
59                         " modes=",
60                         chan->ChanModes(true),
61                         ">\n"
62                 };
63
64                 int lpos = 0, item = 0, ipos = 0;
65                 while (lpos < 1022 && item < 7)
66                 {
67                         char c = items[item][ipos++];
68                         if (c == 0)
69                         {
70                                 // end of this string; hop to next string, insert a quote
71                                 item++;
72                                 ipos = 0;
73                                 c = '"';
74                         }
75                         else if (c == '\\' || c == '"')
76                         {
77                                 line[lpos++] = '\\';
78                         }
79                         line[lpos++] = c;
80                 }
81                 line[--lpos] = 0;
82                 fputs(line, f);
83         }
84
85         int write_error = 0;
86         write_error = ferror(f);
87         write_error |= fclose(f);
88         if (write_error)
89         {
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);
92                 return false;
93         }
94
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)
97         {
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);
100                 return false;
101         }
102
103         return true;
104 }
105
106
107
108 /** Handles the +P channel mode
109  */
110 class PermChannel : public ModeHandler
111 {
112  public:
113         PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { }
114
115         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
116         {
117                 if (!source->HasPrivPermission("channels/set-permanent"))
118                 {
119                         source->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges", source->nick.c_str());
120                         return MODEACTION_DENY;
121                 }
122
123                 if (adding)
124                 {
125                         if (!channel->IsModeSet('P'))
126                         {
127                                 channel->SetMode('P',true);
128
129                                 // Save permchannels db if needed.
130                                 WriteDatabase();
131                                 return MODEACTION_ALLOW;
132                         }
133                 }
134                 else
135                 {
136                         if (channel->IsModeSet('P'))
137                         {
138                                 if (channel->GetUserCounter() == 0 && !IS_SERVER(source))
139                                 {
140                                         /*
141                                          * ugh, ugh, UGH!
142                                          *
143                                          * We can't delete this channel the way things work at the moment,
144                                          * because of the following scenario:
145                                          * s1:#c <-> s2:#c
146                                          *
147                                          * s1 has a user in #c, s2 does not. s2 has +P set. s2 has a losing TS.
148                                          *
149                                          * On netmerge, s2 loses, so s2 removes all modes (including +P) which
150                                          * would subsequently delete the channel here causing big fucking problems.
151                                          *
152                                          * I don't think there's really a way around this, so just deny -P on a 0 user chan.
153                                          * -- w00t
154                                          *
155                                          * delete channel;
156                                          */
157                                         return MODEACTION_DENY;
158                                 }
159
160                                 /* for servers, remove +P (to avoid desyncs) but don't bother trying to delete. */
161                                 channel->SetMode('P',false);
162
163                                 // Save permchannels db if needed.
164                                 WriteDatabase();
165                                 return MODEACTION_ALLOW;
166                         }
167                 }
168
169                 return MODEACTION_DENY;
170         }
171 };
172
173 class ModulePermanentChannels : public Module
174 {
175         PermChannel p;
176 public:
177
178         ModulePermanentChannels() : p(this)
179         {
180                 if (!ServerInstance->Modes->AddMode(&p))
181                         throw ModuleException("Could not add new modes!");
182                 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash };
183                 ServerInstance->Modules->Attach(eventlist, this, 4);
184
185                 OnRehash(NULL);
186         }
187
188         CullResult cull()
189         {
190                 /*
191                  * DelMode can't remove the +P mode on empty channels, or it will break
192                  * merging modes with remote servers. Remove the empty channels now as
193                  * we know this is not the case.
194                  */
195                 chan_hash::iterator iter = ServerInstance->chanlist->begin();
196                 while (iter != ServerInstance->chanlist->end())
197                 {
198                         Channel* c = iter->second;
199                         if (c->GetUserCounter() == 0)
200                         {
201                                 chan_hash::iterator at = iter;
202                                 iter++;
203                                 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
204                                 ServerInstance->chanlist->erase(at);
205                                 ServerInstance->GlobalCulls.AddItem(c);
206                         }
207                         else
208                                 iter++;
209                 }
210                 ServerInstance->Modes->DelMode(&p);
211                 return Module::cull();
212         }
213
214         virtual void OnRehash(User *user)
215         {
216                 /*
217                  * Process config-defined list of permanent channels.
218                  * -- w00t
219                  */
220                 ConfigReader MyConf;
221
222                 permchannelsconf = MyConf.ReadValue("permchanneldb", "filename", "", 0, false);
223
224                 for (int i = 0; i < MyConf.Enumerate("permchannels"); i++)
225                 {
226                         std::string channel = MyConf.ReadValue("permchannels", "channel", i);
227                         std::string topic = MyConf.ReadValue("permchannels", "topic", i);
228                         std::string modes = MyConf.ReadValue("permchannels", "modes", i);
229
230                         if (channel.empty())
231                         {
232                                 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
233                                 continue;
234                         }
235
236                         Channel *c = ServerInstance->FindChan(channel);
237
238                         if (!c)
239                         {
240                                 c = new Channel(channel, ServerInstance->Time());
241                                 if (!topic.empty())
242                                 {
243                                         c->SetTopic(NULL, topic, true);
244
245                                         /*
246                                          * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
247                                          * topic will always win over others.
248                                          *
249                                          * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
250                                          */
251                                         c->topicset = 42;
252                                 }
253                                 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
254
255                                 if (modes.empty())
256                                         continue;
257
258                                 irc::spacesepstream list(modes);
259                                 std::string modeseq;
260                                 std::string par;
261
262                                 list.GetToken(modeseq);
263
264                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
265                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
266                                 {
267                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
268                                         if (mode)
269                                         {
270                                                 if (mode->GetNumParams(true))
271                                                         list.GetToken(par);
272                                                 else
273                                                         par.clear();
274
275                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
276                                         }
277                                 }
278                         }
279                 }
280         }
281
282         virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt)
283         {
284                 if (chan && chan->IsModeSet('P'))
285                         WriteDatabase();
286
287                 return MOD_RES_PASSTHRU;
288         }
289
290         virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
291         {
292                 if (c->IsModeSet('P'))
293                         WriteDatabase();
294         }
295
296         virtual Version GetVersion()
297         {
298                 return Version("Provides support for channel mode +P to provide permanent channels",VF_COMMON|VF_VENDOR);
299         }
300
301         virtual ModResult OnChannelPreDelete(Channel *c)
302         {
303                 if (c->IsModeSet('P'))
304                         return MOD_RES_DENY;
305
306                 return MOD_RES_PASSTHRU;
307         }
308 };
309
310 MODULE_INIT(ModulePermanentChannels)