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