]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
83dc2fd6faa059796836cd549aa2f38169986aaa
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
1 #include <stdio.h>
2
3 #include "users.h"
4 #include "channels.h"
5 #include "modules.h"
6
7 /* $ModDesc: Provides channel modes +a and +q */
8
9 char dummyvalue[] = "on";
10
11 class ModuleChanProtect : public Module
12 {
13         Server *Srv;
14         
15  public:
16  
17         ModuleChanProtect()
18         {
19                 Srv = new Server;
20                 // set up our modes. We're using listmodes and not normal extmodes here.
21                 // listmodes only need one parameter as everything else is assumed by the
22                 // nature of the mode thats being created.
23                 Srv->AddExtendedListMode('a');
24                 Srv->AddExtendedListMode('q');
25         }
26         
27         virtual void OnUserJoin(userrec* user, chanrec* channel)
28         {
29                 // if the user is the first user into the channel, mark them as the founder
30                 if (Srv->CountUsers(channel) == 1)
31                 {
32                         if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
33                         {
34                                 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
35                         }
36                 }
37         }
38         
39         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
40         {
41                 // not out mode, bail
42                 if ((modechar == 'q') && (type == MT_CHANNEL))
43                 {
44                         // set up parameters
45                         chanrec* chan = (chanrec*)target;
46                         userrec* theuser = Srv->FindNick(params[0]);
47                 
48                         // cant find the user given as the parameter, eat the mode change.
49                         if (!theuser)
50                                 return -1;
51                         
52                         // given user isnt even on the channel, eat the mode change
53                         if (!Srv->IsOnChannel(theuser,chan))
54                                 return -1;
55                         
56                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
57                         {
58                                 if (mode_on)
59                                 {
60                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
61                                         {
62                                                 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
63                                                 return 1;
64                                         }
65                                 }
66                                 else
67                                 {
68                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
69                                         {
70                                                 theuser->Shrink("cm_founder_"+std::string(chan->name));
71                                                 return 1;
72                                         }
73                                 }       
74
75                                 return -1;
76                         }
77                         else
78                         {
79                                 WriteServ(user->fd,"482 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
80                                 return -1;
81                         }
82                 }
83                 if ((modechar == 'a') && (type == MT_CHANNEL))
84                 {
85                         // set up parameters
86                         chanrec* chan = (chanrec*)target;
87                         userrec* theuser = Srv->FindNick(params[0]);
88                 
89                         // cant find the user given as the parameter, eat the mode change.
90                         if (!theuser)
91                                 return -1;
92                         
93                         // given user isnt even on the channel, eat the mode change
94                         if (!Srv->IsOnChannel(theuser,chan))
95                                 return -1;
96                         
97                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
98                         {
99                                 if (mode_on)
100                                 {
101                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
102                                         {
103                                                 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
104                                                 return 1;
105                                         }
106                                 }
107                                 else
108                                 {
109                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
110                                         {
111                                                 theuser->Shrink("cm_protect_"+std::string(chan->name));
112                                                 return 1;
113                                         }
114                                 }       
115
116                                 return -1;
117                         }
118                         else
119                         {
120                                 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
121                                 return -1;
122                         }
123                 }
124                 return 0;
125         }
126         
127         virtual ~ModuleChanProtect()
128         {
129                 delete Srv;
130         }
131         
132         virtual Version GetVersion()
133         {
134                 return Version(1,0,0,0);
135         }
136         
137         virtual void OnUserConnect(userrec* user)
138         {
139         }
140
141 };
142
143
144 class ModuleChanProtectFactory : public ModuleFactory
145 {
146  public:
147         ModuleChanProtectFactory()
148         {
149         }
150         
151         ~ModuleChanProtectFactory()
152         {
153         }
154         
155         virtual Module * CreateModule()
156         {
157                 return new ModuleChanProtect;
158         }
159         
160 };
161
162
163 extern "C" void * init_module( void )
164 {
165         return new ModuleChanProtectFactory;
166 }
167