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