]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/mode.h
Comments
[user/henk/code/inspircd.git] / include / mode.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 __MODE_H
18 #define __MODE_H
19
20 /* include the common header files */
21 #include <typeinfo>
22 #include <iostream>
23 #include <string>
24 #include <deque>
25 #include <sstream>
26 #include <vector>
27 #include "users.h"
28 #include "channels.h"
29 #include "ctables.h"
30
31 class InspIRCd;
32
33 /**
34  * Holds the values for different type of modes
35  * that can exist, USER or CHANNEL type.
36  */
37 enum ModeType {
38         MODETYPE_USER = 0,
39         MODETYPE_CHANNEL = 1
40 };
41
42 /**
43  * Holds mode actions - modes can be allowed or denied.
44  */
45 enum ModeAction {
46         MODEACTION_DENY = 0, /* Drop the mode change, AND a parameter if its a parameterized mode */
47         MODEACTION_ALLOW = 1 /* Allow the mode */
48 };
49
50 /**
51  * Used to mask off the mode types in the mode handler
52  * array. Used in a simple two instruction hashing function
53  * "(modeletter - 65) OR mask"
54  */
55 enum ModeMasks {
56         MASK_USER = 128,        /* A user mode */
57         MASK_CHANNEL = 0        /* A channel mode */
58 };
59
60 /**
61  * Used by ModeHandler::ModeSet() to return the state of a mode upon a channel or user.
62  * The pair contains an activity flag, true if the mode is set with the given parameter,
63  * and the parameter of the mode (or the parameter provided) in the std::string.
64  */
65 typedef std::pair<bool,std::string> ModePair;
66
67 /** Each mode is implemented by ONE ModeHandler class.
68  * You must derive ModeHandler and add the child class to
69  * the list of modes handled by the ircd, using
70  * ModeParser::AddMode. When the mode you implement is
71  * set by a user, the virtual function OnModeChange is
72  * called. If you specify a value greater than 0 for
73  * parameters_on or parameters_off, then when the mode is
74  * set or unset respectively, std::string &parameter will
75  * contain the parameter given by the user, else it will
76  * contain an empty string. You may alter this parameter
77  * string, and if you alter it to an empty string, and your
78  * mode is expected to have a parameter, then this is
79  * equivalent to returning MODEACTION_DENY.
80  */
81 class ModeHandler : public Extensible
82 {
83  protected:
84         InspIRCd* ServerInstance;
85         /**
86          * The mode letter you're implementing.
87          */
88         char mode;
89         /**
90          * Number of parameters when being set
91          */
92         int n_params_on;
93         /**
94          * Number of parameters when being unset
95          */
96         int n_params_off;
97         /**
98          * Mode is a 'list' mode. The behaviour
99          * of your mode is now set entirely within
100          * the class as of the 1.1 api, rather than
101          * inside the mode parser as in the 1.0 api,
102          * so the only use of this value (along with
103          * IsListMode()) is for the core to determine
104          * wether your module can produce 'lists' or not
105          * (e.g. banlists, etc)
106          */
107         bool list;
108         /**
109          * The mode type, either MODETYPE_USER or
110          * MODETYPE_CHANNEL.
111          */
112         ModeType m_type;
113         /**
114          * True if the mode requires oper status
115          * to set.
116          */
117         bool oper;
118
119  public:
120         /**
121          * The constructor for ModeHandler initalizes the mode handler.
122          * The constructor of any class you derive from ModeHandler should
123          * probably call this constructor with the parameters set correctly.
124          * @param modeletter The mode letter you wish to handle
125          * @param parameters_on The number of parameters your mode takes when being set. Note that any nonzero value is treated as 1.
126          * @param parameters_off The number of parameters your mode takes when being unset. Note that any nonzero value is treated as 1.
127          * @param listmode Set to true if your mode is a listmode, e.g. it will respond to MODE #channel +modechar with a list of items
128          * @param ModeType Set this to MODETYPE_USER for a usermode, or MODETYPE_CHANNEL for a channelmode.
129          * @param operonly Set this to true if only opers should be allowed to set or unset the mode.
130          */
131         ModeHandler(InspIRCd* Instance, char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly);
132         /**
133          * The default destructor does nothing
134          */
135         virtual ~ModeHandler();
136
137         /**
138          * Returns true if the mode is a list mode
139          */
140         bool IsListMode();
141         /**
142          * Returns the modes type
143          */
144         ModeType GetModeType();
145         /**
146          * Returns true if the mode can only be set/unset by an oper
147          */
148         bool NeedsOper();
149         /**
150          * Returns the number of parameters for the mode. Any non-zero
151          * value should be considered to be equivalent to one.
152          * @param adding If this is true, the number of parameters required to set the mode should be returned, otherwise the number of parameters required to unset the mode shall be returned.
153          * @return The number of parameters the mode expects
154          */
155         int GetNumParams(bool adding);
156         /**
157          * Returns the mode character this handler handles.
158          * @return The mode character
159          */
160         char GetModeChar();
161
162         /**
163          * Called when a mode change for your mode occurs.
164          * @param source Contains the user setting the mode.
165          * @param dest For usermodes, contains the destination user the mode is being set on. For channelmodes, this is an undefined value.
166          * @param channel For channel modes, contains the destination channel the modes are being set on. For usermodes, this is an undefined value.
167          * @param parameter The parameter for your mode, if you indicated that your mode requires a parameter when being set or unset. Note that
168          * if you alter this value, the new value becomes the one displayed and send out to the network, also, if you set this to an empty string
169          * but you specified your mode REQUIRES a parameter, this is equivalent to returning MODEACTION_DENY and will prevent the mode from being
170          * displayed.
171          * @param adding This value is true when the mode is being set, or false when it is being unset.
172          * @return MODEACTION_ALLOW to allow the mode, or MODEACTION_DENY to prevent the mode, also see the description of 'parameter'.
173          */
174         virtual ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
175         /**
176          * If your mode is a listmode, then this method will be called for displaying an item list, e.g. on MODE #channel +modechar
177          * without any parameter or other modes in the command.
178          * @param user The user issuing the command
179          * @parameter channel The channel they're requesting an item list of (e.g. a banlist, or an exception list etc)
180          */
181         virtual void DisplayList(userrec* user, chanrec* channel);
182         /**
183          * If your mode needs special action during a server sync to determine which side wins when comparing timestamps,
184          * override this function and use it to return true or false. The default implementation just returns true if
185          * theirs < ours. This will only be called for non-listmodes with parameters, when adding the mode and where
186          * theirs == ours (therefore the default implementation will always return false).
187          * @param theirs The timestamp of the remote side
188          * @param ours The timestamp of the local side
189          * @param their_param Their parameter if the mode has a parameter
190          * @param our_param Our parameter if the mode has a parameter
191          * @param channel The channel we are checking against
192          * @return True if the other side wins the merge, false if we win the merge for this mode.
193          */
194         virtual bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel);
195
196         /**
197          * When a remote server needs to bounce a set of modes, it will call this method for every mode
198          * in the mode string to determine if the mode is set or not.
199          * @param source of the mode change, this will be NULL for a server mode
200          * @param dest Target user of the mode change, if this is a user mode
201          * @param channel Target channel of the mode change, if this is a channel mode
202          * @param parameter The parameter given for the mode change, or an empty string
203          * @returns The first value of the pair should be true if the mode is set with the given parameter.
204          * In the case of permissions modes such as channelmode +o, this should return true if the user given
205          * as the parameter has the given privilage on the given channel. The string value of the pair will hold
206          * the current setting for this mode set locally, when the bool is true, or, the parameter given.
207          * This allows the local server to enforce our locally set parameters back to a remote server.
208          */
209         virtual ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter);
210 };
211
212 /**
213  * The ModeWatcher class can be used to alter the behaviour of a mode implemented
214  * by the core or by another module. To use ModeWatcher, derive a class from it,
215  * and attach it to the mode using Server::AddModeWatcher and Server::DelModeWatcher.
216  * A ModeWatcher will be called both before and after the mode change.
217  */
218 class ModeWatcher : public Extensible
219 {
220  protected:
221         InspIRCd* ServerInstance;
222         /**
223          * The mode letter this class is watching
224          */
225         char mode;
226         /**
227          * The mode type being watched (user or  channel)
228          */
229         ModeType m_type;
230
231  public:
232         /**
233          * The constructor initializes the mode and the mode type
234          */
235         ModeWatcher(InspIRCd* Instance, char modeletter, ModeType type);
236         /**
237          * The default destructor does nothing.
238          */
239         virtual ~ModeWatcher();
240
241         /**
242          * Get the mode character being watched
243          * @return The mode character being watched
244          */
245         char GetModeChar();
246         /**
247          * Get the mode type being watched
248          * @return The mode type being watched (user or channel)
249          */
250         ModeType GetModeType();
251
252         /**
253          * Before the mode character is processed by its handler, this method will be called.
254          * @param source The sender of the mode
255          * @param dest The target user for the mode, if you are watching a user mode
256          * @param channel The target channel for the mode, if you are watching a channel mode
257          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
258          * If you alter the parameter you are given, the mode handler will see your atered version
259          * when it handles the mode.
260          * @param adding True if the mode is being added and false if it is being removed
261          * @type The mode type, either MODETYPE_USER or MODETYPE_CHANNEL
262          * @return True to allow the mode change to go ahead, false to abort it. If you abort the
263          * change, the mode handler (and ModeWatcher::AfterMode()) will never see the mode change.
264          */
265         virtual bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding, ModeType type);
266         /**
267          * After the mode character has been processed by the ModeHandler, this method will be called.
268          * @param source The sender of the mode
269          * @param dest The target user for the mode, if you are watching a user mode
270          * @param channel The target channel for the mode, if you are watching a channel mode
271          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
272          * You cannot alter the parameter here, as the mode handler has already processed it.
273          * @param adding True if the mode is being added and false if it is being removed
274          * @type The mode type, either MODETYPE_USER or MODETYPE_CHANNEL
275          */
276         virtual void AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter, bool adding, ModeType type);
277 };
278
279 typedef std::vector<ModeWatcher*>::iterator ModeWatchIter;
280
281 /** The mode parser handles routing of modes and handling of mode strings.
282  * It marshalls, controls and maintains both ModeWatcher and ModeHandler classes,
283  * parses client to server MODE strings for user and channel modes, and performs
284  * processing for the 004 mode list numeric, amongst other things.
285  */
286 class ModeParser : public classbase
287 {
288  private:
289         InspIRCd* ServerInstance;
290         /**
291          * Mode handlers for each mode, to access a handler subtract
292          * 65 from the ascii value of the mode letter.
293          * The upper bit of the value indicates if its a usermode
294          * or a channel mode, so we have 256 of them not 64.
295          */
296         ModeHandler* modehandlers[256];
297         /**
298          * Mode watcher classes arranged in the same way as the
299          * mode handlers, except for instead of having 256 of them
300          * we have 256 lists of them.
301          */
302         std::vector<ModeWatcher*> modewatchers[256];
303         /**
304          * Displays the current modes of a channel or user.
305          * Used by ModeParser::Process.
306          */
307         void DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text);
308
309  public:
310
311         /**
312          * The constructor initializes all the RFC basic modes by using ModeParserAddMode().
313          */
314         ModeParser(InspIRCd* Instance);
315
316         /**
317          * Used to check if user 'd' should be allowed to do operation 'MASK' on channel 'chan'.
318          * for example, should 'user A' be able to 'op' on 'channel B'.
319          */
320         userrec* SanityChecks(userrec *user,const char *dest,chanrec *chan,int status);
321         /**
322          * Grant a built in privilage (e.g. ops, halfops, voice) to a user on a channel
323          */
324         const char* Grant(userrec *d,chanrec *chan,int MASK);
325         /**
326          * Revoke a built in privilage (e.g. ops, halfops, voice) to a user on a channel
327          */
328         const char* Revoke(userrec *d,chanrec *chan,int MASK);
329         /**
330          * Tidy a banmask. This makes a banmask 'acceptable' if fields are left out.
331          * E.g.
332          *
333          * nick -> nick!*@*
334          * 
335          * nick!ident -> nick!ident@*
336          * 
337          * host.name -> *!*@host.name
338          * 
339          * ident@host.name -> *!ident@host.name
340          *
341          * This method can be used on both IPV4 and IPV6 user masks.
342          */
343         static void CleanMask(std::string &mask);
344         /**
345          * Add a mode to the mode parser. The modeletter parameter
346          * is purely to save on doing a lookup in the function, as
347          * strictly it could be obtained via ModeHandler::GetModeChar().
348          * @return True if the mode was successfully added.
349          */
350         bool AddMode(ModeHandler* mh, unsigned const char modeletter);
351         /**
352          * Add a mode watcher.
353          * A mode watcher is triggered before and after a mode handler is
354          * triggered. See the documentation of class ModeWatcher for more
355          * information.
356          * @param mw The ModeWatcher you want to add
357          * @return True if the ModeWatcher was added correctly
358          */
359         bool AddModeWatcher(ModeWatcher* mw);
360         /**
361          * Delete a mode watcher.
362          * A mode watcher is triggered before and after a mode handler is
363          * triggered. See the documentation of class ModeWatcher for more
364          * information.
365          * @param mw The ModeWatcher you want to delete
366          * @return True if the ModeWatcher was deleted correctly
367          */
368         bool DelModeWatcher(ModeWatcher* mw);
369         /**
370          * Process a set of mode changes from a server or user.
371          * @param parameters The parameters of the mode change, in the format
372          * they would be from a MODE command.
373          * @param pcnt The number of items in the parameters array
374          * @param user The user setting or removing the modes. When the modes are set
375          * by a server, an 'uninitialized' userrec is used, where *user::nick == NULL
376          * and *user->server == NULL.
377          * @param servermode True if a server is setting the mode.
378          */
379         void Process(const char** parameters, int pcnt, userrec *user, bool servermode);
380
381         /**
382          * Find the mode handler for a given mode and type
383          * @param modeletter mode letter to search for
384          * @param type of mode to search for, user or channel
385          * @returns a pointer to a ModeHandler class, or NULL of there isnt a handler for the given mode
386          */
387         ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
388
389         std::string UserModeList();
390
391         std::string ChannelModeList();
392
393         std::string ParaModeList();
394
395         bool InsertMode(std::string &output, const char* mode, unsigned short section);
396 };
397
398 /**
399  * Command handler class for the MODE command.
400  * put here for completeness.
401  */
402 class cmd_mode : public command_t
403 {
404  public:
405         /**
406          * Standard constructor
407          */
408         cmd_mode (InspIRCd* Instance) : command_t(Instance,"MODE",0,1) { syntax = "<target> <modes> {<mode-parameters>}"; }
409         /**
410          * Handle MODE
411          */
412         void Handle(const char** parameters, int pcnt, userrec *user);
413 };
414
415 #endif