]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/inspircd.h
Tons of comments
[user/henk/code/inspircd.git] / include / inspircd.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __INSPIRCD_H__
18 #define __INSPIRCD_H__
19
20 #include <time.h>
21 #include <string>
22 #include <sstream>
23 #include "inspircd_config.h"
24 #include "users.h"
25 #include "channels.h"
26 #include "socket.h"
27 #include "mode.h"
28 #include "helperfuncs.h"
29 #include "socketengine.h"
30 #include "command_parse.h"
31
32 /* Some misc defines */
33 #define ERROR -1
34 #define MAXCOMMAND 32
35
36 /* Crucial defines */
37 #define ETIREDGERBILS EAGAIN
38
39 /** Debug levels for use with InspIRCd::Log()
40  */
41 enum DebugLevel
42 {
43         DEBUG           =       10,
44         VERBOSE         =       20,
45         DEFAULT         =       30,
46         SPARSE          =       40,
47         NONE            =       50,
48 };
49
50 /* This define is used in place of strcmp when we 
51  * want to check if a char* string contains only one
52  * letter. Pretty fast, its just two compares and an
53  * addition.
54  */
55 #define IS_SINGLE(x,y) ( (*x == y) && (*(x+1) == 0) )
56
57 #define DELETE(x) {if (x) { delete x; x = NULL; }}
58
59 template<typename T> inline std::string ConvToStr(const T &in)
60 {
61         std::stringstream tmp;
62         if (!(tmp << in)) return std::string();
63         return tmp.str();
64 }
65
66 class serverstats : public classbase
67 {
68   public:
69         unsigned long statsAccept;
70         unsigned long statsRefused;
71         unsigned long statsUnknown;
72         unsigned long statsCollisions;
73         unsigned long statsDns;
74         unsigned long statsDnsGood;
75         unsigned long statsDnsBad;
76         unsigned long statsConnects;
77         double statsSent;
78         double statsRecv;
79         unsigned long BoundPortCount;
80
81         serverstats()
82         {
83                 statsAccept = statsRefused = statsUnknown = 0;
84                 statsCollisions = statsDns = statsDnsGood = 0;
85                 statsDnsBad = statsConnects = 0;
86                 statsSent = statsRecv = 0.0;
87                 BoundPortCount = 0;
88         }
89 };
90
91 class XLineManager;
92
93 class InspIRCd : public classbase
94 {
95  private:
96         /** Holds a string describing the last module error to occur
97          */
98         char MODERR[MAXBUF];
99
100         /** This is an internal flag used by the mainloop
101          */
102         bool expire_run;
103
104         /** List of server names we've seen.
105          */
106         servernamelist servernames;
107  
108         /** Remove a ModuleFactory pointer
109          */
110         void EraseFactory(int j);
111
112         /** Remove a Module pointer
113          */
114         void EraseModule(int j);
115
116         /** Build the ISUPPORT string by triggering all modules On005Numeric events
117          */
118         void BuildISupport();
119
120         /** Move a given module to a specific slot in the list
121          */
122         void MoveTo(std::string modulename,int slot);
123
124         /** Display the startup banner
125          */
126         void Start();
127
128         /** Set up the signal handlers
129          */
130         void SetSignals(bool SEGVHandler);
131
132         /** Daemonize the ircd and close standard input/output streams
133          */
134         bool DaemonSeed();
135
136         /** Build the upper/lowercase comparison table
137          */
138         void MakeLowerMap();
139
140         /** Moves the given module to the last slot in the list
141          */
142         void MoveToLast(std::string modulename);
143
144         /** Moves the given module to the first slot in the list
145          */
146         void MoveToFirst(std::string modulename);
147
148         /** Moves one module to be placed after another in the list
149          */
150         void MoveAfter(std::string modulename, std::string after);
151
152         /** Moves one module to be placed before another in the list
153          */
154         void MoveBefore(std::string modulename, std::string before);
155
156         /** Process a user whos socket has been flagged as active
157          */
158         void ProcessUser(userrec* cu);
159
160         /** Iterate the list of InspSocket objects, removing ones which have timed out
161          */
162         void DoSocketTimeouts(time_t TIME);
163
164         /** Perform background user events such as PING checks
165          */
166         void DoBackgroundUserStuff(time_t TIME);
167
168         /** Returns true when all modules have done pre-registration checks on a user
169          */
170         bool AllModulesReportReady(userrec* user);
171
172         /** Total number of modules loaded into the ircd, minus one
173          */
174         int ModCount;
175
176         /** Logfile pathname specified on the commandline, or empty string
177          */
178         char LogFileName[MAXBUF];
179
180         /** The feature names published by various modules
181          */
182         featurelist Features;
183
184         /** The current time, updated in the mainloop
185          */
186         time_t TIME;
187
188         /** The time that was recorded last time around the mainloop
189          */
190         time_t OLDTIME;
191
192         /** A 64k buffer used to read client lines into
193          */
194         char ReadBuffer[65535];
195
196         /** Number of seconds in a minute
197          */
198         const long duration_m;
199
200         /** Number of seconds in an hour
201          */
202         const long duration_h;
203
204         /** Number of seconds in a day
205          */
206         const long duration_d;
207
208         /** Number of seconds in a week
209          */
210         const long duration_w;
211
212         /** Number of seconds in a year
213          */
214         const long duration_y;
215
216  public:
217         /** Time this ircd was booted
218          */
219         time_t startup_time;
220
221         /** Mode handler, handles mode setting and removal
222          */
223         ModeParser* Modes;
224
225         /** Command parser, handles client to server commands
226          */
227         CommandParser* Parser;
228
229         /** Socket engine, handles socket activity events
230          */
231         SocketEngine* SE;
232
233         /** Stats class, holds miscellaneous stats counters
234          */
235         serverstats* stats;
236
237         /**  Server Config class, holds configuration file data
238          */
239         ServerConfig* Config;
240
241         /** Module sockets list, holds the active set of InspSocket classes
242          */
243         std::vector<InspSocket*> module_sockets;
244
245         /** Socket reference table, provides fast lookup of fd to InspSocket*
246          */
247         InspSocket* socket_ref[MAX_DESCRIPTORS];
248
249         /** user reference table, provides fast lookup of fd to userrec*
250          */
251         userrec* fd_ref_table[MAX_DESCRIPTORS];
252
253         /** Client list, a hash_map containing all clients, local and remote
254          */
255         user_hash clientlist;
256
257         /** Channel list, a hash_map containing all channels
258          */
259         chan_hash chanlist;
260
261         /** Local client list, a vector containing only local clients
262          */
263         std::vector<userrec*> local_users;
264
265         /** Oper list, a vector containing all local and remote opered users
266          */
267         std::vector<userrec*> all_opers;
268
269         /** Whowas container, contains a map of vectors of users tracked by WHOWAS
270          */
271         irc::whowas::whowas_users whowas;
272
273         /** DNS class, provides resolver facilities to the core and modules
274          */
275         DNS* Res;
276
277         /** Timer manager class, triggers InspTimer timer events
278          */
279         TimerManager* Timers;
280
281         /** Command list, a hash_map of command names to command_t*
282          */
283         command_table cmdlist;
284
285         /** X-Line manager. Handles G/K/Q/E line setting, removal and matching
286          */
287         XLineManager* XLines;
288
289         /** A list of Module* module classes
290          * Note that this list is always exactly 255 in size.
291          * The actual number of loaded modules is available from GetModuleCount()
292          */
293         ModuleList modules;
294
295         /** A list of ModuleFactory* module factories
296          * Note that this list is always exactly 255 in size.
297          * The actual number of loaded modules is available from GetModuleCount()
298          */
299         FactoryList factory;
300
301         /** Get the current time
302          * Because this only calls time() once every time around the mainloop,
303          * it is much faster than calling time() directly.
304          */
305         time_t Time();
306
307         /** Get the total number of currently loaded modules
308          */
309         int GetModuleCount();
310
311         /** Find a module by name, and return a Module* to it.
312          * This is preferred over iterating the module lists yourself.
313          * @param name The module name to look up
314          */
315         Module* FindModule(const std::string &name);
316
317         /** Bind all ports specified in the configuration file.
318          * @param bail True if the function should bail back to the shell on failure
319          */
320         int BindPorts(bool bail);
321
322         bool HasPort(int port, char* addr);
323         bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr);
324
325         void AddServerName(const std::string &servername);
326         const char* FindServerNamePtr(const std::string &servername);
327         bool FindServerName(const std::string &servername);
328
329         std::string GetServerDescription(const char* servername);
330
331         void WriteOpers(const char* text, ...);
332         void WriteOpers(const std::string &text);
333         
334         userrec* FindNick(const std::string &nick);
335         userrec* FindNick(const char* nick);
336
337         chanrec* FindChan(const std::string &chan);
338         chanrec* FindChan(const char* chan);
339
340         void LoadAllModules();
341         void CheckDie();
342         void CheckRoot();
343         void OpenLog(char** argv, int argc);
344
345         bool UserToPseudo(userrec* user, const std::string &message);
346         bool PseudoToUser(userrec* alive, userrec* zombie, const std::string &message);
347
348         void ServerNoticeAll(char* text, ...);
349         void ServerPrivmsgAll(char* text, ...);
350         void WriteMode(const char* modes, int flags, const char* text, ...);
351
352         bool IsChannel(const char *chname);
353
354         static void Rehash(int status);
355         static void Exit(int status);
356
357         int UserCount();
358         int RegisteredUserCount();
359         int InvisibleUserCount();
360         int OperCount();
361         int UnregisteredUserCount();
362         long ChannelCount();
363         long LocalUserCount();
364
365         void SendError(const char *s);
366
367         /** For use with Module::Prioritize().
368          * When the return value of this function is returned from
369          * Module::Prioritize(), this specifies that the module wishes
370          * to be ordered exactly BEFORE 'modulename'. For more information
371          * please see Module::Prioritize().
372          * @param modulename The module your module wants to be before in the call list
373          * @returns a priority ID which the core uses to relocate the module in the list
374          */
375         long PriorityBefore(const std::string &modulename);
376
377         /** For use with Module::Prioritize().
378          * When the return value of this function is returned from
379          * Module::Prioritize(), this specifies that the module wishes
380          * to be ordered exactly AFTER 'modulename'. For more information please
381          * see Module::Prioritize().
382          * @param modulename The module your module wants to be after in the call list
383          * @returns a priority ID which the core uses to relocate the module in the list
384          */
385         long PriorityAfter(const std::string &modulename);
386
387         /** Publish a 'feature'.
388          * There are two ways for a module to find another module it depends on.
389          * Either by name, using InspIRCd::FindModule, or by feature, using this
390          * function. A feature is an arbitary string which identifies something this
391          * module can do. For example, if your module provides SSL support, but other
392          * modules provide SSL support too, all the modules supporting SSL should
393          * publish an identical 'SSL' feature. This way, any module requiring use
394          * of SSL functions can just look up the 'SSL' feature using FindFeature,
395          * then use the module pointer they are given.
396          * @param FeatureName The case sensitive feature name to make available
397          * @param Mod a pointer to your module class
398          * @returns True on success, false if the feature is already published by
399          * another module.
400          */
401         bool PublishFeature(const std::string &FeatureName, Module* Mod);
402
403         /** Unpublish a 'feature'.
404          * When your module exits, it must call this method for every feature it
405          * is providing so that the feature table is cleaned up.
406          * @param FeatureName the feature to remove
407          */
408         bool UnpublishFeature(const std::string &FeatureName);
409
410         /** Find a 'feature'.
411          * There are two ways for a module to find another module it depends on.
412          * Either by name, using InspIRCd::FindModule, or by feature, using the
413          * InspIRCd::PublishFeature method. A feature is an arbitary string which
414          * identifies something this module can do. For example, if your module
415          * provides SSL support, but other modules provide SSL support too, all
416          * the modules supporting SSL should publish an identical 'SSL' feature.
417          * To find a module capable of providing the feature you want, simply
418          * call this method with the feature name you are looking for.
419          * @param FeatureName The feature name you wish to obtain the module for
420          * @returns A pointer to a valid module class on success, NULL on failure.
421          */
422         Module* FindFeature(const std::string &FeatureName);
423
424         const std::string& GetModuleName(Module* m);
425
426         bool IsNick(const char* n);
427         bool IsIdent(const char* n);
428
429         userrec* FindDescriptor(int socket);
430
431         bool AddMode(ModeHandler* mh, const unsigned char modechar);
432
433         bool AddModeWatcher(ModeWatcher* mw);
434
435         bool DelModeWatcher(ModeWatcher* mw);
436
437         bool AddResolver(Resolver* r);
438
439         void AddCommand(command_t *f);
440
441         void SendMode(const char **parameters, int pcnt, userrec *user);
442
443         bool MatchText(const std::string &sliteral, const std::string &spattern);
444
445         bool CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user);
446
447         bool IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user);
448
449         void AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
450
451         void AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname);
452
453         void AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr);
454
455         void AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
456
457         void AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
458
459         bool DelGLine(const std::string &hostmask);
460
461         bool DelQLine(const std::string &nickname);
462
463         bool DelZLine(const std::string &ipaddr);
464
465         bool DelKLine(const std::string &hostmask);
466
467         bool DelELine(const std::string &hostmask);
468
469         bool IsValidMask(const std::string &mask);
470
471         void AddSocket(InspSocket* sock);
472
473         void RemoveSocket(InspSocket* sock);
474
475         void DelSocket(InspSocket* sock);
476
477         void RehashServer();
478
479         chanrec* GetChannelIndex(long index);
480
481         void DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream);
482
483         bool NickMatchesEveryone(const std::string &nick, userrec* user);
484         bool IPMatchesEveryone(const std::string &ip, userrec* user);
485         bool HostMatchesEveryone(const std::string &mask, userrec* user);
486         long Duration(const char* str);
487         int OperPassCompare(const char* data,const char* input);
488         bool ULine(const char* server);
489
490         std::string GetRevision();
491         std::string GetVersionString();
492         void WritePID(const std::string &filename);
493         char* ModuleError();
494         bool LoadModule(const char* filename);
495         bool UnloadModule(const char* filename);
496         InspIRCd(int argc, char** argv);
497         void DoOneIteration(bool process_module_sockets);
498         void Log(int level, const char* text, ...);
499         void Log(int level, const std::string &text);
500         int Run();
501 };
502
503 #endif