]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslmodes.cpp
Convert to templated GetExt
[user/henk/code/inspircd.git] / src / modules / m_sslmodes.cpp
1 #include "users.h"
2 #include "channels.h"
3 #include "modules.h"
4 #include "helperfuncs.h"
5
6 /* $ModDesc: Provides support for unreal-style channel mode +z */
7
8 class SSLMode : public ModeHandler
9 {
10         Server* Srv;
11  public:
12         SSLMode(Server* s) : ModeHandler('z', 0, 0, false, MODETYPE_CHANNEL, false), Srv(s) { }
13
14         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
15         {
16                 if (adding)
17                 {
18                         if (!channel->IsModeSet('z'))
19                         {
20                                 chanuserlist userlist = Srv->GetUsers(channel);
21                                 for(unsigned int i = 0; i < userlist.size(); i++)
22                                 {
23                                         if(!userlist[i]->GetExt("ssl"))
24                                         {
25                                                 WriteServ(source->fd, "490 %s %s :all members of the channel must be connected via SSL", source->nick, channel->name);
26                                                 return MODEACTION_DENY;
27                                         }
28                                 }
29                                 channel->SetMode('z',true);
30                                 return MODEACTION_ALLOW;
31                         }
32                         else
33                         {
34                                 return MODEACTION_DENY;
35                         }
36                 }
37                 else
38                 {
39                         if (channel->IsModeSet('z'))
40                         {
41                                 channel->SetMode('z',false);
42                                 return MODEACTION_ALLOW;
43                         }
44
45                         return MODEACTION_DENY;
46                 }
47         }
48 };
49
50 class ModuleSSLModes : public Module
51 {
52         Server *Srv;
53         SSLMode* sslm;
54         
55  public:
56         ModuleSSLModes(Server* Me)
57                 : Module::Module(Me)
58         {
59                 Srv = Me;
60
61                 sslm = new SSLMode(Me);
62                 Srv->AddMode(sslm, 'z');
63         }
64
65         void Implements(char* List)
66         {
67                 List[I_On005Numeric] = List[I_OnUserPreJoin] = 1;
68         }
69
70         virtual void On005Numeric(std::string &output)
71         {
72                 InsertMode(output, "z", 4);
73         }
74         
75         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
76         {
77                 if(chan && chan->IsModeSet('z'))
78                 {
79                         if(user->GetExt("ssl"))
80                         {
81                                 // Let them in
82                                 return 0;
83                         }
84                         else
85                         {
86                                 // Deny
87                                 WriteServ(user->fd, "489 %s %s :Cannot join channel (+z)", user->nick, cname);
88                                 return 1;
89                         }
90                 }
91                 
92                 return 0;
93         }
94
95         virtual ~ModuleSSLModes()
96         {
97                 DELETE(sslm);
98         }
99         
100         virtual Version GetVersion()
101         {
102                 return Version(1, 0, 0, 0, VF_STATIC | VF_VENDOR);
103         }
104 };
105
106
107 class ModuleSSLModesFactory : public ModuleFactory
108 {
109  public:
110         ModuleSSLModesFactory()
111         {
112         }
113         
114         ~ModuleSSLModesFactory()
115         {
116         }
117         
118         virtual Module* CreateModule(Server* Me)
119         {
120                 return new ModuleSSLModes(Me);
121         }
122         
123 };
124
125
126 extern "C" void * init_module( void )
127 {
128         return new ModuleSSLModesFactory;
129 }