]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_opermodes.cpp
Change User::oper to an OperInfo reference
[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                 // whenever a user opers, go through the oper types, find their <type:modes>,
44                 // and if they have one apply their modes. The mode string can contain +modes
45                 // to add modes to the user or -modes to take modes from the user.
46                 std::string ThisOpersModes = user->oper->getConfig("modes");
47                 if (!ThisOpersModes.empty())
48                 {
49                         ApplyModes(user, ThisOpersModes);
50                 }
51
52                 if (!opername.empty()) // if user is local ..
53                 {
54                         for (int i = 0;; i++)
55                         {
56                                 ConfigTag* tag = ServerInstance->Config->ConfValue("oper", i);
57                                 if (!tag)
58                                         break;
59                                 if (tag->getString("name") != opername)
60                                         continue;
61                                 ThisOpersModes = tag->getString("modes");
62                                 if (!ThisOpersModes.empty())
63                                 {
64                                         ApplyModes(user, ThisOpersModes);
65                                 }
66                                 break;
67                         }
68                 }
69         }
70
71         void ApplyModes(User *u, std::string &smodes)
72         {
73                 char first = *(smodes.c_str());
74                 if ((first != '+') && (first != '-'))
75                         smodes = "+" + smodes;
76
77                 std::string buf;
78                 std::stringstream ss(smodes);
79                 std::vector<std::string> tokens;
80
81                 // split into modes and mode params
82                 while (ss >> buf)
83                         tokens.push_back(buf);
84
85                 std::vector<std::string> modes;
86                 modes.push_back(u->nick);
87
88                 // process mode params
89                 for (unsigned int k = 0; k < tokens.size(); k++)
90                 {
91                         modes.push_back(tokens[k]);
92                 }
93
94                 ServerInstance->SendMode(modes, u);
95                 ServerInstance->PI->SendMode(u->uuid, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
96         }
97 };
98
99 MODULE_INIT(ModuleModesOnOper)