11ef0120616dd4257eb183b4a0f9445315b0f9b6
[ircu2.10.12-pk.git] / include / channel.h
1 /*
2  * IRC - Internet Relay Chat, ircd/channel.h
3  * Copyright (C) 1990 Jarkko Oikarinen
4  * Copyright (C) 1996 - 1997 Carlo Wood
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 /** @file
21  * @brief Channel management and maintenance.
22  * @version $Id$
23  */
24 #ifndef INCLUDED_channel_h
25 #define INCLUDED_channel_h
26 #ifndef INCLUDED_ircd_defs_h
27 #include "ircd_defs.h"        /* NICKLEN */
28 #endif
29 #ifndef INCLUDED_sys_types_h
30 #include <sys/types.h>
31 #define INCLUDED_sys_types_h
32 #endif
33
34 struct SLink;
35 struct Client;
36
37 /*
38  * General defines
39  */
40
41 #define MAXMODEPARAMS   6       /**< Maximum number of mode parameters */
42 #define MODEBUFLEN      200     /**< Maximum length of a mode */
43
44 #define KEYLEN          23      /**< Maximum length of a key */
45 #define PASSLEN         23      /**< Maximum length of a password */
46 #define CHANNELLEN      200     /**< Maximum length of a channel */
47
48 #define MAXJOINARGS     15      /**< number of slots for join buffer */
49 #define STARTJOINLEN    10      /**< fuzzy numbers */
50 #define STARTCREATELEN  20
51
52 /*
53  * Macro's
54  */
55
56 #define ChannelExists(n)        (0 != FindChannel(n))
57
58 #define CHFL_CHANOP             0x0001  /**< Channel operator */
59 #define CHFL_VOICE              0x0002  /**< the power to speak */
60 #define CHFL_DEOPPED            0x0004  /**< Is de-opped by a server */
61 #define CHFL_SERVOPOK           0x0008  /**< Server op allowed */
62 #define CHFL_ZOMBIE             0x0010  /**< Kicked from channel */
63 #define CHFL_BURST_JOINED       0x0100  /**< Just joined by net.junction */
64 #define CHFL_BANVALID           0x0800  /**< CHFL_BANNED bit is valid */
65 #define CHFL_BANNED             0x1000  /**< Channel member is banned */
66 #define CHFL_SILENCE_IPMASK     0x2000  /**< silence mask is a CIDR */
67 #define CHFL_BURST_ALREADY_OPPED        0x04000  
68                                         /**< In oob BURST, but was already 
69                                          * joined and opped 
70                                          */
71 #define CHFL_BURST_ALREADY_VOICED       0x08000  
72                                         /**, In oob BURST, but was already 
73                                          * joined and voiced 
74                                          */
75 #define CHFL_CHANNEL_MANAGER    0x10000 /**< Set when creating channel or using 
76                                          * Apass 
77                                          */
78 #define CHFL_USER_PARTING       0x20000 /**< User is already parting that 
79                                          * channel 
80                                          */
81 #define CHFL_DELAYED            0x40000 /**< User's join message is delayed */
82
83 #define CHFL_OVERLAP         (CHFL_CHANOP | CHFL_VOICE)
84 #define CHFL_BANVALIDMASK    (CHFL_BANVALID | CHFL_BANNED)
85 #define CHFL_VOICED_OR_OPPED (CHFL_CHANOP | CHFL_VOICE)
86
87 /* Channel Visibility macros */
88
89 #define MODE_CHANOP     CHFL_CHANOP     /**< +o Chanop */
90 #define MODE_VOICE      CHFL_VOICE      /**< +v Voice */
91 #define MODE_PRIVATE    0x0004          /**< +p Private */
92 #define MODE_SECRET     0x0008          /**< +s Secret */
93 #define MODE_MODERATED  0x0010          /**< +m Moderated */
94 #define MODE_TOPICLIMIT 0x0020          /**< +t Topic Limited */
95 #define MODE_INVITEONLY 0x0040          /**< +i Invite only */
96 #define MODE_NOPRIVMSGS 0x0080          /**< +n No Private Messages */
97 #define MODE_KEY        0x0100          /**< +k Keyed */
98 #define MODE_BAN        0x0200          /**< +b Ban */
99 #define MODE_LIMIT      0x0400          /**< +l Limit */
100 #define MODE_REGONLY    0x0800          /**< Only +r users may join */
101 #define MODE_DELJOINS   0x1000          /**< New join messages are delayed */
102 #define MODE_SAVE       0x20000         /**< save this mode-with-arg 'til 
103                                          * later */
104 #define MODE_FREE       0x40000         /**< string needs to be passed to 
105                                          * MyFree() */
106 #define MODE_BURSTADDED 0x80000         /**< channel was created by a BURST */
107 #define MODE_UPASS      0x100000
108 #define MODE_APASS      0x200000
109 #define MODE_WASDELJOINS 0x400000       /**< Not DELJOINS, but some joins 
110                                          * pending */
111 /** mode flags which take another parameter (With PARAmeterS)
112  */
113 #define MODE_WPARAS     (MODE_CHANOP|MODE_VOICE|MODE_BAN|MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)
114
115 /** Available Channel modes */
116 #define infochanmodes feature_bool(FEAT_OPLEVELS) ? "AbiklmnopstUvrD" : "biklmnopstvrD"
117 /** Available Channel modes that take parameters */
118 #define infochanmodeswithparams feature_bool(FEAT_OPLEVELS) ? "AbkloUv" : "bklov"
119
120 #define HoldChannel(x)          (!(x))
121 /** name invisible */
122 #define SecretChannel(x)        ((x) && ((x)->mode.mode & MODE_SECRET))
123 /** channel not shown but names are */
124 #define HiddenChannel(x)        ((x) && ((x)->mode.mode & MODE_PRIVATE))
125 /** channel visible */
126 #define ShowChannel(v,c)        (PubChannel(c) || find_channel_member((v),(c)) || \
127                                  (IsAnOper(v) && HasPriv(v, PRIV_LIST_CHAN)))
128 #define PubChannel(x)           ((!x) || ((x)->mode.mode & \
129                                     (MODE_PRIVATE | MODE_SECRET)) == 0)
130
131 #define IsGlobalChannel(name)   (*(name) == '#')
132 #define IsLocalChannel(name)    (*(name) == '&')
133 #define IsChannelName(name)     (IsGlobalChannel(name) || IsLocalChannel(name))
134
135 typedef enum ChannelGetType {
136   CGT_NO_CREATE,
137   CGT_CREATE
138 } ChannelGetType;
139
140 /* used in SetMode() in channel.c and m_umode() in s_msg.c */
141
142 #define MODE_NULL      0
143 #define MODE_ADD       0x40000000
144 #define MODE_DEL       0x20000000
145
146 /* used in ListingArgs.flags */
147
148 #define LISTARG_TOPICLIMITS     0x0001
149 #define LISTARG_SHOWSECRET      0x0002
150
151 /**
152  * Maximum acceptable lag time in seconds: A channel younger than
153  * this is not protected against hacking admins.
154  * Mainly here to check if the TS clocks really sync (otherwise this
155  * will start causing HACK notices.
156  * This value must be the same on all servers.
157  *
158  * This value has been increased to 1 day in order to distinguish this
159  * "normal" type of HACK wallops / desyncs, from possiblity still
160  * existing bugs.
161  */
162 #define TS_LAG_TIME 86400
163
164 /**
165  * A Magic TS that is used for channels that are created by JOIN,
166  * a channel with this TS accepts all TS without complaining that
167  * it might receive later via MODE or CREATE.
168  *
169  * Part of the P9 compatibility, shouldn't occur on a P10 network.
170  */
171 #define MAGIC_REMOTE_JOIN_TS 1270080000
172
173 /**
174  * used in can_join to determine if an oper forced a join on a channel
175  */
176 #define MAGIC_OPER_OVERRIDE 1000
177
178
179 extern const char* const PartFmt1;
180 extern const char* const PartFmt2;
181 extern const char* const PartFmt1serv;
182 extern const char* const PartFmt2serv;
183
184
185 /*
186  * Structures
187  */
188
189 /** Information about a client on one channel
190  *
191  * This structure forms a sparse matrix with users down the side, and
192  * channels across the top.  This matrix holds all the information about
193  * which users are on what channels, and what modes that user has on that
194  * channel (if they are op'd, voice'd and cached information if they are
195  * banned or not)
196  */
197 struct Membership {
198   struct Client*     user;              /**< The user */
199   struct Channel*    channel;           /**< The channel */
200   struct Membership* next_member;       /**< The next user on this channel */
201   struct Membership* prev_member;       /**< The previous user on this channel*/
202   struct Membership* next_channel;      /**< Next channel this user is on */
203   struct Membership* prev_channel;      /**< Previous channel this user is on*/
204   unsigned int       status;            /**< Flags for op'd, voice'd, etc */
205   unsigned short     oplevel;           /**< Op level */
206 };
207
208 #define MAXOPLEVELDIGITS    3
209 #define MAXOPLEVEL          999
210
211 #define IsZombie(x)         ((x)->status & CHFL_ZOMBIE) /**< see \ref zombie */
212 #define IsDeopped(x)        ((x)->status & CHFL_DEOPPED)
213 #define IsBanned(x)         ((x)->status & CHFL_BANNED)
214 #define IsBanValid(x)       ((x)->status & CHFL_BANVALID)
215 #define IsChanOp(x)         ((x)->status & CHFL_CHANOP)
216 #define OpLevel(x)          ((x)->oplevel)
217 #define HasVoice(x)         ((x)->status & CHFL_VOICE)
218 #define IsServOpOk(x)       ((x)->status & CHFL_SERVOPOK)
219 #define IsBurstJoined(x)    ((x)->status & CHFL_BURST_JOINED)
220 #define IsVoicedOrOpped(x)  ((x)->status & CHFL_VOICED_OR_OPPED)
221 #define IsChannelManager(x) ((x)->status & CHFL_CHANNEL_MANAGER)
222 #define IsUserParting(x)    ((x)->status & CHFL_USER_PARTING)
223 #define IsDelayedJoin(x)    ((x)->status & CHFL_DELAYED)
224
225 #define SetBanned(x)        ((x)->status |= CHFL_BANNED)
226 #define SetBanValid(x)      ((x)->status |= CHFL_BANVALID)
227 #define SetDeopped(x)       ((x)->status |= CHFL_DEOPPED)
228 #define SetServOpOk(x)      ((x)->status |= CHFL_SERVOPOK)
229 #define SetBurstJoined(x)   ((x)->status |= CHFL_BURST_JOINED)
230 #define SetZombie(x)        ((x)->status |= CHFL_ZOMBIE)
231 #define SetChannelManager(x) ((x)->status |= CHFL_CHANNEL_MANAGER)
232 #define SetOpLevel(x, v)    (void)((x)->oplevel = (v))
233 #define SetUserParting(x)   ((x)->status |= CHFL_USER_PARTING)
234 #define SetDelayedJoin(x)   ((x)->status |= CHFL_DELAYED)
235
236 #define ClearBanned(x)      ((x)->status &= ~CHFL_BANNED)
237 #define ClearBanValid(x)    ((x)->status &= ~CHFL_BANVALID)
238 #define ClearDeopped(x)     ((x)->status &= ~CHFL_DEOPPED)
239 #define ClearServOpOk(x)    ((x)->status &= ~CHFL_SERVOPOK)
240 #define ClearBurstJoined(x) ((x)->status &= ~CHFL_BURST_JOINED)
241 #define ClearDelayedJoin(x) ((x)->status &= ~CHFL_DELAYED)
242
243 /** Mode information for a channel */
244 struct Mode {
245   unsigned int mode;
246   unsigned int limit;
247   char key[KEYLEN + 1];
248   char upass[PASSLEN + 1];
249   char apass[PASSLEN + 1];
250 };
251
252 #define BAN_IPMASK         0x0001  /**< ban mask is an IP-number mask */
253 #define BAN_OVERLAPPED     0x0002  /**< ban overlapped, need bounce */
254 #define BAN_BURSTED        0x0004  /**< Ban part of last BURST */
255 #define BAN_BURST_WIPEOUT  0x0008  /**< Ban will be wiped at EOB */
256 #define BAN_DEL            0x4000  /**< Ban is being removed */
257 #define BAN_ADD            0x8000  /**< Ban is being added */
258
259 /** A single ban for a channel. */
260 struct Ban {
261   struct Ban* next;   /**< next ban in the channel */
262   time_t when;        /**< timestamp when ban was added */
263   unsigned int flags; /**< modifier flags for the ban */
264   char *who;          /**< name of client that set the ban */
265   char *banstr;       /**< hostmask that the ban matches */
266 };
267
268 /** Information about a channel */
269 struct Channel {
270   struct Channel*    next;      /**< next channel in the global channel list */
271   struct Channel*    prev;      /**< previous channel */
272   struct Channel*    hnext;     /**< Next channel in the hash table */
273   struct DestructEvent* destruct_event; 
274   time_t             creationtime; /**< Creation time of this channel */
275   time_t             topic_time;   /**< Modification time of the topic */
276   unsigned int       users;        /**< Number of clients on this channel */
277   struct Membership* members;      /**< Pointer to the clients on this channel*/
278   struct SLink*      invites;      /**< List of invites on this channel */
279   struct Ban*        banlist;      /**< List of bans on this channel */
280   struct Mode        mode;         /**< This channels mode */
281   char               topic[TOPICLEN + 1]; /**< Channels topic */
282   char               topic_nick[NICKLEN + 1]; /**< Nick of the person who set
283                                                 *  The topic
284                                                 */
285   char               chname[1];    /**< Dynamically allocated string of the 
286                                      * channel name
287                                      */
288 };
289
290 /** Information about a /list in progress */
291 struct ListingArgs {
292   time_t max_time;
293   time_t min_time;
294   unsigned int max_users;
295   unsigned int min_users;
296   unsigned int flags;
297   time_t max_topic_time;
298   time_t min_topic_time;
299   unsigned int bucket;
300 };
301
302 struct ModeBuf {
303   unsigned int          mb_add;         /**< Modes to add */
304   unsigned int          mb_rem;         /**< Modes to remove */
305   struct Client        *mb_source;      /**< Source of MODE changes */
306   struct Client        *mb_connect;     /**< Connection of MODE changes */
307   struct Channel       *mb_channel;     /**< Channel they affect */
308   unsigned int          mb_dest;        /**< Destination of MODE changes */
309   unsigned int          mb_count;       /**< Number of modes w/args */
310   struct {
311     unsigned int        mbm_type;       /**< Type of argument */
312     union {
313       unsigned int      mbma_uint;      /**< A limit */
314       char             *mbma_string;    /**< A string */
315       struct Client    *mbma_client;    /**< A client */
316     }                   mbm_arg;        /**< The mode argument */
317   }                     mb_modeargs[MAXMODEPARAMS];
318                                         /**< A mode w/args */
319 };
320
321 #define MODEBUF_DEST_CHANNEL    0x00001 /**< Mode is flushed to channel */
322 #define MODEBUF_DEST_SERVER     0x00002 /**< Mode is flushed to server */
323
324 #define MODEBUF_DEST_OPMODE     0x00100 /**< Send server mode as OPMODE */
325 #define MODEBUF_DEST_DEOP       0x00200 /**< Deop the offender */
326 #define MODEBUF_DEST_BOUNCE     0x00400 /**< Bounce the modes */
327 #define MODEBUF_DEST_LOG        0x00800 /**< Log the mode changes to OPATH */
328
329 #define MODEBUF_DEST_HACK2      0x02000 /**< Send a HACK(2) notice, reverse */
330 #define MODEBUF_DEST_HACK3      0x04000 /**< Send a HACK(3) notice, TS == 0 */
331 #define MODEBUF_DEST_HACK4      0x08000 /**< Send a HACK(4) notice, TS == 0 */
332
333 #define MODEBUF_DEST_NOKEY      0x10000 /**< Don't send the real key */
334
335 #define MB_TYPE(mb, i)          ((mb)->mb_modeargs[(i)].mbm_type)
336 #define MB_UINT(mb, i)          ((mb)->mb_modeargs[(i)].mbm_arg.mbma_uint)
337 #define MB_STRING(mb, i)        ((mb)->mb_modeargs[(i)].mbm_arg.mbma_string)
338 #define MB_CLIENT(mb, i)        ((mb)->mb_modeargs[(i)].mbm_arg.mbma_client)
339
340 /** A buffer represeting a list of joins to send */
341 struct JoinBuf {
342   struct Client        *jb_source;      /**< Source of joins (ie, joiner) */
343   struct Client        *jb_connect;     /**< Connection of joiner */
344   unsigned int          jb_type;        /**< Type of join (JOIN or CREATE) */
345   char                 *jb_comment;     /**< part comment */
346   time_t                jb_create;      /**< Creation timestamp */
347   unsigned int          jb_count;       /**< Number of channels */
348   unsigned int          jb_strlen;      /**< length so far */
349   struct Channel       *jb_channels[MAXJOINARGS];
350                                         /**< channels joined or whatever */
351 };
352
353 #define JOINBUF_TYPE_JOIN       0       /**< send JOINs */
354 #define JOINBUF_TYPE_CREATE     1       /**< send CREATEs */
355 #define JOINBUF_TYPE_PART       2       /**< send PARTs */
356 #define JOINBUF_TYPE_PARTALL    3       /**< send local PARTs, but not remote */
357
358 extern struct Channel* GlobalChannelList;
359 extern int             LocalChanOperMode;
360
361 /*
362  * Proto types
363  */
364 extern void clean_channelname(char* name);
365 extern void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
366                           int buflen, struct Channel *chptr,
367                           struct Membership *member);
368 extern int set_mode(struct Client* cptr, struct Client* sptr,
369                     struct Channel* chptr, int parc, char* parv[],
370                     char* mbuf, char* pbuf, char* npbuf, int* badop);
371 extern void send_hack_notice(struct Client *cptr, struct Client *sptr,
372                              int parc, char *parv[], int badop, int mtype);
373 extern struct Channel *get_channel(struct Client *cptr,
374                                    char *chname, ChannelGetType flag);
375 extern struct Membership* find_member_link(struct Channel * chptr,
376                                            const struct Client* cptr);
377 extern int sub1_from_channel(struct Channel* chptr);
378 extern int destruct_channel(struct Channel* chptr);
379 extern int can_join(struct Client *sptr, struct Channel *chptr, char *key);
380 extern void add_user_to_channel(struct Channel* chptr, struct Client* who,
381                                 unsigned int flags, int oplevel);
382 extern void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
383                         const char *param, int *count);
384 extern void add_token_to_sendbuf(char *token, size_t *sblenp, int *firstp,
385                                  int *send_itp, char is_a_ban, int mode);
386 extern int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
387                      int change, int firsttime);
388 extern struct Ban *next_removed_overlapped_ban(void);
389 extern void cancel_mode(struct Client *sptr, struct Channel *chptr, char m,
390                         const char *param, int *count);
391 extern void make_zombie(struct Membership* member, struct Client* who,
392                         struct Client* cptr, struct Client* sptr,
393                         struct Channel* chptr);
394 extern struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing);
395 void add_invite(struct Client *cptr, struct Channel *chptr);
396 int number_of_zombies(struct Channel *chptr);
397
398 extern const char* find_no_nickchange_channel(struct Client* cptr);
399 extern struct Membership* IsMember(struct Client *cptr, struct Channel *chptr);
400 extern struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr);
401 extern int member_can_send_to_channel(struct Membership* member, int reveal);
402 extern int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr, int reveal);
403
404 extern void remove_user_from_channel(struct Client *sptr, struct Channel *chptr);
405 extern void remove_user_from_all_channels(struct Client* cptr);
406
407 extern int is_chan_op(struct Client *cptr, struct Channel *chptr);
408 extern int is_zombie(struct Client *cptr, struct Channel *chptr);
409 extern int has_voice(struct Client *cptr, struct Channel *chptr);
410 /*
411    NOTE: pointer is compared, and not dereferenced, called by
412    add_target with a void*, since targets could be anything,
413    this function can't make any assumptions that it has a channel
414 */
415 extern int IsInvited(struct Client* cptr, const void* chptr);
416 extern void send_channel_modes(struct Client *cptr, struct Channel *chptr);
417 extern char *pretty_mask(char *mask);
418 extern void del_invite(struct Client *cptr, struct Channel *chptr);
419 extern void list_set_default(void); /* this belongs elsewhere! */
420
421 extern void RevealDelayedJoin(struct Membership *member);
422 extern void CheckDelayedJoins(struct Channel *chan);
423
424 extern void modebuf_init(struct ModeBuf *mbuf, struct Client *source,
425                          struct Client *connect, struct Channel *chan,
426                          unsigned int dest);
427 extern void modebuf_mode(struct ModeBuf *mbuf, unsigned int mode);
428 extern void modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode,
429                               unsigned int uint);
430 extern void modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode,
431                                 char *string, int free);
432 extern void modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
433                                 struct Client *client);
434 extern int modebuf_flush(struct ModeBuf *mbuf);
435 extern void modebuf_extract(struct ModeBuf *mbuf, char *buf);
436
437 extern void mode_ban_invalidate(struct Channel *chan);
438 extern void mode_invite_clear(struct Channel *chan);
439
440 extern int mode_parse(struct ModeBuf *mbuf, struct Client *cptr,
441                       struct Client *sptr, struct Channel *chptr,
442                       int parc, char *parv[], unsigned int flags,
443                       struct Membership* member);
444
445 #define MODE_PARSE_SET          0x01    /**< actually set channel modes */
446 #define MODE_PARSE_STRICT       0x02    /**< +m +n +t style not supported */
447 #define MODE_PARSE_FORCE        0x04    /**< force the mode to be applied */
448 #define MODE_PARSE_BOUNCE       0x08    /**< we will be bouncing the modes */
449 #define MODE_PARSE_NOTOPER      0x10    /**< send "not chanop" to user */
450 #define MODE_PARSE_NOTMEMBER    0x20    /**< send "not member" to user */
451 #define MODE_PARSE_WIPEOUT      0x40    /**< wipe out +k and +l during burst */
452 #define MODE_PARSE_BURST        0x80    /**< be even more strict w/extra args */
453
454 extern void joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
455                          struct Client *connect, unsigned int type,
456                          char *comment, time_t create);
457 extern void joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan,
458                          unsigned int flags);
459 extern int joinbuf_flush(struct JoinBuf *jbuf);
460 extern struct Ban *make_ban(const char *banstr);
461 extern void free_ban(struct Ban *ban);
462
463 #endif /* INCLUDED_channel_h */