]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_testnet.cpp
Merge pull request #1157 from SaberUK/insp20+fix-cron-restart
[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(OnUserPreNotice);
116         CHK(OnUserPreNick);
117         CHK(OnUserMessage);
118         CHK(OnUserNotice);
119         CHK(OnMode);
120         CHK(OnGetServerDescription);
121         CHK(OnSyncUser);
122         CHK(OnSyncChannel);
123         CHK(OnDecodeMetaData);
124         CHK(OnWallops);
125         CHK(OnAcceptConnection);
126         CHK(OnChangeHost);
127         CHK(OnChangeName);
128         CHK(OnAddLine);
129         CHK(OnDelLine);
130         CHK(OnExpireLine);
131         CHK(OnUserPostNick);
132         CHK(OnPreMode);
133         CHK(On005Numeric);
134         CHK(OnKill);
135         CHK(OnRemoteKill);
136         CHK(OnLoadModule);
137         CHK(OnUnloadModule);
138         CHK(OnBackgroundTimer);
139         CHK(OnPreCommand);
140         CHK(OnCheckReady);
141         CHK(OnCheckInvite);
142         CHK(OnRawMode);
143         CHK(OnCheckKey);
144         CHK(OnCheckLimit);
145         CHK(OnCheckBan);
146         CHK(OnCheckChannelBan);
147         CHK(OnExtBanCheck);
148         CHK(OnStats);
149         CHK(OnChangeLocalUserHost);
150         CHK(OnPreTopicChange);
151         CHK(OnPostTopicChange);
152         CHK(OnEvent);
153         CHK(OnGlobalOper);
154         CHK(OnPostConnect);
155         CHK(OnAddBan);
156         CHK(OnDelBan);
157         CHK(OnChangeLocalUserGECOS);
158         CHK(OnUserRegister);
159         CHK(OnChannelPreDelete);
160         CHK(OnChannelDelete);
161         CHK(OnPostOper);
162         CHK(OnSyncNetwork);
163         CHK(OnSetAway);
164         CHK(OnPostCommand);
165         CHK(OnPostJoin);
166         CHK(OnWhoisLine);
167         CHK(OnBuildNeighborList);
168         CHK(OnGarbageCollect);
169         CHK(OnText);
170         CHK(OnPassCompare);
171         CHK(OnRunTestSuite);
172         CHK(OnNamesListItem);
173         CHK(OnNumeric);
174         CHK(OnHookIO);
175         CHK(OnPreRehash);
176         CHK(OnModuleRehash);
177         CHK(OnSendWhoLine);
178         CHK(OnChangeIdent);
179 }
180
181 class CommandTest : public Command
182 {
183  public:
184         CommandTest(Module* parent) : Command(parent, "TEST", 1)
185         {
186                 syntax = "<action> <parameters>";
187         }
188
189         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
190         {
191                 if (parameters[0] == "flood")
192                 {
193                         unsigned int count = parameters.size() > 1 ? atoi(parameters[1].c_str()) : 100;
194                         std::string line = parameters.size() > 2 ? parameters[2] : ":z.z NOTICE !flood :Flood text";
195                         for(unsigned int i=0; i < count; i++)
196                                 user->Write(line);
197                 }
198                 else if (parameters[0] == "freeze" && IS_LOCAL(user) && parameters.size() > 1)
199                 {
200                         IS_LOCAL(user)->CommandFloodPenalty += atoi(parameters[1].c_str());
201                 }
202                 else if (parameters[0] == "check")
203                 {
204                         checkall(creator);
205                         ServerInstance->SNO->WriteToSnoMask('a', "Module check complete");
206                 }
207                 return CMD_SUCCESS;
208         }
209 };
210
211 class ModuleTest : public Module
212 {
213         CommandTest cmd;
214  public:
215         ModuleTest() : cmd(this)
216         {
217         }
218
219         void init()
220         {
221                 if (!strstr(ServerInstance->Config->ServerName.c_str(), ".test"))
222                         throw ModuleException("Don't load modules without reading their descriptions!");
223                 ServerInstance->Modules->AddService(cmd);
224         }
225
226         Version GetVersion()
227         {
228                 return Version("Provides a module for testing the server while linked in a network", VF_VENDOR|VF_OPTCOMMON);
229         }
230 };
231
232 MODULE_INIT(ModuleTest)
233