1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #ifndef __INSPIRCD_H__
18 #define __INSPIRCD_H__
23 #include "inspircd_config.h"
28 #include "helperfuncs.h"
29 #include "socketengine.h"
30 #include "command_parse.h"
32 /* Some misc defines */
37 #define ETIREDGERBILS EAGAIN
39 /** Debug levels for use with InspIRCd::Log()
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
55 #define IS_SINGLE(x,y) ( (*x == y) && (*(x+1) == 0) )
57 #define DELETE(x) {if (x) { delete x; x = NULL; }}
59 template<typename T> inline std::string ConvToStr(const T &in)
61 std::stringstream tmp;
62 if (!(tmp << in)) return std::string();
66 class serverstats : public classbase
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;
79 unsigned long BoundPortCount;
83 statsAccept = statsRefused = statsUnknown = 0;
84 statsCollisions = statsDns = statsDnsGood = 0;
85 statsDnsBad = statsConnects = 0;
86 statsSent = statsRecv = 0.0;
93 class InspIRCd : public classbase
96 /** Holds a string describing the last module error to occur
100 /** This is an internal flag used by the mainloop
104 /** List of server names we've seen.
106 servernamelist servernames;
108 /** Remove a ModuleFactory pointer
110 void EraseFactory(int j);
112 /** Remove a Module pointer
114 void EraseModule(int j);
116 /** Build the ISUPPORT string by triggering all modules On005Numeric events
118 void BuildISupport();
120 /** Move a given module to a specific slot in the list
122 void MoveTo(std::string modulename,int slot);
124 /** Display the startup banner
128 /** Set up the signal handlers
130 void SetSignals(bool SEGVHandler);
132 /** Daemonize the ircd and close standard input/output streams
136 /** Build the upper/lowercase comparison table
140 /** Moves the given module to the last slot in the list
142 void MoveToLast(std::string modulename);
144 /** Moves the given module to the first slot in the list
146 void MoveToFirst(std::string modulename);
148 /** Moves one module to be placed after another in the list
150 void MoveAfter(std::string modulename, std::string after);
152 /** Moves one module to be placed before another in the list
154 void MoveBefore(std::string modulename, std::string before);
156 /** Process a user whos socket has been flagged as active
158 void ProcessUser(userrec* cu);
160 /** Iterate the list of InspSocket objects, removing ones which have timed out
162 void DoSocketTimeouts(time_t TIME);
164 /** Perform background user events such as PING checks
166 void DoBackgroundUserStuff(time_t TIME);
168 /** Returns true when all modules have done pre-registration checks on a user
170 bool AllModulesReportReady(userrec* user);
172 /** Total number of modules loaded into the ircd, minus one
176 /** Logfile pathname specified on the commandline, or empty string
178 char LogFileName[MAXBUF];
180 /** The feature names published by various modules
182 featurelist Features;
184 /** The current time, updated in the mainloop
188 /** The time that was recorded last time around the mainloop
192 /** A 64k buffer used to read client lines into
194 char ReadBuffer[65535];
196 /** Number of seconds in a minute
198 const long duration_m;
200 /** Number of seconds in an hour
202 const long duration_h;
204 /** Number of seconds in a day
206 const long duration_d;
208 /** Number of seconds in a week
210 const long duration_w;
212 /** Number of seconds in a year
214 const long duration_y;
217 /** Time this ircd was booted
221 /** Mode handler, handles mode setting and removal
225 /** Command parser, handles client to server commands
227 CommandParser* Parser;
229 /** Socket engine, handles socket activity events
233 /** Stats class, holds miscellaneous stats counters
237 /** Server Config class, holds configuration file data
239 ServerConfig* Config;
241 /** Module sockets list, holds the active set of InspSocket classes
243 std::vector<InspSocket*> module_sockets;
245 /** Socket reference table, provides fast lookup of fd to InspSocket*
247 InspSocket* socket_ref[MAX_DESCRIPTORS];
249 /** user reference table, provides fast lookup of fd to userrec*
251 userrec* fd_ref_table[MAX_DESCRIPTORS];
253 /** Client list, a hash_map containing all clients, local and remote
255 user_hash clientlist;
257 /** Channel list, a hash_map containing all channels
261 /** Local client list, a vector containing only local clients
263 std::vector<userrec*> local_users;
265 /** Oper list, a vector containing all local and remote opered users
267 std::vector<userrec*> all_opers;
269 /** Whowas container, contains a map of vectors of users tracked by WHOWAS
271 irc::whowas::whowas_users whowas;
273 /** DNS class, provides resolver facilities to the core and modules
277 /** Timer manager class, triggers InspTimer timer events
279 TimerManager* Timers;
281 /** Command list, a hash_map of command names to command_t*
283 command_table cmdlist;
285 /** X-Line manager. Handles G/K/Q/E line setting, removal and matching
287 XLineManager* XLines;
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()
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()
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.
307 /** Get the total number of currently loaded modules
309 int GetModuleCount();
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
315 Module* FindModule(const std::string &name);
317 /** Bind all ports specified in the configuration file.
318 * @param bail True if the function should bail back to the shell on failure
320 int BindPorts(bool bail);
322 bool HasPort(int port, char* addr);
323 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr);
325 void AddServerName(const std::string &servername);
326 const char* FindServerNamePtr(const std::string &servername);
327 bool FindServerName(const std::string &servername);
329 std::string GetServerDescription(const char* servername);
331 void WriteOpers(const char* text, ...);
332 void WriteOpers(const std::string &text);
334 userrec* FindNick(const std::string &nick);
335 userrec* FindNick(const char* nick);
337 chanrec* FindChan(const std::string &chan);
338 chanrec* FindChan(const char* chan);
340 void LoadAllModules();
343 void OpenLog(char** argv, int argc);
345 bool UserToPseudo(userrec* user, const std::string &message);
346 bool PseudoToUser(userrec* alive, userrec* zombie, const std::string &message);
348 void ServerNoticeAll(char* text, ...);
349 void ServerPrivmsgAll(char* text, ...);
350 void WriteMode(const char* modes, int flags, const char* text, ...);
352 bool IsChannel(const char *chname);
354 static void Rehash(int status);
355 static void Exit(int status);
358 int RegisteredUserCount();
359 int InvisibleUserCount();
361 int UnregisteredUserCount();
363 long LocalUserCount();
365 void SendError(const char *s);
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
375 long PriorityBefore(const std::string &modulename);
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
385 long PriorityAfter(const std::string &modulename);
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
401 bool PublishFeature(const std::string &FeatureName, Module* Mod);
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
408 bool UnpublishFeature(const std::string &FeatureName);
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.
422 Module* FindFeature(const std::string &FeatureName);
424 const std::string& GetModuleName(Module* m);
426 bool IsNick(const char* n);
427 bool IsIdent(const char* n);
429 userrec* FindDescriptor(int socket);
431 bool AddMode(ModeHandler* mh, const unsigned char modechar);
433 bool AddModeWatcher(ModeWatcher* mw);
435 bool DelModeWatcher(ModeWatcher* mw);
437 bool AddResolver(Resolver* r);
439 void AddCommand(command_t *f);
441 void SendMode(const char **parameters, int pcnt, userrec *user);
443 bool MatchText(const std::string &sliteral, const std::string &spattern);
445 bool CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user);
447 bool IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user);
449 void AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
451 void AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname);
453 void AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr);
455 void AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
457 void AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
459 bool DelGLine(const std::string &hostmask);
461 bool DelQLine(const std::string &nickname);
463 bool DelZLine(const std::string &ipaddr);
465 bool DelKLine(const std::string &hostmask);
467 bool DelELine(const std::string &hostmask);
469 bool IsValidMask(const std::string &mask);
471 void AddSocket(InspSocket* sock);
473 void RemoveSocket(InspSocket* sock);
475 void DelSocket(InspSocket* sock);
479 chanrec* GetChannelIndex(long index);
481 void DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream);
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);
490 std::string GetRevision();
491 std::string GetVersionString();
492 void WritePID(const std::string &filename);
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);