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