]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
Add Module* creator to Command and ModeHandler
[user/henk/code/inspircd.git] / src / modules / m_permchannels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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
19 /** Handles the +P channel mode
20  */
21 class PermChannel : public ModeHandler
22 {
23  public:
24         PermChannel(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'P', 0, 0, false, MODETYPE_CHANNEL, false) { }
25
26         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool sm)
27         {
28                 if (!source->HasPrivPermission("channels/set-permanent"))
29                 {
30                         source->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - You do not have the required operator privileges", source->nick.c_str());
31                         return MODEACTION_DENY;
32                 }
33
34                 if (adding)
35                 {
36                         if (!channel->IsModeSet('P'))
37                         {
38                                 channel->SetMode('P',true);
39                                 return MODEACTION_ALLOW;
40                         }
41                 }
42                 else
43                 {
44                         if (channel->IsModeSet('P'))
45                         {
46                                 if (channel->GetUserCounter() == 0 && !sm)
47                                 {
48                                         /*
49                                          * ugh, ugh, UGH!
50                                          *
51                                          * We can't delete this channel the way things work at the moment,
52                                          * because of the following scenario:
53                                          * s1:#c <-> s2:#c
54                                          *
55                                          * s1 has a user in #c, s2 does not. s2 has +P set. s2 has a losing TS.
56                                          *
57                                          * On netmerge, s2 loses, so s2 removes all modes (including +P) which
58                                          * would subsequently delete the channel here causing big fucking problems.
59                                          *
60                                          * I don't think there's really a way around this, so just deny -P on a 0 user chan.
61                                          * -- w00t
62                                          *
63                                          * delete channel;
64                                          */
65                                         return MODEACTION_DENY;
66                                 }
67
68                                 /* for servers, remove +P (to avoid desyncs) but don't bother trying to delete. */
69                                 channel->SetMode('P',false);
70                                 return MODEACTION_ALLOW;
71                         }
72                 }
73
74                 return MODEACTION_DENY;
75         }
76 };
77
78 class ModulePermanentChannels : public Module
79 {
80         PermChannel p;
81 public:
82
83         ModulePermanentChannels(InspIRCd* Me) : Module(Me), p(Me, this)
84         {
85                 if (!ServerInstance->Modes->AddMode(&p))
86                         throw ModuleException("Could not add new modes!");
87                 Implementation eventlist[] = { I_OnChannelPreDelete };
88                 ServerInstance->Modules->Attach(eventlist, this, 1);
89
90                 OnRehash(NULL);
91         }
92
93         virtual ~ModulePermanentChannels()
94         {
95                 ServerInstance->Modes->DelMode(&p);
96                 /*
97                  * DelMode can't remove the +P mode on empty channels, or it will break
98                  * merging modes with remote servers. Remove the empty channels now as
99                  * we know this is not the case.
100                  */
101                 chan_hash::iterator iter = ServerInstance->chanlist->begin();
102                 while (iter != ServerInstance->chanlist->end())
103                 {
104                         Channel* c = iter->second;
105                         if (c->GetUserCounter() == 0)
106                         {
107                                 chan_hash::iterator at = iter;
108                                 iter++;
109                                 ServerInstance->chanlist->erase(at);
110                                 delete c;
111                         }
112                         else
113                                 iter++;
114                 }
115         }
116
117         virtual void OnRehash(User *user)
118         {
119                 /*
120                  * Process config-defined list of permanent channels.
121                  * -- w00t
122                  */
123                 ConfigReader MyConf(ServerInstance);
124                 for (int i = 0; i < MyConf.Enumerate("permchannels"); i++)
125                 {
126                         std::string channel = MyConf.ReadValue("permchannels", "channel", i);
127                         std::string topic = MyConf.ReadValue("permchannels", "topic", i);
128                         std::string modes = MyConf.ReadValue("permchannels", "modes", i);
129
130                         if (channel.empty())
131                         {
132                                 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
133                                 continue;
134                         }
135
136                         Channel *c = ServerInstance->FindChan(channel);
137
138                         if (!c)
139                         {
140                                 c = new Channel(ServerInstance, channel, ServerInstance->Time());
141                                 if (!topic.empty())
142                                 {
143                                         c->SetTopic(NULL, topic, true);
144
145                                         /*
146                                          * Due to the way protocol works in 1.2, we need to hack the topic TS in such a way that this
147                                          * topic will always win over others.
148                                          *
149                                          * This is scheduled for (proper) fixing in a later release, and can be removed at a later date.
150                                          */
151                                         c->topicset = 42;
152                                 }
153                                 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
154
155                                 if (modes.empty())
156                                         continue;
157
158                                 irc::spacesepstream list(modes);
159                                 std::string modeseq;
160                                 std::string par;
161
162                                 list.GetToken(modeseq);
163
164                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
165                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
166                                 {
167                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
168                                         if (mode)
169                                         {
170                                                 if (mode->GetNumParams(true))
171                                                         list.GetToken(par);
172                                                 else
173                                                         par.clear();
174
175                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
176                                         }
177                                 }
178                         }
179                 }
180         }
181
182         virtual Version GetVersion()
183         {
184                 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
185         }
186
187         virtual int OnChannelPreDelete(Channel *c)
188         {
189                 if (c->IsModeSet('P'))
190                         return 1;
191
192                 return 0;
193         }
194 };
195
196 MODULE_INIT(ModulePermanentChannels)