]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/mode.h
18fed8b0ab86bec9f897b790ff0b8e28b922c478
[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  public:
104         enum Class
105         {
106                 MC_OTHER
107         };
108
109         /**
110          * Removes this prefix mode from all users on the given channel
111          * @param channel The channel which the server wants to remove your mode from
112          * @param stack The mode stack to add the mode change to
113          */
114         void RemovePrefixMode(Channel* chan, irc::modestacker& stack);
115
116  protected:
117         /**
118          * The mode parameter translation type
119          */
120         TranslateType m_paramtype;
121
122         /** What kind of parameters does the mode take?
123          */
124         ParamSpec parameters_taken;
125
126         /**
127          * The mode letter you're implementing.
128          */
129         char mode;
130
131         /** Mode prefix, or 0
132          */
133         char prefix;
134
135         /**
136          * True if the mode requires oper status
137          * to set.
138          */
139         bool oper;
140
141         /**
142          * Mode is a 'list' mode. The behaviour
143          * of your mode is now set entirely within
144          * the class as of the 1.1 api, rather than
145          * inside the mode parser as in the 1.0 api,
146          * so the only use of this value (along with
147          * IsListMode()) is for the core to determine
148          * wether your module can produce 'lists' or not
149          * (e.g. banlists, etc)
150          */
151         bool list;
152
153         /**
154          * The mode type, either MODETYPE_USER or
155          * MODETYPE_CHANNEL.
156          */
157         ModeType m_type;
158
159         /** The object type of this mode handler
160          */
161         const Class type_id;
162
163         /** The prefix char needed on channel to use this mode,
164          * only checked for channel modes
165          */
166         int levelrequired;
167
168         /** The prefix rank of this mode, used to compare prefix
169          * modes
170          */
171         unsigned int prefixrank;
172
173  public:
174         /**
175          * The constructor for ModeHandler initalizes the mode handler.
176          * The constructor of any class you derive from ModeHandler should
177          * probably call this constructor with the parameters set correctly.
178          * @param me The module which created this mode
179          * @param name A one-word name for the mode
180          * @param modeletter The mode letter you wish to handle
181          * @param params Parameters taken by the mode
182          * @param type Type of the mode (MODETYPE_USER or MODETYPE_CHANNEL)
183          * @param mclass The object type of this mode handler, one of ModeHandler::Class
184          */
185         ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER);
186         virtual CullResult cull();
187         virtual ~ModeHandler();
188         /**
189          * Returns true if the mode is a list mode
190          */
191         bool IsListMode() const { return list; }
192         /**
193          * Mode prefix or 0. If this is defined, you should
194          * also implement GetPrefixRank() to return an integer
195          * value for this mode prefix.
196          */
197         inline char GetPrefix() const { return prefix; }
198         /**
199          * Get the 'value' of this modes prefix.
200          * determines which to display when there are multiple.
201          * The mode with the highest value is ranked first. See the
202          * PrefixModeValue enum and Channel::GetPrefixValue() for
203          * more information.
204          */
205         unsigned int GetPrefixRank() const { return prefixrank; }
206         /**
207          * Returns the mode's type
208          */
209         inline ModeType GetModeType() const { return m_type; }
210         /**
211          * Returns the mode's parameter translation type
212          */
213         inline TranslateType GetTranslateType() const { return m_paramtype; }
214         /**
215          * Returns true if the mode can only be set/unset by an oper
216          */
217         inline bool NeedsOper() const { return oper; }
218         /**
219          * Returns the number of parameters for the mode. Any non-zero
220          * value should be considered to be equivalent to one.
221          * @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.
222          * @return The number of parameters the mode expects
223          */
224         int GetNumParams(bool adding);
225         /**
226          * Returns the mode character this handler handles.
227          * @return The mode character
228          */
229         inline char GetModeChar() { return mode; }
230
231         /** For user modes, return the current parameter, if any
232          */
233         virtual std::string GetUserParameter(User* useor);
234
235         /**
236          * Called when a channel mode change access check for your mode occurs.
237          * @param source Contains the user setting the mode.
238          * @param channel contains the destination channel the modes are being set on.
239          * @param parameter The parameter for your mode. This is modifiable.
240          * @param adding This value is true when the mode is being set, or false when it is being unset.
241          * @return allow, deny, or passthru to check against the required level
242          */
243         virtual ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding);
244
245         /**
246          * Called when a mode change for your mode occurs.
247          * @param source Contains the user setting the mode.
248          * @param dest For usermodes, contains the destination user the mode is being set on. For channelmodes, this is an undefined value.
249          * @param channel For channel modes, contains the destination channel the modes are being set on. For usermodes, this is an undefined value.
250          * @param parameter The parameter for your mode, if you indicated that your mode requires a parameter when being set or unset. Note that
251          * 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
252          * but you specified your mode REQUIRES a parameter, this is equivalent to returning MODEACTION_DENY and will prevent the mode from being
253          * displayed.
254          * @param adding This value is true when the mode is being set, or false when it is being unset.
255          * @return MODEACTION_ALLOW to allow the mode, or MODEACTION_DENY to prevent the mode, also see the description of 'parameter'.
256          */
257         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding); /* Can change the mode parameter as its a ref */
258         /**
259          * If your mode is a listmode, then this method will be called for displaying an item list, e.g. on MODE \#channel +modechar
260          * without any parameter or other modes in the command.
261          * @param user The user issuing the command
262          * @param channel The channel they're requesting an item list of (e.g. a banlist, or an exception list etc)
263          */
264         virtual void DisplayList(User* user, Channel* channel);
265
266         /** In the event that the mode should be given a parameter, and no parameter was provided, this method is called.
267          * This allows you to give special information to the user, or handle this any way you like.
268          * @param user The user issuing the mode change
269          * @param dest For user mode changes, the target of the mode. For channel mode changes, NULL.
270          * @param channel For channel mode changes, the target of the mode. For user mode changes, NULL.
271          */
272         virtual void OnParameterMissing(User* user, User* dest, Channel* channel);
273
274         /**
275          * If your mode is a listmode, this method will be called to display an empty list (just the end of list numeric)
276          * @param user The user issuing the command
277          * @param channel The channel tehy're requesting an item list of (e.g. a banlist, or an exception list etc)
278          */
279         virtual void DisplayEmptyList(User* user, Channel* channel);
280
281         /**
282          * If your mode needs special action during a server sync to determine which side wins when comparing timestamps,
283          * override this function and use it to return true or false. The default implementation just returns true if
284          * theirs < ours. This will only be called for non-listmodes with parameters, when adding the mode and where
285          * theirs == ours (therefore the default implementation will always return false).
286          * @param their_param Their parameter if the mode has a parameter
287          * @param our_param Our parameter if the mode has a parameter
288          * @param channel The channel we are checking against
289          * @return True if the other side wins the merge, false if we win the merge for this mode.
290          */
291         virtual bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel);
292
293         /**
294          * When a MODETYPE_USER mode handler is being removed, the core will call this method for every user on the server.
295          * The usermode will be removed using the appropiate server mode using InspIRCd::SendMode().
296          * @param user The user which the server wants to remove your mode from
297          */
298         void RemoveMode(User* user);
299
300         /**
301          * When a MODETYPE_CHANNEL mode handler is being removed, the server will call this method for every channel on the server.
302          * The mode handler has to populate the given modestacker with mode changes that remove the mode from the channel.
303          * The default implementation of this method can remove all kinds of channel modes except listmodes.
304          * In the case of listmodes, the entire list of items must be added to the modestacker (which is handled by ListModeBase,
305          * so if you inherit from it or your mode can be removed by the default implementation then you do not have to implement
306          * this function).
307          * @param channel The channel which the server wants to remove your mode from
308          * @param stack The mode stack to add the mode change to
309          */
310         virtual void RemoveMode(Channel* channel, irc::modestacker& stack);
311
312         inline unsigned int GetLevelRequired() const { return levelrequired; }
313 };
314
315 /** A prebuilt mode handler which handles a simple user mode, e.g. no parameters, usable by any user, with no extra
316  * behaviour to the mode beyond the basic setting and unsetting of the mode, not allowing the mode to be set if it
317  * is already set and not allowing it to be unset if it is already unset.
318  * An example of a simple user mode is user mode +w.
319  */
320 class CoreExport SimpleUserModeHandler : public ModeHandler
321 {
322  public:
323         SimpleUserModeHandler(Module* Creator, const std::string& Name, char modeletter)
324                 : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) {}
325         virtual ~SimpleUserModeHandler() {}
326         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
327 };
328
329 /** A prebuilt mode handler which handles a simple channel mode, e.g. no parameters, usable by any user, with no extra
330  * behaviour to the mode beyond the basic setting and unsetting of the mode, not allowing the mode to be set if it
331  * is already set and not allowing it to be unset if it is already unset.
332  * An example of a simple channel mode is channel mode +s.
333  */
334 class CoreExport SimpleChannelModeHandler : public ModeHandler
335 {
336  public:
337         SimpleChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
338                 : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) {}
339         virtual ~SimpleChannelModeHandler() {}
340         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
341 };
342
343 class CoreExport ParamChannelModeHandler : public ModeHandler
344 {
345  public:
346         ParamChannelModeHandler(Module* Creator, const std::string& Name, char modeletter)
347                 : ModeHandler(Creator, Name, modeletter, PARAM_SETONLY, MODETYPE_CHANNEL) {}
348         virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
349         /** Validate the parameter - you may change the value to normalize it. Return true if it is valid. */
350         virtual bool ParamValidate(std::string& parameter);
351 };
352
353 /**
354  * The ModeWatcher class can be used to alter the behaviour of a mode implemented
355  * by the core or by another module. To use ModeWatcher, derive a class from it,
356  * and attach it to the mode using Server::AddModeWatcher and Server::DelModeWatcher.
357  * A ModeWatcher will be called both before and after the mode change.
358  */
359 class CoreExport ModeWatcher : public classbase
360 {
361  private:
362         /**
363          * The mode name this class is watching
364          */
365         const std::string mode;
366
367         /**
368          * The mode type being watched (user or channel)
369          */
370         ModeType m_type;
371
372  public:
373         ModuleRef creator;
374         /**
375          * The constructor initializes the mode and the mode type
376          */
377         ModeWatcher(Module* creator, const std::string& modename, ModeType type);
378         /**
379          * The default destructor does nothing.
380          */
381         virtual ~ModeWatcher();
382
383         /**
384          * Get the mode name being watched
385          * @return The mode name being watched
386          */
387         const std::string& GetModeName() const { return mode; }
388
389         /**
390          * Get the mode type being watched
391          * @return The mode type being watched (user or channel)
392          */
393         ModeType GetModeType();
394
395         /**
396          * Before the mode character is processed by its handler, 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          * If you alter the parameter you are given, the mode handler will see your atered version
402          * when it handles the mode.
403          * @param adding True if the mode is being added and false if it is being removed
404          * @return True to allow the mode change to go ahead, false to abort it. If you abort the
405          * change, the mode handler (and ModeWatcher::AfterMode()) will never see the mode change.
406          */
407         virtual bool BeforeMode(User* source, User* dest, Channel* channel, std::string& parameter, bool adding);
408         /**
409          * After the mode character has been processed by the ModeHandler, this method will be called.
410          * @param source The sender of the mode
411          * @param dest The target user for the mode, if you are watching a user mode
412          * @param channel The target channel for the mode, if you are watching a channel mode
413          * @param parameter The parameter of the mode, if the mode is supposed to have a parameter.
414          * You cannot alter the parameter here, as the mode handler has already processed it.
415          * @param adding True if the mode is being added and false if it is being removed
416          */
417         virtual void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding);
418 };
419
420 typedef std::multimap<std::string, ModeWatcher*>::iterator ModeWatchIter;
421
422 /** The mode parser handles routing of modes and handling of mode strings.
423  * It marshalls, controls and maintains both ModeWatcher and ModeHandler classes,
424  * parses client to server MODE strings for user and channel modes, and performs
425  * processing for the 004 mode list numeric, amongst other things.
426  */
427 class CoreExport ModeParser
428 {
429  private:
430         /** Mode handlers for each mode, to access a handler subtract
431          * 65 from the ascii value of the mode letter.
432          * The upper bit of the value indicates if its a usermode
433          * or a channel mode, so we have 256 of them not 64.
434          */
435         ModeHandler* modehandlers[256];
436
437         /** Mode watcher classes
438          */
439         std::multimap<std::string, ModeWatcher*> modewatchermap;
440
441         /** Displays the current modes of a channel or user.
442          * Used by ModeParser::Process.
443          */
444         void DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text);
445         /** Displays the value of a list mode
446          * Used by ModeParser::Process.
447          */
448         void DisplayListModes(User* user, Channel* chan, std::string &mode_sequence);
449
450         /**
451          * Attempts to apply a mode change to a user or channel
452          */
453         ModeAction TryMode(User* user, User* targu, Channel* targc, bool adding, unsigned char mode, std::string &param, bool SkipACL);
454
455         /** Returns a list of user or channel mode characters.
456          * Used for constructing the parts of the mode list in the 004 numeric.
457          * @param mt Controls whether to list user modes or channel modes
458          * @param needparam Return modes only if they require a parameter to be set
459          * @return The available mode letters that satisfy the given conditions
460          */
461         std::string CreateModeList(ModeType mt, bool needparam = false);
462
463         /** Recreate the cached mode list that is displayed in the 004 numeric
464          * in Cached004ModeList.
465          * Called when a mode handler is added or removed.
466          */
467         void RecreateModeListFor004Numeric();
468
469         /** The string representing the last set of modes to be parsed.
470          * Use GetLastParse() to get this value, to be used for  display purposes.
471          */
472         std::string LastParse;
473         std::vector<std::string> LastParseParams;
474         std::vector<TranslateType> LastParseTranslate;
475
476         unsigned int sent[256];
477
478         unsigned int seq;
479
480         /** Cached mode list for use in 004 numeric
481          */
482         std::string Cached004ModeList;
483
484  public:
485         typedef unsigned int ModeProcessFlag;
486         enum ModeProcessFlags
487         {
488                 /** If only this flag is specified, the mode change will be global
489                  * and parameter modes will have their parameters explicitly set
490                  * (not merged). This is the default.
491                  */
492                 MODE_NONE = 0,
493
494                 /** If this flag is set then the parameters of non-listmodes will be
495                  * merged according to their conflict resolution rules.
496                  * Does not affect user modes, channel modes without a parameter and
497                  * listmodes.
498                  */
499                 MODE_MERGE = 1,
500
501                 /** If this flag is set then the mode change won't be handed over to
502                  * the linking module to be sent to other servers, but will be processed
503                  * locally and sent to local user(s) as usual.
504                  */
505                 MODE_LOCALONLY = 2
506         };
507
508         ModeParser();
509         ~ModeParser();
510
511         /** Initialize all built-in modes
512          */
513         static void InitBuiltinModes();
514
515         /** Tidy a banmask. This makes a banmask 'acceptable' if fields are left out.
516          * E.g.
517          *
518          * nick -> nick!*@*
519          *
520          * nick!ident -> nick!ident@*
521          *
522          * host.name -> *!*\@host.name
523          *
524          * ident@host.name -> *!ident\@host.name
525          *
526          * This method can be used on both IPV4 and IPV6 user masks.
527          */
528         static void CleanMask(std::string &mask);
529         /** Get the last string to be processed, as it was sent to the user or channel.
530          * Use this to display a string you just sent to be parsed, as the actual output
531          * may be different to what you sent after it has been 'cleaned up' by the parser.
532          * @return Last parsed string, as seen by users.
533          */
534         const std::string& GetLastParse() const { return LastParse; }
535         const std::vector<std::string>& GetLastParseParams() { return LastParseParams; }
536         const std::vector<TranslateType>& GetLastParseTranslate() { return LastParseTranslate; }
537         /** Add a mode to the mode parser.
538          * @return True if the mode was successfully added.
539          */
540         bool AddMode(ModeHandler* mh);
541         /** Delete a mode from the mode parser.
542          * When a mode is deleted, the mode handler will be called
543          * for every user (if it is a user mode) or for every  channel
544          * (if it is a channel mode) to unset the mode on all objects.
545          * This prevents modes staying in the system which no longer exist.
546          * @param mh The mode handler to remove
547          * @return True if the mode was successfully removed.
548          */
549         bool DelMode(ModeHandler* mh);
550
551         /** Add a mode watcher.
552          * A mode watcher is triggered before and after a mode handler is
553          * triggered. See the documentation of class ModeWatcher for more
554          * information.
555          * @param mw The ModeWatcher you want to add
556          */
557         void AddModeWatcher(ModeWatcher* mw);
558
559         /** Delete a mode watcher.
560          * A mode watcher is triggered before and after a mode handler is
561          * triggered. See the documentation of class ModeWatcher for more
562          * information.
563          * @param mw The ModeWatcher you want to delete
564          * @return True if the ModeWatcher was deleted correctly
565          */
566         bool DelModeWatcher(ModeWatcher* mw);
567         /** Process a set of mode changes from a server or user.
568          * @param parameters The parameters of the mode change, in the format
569          * they would be from a MODE command.
570          * @param user The source of the mode change, can be a server user.
571          * @param flags Optional flags controlling how the mode change is processed,
572          * defaults to MODE_NONE.
573          */
574         void Process(const std::vector<std::string>& parameters, User* user, ModeProcessFlag flags = MODE_NONE);
575
576         /** Find the mode handler for a given mode and type.
577          * @param modeletter mode letter to search for
578          * @param mt type of mode to search for, user or channel
579          * @returns a pointer to a ModeHandler class, or NULL of there isnt a handler for the given mode
580          */
581         ModeHandler* FindMode(unsigned const char modeletter, ModeType mt);
582
583         /** Find a mode handler by its prefix.
584          * If there is no mode handler with the given prefix, NULL will be returned.
585          * @param pfxletter The prefix to find, e.g. '@'
586          * @return The mode handler which handles this prefix, or NULL if there is none.
587          */
588         ModeHandler* FindPrefix(unsigned const char pfxletter);
589
590         /** Returns a list of modes, space seperated by type:
591          * 1. User modes
592          * 2. Channel modes
593          * 3. Channel modes that require a parameter when set
594          * This is sent to users as the last part of the 004 numeric
595          */
596         const std::string& GetModeListFor004Numeric();
597
598         /** Generates a list of modes, comma seperated by type:
599          *  1; Listmodes EXCEPT those with a prefix
600          *  2; Modes that take a param when adding or removing
601          *  3; Modes that only take a param when adding
602          *  4; Modes that dont take a param
603          */
604         std::string GiveModeList(ModeMasks m);
605
606         /** This returns the PREFIX=(ohv)@%+ section of the 005 numeric, or
607          * just the "@%+" part if the parameter false
608          */
609         std::string BuildPrefixes(bool lettersAndModes = true);
610 };
611
612 inline const std::string& ModeParser::GetModeListFor004Numeric()
613 {
614         return Cached004ModeList;
615 }