]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/inspircd.h
64489aaf6a84f0a29af555fc7c4af233a089e6fd
[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 "socketengine.h"
29 #include "command_parse.h"
30 #include "snomasks.h"
31
32 /** Returned by some functions to indicate failure,
33  * and the exit code of the program if it terminates.
34  */
35 #define ERROR -1
36
37 /** Crucial defines
38  */
39 #define ETIREDGERBILS EAGAIN
40
41 /** Debug levels for use with InspIRCd::Log()
42  */
43 enum DebugLevel
44 {
45         DEBUG           =       10,
46         VERBOSE         =       20,
47         DEFAULT         =       30,
48         SPARSE          =       40,
49         NONE            =       50,
50 };
51
52 /**
53  * This define is used in place of strcmp when we 
54  * want to check if a char* string contains only one
55  * letter. Pretty fast, its just two compares and an
56  * addition.
57  */
58 #define IS_SINGLE(x,y) ( (*x == y) && (*(x+1) == 0) )
59
60 /** Delete a pointer, and NULL its value
61  */
62 template<typename T> inline void DELETE(T* x)
63 {
64         delete x;
65         x = NULL;
66 }
67
68 /** Template function to convert any input type to std::string
69  */
70 template<typename T> inline std::string ConvToStr(const T &in)
71 {
72         std::stringstream tmp;
73         if (!(tmp << in)) return std::string();
74         return tmp.str();
75 }
76
77 /** This class contains various STATS counters
78  * It is used by the InspIRCd class, which internally
79  * has an instance of it.
80  */
81 class serverstats : public classbase
82 {
83   public:
84         /** Number of accepted connections
85          */
86         unsigned long statsAccept;
87         /** Number of failed accepts
88          */
89         unsigned long statsRefused;
90         /** Number of unknown commands seen
91          */
92         unsigned long statsUnknown;
93         /** Number of nickname collisions handled
94          */
95         unsigned long statsCollisions;
96         /** Number of DNS queries sent out
97          */
98         unsigned long statsDns;
99         /** Number of good DNS replies received
100          * NOTE: This may not tally to the number sent out,
101          * due to timeouts and other latency issues.
102          */
103         unsigned long statsDnsGood;
104         /** Number of bad (negative) DNS replies received
105          * NOTE: This may not tally to the number sent out,
106          * due to timeouts and other latency issues.
107          */
108         unsigned long statsDnsBad;
109         /** Number of inbound connections seen
110          */
111         unsigned long statsConnects;
112         /** Total bytes of data transmitted
113          */
114         double statsSent;
115         /** Total bytes of data received
116          */
117         double statsRecv;
118         /** Number of bound listening ports
119          */
120         unsigned long BoundPortCount;
121
122         /** The constructor initializes all the counts to zero
123          */
124         serverstats()
125                 : statsAccept(0), statsRefused(0), statsUnknown(0), statsCollisions(0), statsDns(0),
126                 statsDnsGood(0), statsDnsBad(0), statsConnects(0), statsSent(0.0), statsRecv(0.0),
127                 BoundPortCount(0)
128         {
129         }
130 };
131
132 class InspIRCd;
133
134 /** This class implements a nonblocking log-writer.
135  * Most people writing an ircd give little thought to their disk
136  * i/o. On a congested system, disk writes can block for long
137  * periods of time (e.g. if the system is busy and/or swapping
138  * a lot). If we just use a blocking fprintf() call, this could
139  * block for undesirable amounts of time (half of a second through
140  * to whole seconds). We DO NOT want this, so we make our logfile
141  * nonblocking and hook it into the SocketEngine.
142  * NB: If the operating system does not support nonblocking file
143  * I/O (linux seems to, as does freebsd) this will default to
144  * blocking behaviour.
145  */
146 class FileLogger : public EventHandler
147 {
148  protected:
149         /** The creator/owner of this object
150          */
151         InspIRCd* ServerInstance;
152         /** The log file (fd is inside this somewhere,
153          * we get it out with fileno())
154          */
155         FILE* log;
156         /** Buffer of pending log lines to be written
157          */
158         std::string buffer;
159         /** Number of write operations that have occured
160          */
161         int writeops;
162  public:
163         /** The constructor takes an already opened logfile.
164          */
165         FileLogger(InspIRCd* Instance, FILE* logfile);
166         /** This returns false, logfiles are writeable.
167          */
168         virtual bool Readable();
169         /** Handle pending write events.
170          * This will flush any waiting data to disk.
171          * If any data remains after the fprintf call,
172          * another write event is scheduled to write
173          * the rest of the data when possible.
174          */
175         virtual void HandleEvent(EventType et, int errornum = 0);
176         /** Write one or more preformatted log lines.
177          * If the data cannot be written immediately,
178          * this class will insert itself into the
179          * SocketEngine, and register a write event,
180          * and when the write event occurs it will
181          * attempt again to write the data.
182          */
183         void WriteLogLine(const std::string &line);
184         /** Close the log file and cancel any events.
185          */
186         virtual void Close();
187         /** Close the log file and cancel any events.
188          * (indirectly call Close()
189          */
190         virtual ~FileLogger();
191 };
192
193 /** A list of failed port bindings, used for informational purposes on startup */
194 typedef std::vector<std::pair<std::string, long> > FailedPortList;
195
196 /** A list of ip addresses cross referenced against clone counts */
197 typedef std::map<irc::string, unsigned int> clonemap;
198
199 class XLineManager;
200
201 /** The main class of the irc server.
202  * This class contains instances of all the other classes
203  * in this software, with the exception of the base class,
204  * classbase. Amongst other things, it contains a ModeParser,
205  * a DNS object, a CommandParser object, and a list of active
206  * Module objects, and facilities for Module objects to
207  * interact with the core system it implements. You should
208  * NEVER attempt to instantiate a class of type InspIRCd
209  * yourself. If you do, this is equivalent to spawning a second
210  * IRC server, and could have catastrophic consequences for the
211  * program in terms of ram usage (basically, you could create
212  * an obese forkbomb built from recursively spawning irc servers!)
213  */
214 class InspIRCd : public classbase
215 {
216  private:
217         /** Holds a string describing the last module error to occur
218          */
219         char MODERR[MAXBUF];
220  
221         /** Remove a ModuleFactory pointer
222          * @param j Index number of the ModuleFactory to remove
223          */
224         void EraseFactory(int j);
225
226         /** Remove a Module pointer
227          * @param j Index number of the Module to remove
228          */
229         void EraseModule(int j);
230
231         /** Build the ISUPPORT string by triggering all modules On005Numeric events
232          */
233         void BuildISupport();
234
235         /** Move a given module to a specific slot in the list
236          * @param modulename The module name to relocate
237          * @param slot The slot to move the module into
238          */
239         void MoveTo(std::string modulename,int slot);
240
241         /** Display the startup banner
242          */
243         void Start();
244
245         /** Set up the signal handlers
246          */
247         void SetSignals();
248
249         /** Daemonize the ircd and close standard input/output streams
250          * @return True if the program daemonized succesfully
251          */
252         bool DaemonSeed();
253
254         /** Moves the given module to the last slot in the list
255          * @param modulename The module name to relocate
256          */
257         void MoveToLast(std::string modulename);
258
259         /** Moves the given module to the first slot in the list
260          * @param modulename The module name to relocate
261          */
262         void MoveToFirst(std::string modulename);
263
264         /** Moves one module to be placed after another in the list
265          * @param modulename The module name to relocate
266          * @param after The module name to place the module after
267          */
268         void MoveAfter(std::string modulename, std::string after);
269
270         /** Moves one module to be placed before another in the list
271          * @param modulename The module name to relocate
272          * @param after The module name to place the module before
273          */
274         void MoveBefore(std::string modulename, std::string before);
275
276         /** Iterate the list of InspSocket objects, removing ones which have timed out
277          * @param TIME the current time
278          */
279         void DoSocketTimeouts(time_t TIME);
280
281         /** Perform background user events such as PING checks
282          * @param TIME the current time
283          */
284         void DoBackgroundUserStuff(time_t TIME);
285
286         /** Returns true when all modules have done pre-registration checks on a user
287          * @param user The user to verify
288          * @return True if all modules have finished checking this user
289          */
290         bool AllModulesReportReady(userrec* user);
291
292         /** Total number of modules loaded into the ircd, minus one
293          */
294         int ModCount;
295
296         /** Logfile pathname specified on the commandline, or empty string
297          */
298         char LogFileName[MAXBUF];
299
300         /** The feature names published by various modules
301          */
302         featurelist Features;
303
304         /** The interface names published by various modules
305          */
306         interfacelist Interfaces;
307
308         /** The current time, updated in the mainloop
309          */
310         time_t TIME;
311
312         /** The time that was recorded last time around the mainloop
313          */
314         time_t OLDTIME;
315
316         /** A 64k buffer used to read client lines into
317          */
318         char ReadBuffer[65535];
319
320         /** Number of seconds in a minute
321          */
322         const long duration_m;
323
324         /** Number of seconds in an hour
325          */
326         const long duration_h;
327
328         /** Number of seconds in a day
329          */
330         const long duration_d;
331
332         /** Number of seconds in a week
333          */
334         const long duration_w;
335
336         /** Number of seconds in a year
337          */
338         const long duration_y;
339
340         /** Used when connecting clients
341          */
342         insp_sockaddr client, server;
343
344         /** Used when connecting clients
345          */
346         socklen_t length;
347
348         /** Nonblocking file writer
349          */
350         FileLogger* Logger;
351
352         /** Time offset in seconds
353          * This offset is added to all calls to Time(). Use SetTimeDelta() to update
354          */
355         int time_delta;
356
357  public:
358         /** List of server names we've seen.
359          */
360         servernamelist servernames;
361
362         /** Time this ircd was booted
363          */
364         time_t startup_time;
365
366         /** Mode handler, handles mode setting and removal
367          */
368         ModeParser* Modes;
369
370         /** Command parser, handles client to server commands
371          */
372         CommandParser* Parser;
373
374         /** Socket engine, handles socket activity events
375          */
376         SocketEngine* SE;
377
378         /** Stats class, holds miscellaneous stats counters
379          */
380         serverstats* stats;
381
382         /**  Server Config class, holds configuration file data
383          */
384         ServerConfig* Config;
385
386         /** Snomask manager - handles routing of snomask messages
387          * to opers.
388          */
389         SnomaskManager* SNO;
390
391         /** Client list, a hash_map containing all clients, local and remote
392          */
393         user_hash clientlist;
394
395         /** Channel list, a hash_map containing all channels
396          */
397         chan_hash chanlist;
398
399         /** Local client list, a vector containing only local clients
400          */
401         std::vector<userrec*> local_users;
402
403         /** Oper list, a vector containing all local and remote opered users
404          */
405         std::vector<userrec*> all_opers;
406
407         clonemap local_clones;
408
409         clonemap global_clones;
410
411         /** Whowas container, contains a map of vectors of users tracked by WHOWAS
412          */
413         irc::whowas::whowas_users whowas;
414
415         /** Whowas container, contains a map of time_t to users tracked by WHOWAS
416          */
417         irc::whowas::whowas_users_fifo whowas_fifo;
418
419         /** DNS class, provides resolver facilities to the core and modules
420          */
421         DNS* Res;
422
423         /** Timer manager class, triggers InspTimer timer events
424          */
425         TimerManager* Timers;
426
427         /** X-Line manager. Handles G/K/Q/E line setting, removal and matching
428          */
429         XLineManager* XLines;
430
431         /** A list of Module* module classes
432          * Note that this list is always exactly 255 in size.
433          * The actual number of loaded modules is available from GetModuleCount()
434          */
435         ModuleList modules;
436
437         /** A list of ModuleFactory* module factories
438          * Note that this list is always exactly 255 in size.
439          * The actual number of loaded modules is available from GetModuleCount()
440          */
441         FactoryList factory;
442
443         /** The time we next call our ping timeout and reg timeout checks
444          */
445         time_t next_call;
446
447         /** Get the current time
448          * Because this only calls time() once every time around the mainloop,
449          * it is much faster than calling time() directly.
450          * @param delta True to use the delta as an offset, false otherwise
451          * @return The current time as an epoch value (time_t)
452          */
453         time_t Time(bool delta = false);
454
455         /** Set the time offset in seconds
456          * This offset is added to Time() to offset the system time by the specified
457          * number of seconds.
458          * @param delta The number of seconds to offset
459          * @return The old time delta
460          */
461         int SetTimeDelta(int delta);
462
463         void AddLocalClone(userrec* user);
464
465         void AddGlobalClone(userrec* user);
466
467         /** Get the time offset in seconds
468          * @return The current time delta (in seconds)
469          */
470         int GetTimeDelta();
471
472         /** Process a user whos socket has been flagged as active
473          * @param cu The user to process
474          * @return There is no actual return value, however upon exit, the user 'cu' may have been deleted
475          */
476         void ProcessUser(userrec* cu);
477
478         /** Get the total number of currently loaded modules
479          * @return The number of loaded modules
480          */
481         int GetModuleCount();
482
483         /** Find a module by name, and return a Module* to it.
484          * This is preferred over iterating the module lists yourself.
485          * @param name The module name to look up
486          * @return A pointer to the module, or NULL if the module cannot be found
487          */
488         Module* FindModule(const std::string &name);
489
490         /** Bind all ports specified in the configuration file.
491          * @param bail True if the function should bail back to the shell on failure
492          * @param found_ports The actual number of ports found in the config, as opposed to the number actually bound
493          * @return The number of ports actually bound without error
494          */
495         int BindPorts(bool bail, int &found_ports, FailedPortList &failed_ports);
496
497         /** Returns true if this server has the given port bound to the given address
498          * @param port The port number
499          * @param addr The address
500          * @return True if we have a port listening on this address
501          */
502         bool HasPort(int port, char* addr);
503
504         /** Binds a socket on an already open file descriptor
505          * @param sockfd A valid file descriptor of an open socket
506          * @param client A sockaddr to use as temporary storage
507          * @param server A sockaddr to use as temporary storage
508          * @param port The port number to bind to
509          * @param addr The address to bind to (IP only)
510          * @return True if the port was bound successfully
511          */
512         bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr);
513
514         /** Adds a server name to the list of servers we've seen
515          * @param The servername to add
516          */
517         void AddServerName(const std::string &servername);
518
519         /** Finds a cached char* pointer of a server name,
520          * This is used to optimize userrec by storing only the pointer to the name
521          * @param The servername to find
522          * @return A pointer to this name, gauranteed to never become invalid
523          */
524         const char* FindServerNamePtr(const std::string &servername);
525
526         /** Returns true if we've seen the given server name before
527          * @param The servername to find
528          * @return True if we've seen this server name before
529          */
530         bool FindServerName(const std::string &servername);
531
532         /** Gets the GECOS (description) field of the given server.
533          * If the servername is not that of the local server, the name
534          * is passed to handling modules which will attempt to determine
535          * the GECOS that bleongs to the given servername.
536          * @param servername The servername to find the description of
537          * @return The description of this server, or of the local server
538          */
539         std::string GetServerDescription(const char* servername);
540
541         /** Write text to all opers connected to this server
542          * @param text The text format string
543          * @param ... Format args
544          */
545         void WriteOpers(const char* text, ...);
546
547         /** Write text to all opers connected to this server
548          * @param text The text to send
549          */
550         void WriteOpers(const std::string &text);
551         
552         /** Find a nickname in the nick hash
553          * @param nick The nickname to find
554          * @return A pointer to the user, or NULL if the user does not exist
555          */
556         userrec* FindNick(const std::string &nick);
557
558         /** Find a nickname in the nick hash
559          * @param nick The nickname to find
560          * @return A pointer to the user, or NULL if the user does not exist
561          */
562         userrec* FindNick(const char* nick);
563
564         /** Find a channel in the channels hash
565          * @param chan The channel to find
566          * @return A pointer to the channel, or NULL if the channel does not exist
567          */
568         chanrec* FindChan(const std::string &chan);
569
570         /** Find a channel in the channels hash
571          * @param chan The channel to find
572          * @return A pointer to the channel, or NULL if the channel does not exist
573          */
574         chanrec* FindChan(const char* chan);
575
576         /** Called by the constructor to load all modules from the config file.
577          */
578         void LoadAllModules();
579
580         /** Check for a 'die' tag in the config file, and abort if found
581          * @return Depending on the configuration, this function may never return
582          */
583         void CheckDie();
584
585         /** Check we aren't running as root, and exit if we are
586          * @return Depending on the configuration, this function may never return
587          */
588         void CheckRoot();
589
590         /** Determine the right path for, and open, the logfile
591          * @param argv The argv passed to main() initially, used to calculate program path
592          * @param argc The argc passed to main() initially, used to calculate program path
593          */
594         void OpenLog(char** argv, int argc);
595
596         /** Convert a user to a pseudoclient, disconnecting the real user
597          * @param user The user to convert
598          * @param message The quit message to display when exiting the user
599          * @return True if the operation succeeded
600          */
601         bool UserToPseudo(userrec* user, const std::string &message);
602
603         /** Convert a pseudoclient to a real user, discarding the pseudoclient
604          * @param alive A live client
605          * @param zombie A pseudoclient
606          * @param message The message to display when quitting the pseudoclient
607          * @return True if the operation succeeded
608          */
609         bool PseudoToUser(userrec* alive, userrec* zombie, const std::string &message);
610
611         /** Send a server notice to all local users
612          * @param text The text format string to send
613          * @param ... The format arguments
614          */
615         void ServerNoticeAll(char* text, ...);
616
617         /** Send a server message (PRIVMSG) to all local users
618          * @param text The text format string to send
619          * @param ... The format arguments
620          */
621         void ServerPrivmsgAll(char* text, ...);
622
623         /** Send text to all users with a specific set of modes
624          * @param modes The modes to check against, without a +, e.g. 'og'
625          * @param flags one of WM_OR or WM_AND. If you specify WM_OR, any one of the 
626          * mode characters in the first parameter causes receipt of the message, and
627          * if you specify WM_OR, all the modes must be present.
628          * @param text The text format string to send
629          * @param ... The format arguments
630          */
631         void WriteMode(const char* modes, int flags, const char* text, ...);
632
633         /** Return true if a channel name is valid
634          * @param chname A channel name to verify
635          * @return True if the name is valid
636          */
637         bool IsChannel(const char *chname);
638
639         /** Rehash the local server
640          * @param status This value is unused, and required for signal handler functions
641          */
642         static void Rehash(int status);
643
644         /** Causes the server to exit immediately
645          * @param The exit code to give to the operating system
646          */
647         static void Exit(int status);
648
649         /** Return a count of users, unknown and known connections
650          * @return The number of users
651          */
652         int UserCount();
653
654         /** Return a count of fully registered connections only
655          * @return The number of registered users
656          */
657         int RegisteredUserCount();
658
659         /** Return a count of invisible (umode +i) users only
660          * @return The number of invisible users
661          */
662         int InvisibleUserCount();
663
664         /** Return a count of opered (umode +o) users only
665          * @return The number of opers
666          */
667         int OperCount();
668
669         /** Return a count of unregistered (before NICK/USER) users only
670          * @return The number of unregistered (unknown) connections
671          */
672         int UnregisteredUserCount();
673
674         /** Return a count of channels on the network
675          * @return The number of channels
676          */
677         long ChannelCount();
678
679         /** Return a count of local users on this server only
680          * @return The number of local users
681          */
682         long LocalUserCount();
683
684         /** Send an error notice to all local users, opered and unopered
685          * @param s The error string to send
686          */
687         void SendError(const char *s);
688
689         /** For use with Module::Prioritize().
690          * When the return value of this function is returned from
691          * Module::Prioritize(), this specifies that the module wishes
692          * to be ordered exactly BEFORE 'modulename'. For more information
693          * please see Module::Prioritize().
694          * @param modulename The module your module wants to be before in the call list
695          * @returns a priority ID which the core uses to relocate the module in the list
696          */
697         long PriorityBefore(const std::string &modulename);
698
699         /** For use with Module::Prioritize().
700          * When the return value of this function is returned from
701          * Module::Prioritize(), this specifies that the module wishes
702          * to be ordered exactly AFTER 'modulename'. For more information please
703          * see Module::Prioritize().
704          * @param modulename The module your module wants to be after in the call list
705          * @returns a priority ID which the core uses to relocate the module in the list
706          */
707         long PriorityAfter(const std::string &modulename);
708
709         /** Publish a 'feature'.
710          * There are two ways for a module to find another module it depends on.
711          * Either by name, using InspIRCd::FindModule, or by feature, using this
712          * function. A feature is an arbitary string which identifies something this
713          * module can do. For example, if your module provides SSL support, but other
714          * modules provide SSL support too, all the modules supporting SSL should
715          * publish an identical 'SSL' feature. This way, any module requiring use
716          * of SSL functions can just look up the 'SSL' feature using FindFeature,
717          * then use the module pointer they are given.
718          * @param FeatureName The case sensitive feature name to make available
719          * @param Mod a pointer to your module class
720          * @returns True on success, false if the feature is already published by
721          * another module.
722          */
723         bool PublishFeature(const std::string &FeatureName, Module* Mod);
724
725         /** Publish a module to an 'interface'.
726          * Modules which implement the same interface (the same way of communicating
727          * with other modules) can publish themselves to an interface, using this
728          * method. When they do so, they become part of a list of related or
729          * compatible modules, and a third module may then query for that list
730          * and know that all modules within that list offer the same API.
731          * A prime example of this is the hashing modules, which all accept the
732          * same types of Request class. Consider this to be similar to PublishFeature,
733          * except for that multiple modules may publish the same 'feature'.
734          * @param InterfaceName The case sensitive interface name to make available
735          * @param Mod a pointer to your module class
736          * @returns True on success, false on failure (there are currently no failure
737          * cases)
738          */
739         bool PublishInterface(const std::string &InterfaceName, Module* Mod);
740
741         /** Return a pair saying how many other modules are currently using the
742          * interfaces provided by module m.
743          * @param m The module to count usage for
744          * @return A pair, where the first value is the number of uses of the interface,
745          * and the second value is the interface name being used.
746          */
747         std::pair<int,std::string> GetInterfaceInstanceCount(Module* m);
748
749         /** Mark your module as using an interface.
750          * If you mark your module as using an interface, then that interface
751          * module may not unload until your module has unloaded first.
752          * This can be used to prevent crashes by ensuring code you depend on
753          * is always in memory while your module is active.
754          * @param InterfaceName The interface to use
755          */
756         void UseInterface(const std::string &InterfaceName);
757
758         /** Mark your module as finished with an interface.
759          * If you used UseInterface() above, you should use this method when
760          * your module is finished with the interface (usually in its destructor)
761          * to allow the modules which implement the given interface to be unloaded.
762          * @param InterfaceName The interface you are finished with using.
763          */
764         void DoneWithInterface(const std::string &InterfaceName);
765
766         /** Unpublish a 'feature'.
767          * When your module exits, it must call this method for every feature it
768          * is providing so that the feature table is cleaned up.
769          * @param FeatureName the feature to remove
770          */
771         bool UnpublishFeature(const std::string &FeatureName);
772
773         /** Unpublish your module from an interface
774          * When your module exits, it must call this method for every interface
775          * it is part of so that the interfaces table is cleaned up. Only when
776          * the last item is deleted from an interface does the interface get
777          * removed.
778          * @param InterfaceName the interface to be removed from
779          * @param Mod The module to remove from the interface list
780          */
781         bool UnpublishInterface(const std::string &InterfaceName, Module* Mod);
782
783         /** Find a 'feature'.
784          * There are two ways for a module to find another module it depends on.
785          * Either by name, using InspIRCd::FindModule, or by feature, using the
786          * InspIRCd::PublishFeature method. A feature is an arbitary string which
787          * identifies something this module can do. For example, if your module
788          * provides SSL support, but other modules provide SSL support too, all
789          * the modules supporting SSL should publish an identical 'SSL' feature.
790          * To find a module capable of providing the feature you want, simply
791          * call this method with the feature name you are looking for.
792          * @param FeatureName The feature name you wish to obtain the module for
793          * @returns A pointer to a valid module class on success, NULL on failure.
794          */
795         Module* FindFeature(const std::string &FeatureName);
796
797         /** Find an 'interface'.
798          * An interface is a list of modules which all implement the same API.
799          * @param InterfaceName The Interface you wish to obtain the module
800          * list of.
801          * @return A pointer to a deque of Module*, or NULL if the interface
802          * does not exist.
803          */
804         modulelist* FindInterface(const std::string &InterfaceName);
805
806         /** Given a pointer to a Module, return its filename
807          * @param m The module pointer to identify
808          * @return The module name or an empty string
809          */
810         const std::string& GetModuleName(Module* m);
811
812         /** Return true if a nickname is valid
813          * @param n A nickname to verify
814          * @return True if the nick is valid
815          */
816         bool IsNick(const char* n);
817
818         /** Return true if an ident is valid
819          * @param An ident to verify
820          * @return True if the ident is valid
821          */
822         bool IsIdent(const char* n);
823
824         /** Find a username by their file descriptor.
825          * It is preferred to use this over directly accessing the fd_ref_table array.
826          * @param socket The file descriptor of a user
827          * @return A pointer to the user if the user exists locally on this descriptor
828          */
829         userrec* FindDescriptor(int socket);
830
831         /** Add a new mode to this server's mode parser
832          * @param mh The modehandler to add
833          * @param modechar The mode character this modehandler handles
834          * @return True if the mode handler was added
835          */
836         bool AddMode(ModeHandler* mh, const unsigned char modechar);
837
838         /** Add a new mode watcher to this server's mode parser
839          * @param mw The modewatcher to add
840          * @return True if the modewatcher was added
841          */
842         bool AddModeWatcher(ModeWatcher* mw);
843
844         /** Delete a mode watcher from this server's mode parser
845          * @param mw The modewatcher to delete
846          * @return True if the modewatcher was deleted
847          */
848         bool DelModeWatcher(ModeWatcher* mw);
849
850         /** Add a dns Resolver class to this server's active set
851          * @param r The resolver to add
852          * @return True if the resolver was added
853          */
854         bool AddResolver(Resolver* r);
855
856         /** Add a command to this server's command parser
857          * @param f A command_t command handler object to add
858          * @throw ModuleException Will throw ModuleExcption if the command already exists
859          */
860         void AddCommand(command_t *f);
861
862         /** Send a modechange.
863          * The parameters provided are identical to that sent to the
864          * handler for class cmd_mode.
865          * @param parameters The mode parameters
866          * @param pcnt The number of items you have given in the first parameter
867          * @param user The user to send error messages to
868          */
869         void SendMode(const char **parameters, int pcnt, userrec *user);
870
871         /** Match two strings using pattern matching.
872          * This operates identically to the global function match(),
873          * except for that it takes std::string arguments rather than
874          * const char* ones.
875          * @param sliteral The literal string to match against
876          * @param spattern The pattern to match against. CIDR and globs are supported.
877          */
878         bool MatchText(const std::string &sliteral, const std::string &spattern);
879
880         /** Call the handler for a given command.
881          * @param commandname The command whos handler you wish to call
882          * @param parameters The mode parameters
883          * @param pcnt The number of items you have given in the first parameter
884          * @param user The user to execute the command as
885          * @return True if the command handler was called successfully
886          */
887         bool CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user);
888
889         /** Return true if the command is a module-implemented command and the given parameters are valid for it
890          * @param parameters The mode parameters
891          * @param pcnt The number of items you have given in the first parameter
892          * @param user The user to test-execute the command as
893          * @return True if the command handler is a module command, and there are enough parameters and the user has permission to the command
894          */
895         bool IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user);
896
897         /** Add a gline and apply it
898          * @param duration How long the line should last
899          * @param source Who set the line
900          * @param reason The reason for the line
901          * @param hostmask The hostmask to set the line against
902          */
903         void AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
904
905         /** Add a qline and apply it
906          * @param duration How long the line should last
907          * @param source Who set the line
908          * @param reason The reason for the line
909          * @param nickname The nickmask to set the line against
910          */
911         void AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname);
912
913         /** Add a zline and apply it
914          * @param duration How long the line should last
915          * @param source Who set the line
916          * @param reason The reason for the line
917          * @param ipaddr The ip-mask to set the line against
918          */
919         void AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr);
920
921         /** Add a kline and apply it
922          * @param duration How long the line should last
923          * @param source Who set the line
924          * @param reason The reason for the line
925          * @param hostmask The hostmask to set the line against
926          */
927         void AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
928
929         /** Add an eline
930          * @param duration How long the line should last
931          * @param source Who set the line
932          * @param reason The reason for the line
933          * @param hostmask The hostmask to set the line against
934          */
935         void AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
936
937         /** Delete a gline
938          * @param hostmask The gline to delete
939          * @return True if the item was removed
940          */
941         bool DelGLine(const std::string &hostmask);
942
943         /** Delete a qline
944          * @param nickname The qline to delete
945          * @return True if the item was removed
946          */
947         bool DelQLine(const std::string &nickname);
948
949         /** Delete a zline
950          * @param ipaddr The zline to delete
951          * @return True if the item was removed
952          */
953         bool DelZLine(const std::string &ipaddr);
954
955         /** Delete a kline
956          * @param hostmask The kline to delete
957          * @return True if the item was removed
958          */
959         bool DelKLine(const std::string &hostmask);
960
961         /** Delete an eline
962          * @param hostmask The kline to delete
963          * @return True if the item was removed
964          */
965         bool DelELine(const std::string &hostmask);
966
967         /** Return true if the given parameter is a valid nick!user\@host mask
968          * @param mask A nick!user\@host masak to match against 
969          * @return True i the mask is valid
970          */
971         bool IsValidMask(const std::string &mask);
972
973         /** Rehash the local server
974          */
975         void RehashServer();
976
977         /** Return the channel whos index number matches that provided
978          * @param The index number of the channel to fetch
979          * @return A channel record, or NUll if index < 0 or index >= InspIRCd::ChannelCount()
980          */
981         chanrec* GetChannelIndex(long index);
982
983         /** Dump text to a user target, splitting it appropriately to fit
984          * @param User the user to dump the text to
985          * @param LinePrefix text to prefix each complete line with
986          * @param TextStream the text to send to the user
987          */
988         void DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream);
989
990         /** Check if the given nickmask matches too many users, send errors to the given user
991          * @param nick A nickmask to match against
992          * @param user A user to send error text to
993          * @return True if the nick matches too many users
994          */
995         bool NickMatchesEveryone(const std::string &nick, userrec* user);
996
997         /** Check if the given IP mask matches too many users, send errors to the given user
998          * @param ip An ipmask to match against
999          * @param user A user to send error text to
1000          * @return True if the ip matches too many users
1001          */
1002         bool IPMatchesEveryone(const std::string &ip, userrec* user);
1003
1004         /** Check if the given hostmask matches too many users, send errors to the given user
1005          * @param mask A hostmask to match against
1006          * @param user A user to send error text to
1007          * @return True if the host matches too many users
1008          */
1009         bool HostMatchesEveryone(const std::string &mask, userrec* user);
1010
1011         /** Calculate a duration in seconds from a string in the form 1y2w3d4h6m5s
1012          * @param str A string containing a time in the form 1y2w3d4h6m5s
1013          * (one year, two weeks, three days, four hours, six minutes and five seconds)
1014          * @return The total number of seconds
1015          */
1016         long Duration(const char* str);
1017
1018         /** Attempt to compare an oper password to a string from the config file.
1019          * This will be passed to handling modules which will compare the data
1020          * against possible hashed equivalents in the input string.
1021          * @param data The data from the config file
1022          * @param input The data input by the oper
1023          * @param tagnum the tag number of the oper's tag in the config file
1024          * @return 0 if the strings match, 1 or -1 if they do not
1025          */
1026         int OperPassCompare(const char* data,const char* input, int tagnum);
1027
1028         /** Check if a given server is a uline.
1029          * An empty string returns true, this is by design.
1030          * @param server The server to check for uline status
1031          * @return True if the server is a uline OR the string is empty
1032          */
1033         bool ULine(const char* server);
1034
1035         /** Returns the subversion revision ID of this ircd
1036          * @return The revision ID or an empty string
1037          */
1038         std::string GetRevision();
1039
1040         /** Returns the full version string of this ircd
1041          * @return The version string
1042          */
1043         std::string GetVersionString();
1044
1045         /** Attempt to write the process id to a given file
1046          * @param filename The PID file to attempt to write to
1047          * @return This function may bail if the file cannot be written
1048          */
1049         void WritePID(const std::string &filename);
1050
1051         /** Returns text describing the last module error
1052          * @return The last error message to occur
1053          */
1054         char* ModuleError();
1055
1056         /** Load a given module file
1057          * @param filename The file to load
1058          * @return True if the module was found and loaded
1059          */
1060         bool LoadModule(const char* filename);
1061
1062         /** Unload a given module file
1063          * @param filename The file to unload
1064          * @return True if the module was unloaded
1065          */
1066         bool UnloadModule(const char* filename);
1067
1068         /** This constructor initialises all the subsystems and reads the config file.
1069          * @param argc The argument count passed to main()
1070          * @param argv The argument list passed to main()
1071          * @throw <anything> If anything is thrown from here and makes it to
1072          * you, you should probably just give up and go home. Yes, really.
1073          * It's that bad. Higher level classes should catch any non-fatal exceptions.
1074          */
1075         InspIRCd(int argc, char** argv);
1076
1077         /** Do one iteration of the mainloop
1078          * @param process_module_sockets True if module sockets are to be processed
1079          * this time around the event loop. The is the default.
1080          */
1081         void DoOneIteration(bool process_module_sockets = true);
1082
1083         /** Output a log message to the ircd.log file
1084          * The text will only be output if the current loglevel
1085          * is less than or equal to the level you provide
1086          * @param level A log level from the DebugLevel enum
1087          * @param text Format string of to write to the log
1088          * @param ... Format arguments of text to write to the log
1089          */
1090         void Log(int level, const char* text, ...);
1091
1092         /** Output a log message to the ircd.log file
1093          * The text will only be output if the current loglevel
1094          * is less than or equal to the level you provide
1095          * @param level A log level from the DebugLevel enum
1096          * @param text Text to write to the log
1097          */
1098         void Log(int level, const std::string &text);
1099
1100         void SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text);
1101
1102         void SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...);
1103
1104         /** Begin execution of the server.
1105          * NOTE: this function NEVER returns. Internally,
1106          * after performing some initialisation routines,
1107          * it will repeatedly call DoOneIteration in a loop.
1108          * @return The return value for this function is undefined.
1109          */
1110         int Run();
1111 };
1112
1113 #endif
1114