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