]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operjoin.cpp
6b94a4ef9054dd787c6732cd76b9d35af7df07ee
[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(InspIRCd* Me) : Module(Me)
47                 {
48                         OnRehash(NULL);
49                 Implementation eventlist[] = { I_OnPostOper, I_OnRehash };
50                 ServerInstance->Modules->Attach(eventlist, this, 2);
51                 }
52
53
54                 virtual void OnRehash(User* user)
55                 {
56                         ConfigReader* conf = new ConfigReader(ServerInstance);
57
58                         operChan = conf->ReadValue("operjoin", "channel", 0);
59                         override = conf->ReadFlag("operjoin", "override", "0", 0);
60                         operChans.clear();
61                         if (!operChan.empty())
62                                 tokenize(operChan,operChans);
63
64                         std::map<std::string, std::vector<std::string> >().swap(operTypeChans);
65
66                         int olines = conf->Enumerate("type");
67                         for (int index = 0; index < olines; ++index)
68                         {
69                                 std::string chanList = conf->ReadValue("type", "autojoin", index);
70                                 if (!chanList.empty())
71                                 {
72                                         tokenize(chanList, operTypeChans[conf->ReadValue("type", "name", index)]);
73                                 }
74                         }
75
76                         delete conf;
77                 }
78
79                 virtual ~ModuleOperjoin()
80                 {
81                 }
82
83                 virtual Version GetVersion()
84                 {
85                         return Version("Forces opers to join the specified channel(s) on oper-up", VF_VENDOR, API_VERSION);
86                 }
87
88                 virtual void OnPostOper(User* user, const std::string &opertype, const std::string &opername)
89                 {
90                         if (!IS_LOCAL(user))
91                                 return;
92
93                         for(std::vector<std::string>::iterator it = operChans.begin(); it != operChans.end(); it++)
94                                 if (ServerInstance->IsChannel(it->c_str(), ServerInstance->Config->Limits.ChanMax))
95                                         Channel::JoinUser(ServerInstance, user, it->c_str(), override, "", false, ServerInstance->Time());
96
97                         std::map<std::string, std::vector<std::string> >::iterator i = operTypeChans.find(user->oper);
98
99                         if (i != operTypeChans.end())
100                         {
101                                 const std::vector<std::string>& list = i->second;
102                                 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
103                                 {
104                                         if (ServerInstance->IsChannel(it->c_str(), ServerInstance->Config->Limits.ChanMax))
105                                         {
106                                                 Channel::JoinUser(ServerInstance, user, it->c_str(), override, "", false, ServerInstance->Time());
107                                         }
108                                 }
109                         }
110                 }
111
112 };
113
114 MODULE_INIT(ModuleOperjoin)