]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/channels.h
The rest of the server protocol interface and fix a warning in m_rline
[user/henk/code/inspircd.git] / include / channels.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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         char set_by[64];
45         /** The actual item data
46          */
47         char data[MAXBUF];
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::vector<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,char*> 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, time created, 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);
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         char name[CHANMAX];
138
139         /** Modes for the channel.
140          * This is not a null terminated string! It is a hash where
141          * each item in it represents if a mode is set. For example
142          * for mode +A, index 0. Use modechar-65 to calculate which
143          * field to check.
144          */
145         char modes[64];
146
147         /** User lists.
148          * There are four user lists, one for 
149          * all the users, one for the ops, one for
150          * the halfops and another for the voices.
151          */
152         CUList internal_userlist;
153
154         /** Opped users.
155          * There are four user lists, one for 
156          * all the users, one for the ops, one for
157          * the halfops and another for the voices.
158          */
159         CUList internal_op_userlist;
160
161         /** Halfopped users.
162          * There are four user lists, one for 
163          * all the users, one for the ops, one for
164          * the halfops and another for the voices.
165          */
166         CUList internal_halfop_userlist;
167
168         /** Voiced users.
169          * There are four user lists, one for
170          * all the users, one for the ops, one for
171          * the halfops and another for the voices.
172          */
173         CUList internal_voice_userlist;
174
175         /** Parameters for custom modes.
176          * One for each custom mode letter.
177          */
178         CustomModeList custom_mode_params;
179
180         /** Channel topic.
181          * If this is an empty string, no channel topic is set.
182          */
183         char topic[MAXTOPIC];
184
185         /** Creation time.
186          * This is a timestamp (TS) value.
187          */
188         time_t created;
189
190         /** Time topic was set.
191          * If no topic was ever set, this will be equal to Channel::created
192          */
193         time_t topicset;
194
195         /** The last user to set the topic.
196          * If this member is an empty string, no topic was ever set.
197          */
198         char setby[128];
199
200         /** Contains the channel user limit.
201          * If this value is zero, there is no limit in place.
202          */
203         short int limit;
204         
205         /** Contains the channel key.
206          * If this value is an empty string, there is no channel key in place.
207          */
208         char key[32];
209
210         /** The list of all bans set on the channel.
211          */
212         BanList bans;
213         
214         /** Sets or unsets a custom mode in the channels info
215          * @param mode The mode character to set or unset
216          * @param mode_on True if you want to set the mode or false if you want to remove it
217          */
218         void SetMode(char mode,bool mode_on);
219
220         /** Sets or unsets the parameters for a custom mode in a channels info
221          * @param mode The mode character to set or unset
222          * @param parameter The parameter string to associate with this mode character
223          * @param mode_on True if you want to set the mode or false if you want to remove it
224          */
225         void SetModeParam(char mode,const char* parameter,bool mode_on);
226  
227         /** Returns true if a mode is set on a channel
228           * @param mode The mode character you wish to query
229           * @return True if the custom mode is set, false if otherwise
230           */
231         bool IsModeSet(char mode);
232
233         /** Returns the parameter for a custom mode on a channel.
234           * @param mode The mode character you wish to query
235           *
236           * For example if "+L #foo" is set, and you pass this method
237           * 'L', it will return '#foo'. If the mode is not set on the
238           * channel, or the mode has no parameters associated with it,
239           * it will return an empty string.
240           *
241           * @return The parameter for this mode is returned, or an empty string
242           */
243         std::string GetModeParameter(char mode);
244
245         /** Obtain the channel "user counter"
246          * This returns the channel reference counter, which is initialized
247          * to 0 when the channel is created and incremented/decremented
248          * upon joins, parts quits and kicks.
249          *
250          * @return The number of users on this channel
251          */
252         long GetUserCounter();
253
254         /** Add a user pointer to the internal reference list
255          * @param user The user to add
256          *
257          * The data inserted into the reference list is a table as it is
258          * an arbitary pointer compared to other users by its memory address,
259          * as this is a very fast 32 or 64 bit integer comparison.
260          */
261         void AddUser(User* user);
262
263         /** Add a user pointer to the internal reference list of opped users
264          * @param user The user to add
265          */
266         void AddOppedUser(User* user);
267
268         /** Add a user pointer to the internal reference list of halfopped users
269          * @param user The user to add
270          */
271         void AddHalfoppedUser(User* user);
272
273         /** Add a user pointer to the internal reference list of voiced users
274          * @param user The user to add
275          */
276         void AddVoicedUser(User* user);
277
278         /** Delete a user pointer to the internal reference list
279          * @param user The user to delete
280          * @return number of users left on the channel after deletion of the user
281          */
282         unsigned long DelUser(User* user);
283
284         /** Delete a user pointer to the internal reference list of opped users
285          * @param user The user to delete
286          */
287         void DelOppedUser(User* user);
288
289         /** Delete a user pointer to the internal reference list of halfopped users
290          * @param user The user to delete
291          */
292         void DelHalfoppedUser(User* user);
293
294         /** Delete a user pointer to the internal reference list of voiced users
295          * @param user The user to delete
296          */
297         void DelVoicedUser(User* user);
298
299         /** Obtain the internal reference list
300          * The internal reference list contains a list of User*.
301          * These are used for rapid comparison to determine
302          * channel membership for PRIVMSG, NOTICE, QUIT, PART etc.
303          * The resulting pointer to the vector should be considered
304          * readonly and only modified via AddUser and DelUser.
305          *
306          * @return This function returns pointer to a map of User pointers (CUList*).
307          */
308         CUList* GetUsers();
309
310         /** Obtain the internal reference list of opped users
311          * @return This function returns pointer to a map of User pointers (CUList*).
312          */
313         CUList* GetOppedUsers();
314
315         /** Obtain the internal reference list of halfopped users
316          * @return This function returns pointer to a map of User pointers (CUList*).
317          */
318         CUList* GetHalfoppedUsers();
319
320         /** Obtain the internal reference list of voiced users
321          * @return This function returns pointer to a map of User pointers (CUList*).
322          */
323         CUList* GetVoicedUsers();
324
325         /** Returns true if the user given is on the given channel.
326          * @param The user to look for
327          * @return True if the user is on this channel
328          */
329         bool HasUser(User* user);
330
331         /** Make src kick user from this channel with the given reason.
332          * @param src The source of the kick
333          * @param user The user being kicked (must be on this channel)
334          * @param reason The reason for the kick
335          * @return The number of users left on the channel. If this is zero
336          * when the method returns, you MUST delete the Channel immediately!
337          */
338         long KickUser(User *src, User *user, const char* reason);
339
340         /** Make the server kick user from this channel with the given reason.
341          * @param user The user being kicked (must be on this channel)
342          * @param reason The reason for the kick
343          * @param triggerevents True if you wish this kick to trigger module events
344          * @return The number of users left on the channel. If this is zero
345          * when the method returns, you MUST delete the Channel immediately!
346          */
347         long ServerKickUser(User* user, const char* reason, bool triggerevents);
348
349         /** Part a user from this channel with the given reason.
350          * If the reason field is NULL, no reason will be sent.
351          * @param user The user who is parting (must be on this channel)
352          * @param reason The (optional) part reason
353          * @return The number of users left on the channel. If this is zero
354          * when the method returns, you MUST delete the Channel immediately!
355          */
356         long PartUser(User *user, const char* reason = NULL);
357
358         /* Join a user to a channel. May be a channel that doesnt exist yet.
359          * @param user The user to join to the channel.
360          * @param cn The channel name to join to. Does not have to exist.
361          * @param key The key of the channel, if given
362          * @param override If true, override all join restrictions such as +bkil
363          * @return A pointer to the Channel the user was joined to. A new Channel may have
364          * been created if the channel did not exist before the user was joined to it.
365          * If the user could not be joined to a channel, the return value may be NULL.
366          */
367         static Channel* JoinUser(InspIRCd* ServerInstance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS = 0);
368
369         /** Write to a channel, from a user, using va_args for text
370          * @param user User whos details to prefix the line with
371          * @param text A printf-style format string which builds the output line without prefix
372          * @param ... Zero or more POD types
373          */
374         void WriteChannel(User* user, const char* text, ...) CUSTOM_PRINTF(3, 4);
375
376         /** Write to a channel, from a user, using std::string for text
377          * @param user User whos details to prefix the line with
378          * @param text A std::string containing the output line without prefix
379          */
380         void WriteChannel(User* user, const std::string &text);
381
382         /** Write to a channel, from a server, using va_args for text
383          * @param ServName Server name to prefix the line with
384          * @param text A printf-style format string which builds the output line without prefix
385          * @param ... Zero or more POD type
386          */
387         void WriteChannelWithServ(const char* ServName, const char* text, ...) CUSTOM_PRINTF(3, 4);
388
389         /** Write to a channel, from a server, using std::string for text
390          * @param ServName Server name to prefix the line with
391          * @param text A std::string containing the output line without prefix
392          */
393         void WriteChannelWithServ(const char* ServName, const std::string &text);
394
395         /** Write to all users on a channel except a specific user, using va_args for text.
396          * Internally, this calls WriteAllExcept().
397          * @param user User whos details to prefix the line with, and to omit from receipt of the message
398          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
399          * use the nick!user@host of the user.
400          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
401          * @param text A printf-style format string which builds the output line without prefix
402          * @param ... Zero or more POD type
403          */
404         void WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...) CUSTOM_PRINTF(5, 6);
405
406         /** Write to all users on a channel except a list of users, using va_args for text
407          * @param user User whos details to prefix the line with, and to omit from receipt of the message
408          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
409          * use the nick!user@host of the user.
410          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
411          * @param except_list A list of users NOT to send the text to
412          * @param text A printf-style format string which builds the output line without prefix
413          * @param ... Zero or more POD type
414          */
415         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...) CUSTOM_PRINTF(6, 7);
416
417         /** Write to all users on a channel except a specific user, using std::string for text.
418          * Internally, this calls WriteAllExcept().
419          * @param user User whos details to prefix the line with, and to omit from receipt of the message
420          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
421          * use the nick!user@host of the user.
422          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
423          * @param text A std::string containing the output line without prefix
424          */
425         void WriteAllExceptSender(User* user, bool serversource, char status, const std::string& text);
426
427         /** Write to all users on a channel except a list of users, using std::string for text
428          * @param user User whos details to prefix the line with, and to omit from receipt of the message
429          * @param serversource If this parameter is true, use the local server name as the source of the text, otherwise,
430          * use the nick!user@host of the user.
431          * @param status The status of the users to write to, e.g. '@' or '%'. Use a value of 0 to write to everyone
432          * @param except_list A list of users NOT to send the text to
433          * @param text A std::string containing the output line without prefix
434          */
435         void WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string& text);
436
437         /** Returns the maximum number of bans allowed to be set on this channel
438          * @return The maximum number of bans allowed
439          */
440         long GetMaxBans();
441
442         /** Return the channel's modes with parameters.
443          * @param showkey If this is set to true, the actual key is shown,
444          * otherwise it is replaced with '&lt;KEY&gt;'
445          * @return The channel mode string
446          */
447         char* ChanModes(bool showkey);
448
449         /** Spool the NAMES list for this channel to the given user
450          * @param user The user to spool the NAMES list to
451          * @param ulist The user list to send, NULL to use the
452          * channel's default names list of everyone
453          */
454         void UserList(User *user, CUList* ulist = NULL);
455
456         /** Get the number of invisible users on this channel
457          * @return Number of invisible users
458          */
459         int CountInvisible();
460
461         /** Get a users status on this channel
462          * @param user The user to look up
463          * @return One of STATUS_OP, STATUS_HOP, STATUS_VOICE, or zero.
464          */
465         int GetStatus(User *user);
466
467         /** Get a users status on this channel in a bitmask
468          * @param user The user to look up
469          * @return A bitmask containing zero or more of STATUS_OP, STATUS_HOP, STATUS_VOICE
470          */
471         int GetStatusFlags(User *user);
472
473         /** Get a users prefix on this channel in a string.
474          * @param user The user to look up
475          * @return A character array containing the prefix string.
476          * Unlike GetStatus and GetStatusFlags which will only return the
477          * core specified modes @, % and + (op, halfop and voice), GetPrefixChar
478          * will also return module-defined prefixes. If the user has to prefix,
479          * an empty but non-null string is returned. If the user has multiple
480          * prefixes, the highest is returned. If you do not recognise the prefix
481          * character you can get, you can deal with it in a 'proprtional' manner
482          * compared to known prefixes, using GetPrefixValue().
483          */
484         const char* GetPrefixChar(User *user);
485
486         /** Return all of a users mode prefixes into a char* string.
487          * @param user The user to look up
488          * @return A list of all prefix characters. The prefixes will always
489          * be in rank order, greatest first, as certain IRC clients require
490          * this when multiple prefixes are used names lists.
491          */
492         const char* GetAllPrefixChars(User* user);
493
494         /** Get the value of a users prefix on this channel.
495          * @param user The user to look up
496          * @return The module or core-defined value of the users prefix.
497          * The values for op, halfop and voice status are constants in
498          * mode.h, and are OP_VALUE, HALFOP_VALUE, and VOICE_VALUE respectively.
499          * If the value you are given does not match one of these three, it is
500          * a module-defined mode, and it should be compared in proportion to
501          * these three constants. For example a value greater than OP_VALUE
502          * is a prefix of greater 'worth' than ops, and a value less than
503          * VOICE_VALUE is of lesser 'worth' than a voice.
504          */
505         unsigned int GetPrefixValue(User* user);
506
507         /** This method removes all prefix characters from a user.
508          * It will not inform the user or the channel of the removal of prefixes,
509          * and should be used when the user parts or quits.
510          * @param user The user to remove all prefixes from
511          */
512         void RemoveAllPrefixes(User* user);
513
514         /** Add a prefix character to a user.
515          * Only the core should call this method, usually  from
516          * within the mode parser or when the first user joins
517          * the channel (to grant ops to them)
518          * @param user The user to associate the privilage with
519          * @param prefix The prefix character to associate
520          * @param prefix_rank The rank (value) of this prefix character
521          * @param adding True if adding the prefix, false when removing
522          */
523         void SetPrefix(User* user, char prefix, unsigned int prefix_rank, bool adding);
524
525         /** Check if a user is banned on this channel
526          * @param user A user to check against the banlist
527          * @returns True if the user given is banned
528          */
529         bool IsBanned(User* user);
530
531         /** Clears the cached max bans value
532          */
533         void ResetMaxBans();
534
535         /** Destructor for Channel
536          */
537         virtual ~Channel() { /* stub */ }
538 };
539
540 #endif