]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/mode.h
Wheee, mass commit! this adds const stafety, throwing a compile error if anyone does...
[user/henk/code/inspircd.git] / include / mode.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __MODE_H
15 #define __MODE_H
16
17 /**
18  * Holds the values for different type of modes
19  * that can exist, USER or CHANNEL type.
20  */
21 enum ModeType
22 {
23         /** User mode */
24         MODETYPE_USER = 0,
25         /** Channel mode */
26         MODETYPE_CHANNEL = 1
27 };
28
29 /**
30  * Holds mode actions - modes can be allowed or denied.
31  */
32 enum ModeAction
33 {
34         MODEACTION_DENY = 0, /* Drop the mode change, AND a parameter if its a parameterized mode */
35         MODEACTION_ALLOW = 1 /* Allow the mode */
36 };
37
38 /**
39  * Used to mask off the mode types in the mode handler
40  * array. Used in a simple two instruction hashing function
41  * "(modeletter - 65) OR mask"
42  */
43 enum ModeMasks
44 {
45         MASK_USER = 128,        /* A user mode */
46         MASK_CHANNEL = 0        /* A channel mode */
47 };
48
49 /**
50  * These fixed values can be used to proportionally compare module-defined prefixes to known values.
51  * For example, if your module queries a Channel, and is told that user 'joebloggs' has the prefix
52  * '$', and you dont know what $ means, then you can compare it to these three values to determine
53  * its worth against them. For example if '$' had a value of 15000, you would know it is of higher
54  * status than voice, but lower status than halfop.
55  * No two modes should have equal prefix values.
56  */
57 enum PrefixModeValue
58 {
59         /* +v */
60         VOICE_VALUE     =       10000,
61         /* +h */
62         HALFOP_VALUE    =       20000,
63         /* +o */
64         OP_VALUE        =       30000
65 };
66
67 /**
68  * Used by ModeHandler::ModeSet() to return the state of a mode upon a channel or user.
69  * The pair contains an activity flag, true if the mode is set with the given parameter,
70  * and the parameter of the mode (or the parameter provided) in the std::string.
71  */
72 typedef std::pair<bool,std::string> ModePair;
73
74 /** Each mode is implemented by ONE ModeHandler class.
75  * You must derive ModeHandler and add the child class to
76  * the list of modes handled by the ircd, using
77  * ModeParser::AddMode. When the mode you implement is
78  * set by a user, the virtual function OnModeChange is
79  * called. If you specify a value greater than 0 for
80  * parameters_on or parameters_off, then when the mode is
81  * set or unset respectively, std::string &parameter will
82  * contain the parameter given by the user, else it will
83  * contain an empty string. You may alter this parameter
84  * string, and if you alter it to an empty string, and your
85  * mode is expected to have a parameter, then this is
86  * equivalent to returning MODEACTION_DENY.
87  */
88 class CoreExport ModeHandler : public Extensible
89 {
90  protected:
91         /**
92          * Creator/owner pointer
93          */
94         InspIRCd* ServerInstance;
95         /**
96          * The mode letter you're implementing.
97          */
98         char mode;
99         /**
100          * Number of parameters when being set
101          */
102         int n_params_on;
103         /**
104          * Number of parameters when being unset
105          */
106         int n_params_off;
107         /**
108          * Mode is a 'list' mode. The behaviour
109          * of your mode is now set entirely within
110          * the class as of the 1.1 api, rather than
111          * inside the mode parser as in the 1.0 api,
112          * so the only use of this value (along with
113          * IsListMode()) is for the core to determine
114          * wether your module can produce 'lists' or not
115          * (e.g. banlists, etc)
116          */
117         bool list;
118         /**
119          * The mode type, either MODETYPE_USER or
120          * MODETYPE_CHANNEL.
121          */
122         ModeType m_type;
123         /**
124          * True if the mode requires oper status
125          * to set.
126          */
127         bool oper;
128
129         /** Mode prefix, or 0
130          */
131         char prefix;
132
133         /** Number of items with this mode set on them
134          */
135         unsigned int count;
136
137         /** The prefix char needed on channel to use this mode,
138          * only checked for channel modes
139          */
140         char prefixneeded;
141
142  public:
143         /**
144          * The constructor for ModeHandler initalizes the mode handler.
145          * The constructor of any class you derive from ModeHandler should
146          * probably call this constructor with the parameters set correctly.
147          * @param modeletter The mode letter you wish to handle
148          * @param parameters_on The number of parameters your mode takes when being set. Note that any nonzero value is treated as 1.
149          * @param parameters_off The number of parameters your mode takes when being unset. Note that any nonzero value is treated as 1.
150          * @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
151          * @param ModeType Set this to MODETYPE_USER for a usermode, or MODETYPE_CHANNEL for a channelmode.
152          * @param operonly Set this to true if only opers should be allowed to set or unset the mode.
153          * @param mprefix For listmodes where parameters are NICKNAMES which are on the channel (for example, +ohv), you may define a prefix.
154          * When you define a prefix, it can be returned in NAMES, WHO etc if it has the highest value (as returned by GetPrefixRank())
155          * In the core, the only modes to implement prefixes are +ovh (ops, voice, halfop) which define the prefix characters @, % and +
156          * and the rank values OP_VALUE, HALFOP_VALUE and VOICE_VALUE respectively. Any prefixes you define should have unique values proportional
157          * to these three defaults or proportional to another mode in a module you depend on. See src/cmode_o.cpp as an example.
158          */
159         ModeHandler(InspIRCd* Instance, char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly, char mprefix = 0, char prefixrequired = '%');
160         /**
161          * The default destructor does nothing
162          */
163         virtual ~ModeHandler();
164         /**
165          * Returns true if the mode is a list mode
166          */
167         bool IsListMode();
168         /**
169          * Mode prefix or 0. If this is defined, you should
170          * also implement GetPrefixRank() to return an integer
171          * value for this mode prefix.
172          */
173         char GetPrefix();
174         /** Get number of items with this mode set on them
175          */
176         virtual unsigned int GetCount();
177         /** Adjust usage count returned by GetCount
178          */
179         virtual void ChangeCount(int modifier);
180         /**
181          * Get the 'value' of this modes prefix.
182          * determines which to display when there are multiple.
183          * The mode with the highest value is ranked first. See the
184          * PrefixModeValue enum and Channel::GetPrefixValue() for
185          * more information.
186          */
187         virtual unsigned int GetPrefixRank();
188         /**
189          * Returns the modes type
190          */
191         ModeType GetModeType();
192         /**
193          * Returns true if the mode can only be set/unset by an oper
194          */
195         bool NeedsOper();
196         /**
197          * Returns the number of parameters for the mode. Any non-zero
198          * value should be considered to be equivalent to one.
199          * @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.
200          * @return The number of parameters the mode expects
201          */
202         int GetNumParams(bool adding);
203         /**
204          * Returns the mode character this handler handles.
205          * @return The mode character
206          */
207         char GetModeChar();
208
209         /**
210          * Called when a mode change for your mode occurs.
211          * @param source Contains the user setting the mode.
212          * @param dest For usermodes, contains the destination user the mode is being set on. For channelmodes, this is an undefined value.
213          * @param channel For channel modes, contains the destination channel the modes are being set on. For usermodes, this is an undefined value.
214          * @param parameter The parameter for your mode, if you indicated that your mode requires a parameter when being set or unset. Note that
215          * 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
216          * but you specified your mode REQUIRES a parameter, this is equivalent to returning MODEACTION_DENY and will prevent the mode from being
217          * displayed.
218          * @param adding This value is true when the mode is being set, or false when it is being unset.
219          * @return MODEACTION_ALLOW to allow the mode, or MODEACTION_DENY to prevent the mode, also see the description of 'parameter'.
220          */
221         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool servermode = false); /* Can change the mode parameter as its a ref */
222         /**
223          * If your mode is a listmode, then this method will be called for displaying an item list, e.g. on MODE #channel +modechar
224          * without any parameter or other modes in the command.
225          * @param user The user issuing the command
226          * @param channel The channel they're requesting an item list of (e.g. a banlist, or an exception list etc)
227          */
228         virtual void DisplayList(User* user, Channel* channel);
229
230         /**
231          * If your mode is a listmode, this method will be called to display an empty list (just the end of list numeric)
232          * @param user The user issuing the command
233          * @param channel The channel tehy're requesting an item list of (e.g. a banlist, or an exception list etc)
234          */
235         virtual void DisplayEmptyList(User* user, Channel* channel);
236
237         /**
238          * If your mode needs special action during a server sync to determine which side wins when comparing timestamps,
239          * override this function and use it to return true or false. The default implementation just returns true if
240          * theirs < ours. This will only be called for non-listmodes with parameters, when adding the mode and where
241          * theirs == ours (therefore the default implementation will always return false).
242          * @param theirs The timestamp of the remote side
243          * @param ours The timestamp of the local side
244          * @param their_param Their parameter if the mode has a parameter
245          * @param our_param Our parameter if the mode has a parameter
246          * @param channel The channel we are checking against
247          * @return True if the other side wins the merge, false if we win the merge for this mode.
248          */
249         virtual bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel);
250
251         /**
252          * When a remote server needs to bounce a set of modes, it will call this method for every mode
253          * in the mode string to determine if the mode is set or not.
254          * @param source of the mode change, this will be NULL for a server mode
255          * @param dest Target user of the mode change, if this is a user mode
256          * @param channel Target channel of the mode change, if this is a channel mode
257          * @param parameter The parameter given for the mode change, or an empty string
258          * @returns The first value of the pair should be true if the mode is set with the given parameter.
259          * In the case of permissions modes such as channelmode +o, this should return true if the user given
260          * as the parameter has the given privilage on the given channel. The string value of the pair will hold
261          * the current setting for this mode set locally, when the bool is true, or, the parameter given.
262          * This allows the local server to enforce our locally set parameters back to a remote server.
263          */
264         virtual ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter);
265
266         /**
267          * When a MODETYPE_USER mode handler is being removed, the server will call this method for every user on the server.
268          * Your mode handler should remove its user mode from the user by sending the appropriate server modes using
269          * InspIRCd::SendMode(). The default implementation of this method can remove simple modes which have no parameters,
270          * and can be used when your mode is of this type, otherwise you must implement a more advanced version of it to remove
271          * your mode properly from each user.
272          * @param user The user which the server wants to remove your mode from
273          */
274         virtual void RemoveMode(User* user);
275
276         /**
277          * When a MODETYPE_CHANNEL mode handler is being removed, the server will call this method for every channel on the server.
278          * Your mode handler should remove its user mode from the channel by sending the appropriate server modes using
279          * InspIRCd::SendMode(). The default implementation of this method can remove simple modes which have no parameters,
280          * and can be used when your mode is of this type, otherwise you must implement a more advanced version of it to remove
281          * your mode properly from each channel. Note that in the case of listmodes, you should remove the entire list of items.
282          * @param channel The channel which the server wants to remove your mode from
283          */
284         virtual void RemoveMode(Channel* channel);
285
286         char GetNeededPrefix();
287
288         void SetNeededPrefix(char needsprefix);
289 };
290
291 /**
292  * The ModeWatcher class can be used to alter the behaviour of a mode implemented
293  * by the core or by another module. To use ModeWatcher, derive a class from it,
294  * and attach it to the mode using Server::AddModeWatcher and Server::DelModeWatcher.
295  * A ModeWatcher will be called both before and after the mode change.
296  */
297 class CoreExport ModeWatcher : public Extensible
298 {
299  protected:
300         /**
301          * Creator/owner pointer
302          */
303         InspIRCd* ServerInstance;
304         /**
305          * The mode letter this class is watching
306          */
307         char mode;
308         /**
309          * The mode type being watched (user or  channel)
310          */
311         ModeType m_type;
312
313  public:
314         /**
315          * The constructor initializes the mode and the mode type
316          */
317         ModeWatcher(InspIRCd* Instance, char modeletter, ModeType type);
318         /**
319          * The default destructor does nothing.
320          */
321         virtual ~ModeWatcher();
322
323         /**
324          * Get the mode character being watched
325          * @return The mode character being watched
326          */
327         char GetModeChar();
328         /**
329          * Get the mode type being watched
330          * @return The mode type being watched (user or channel)
331          */
332         ModeType GetModeType();
333
334         /**
335          * Before the mode character is processed by its handler, this method will be called.
336          * @param source The sender of the mode
337          * @param dest The target user for the mode, if you are watching a user mode
338          * @param channel The target channel for the mode, if you are watching a channel mode
339          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
340          * If you alter the parameter you are given, the mode handler will see your atered version
341          * when it handles the mode.
342          * @param adding True if the mode is being added and false if it is being removed
343          * @type The mode type, either MODETYPE_USER or MODETYPE_CHANNEL
344          * @return True to allow the mode change to go ahead, false to abort it. If you abort the
345          * change, the mode handler (and ModeWatcher::AfterMode()) will never see the mode change.
346          */
347         virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, ModeType type, bool servermode = false);
348         /**
349          * After the mode character has been processed by the ModeHandler, this method will be called.
350          * @param source The sender of the mode
351          * @param dest The target user for the mode, if you are watching a user mode
352          * @param channel The target channel for the mode, if you are watching a channel mode
353          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
354          * You cannot alter the parameter here, as the mode handler has already processed it.
355          * @param adding True if the mode is being added and false if it is being removed
356          * @type The mode type, either MODETYPE_USER or MODETYPE_CHANNEL
357          */
358         virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string &parameter, bool adding, ModeType type, bool servermode = false);
359 };
360
361 typedef std::vector<ModeWatcher*>::iterator ModeWatchIter;
362
363 /** The mode parser handles routing of modes and handling of mode strings.
364  * It marshalls, controls and maintains both ModeWatcher and ModeHandler classes,
365  * parses client to server MODE strings for user and channel modes, and performs
366  * processing for the 004 mode list numeric, amongst other things.
367  */
368 class CoreExport ModeParser : public classbase
369 {
370  private:
371         /**
372          * Creator/owner pointer
373          */
374         InspIRCd* ServerInstance;
375         /** Mode handlers for each mode, to access a handler subtract
376          * 65 from the ascii value of the mode letter.
377          * The upper bit of the value indicates if its a usermode
378          * or a channel mode, so we have 256 of them not 64.
379          */
380         ModeHandler* modehandlers[256];
381         /** Mode watcher classes arranged in the same way as the
382          * mode handlers, except for instead of having 256 of them
383          * we have 256 lists of them.
384          */
385         std::vector<ModeWatcher*> modewatchers[256];
386         /** Displays the current modes of a channel or user.
387          * Used by ModeParser::Process.
388          */
389         void DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text);
390
391         /** The string representing the last set of modes to be parsed.
392          * Use GetLastParse() to get this value, to be used for  display purposes.
393          */
394         std::string LastParse;
395
396  public:
397
398         /** The constructor initializes all the RFC basic modes by using ModeParserAddMode().
399          */
400         ModeParser(InspIRCd* Instance);
401
402         /** Used to check if user 'd' should be allowed to do operation 'MASK' on channel 'chan'.
403          * for example, should 'user A' be able to 'op' on 'channel B'.
404          */
405         User* SanityChecks(User *user,const char *dest,Channel *chan,int status);
406         /** Grant a built in privilage (e.g. ops, halfops, voice) to a user on a channel
407          */
408         const char* Grant(User *d,Channel *chan,int MASK);
409         /** Revoke a built in privilage (e.g. ops, halfops, voice) to a user on a channel
410          */
411         const char* Revoke(User *d,Channel *chan,int MASK);
412         /** Tidy a banmask. This makes a banmask 'acceptable' if fields are left out.
413          * E.g.
414          *
415          * nick -> nick!*@*
416          * 
417          * nick!ident -> nick!ident@*
418          * 
419          * host.name -> *!*@host.name
420          * 
421          * ident@host.name -> *!ident@host.name
422          *
423          * This method can be used on both IPV4 and IPV6 user masks.
424          */
425         static void CleanMask(std::string &mask);
426         /** Get the last string to be processed, as it was sent to the user or channel. 
427          * Use this to display a string you just sent to be parsed, as the actual output
428          * may be different to what you sent after it has been 'cleaned up' by the parser.
429          * @return Last parsed string, as seen by users.
430          */
431         const std::string& GetLastParse();
432         /** Add a mode to the mode parser.
433          * @return True if the mode was successfully added.
434          */
435         bool AddMode(ModeHandler* mh);
436         /** Delete a mode from the mode parser.
437          * When a mode is deleted, the mode handler will be called
438          * for every user (if it is a user mode) or for every  channel
439          * (if it is a channel mode) to unset the mode on all objects.
440          * This prevents modes staying in the system which no longer exist.
441          * @param mh The mode handler to remove
442          * @return True if the mode was successfully removed.
443          */
444         bool DelMode(ModeHandler* mh);
445         /** Add a mode watcher.
446          * A mode watcher is triggered before and after a mode handler is
447          * triggered. See the documentation of class ModeWatcher for more
448          * information.
449          * @param mw The ModeWatcher you want to add
450          * @return True if the ModeWatcher was added correctly
451          */
452         bool AddModeWatcher(ModeWatcher* mw);
453         /** Delete a mode watcher.
454          * A mode watcher is triggered before and after a mode handler is
455          * triggered. See the documentation of class ModeWatcher for more
456          * information.
457          * @param mw The ModeWatcher you want to delete
458          * @return True if the ModeWatcher was deleted correctly
459          */
460         bool DelModeWatcher(ModeWatcher* mw);
461         /** Process a set of mode changes from a server or user.
462          * @param parameters The parameters of the mode change, in the format
463          * they would be from a MODE command.
464          * @param pcnt The number of items in the parameters array
465          * @param user The user setting or removing the modes. When the modes are set
466          * by a server, an 'uninitialized' User is used, where *user::nick == NULL
467          * and *user->server == NULL.
468          * @param servermode True if a server is setting the mode.
469          */
470         void Process(const char* const* parameters, int pcnt, User *user, bool servermode);
471
472         /** Find the mode handler for a given mode and type.
473          * @param modeletter mode letter to search for
474          * @param type of mode to search for, user or channel
475          * @returns a pointer to a ModeHandler class, or NULL of there isnt a handler for the given mode
476          */
477         ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
478
479         /** Find a mode handler by its prefix.
480          * If there is no mode handler with the given prefix, NULL will be returned.
481          * @param pfxletter The prefix to find, e.g. '@'
482          * @return The mode handler which handles this prefix, or NULL if there is none.
483          */
484         ModeHandler* FindPrefix(unsigned const char pfxletter);
485
486         /** Returns a list of mode characters which are usermodes.
487          * This is used in the 004 numeric when users connect.
488          */
489         std::string UserModeList();
490
491         /** Returns a list of channel mode characters which are listmodes.
492          * This is used in the 004 numeric when users connect.
493          */
494         std::string ChannelModeList();
495
496         /** Returns a list of channel mode characters which take parameters.
497          * This is used in the 004 numeric when users connect.
498          */
499         std::string ParaModeList();
500
501         /** Generates the CHANMODES= 005 sequence
502          */
503         std::string ChanModes();
504         /** Used by this class internally during std::sort and 005 generation
505          */
506         static bool PrefixComparison(prefixtype one, prefixtype two);
507
508         /** This returns the PREFIX=(ohv)@%+ section of the 005 numeric.
509          */
510         std::string BuildPrefixes();
511
512         /** This returns the privilages of a user upon a channel, in the format of a mode change.
513          * For example, if a user has privilages +avh, this will return the string "avh nick nick nick".
514          * This is used by the core when cycling a user to refresh their hostname. You may use it for
515          * similar purposes.
516          * @param user The username to look up
517          * @param channel The channel name to look up the privilages of the user for
518          * @return The mode string.
519          */
520         std::string ModeString(User* user, Channel* channel);
521 };
522
523 #endif
524