]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operjoin.cpp
Move destruction logic for User and Spanningtree into cull()
[user/henk/code/inspircd.git] / src / modules / m_operjoin.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: Forces opers to join the specified channel(s) on oper-up */
17
18 class ModuleOperjoin : public Module
19 {
20         private:
21                 std::string operChan;
22                 std::vector<std::string> operChans;
23                 std::map<std::string, std::vector<std::string> > operTypeChans; // Channels specific to an oper type.
24                 bool override;
25
26                 int tokenize(const std::string &str, std::vector<std::string> &tokens)
27                 {
28                         // skip delimiters at beginning.
29                         std::string::size_type lastPos = str.find_first_not_of(",", 0);
30                         // find first "non-delimiter".
31                         std::string::size_type pos = str.find_first_of(",", lastPos);
32
33                         while (std::string::npos != pos || std::string::npos != lastPos)
34                         {
35                                 // found a token, add it to the vector.
36                                 tokens.push_back(str.substr(lastPos, pos - lastPos));
37                                 // skip delimiters. Note the "not_of"
38                                 lastPos = str.find_first_not_of(",", pos);
39                                 // find next "non-delimiter"
40                                 pos = str.find_first_of(",", lastPos);
41                         }
42                         return tokens.size();
43                 }
44
45         public:
46                 ModuleOperjoin()                {
47                         OnRehash(NULL);
48                 Implementation eventlist[] = { I_OnPostOper, I_OnRehash };
49                 ServerInstance->Modules->Attach(eventlist, this, 2);
50                 }
51
52
53                 virtual void OnRehash(User* user)
54                 {
55                         ConfigReader* conf = new ConfigReader;
56
57                         operChan = conf->ReadValue("operjoin", "channel", 0);
58                         override = conf->ReadFlag("operjoin", "override", "0", 0);
59                         operChans.clear();
60                         if (!operChan.empty())
61                                 tokenize(operChan,operChans);
62
63                         std::map<std::string, std::vector<std::string> >().swap(operTypeChans);
64
65                         int olines = conf->Enumerate("type");
66                         for (int index = 0; index < olines; ++index)
67                         {
68                                 std::string chanList = conf->ReadValue("type", "autojoin", index);
69                                 if (!chanList.empty())
70                                 {
71                                         tokenize(chanList, operTypeChans[conf->ReadValue("type", "name", index)]);
72                                 }
73                         }
74
75                         delete conf;
76                 }
77
78                 virtual ~ModuleOperjoin()
79                 {
80                 }
81
82                 virtual Version GetVersion()
83                 {
84                         return Version("Forces opers to join the specified channel(s) on oper-up", VF_VENDOR, API_VERSION);
85                 }
86
87                 virtual void OnPostOper(User* user, const std::string &opertype, const std::string &opername)
88                 {
89                         if (!IS_LOCAL(user))
90                                 return;
91
92                         for(std::vector<std::string>::iterator it = operChans.begin(); it != operChans.end(); it++)
93                                 if (ServerInstance->IsChannel(it->c_str(), ServerInstance->Config->Limits.ChanMax))
94                                         Channel::JoinUser(user, it->c_str(), override, "", false, ServerInstance->Time());
95
96                         std::map<std::string, std::vector<std::string> >::iterator i = operTypeChans.find(user->oper);
97
98                         if (i != operTypeChans.end())
99                         {
100                                 const std::vector<std::string>& list = i->second;
101                                 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
102                                 {
103                                         if (ServerInstance->IsChannel(it->c_str(), ServerInstance->Config->Limits.ChanMax))
104                                         {
105                                                 Channel::JoinUser(user, it->c_str(), override, "", false, ServerInstance->Time());
106                                         }
107                                 }
108                         }
109                 }
110
111 };
112
113 MODULE_INIT(ModuleOperjoin)