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