]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_opermodes.cpp
9db0b5b4ad64feb5e6dce64973f7abb0d70fada9
[user/henk/code/inspircd.git] / src / modules / m_opermodes.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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
16 /* $ModDesc: Sets (and unsets) modes on opers when they oper up */
17
18 class ModuleModesOnOper : public Module
19 {
20  public:
21         ModuleModesOnOper()
22         {
23                 Implementation eventlist[] = { I_OnPostOper, I_OnRehash };
24                 ServerInstance->Modules->Attach(eventlist, this, 2);
25         }
26
27
28         virtual void OnRehash(User* user)
29         {
30         }
31
32         virtual ~ModuleModesOnOper()
33         {
34         }
35
36         virtual Version GetVersion()
37         {
38                 return Version("Sets (and unsets) modes on opers when they oper up", VF_VENDOR);
39         }
40
41         virtual void OnPostOper(User* user, const std::string &opertype, const std::string &opername)
42         {
43                 TagIndex::iterator typetag = ServerInstance->Config->opertypes.find(opertype);
44                 if (typetag == ServerInstance->Config->opertypes.end())
45                         return;
46                 // whenever a user opers, go through the oper types, find their <type:modes>,
47                 // and if they have one apply their modes. The mode string can contain +modes
48                 // to add modes to the user or -modes to take modes from the user.
49                 std::string ThisOpersModes = typetag->second->getString("modes");
50                 if (!ThisOpersModes.empty())
51                 {
52                         ApplyModes(user, ThisOpersModes);
53                 }
54
55                 if (!opername.empty()) // if user is local ..
56                 {
57                         for (int i = 0;; i++)
58                         {
59                                 ConfigTag* tag = ServerInstance->Config->ConfValue("oper", i);
60                                 if (!tag)
61                                         break;
62                                 if (tag->getString("name") != opername)
63                                         continue;
64                                 ThisOpersModes = tag->getString("modes");
65                                 if (!ThisOpersModes.empty())
66                                 {
67                                         ApplyModes(user, ThisOpersModes);
68                                 }
69                                 break;
70                         }
71                 }
72         }
73
74         void ApplyModes(User *u, std::string &smodes)
75         {
76                 char first = *(smodes.c_str());
77                 if ((first != '+') && (first != '-'))
78                         smodes = "+" + smodes;
79
80                 std::string buf;
81                 std::stringstream ss(smodes);
82                 std::vector<std::string> tokens;
83
84                 // split into modes and mode params
85                 while (ss >> buf)
86                         tokens.push_back(buf);
87
88                 std::vector<std::string> modes;
89                 modes.push_back(u->nick);
90
91                 // process mode params
92                 for (unsigned int k = 0; k < tokens.size(); k++)
93                 {
94                         modes.push_back(tokens[k]);
95                 }
96
97                 ServerInstance->SendMode(modes, u);
98                 ServerInstance->PI->SendMode(u->uuid, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
99         }
100 };
101
102 MODULE_INIT(ModuleModesOnOper)