]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operjoin.cpp
469bb6a2c0765770fec190dd65f2431efefb1a8a
[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                 {
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;
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
77                 virtual ~ModuleOperjoin()
78                 {
79                 }
80
81                 virtual Version GetVersion()
82                 {
83                         return Version("Forces opers to join the specified channel(s) on oper-up", VF_VENDOR);
84                 }
85
86                 virtual void OnPostOper(User* user, const std::string &opertype, const std::string &opername)
87                 {
88                         if (!IS_LOCAL(user))
89                                 return;
90
91                         for(std::vector<std::string>::iterator it = operChans.begin(); it != operChans.end(); it++)
92                                 if (ServerInstance->IsChannel(it->c_str(), ServerInstance->Config->Limits.ChanMax))
93                                         Channel::JoinUser(user, it->c_str(), override, "", false, ServerInstance->Time());
94
95                         std::map<std::string, std::vector<std::string> >::iterator i = operTypeChans.find(user->oper->name);
96
97                         if (i != operTypeChans.end())
98                         {
99                                 const std::vector<std::string>& list = i->second;
100                                 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
101                                 {
102                                         if (ServerInstance->IsChannel(it->c_str(), ServerInstance->Config->Limits.ChanMax))
103                                         {
104                                                 Channel::JoinUser(user, it->c_str(), override, "", false, ServerInstance->Time());
105                                         }
106                                 }
107                         }
108                 }
109
110 };
111
112 MODULE_INIT(ModuleOperjoin)