]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_opermodes.cpp
Fix desync (modes not being pushed to remote servers with send_mode event)
[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(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                                                         ServerInstance->Log(DEBUG, "m_opermodes.so: got mode param: %s", modes[i]);
97                                                         i++;
98                                                 }
99                                         }
100                                         std::deque<std::string> n;
101                                         Event rmode((char *)&n, NULL, "send_mode");
102                                         n.push_back(user->nick);
103                                         n.push_back(modes[0]);
104                                         for (unsigned int j = 1; j < tokens.size(); j++)
105                                         {
106                                                 n.push_back(modes[j]);
107                                         }
108                                         rmode.Send(ServerInstance);
109                                         ServerInstance->Log(DEBUG, "m_opermodes.so: new modes for %s: %s", modes[0], modes[1]);
110                                         ServerInstance->SendMode(modes, size, user);
111                                 }
112                                 break;
113                         }
114                 }
115         }
116 };
117
118 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
119
120 class ModuleModesOnOperFactory : public ModuleFactory
121 {
122  public:
123         ModuleModesOnOperFactory()
124         {
125         }
126         
127         ~ModuleModesOnOperFactory()
128         {
129         }
130         
131         virtual Module * CreateModule(InspIRCd* Me)
132         {
133                 return new ModuleModesOnOper(Me);
134         }
135         
136 };
137
138
139 extern "C" void * init_module( void )
140 {
141         return new ModuleModesOnOperFactory;
142 }
143