]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/inspircd.h
Add support for OnWhoisLine, which allows modules to change or drop any line of whois...
[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);
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 class XLineManager;
194
195 /** The main class of the irc server.
196  * This class contains instances of all the other classes
197  * in this software, with the exception of the base class,
198  * classbase. Amongst other things, it contains a ModeParser,
199  * a DNS object, a CommandParser object, and a list of active
200  * Module objects, and facilities for Module objects to
201  * interact with the core system it implements. You should
202  * NEVER attempt to instantiate a class of type InspIRCd
203  * yourself. If you do, this is equivalent to spawning a second
204  * IRC server, and could have catastrophic consequences for the
205  * program in terms of ram usage (basically, you could create
206  * an obese forkbomb built from recursively spawning irc servers!)
207  */
208 class InspIRCd : public classbase
209 {
210  private:
211         /** Holds a string describing the last module error to occur
212          */
213         char MODERR[MAXBUF];
214
215         /** This is an internal flag used by the mainloop
216          */
217         bool expire_run;
218
219         /** List of server names we've seen.
220          */
221         servernamelist servernames;
222  
223         /** Remove a ModuleFactory pointer
224          * @param j Index number of the ModuleFactory to remove
225          */
226         void EraseFactory(int j);
227
228         /** Remove a Module pointer
229          * @param j Index number of the Module to remove
230          */
231         void EraseModule(int j);
232
233         /** Build the ISUPPORT string by triggering all modules On005Numeric events
234          */
235         void BuildISupport();
236
237         /** Move a given module to a specific slot in the list
238          * @param modulename The module name to relocate
239          * @param slot The slot to move the module into
240          */
241         void MoveTo(std::string modulename,int slot);
242
243         /** Display the startup banner
244          */
245         void Start();
246
247         /** Set up the signal handlers
248          */
249         void SetSignals();
250
251         /** Daemonize the ircd and close standard input/output streams
252          * @return True if the program daemonized succesfully
253          */
254         bool DaemonSeed();
255
256         /** Moves the given module to the last slot in the list
257          * @param modulename The module name to relocate
258          */
259         void MoveToLast(std::string modulename);
260
261         /** Moves the given module to the first slot in the list
262          * @param modulename The module name to relocate
263          */
264         void MoveToFirst(std::string modulename);
265
266         /** Moves one module to be placed after another in the list
267          * @param modulename The module name to relocate
268          * @param after The module name to place the module after
269          */
270         void MoveAfter(std::string modulename, std::string after);
271
272         /** Moves one module to be placed before another in the list
273          * @param modulename The module name to relocate
274          * @param after The module name to place the module before
275          */
276         void MoveBefore(std::string modulename, std::string before);
277
278         /** Iterate the list of InspSocket objects, removing ones which have timed out
279          * @param TIME the current time
280          */
281         void DoSocketTimeouts(time_t TIME);
282
283         /** Perform background user events such as PING checks
284          * @param TIME the current time
285          */
286         void DoBackgroundUserStuff(time_t TIME);
287
288         /** Returns true when all modules have done pre-registration checks on a user
289          * @param user The user to verify
290          * @return True if all modules have finished checking this user
291          */
292         bool AllModulesReportReady(userrec* user);
293
294         /** Total number of modules loaded into the ircd, minus one
295          */
296         int ModCount;
297
298         /** Logfile pathname specified on the commandline, or empty string
299          */
300         char LogFileName[MAXBUF];
301
302         /** The feature names published by various modules
303          */
304         featurelist Features;
305
306         /** The current time, updated in the mainloop
307          */
308         time_t TIME;
309
310         /** The time that was recorded last time around the mainloop
311          */
312         time_t OLDTIME;
313
314         /** A 64k buffer used to read client lines into
315          */
316         char ReadBuffer[65535];
317
318         /** Number of seconds in a minute
319          */
320         const long duration_m;
321
322         /** Number of seconds in an hour
323          */
324         const long duration_h;
325
326         /** Number of seconds in a day
327          */
328         const long duration_d;
329
330         /** Number of seconds in a week
331          */
332         const long duration_w;
333
334         /** Number of seconds in a year
335          */
336         const long duration_y;
337
338         /** Used when connecting clients
339          */
340         insp_sockaddr client, server;
341
342         /** Used when connecting clients
343          */
344         socklen_t length;
345
346         /** Nonblocking file writer
347          */
348         FileLogger* Logger;
349
350  public:
351         /** Time this ircd was booted
352          */
353         time_t startup_time;
354
355         /** Mode handler, handles mode setting and removal
356          */
357         ModeParser* Modes;
358
359         /** Command parser, handles client to server commands
360          */
361         CommandParser* Parser;
362
363         /** Socket engine, handles socket activity events
364          */
365         SocketEngine* SE;
366
367         /** Stats class, holds miscellaneous stats counters
368          */
369         serverstats* stats;
370
371         /**  Server Config class, holds configuration file data
372          */
373         ServerConfig* Config;
374
375         /** Snomask manager - handles routing of snomask messages
376          * to opers.
377          */
378         SnomaskManager* SNO;
379
380         /** Client list, a hash_map containing all clients, local and remote
381          */
382         user_hash clientlist;
383
384         /** Channel list, a hash_map containing all channels
385          */
386         chan_hash chanlist;
387
388         /** Local client list, a vector containing only local clients
389          */
390         std::vector<userrec*> local_users;
391
392         /** Oper list, a vector containing all local and remote opered users
393          */
394         std::vector<userrec*> all_opers;
395
396         /** Whowas container, contains a map of vectors of users tracked by WHOWAS
397          */
398         irc::whowas::whowas_users whowas;
399
400         /** DNS class, provides resolver facilities to the core and modules
401          */
402         DNS* Res;
403
404         /** Timer manager class, triggers InspTimer timer events
405          */
406         TimerManager* Timers;
407
408         /** X-Line manager. Handles G/K/Q/E line setting, removal and matching
409          */
410         XLineManager* XLines;
411
412         /** A list of Module* module classes
413          * Note that this list is always exactly 255 in size.
414          * The actual number of loaded modules is available from GetModuleCount()
415          */
416         ModuleList modules;
417
418         /** A list of ModuleFactory* module factories
419          * Note that this list is always exactly 255 in size.
420          * The actual number of loaded modules is available from GetModuleCount()
421          */
422         FactoryList factory;
423
424         /** The time we next call our ping timeout and reg timeout checks
425          */
426         time_t next_call;
427
428         /** Get the current time
429          * Because this only calls time() once every time around the mainloop,
430          * it is much faster than calling time() directly.
431          * @return The current time as an epoch value (time_t)
432          */
433         time_t Time();
434
435         /** Process a user whos socket has been flagged as active
436          * @param cu The user to process
437          * @return There is no actual return value, however upon exit, the user 'cu' may have been deleted
438          */
439         void ProcessUser(userrec* cu);
440
441         /** Get the total number of currently loaded modules
442          * @return The number of loaded modules
443          */
444         int GetModuleCount();
445
446         /** Find a module by name, and return a Module* to it.
447          * This is preferred over iterating the module lists yourself.
448          * @param name The module name to look up
449          * @return A pointer to the module, or NULL if the module cannot be found
450          */
451         Module* FindModule(const std::string &name);
452
453         /** Bind all ports specified in the configuration file.
454          * @param bail True if the function should bail back to the shell on failure
455          * @return The number of ports actually bound without error
456          */
457         int BindPorts(bool bail);
458
459         /** Returns true if this server has the given port bound to the given address
460          * @param port The port number
461          * @param addr The address
462          * @return True if we have a port listening on this address
463          */
464         bool HasPort(int port, char* addr);
465
466         /** Binds a socket on an already open file descriptor
467          * @param sockfd A valid file descriptor of an open socket
468          * @param client A sockaddr to use as temporary storage
469          * @param server A sockaddr to use as temporary storage
470          * @param port The port number to bind to
471          * @param addr The address to bind to (IP only)
472          * @return True if the port was bound successfully
473          */
474         bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr);
475
476         /** Adds a server name to the list of servers we've seen
477          * @param The servername to add
478          */
479         void AddServerName(const std::string &servername);
480
481         /** Finds a cached char* pointer of a server name,
482          * This is used to optimize userrec by storing only the pointer to the name
483          * @param The servername to find
484          * @return A pointer to this name, gauranteed to never become invalid
485          */
486         const char* FindServerNamePtr(const std::string &servername);
487
488         /** Returns true if we've seen the given server name before
489          * @param The servername to find
490          * @return True if we've seen this server name before
491          */
492         bool FindServerName(const std::string &servername);
493
494         /** Gets the GECOS (description) field of the given server.
495          * If the servername is not that of the local server, the name
496          * is passed to handling modules which will attempt to determine
497          * the GECOS that bleongs to the given servername.
498          * @param servername The servername to find the description of
499          * @return The description of this server, or of the local server
500          */
501         std::string GetServerDescription(const char* servername);
502
503         /** Write text to all opers connected to this server
504          * @param text The text format string
505          * @param ... Format args
506          */
507         void WriteOpers(const char* text, ...);
508
509         /** Write text to all opers connected to this server
510          * @param text The text to send
511          */
512         void WriteOpers(const std::string &text);
513         
514         /** Find a nickname in the nick hash
515          * @param nick The nickname to find
516          * @return A pointer to the user, or NULL if the user does not exist
517          */
518         userrec* FindNick(const std::string &nick);
519
520         /** Find a nickname in the nick hash
521          * @param nick The nickname to find
522          * @return A pointer to the user, or NULL if the user does not exist
523          */
524         userrec* FindNick(const char* nick);
525
526         /** Find a channel in the channels hash
527          * @param chan The channel to find
528          * @return A pointer to the channel, or NULL if the channel does not exist
529          */
530         chanrec* FindChan(const std::string &chan);
531
532         /** Find a channel in the channels hash
533          * @param chan The channel to find
534          * @return A pointer to the channel, or NULL if the channel does not exist
535          */
536         chanrec* FindChan(const char* chan);
537
538         /** Called by the constructor to load all modules from the config file.
539          */
540         void LoadAllModules();
541
542         /** Check for a 'die' tag in the config file, and abort if found
543          * @return Depending on the configuration, this function may never return
544          */
545         void CheckDie();
546
547         /** Check we aren't running as root, and exit if we are
548          * @return Depending on the configuration, this function may never return
549          */
550         void CheckRoot();
551
552         /** Determine the right path for, and open, the logfile
553          * @param argv The argv passed to main() initially, used to calculate program path
554          * @param argc The argc passed to main() initially, used to calculate program path
555          */
556         void OpenLog(char** argv, int argc);
557
558         /** Convert a user to a pseudoclient, disconnecting the real user
559          * @param user The user to convert
560          * @param message The quit message to display when exiting the user
561          * @return True if the operation succeeded
562          */
563         bool UserToPseudo(userrec* user, const std::string &message);
564
565         /** Convert a pseudoclient to a real user, discarding the pseudoclient
566          * @param alive A live client
567          * @param zombie A pseudoclient
568          * @param message The message to display when quitting the pseudoclient
569          * @return True if the operation succeeded
570          */
571         bool PseudoToUser(userrec* alive, userrec* zombie, const std::string &message);
572
573         /** Send a server notice to all local users
574          * @param text The text format string to send
575          * @param ... The format arguments
576          */
577         void ServerNoticeAll(char* text, ...);
578
579         /** Send a server message (PRIVMSG) to all local users
580          * @param text The text format string to send
581          * @param ... The format arguments
582          */
583         void ServerPrivmsgAll(char* text, ...);
584
585         /** Send text to all users with a specific set of modes
586          * @param modes The modes to check against, without a +, e.g. 'og'
587          * @param flags one of WM_OR or WM_AND. If you specify WM_OR, any one of the 
588          * mode characters in the first parameter causes receipt of the message, and
589          * if you specify WM_OR, all the modes must be present.
590          * @param text The text format string to send
591          * @param ... The format arguments
592          */
593         void WriteMode(const char* modes, int flags, const char* text, ...);
594
595         /** Return true if a channel name is valid
596          * @param chname A channel name to verify
597          * @return True if the name is valid
598          */
599         bool IsChannel(const char *chname);
600
601         /** Rehash the local server
602          * @param status This value is unused, and required for signal handler functions
603          */
604         static void Rehash(int status);
605
606         /** Causes the server to exit immediately
607          * @param The exit code to give to the operating system
608          */
609         static void Exit(int status);
610
611         /** Return a count of users, unknown and known connections
612          * @return The number of users
613          */
614         int UserCount();
615
616         /** Return a count of fully registered connections only
617          * @return The number of registered users
618          */
619         int RegisteredUserCount();
620
621         /** Return a count of invisible (umode +i) users only
622          * @return The number of invisible users
623          */
624         int InvisibleUserCount();
625
626         /** Return a count of opered (umode +o) users only
627          * @return The number of opers
628          */
629         int OperCount();
630
631         /** Return a count of unregistered (before NICK/USER) users only
632          * @return The number of unregistered (unknown) connections
633          */
634         int UnregisteredUserCount();
635
636         /** Return a count of channels on the network
637          * @return The number of channels
638          */
639         long ChannelCount();
640
641         /** Return a count of local users on this server only
642          * @return The number of local users
643          */
644         long LocalUserCount();
645
646         /** Send an error notice to all local users, opered and unopered
647          * @param s The error string to send
648          */
649         void SendError(const char *s);
650
651         /** For use with Module::Prioritize().
652          * When the return value of this function is returned from
653          * Module::Prioritize(), this specifies that the module wishes
654          * to be ordered exactly BEFORE 'modulename'. For more information
655          * please see Module::Prioritize().
656          * @param modulename The module your module wants to be before in the call list
657          * @returns a priority ID which the core uses to relocate the module in the list
658          */
659         long PriorityBefore(const std::string &modulename);
660
661         /** For use with Module::Prioritize().
662          * When the return value of this function is returned from
663          * Module::Prioritize(), this specifies that the module wishes
664          * to be ordered exactly AFTER 'modulename'. For more information please
665          * see Module::Prioritize().
666          * @param modulename The module your module wants to be after in the call list
667          * @returns a priority ID which the core uses to relocate the module in the list
668          */
669         long PriorityAfter(const std::string &modulename);
670
671         /** Publish a 'feature'.
672          * There are two ways for a module to find another module it depends on.
673          * Either by name, using InspIRCd::FindModule, or by feature, using this
674          * function. A feature is an arbitary string which identifies something this
675          * module can do. For example, if your module provides SSL support, but other
676          * modules provide SSL support too, all the modules supporting SSL should
677          * publish an identical 'SSL' feature. This way, any module requiring use
678          * of SSL functions can just look up the 'SSL' feature using FindFeature,
679          * then use the module pointer they are given.
680          * @param FeatureName The case sensitive feature name to make available
681          * @param Mod a pointer to your module class
682          * @returns True on success, false if the feature is already published by
683          * another module.
684          */
685         bool PublishFeature(const std::string &FeatureName, Module* Mod);
686
687         /** Unpublish a 'feature'.
688          * When your module exits, it must call this method for every feature it
689          * is providing so that the feature table is cleaned up.
690          * @param FeatureName the feature to remove
691          */
692         bool UnpublishFeature(const std::string &FeatureName);
693
694         /** Find a 'feature'.
695          * There are two ways for a module to find another module it depends on.
696          * Either by name, using InspIRCd::FindModule, or by feature, using the
697          * InspIRCd::PublishFeature method. A feature is an arbitary string which
698          * identifies something this module can do. For example, if your module
699          * provides SSL support, but other modules provide SSL support too, all
700          * the modules supporting SSL should publish an identical 'SSL' feature.
701          * To find a module capable of providing the feature you want, simply
702          * call this method with the feature name you are looking for.
703          * @param FeatureName The feature name you wish to obtain the module for
704          * @returns A pointer to a valid module class on success, NULL on failure.
705          */
706         Module* FindFeature(const std::string &FeatureName);
707
708         /** Given a pointer to a Module, return its filename
709          * @param m The module pointer to identify
710          * @return The module name or an empty string
711          */
712         const std::string& GetModuleName(Module* m);
713
714         /** Return true if a nickname is valid
715          * @param n A nickname to verify
716          * @return True if the nick is valid
717          */
718         bool IsNick(const char* n);
719
720         /** Return true if an ident is valid
721          * @param An ident to verify
722          * @return True if the ident is valid
723          */
724         bool IsIdent(const char* n);
725
726         /** Find a username by their file descriptor.
727          * It is preferred to use this over directly accessing the fd_ref_table array.
728          * @param socket The file descriptor of a user
729          * @return A pointer to the user if the user exists locally on this descriptor
730          */
731         userrec* FindDescriptor(int socket);
732
733         /** Add a new mode to this server's mode parser
734          * @param mh The modehandler to add
735          * @param modechar The mode character this modehandler handles
736          * @return True if the mode handler was added
737          */
738         bool AddMode(ModeHandler* mh, const unsigned char modechar);
739
740         /** Add a new mode watcher to this server's mode parser
741          * @param mw The modewatcher to add
742          * @return True if the modewatcher was added
743          */
744         bool AddModeWatcher(ModeWatcher* mw);
745
746         /** Delete a mode watcher from this server's mode parser
747          * @param mw The modewatcher to delete
748          * @return True if the modewatcher was deleted
749          */
750         bool DelModeWatcher(ModeWatcher* mw);
751
752         /** Add a dns Resolver class to this server's active set
753          * @param r The resolver to add
754          * @return True if the resolver was added
755          */
756         bool AddResolver(Resolver* r);
757
758         /** Add a command to this server's command parser
759          * @param f A command_t command handler object to add
760          * @throw ModuleException Will throw ModuleExcption if the command already exists
761          */
762         void AddCommand(command_t *f);
763
764         /** Send a modechange.
765          * The parameters provided are identical to that sent to the
766          * handler for class cmd_mode.
767          * @param parameters The mode parameters
768          * @param pcnt The number of items you have given in the first parameter
769          * @param user The user to send error messages to
770          */
771         void SendMode(const char **parameters, int pcnt, userrec *user);
772
773         /** Match two strings using pattern matching.
774          * This operates identically to the global function match(),
775          * except for that it takes std::string arguments rather than
776          * const char* ones.
777          * @param sliteral The literal string to match against
778          * @param spattern The pattern to match against. CIDR and globs are supported.
779          */
780         bool MatchText(const std::string &sliteral, const std::string &spattern);
781
782         /** Call the handler for a given command.
783          * @param commandname The command whos handler you wish to call
784          * @param parameters The mode parameters
785          * @param pcnt The number of items you have given in the first parameter
786          * @param user The user to execute the command as
787          * @return True if the command handler was called successfully
788          */
789         bool CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user);
790
791         /** Return true if the command is a module-implemented command and the given parameters are valid for it
792          * @param parameters The mode parameters
793          * @param pcnt The number of items you have given in the first parameter
794          * @param user The user to test-execute the command as
795          * @return True if the command handler is a module command, and there are enough parameters and the user has permission to the command
796          */
797         bool IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user);
798
799         /** Add a gline and apply it
800          * @param duration How long the line should last
801          * @param source Who set the line
802          * @param reason The reason for the line
803          * @param hostmask The hostmask to set the line against
804          */
805         void AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
806
807         /** Add a qline and apply it
808          * @param duration How long the line should last
809          * @param source Who set the line
810          * @param reason The reason for the line
811          * @param nickname The nickmask to set the line against
812          */
813         void AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname);
814
815         /** Add a zline and apply it
816          * @param duration How long the line should last
817          * @param source Who set the line
818          * @param reason The reason for the line
819          * @param ipaddr The ip-mask to set the line against
820          */
821         void AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr);
822
823         /** Add a kline and apply it
824          * @param duration How long the line should last
825          * @param source Who set the line
826          * @param reason The reason for the line
827          * @param hostmask The hostmask to set the line against
828          */
829         void AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
830
831         /** Add an eline
832          * @param duration How long the line should last
833          * @param source Who set the line
834          * @param reason The reason for the line
835          * @param hostmask The hostmask to set the line against
836          */
837         void AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask);
838
839         /** Delete a gline
840          * @param hostmask The gline to delete
841          * @return True if the item was removed
842          */
843         bool DelGLine(const std::string &hostmask);
844
845         /** Delete a qline
846          * @param nickname The qline to delete
847          * @return True if the item was removed
848          */
849         bool DelQLine(const std::string &nickname);
850
851         /** Delete a zline
852          * @param ipaddr The zline to delete
853          * @return True if the item was removed
854          */
855         bool DelZLine(const std::string &ipaddr);
856
857         /** Delete a kline
858          * @param hostmask The kline to delete
859          * @return True if the item was removed
860          */
861         bool DelKLine(const std::string &hostmask);
862
863         /** Delete an eline
864          * @param hostmask The kline to delete
865          * @return True if the item was removed
866          */
867         bool DelELine(const std::string &hostmask);
868
869         /** Return true if the given parameter is a valid nick!user\@host mask
870          * @param mask A nick!user\@host masak to match against 
871          * @return True i the mask is valid
872          */
873         bool IsValidMask(const std::string &mask);
874
875         /** Rehash the local server
876          */
877         void RehashServer();
878
879         /** Return the channel whos index number matches that provided
880          * @param The index number of the channel to fetch
881          * @return A channel record, or NUll if index < 0 or index >= InspIRCd::ChannelCount()
882          */
883         chanrec* GetChannelIndex(long index);
884
885         /** Dump text to a user target, splitting it appropriately to fit
886          * @param User the user to dump the text to
887          * @param LinePrefix text to prefix each complete line with
888          * @param TextStream the text to send to the user
889          */
890         void DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream);
891
892         /** Check if the given nickmask matches too many users, send errors to the given user
893          * @param nick A nickmask to match against
894          * @param user A user to send error text to
895          * @return True if the nick matches too many users
896          */
897         bool NickMatchesEveryone(const std::string &nick, userrec* user);
898
899         /** Check if the given IP mask matches too many users, send errors to the given user
900          * @param ip An ipmask to match against
901          * @param user A user to send error text to
902          * @return True if the ip matches too many users
903          */
904         bool IPMatchesEveryone(const std::string &ip, userrec* user);
905
906         /** Check if the given hostmask matches too many users, send errors to the given user
907          * @param mask A hostmask to match against
908          * @param user A user to send error text to
909          * @return True if the host matches too many users
910          */
911         bool HostMatchesEveryone(const std::string &mask, userrec* user);
912
913         /** Calculate a duration in seconds from a string in the form 1y2w3d4h6m5s
914          * @param str A string containing a time in the form 1y2w3d4h6m5s
915          * (one year, two weeks, three days, four hours, six minutes and five seconds)
916          * @return The total number of seconds
917          */
918         long Duration(const char* str);
919
920         /** Attempt to compare an oper password to a string from the config file.
921          * This will be passed to handling modules which will compare the data
922          * against possible hashed equivalents in the input string.
923          * @param data The data from the config file
924          * @param input The data input by the oper
925          * @return 0 if the strings match, 1 or -1 if they do not
926          */
927         int OperPassCompare(const char* data,const char* input);
928
929         /** Check if a given server is a uline.
930          * An empty string returns true, this is by design.
931          * @param server The server to check for uline status
932          * @return True if the server is a uline OR the string is empty
933          */
934         bool ULine(const char* server);
935
936         /** Returns the subversion revision ID of this ircd
937          * @return The revision ID or an empty string
938          */
939         std::string GetRevision();
940
941         /** Returns the full version string of this ircd
942          * @return The version string
943          */
944         std::string GetVersionString();
945
946         /** Attempt to write the process id to a given file
947          * @param filename The PID file to attempt to write to
948          * @return This function may bail if the file cannot be written
949          */
950         void WritePID(const std::string &filename);
951
952         /** Returns text describing the last module error
953          * @return The last error message to occur
954          */
955         char* ModuleError();
956
957         /** Load a given module file
958          * @param filename The file to load
959          * @return True if the module was found and loaded
960          */
961         bool LoadModule(const char* filename);
962
963         /** Unload a given module file
964          * @param filename The file to unload
965          * @return True if the module was unloaded
966          */
967         bool UnloadModule(const char* filename);
968
969         /** This constructor initialises all the subsystems and reads the config file.
970          * @param argc The argument count passed to main()
971          * @param argv The argument list passed to main()
972          * @throw <anything> If anything is thrown from here and makes it to
973          * you, you should probably just give up and go home. Yes, really.
974          * It's that bad. Higher level classes should catch any non-fatal exceptions.
975          */
976         InspIRCd(int argc, char** argv);
977
978         /** Do one iteration of the mainloop
979          * @param process_module_sockets True if module sockets are to be processed
980          * this time around the event loop. The is the default.
981          */
982         void DoOneIteration(bool process_module_sockets = true);
983
984         /** Output a log message to the ircd.log file
985          * The text will only be output if the current loglevel
986          * is less than or equal to the level you provide
987          * @param level A log level from the DebugLevel enum
988          * @param text Format string of to write to the log
989          * @param ... Format arguments of text to write to the log
990          */
991         void Log(int level, const char* text, ...);
992
993         /** Output a log message to the ircd.log file
994          * The text will only be output if the current loglevel
995          * is less than or equal to the level you provide
996          * @param level A log level from the DebugLevel enum
997          * @param text Text to write to the log
998          */
999         void Log(int level, const std::string &text);
1000
1001         void SendWhoisLine(userrec* user, int numeric, const std::string &text);
1002
1003         void SendWhoisLine(userrec* user, int numeric, const char* format, ...);
1004
1005         /** Begin execution of the server.
1006          * NOTE: this function NEVER returns. Internally,
1007          * after performing some initialisation routines,
1008          * it will repeatedly call DoOneIteration in a loop.
1009          * @return The return value for this function is undefined.
1010          */
1011         int Run();
1012 };
1013
1014 #endif
1015