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