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