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