]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/mode.h
dd5334c66e640ce5e7d56f96088bb4b0a638457c
[user/henk/code/inspircd.git] / include / mode.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2004-2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #pragma once
24
25 #include "ctables.h"
26
27 /**
28  * Holds the values for different type of modes
29  * that can exist, USER or CHANNEL type.
30  */
31 enum ModeType
32 {
33         /** User mode */
34         MODETYPE_USER = 0,
35         /** Channel mode */
36         MODETYPE_CHANNEL = 1
37 };
38
39 /**
40  * Holds mode actions - modes can be allowed or denied.
41  */
42 enum ModeAction
43 {
44         MODEACTION_DENY = 0, /* Drop the mode change, AND a parameter if its a parameterized mode */
45         MODEACTION_ALLOW = 1 /* Allow the mode */
46 };
47
48 /**
49  * Used to mask off the mode types in the mode handler
50  * array. Used in a simple two instruction hashing function
51  * "(modeletter - 65) OR mask"
52  */
53 enum ModeMasks
54 {
55         MASK_USER = 128,        /* A user mode */
56         MASK_CHANNEL = 0        /* A channel mode */
57 };
58
59 /**
60  * These fixed values can be used to proportionally compare module-defined prefixes to known values.
61  * For example, if your module queries a Channel, and is told that user 'joebloggs' has the prefix
62  * '$', and you dont know what $ means, then you can compare it to these three values to determine
63  * its worth against them. For example if '$' had a value of 15000, you would know it is of higher
64  * status than voice, but lower status than halfop.
65  * No two modes should have equal prefix values.
66  */
67 enum PrefixModeValue
68 {
69         /* +v */
70         VOICE_VALUE     =       10000,
71         /* +h */
72         HALFOP_VALUE    =       20000,
73         /* +o */
74         OP_VALUE        =       30000
75 };
76
77 enum ParamSpec
78 {
79         /** No parameters */
80         PARAM_NONE,
81         /** Parameter required on mode setting only */
82         PARAM_SETONLY,
83         /** Parameter always required */
84         PARAM_ALWAYS
85 };
86
87 class PrefixMode;
88 class ListModeBase;
89 class ParamModeBase;
90
91 /** Each mode is implemented by ONE ModeHandler class.
92  * You must derive ModeHandler and add the child class to
93  * the list of modes handled by the ircd, using
94  * ModeParser::AddMode. When the mode you implement is
95  * set by a user, the virtual function OnModeChange is
96  * called. If you specify a value greater than 0 for
97  * parameters_on or parameters_off, then when the mode is
98  * set or unset respectively, std::string &parameter will
99  * contain the parameter given by the user, else it will
100  * contain an empty string. You may alter this parameter
101  * string, and if you alter it to an empty string, and your
102  * mode is expected to have a parameter, then this is
103  * equivalent to returning MODEACTION_DENY.
104  */
105 class CoreExport ModeHandler : public ServiceProvider
106 {
107  public:
108         enum Class
109         {
110                 MC_PREFIX,
111                 MC_LIST,
112                 MC_PARAM,
113                 MC_OTHER
114         };
115
116  protected:
117         /**
118          * The mode parameter translation type
119          */
120         TranslateType m_paramtype;
121
122         /** What kind of parameters does the mode take?
123          */
124         ParamSpec parameters_taken;
125
126         /**
127          * The mode letter you're implementing.
128          */
129         char mode;
130
131         /**
132          * True if the mode requires oper status
133          * to set.
134          */
135         bool oper;
136
137         /**
138          * Mode is a 'list' mode. The behaviour
139          * of your mode is now set entirely within
140          * the class as of the 1.1 api, rather than
141          * inside the mode parser as in the 1.0 api,
142          * so the only use of this value (along with
143          * IsListMode()) is for the core to determine
144          * wether your module can produce 'lists' or not
145          * (e.g. banlists, etc)
146          */
147         bool list;
148
149         /**
150          * The mode type, either MODETYPE_USER or
151          * MODETYPE_CHANNEL.
152          */
153         ModeType m_type;
154
155         /** The object type of this mode handler
156          */
157         const Class type_id;
158
159         /** The prefix char needed on channel to use this mode,
160          * only checked for channel modes
161          */
162         int levelrequired;
163
164  public:
165         /**
166          * The constructor for ModeHandler initalizes the mode handler.
167          * The constructor of any class you derive from ModeHandler should
168          * probably call this constructor with the parameters set correctly.
169          * @param me The module which created this mode
170          * @param name A one-word name for the mode
171          * @param modeletter The mode letter you wish to handle
172          * @param params Parameters taken by the mode
173          * @param type Type of the mode (MODETYPE_USER or MODETYPE_CHANNEL)
174          * @param mclass The object type of this mode handler, one of ModeHandler::Class
175          */
176         ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER);
177         virtual CullResult cull();
178         virtual ~ModeHandler();
179         /**
180          * Returns true if the mode is a list mode
181          */
182         bool IsListMode() const { return list; }
183
184         /**
185          * Check whether this mode is a prefix mode
186          * @return non-NULL if this mode is a prefix mode, NULL otherwise
187          */
188         PrefixMode* IsPrefixMode();
189
190         /**
191          * Check whether this mode handler inherits from ListModeBase
192          * @return non-NULL if this mode handler inherits from ListModeBase, NULL otherwise
193          */
194         ListModeBase* IsListModeBase();
195
196         /**
197          * Check whether this mode handler inherits from ListModeBase
198          * @return non-NULL if this mode handler inherits from ParamModeBase, NULL otherwise
199          */
200         ParamModeBase* IsParameterMode();
201
202         /**
203          * Returns the mode's type
204          */
205         inline ModeType GetModeType() const { return m_type; }
206         /**
207          * Returns the mode's parameter translation type
208          */
209         inline TranslateType GetTranslateType() const { return m_paramtype; }
210         /**
211          * Returns true if the mode can only be set/unset by an oper
212          */
213         inline bool NeedsOper() const { return oper; }
214         /**
215          * Returns the number of parameters for the mode. Any non-zero
216          * value should be considered to be equivalent to one.
217          * @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.
218          * @return The number of parameters the mode expects
219          */
220         int GetNumParams(bool adding);
221         /**
222          * Returns the mode character this handler handles.
223          * @return The mode character
224          */
225         inline char GetModeChar() { return mode; }
226
227         /** For user modes, return the current parameter, if any
228          */
229         virtual std::string GetUserParameter(User* useor);
230
231         /**
232          * Called when a channel mode change access check for your mode occurs.
233          * @param source Contains the user setting the mode.
234          * @param channel contains the destination channel the modes are being set on.
235          * @param parameter The parameter for your mode. This is modifiable.
236          * @param adding This value is true when the mode is being set, or false when it is being unset.
237          * @return allow, deny, or passthru to check against the required level
238          */
239         virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
240
241         /**
242          * Called when a mode change for your mode occurs.
243          * @param source Contains the user setting the mode.
244          * @param dest For usermodes, contains the destination user the mode is being set on. For channelmodes, this is an undefined value.
245          * @param channel For channel modes, contains the destination channel the modes are being set on. For usermodes, this is an undefined value.
246          * @param parameter The parameter for your mode, if you indicated that your mode requires a parameter when being set or unset. Note that
247          * 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
248          * but you specified your mode REQUIRES a parameter, this is equivalent to returning MODEACTION_DENY and will prevent the mode from being
249          * displayed.
250          * @param adding This value is true when the mode is being set, or false when it is being unset.
251          * @return MODEACTION_ALLOW to allow the mode, or MODEACTION_DENY to prevent the mode, also see the description of 'parameter'.
252          */
253         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
254         /**
255          * If your mode is a listmode, then this method will be called for displaying an item list, e.g. on MODE \#channel +modechar
256          * without any parameter or other modes in the command.
257          * @param user The user issuing the command
258          * @param channel The channel they're requesting an item list of (e.g. a banlist, or an exception list etc)
259          */
260         virtual void DisplayList(User* user, Channel* channel);
261
262         /** In the event that the mode should be given a parameter, and no parameter was provided, this method is called.
263          * This allows you to give special information to the user, or handle this any way you like.
264          * @param user The user issuing the mode change
265          * @param dest For user mode changes, the target of the mode. For channel mode changes, NULL.
266          * @param channel For channel mode changes, the target of the mode. For user mode changes, NULL.
267          */
268         virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
269
270         /**
271          * If your mode is a listmode, this method will be called to display an empty list (just the end of list numeric)
272          * @param user The user issuing the command
273          * @param channel The channel tehy're requesting an item list of (e.g. a banlist, or an exception list etc)
274          */
275         virtual void DisplayEmptyList(User* user, Channel* channel);
276
277         /**
278          * If your mode needs special action during a server sync to determine which side wins when comparing timestamps,
279          * override this function and use it to return true or false. The default implementation just returns true if
280          * theirs < ours. This will only be called for non-listmodes with parameters, when adding the mode and where
281          * theirs == ours (therefore the default implementation will always return false).
282          * @param their_param Their parameter if the mode has a parameter
283          * @param our_param Our parameter if the mode has a parameter
284          * @param channel The channel we are checking against
285          * @return True if the other side wins the merge, false if we win the merge for this mode.
286          */
287         virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
288
289         /**
290          * When a MODETYPE_USER mode handler is being removed, the core will call this method for every user on the server.
291          * The usermode will be removed using the appropiate server mode using InspIRCd::SendMode().
292          * @param user The user which the server wants to remove your mode from
293          */
294         void RemoveMode(User* user);
295
296         /**
297          * When a MODETYPE_CHANNEL mode handler is being removed, the server will call this method for every channel on the server.
298          * The mode handler has to populate the given modestacker with mode changes that remove the mode from the channel.
299          * The default implementation of this method can remove all kinds of channel modes except listmodes.
300          * In the case of listmodes, the entire list of items must be added to the modestacker (which is handled by ListModeBase,
301          * so if you inherit from it or your mode can be removed by the default implementation then you do not have to implement
302          * this function).
303          * @param channel The channel which the server wants to remove your mode from
304          * @param stack The mode stack to add the mode change to
305          */
306         virtual void RemoveMode(Channel* channel, irc::modestacker& stack);
307
308         inline unsigned int GetLevelRequired() const { return levelrequired; }
309 };
310
311 /**
312  * Prefix modes are channel modes that grant a specific rank to members having prefix mode set.
313  * They require a parameter when setting and unsetting; the parameter is always a member of the channel.
314  * A prefix mode may be set on any number of members on a channel, but for a given member a given prefix
315  * mode is either set or not set, in other words members cannot have the same prefix mode set more than once.
316  *
317  * A rank of a member is defined as the rank given by the 'strongest' prefix mode that member has.
318  * Other parts of the IRCd use this rank to determine whether a channel action is allowable for a user or not.
319  * The rank of a prefix mode is constant, i.e. the same rank value is given to all users having that prefix mode set.
320  *
321  * Note that it is possible that the same action requires a different rank on a different channel;
322  * for example changing the topic on a channel having +t set requires a rank that is >= than the rank of a halfop,
323  * but there is no such restriction when +t isn't set.
324  */
325 class CoreExport PrefixMode : public ModeHandler
326 {
327  protected:
328         /** The prefix character granted by this mode. '@' for op, '+' for voice, etc.
329          * If 0, this mode does not have a visible prefix character.
330          */
331         char prefix;
332
333         /** The prefix rank of this mode, used to compare prefix
334          * modes
335          */
336         unsigned int prefixrank;
337
338  public:
339         /**
340          * Constructor
341          * @param Creator The module creating this mode
342          * @param Name The user-friendly one word name of the prefix mode, e.g.: "op", "voice"
343          * @param ModeLetter The mode letter of this mode
344          */
345         PrefixMode(Module* Creator, const std::string& Name, char ModeLetter);
346
347         /**
348          * Handles setting and unsetting the prefix mode.
349          * Finds the given member of the given channel, if it's not found an error message is sent to 'source'
350          * and MODEACTION_DENY is returned. Otherwise the mode change is attempted.
351          * @param source Source of the mode change, an error message is sent to this user if the target is not found
352          * @param dest Unused
353          * @param channel The channel the mode change is happening on
354          * @param param The nickname or uuid of the target user
355          * @param adding True when the mode is being set, false when it is being unset
356          * @return MODEACTION_ALLOW if the change happened, MODEACTION_DENY if no change happened
357          * The latter occurs either when the member cannot be found or when the member already has this prefix set
358          * (when setting) or doesn't have this prefix set (when unsetting).
359          */
360         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding);
361
362         /**
363          * Removes this prefix mode from all users on the given channel
364          * @param chan The channel which the server wants to remove your mode from
365          * @param stack The mode stack to add the mode change to
366          */
367         void RemoveMode(Channel* chan, irc::modestacker& stack);
368
369         /**
370          * Mode prefix or 0. If this is defined, you should
371          * also implement GetPrefixRank() to return an integer
372          * value for this mode prefix.
373          */
374         char GetPrefix() const { return prefix; }
375
376         /**
377          * Get the 'value' of this modes prefix.
378          * determines which to display when there are multiple.
379          * The mode with the highest value is ranked first. See the
380          * PrefixModeValue enum and Channel::GetPrefixValue() for
381          * more information.
382          */
383         unsigned int GetPrefixRank() const { return prefixrank; }
384 };
385
386 /** A prebuilt mode handler which handles a simple user mode, e.g. no parameters, usable by any user, with no extra
387  * behaviour to the mode beyond the basic setting and unsetting of the mode, not allowing the mode to be set if it
388  * is already set and not allowing it to be unset if it is already unset.
389  * An example of a simple user mode is user mode +w.
390  */
391 class CoreExport SimpleUserModeHandler : public ModeHandler
392 {
393  public:
394         SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter)
395                 : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) {}
396         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
397 };
398
399 /** A prebuilt mode handler which handles a simple channel mode, e.g. no parameters, usable by any user, with no extra
400  * behaviour to the mode beyond the basic setting and unsetting of the mode, not allowing the mode to be set if it
401  * is already set and not allowing it to be unset if it is already unset.
402  * An example of a simple channel mode is channel mode +s.
403  */
404 class CoreExport SimpleChannelModeHandler : public ModeHandler
405 {
406  public:
407         SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
408                 : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) {}
409         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
410 };
411
412 class CoreExport ParamChannelModeHandler : public ModeHandler
413 {
414  public:
415         ParamChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
416                 : ModeHandler(Creator, Name, modeletter, PARAM_SETONLY, MODETYPE_CHANNEL) {}
417         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
418         /** Validate the parameter - you may change the value to normalize it. Return true if it is valid. */
419         virtual bool ParamValidate(std::string& parameter);
420 };
421
422 /**
423  * The ModeWatcher class can be used to alter the behaviour of a mode implemented
424  * by the core or by another module. To use ModeWatcher, derive a class from it,
425  * and attach it to the mode using Server::AddModeWatcher and Server::DelModeWatcher.
426  * A ModeWatcher will be called both before and after the mode change.
427  */
428 class CoreExport ModeWatcher : public classbase
429 {
430  private:
431         /**
432          * The mode name this class is watching
433          */
434         const std::string mode;
435
436         /**
437          * The mode type being watched (user or channel)
438          */
439         ModeType m_type;
440
441  public:
442         ModuleRef creator;
443         /**
444          * The constructor initializes the mode and the mode type
445          */
446         ModeWatcher(Module* creator, const std::string& modename, ModeType type);
447         /**
448          * The default destructor does nothing.
449          */
450         virtual ~ModeWatcher();
451
452         /**
453          * Get the mode name being watched
454          * @return The mode name being watched
455          */
456         const std::string& GetModeName() const { return mode; }
457
458         /**
459          * Get the mode type being watched
460          * @return The mode type being watched (user or channel)
461          */
462         ModeType GetModeType();
463
464         /**
465          * Before the mode character is processed by its handler, this method will be called.
466          * @param source The sender of the mode
467          * @param dest The target user for the mode, if you are watching a user mode
468          * @param channel The target channel for the mode, if you are watching a channel mode
469          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
470          * If you alter the parameter you are given, the mode handler will see your atered version
471          * when it handles the mode.
472          * @param adding True if the mode is being added and false if it is being removed
473          * @return True to allow the mode change to go ahead, false to abort it. If you abort the
474          * change, the mode handler (and ModeWatcher::AfterMode()) will never see the mode change.
475          */
476         virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
477         /**
478          * After the mode character has been processed by the ModeHandler, this method will be called.
479          * @param source The sender of the mode
480          * @param dest The target user for the mode, if you are watching a user mode
481          * @param channel The target channel for the mode, if you are watching a channel mode
482          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
483          * You cannot alter the parameter here, as the mode handler has already processed it.
484          * @param adding True if the mode is being added and false if it is being removed
485          */
486         virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
487 };
488
489 typedef std::multimap<std::string, ModeWatcher*>::iterator ModeWatchIter;
490
491 /** The mode parser handles routing of modes and handling of mode strings.
492  * It marshalls, controls and maintains both ModeWatcher and ModeHandler classes,
493  * parses client to server MODE strings for user and channel modes, and performs
494  * processing for the 004 mode list numeric, amongst other things.
495  */
496 class CoreExport ModeParser
497 {
498  private:
499         /** Mode handlers for each mode, to access a handler subtract
500          * 65 from the ascii value of the mode letter.
501          * The upper bit of the value indicates if its a usermode
502          * or a channel mode, so we have 256 of them not 64.
503          */
504         ModeHandler* modehandlers[256];
505
506         /** Lists of mode handlers by type
507          */
508         struct
509         {
510                 /** List of mode handlers that inherit from ListModeBase
511                  */
512                 std::vector<ListModeBase*> list;
513
514                 /** List of mode handlers that inherit from PrefixMode
515                  */
516                 std::vector<PrefixMode*> prefix;
517         } mhlist;
518
519         /** Mode watcher classes
520          */
521         std::multimap<std::string, ModeWatcher*> modewatchermap;
522
523         /** Displays the current modes of a channel or user.
524          * Used by ModeParser::Process.
525          */
526         void DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text);
527         /** Displays the value of a list mode
528          * Used by ModeParser::Process.
529          */
530         void DisplayListModes(User* user, Channel* chan, std::string &mode_sequence);
531
532         /**
533          * Attempts to apply a mode change to a user or channel
534          */
535         ModeAction TryMode(User* user, User* targu, Channel* targc, bool adding, unsigned char mode, std::string &param, bool SkipACL);
536
537         /** Returns a list of user or channel mode characters.
538          * Used for constructing the parts of the mode list in the 004 numeric.
539          * @param mt Controls whether to list user modes or channel modes
540          * @param needparam Return modes only if they require a parameter to be set
541          * @return The available mode letters that satisfy the given conditions
542          */
543         std::string CreateModeList(ModeType mt, bool needparam = false);
544
545         /** Recreate the cached mode list that is displayed in the 004 numeric
546          * in Cached004ModeList.
547          * Called when a mode handler is added or removed.
548          */
549         void RecreateModeListFor004Numeric();
550
551         /** The string representing the last set of modes to be parsed.
552          * Use GetLastParse() to get this value, to be used for  display purposes.
553          */
554         std::string LastParse;
555         std::vector<std::string> LastParseParams;
556         std::vector<TranslateType> LastParseTranslate;
557
558         unsigned int sent[256];
559
560         unsigned int seq;
561
562         /** Cached mode list for use in 004 numeric
563          */
564         std::string Cached004ModeList;
565
566  public:
567         typedef std::vector<ListModeBase*> ListModeList;
568         typedef std::vector<PrefixMode*> PrefixModeList;
569
570         typedef unsigned int ModeProcessFlag;
571         enum ModeProcessFlags
572         {
573                 /** If only this flag is specified, the mode change will be global
574                  * and parameter modes will have their parameters explicitly set
575                  * (not merged). This is the default.
576                  */
577                 MODE_NONE = 0,
578
579                 /** If this flag is set then the parameters of non-listmodes will be
580                  * merged according to their conflict resolution rules.
581                  * Does not affect user modes, channel modes without a parameter and
582                  * listmodes.
583                  */
584                 MODE_MERGE = 1,
585
586                 /** If this flag is set then the mode change won't be handed over to
587                  * the linking module to be sent to other servers, but will be processed
588                  * locally and sent to local user(s) as usual.
589                  */
590                 MODE_LOCALONLY = 2
591         };
592
593         ModeParser();
594         ~ModeParser();
595
596         /** Initialize all built-in modes
597          */
598         static void InitBuiltinModes();
599
600         /** Tidy a banmask. This makes a banmask 'acceptable' if fields are left out.
601          * E.g.
602          *
603          * nick -> nick!*@*
604          *
605          * nick!ident -> nick!ident@*
606          *
607          * host.name -> *!*\@host.name
608          *
609          * ident@host.name -> *!ident\@host.name
610          *
611          * This method can be used on both IPV4 and IPV6 user masks.
612          */
613         static void CleanMask(std::string &mask);
614         /** Get the last string to be processed, as it was sent to the user or channel.
615          * Use this to display a string you just sent to be parsed, as the actual output
616          * may be different to what you sent after it has been 'cleaned up' by the parser.
617          * @return Last parsed string, as seen by users.
618          */
619         const std::string& GetLastParse() const { return LastParse; }
620         const std::vector<std::string>& GetLastParseParams() { return LastParseParams; }
621         const std::vector<TranslateType>& GetLastParseTranslate() { return LastParseTranslate; }
622         /** Add a mode to the mode parser.
623          * @return True if the mode was successfully added.
624          */
625         bool AddMode(ModeHandler* mh);
626         /** Delete a mode from the mode parser.
627          * When a mode is deleted, the mode handler will be called
628          * for every user (if it is a user mode) or for every  channel
629          * (if it is a channel mode) to unset the mode on all objects.
630          * This prevents modes staying in the system which no longer exist.
631          * @param mh The mode handler to remove
632          * @return True if the mode was successfully removed.
633          */
634         bool DelMode(ModeHandler* mh);
635
636         /** Add a mode watcher.
637          * A mode watcher is triggered before and after a mode handler is
638          * triggered. See the documentation of class ModeWatcher for more
639          * information.
640          * @param mw The ModeWatcher you want to add
641          */
642         void AddModeWatcher(ModeWatcher* mw);
643
644         /** Delete a mode watcher.
645          * A mode watcher is triggered before and after a mode handler is
646          * triggered. See the documentation of class ModeWatcher for more
647          * information.
648          * @param mw The ModeWatcher you want to delete
649          * @return True if the ModeWatcher was deleted correctly
650          */
651         bool DelModeWatcher(ModeWatcher* mw);
652         /** Process a set of mode changes from a server or user.
653          * @param parameters The parameters of the mode change, in the format
654          * they would be from a MODE command.
655          * @param user The source of the mode change, can be a server user.
656          * @param flags Optional flags controlling how the mode change is processed,
657          * defaults to MODE_NONE.
658          */
659         void Process(const std::vector<std::string>& parameters, User* user, ModeProcessFlag flags = MODE_NONE);
660
661         /** Find the mode handler for a given mode and type.
662          * @param modeletter mode letter to search for
663          * @param mt type of mode to search for, user or channel
664          * @returns a pointer to a ModeHandler class, or NULL of there isnt a handler for the given mode
665          */
666         ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
667
668         /** Find the mode handler for the given prefix mode
669          * @param modeletter The mode letter to search for
670          * @return A pointer to the PrefixMode or NULL if the mode wasn't found or it isn't a prefix mode
671          */
672         PrefixMode* FindPrefixMode(unsigned char modeletter);
673
674         /** Find a mode handler by its prefix.
675          * If there is no mode handler with the given prefix, NULL will be returned.
676          * @param pfxletter The prefix to find, e.g. '@'
677          * @return The mode handler which handles this prefix, or NULL if there is none.
678          */
679         PrefixMode* FindPrefix(unsigned const char pfxletter);
680
681         /** Returns a list of modes, space seperated by type:
682          * 1. User modes
683          * 2. Channel modes
684          * 3. Channel modes that require a parameter when set
685          * This is sent to users as the last part of the 004 numeric
686          */
687         const std::string& GetModeListFor004Numeric();
688
689         /** Generates a list of modes, comma seperated by type:
690          *  1; Listmodes EXCEPT those with a prefix
691          *  2; Modes that take a param when adding or removing
692          *  3; Modes that only take a param when adding
693          *  4; Modes that dont take a param
694          */
695         std::string GiveModeList(ModeMasks m);
696
697         /** This returns the PREFIX=(ohv)@%+ section of the 005 numeric, or
698          * just the "@%+" part if the parameter false
699          */
700         std::string BuildPrefixes(bool lettersAndModes = true);
701
702         /** Get a list of all mode handlers that inherit from ListModeBase
703          * @return A list containing ListModeBase modes
704          */
705         const ListModeList& GetListModes() const { return mhlist.list; }
706
707         /** Get a list of all prefix modes
708          * @return A list containing all prefix modes
709          */
710         const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
711 };
712
713 inline const std::string& ModeParser::GetModeListFor004Numeric()
714 {
715         return Cached004ModeList;
716 }
717
718 inline PrefixMode* ModeHandler::IsPrefixMode()
719 {
720         return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
721 }
722
723 inline ListModeBase* ModeHandler::IsListModeBase()
724 {
725         return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
726 }
727
728 inline ParamModeBase* ModeHandler::IsParameterMode()
729 {
730         return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
731 }