]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules.h
Added more code for custom channel/user modes via modules
[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          * If the mode is not a channel mode, chanrec* chan is null, and should not be read from or written to.
149          */
150         virtual bool OnExtendedMode(userrec* user, chanrec* chan, char modechar, int type, bool mode_on, string_list &params);
151          
152 };
153
154
155 /** Allows server output and query functions
156  * This class contains methods which allow a module to query the state of the irc server, and produce
157  * output to users and other servers. All modules should instantiate at least one copy of this class,
158  * and use its member functions to perform their tasks.
159  */
160 class Server : public classbase
161 {
162  public:
163         /** Default constructor.
164          * Creates a Server object.
165          */
166         Server();
167         /** Default destructor.
168          * Destroys a Server object.
169          */
170         virtual ~Server();
171
172         /** Sends text to all opers.
173          * This method sends a server notice to all opers with the usermode +s.
174          */
175         virtual void SendOpers(string s);
176         /** Writes a log string.
177          * This method writes a line of text to the log. If the level given is lower than the
178          * level given in the configuration, this command has no effect.
179          */
180         virtual void Log(int level, string s);
181         /** Sends a line of text down a TCP/IP socket.
182          * This method writes a line of text to an established socket, cutting it to 510 characters
183          * plus a carriage return and linefeed if required.
184          */
185         virtual void Send(int Socket, string s);
186         /** Sends text from the server to a socket.
187          * This method writes a line of text to an established socket, with the servername prepended
188          * as used by numerics (see RFC 1459)
189          */
190         virtual void SendServ(int Socket, string s);
191         /** Sends text from a user to a socket.
192          * This method writes a line of text to an established socket, with the given user's nick/ident
193          * /host combination prepended, as used in PRIVSG etc commands (see RFC 1459)
194          */
195         virtual void SendFrom(int Socket, userrec* User, string s);
196         /** Sends text from a user to another user.
197          * This method writes a line of text to a user, with a user's nick/ident
198          * /host combination prepended, as used in PRIVMSG etc commands (see RFC 1459)
199          */
200         virtual void SendTo(userrec* Source, userrec* Dest, string s);
201         /** Sends text from a user to a channel (mulicast).
202          * This method writes a line of text to a channel, with the given user's nick/ident
203          * /host combination prepended, as used in PRIVMSG etc commands (see RFC 1459). If the
204          * IncludeSender flag is set, then the text is also sent back to the user from which
205          * it originated, as seen in MODE (see RFC 1459).
206          */
207         virtual void SendChannel(userrec* User, chanrec* Channel, string s,bool IncludeSender);
208         /** Returns true if two users share a common channel.
209          * This method is used internally by the NICK and QUIT commands, and the Server::SendCommon
210          * method.
211          */
212         virtual bool CommonChannels(userrec* u1, userrec* u2);
213         /** Sends text from a user to one or more channels (mulicast).
214          * This method writes a line of text to all users which share a common channel with a given     
215          * user, with the user's nick/ident/host combination prepended, as used in PRIVMSG etc
216          * commands (see RFC 1459). If the IncludeSender flag is set, then the text is also sent
217          * back to the user from which it originated, as seen in NICK (see RFC 1459). Otherwise, it
218          * is only sent to the other recipients, as seen in QUIT.
219          */
220         virtual void SendCommon(userrec* User, string text,bool IncludeSender);
221         /** Sends a WALLOPS message.
222          * This method writes a WALLOPS message to all users with the +w flag, originating from the
223          * specified user.
224          */
225         virtual void SendWallops(userrec* User, string text);
226
227         /** Returns true if a nick is valid.
228          * Nicks for unregistered connections will return false.
229          */
230         virtual bool IsNick(string nick);
231         /** Attempts to look up a nick and return a pointer to it.
232          * This function will return NULL if the nick does not exist.
233          */
234         virtual userrec* FindNick(string nick);
235         /** Attempts to look up a channel and return a pointer to it.
236          * This function will return NULL if the channel does not exist.
237          */
238         virtual chanrec* FindChannel(string channel);
239         /** Attempts to look up a user's privilages on a channel.
240          * This function will return a string containing either @, %, +, or an empty string,
241          * representing the user's privilages upon the channel you specify.
242          */
243         virtual string ChanMode(userrec* User, chanrec* Chan);
244         /** Returns the server name of the server where the module is loaded.
245          */
246         virtual string GetServerName();
247         /** Returns the network name, global to all linked servers.
248          */
249         virtual string GetNetworkName();
250         /** Returns the information of the server as returned by the /ADMIN command.
251          * See the Admin class for further information of the return value. The members
252          * Admin::Nick, Admin::Email and Admin::Name contain the information for the
253          * server where the module is loaded.
254          */
255         virtual Admin GetAdmin();
256         /** Adds an extended mode letter which is parsed by a module
257          * This allows modules to add extra mode letters, e.g. +x for hostcloak.
258          * the "type" parameter is either MT_CHANNEL, MT_CLIENT, or MT_SERVER, to
259          * indicate wether the mode is a channel mode, a client mode, or a server mode.
260          * default_on is true if the mode is to be applied to default connections.
261          * params_when_on is the number of modes to expect when the mode is turned on
262          * (for type MT_CHANNEL only), e.g. with mode +b, this would have a value of 1.
263          * the params_when_off value has a similar value to params_when_on, except it indicates
264          * the number of parameters to expect when the mode is disabled. Modes which act in a similar
265          * way to channel mode +l (e.g. require a parameter to enable, but not to disable) should
266          * use this parameter. The function returns false if the mode is unavailable, and will not
267          * attempt to allocate another character, as this will confuse users. This also means that
268          * as only one module can claim a specific mode character, the core does not need to keep track
269          * of which modules own which modes, which speeds up operation of the server. In this version,
270          * a mode can have at most one parameter, attempting to use more parameters will have undefined
271          * effects.
272          */
273         virtual bool AddExtendedMode(char modechar, int type, bool default_on, int params_when_on, int params_when_off);
274          
275 };
276
277 /** Allows reading of values from configuration files
278  * This class allows a module to read from either the main configuration file (inspircd.conf) or from
279  * a module-specified configuration file. It may either be instantiated with one parameter or none.
280  * Constructing the class using one parameter allows you to specify a path to your own configuration
281  * file, otherwise, inspircd.conf is read.
282  */
283 class ConfigReader : public classbase
284 {
285   protected:
286         /** The filename of the configuration file, as set by the constructor.
287          */
288         string fname;
289   public:
290         /** Default constructor.
291          * This constructor initialises the ConfigReader class to read the inspircd.conf file
292          * as specified when running ./configure.
293          */
294         ConfigReader();                 // default constructor reads ircd.conf
295         /** Overloaded constructor.
296          * This constructor initialises the ConfigReader class to read a user-specified config file
297          */
298         ConfigReader(string filename);  // read a module-specific config
299         /** Default destructor.
300          * This method destroys the ConfigReader class.
301          */
302         ~ConfigReader();
303         /** Retrieves a value from the config file.
304          * This method retrieves a value from the config file. Where multiple copies of the tag
305          * exist in the config file, index indicates which of the values to retrieve.
306          */
307         string ReadValue(string tag, string name, int index);
308         /** Counts the number of times a given tag appears in the config file.
309          * This method counts the number of times a tag appears in a config file, for use where
310          * there are several tags of the same kind, e.g. with opers and connect types. It can be
311          * used with the index value of ConfigReader::ReadValue to loop through all copies of a
312          * multiple instance tag.
313          */
314         int Enumerate(string tag);
315         /** Returns true if a config file is valid.
316          * This method is unimplemented and will always return true.
317          */
318         bool Verify();
319 };
320
321
322
323 /** Caches a text file into memory and can be used to retrieve lines from it.
324  * This class contains methods for read-only manipulation of a text file in memory.
325  * Either use the constructor type with one parameter to load a file into memory
326  * at construction, or use the LoadFile method to load a file.
327  */
328 class FileReader : public classbase
329 {
330  file_cache fc;
331  public:
332          /** Default constructor.
333           * This method does not load any file into memory, you must use the LoadFile method
334           * after constructing the class this way.
335           */
336          FileReader();
337          /** Secondary constructor.
338           * This method initialises the class with a file loaded into it ready for GetLine and
339           * and other methods to be called. If the file could not be loaded, FileReader::FileSize
340           * returns 0.
341           */
342          FileReader(string filename);
343          /** Default destructor.
344           * This deletes the memory allocated to the file.
345           */
346          ~FileReader();
347          /** Used to load a file.
348           * This method loads a file into the class ready for GetLine and
349           * and other methods to be called. If the file could not be loaded, FileReader::FileSize
350           * returns 0.
351           */
352          void LoadFile(string filename);
353          /** Retrieve one line from the file.
354           * This method retrieves one line from the text file. If an empty non-NULL string is returned,
355           * the index was out of bounds, or the line had no data on it.
356           */
357          string GetLine(int x);
358          /** Returns the size of the file in lines.
359           * This method returns the number of lines in the read file. If it is 0, no lines have been
360           * read into memory, either because the file is empty or it does not exist, or cannot be
361           * opened due to permission problems.
362           */
363          int FileSize();
364 };
365
366
367 /** Instantiates classes inherited from Module
368  * This class creates a class inherited from type Module, using new. This is to allow for modules
369  * to create many different variants of Module, dependent on architecture, configuration, etc.
370  * In most cases, the simple class shown in the example module m_foobar.so will suffice for most
371  * modules.
372  */
373 class ModuleFactory : public classbase
374 {
375  public:
376         ModuleFactory() { }
377         virtual ~ModuleFactory() { }
378         /** Creates a new module.
379          * Your inherited class of ModuleFactory must return a pointer to your Module class
380          * using this method.
381          */
382         virtual Module * CreateModule() = 0;
383 };
384
385
386 typedef DLLFactory<ModuleFactory> ircd_module;
387
388 #endif