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