]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
Remove unneeded Extensible inheritance and remove "age" field from classbase
[user/henk/code/inspircd.git] / include / channels.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __CHANNELS_H__
15 #define __CHANNELS_H__
16
17 /** RFC1459 channel modes
18  */
19 enum ChannelModes {
20         CM_TOPICLOCK = 't'-65,  /* +t: Only operators can change topic */
21         CM_NOEXTERNAL = 'n'-65, /* +n: Only users in the channel can message it */
22         CM_INVITEONLY = 'i'-65, /* +i: Invite only */
23         CM_MODERATED = 'm'-65,  /* +m: Only voices and above can talk */
24         CM_SECRET = 's'-65,     /* +s: Secret channel */
25         CM_PRIVATE = 'p'-65,    /* +p: Private channel */
26         CM_KEY = 'k'-65,        /* +k: Keyed channel */
27         CM_LIMIT = 'l'-65       /* +l: Maximum user limit */
28 };
29
30 /* Forward declarations - needed */
31 class User;
32
33 /** Holds an entry for a ban list, exemption list, or invite list.
34  * This class contains a single element in a channel list, such as a banlist.
35  */
36 class HostItem : public classbase
37 {
38  public:
39         /** Time the item was added
40          */
41         time_t set_time;
42         /** Who added the item
43          */
44         std::string set_by;
45         /** The actual item data
46          */
47         std::string data;
48
49         HostItem() { /* stub */ }
50         virtual ~HostItem() { /* stub */ }
51 };
52
53 /** A subclass of HostItem designed to hold channel bans (+b)
54  */
55 class BanItem : public HostItem
56 {
57 };
58
59 /** Holds a complete ban list
60  */
61 typedef std::list<BanItem>      BanList;
62
63 /** A list of users on a channel
64  */
65 typedef std::map<User*,std::string> CUList;
66
67 /** Shorthand for CUList::iterator
68  */
69 typedef CUList::iterator CUListIter;
70
71 /** Shorthand for CUList::const_iterator
72  */
73 typedef CUList::const_iterator CUListConstIter;
74
75 /** A list of custom modes parameters on a channel
76  */
77 typedef std::map<char,std::string> CustomModeList;
78
79
80 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
81  */
82 enum UserChannelModes {
83         UCMODE_OP       = 1,    /* Opped user */
84         UCMODE_VOICE    = 2,    /* Voiced user */
85         UCMODE_HOP      = 4     /* Halfopped user */
86 };
87
88 /** A stored prefix and its rank
89  */
90 typedef std::pair<char, unsigned int> prefixtype;
91
92 /** A list of prefixes set on a user in a channel
93  */
94 typedef std::vector<prefixtype> pfxcontainer;
95
96 /** A list of users with zero or more prefixes set on them
97  */
98 typedef std::map<User*, std::vector<prefixtype> > prefixlist;
99
100 /** Holds all relevent information for a channel.
101  * This class represents a channel, and contains its name, modes, topic, topic set time,
102  * etc, and an instance of the BanList type.
103  */
104 class CoreExport Channel : public Extensible
105 {
106  private:
107
108         /** Pointer to creator object
109          */
110         InspIRCd* ServerInstance;
111
112         /** Connect a Channel to a User
113          */
114         static Channel* ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting, bool created);
115
116         /** Set default modes for the channel on creation
117          */
118         void SetDefaultModes();
119
120         /** A list of prefixes associated with each user in the channel
121          * (e.g. &%+ etc)
122          */
123         prefixlist prefixes;
124
125         /** Maximum number of bans (cached)
126          */
127         int maxbans;
128
129  public:
130         /** Creates a channel record and initialises it with default values
131          * @throw Nothing at present.
132          */
133         Channel(InspIRCd* Instance, const std::string &name, time_t ts);
134
135         /** The channel's name.
136          */
137         std::string name; /* CHANMAX */
138
139         /** Time that the object was instantiated (used for TS calculation etc)
140         */
141         time_t age;
142
143         /** Modes for the channel.
144          * This is not a null terminated string! It is a bitset where
145          * each item in it represents if a mode is set. For example
146          * for mode +A, index 0. Use modechar-65 to calculate which
147          * field to check.
148          */
149         std::bitset<64> modes;
150
151         /** User lists.
152          * There are four user lists, one for
153          * all the users, one for the ops, one for
154          * the halfops and another for the voices.
155          */
156         CUList internal_userlist;
157
158         /** Opped users.
159          * There are four user lists, one for
160          * all the users, one for the ops, one for
161          * the halfops and another for the voices.
162          */
163         CUList internal_op_userlist;
164
165         /** Halfopped users.
166          * There are four user lists, one for
167          * all the users, one for the ops, one for
168          * the halfops and another for the voices.
169          */
170         CUList internal_halfop_userlist;
171
172         /** Voiced users.
173          * There are four user lists, one for
174          * all the users, one for the ops, one for
175          * the halfops and another for the voices.
176          */
177         CUList internal_voice_userlist;
178
179         /** Parameters for custom modes.
180          * One for each custom mode letter.
181          */
182         CustomModeList custom_mode_params;
183
184         /** Channel topic.
185          * If this is an empty string, no channel topic is set.
186          */
187         std::string topic; /* MAXTOPIC */
188
189         /** Time topic was set.
190          * If no topic was ever set, this will be equal to Channel::created
191          */
192         time_t topicset;
193
194         /** The last user to set the topic.
195          * If this member is an empty string, no topic was ever set.
196          */
197         std::string setby; /* 128 */
198
199         /** The list of all bans set on the channel.
200          */
201         BanList bans;
202
203         /** Sets or unsets a custom mode in the channels info
204          * @param mode The mode character to set or unset
205          * @param mode_on True if you want to set the mode or false if you want to remove it
206          */
207         void SetMode(char mode,bool mode_on);
208
209         /** Sets or unsets a custom mode in the channels info
210          * @param mode The mode character to set or unset
211          * @param parameter The parameter string to associate with this mode character.
212          * If it is empty, the mode is unset; if it is nonempty, the mode is set.
213          */
214         void SetModeParam(char mode, std::string parameter);
215
216         /** Returns true if a mode is set on a channel
217           * @param mode The mode character you wish to query
218           * @return True if the custom mode is set, false if otherwise
219           */
220         bool IsModeSet(char mode);
221
222         /** Returns the parameter for a custom mode on a channel.
223           * @param mode The mode character you wish to query
224           *
225           * For example if "+L #foo" is set, and you pass this method
226           * 'L', it will return '#foo'. If the mode is not set on the
227           * channel, or the mode has no parameters associated with it,
228           * it will return an empty string.
229           *
230           * @return The parameter for this mode is returned, or an empty string
231           */
232         std::string GetModeParameter(char mode);
233
234         /** Sets the channel topic.
235          * @param u The user setting the topic
236          * @param t The topic to set it to. Non-const, as it may be modified by a hook.
237          * @param forceset If set to true then all access checks will be bypassed.
238          */
239         int SetTopic(User *u, std::string &t, bool forceset = false);
240
241         /** Obtain the channel "user counter"
242          * This returns the channel reference counter, which is initialized
243          * to 0 when the channel is created and incremented/decremented
244          * upon joins, parts quits and kicks.
245          *
246          * @return The number of users on this channel
247          */
248         long GetUserCounter();
249
250         /** Add a user pointer to the internal reference list
251          * @param user The user to add
252          *
253          * The data inserted into the reference list is a table as it is
254          * an arbitary pointer compared to other users by its memory address,
255          * as this is a very fast 32 or 64 bit integer comparison.
256          */
257         void AddUser(User* user);
258
259         /** Add a user pointer to the internal reference list of opped users
260          * @param user The user to add
261          */
262         void AddOppedUser(User* user);
263
264         /** Add a user pointer to the internal reference list of halfopped users
265          * @param user The user to add
266          */
267         void AddHalfoppedUser(User* user);
268
269         /** Add a user pointer to the internal reference list of voiced users
270          * @param user The user to add
271          */
272         void AddVoicedUser(User* user);
273
274         /** Delete a user pointer to the internal reference list
275          * @param user The user to delete
276          * @return number of users left on the channel after deletion of the user
277          */
278         unsigned long DelUser(User* user);
279
280         /** Delete a user pointer to the internal reference list of opped users
281          * @param user The user to delete
282          */
283         void DelOppedUser(User* user);
284
285         /** Delete a user pointer to the internal reference list of halfopped users
286          * @param user The user to delete
287          */
288         void DelHalfoppedUser(User* user);
289
290         /** Delete a user pointer to the internal reference list of voiced users
291          * @param user The user to delete
292          */
293         void DelVoicedUser(User* user);
294
295         /** Obtain the internal reference list
296          * The internal reference list contains a list of User*.
297          * These are used for rapid comparison to determine
298          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
299          * The resulting pointer to the vector should be considered
300          * readonly and only modified via AddUser and DelUser.
301          *
302          * @return This function returns pointer to a map of User pointers (CUList*).
303          */
304         CUList* GetUsers();
305
306         /** Obtain the internal reference list of opped users
307          * @return This function returns pointer to a map of User pointers (CUList*).
308          */
309         CUList* GetOppedUsers();
310
311         /** Obtain the internal reference list of halfopped users
312          * @return This function returns pointer to a map of User pointers (CUList*).
313          */
314         CUList* GetHalfoppedUsers();
315
316         /** Obtain the internal reference list of voiced users
317          * @return This function returns pointer to a map of User pointers (CUList*).
318          */
319         CUList* GetVoicedUsers();
320
321         /** Returns true if the user given is on the given channel.
322          * @param The user to look for
323          * @return True if the user is on this channel
324          */
325         bool HasUser(User* user);
326
327         /** Make src kick user from this channel with the given reason.
328          * @param src The source of the kick
329          * @param user The user being kicked (must be on this channel)
330          * @param reason The reason for the kick
331          * @return The number of users left on the channel. If this is zero
332          * when the method returns, you MUST delete the Channel immediately!
333          */
334         long KickUser(User *src, User *user, const char* reason);
335
336         /** Make the server kick user from this channel with the given reason.
337          * @param user The user being kicked (must be on this channel)
338          * @param reason The reason for the kick
339          * @param triggerevents True if you wish this kick to trigger module events
340          * @return The number of users left on the channel. If this is zero
341          * when the method returns, you MUST delete the Channel immediately!
342          */
343         long ServerKickUser(User* user, const char* reason, const char* servername = NULL);
344
345         /** Part a user from this channel with the given reason.
346          * If the reason field is NULL, no reason will be sent.
347          * @param user The user who is parting (must be on this channel)
348          * @param reason The part reason
349          * @return The number of users left on the channel. If this is zero
350          * when the method returns, you MUST delete the Channel immediately!
351          */
352         long PartUser(User *user, std::string &reason);
353
354         /* Join a user to a channel. May be a channel that doesnt exist yet.
355          * @param user The user to join to the channel.
356          * @param cn The channel name to join to. Does not have to exist.
357          * @param key The key of the channel, if given
358          * @param override If true, override all join restrictions such as +bkil
359          * @return A pointer to the Channel the user was joined to. A new Channel may have
360          * been created if the channel did not exist before the user was joined to it.
361          * If the user could not be joined to a channel, the return value may be NULL.
362          */
363         static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
364
365         /** Write to a channel, from a user, using va_args for text
366          * @param user User whos details to prefix the line with
367          * @param text A printf-style format string which builds the output line without prefix
368          * @param ... Zero or more POD types
369          */
370         void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
371
372         /** Write to a channel, from a user, using std::string for text
373          * @param user User whos details to prefix the line with
374          * @param text A std::string containing the output line without prefix
375          */
376         void WriteChannel(User* user, const std::string &text);
377
378         /** Write to a channel, from a server, using va_args for text
379          * @param ServName Server name to prefix the line with
380          * @param text A printf-style format string which builds the output line without prefix
381          * @param ... Zero or more POD type
382          */
383         void WriteChannelWithServ(const char* ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
384
385         /** Write to a channel, from a server, using std::string for text
386          * @param ServName Server name to prefix the line with
387          * @param text A std::string containing the output line without prefix
388          */
389         void WriteChannelWithServ(const char* ServName, const std::string &text);
390
391         /** Write to all users on a channel except a specific user, using va_args for text.
392          * Internally, this calls WriteAllExcept().
393          * @param user User whos details to prefix the line with, and to omit from receipt of the message
394          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
395          * use the nick!user@host of the user.
396          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
397          * @param text A printf-style format string which builds the output line without prefix
398          * @param ... Zero or more POD type
399          */
400         void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
401
402         /** Write to all users on a channel except a list of users, using va_args for text
403          * @param user User whos details to prefix the line with, and to omit from receipt of the message
404          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
405          * use the nick!user@host of the user.
406          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
407          * @param except_list A list of users NOT to send the text to
408          * @param text A printf-style format string which builds the output line without prefix
409          * @param ... Zero or more POD type
410          */
411         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
412
413         /** Write to all users on a channel except a specific user, using std::string for text.
414          * Internally, this calls WriteAllExcept().
415          * @param user User whos details to prefix the line with, and to omit from receipt of the message
416          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
417          * use the nick!user@host of the user.
418          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
419          * @param text A std::string containing the output line without prefix
420          */
421         void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
422
423         /** Write to all users on a channel except a list of users, using std::string for text
424          * @param user User whos details to prefix the line with, and to omit from receipt of the message
425          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
426          * use the nick!user@host of the user.
427          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
428          * @param except_list A list of users NOT to send the text to
429          * @param text A std::string containing the output line without prefix
430          */
431         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
432         /** Write a line of text that already includes the source */
433         void RawWriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
434
435         /** Returns the maximum number of bans allowed to be set on this channel
436          * @return The maximum number of bans allowed
437          */
438         long GetMaxBans();
439
440         /** Return the channel's modes with parameters.
441          * @param showkey If this is set to true, the actual key is shown,
442          * otherwise it is replaced with '&lt;KEY&gt;'
443          * @return The channel mode string
444          */
445         char* ChanModes(bool showkey);
446
447         /** Spool the NAMES list for this channel to the given user
448          * @param user The user to spool the NAMES list to
449          * @param ulist The user list to send, NULL to use the
450          * channel's default names list of everyone
451          */
452         void UserList(User *user, CUList* ulist = NULL);
453
454         /** Get the number of invisible users on this channel
455          * @return Number of invisible users
456          */
457         int CountInvisible();
458
459         /** Get a users status on this channel
460          * @param user The user to look up
461          * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
462          */
463         int GetStatus(User *user);
464
465         /** Get a users status on this channel in a bitmask
466          * @param user The user to look up
467          * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
468          */
469         int GetStatusFlags(User *user);
470
471         /** Get a users prefix on this channel in a string.
472          * @param user The user to look up
473          * @return A character array containing the prefix string.
474          * Unlike GetStatus and GetStatusFlags which will only return the
475          * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
476          * will also return module-defined prefixes. If the user has to prefix,
477          * an empty but non-null string is returned. If the user has multiple
478          * prefixes, the highest is returned. If you do not recognise the prefix
479          * character you can get, you can deal with it in a 'proprtional' manner
480          * compared to known prefixes, using GetPrefixValue().
481          */
482         const char* GetPrefixChar(User *user);
483
484         /** Return all of a users mode prefixes into a char* string.
485          * @param user The user to look up
486          * @return A list of all prefix characters. The prefixes will always
487          * be in rank order, greatest first, as certain IRC clients require
488          * this when multiple prefixes are used names lists.
489          */
490         const char* GetAllPrefixChars(User* user);
491
492         /** Get the value of a users prefix on this channel.
493          * @param user The user to look up
494          * @return The module or core-defined value of the users prefix.
495          * The values for op, halfop and voice status are constants in
496          * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
497          * If the value you are given does not match one of these three, it is
498          * a module-defined mode, and it should be compared in proportion to
499          * these three constants. For example a value greater than OP_VALUE
500          * is a prefix of greater 'worth' than ops, and a value less than
501          * VOICE_VALUE is of lesser 'worth' than a voice.
502          */
503         unsigned int GetPrefixValue(User* user);
504
505         /** This method removes all prefix characters from a user.
506          * It will not inform the user or the channel of the removal of prefixes,
507          * and should be used when the user parts or quits.
508          * @param user The user to remove all prefixes from
509          */
510         void RemoveAllPrefixes(User* user);
511
512         /** Add a prefix character to a user.
513          * Only the core should call this method, usually  from
514          * within the mode parser or when the first user joins
515          * the channel (to grant ops to them)
516          * @param user The user to associate the privilage with
517          * @param prefix The prefix character to associate
518          * @param prefix_rank The rank (value) of this prefix character
519          * @param adding True if adding the prefix, false when removing
520          */
521         void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
522
523         /** Check if a user is banned on this channel
524          * @param user A user to check against the banlist
525          * @returns True if the user given is banned
526          */
527         bool IsBanned(User* user);
528
529         /** Check whether an extban of a given type matches
530          * a given user for this channel.
531          * @param u The user to match bans against
532          * @param type The type of extban to check
533          * @returns 1 = exempt, 0 = no match, -1 = banned
534          */
535         int GetExtBanStatus(User *u, char type);
536
537         /** Overloaded version to check whether a particular string is extbanned
538          * @returns 1 = exempt, 0 = no match, -1 = banned
539          */
540         int GetExtBanStatus(const std::string &str, char type);
541
542         /** Clears the cached max bans value
543          */
544         void ResetMaxBans();
545
546         /** Destructor for Channel
547          */
548         virtual ~Channel() { /* stub */ }
549 };
550
551 static inline int banmatch_reduce(int v1, int v2)
552 {
553         int a1 = abs(v1);
554         int a2 = abs(v2);
555         if (a1 > a2)
556                 return v1;
557         else if (a2 > a1)
558                 return v2;
559         else if (v1 > v2)
560                 return v1;
561         // otherwise v2 > v1 or equal
562         return v2;
563 }
564
565 #endif