]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
Use FindNickOnly in a few commands to prevent enumerating users via UID walking
[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         fputs("# Permchannels DB\n# This file is autogenerated; any changes will be overwritten!\n<config format=\"compat\">\n", f);
46         // Now, let's write.
47         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
48         {
49                 Channel* chan = i->second;
50                 if (!chan->IsModeSet('P'))
51                         continue;
52
53                 char line[1024];
54                 const char* items[] =
55                 {
56                         "<permchannels channel=",
57                         chan->name.c_str(),
58                         " topic=",
59                         chan->topic.c_str(),
60                         " modes=",
61                         chan->ChanModes(true),
62                         ">\n"
63                 };
64
65                 int lpos = 0, item = 0, ipos = 0;
66                 while (lpos < 1022 && item < 7)
67                 {
68                         char c = items[item][ipos++];
69                         if (c == 0)
70                         {
71                                 // end of this string; hop to next string, insert a quote
72                                 item++;
73                                 ipos = 0;
74                                 c = '"';
75                         }
76                         else if (c == '\\' || c == '"')
77                         {
78                                 line[lpos++] = '\\';
79                         }
80                         line[lpos++] = c;
81                 }
82                 line[--lpos] = 0;
83                 fputs(line, f);
84         }
85
86         int write_error = 0;
87         write_error = ferror(f);
88         write_error |= fclose(f);
89         if (write_error)
90         {
91                 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot write to new database! %s (%d)", strerror(errno), errno);
92                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
93                 return false;
94         }
95
96         // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
97         if (rename(tempname.c_str(), permchannelsconf.c_str()) < 0)
98         {
99                 ServerInstance->Logs->Log("m_permchannels",DEFAULT, "permchannels: Cannot move new to old database! %s (%d)", strerror(errno), errno);
100                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
101                 return false;
102         }
103
104         return true;
105 }
106
107
108
109 /** Handles the +P channel mode
110  */
111 class PermChannel : public ModeHandler
112 {
113  public:
114         PermChannel(Module* Creator) : ModeHandler(Creator, "permanent", 'P', PARAM_NONE, MODETYPE_CHANNEL) { oper = true; }
115
116         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
117         {
118                 if (adding)
119                 {
120                         if (!channel->IsModeSet('P'))
121                         {
122                                 channel->SetMode('P',true);
123                                 return MODEACTION_ALLOW;
124                         }
125                 }
126                 else
127                 {
128                         if (channel->IsModeSet('P'))
129                         {
130                                 channel->SetMode(this,false);
131                                 if (channel->GetUserCounter() == 0)
132                                 {
133                                         channel->DelUser(ServerInstance->FakeClient);
134                                 }
135                                 return MODEACTION_ALLOW;
136                         }
137                 }
138
139                 return MODEACTION_DENY;
140         }
141 };
142
143 class ModulePermanentChannels : public Module
144 {
145         PermChannel p;
146         bool dirty;
147 public:
148
149         ModulePermanentChannels() : p(this), dirty(false)
150         {
151         }
152
153         void init()
154         {
155                 ServerInstance->Modules->AddService(p);
156                 Implementation eventlist[] = { I_OnChannelPreDelete, I_OnPostTopicChange, I_OnRawMode, I_OnRehash, I_OnBackgroundTimer };
157                 ServerInstance->Modules->Attach(eventlist, this, 5);
158
159                 OnRehash(NULL);
160         }
161
162         CullResult cull()
163         {
164                 /*
165                  * DelMode can't remove the +P mode on empty channels, or it will break
166                  * merging modes with remote servers. Remove the empty channels now as
167                  * we know this is not the case.
168                  */
169                 chan_hash::iterator iter = ServerInstance->chanlist->begin();
170                 while (iter != ServerInstance->chanlist->end())
171                 {
172                         Channel* c = iter->second;
173                         if (c->GetUserCounter() == 0)
174                         {
175                                 chan_hash::iterator at = iter;
176                                 iter++;
177                                 FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(c));
178                                 ServerInstance->chanlist->erase(at);
179                                 ServerInstance->GlobalCulls.AddItem(c);
180                         }
181                         else
182                                 iter++;
183                 }
184                 ServerInstance->Modes->DelMode(&p);
185                 return Module::cull();
186         }
187
188         virtual void OnRehash(User *user)
189         {
190                 /*
191                  * Process config-defined list of permanent channels.
192                  * -- w00t
193                  */
194
195                 permchannelsconf = ServerInstance->Config->ConfValue("permchanneldb")->getString("filename");
196
197                 ConfigTagList permchannels = ServerInstance->Config->ConfTags("permchannels");
198                 for (ConfigIter i = permchannels.first; i != permchannels.second; ++i)
199                 {
200                         ConfigTag* tag = i->second;
201                         std::string channel = tag->getString("channel");
202                         std::string topic = tag->getString("topic");
203                         std::string modes = tag->getString("modes");
204
205                         if (channel.empty())
206                         {
207                                 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
208                                 continue;
209                         }
210
211                         Channel *c = ServerInstance->FindChan(channel);
212
213                         if (!c)
214                         {
215                                 c = new Channel(channel, ServerInstance->Time());
216                                 if (!topic.empty())
217                                 {
218                                         c->SetTopic(NULL, topic, true);
219
220                                         /*
221                                          * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
222                                          * topic will always win over others.
223                                          *
224                                          * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
225                                          */
226                                         c->topicset = 42;
227                                 }
228                                 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), 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->GetNumParams(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                 }
255         }
256
257         virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string &param, bool adding, int pcnt)
258         {
259                 if (chan && (chan->IsModeSet('P') || mode == 'P'))
260                         dirty = true;
261
262                 return MOD_RES_PASSTHRU;
263         }
264
265         virtual void OnPostTopicChange(User*, Channel *c, const std::string&)
266         {
267                 if (c->IsModeSet('P'))
268                         dirty = true;
269         }
270
271         void OnBackgroundTimer(time_t)
272         {
273                 if (dirty)
274                         WriteDatabase();
275                 dirty = false;
276         }
277
278         virtual Version GetVersion()
279         {
280                 return Version("Provides support for channel mode +P to provide permanent channels",VF_VENDOR);
281         }
282
283         virtual ModResult OnChannelPreDelete(Channel *c)
284         {
285                 if (c->IsModeSet('P'))
286                         return MOD_RES_DENY;
287
288                 return MOD_RES_PASSTHRU;
289         }
290 };
291
292 MODULE_INIT(ModulePermanentChannels)