]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/mode.h
Improve behaviour when running as root.
[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 #include "modechange.h"
27
28 /**
29  * Holds the values for different type of modes
30  * that can exist, USER or CHANNEL type.
31  */
32 enum ModeType
33 {
34         /** User mode */
35         MODETYPE_USER = 0,
36         /** Channel mode */
37         MODETYPE_CHANNEL = 1
38 };
39
40 /**
41  * Holds mode actions - modes can be allowed or denied.
42  */
43 enum ModeAction
44 {
45         MODEACTION_DENY = 0, /* Drop the mode change, AND a parameter if its a parameterized mode */
46         MODEACTION_ALLOW = 1 /* Allow the 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 enum ParamSpec
68 {
69         /** No parameters */
70         PARAM_NONE,
71         /** Parameter required on mode setting only */
72         PARAM_SETONLY,
73         /** Parameter always required */
74         PARAM_ALWAYS
75 };
76
77 class PrefixMode;
78 class ListModeBase;
79 class ParamModeBase;
80
81 /** Each mode is implemented by ONE ModeHandler class.
82  * You must derive ModeHandler and add the child class to
83  * the list of modes handled by the ircd, using
84  * ModeParser::AddMode. When the mode you implement is
85  * set by a user, the virtual function OnModeChange is
86  * called. If you specify a value greater than 0 for
87  * parameters_on or parameters_off, then when the mode is
88  * set or unset respectively, std::string &parameter will
89  * contain the parameter given by the user, else it will
90  * contain an empty string. You may alter this parameter
91  * string, and if you alter it to an empty string, and your
92  * mode is expected to have a parameter, then this is
93  * equivalent to returning MODEACTION_DENY.
94  */
95 class CoreExport ModeHandler : public ServiceProvider
96 {
97  public:
98         typedef size_t Id;
99
100         enum Class
101         {
102                 MC_PREFIX,
103                 MC_LIST,
104                 MC_PARAM,
105                 MC_OTHER
106         };
107
108  private:
109         /** The opaque id of this mode assigned by the mode parser
110          */
111         Id modeid;
112
113  protected:
114         /** What kind of parameters does the mode take?
115          */
116         ParamSpec parameters_taken;
117
118         /**
119          * The mode letter you're implementing.
120          */
121         char mode;
122
123         /**
124          * True if the mode requires oper status
125          * to set.
126          */
127         bool oper;
128
129         /**
130          * Mode is a 'list' mode. The behaviour
131          * of your mode is now set entirely within
132          * the class as of the 1.1 api, rather than
133          * inside the mode parser as in the 1.0 api,
134          * so the only use of this value (along with
135          * IsListMode()) is for the core to determine
136          * wether your module can produce 'lists' or not
137          * (e.g. banlists, etc)
138          */
139         bool list;
140
141         /**
142          * The mode type, either MODETYPE_USER or
143          * MODETYPE_CHANNEL.
144          */
145         ModeType m_type;
146
147         /** The object type of this mode handler
148          */
149         const Class type_id;
150
151         /** The prefix rank required to set this mode on channels. */
152         unsigned int ranktoset;
153
154         /** The prefix rank required to unset this mode on channels. */
155         unsigned int ranktounset;
156
157         /** If non-empty then the syntax of the parameter for this mode. */
158         std::string syntax;
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         CullResult cull() CXX11_OVERRIDE;
174         virtual ~ModeHandler();
175
176         /** Register this object in the ModeParser
177          */
178         void RegisterService() CXX11_OVERRIDE;
179
180         /**
181          * Returns true if the mode is a list mode
182          */
183         bool IsListMode() const { return list; }
184
185         /**
186          * Check whether this mode is a prefix mode
187          * @return non-NULL if this mode is a prefix mode, NULL otherwise
188          */
189         PrefixMode* IsPrefixMode();
190
191         /**
192          * Check whether this mode is a prefix mode
193          * @return non-NULL if this mode is a prefix mode, NULL otherwise
194          */
195         const PrefixMode* IsPrefixMode() const;
196
197         /**
198          * Check whether this mode handler inherits from ListModeBase
199          * @return non-NULL if this mode handler inherits from ListModeBase, NULL otherwise
200          */
201         ListModeBase* IsListModeBase();
202
203         /**
204          * Check whether this mode handler inherits from ListModeBase
205          * @return non-NULL if this mode handler inherits from ListModeBase, NULL otherwise
206          */
207         const ListModeBase* IsListModeBase() const;
208
209         /**
210          * Check whether this mode handler inherits from ParamModeBase
211          * @return non-NULL if this mode handler inherits from ParamModeBase, NULL otherwise
212          */
213         ParamModeBase* IsParameterMode();
214
215         /**
216          * Check whether this mode handler inherits from ParamModeBase
217          * @return non-NULL if this mode handler inherits from ParamModeBase, NULL otherwise
218          */
219         const ParamModeBase* IsParameterMode() const;
220
221         /**
222          * Returns the mode's type
223          */
224         inline ModeType GetModeType() const { return m_type; }
225         /**
226          * Returns true if the mode can only be set/unset by an oper
227          */
228         inline bool NeedsOper() const { return oper; }
229         /**
230          * Check if the mode needs a parameter for adding or removing
231          * @param adding True to check if the mode needs a parameter when setting, false to check if the mode needs a parameter when unsetting
232          * @return True if the mode needs a parameter for the specified action, false if it doesn't
233          */
234         bool NeedsParam(bool adding) const;
235         /**
236          * Returns the mode character this handler handles.
237          * @return The mode character
238          */
239         char GetModeChar() const { return mode; }
240
241         /** Return the id of this mode which is used in User::modes and
242          * Channel::modes as the index to determine whether a mode is set.
243          */
244         Id GetId() const { return modeid; }
245
246         /** For user modes, return the current parameter, if any
247          */
248         virtual std::string GetUserParameter(const User* user) const;
249
250         /**
251          * Called when a channel mode change access check for your mode occurs.
252          * @param source Contains the user setting the mode.
253          * @param channel contains the destination channel the modes are being set on.
254          * @param parameter The parameter for your mode. This is modifiable.
255          * @param adding This value is true when the mode is being set, or false when it is being unset.
256          * @return allow, deny, or passthru to check against the required level
257          */
258         virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
259
260         /**
261          * Called when a mode change for your mode occurs.
262          * @param source Contains the user setting the mode.
263          * @param dest For usermodes, contains the destination user the mode is being set on. For channelmodes, this is an undefined value.
264          * @param channel For channel modes, contains the destination channel the modes are being set on. For usermodes, this is an undefined value.
265          * @param parameter The parameter for your mode, if you indicated that your mode requires a parameter when being set or unset. Note that
266          * 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
267          * but you specified your mode REQUIRES a parameter, this is equivalent to returning MODEACTION_DENY and will prevent the mode from being
268          * displayed.
269          * @param adding This value is true when the mode is being set, or false when it is being unset.
270          * @return MODEACTION_ALLOW to allow the mode, or MODEACTION_DENY to prevent the mode, also see the description of 'parameter'.
271          */
272         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
273         /**
274          * If your mode is a listmode, then this method will be called for displaying an item list, e.g. on MODE \#channel +modechar
275          * without any parameter or other modes in the command.
276          * @param user The user issuing the command
277          * @param channel The channel they're requesting an item list of (e.g. a banlist, or an exception list etc)
278          */
279         virtual void DisplayList(User* user, Channel* channel);
280
281         /** In the event that the mode should be given a parameter, and no parameter was provided, this method is called.
282          * This allows you to give special information to the user, or handle this any way you like.
283          * @param user The user issuing the mode change
284          * @param dest For user mode changes, the target of the mode. For channel mode changes, NULL.
285          * @param channel For channel mode changes, the target of the mode. For user mode changes, NULL.
286          */
287         virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
288
289         /**
290          * If your mode is a listmode, this method will be called to display an empty list (just the end of list numeric)
291          * @param user The user issuing the command
292          * @param channel The channel tehy're requesting an item list of (e.g. a banlist, or an exception list etc)
293          */
294         virtual void DisplayEmptyList(User* user, Channel* channel);
295
296         /**
297          * If your mode needs special action during a server sync to determine which side wins when comparing timestamps,
298          * override this function and use it to return true or false. The default implementation just returns true if
299          * theirs < ours. This will only be called for non-listmodes with parameters, when adding the mode and where
300          * theirs == ours (therefore the default implementation will always return false).
301          * @param their_param Their parameter if the mode has a parameter
302          * @param our_param Our parameter if the mode has a parameter
303          * @param channel The channel we are checking against
304          * @return True if the other side wins the merge, false if we win the merge for this mode.
305          */
306         virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
307
308         /**
309          * When a MODETYPE_USER mode handler is being removed, the core will call this method for every user on the server.
310          * The usermode will be removed using the appropiate server mode using InspIRCd::SendMode().
311          * @param user The user which the server wants to remove your mode from
312          */
313         void RemoveMode(User* user);
314
315         /**
316          * When a MODETYPE_CHANNEL mode handler is being removed, the server will call this method for every channel on the server.
317          * The mode handler has to populate the given modestacker with mode changes that remove the mode from the channel.
318          * The default implementation of this method can remove all kinds of channel modes except listmodes.
319          * In the case of listmodes, the entire list of items must be added to the modestacker (which is handled by ListModeBase,
320          * so if you inherit from it or your mode can be removed by the default implementation then you do not have to implement
321          * this function).
322          * @param channel The channel which the server wants to remove your mode from
323          * @param changelist Mode change list to populate with the removal of this mode
324          */
325         virtual void RemoveMode(Channel* channel, Modes::ChangeList& changelist);
326
327         /** Retrieves the level required to modify this mode.
328          * @param adding Whether the mode is being added or removed.
329          */
330         inline unsigned int GetLevelRequired(bool adding) const
331         {
332                 return adding ? ranktoset : ranktounset;
333         }
334
335         /** Retrieves the syntax of the parameter for this mode. */
336         const std::string& GetSyntax() const { return syntax; }
337
338         friend class ModeParser;
339 };
340
341 /**
342  * Prefix modes are channel modes that grant a specific rank to members having prefix mode set.
343  * They require a parameter when setting and unsetting; the parameter is always a member of the channel.
344  * A prefix mode may be set on any number of members on a channel, but for a given member a given prefix
345  * mode is either set or not set, in other words members cannot have the same prefix mode set more than once.
346  *
347  * A rank of a member is defined as the rank given by the 'strongest' prefix mode that member has.
348  * Other parts of the IRCd use this rank to determine whether a channel action is allowable for a user or not.
349  * The rank of a prefix mode is constant, i.e. the same rank value is given to all users having that prefix mode set.
350  *
351  * Note that it is possible that the same action requires a different rank on a different channel;
352  * for example changing the topic on a channel having +t set requires a rank that is >= than the rank of a halfop,
353  * but there is no such restriction when +t isn't set.
354  */
355 class CoreExport PrefixMode : public ModeHandler
356 {
357  protected:
358         /** The prefix character granted by this mode. '@' for op, '+' for voice, etc.
359          * If 0, this mode does not have a visible prefix character.
360          */
361         char prefix;
362
363         /** The prefix rank of this mode, used to compare prefix
364          * modes
365          */
366         unsigned int prefixrank;
367
368         /** Whether a client with this prefix can remove it from themself. */
369         bool selfremove;
370
371  public:
372         /**
373          * Constructor
374          * @param Creator The module creating this mode
375          * @param Name The user-friendly one word name of the prefix mode, e.g.: "op", "voice"
376          * @param ModeLetter The mode letter of this mode
377          * @param Rank Rank given by this prefix mode, see explanation above
378          * @param PrefixChar Prefix character, or 0 if the mode has no prefix character
379          */
380         PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank = 0, char PrefixChar = 0);
381
382         /**
383          * Called when a channel mode change access check for your mode occurs.
384          * @param source Contains the user setting the mode.
385          * @param channel contains the destination channel the modes are being set on.
386          * @param parameter The parameter for your mode. This is modifiable.
387          * @param adding This value is true when the mode is being set, or false when it is being unset.
388          * @return allow, deny, or passthru to check against the required level
389          */
390         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE;
391
392         /**
393          * Handles setting and unsetting the prefix mode.
394          * Finds the given member of the given channel, if it's not found an error message is sent to 'source'
395          * and MODEACTION_DENY is returned. Otherwise the mode change is attempted.
396          * @param source Source of the mode change, an error message is sent to this user if the target is not found
397          * @param dest Unused
398          * @param channel The channel the mode change is happening on
399          * @param param The nickname or uuid of the target user
400          * @param adding True when the mode is being set, false when it is being unset
401          * @return MODEACTION_ALLOW if the change happened, MODEACTION_DENY if no change happened
402          * The latter occurs either when the member cannot be found or when the member already has this prefix set
403          * (when setting) or doesn't have this prefix set (when unsetting).
404          */
405         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE;
406
407         /**
408          * Updates the configuration of this prefix.
409          * @param rank The prefix rank of this mode.
410          * @param setrank The prefix rank required to set this mode on channels.
411          * @param unsetrank The prefix rank required to set this unmode on channels.
412          * @param selfrm Whether a client with this prefix can remove it from themself.
413          */
414         void Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm);
415
416         /**
417          * Removes this prefix mode from all users on the given channel
418          * @param channel The channel which the server wants to remove your mode from
419          * @param changelist Mode change list to populate with the removal of this mode
420          */
421         void RemoveMode(Channel* channel, Modes::ChangeList& changelist) CXX11_OVERRIDE;
422
423
424         /**
425         * Determines whether a user with this prefix mode can remove it.
426         */
427         bool CanSelfRemove() const { return selfremove; }
428
429         /**
430          * Mode prefix or 0. If this is defined, you should
431          * also implement GetPrefixRank() to return an integer
432          * value for this mode prefix.
433          */
434         char GetPrefix() const { return prefix; }
435
436         /**
437          * Get the 'value' of this modes prefix.
438          * determines which to display when there are multiple.
439          * The mode with the highest value is ranked first. See the
440          * PrefixModeValue enum and Channel::GetPrefixValue() for
441          * more information.
442          */
443         unsigned int GetPrefixRank() const { return prefixrank; }
444 };
445
446 /** A prebuilt mode handler which handles a simple user mode, e.g. no parameters, usable by any user, with no extra
447  * behaviour to the mode beyond the basic setting and unsetting of the mode, not allowing the mode to be set if it
448  * is already set and not allowing it to be unset if it is already unset.
449  * An example of a simple user mode is user mode +w.
450  */
451 class CoreExport SimpleUserModeHandler : public ModeHandler
452 {
453  public:
454         SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter)
455                 : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) {}
456         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
457 };
458
459 /** A prebuilt mode handler which handles a simple channel mode, e.g. no parameters, usable by any user, with no extra
460  * behaviour to the mode beyond the basic setting and unsetting of the mode, not allowing the mode to be set if it
461  * is already set and not allowing it to be unset if it is already unset.
462  * An example of a simple channel mode is channel mode +s.
463  */
464 class CoreExport SimpleChannelModeHandler : public ModeHandler
465 {
466  public:
467         SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
468                 : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) {}
469         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
470 };
471
472 /**
473  * The ModeWatcher class can be used to alter the behaviour of a mode implemented
474  * by the core or by another module. To use ModeWatcher, derive a class from it,
475  * and attach it to the mode using Server::AddModeWatcher and Server::DelModeWatcher.
476  * A ModeWatcher will be called both before and after the mode change.
477  */
478 class CoreExport ModeWatcher : public classbase
479 {
480  private:
481         /**
482          * The mode name this class is watching
483          */
484         const std::string mode;
485
486         /**
487          * The mode type being watched (user or channel)
488          */
489         ModeType m_type;
490
491  public:
492         ModuleRef creator;
493         /**
494          * The constructor initializes the mode and the mode type
495          */
496         ModeWatcher(Module* creator, const std::string& modename, ModeType type);
497         /**
498          * The default destructor does nothing.
499          */
500         virtual ~ModeWatcher();
501
502         /**
503          * Get the mode name being watched
504          * @return The mode name being watched
505          */
506         const std::string& GetModeName() const { return mode; }
507
508         /**
509          * Get the mode type being watched
510          * @return The mode type being watched (user or channel)
511          */
512         ModeType GetModeType() const { return m_type; }
513
514         /**
515          * Before the mode character is processed by its handler, this method will be called.
516          * @param source The sender of the mode
517          * @param dest The target user for the mode, if you are watching a user mode
518          * @param channel The target channel for the mode, if you are watching a channel mode
519          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
520          * If you alter the parameter you are given, the mode handler will see your atered version
521          * when it handles the mode.
522          * @param adding True if the mode is being added and false if it is being removed
523          * @return True to allow the mode change to go ahead, false to abort it. If you abort the
524          * change, the mode handler (and ModeWatcher::AfterMode()) will never see the mode change.
525          */
526         virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
527         /**
528          * After the mode character has been processed by the ModeHandler, this method will be called.
529          * @param source The sender of the mode
530          * @param dest The target user for the mode, if you are watching a user mode
531          * @param channel The target channel for the mode, if you are watching a channel mode
532          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
533          * You cannot alter the parameter here, as the mode handler has already processed it.
534          * @param adding True if the mode is being added and false if it is being removed
535          */
536         virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
537 };
538
539 /** The mode parser handles routing of modes and handling of mode strings.
540  * It marshalls, controls and maintains both ModeWatcher and ModeHandler classes,
541  * parses client to server MODE strings for user and channel modes, and performs
542  * processing for the 004 mode list numeric, amongst other things.
543  */
544 class CoreExport ModeParser : public fakederef<ModeParser>
545 {
546  public:
547         /** The maximum number of modes which can be created. */
548         static const ModeHandler::Id MODEID_MAX = 64;
549
550         /** The maximum length of a mode parameter. */
551         static const size_t MODE_PARAM_MAX = 250;
552
553         /** Type of the container that maps mode names to ModeHandlers
554          */
555         typedef TR1NS::unordered_map<std::string, ModeHandler*, irc::insensitive, irc::StrHashComp> ModeHandlerMap;
556
557  private:
558         /** Type of the container that maps mode names to ModeWatchers
559          */
560         typedef insp::flat_multimap<std::string, ModeWatcher*> ModeWatcherMap;
561
562         /** Last item in the ModeType enum
563          */
564         static const unsigned int MODETYPE_LAST = 2;
565
566         /** Mode handlers for each mode, to access a handler subtract
567          * 65 from the ascii value of the mode letter.
568          * The upper bit of the value indicates if its a usermode
569          * or a channel mode, so we have 256 of them not 64.
570          */
571         ModeHandler* modehandlers[MODETYPE_LAST][128];
572
573         /** An array of mode handlers indexed by the mode id
574          */
575         ModeHandler* modehandlersbyid[MODETYPE_LAST][MODEID_MAX];
576
577         /** A map of mode handlers keyed by their name
578          */
579         ModeHandlerMap modehandlersbyname[MODETYPE_LAST];
580
581         /** Lists of mode handlers by type
582          */
583         struct
584         {
585                 /** List of mode handlers that inherit from ListModeBase
586                  */
587                 std::vector<ListModeBase*> list;
588
589                 /** List of mode handlers that inherit from PrefixMode
590                  */
591                 std::vector<PrefixMode*> prefix;
592         } mhlist;
593
594         /** Mode watcher classes
595          */
596         ModeWatcherMap modewatchermap;
597
598         /** Last processed mode change
599          */
600         Modes::ChangeList LastChangeList;
601
602         /**
603          * Attempts to apply a mode change to a user or channel
604          */
605         ModeAction TryMode(User* user, User* targu, Channel* targc, Modes::Change& mcitem, bool SkipACL);
606
607         /** Allocates an unused id for the given mode type, throws a ModuleException if out of ids.
608          * @param mt The type of the mode to allocate the id for
609          * @return The id
610          */
611         ModeHandler::Id AllocateModeId(ModeType mt);
612
613  public:
614         typedef std::vector<ListModeBase*> ListModeList;
615         typedef std::vector<PrefixMode*> PrefixModeList;
616
617         typedef unsigned int ModeProcessFlag;
618         enum ModeProcessFlags
619         {
620                 /** If only this flag is specified, the mode change will be global
621                  * and parameter modes will have their parameters explicitly set
622                  * (not merged). This is the default.
623                  */
624                 MODE_NONE = 0,
625
626                 /** If this flag is set then the parameters of non-listmodes will be
627                  * merged according to their conflict resolution rules.
628                  * Does not affect user modes, channel modes without a parameter and
629                  * listmodes.
630                  */
631                 MODE_MERGE = 1,
632
633                 /** If this flag is set then the linking module will ignore the mode change
634                  * and not send it to other servers. The mode change will be processed
635                  * locally and sent to local user(s) as usual.
636                  */
637                 MODE_LOCALONLY = 2,
638
639                 /** If this flag is set then the mode change will be subject to access checks.
640                  * For more information see the documentation of the PrefixMode class,
641                  * ModeHandler::ranktoset and ModeHandler::AccessCheck().
642                  * Modules may explicitly allow a mode change regardless of this flag by returning
643                  * MOD_RES_ALLOW from the OnPreMode hook. Only affects channel mode changes.
644                  */
645                 MODE_CHECKACCESS = 4
646         };
647
648         ModeParser();
649         ~ModeParser();
650
651         static bool IsModeChar(char chr);
652
653         /** Tidy a banmask. This makes a banmask 'acceptable' if fields are left out.
654          * E.g.
655          *
656          * nick -> nick!*@*
657          *
658          * nick!ident -> nick!ident@*
659          *
660          * host.name -> *!*\@host.name
661          *
662          * ident@host.name -> *!ident\@host.name
663          *
664          * This method can be used on both IPV4 and IPV6 user masks.
665          */
666         static void CleanMask(std::string &mask);
667
668         /** Gets the last mode change to be processed. */
669         const Modes::ChangeList& GetLastChangeList() const { return LastChangeList; }
670
671         /** Add a mode to the mode parser.
672          * Throws a ModuleException if the mode cannot be added.
673          */
674         void AddMode(ModeHandler* mh);
675
676         /** Delete a mode from the mode parser.
677          * When a mode is deleted, the mode handler will be called
678          * for every user (if it is a user mode) or for every  channel
679          * (if it is a channel mode) to unset the mode on all objects.
680          * This prevents modes staying in the system which no longer exist.
681          * @param mh The mode handler to remove
682          * @return True if the mode was successfully removed.
683          */
684         bool DelMode(ModeHandler* mh);
685
686         /** Add a mode watcher.
687          * A mode watcher is triggered before and after a mode handler is
688          * triggered. See the documentation of class ModeWatcher for more
689          * information.
690          * @param mw The ModeWatcher you want to add
691          */
692         void AddModeWatcher(ModeWatcher* mw);
693
694         /** Delete a mode watcher.
695          * A mode watcher is triggered before and after a mode handler is
696          * triggered. See the documentation of class ModeWatcher for more
697          * information.
698          * @param mw The ModeWatcher you want to delete
699          * @return True if the ModeWatcher was deleted correctly
700          */
701         bool DelModeWatcher(ModeWatcher* mw);
702
703         /** Process a list of mode changes entirely. If the mode changes do not fit into one MODE line
704          * then multiple MODE lines are generated.
705          * @param user The source of the mode change, can be a server user.
706          * @param targetchannel Channel to apply the mode change on. NULL if changing modes on a channel.
707          * @param targetuser User to apply the mode change on. NULL if changing modes on a user.
708          * @param changelist Modes to change in form of a Modes::ChangeList.
709          * @param flags Optional flags controlling how the mode change is processed,
710          * defaults to MODE_NONE.
711          */
712         void Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE);
713
714         /** Process a single MODE line's worth of mode changes, taking max modes and line length limits
715          * into consideration. Return value indicates how many modes were processed.
716          * @param user The source of the mode change, can be a server user.
717          * @param targetchannel Channel to apply the mode change on. NULL if changing modes on a channel.
718          * @param targetuser User to apply the mode change on. NULL if changing modes on a user.
719          * @param changelist Modes to change in form of a Modes::ChangeList. May not process
720          * the entire list due to MODE line length and max modes limitations.
721          * @param flags Optional flags controlling how the mode change is processed,
722          * defaults to MODE_NONE.
723          * @param beginindex Index of the first element in changelist to process. Mode changes before
724          * the element with this index are ignored.
725          * @return Number of mode changes processed from changelist.
726          */
727         unsigned int ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags = MODE_NONE, unsigned int beginindex = 0);
728
729         /** Turn a list of parameters compatible with the format of the MODE command into
730          * Modes::ChangeList form. All modes are processed, regardless of max modes. Unknown modes
731          * are skipped.
732          * @param user The source of the mode change, can be a server user. Error numerics are sent to
733          * this user.
734          * @param type MODETYPE_USER if this is a user mode change or MODETYPE_CHANNEL if this
735          * is a channel mode change.
736          * @param parameters List of strings describing the mode change to convert to a ChangeList.
737          * Must be using the same format as the parameters of a MODE command.
738          * @param changelist ChangeList object to populate.
739          * @param beginindex Index of the first element that is part of the MODE list in the parameters
740          * container. Defaults to 1.
741          * @param endindex Index of the first element that is not part of the MODE list. By default,
742          * the entire container is considered part of the MODE list.
743          */
744         void ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex = 1, unsigned int endindex = UINT_MAX);
745
746         /** Find the mode handler for a given mode name and type.
747          * @param modename The mode name to search for.
748          * @param mt Type of mode to search for, user or channel.
749          * @return A pointer to a ModeHandler class, or NULL of there isn't a handler for the given mode name.
750          */
751         ModeHandler* FindMode(const std::string& modename, ModeType mt);
752
753         /** Find the mode handler for a given mode and type.
754          * @param modeletter mode letter to search for
755          * @param mt type of mode to search for, user or channel
756          * @returns a pointer to a ModeHandler class, or NULL of there isnt a handler for the given mode
757          */
758         ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
759
760         /** Find the mode handler for the given prefix mode
761          * @param modeletter The mode letter to search for
762          * @return A pointer to the PrefixMode or NULL if the mode wasn't found or it isn't a prefix mode
763          */
764         PrefixMode* FindPrefixMode(unsigned char modeletter);
765
766         /** Find a mode handler by its prefix.
767          * If there is no mode handler with the given prefix, NULL will be returned.
768          * @param pfxletter The prefix to find, e.g. '@'
769          * @return The mode handler which handles this prefix, or NULL if there is none.
770          */
771         PrefixMode* FindPrefix(unsigned const char pfxletter);
772
773         /** Generates a list of modes, comma seperated by type:
774          *  1; Listmodes EXCEPT those with a prefix
775          *  2; Modes that take a param when adding or removing
776          *  3; Modes that only take a param when adding
777          *  4; Modes that dont take a param
778          */
779         std::string GiveModeList(ModeType mt);
780
781         /** This returns the PREFIX=(ohv)@%+ section of the 005 numeric, or
782          * just the "@%+" part if the parameter false
783          */
784         std::string BuildPrefixes(bool lettersAndModes = true);
785
786         /** Get a list of all mode handlers that inherit from ListModeBase
787          * @return A list containing ListModeBase modes
788          */
789         const ListModeList& GetListModes() const { return mhlist.list; }
790
791         /** Get a list of all prefix modes
792          * @return A list containing all prefix modes
793          */
794         const PrefixModeList& GetPrefixModes() const { return mhlist.prefix; }
795
796         /** Get a mode name -> ModeHandler* map containing all modes of the given type
797          * @param mt Type of modes to return, MODETYPE_USER or MODETYPE_CHANNEL
798          * @return A map of mode handlers of the given type
799          */
800         const ModeHandlerMap& GetModes(ModeType mt) const { return modehandlersbyname[mt]; }
801
802         /** Show the list of a list mode to a user. Modules can deny the listing.
803          * @param user User to show the list to.
804          * @param chan Channel to show the list of.
805          * @param mh List mode to show the list of.
806          */
807         void ShowListModeList(User* user, Channel* chan, ModeHandler* mh);
808 };
809
810 inline PrefixMode* ModeHandler::IsPrefixMode()
811 {
812         return (this->type_id == MC_PREFIX ? static_cast<PrefixMode*>(this) : NULL);
813 }
814
815 inline const PrefixMode* ModeHandler::IsPrefixMode() const
816 {
817         return (this->type_id == MC_PREFIX ? static_cast<const PrefixMode*>(this) : NULL);
818 }
819
820 inline ListModeBase* ModeHandler::IsListModeBase()
821 {
822         return (this->type_id == MC_LIST ? reinterpret_cast<ListModeBase*>(this) : NULL);
823 }
824
825 inline const ListModeBase* ModeHandler::IsListModeBase() const
826 {
827         return (this->type_id == MC_LIST ? reinterpret_cast<const ListModeBase*>(this) : NULL);
828 }
829
830 inline ParamModeBase* ModeHandler::IsParameterMode()
831 {
832         return (this->type_id == MC_PARAM ? reinterpret_cast<ParamModeBase*>(this) : NULL);
833 }
834
835 inline const ParamModeBase* ModeHandler::IsParameterMode() const
836 {
837         return (this->type_id == MC_PARAM ? reinterpret_cast<const ParamModeBase*>(this) : NULL);
838 }