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