]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_testnet.cpp
ISupportManager: Tidy-up, expand comments
[user/henk/code/inspircd.git] / src / modules / m_testnet.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 /* $ModDesc: Provides a module for testing the server while linked in a network */
21
22 #include "inspircd.h"
23
24 struct vtbase
25 {
26         virtual void isok(const char* name, int impl, Module* basemod, std::vector<std::string>& allmods) = 0;
27         virtual ~vtbase() {}
28 };
29
30 template<typename T> struct vtable : public vtbase
31 {
32         union u {
33                 T function;
34                 struct v {
35                         size_t delta;
36                         size_t vtoff;
37                 } v;
38         } u;
39         vtable(T t) {
40                 u.function = t;
41         }
42         /** member function pointer dereference from vtable; depends on the GCC 4.4 ABI (x86_64) */
43         template<typename E> void* read(E* obj)
44         {
45                 if (u.v.delta & 1)
46                 {
47                         uint8_t* optr = reinterpret_cast<uint8_t*>(obj);
48                         optr += u.v.vtoff;
49                         uint8_t* vptr = *reinterpret_cast<uint8_t**>(optr);
50                         vptr += u.v.delta - 1;
51                         return *reinterpret_cast<void**>(vptr);
52                 }
53                 else
54                         return reinterpret_cast<void*>(u.v.delta);
55         }
56         void isok(const char* name, int impl, Module* basemod, std::vector<std::string>& allmods)
57         {
58                 void* base = read(basemod);
59                 for(unsigned int i=0; i < allmods.size(); ++i)
60                 {
61                         Module* mod = ServerInstance->Modules->Find(allmods[i]);
62                         void* fptr = read(mod);
63                         for(EventHandlerIter j = ServerInstance->Modules->EventHandlers[impl].begin();
64                                 j != ServerInstance->Modules->EventHandlers[impl].end(); j++)
65                         {
66                                 if (mod == *j)
67                                 {
68                                         if (fptr == base)
69                                         {
70                                                 ServerInstance->SNO->WriteToSnoMask('a', "Module %s implements %s but uses default function",
71                                                         mod->ModuleSourceFile.c_str(), name);
72                                         }
73                                         goto done;
74                                 }
75                         }
76                         if (fptr != base)
77                         {
78                                 ServerInstance->SNO->WriteToSnoMask('a', "Module %s does not implement %s but overrides function",
79                                         mod->ModuleSourceFile.c_str(), name);
80                         }
81                         done:;
82                 }
83         }
84 };
85
86 template<typename T> vtbase* vtinit(T t)
87 {
88         return new vtable<T>(t);
89 }
90
91 static void checkall(Module* noimpl)
92 {
93         std::vector<std::string> allmods = ServerInstance->Modules->GetAllModuleNames(0);
94 #define CHK(name) do { \
95         vtbase* vt = vtinit(&Module::name); \
96         vt->isok(#name, I_ ## name, noimpl, allmods); \
97         delete vt; \
98 } while (0)
99         CHK(OnUserConnect);
100         CHK(OnUserQuit);
101         CHK(OnUserDisconnect);
102         CHK(OnUserJoin);
103         CHK(OnUserPart);
104         CHK(OnRehash);
105         CHK(OnSendSnotice);
106         CHK(OnUserPreJoin);
107         CHK(OnUserPreKick);
108         CHK(OnUserKick);
109         CHK(OnOper);
110         CHK(OnInfo);
111         CHK(OnWhois);
112         CHK(OnUserPreInvite);
113         CHK(OnUserInvite);
114         CHK(OnUserPreMessage);
115         CHK(OnUserPreNick);
116         CHK(OnUserMessage);
117         CHK(OnMode);
118         CHK(OnGetServerDescription);
119         CHK(OnSyncUser);
120         CHK(OnSyncChannel);
121         CHK(OnDecodeMetaData);
122         CHK(OnWallops);
123         CHK(OnAcceptConnection);
124         CHK(OnChangeHost);
125         CHK(OnChangeName);
126         CHK(OnAddLine);
127         CHK(OnDelLine);
128         CHK(OnExpireLine);
129         CHK(OnUserPostNick);
130         CHK(OnPreMode);
131         CHK(On005Numeric);
132         CHK(OnKill);
133         CHK(OnRemoteKill);
134         CHK(OnLoadModule);
135         CHK(OnUnloadModule);
136         CHK(OnBackgroundTimer);
137         CHK(OnPreCommand);
138         CHK(OnCheckReady);
139         CHK(OnCheckInvite);
140         CHK(OnRawMode);
141         CHK(OnCheckKey);
142         CHK(OnCheckLimit);
143         CHK(OnCheckBan);
144         CHK(OnCheckChannelBan);
145         CHK(OnExtBanCheck);
146         CHK(OnStats);
147         CHK(OnChangeLocalUserHost);
148         CHK(OnPreTopicChange);
149         CHK(OnPostTopicChange);
150         CHK(OnEvent);
151         CHK(OnGlobalOper);
152         CHK(OnPostConnect);
153         CHK(OnChangeLocalUserGECOS);
154         CHK(OnUserRegister);
155         CHK(OnChannelPreDelete);
156         CHK(OnChannelDelete);
157         CHK(OnPostOper);
158         CHK(OnSyncNetwork);
159         CHK(OnSetAway);
160         CHK(OnPostCommand);
161         CHK(OnPostJoin);
162         CHK(OnWhoisLine);
163         CHK(OnBuildNeighborList);
164         CHK(OnGarbageCollect);
165         CHK(OnText);
166         CHK(OnPassCompare);
167         CHK(OnRunTestSuite);
168         CHK(OnNamesListItem);
169         CHK(OnNumeric);
170         CHK(OnHookIO);
171         CHK(OnPreRehash);
172         CHK(OnModuleRehash);
173         CHK(OnSendWhoLine);
174         CHK(OnChangeIdent);
175 }
176
177 class CommandTest : public Command
178 {
179  public:
180         CommandTest(Module* parent) : Command(parent, "TEST", 1)
181         {
182                 syntax = "<action> <parameters>";
183         }
184
185         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
186         {
187                 if (parameters[0] == "flood")
188                 {
189                         unsigned int count = parameters.size() > 1 ? atoi(parameters[1].c_str()) : 100;
190                         std::string line = parameters.size() > 2 ? parameters[2] : ":z.z NOTICE !flood :Flood text";
191                         for(unsigned int i=0; i < count; i++)
192                                 user->Write(line);
193                 }
194                 else if (parameters[0] == "freeze" && IS_LOCAL(user) && parameters.size() > 1)
195                 {
196                         IS_LOCAL(user)->CommandFloodPenalty += atoi(parameters[1].c_str());
197                 }
198                 else if (parameters[0] == "check")
199                 {
200                         checkall(creator);
201                         ServerInstance->SNO->WriteToSnoMask('a', "Module check complete");
202                 }
203                 return CMD_SUCCESS;
204         }
205 };
206
207 class ModuleTest : public Module
208 {
209         CommandTest cmd;
210  public:
211         ModuleTest() : cmd(this)
212         {
213         }
214
215         void init() CXX11_OVERRIDE
216         {
217                 if (!strstr(ServerInstance->Config->ServerName.c_str(), ".test"))
218                         throw ModuleException("Don't load modules without reading their descriptions!");
219                 ServerInstance->Modules->AddService(cmd);
220         }
221
222         Version GetVersion() CXX11_OVERRIDE
223         {
224                 return Version("Provides a module for testing the server while linked in a network", VF_VENDOR|VF_OPTCOMMON);
225         }
226 };
227
228 MODULE_INIT(ModuleTest)