]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules.h
b26554d563e698621ebb3a01319a8f831ebdcb9d
[user/henk/code/inspircd.git] / include / modules.h
1 /*
2
3
4
5 */
6
7
8 #ifndef __PLUGIN_H
9 #define __PLUGIN_H
10
11 #define DEBUG 10
12 #define VERBOSE 20
13 #define DEFAULT 30
14 #define SPARSE 40
15 #define NONE 50
16
17 #define MT_CHANNEL 1
18 #define MT_CLIENT 2
19 #define MT_SERVER 3
20
21 #include "dynamic.h"
22 #include "base.h"
23 #include "ctables.h"
24 #include <string>
25 #include <deque>
26
27 /** Low level definition of a FileReader classes file cache area
28  */
29 typedef std::deque<std::string> file_cache;
30 typedef file_cache string_list;
31
32 // This #define allows us to call a method in all
33 // loaded modules in a readable simple way, e.g.:
34 // 'FOREACH_MOD OnConnect(user);'
35
36 #define FOREACH_MOD for (int i = 0; i <= MODCOUNT; i++) modules[i]->
37
38 extern void createcommand(char* cmd, handlerfunc f, char flags, int minparams);
39
40 // class Version holds the version information of a Module, returned
41 // by Module::GetVersion (thanks RD)
42
43 /** Holds a module's Version information
44  *  The four members (set by the constructor only) indicate details as to the version number
45  *  of a module. A class of type Version is returned by the GetVersion method of the Module class.
46  */
47 class Version : public classbase
48 {
49  public:
50          const int Major, Minor, Revision, Build;
51          Version(int major, int minor, int revision, int build);
52 };
53
54 /** Holds /ADMIN data
55  *  This class contains the admin details of the local server. It is constructed by class Server,
56  *  and has three read-only values, Name, Email and Nick that contain the specified values for the
57  *  server where the module is running.
58  */
59 class Admin : public classbase
60 {
61  public:
62          const std::string Name, Email, Nick;
63          Admin(std::string name, std::string email, std::string nick);
64 };
65
66 /** Base class for all InspIRCd modules
67  *  This class is the base class for InspIRCd modules. All modules must inherit from this class,
68  *  its methods will be called when irc server events occur. class inherited from module must be
69  *  instantiated by the ModuleFactory class (see relevent section) for the plugin to be initialised.
70  */
71 class Module : public classbase
72 {
73  public:
74
75         /** Default constructor
76          * creates a module class
77          */
78         Module();
79
80         /** Default destructor
81          * destroys a module class
82          */
83         virtual ~Module();
84
85         /** Returns the version number of a Module.
86          * The method should return a Version object with its version information assigned via
87          * Version::Version
88          */
89         virtual Version GetVersion();
90
91         /** Called when a user connects.
92          * The details of the connecting user are available to you in the parameter userrec *user
93          */
94         virtual void OnUserConnect(userrec* user);
95
96         /** Called when a user quits.
97          * The details of the exiting user are available to you in the parameter userrec *user
98          */
99         virtual void OnUserQuit(userrec* user);
100
101         /** Called when a user joins a channel.
102          * The details of the joining user are available to you in the parameter userrec *user,
103          * and the details of the channel they have joined is available in the variable chanrec *channel
104          */
105         virtual void OnUserJoin(userrec* user, chanrec* channel);
106
107         /** Called when a user parts a channel.
108          * The details of the leaving user are available to you in the parameter userrec *user,
109          * and the details of the channel they have left is available in the variable chanrec *channel
110          */
111         virtual void OnUserPart(userrec* user, chanrec* channel);
112
113         /** Called before a packet is transmitted across the irc network between two irc servers.
114          * The packet is represented as a char*, as it should be regarded as a buffer, and not a string.
115          * This allows you to easily represent it in the correct ways to implement encryption, compression,
116          * digital signatures and anything else you may want to add. This should be regarded as a pre-processor
117          * and will be called before ANY other operations within the ircd core program.
118          */
119         virtual void Module::OnPacketTransmit(char *p);
120
121         /** Called after a packet is received from another irc server.
122          * The packet is represented as a char*, as it should be regarded as a buffer, and not a string.
123          * This allows you to easily represent it in the correct ways to implement encryption, compression,
124          * digital signatures and anything else you may want to add. This should be regarded as a pre-processor
125          * and will be called immediately after the packet is received but before any other operations with the
126          * core of the ircd.
127          */
128         virtual void Module::OnPacketReceive(char *p);
129
130         /** Called on rehash.
131          * This method is called prior to a /REHASH or when a SIGHUP is received from the operating
132          * system. You should use it to reload any files so that your module keeps in step with the
133          * rest of the application.
134          */
135         virtual void OnRehash();
136
137         /** Called when a raw command is transmitted or received.
138          * This method is the lowest level of handler available to a module. It will be called with raw
139          * data which is passing through a connected socket. If you wish, you may munge this data by changing
140          * the string parameter "raw". If you do this, after your function exits it will immediately be
141          * cut down to 510 characters plus a carriage return and linefeed.
142          */
143         virtual void OnServerRaw(std::string &raw, bool inbound);
144
145         /** Called whenever an extended mode is to be processed.
146          * The type parameter is MT_SERVER, MT_CLIENT or MT_CHANNEL, dependent on where the mode is being
147          * changed. mode_on is set when the mode is being set, in which case params contains a list of
148          * parameters for the mode as strings. If mode_on is false, the mode is being removed, and parameters
149          * may contain the parameters for the mode, dependent on wether they were defined when a mode handler
150          * was set up with Server::AddExtendedMode
151          * If the mode is not a channel mode, chanrec* chan is null, and should not be read from or written to.
152          */
153         virtual bool OnExtendedMode(userrec* user, chanrec* chan, char modechar, int type, bool mode_on, string_list &params);
154          
155 };
156
157
158 /** Allows server output and query functions
159  * This class contains methods which allow a module to query the state of the irc server, and produce
160  * output to users and other servers. All modules should instantiate at least one copy of this class,
161  * and use its member functions to perform their tasks.
162  */
163 class Server : public classbase
164 {
165  public:
166         /** Default constructor.
167          * Creates a Server object.
168          */
169         Server();
170         /** Default destructor.
171          * Destroys a Server object.
172          */
173         virtual ~Server();
174
175         /** Sends text to all opers.
176          * This method sends a server notice to all opers with the usermode +s.
177          */
178         virtual void SendOpers(std::string s);
179         /** Writes a log string.
180          * This method writes a line of text to the log. If the level given is lower than the
181          * level given in the configuration, this command has no effect.
182          */
183         virtual void Log(int level, std::string s);
184         /** Sends a line of text down a TCP/IP socket.
185          * This method writes a line of text to an established socket, cutting it to 510 characters
186          * plus a carriage return and linefeed if required.
187          */
188         virtual void Send(int Socket, std::string s);
189         /** Sends text from the server to a socket.
190          * This method writes a line of text to an established socket, with the servername prepended
191          * as used by numerics (see RFC 1459)
192          */
193         virtual void SendServ(int Socket, std::string s);
194         /** Sends text from a user to a socket.
195          * This method writes a line of text to an established socket, with the given user's nick/ident
196          * /host combination prepended, as used in PRIVSG etc commands (see RFC 1459)
197          */
198         virtual void SendFrom(int Socket, userrec* User, std::string s);
199         /** Sends text from a user to another user.
200          * This method writes a line of text to a user, with a user's nick/ident
201          * /host combination prepended, as used in PRIVMSG etc commands (see RFC 1459)
202          */
203         virtual void SendTo(userrec* Source, userrec* Dest, std::string s);
204         /** Sends text from a user to a channel (mulicast).
205          * This method writes a line of text to a channel, with the given user's nick/ident
206          * /host combination prepended, as used in PRIVMSG etc commands (see RFC 1459). If the
207          * IncludeSender flag is set, then the text is also sent back to the user from which
208          * it originated, as seen in MODE (see RFC 1459).
209          */
210         virtual void SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender);
211         /** Returns true if two users share a common channel.
212          * This method is used internally by the NICK and QUIT commands, and the Server::SendCommon
213          * method.
214          */
215         virtual bool CommonChannels(userrec* u1, userrec* u2);
216         /** Sends text from a user to one or more channels (mulicast).
217          * This method writes a line of text to all users which share a common channel with a given     
218          * user, with the user's nick/ident/host combination prepended, as used in PRIVMSG etc
219          * commands (see RFC 1459). If the IncludeSender flag is set, then the text is also sent
220          * back to the user from which it originated, as seen in NICK (see RFC 1459). Otherwise, it
221          * is only sent to the other recipients, as seen in QUIT.
222          */
223         virtual void SendCommon(userrec* User, std::string text,bool IncludeSender);
224         /** Sends a WALLOPS message.
225          * This method writes a WALLOPS message to all users with the +w flag, originating from the
226          * specified user.
227          */
228         virtual void SendWallops(userrec* User, std::string text);
229
230         /** Returns true if a nick is valid.
231          * Nicks for unregistered connections will return false.
232          */
233         virtual bool IsNick(std::string nick);
234         /** Attempts to look up a nick and return a pointer to it.
235          * This function will return NULL if the nick does not exist.
236          */
237         virtual userrec* FindNick(std::string nick);
238         /** Attempts to look up a channel and return a pointer to it.
239          * This function will return NULL if the channel does not exist.
240          */
241         virtual chanrec* FindChannel(std::string channel);
242         /** Attempts to look up a user's privilages on a channel.
243          * This function will return a string containing either @, %, +, or an empty string,
244          * representing the user's privilages upon the channel you specify.
245          */
246         virtual std::string ChanMode(userrec* User, chanrec* Chan);
247         /** Returns the server name of the server where the module is loaded.
248          */
249         virtual std::string GetServerName();
250         /** Returns the network name, global to all linked servers.
251          */
252         virtual std::string GetNetworkName();
253         /** Returns the information of the server as returned by the /ADMIN command.
254          * See the Admin class for further information of the return value. The members
255          * Admin::Nick, Admin::Email and Admin::Name contain the information for the
256          * server where the module is loaded.
257          */
258         virtual Admin GetAdmin();
259         /** Adds an extended mode letter which is parsed by a module
260          * This allows modules to add extra mode letters, e.g. +x for hostcloak.
261          * the "type" parameter is either MT_CHANNEL, MT_CLIENT, or MT_SERVER, to
262          * indicate wether the mode is a channel mode, a client mode, or a server mode.
263          * default_on is true if the mode is to be applied to default connections.
264          * params_when_on is the number of modes to expect when the mode is turned on
265          * (for type MT_CHANNEL only), e.g. with mode +b, this would have a value of 1.
266          * the params_when_off value has a similar value to params_when_on, except it indicates
267          * the number of parameters to expect when the mode is disabled. Modes which act in a similar
268          * way to channel mode +l (e.g. require a parameter to enable, but not to disable) should
269          * use this parameter. The function returns false if the mode is unavailable, and will not
270          * attempt to allocate another character, as this will confuse users. This also means that
271          * as only one module can claim a specific mode character, the core does not need to keep track
272          * of which modules own which modes, which speeds up operation of the server. In this version,
273          * a mode can have at most one parameter, attempting to use more parameters will have undefined
274          * effects.
275          */
276         virtual bool AddExtendedMode(char modechar, int type, bool default_on, int params_when_on, int params_when_off);
277
278         /** Adds a command to the command table.
279          * This allows modules to add extra commands into the command table. You must place a function within your
280          * module which is is of type handlerfunc:
281          * 
282          * typedef void (handlerfunc) (char**, int, userrec*);
283          * ...
284          * void handle_kill(char **parameters, int pcnt, userrec *user)
285          *
286          * When the command is typed, the parameters will be placed into the parameters array (similar to argv) and
287          * the parameter count will be placed into pcnt (similar to argv). There will never be any less parameters
288          * than the 'minparams' value you specified when creating the command. The *user parameter is the class of
289          * the user which caused the command to trigger, who will always have the flag you specified in 'flags' when
290          * creating the initial command. For example to create an oper only command create the commands with flags='o'.
291          */
292         virtual void AddCommand(char* cmd, handlerfunc f, char flags, int minparams);
293          
294 };
295
296 /** Allows reading of values from configuration files
297  * This class allows a module to read from either the main configuration file (inspircd.conf) or from
298  * a module-specified configuration file. It may either be instantiated with one parameter or none.
299  * Constructing the class using one parameter allows you to specify a path to your own configuration
300  * file, otherwise, inspircd.conf is read.
301  */
302 class ConfigReader : public classbase
303 {
304   protected:
305         /** The filename of the configuration file, as set by the constructor.
306          */
307         std::string fname;
308   public:
309         /** Default constructor.
310          * This constructor initialises the ConfigReader class to read the inspircd.conf file
311          * as specified when running ./configure.
312          */
313         ConfigReader();                 // default constructor reads ircd.conf
314         /** Overloaded constructor.
315          * This constructor initialises the ConfigReader class to read a user-specified config file
316          */
317         ConfigReader(std::string filename);     // read a module-specific config
318         /** Default destructor.
319          * This method destroys the ConfigReader class.
320          */
321         ~ConfigReader();
322         /** Retrieves a value from the config file.
323          * This method retrieves a value from the config file. Where multiple copies of the tag
324          * exist in the config file, index indicates which of the values to retrieve.
325          */
326         std::string ReadValue(std::string tag, std::string name, int index);
327         /** Counts the number of times a given tag appears in the config file.
328          * This method counts the number of times a tag appears in a config file, for use where
329          * there are several tags of the same kind, e.g. with opers and connect types. It can be
330          * used with the index value of ConfigReader::ReadValue to loop through all copies of a
331          * multiple instance tag.
332          */
333         int Enumerate(std::string tag);
334         /** Returns true if a config file is valid.
335          * This method is unimplemented and will always return true.
336          */
337         bool Verify();
338 };
339
340
341
342 /** Caches a text file into memory and can be used to retrieve lines from it.
343  * This class contains methods for read-only manipulation of a text file in memory.
344  * Either use the constructor type with one parameter to load a file into memory
345  * at construction, or use the LoadFile method to load a file.
346  */
347 class FileReader : public classbase
348 {
349  file_cache fc;
350  public:
351          /** Default constructor.
352           * This method does not load any file into memory, you must use the LoadFile method
353           * after constructing the class this way.
354           */
355          FileReader();
356          /** Secondary constructor.
357           * This method initialises the class with a file loaded into it ready for GetLine and
358           * and other methods to be called. If the file could not be loaded, FileReader::FileSize
359           * returns 0.
360           */
361          FileReader(std::string filename);
362          /** Default destructor.
363           * This deletes the memory allocated to the file.
364           */
365          ~FileReader();
366          /** Used to load a file.
367           * This method loads a file into the class ready for GetLine and
368           * and other methods to be called. If the file could not be loaded, FileReader::FileSize
369           * returns 0.
370           */
371          void LoadFile(std::string filename);
372          /** Retrieve one line from the file.
373           * This method retrieves one line from the text file. If an empty non-NULL string is returned,
374           * the index was out of bounds, or the line had no data on it.
375           */
376          bool Exists();
377          std::string GetLine(int x);
378          /** Returns the size of the file in lines.
379           * This method returns the number of lines in the read file. If it is 0, no lines have been
380           * read into memory, either because the file is empty or it does not exist, or cannot be
381           * opened due to permission problems.
382           */
383          int FileSize();
384 };
385
386
387 /** Instantiates classes inherited from Module
388  * This class creates a class inherited from type Module, using new. This is to allow for modules
389  * to create many different variants of Module, dependent on architecture, configuration, etc.
390  * In most cases, the simple class shown in the example module m_foobar.so will suffice for most
391  * modules.
392  */
393 class ModuleFactory : public classbase
394 {
395  public:
396         ModuleFactory() { }
397         virtual ~ModuleFactory() { }
398         /** Creates a new module.
399          * Your inherited class of ModuleFactory must return a pointer to your Module class
400          * using this method.
401          */
402         virtual Module * CreateModule() = 0;
403 };
404
405
406 typedef DLLFactory<ModuleFactory> ircd_module;
407
408 #endif