]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
Readd check for +P setting to channels/set-permanent priv, thanks Johannes13 - naught...
[user/henk/code/inspircd.git] / src / modules / m_permchannels.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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) : ModeHandler(Instance, '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                         return MODEACTION_DENY;
30
31                 if (adding)
32                 {
33                         if (!channel->IsModeSet('P'))
34                         {
35                                 channel->SetMode('P',true);
36                                 return MODEACTION_ALLOW;
37                         }
38                 }
39                 else
40                 {
41                         if (channel->IsModeSet('P'))
42                         {
43                                 if (channel->GetUserCounter() == 0 && !sm)
44                                 {
45                                         /*
46                                          * ugh, ugh, UGH!
47                                          *
48                                          * We can't delete this channel the way things work at the moment,
49                                          * because of the following scenario:
50                                          * s1:#c <-> s2:#c
51                                          *
52                                          * s1 has a user in #c, s2 does not. s2 has +P set. s2 has a losing TS.
53                                          *
54                                          * On netmerge, s2 loses, so s2 removes all modes (including +P) which
55                                          * would subsequently delete the channel here causing big fucking problems.
56                                          *
57                                          * I don't think there's really a way around this, so just deny -P on a 0 user chan.
58                                          * -- w00t
59                                          *
60                                          * delete channel;
61                                          */
62                                         return MODEACTION_DENY;
63                                 }
64
65                                 /* for servers, remove +P (to avoid desyncs) but don't bother trying to delete. */
66                                 channel->SetMode('P',false);
67                                 return MODEACTION_ALLOW;
68                         }
69                 }
70
71                 return MODEACTION_DENY;
72         }
73 };
74
75 class ModulePermanentChannels : public Module
76 {
77         PermChannel *p;
78 public:
79
80         ModulePermanentChannels(InspIRCd* Me) : Module(Me)
81         {
82                 p = new PermChannel(ServerInstance);
83                 if (!ServerInstance->Modes->AddMode(p))
84                 {
85                         delete p;
86                         throw ModuleException("Could not add new modes!");
87                 }
88                 Implementation eventlist[] = { I_OnChannelPreDelete };
89                 ServerInstance->Modules->Attach(eventlist, this, 1);
90
91                 OnRehash(NULL, "");
92         }
93
94         virtual ~ModulePermanentChannels()
95         {
96                 ServerInstance->Modes->DelMode(p);
97                 delete p;
98         }
99
100         virtual void OnRehash(User *user, const std::string &parameter)
101         {
102                 /*
103                  * Process config-defined list of permanent channels.
104                  * -- w00t
105                  */
106                 ConfigReader MyConf(ServerInstance);
107                 for (int i = 0; i < MyConf.Enumerate("permchannels"); i++)
108                 {
109                         std::string channel = MyConf.ReadValue("permchannels", "channel", i);
110                         std::string topic = MyConf.ReadValue("permchannels", "topic", i);
111                         std::string modes = MyConf.ReadValue("permchannels", "modes", i);
112
113                         if (channel.empty())
114                         {
115                                 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty channel name.");
116                                 continue;
117                         }
118
119                         Channel *c = ServerInstance->FindChan(channel);
120
121                         if (!c)
122                         {
123                                 c = new Channel(ServerInstance, channel, ServerInstance->Time());
124                                 if (!topic.empty())
125                                         c->SetTopic(NULL, topic, true);
126                                 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
127
128                                 if (modes.empty())
129                                         continue;
130
131                                 irc::spacesepstream list(modes);
132                                 std::string modeseq;
133                                 std::string par;
134
135                                 list.GetToken(modeseq);
136
137                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
138                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
139                                 {
140                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
141                                         if (mode)
142                                         {
143                                                 if (mode->GetNumParams(true))
144                                                         list.GetToken(par);
145                                                 else
146                                                         par.clear();
147
148                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
149                                         }
150                                 }
151                         }
152                 }
153         }
154
155         virtual Version GetVersion()
156         {
157                 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
158         }
159
160         virtual int OnChannelPreDelete(Channel *c)
161         {
162                 if (c->IsModeSet('P'))
163                         return 1;
164
165                 return 0;
166         }
167 };
168
169 MODULE_INIT(ModulePermanentChannels)