]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_opermodes.cpp
57eb8ae234ec1854bcb3cdea477a6bc8bbbc1733
[user/henk/code/inspircd.git] / src / modules / m_opermodes.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 <stdio.h>
15 #include <vector>
16 #include "users.h"
17 #include "channels.h"
18 #include "inspircd.h"
19 #include "modules.h"
20
21 /* $ModDesc: Sets (and unsets) modes on opers when they oper up */
22
23 class ModuleModesOnOper : public Module
24 {
25  private:
26
27         
28         ConfigReader *Conf;
29
30  public:
31         ModuleModesOnOper(InspIRCd* Me)
32                 : Module::Module(Me)
33         {
34                 
35                 Conf = new ConfigReader(ServerInstance);
36         }
37
38         void Implements(char* List)
39         {
40                 List[I_OnPostOper] = List[I_OnRehash] = 1;
41         }
42
43         virtual void OnRehash(userrec* user, const std::string &parameter)
44         {
45                 DELETE(Conf);
46                 Conf = new ConfigReader(ServerInstance);
47         }
48         
49         virtual ~ModuleModesOnOper()
50         {
51                 DELETE(Conf);
52         }
53         
54         virtual Version GetVersion()
55         {
56                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
57         }
58         
59         virtual void OnPostOper(userrec* user, const std::string &opertype)
60         {
61                 // whenever a user opers, go through the oper types, find their <type:modes>,
62                 // and if they have one apply their modes. The mode string can contain +modes
63                 // to add modes to the user or -modes to take modes from the user.
64                 for (int j =0; j < Conf->Enumerate("type"); j++)
65                 {
66                         std::string typen = Conf->ReadValue("type","name",j);
67                         if (!strcmp(typen.c_str(),user->oper))
68                         {
69                                 std::string ThisOpersModes = Conf->ReadValue("type","modes",j);
70                                 char first = *(ThisOpersModes.c_str());
71                                 if ((first != '+') && (first != '-'))
72                                         ThisOpersModes = "+" + ThisOpersModes;
73                                 if (ThisOpersModes != "")
74                                 {
75                                         std::string buf;
76                                         stringstream ss(ThisOpersModes);
77
78                                         vector<string> tokens;
79
80                                         // split ThisOperModes into modes and mode params
81                                         while (ss >> buf)
82                                                 tokens.push_back(buf);
83
84                                         int size = tokens.size() + 1;
85                                         const char* modes[size];
86                                         modes[0] = user->nick;
87                                         modes[1] = (char*)tokens[0].c_str();
88
89                                         if (tokens.size() > 1)
90                                         {
91                                                 // process mode params
92                                                 int i = 2;
93                                                 for (unsigned int k = 1; k < tokens.size(); k++)
94                                                 {
95                                                         modes[i] = (char*)tokens[k].c_str();
96                                                         i++;
97                                                 }
98                                         }
99                                         std::deque<std::string> n;
100                                         Event rmode((char *)&n, NULL, "send_mode");
101                                         n.push_back(user->nick);
102                                         n.push_back(modes[0]);
103                                         for (unsigned int j = 1; j < tokens.size(); j++)
104                                         {
105                                                 n.push_back(modes[j]);
106                                         }
107                                         rmode.Send(ServerInstance);
108                                         ServerInstance->SendMode(modes, size, user);
109                                 }
110                                 break;
111                         }
112                 }
113         }
114 };
115
116 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
117
118 class ModuleModesOnOperFactory : public ModuleFactory
119 {
120  public:
121         ModuleModesOnOperFactory()
122         {
123         }
124         
125         ~ModuleModesOnOperFactory()
126         {
127         }
128         
129         virtual Module * CreateModule(InspIRCd* Me)
130         {
131                 return new ModuleModesOnOper(Me);
132         }
133         
134 };
135
136
137 extern "C" void * init_module( void )
138 {
139         return new ModuleModesOnOperFactory;
140 }
141