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