]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_autoop.cpp
Replace [cC]olour with [cC]olor
[user/henk/code/inspircd.git] / src / modules / m_autoop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 #include "u_listmode.h"
16
17 /* $ModDesc: Provides support for the +w channel mode, autoop list */
18
19 /** Handles +w channel mode
20  */
21 class AutoOpList : public ListModeBase
22 {
23  public:
24         AutoOpList(Module* Creator) : ListModeBase(Creator, "autoop", 'w', "End of Channel Access List", 910, 911, true)
25         {
26                 levelrequired = OP_VALUE;
27                 tidy = false;
28         }
29
30         ModeHandler* FindMode(const std::string& mid)
31         {
32                 if (mid.length() == 1)
33                         return ServerInstance->Modes->FindMode(mid[0], MODETYPE_CHANNEL);
34                 for(char c='A'; c < 'z'; c++)
35                 {
36                         ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
37                         if (mh && mh->name == mid)
38                                 return mh;
39                 }
40                 return NULL;
41         }
42
43         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
44         {
45                 std::string::size_type pos = parameter.find(':');
46                 if (pos == 0 || pos == std::string::npos)
47                         return adding ? MOD_RES_DENY : MOD_RES_PASSTHRU;
48                 unsigned int mylevel = channel->GetPrefixValue(source);
49                 std::string mid = parameter.substr(0, pos);
50                 ModeHandler* mh = FindMode(mid);
51
52                 if (adding && (!mh || !mh->GetPrefixRank()))
53                 {
54                         source->WriteNumeric(415, "%s %s :Cannot find prefix mode '%s' for autoop",
55                                 source->nick.c_str(), mid.c_str(), mid.c_str());
56                         return MOD_RES_DENY;
57                 }
58                 else if (!mh)
59                         return MOD_RES_PASSTHRU;
60
61                 std::string dummy;
62                 if (mh->AccessCheck(source, channel, dummy, true) == MOD_RES_DENY)
63                         return MOD_RES_DENY;
64                 if (mh->GetLevelRequired() > mylevel)
65                 {
66                         source->WriteNumeric(482, "%s %s :You must be able to set mode '%s' to include it in an autoop",
67                                 source->nick.c_str(), channel->name.c_str(), mid.c_str());
68                         return MOD_RES_DENY;
69                 }
70                 return MOD_RES_PASSTHRU;
71         }
72 };
73
74 class ModuleAutoOp : public Module
75 {
76         AutoOpList mh;
77
78 public:
79         ModuleAutoOp() : mh(this)
80         {
81                 ServerInstance->Modules->AddService(mh);
82                 mh.DoImplements(this);
83
84                 Implementation list[] = { I_OnPostJoin, };
85                 ServerInstance->Modules->Attach(list, this, 1);
86         }
87
88         void OnPostJoin(Membership *memb)
89         {
90                 if (!IS_LOCAL(memb->user))
91                         return;
92
93                 modelist* list = mh.extItem.get(memb->chan);
94                 if (list)
95                 {
96                         std::string modeline("+");
97                         std::vector<std::string> modechange;
98                         modechange.push_back(memb->chan->name);
99                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
100                         {
101                                 std::string::size_type colon = it->mask.find(':');
102                                 if (colon == std::string::npos)
103                                         continue;
104                                 if (memb->chan->CheckBan(memb->user, it->mask.substr(colon+1)))
105                                 {
106                                         ModeHandler* given = mh.FindMode(it->mask.substr(0, colon));
107                                         if (given && given->GetPrefixRank())
108                                                 modeline.push_back(given->GetModeChar());
109                                 }
110                         }
111                         modechange.push_back(modeline);
112                         for(std::string::size_type i = modeline.length(); i > 1; --i) // we use "i > 1" instead of "i" so we skip the +
113                                 modechange.push_back(memb->user->nick);
114                         if(modechange.size() >= 3)
115                                 ServerInstance->SendMode(modechange,ServerInstance->FakeClient);
116                 }
117         }
118
119         void OnCleanup(int target_type, void* item)
120         {
121                 mh.DoCleanup(target_type, item);
122         }
123
124         void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
125         {
126                 mh.DoSyncChannel(chan, proto, opaque);
127         }
128
129         void OnRehash(User* user)
130         {
131                 mh.DoRehash();
132         }
133
134         Version GetVersion()
135         {
136                 return Version("Provides support for the +w channel mode", VF_VENDOR);
137         }
138 };
139
140 MODULE_INIT(ModuleAutoOp)