]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_opermodes.cpp
2e947809ac979ba0090a365030dce80a2cf663a6
[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://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
16 /* $ModDesc: Sets (and unsets) modes on opers when they oper up */
17
18 class ModuleModesOnOper : public Module
19 {
20  private:
21
22
23         ConfigReader *Conf;
24
25  public:
26         ModuleModesOnOper(InspIRCd* Me)
27                 : Module(Me)
28         {
29
30                 Conf = new ConfigReader(ServerInstance);
31                 Implementation eventlist[] = { I_OnPostOper, I_OnRehash };
32                 ServerInstance->Modules->Attach(eventlist, this, 2);
33         }
34
35
36         virtual void OnRehash(User* user, const std::string &parameter)
37         {
38                 delete Conf;
39                 Conf = new ConfigReader(ServerInstance);
40         }
41
42         virtual ~ModuleModesOnOper()
43         {
44                 delete Conf;
45         }
46
47         virtual Version GetVersion()
48         {
49                 return Version("$Id$", VF_VENDOR, API_VERSION);
50         }
51
52         virtual void OnPostOper(User* user, const std::string &opertype, const std::string &opername)
53         {
54                 // whenever a user opers, go through the oper types, find their <type:modes>,
55                 // and if they have one apply their modes. The mode string can contain +modes
56                 // to add modes to the user or -modes to take modes from the user.
57                 for (int j =0; j < Conf->Enumerate("type"); j++)
58                 {
59                         std::string typen = Conf->ReadValue("type","name",j);
60                         if (typen == user->oper)
61                         {
62                                 std::string ThisOpersModes = Conf->ReadValue("type","modes",j);
63                                 if (!ThisOpersModes.empty())
64                                 {
65                                         ApplyModes(user, ThisOpersModes);
66                                 }
67                                 break;
68                         }
69                 }
70
71                 if (!opername.empty()) // if user is local ..
72                 {
73                         for (int j = 0; j < Conf->Enumerate("oper"); j++)
74                         {
75                                 if (opername == Conf->ReadValue("oper", "name", j))
76                                 {
77                                         std::string ThisOpersModes = Conf->ReadValue("oper", "modes", j);
78                                         if (!ThisOpersModes.empty())
79                                         {
80                                                 ApplyModes(user, ThisOpersModes);
81                                         }
82                                         break;
83                                 }
84                         }
85                 }
86         }
87
88         void ApplyModes(User *u, std::string &smodes)
89         {
90                 char first = *(smodes.c_str());
91                 if ((first != '+') && (first != '-'))
92                         smodes = "+" + smodes;
93
94                 std::string buf;
95                 std::stringstream ss(smodes);
96                 std::vector<std::string> tokens;
97
98                 // split into modes and mode params
99                 while (ss >> buf)
100                         tokens.push_back(buf);
101
102                 std::vector<std::string> modes;
103                 modes.push_back(u->nick);
104
105                 // process mode params
106                 for (unsigned int k = 0; k < tokens.size(); k++)
107                 {
108                         modes.push_back(tokens[k]);
109                 }
110
111                 std::deque<std::string> n;
112                 for (unsigned int j = 1; j < tokens.size(); j++)
113                         n.push_back(modes[j]);
114
115                 ServerInstance->PI->SendMode(u->uuid, n);
116                 ServerInstance->SendMode(modes, u);
117         }
118 };
119
120 MODULE_INIT(ModuleModesOnOper)