]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_permchannels.cpp
Change module versions to use a string instead of fixed digits, and use propset ID...
[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)
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                                 channel->SetMode('P',false);
41
42                                 if (channel->GetUserCounter() == 0)
43                                         delete channel;
44                                 return MODEACTION_ALLOW;
45                         }
46                 }
47
48                 return MODEACTION_DENY;
49         }
50 };
51
52 class ModulePermanentChannels : public Module
53 {
54         PermChannel *p;
55 public:
56
57         ModulePermanentChannels(InspIRCd* Me) : Module(Me)
58         {
59                 p = new PermChannel(ServerInstance);
60                 if (!ServerInstance->Modes->AddMode(p))
61                 {
62                         delete p;
63                         throw ModuleException("Could not add new modes!");
64                 }
65                 Implementation eventlist[] = { I_OnChannelPreDelete };
66                 ServerInstance->Modules->Attach(eventlist, this, 1);
67
68                 OnRehash(NULL, "");
69         }
70
71         virtual ~ModulePermanentChannels()
72         {
73                 ServerInstance->Modes->DelMode(p);
74                 delete p;
75         }
76
77         virtual void OnRehash(User *user, const std::string &parameter)
78         {
79                 /*
80                  * Process config-defined list of permanent channels.
81                  * -- w00t
82                  */
83                 ConfigReader MyConf(ServerInstance);
84                 for (int i = 0; i < MyConf.Enumerate("permchannels"); i++)
85                 {
86                         std::string channel = MyConf.ReadValue("permchannels", "channel", i);
87                         std::string topic = MyConf.ReadValue("permchannels", "topic", i);
88                         std::string modes = MyConf.ReadValue("permchannels", "modes", i);
89
90                         if (channel.empty() || topic.empty())
91                         {
92                                 ServerInstance->Logs->Log("blah", DEBUG, "Malformed permchannels tag with empty topic or channel name.");
93                                 continue;
94                         }
95
96                         Channel *c = ServerInstance->FindChan(channel);
97
98                         if (!c)
99                         {
100                                 c = new Channel(ServerInstance, channel, ServerInstance->Time());
101                                 c->SetTopic(NULL, topic, true);
102                                 ServerInstance->Logs->Log("blah", DEBUG, "Added %s with topic %s", channel.c_str(), topic.c_str());
103
104                                 if (modes.empty())
105                                         continue;
106
107                                 irc::spacesepstream list(modes);
108                                 std::string modeseq;
109                                 std::string par;
110
111                                 list.GetToken(modeseq);
112
113                                 // XXX bleh, should we pass this to the mode parser instead? ugly. --w00t
114                                 for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
115                                 {
116                                         ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
117                                         if (mode)
118                                         {
119                                                 if (mode->GetNumParams(true))
120                                                         list.GetToken(par);
121                                                 else
122                                                         par.clear();
123
124                                                 mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, c, par, true);
125                                         }
126                                 }
127                         }
128                 }
129         }
130
131         virtual Version GetVersion()
132         {
133                 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
134         }
135
136         virtual int OnChannelPreDelete(Channel *c)
137         {
138                 if (c->IsModeSet('P'))
139                         return 1;
140
141                 return 0;
142         }
143 };
144
145 MODULE_INIT(ModulePermanentChannels)