added basic ssl support to ircu
[ircu2.10.12-pk.git] / include / client.h
1 /*
2  * IRC - Internet Relay Chat, include/client.h
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
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 Structures and functions for handling local clients.
22  * @version $Id$
23  */
24 #ifndef INCLUDED_client_h
25 #define INCLUDED_client_h
26 #ifndef INCLUDED_ircd_defs_h
27 #include "ircd_defs.h"
28 #endif
29 #ifndef INCLUDED_dbuf_h
30 #include "dbuf.h"
31 #endif
32 #ifndef INCLUDED_msgq_h
33 #include "msgq.h"
34 #endif
35 #ifndef INCLUDED_ircd_events_h
36 #include "ircd_events.h"
37 #endif
38 #ifndef INCLUDED_ircd_handler_h
39 #include "ircd_handler.h"
40 #endif
41 #ifndef INCLUDED_res_h
42 #include "res.h"
43 #endif
44 #ifndef INCLUDED_sys_types_h
45 #include <sys/types.h>          /* time_t, size_t */
46 #define INCLUDED_sys_types_h
47 #endif
48
49 struct ConfItem;
50 struct Listener;
51 struct ListingArgs;
52 struct SLink;
53 struct Server;
54 struct User;
55 struct Whowas;
56 struct hostent;
57 struct Privs;
58 struct AuthRequest;
59 struct SSLConnection;
60
61 /*
62  * Structures
63  *
64  * Only put structures here that are being used in a very large number of
65  * source files. Other structures go in the header file of there corresponding
66  * source file, or in the source file itself (when only used in that file).
67  */
68
69 /** Single element in a flag bitset array. */
70 typedef unsigned long flagpage_t;
71
72 /** Number of bits in a flagpage_t. */
73 #define FLAGSET_NBITS (8 * sizeof(flagpage_t))
74 /** Element number for flag \a flag. */
75 #define FLAGSET_INDEX(flag) ((flag) / FLAGSET_NBITS)
76 /** Element bit for flag \a flag. */
77 #define FLAGSET_MASK(flag) (1ul<<((flag) % FLAGSET_NBITS))
78
79 /** Declare a flagset structure of a particular size. */
80 #define DECLARE_FLAGSET(name,max) \
81   struct name \
82   { \
83     unsigned long bits[((max + FLAGSET_NBITS - 1) / FLAGSET_NBITS)]; \
84   }
85
86 /** Test whether a flag is set in a flagset. */
87 #define FlagHas(set,flag) ((set)->bits[FLAGSET_INDEX(flag)] & FLAGSET_MASK(flag))
88 /** Set a flag in a flagset. */
89 #define FlagSet(set,flag) ((set)->bits[FLAGSET_INDEX(flag)] |= FLAGSET_MASK(flag))
90 /** Clear a flag in a flagset. */
91 #define FlagClr(set,flag) ((set)->bits[FLAGSET_INDEX(flag)] &= ~FLAGSET_MASK(flag))
92
93 /** String containing valid user modes, in no particular order. */
94 #define infousermodes "diOoswkgx"
95
96 /** Operator privileges. */
97 enum Priv
98   {
99     PRIV_CHAN_LIMIT, /**< no channel limit on oper */
100     PRIV_MODE_LCHAN, /**< oper can mode local chans */
101     PRIV_WALK_LCHAN, /**< oper can walk through local modes */
102     PRIV_DEOP_LCHAN, /**< no deop oper on local chans */
103     PRIV_SHOW_INVIS, /**< show local invisible users */
104     PRIV_SHOW_ALL_INVIS, /**< show all invisible users */
105     PRIV_UNLIMIT_QUERY, /**< unlimit who queries */
106     PRIV_KILL, /**< oper can KILL */
107     PRIV_LOCAL_KILL, /**< oper can local KILL */
108     PRIV_REHASH, /**< oper can REHASH */
109     PRIV_RESTART, /**< oper can RESTART */
110     PRIV_DIE, /**< oper can DIE */
111     PRIV_GLINE, /**< oper can GLINE */
112     PRIV_LOCAL_GLINE, /**< oper can local GLINE */
113     PRIV_JUPE, /**< oper can JUPE */
114     PRIV_LOCAL_JUPE, /**< oper can local JUPE */
115     PRIV_OPMODE, /**< oper can OP/CLEARMODE */
116     PRIV_LOCAL_OPMODE, /**< oper can local OP/CLEARMODE */
117     PRIV_SET,  /**< oper can SET */
118     PRIV_WHOX, /**< oper can use /who x */
119     PRIV_BADCHAN, /**< oper can BADCHAN */
120     PRIV_LOCAL_BADCHAN, /**< oper can local BADCHAN */
121     PRIV_SEE_CHAN, /**< oper can see in secret chans */
122     PRIV_PROPAGATE, /**< propagate oper status */
123     PRIV_DISPLAY, /**< "Is an oper" displayed */
124     PRIV_SEE_OPERS, /**< display hidden opers */
125     PRIV_WIDE_GLINE, /**< oper can set wider G-lines */
126     PRIV_LIST_CHAN, /**< oper can list secret channels */
127     PRIV_FORCE_OPMODE, /**< can hack modes on quarantined channels */
128     PRIV_FORCE_LOCAL_OPMODE, /**< can hack modes on quarantined local channels */
129     PRIV_APASS_OPMODE, /**< can hack modes +A/-A/+U/-U */
130     PRIV_LAST_PRIV /**< number of privileges */
131   };
132
133 /** Client flags and modes.
134  * Note that flags at least FLAG_LOCAL_UMODES but less than
135  * FLAG_GLOBAL_UMODES are treated as local modes, and flags at least
136  * FLAG_GLOBAL_UMODES (but less than FLAG_LAST_FLAG) are treated as
137  * global modes.
138  */
139 enum Flag
140   {
141     FLAG_PINGSENT,                  /**< Unreplied ping sent */
142     FLAG_DEADSOCKET,                /**< Local socket is dead--Exiting soon */
143     FLAG_KILLED,                    /**< Prevents "QUIT" from being sent for this */
144     FLAG_BLOCKED,                   /**< socket is in a blocked condition */
145     FLAG_CLOSING,                   /**< set when closing to suppress errors */
146     FLAG_UPING,                     /**< has active UDP ping request */
147     FLAG_HUB,                       /**< server is a hub */
148     FLAG_IPV6,                      /**< server understands P10 IPv6 addrs */
149     FLAG_SERVICE,                   /**< server is a service */
150     FLAG_GOTID,                     /**< successful ident lookup achieved */
151     FLAG_DOID,                      /**< I-lines say must use ident return */
152     FLAG_NONL,                      /**< No \n in buffer */
153     FLAG_TS8,                       /**< Why do you want to know? */
154     FLAG_MAP,                       /**< Show server on the map */
155     FLAG_JUNCTION,                  /**< Junction causing the net.burst. */
156     FLAG_BURST,                     /**< Server is receiving a net.burst */
157     FLAG_BURST_ACK,                 /**< Server is waiting for eob ack */
158     FLAG_IPCHECK,                   /**< Added or updated IPregistry data */
159     FLAG_LOCOP,                     /**< Local operator -- SRB */
160     FLAG_SERVNOTICE,                /**< server notices such as kill */
161     FLAG_OPER,                      /**< Operator */
162     FLAG_INVISIBLE,                 /**< makes user invisible */
163     FLAG_WALLOP,                    /**< send wallops to them */
164     FLAG_DEAF,                      /**< Makes user deaf */
165     FLAG_CHSERV,                    /**< Disallow KICK or MODE -o on the user;
166                                        don't display channels in /whois */
167     FLAG_DEBUG,                     /**< send global debug/anti-hack info */
168     FLAG_ACCOUNT,                   /**< account name has been set */
169     FLAG_HIDDENHOST,                /**< user's host is hidden */
170     FLAG_SSLCONN,                   /**< SSL Connection */
171     FLAG_LAST_FLAG,                 /**< number of flags */
172     FLAG_LOCAL_UMODES = FLAG_LOCOP, /**< First local mode flag */
173     FLAG_GLOBAL_UMODES = FLAG_OPER  /**< First global mode flag */
174   };
175
176 /** Declare flagset type for operator privileges. */
177 DECLARE_FLAGSET(Privs, PRIV_LAST_PRIV);
178 /** Declare flagset type for user flags. */
179 DECLARE_FLAGSET(Flags, FLAG_LAST_FLAG);
180
181 #include "capab.h" /* client capabilities */
182
183 /** Represents a local connection.
184  * This contains a lot of stuff irrelevant to server connections, but
185  * those are so rare as to not be worth special-casing.
186  */
187 struct Connection
188 {
189   unsigned long       con_magic;     /**< magic number */
190   struct Connection*  con_next;      /**< Next connection with queued data */
191   struct Connection** con_prev_p;    /**< What points to us */
192   struct Client*      con_client;    /**< Client associated with connection */
193   unsigned int        con_count;     /**< Amount of data in buffer */
194   int                 con_freeflag;  /**< indicates if connection can be freed */
195   int                 con_error;     /**< last socket level error for client */
196   int                 con_sentalong; /**< sentalong marker for connection */
197   unsigned int        con_snomask;   /**< mask for server messages */
198   time_t              con_nextnick;  /**< Next time a nick change is allowed */
199   time_t              con_nexttarget;/**< Next time a target change is allowed */
200   time_t              con_lasttime;  /**< Last time data read from socket */
201   time_t              con_since;     /**< Last time we accepted a command */
202   struct MsgQ         con_sendQ;     /**< Outgoing message queue */
203   struct DBuf         con_recvQ;     /**< Incoming data yet to be parsed */
204   unsigned int        con_sendM;     /**< Stats: protocol messages sent */
205   unsigned int        con_receiveM;  /**< Stats: protocol messages received */
206   uint64_t            con_sendB;     /**< Bytes sent. */
207   uint64_t            con_receiveB;  /**< Bytes received. */
208   struct Listener*    con_listener;  /**< Listening socket which we accepted
209                                         from. */
210   struct SLink*       con_confs;     /**< Associated configuration records. */
211   HandlerType         con_handler;   /**< Message index into command table
212                                         for parsing. */
213   struct ListingArgs* con_listing;   /**< Current LIST status. */
214   unsigned int        con_max_sendq; /**< cached max send queue for client */
215   unsigned int        con_ping_freq; /**< cached ping freq */
216   unsigned short      con_lastsq;    /**< # 2k blocks when sendqueued
217                                         called last. */
218   unsigned char       con_targets[MAXTARGETS]; /**< Hash values of
219                                                   current targets. */
220   char con_sock_ip[SOCKIPLEN + 1];   /**< Remote IP address as a string. */
221   char con_sockhost[HOSTLEN + 1];    /**< This is the host name from
222                                         the socket and after which the
223                                         connection was accepted. */
224   char con_passwd[PASSWDLEN + 1];    /**< Password given by user. */
225   char con_buffer[BUFSIZE];          /**< Incoming message buffer; or
226                                         the error that caused this
227                                         clients socket to close. */
228   struct Socket       con_socket;    /**< socket descriptor for
229                                       client */
230   struct Timer        con_proc;      /**< process latent messages from
231                                       client */
232   struct Privs        con_privs;     /**< Oper privileges */
233   struct CapSet       con_capab;     /**< Client capabilities (from us) */
234   struct CapSet       con_active;    /**< Active capabilities (to us) */
235   struct AuthRequest* con_auth;      /**< Auth request for client */
236   struct SSLConnection* con_ssl;     /**< SSL connection for client */
237 };
238
239 /** Magic constant to identify valid Connection structures. */
240 #define CONNECTION_MAGIC 0x12f955f3
241
242 /** Represents a client anywhere on the network. */
243 struct Client {
244   unsigned long  cli_magic;       /**< magic number */
245   struct Client* cli_next;        /**< link in GlobalClientList */
246   struct Client* cli_prev;        /**< link in GlobalClientList */
247   struct Client* cli_hnext;       /**< link in hash table bucket or this */
248   struct Connection* cli_connect; /**< Connection structure associated with us */
249   struct User*   cli_user;        /**< Defined if this client is a user */
250   struct Server* cli_serv;        /**< Defined if this client is a server */
251   struct Whowas* cli_whowas;      /**< Pointer to ww struct to be freed on quit */
252   char           cli_yxx[4];      /**< Numeric Nick: YY if this is a
253                                      server, XXX if this is a user */
254   time_t         cli_firsttime;   /**< time client was created */
255   time_t         cli_lastnick;    /**< TimeStamp on nick */
256   int            cli_marker;      /**< /who processing marker */
257   struct Flags   cli_flags;       /**< client flags */
258   unsigned int   cli_hopcount;    /**< number of servers to this 0 = local */
259   struct irc_in_addr cli_ip;      /**< Real IP of client */
260   short          cli_status;      /**< Client type */
261   char cli_name[HOSTLEN + 1];     /**< Unique name of the client, nick or host */
262   char cli_username[USERLEN + 1]; /**< Username determined by ident lookup */
263   char cli_info[REALLEN + 1];     /**< Free form additional client information */
264 };
265
266 /** Magic constant to identify valid Client structures. */
267 #define CLIENT_MAGIC 0x4ca08286
268
269 /** Verify that a client is valid. */
270 #define cli_verify(cli)         ((cli)->cli_magic == CLIENT_MAGIC)
271 /** Get client's magic number. */
272 #define cli_magic(cli)          ((cli)->cli_magic)
273 /** Get global next client. */
274 #define cli_next(cli)           ((cli)->cli_next)
275 /** Get global previous client. */
276 #define cli_prev(cli)           ((cli)->cli_prev)
277 /** Get next client in hash bucket chain. */
278 #define cli_hnext(cli)          ((cli)->cli_hnext)
279 /** Get connection associated with client. */
280 #define cli_connect(cli)        ((cli)->cli_connect)
281 /** Get local client that links us to \a cli. */
282 #define cli_from(cli)           con_client(cli_connect(cli))
283 /** Get User structure for client, if client is a user. */
284 #define cli_user(cli)           ((cli)->cli_user)
285 /** Get Server structure for client, if client is a server. */
286 #define cli_serv(cli)           ((cli)->cli_serv)
287 /** Get Whowas link for client. */
288 #define cli_whowas(cli)         ((cli)->cli_whowas)
289 /** Get client numnick. */
290 #define cli_yxx(cli)            ((cli)->cli_yxx)
291 /** Get time we last read data from the client socket. */
292 #define cli_lasttime(cli)       con_lasttime(cli_connect(cli))
293 /** Get time we last parsed something from the client. */
294 #define cli_since(cli)          con_since(cli_connect(cli))
295 /** Get time client was created. */
296 #define cli_firsttime(cli)      ((cli)->cli_firsttime)
297 /** Get time client last changed nickname. */
298 #define cli_lastnick(cli)       ((cli)->cli_lastnick)
299 /** Get WHO marker for client. */
300 #define cli_marker(cli)         ((cli)->cli_marker)
301 /** Get flags flagset for client. */
302 #define cli_flags(cli)          ((cli)->cli_flags)
303 /** Get hop count to client. */
304 #define cli_hopcount(cli)       ((cli)->cli_hopcount)
305 /** Get client IP address. */
306 #define cli_ip(cli)             ((cli)->cli_ip)
307 /** Get status bitmask for client. */
308 #define cli_status(cli)         ((cli)->cli_status)
309 /** Return non-zero if the client is local. */
310 #define cli_local(cli)          (cli_from(cli) == cli)
311 /** Get oper privileges for client. */
312 #define cli_privs(cli)          con_privs(cli_connect(cli))
313 /** Get client capabilities for client */
314 #define cli_capab(cli)          con_capab(cli_connect(cli))
315 /** Get active client capabilities for client */
316 #define cli_active(cli)         con_active(cli_connect(cli))
317 /** Get client name. */
318 #define cli_name(cli)           ((cli)->cli_name)
319 /** Get client username (ident). */
320 #define cli_username(cli)       ((cli)->cli_username)
321 /** Get client realname (information field). */
322 #define cli_info(cli)           ((cli)->cli_info)
323 /** Get client account string. */
324 #define cli_account(cli)        (cli_user(cli) ? cli_user(cli)->account : "0")
325
326 /** Get number of incoming bytes queued for client. */
327 #define cli_count(cli)          con_count(cli_connect(cli))
328 /** Get file descriptor for sending in client's direction. */
329 #define cli_fd(cli)             con_fd(cli_connect(cli))
330 /** Get free flags for the client's connection. */
331 #define cli_freeflag(cli)       con_freeflag(cli_connect(cli))
332 /** Get last error code for the client's connection. */
333 #define cli_error(cli)          con_error(cli_connect(cli))
334 /** Get server notice mask for the client. */
335 #define cli_snomask(cli)        con_snomask(cli_connect(cli))
336 /** Get next time a nick change is allowed for the client. */
337 #define cli_nextnick(cli)       con_nextnick(cli_connect(cli))
338 /** Get next time a target change is allowed for the client. */
339 #define cli_nexttarget(cli)     con_nexttarget(cli_connect(cli))
340 /** Get SendQ for client. */
341 #define cli_sendQ(cli)          con_sendQ(cli_connect(cli))
342 /** Get RecvQ for client. */
343 #define cli_recvQ(cli)          con_recvQ(cli_connect(cli))
344 /** Get count of messages sent to client. */
345 #define cli_sendM(cli)          con_sendM(cli_connect(cli))
346 /** Get number of messages received from client. */
347 #define cli_receiveM(cli)       con_receiveM(cli_connect(cli))
348 /** Get number of bytes (modulo 1024) sent to client. */
349 #define cli_sendB(cli)          con_sendB(cli_connect(cli))
350 /** Get number of bytes (modulo 1024) received from client. */
351 #define cli_receiveB(cli)       con_receiveB(cli_connect(cli))
352 /** Get listener that accepted the client's connection. */
353 #define cli_listener(cli)       con_listener(cli_connect(cli))
354 /** Get list of attached conf lines. */
355 #define cli_confs(cli)          con_confs(cli_connect(cli))
356 /** Get handler type for client. */
357 #define cli_handler(cli)        con_handler(cli_connect(cli))
358 /** Get LIST status for client. */
359 #define cli_listing(cli)        con_listing(cli_connect(cli))
360 /** Get cached max SendQ for client. */
361 #define cli_max_sendq(cli)      con_max_sendq(cli_connect(cli))
362 /** Get ping frequency for client. */
363 #define cli_ping_freq(cli)      con_ping_freq(cli_connect(cli))
364 /** Get lastsq for client's connection. */
365 #define cli_lastsq(cli)         con_lastsq(cli_connect(cli))
366 /** Get the array of current targets for the client.  */
367 #define cli_targets(cli)        con_targets(cli_connect(cli))
368 /** Get the string form of the client's IP address. */
369 #define cli_sock_ip(cli)        con_sock_ip(cli_connect(cli))
370 /** Get the resolved hostname for the client. */
371 #define cli_sockhost(cli)       con_sockhost(cli_connect(cli))
372 /** Get the client's password. */
373 #define cli_passwd(cli)         con_passwd(cli_connect(cli))
374 /** Get the unprocessed input buffer for a client's connection.  */
375 #define cli_buffer(cli)         con_buffer(cli_connect(cli))
376 /** Get the Socket structure for sending to a client. */
377 #define cli_socket(cli)         con_socket(cli_connect(cli))
378 /** Get Timer for processing waiting messages from the client. */
379 #define cli_proc(cli)           con_proc(cli_connect(cli))
380 /** Get auth request for client. */
381 #define cli_auth(cli)           con_auth(cli_connect(cli))
382 /** Get sentalong marker for client. */
383 #define cli_sentalong(cli)      con_sentalong(cli_connect(cli))
384
385 /** Verify that a connection is valid. */
386 #define con_verify(con)         ((con)->con_magic == CONNECTION_MAGIC)
387 /** Get connection's magic number. */
388 #define con_magic(con)          ((con)->con_magic)
389 /** Get global next connection. */
390 #define con_next(con)           ((con)->con_next)
391 /** Get global previous connection. */
392 #define con_prev_p(con)         ((con)->con_prev_p)
393 /** Get locally connected client for connection. */
394 #define con_client(con)         ((con)->con_client)
395 /** Get number of unprocessed data bytes from connection. */
396 #define con_count(con)          ((con)->con_count)
397 /** Get file descriptor for connection. */
398 #define con_fd(con)             s_fd(&(con)->con_socket)
399 /** Get freeable flags for connection. */
400 #define con_freeflag(con)       ((con)->con_freeflag)
401 /** Get last error code on connection. */
402 #define con_error(con)          ((con)->con_error)
403 /** Get sentalong marker for connection. */
404 #define con_sentalong(con)      ((con)->con_sentalong)
405 /** Get server notice mask for connection. */
406 #define con_snomask(con)        ((con)->con_snomask)
407 /** Get next nick change time for connection. */
408 #define con_nextnick(con)       ((con)->con_nextnick)
409 /** Get next new target time for connection. */
410 #define con_nexttarget(con)     ((con)->con_nexttarget)
411 /** Get last time we read from the connection. */
412 #define con_lasttime(con)       ((con)->con_lasttime)
413 /** Get last time we accepted a command from the connection. */
414 #define con_since(con)          ((con)->con_since)
415 /** Get SendQ for connection. */
416 #define con_sendQ(con)          ((con)->con_sendQ)
417 /** Get RecvQ for connection. */
418 #define con_recvQ(con)          ((con)->con_recvQ)
419 /** Get number of messages sent to connection. */
420 #define con_sendM(con)          ((con)->con_sendM)
421 /** Get number of messages received from connection. */
422 #define con_receiveM(con)       ((con)->con_receiveM)
423 /** Get number of bytes (modulo 1024) sent to connection. */
424 #define con_sendB(con)          ((con)->con_sendB)
425 /** Get number of bytes (modulo 1024) received from connection. */
426 #define con_receiveB(con)       ((con)->con_receiveB)
427 /** Get listener that accepted the connection. */
428 #define con_listener(con)       ((con)->con_listener)
429 /** Get list of ConfItems attached to the connection. */
430 #define con_confs(con)          ((con)->con_confs)
431 /** Get command handler for the connection. */
432 #define con_handler(con)        ((con)->con_handler)
433 /** Get the LIST status for the connection. */
434 #define con_listing(con)        ((con)->con_listing)
435 /** Get the maximum permitted SendQ size for the connection. */
436 #define con_max_sendq(con)      ((con)->con_max_sendq)
437 /** Get the ping frequency for the connection. */
438 #define con_ping_freq(con)      ((con)->con_ping_freq)
439 /** Get the lastsq for the connection. */
440 #define con_lastsq(con)         ((con)->con_lastsq)
441 /** Get the current targets array for the connection. */
442 #define con_targets(con)        ((con)->con_targets)
443 /** Get the string-formatted IP address for the connection. */
444 #define con_sock_ip(con)        ((con)->con_sock_ip)
445 /** Get the resolved hostname for the connection. */
446 #define con_sockhost(con)       ((con)->con_sockhost)
447 /** Get the password sent by the remote end of the connection.  */
448 #define con_passwd(con)         ((con)->con_passwd)
449 /** Get the buffer of unprocessed incoming data from the connection. */
450 #define con_buffer(con)         ((con)->con_buffer)
451 /** Get the Socket for the connection. */
452 #define con_socket(con)         ((con)->con_socket)
453 /** Get the Timer for processing more data from the connection. */
454 #define con_proc(con)           ((con)->con_proc)
455 /** Get the oper privilege set for the connection. */
456 #define con_privs(con)          (&(con)->con_privs)
457 /** Get the peer's capabilities for the connection. */
458 #define con_capab(con)          (&(con)->con_capab)
459 /** Get the active capabilities for the connection. */
460 #define con_active(con)         (&(con)->con_active)
461 /** Get the auth request for the connection. */
462 #define con_auth(con)           ((con)->con_auth)
463 /** Get the ssl connection for the connection. */
464 #define con_ssl(con)            ((con)->con_ssl)
465
466 #define STAT_CONNECTING         0x001 /**< connecting to another server */
467 #define STAT_HANDSHAKE          0x002 /**< pass - server sent */
468 #define STAT_ME                 0x004 /**< this server */
469 #define STAT_UNKNOWN            0x008 /**< unidentified connection */
470 #define STAT_UNKNOWN_USER       0x010 /**< connection on a client port */
471 #define STAT_UNKNOWN_SERVER     0x020 /**< connection on a server port */
472 #define STAT_SERVER             0x040 /**< fully registered server */
473 #define STAT_USER               0x080 /**< fully registered user */
474
475 /*
476  * status macros.
477  */
478 /** Return non-zero if the client is registered. */
479 #define IsRegistered(x)         (cli_status(x) & (STAT_SERVER | STAT_USER))
480 /** Return non-zero if the client is an outbound connection that is
481  * still connecting. */
482 #define IsConnecting(x)         (cli_status(x) == STAT_CONNECTING)
483 /** Return non-zero if the client is an outbound connection that has
484  * sent our password. */
485 #define IsHandshake(x)          (cli_status(x) == STAT_HANDSHAKE)
486 /** Return non-zero if the client is this server. */
487 #define IsMe(x)                 (cli_status(x) == STAT_ME)
488 /** Return non-zero if the client has not yet registered. */
489 #define IsUnknown(x)            (cli_status(x) & \
490         (STAT_UNKNOWN | STAT_UNKNOWN_USER | STAT_UNKNOWN_SERVER))
491 /** Return non-zero if the client is an unregistered connection on a
492  * server port. */
493 #define IsServerPort(x)         (cli_status(x) == STAT_UNKNOWN_SERVER )
494 /** Return non-zero if the client is an unregistered connection on a
495  * user port. */
496 #define IsUserPort(x)           (cli_status(x) == STAT_UNKNOWN_USER )
497 /** Return non-zero if the client is a real client connection. */
498 #define IsClient(x)             (cli_status(x) & \
499         (STAT_HANDSHAKE | STAT_ME | STAT_UNKNOWN |\
500          STAT_UNKNOWN_USER | STAT_UNKNOWN_SERVER | STAT_SERVER | STAT_USER))
501 /** Return non-zero if the client ignores flood limits. */
502 #define IsTrusted(x)            (cli_status(x) & \
503         (STAT_CONNECTING | STAT_HANDSHAKE | STAT_ME | STAT_SERVER))
504 /** Return non-zero if the client is a registered server. */
505 #define IsServer(x)             (cli_status(x) == STAT_SERVER)
506 /** Return non-zero if the client is a registered user. */
507 #define IsUser(x)               (cli_status(x) == STAT_USER)
508
509
510 /** Mark a client with STAT_CONNECTING. */
511 #define SetConnecting(x)        (cli_status(x) = STAT_CONNECTING)
512 /** Mark a client with STAT_HANDSHAKE. */
513 #define SetHandshake(x)         (cli_status(x) = STAT_HANDSHAKE)
514 /** Mark a client with STAT_SERVER. */
515 #define SetServer(x)            (cli_status(x) = STAT_SERVER)
516 /** Mark a client with STAT_ME. */
517 #define SetMe(x)                (cli_status(x) = STAT_ME)
518 /** Mark a client with STAT_USER. */
519 #define SetUser(x)              (cli_status(x) = STAT_USER)
520
521 /** Return non-zero if a client is directly connected to me. */
522 #define MyConnect(x)    (cli_from(x) == (x))
523 /** Return non-zero if a client is a locally connected user. */
524 #define MyUser(x)       (MyConnect(x) && IsUser(x))
525 /** Return non-zero if a client is a locally connected IRC operator. */
526 #define MyOper(x)       (MyConnect(x) && IsOper(x))
527 /** Return protocol version used by a server. */
528 #define Protocol(x)     ((cli_serv(x))->prot)
529
530 /*
531  * flags macros
532  */
533 /** Set a flag in a client's flags. */
534 #define SetFlag(cli, flag)  FlagSet(&cli_flags(cli), flag)
535 /** Clear a flag from a client's flags. */
536 #define ClrFlag(cli, flag)  FlagClr(&cli_flags(cli), flag)
537 /** Return non-zero if a flag is set in a client's flags. */
538 #define HasFlag(cli, flag)  FlagHas(&cli_flags(cli), flag)
539
540 /** Return non-zero if the client is an IRC operator (global or local). */
541 #define IsAnOper(x)             (IsOper(x) || IsLocOp(x))
542 /** Return non-zero if the client's connection is blocked. */
543 #define IsBlocked(x)            HasFlag(x, FLAG_BLOCKED)
544 /** Return non-zero if the client's connection is still being burst. */
545 #define IsBurst(x)              HasFlag(x, FLAG_BURST)
546 /** Return non-zero if we have received the peer's entire burst but
547  * not their EOB ack. */
548 #define IsBurstAck(x)           HasFlag(x, FLAG_BURST_ACK)
549 /** Return non-zero if we are still bursting to the client. */
550 #define IsBurstOrBurstAck(x)    (HasFlag(x, FLAG_BURST) || HasFlag(x, FLAG_BURST_ACK))
551 /** Return non-zero if the client has set mode +k (channel service). */
552 #define IsChannelService(x)     HasFlag(x, FLAG_CHSERV)
553 /** Return non-zero if the client's socket is disconnected. */
554 #define IsDead(x)               HasFlag(x, FLAG_DEADSOCKET)
555 /** Return non-zero if the client has set mode +d (deaf). */
556 #define IsDeaf(x)               HasFlag(x, FLAG_DEAF)
557 /** Return non-zero if the client has been IP-checked for clones. */
558 #define IsIPChecked(x)          HasFlag(x, FLAG_IPCHECK)
559 /** Return non-zero if we have received an ident response for the client. */
560 #define IsIdented(x)            HasFlag(x, FLAG_GOTID)
561 /** Return non-zero if the client has set mode +i (invisible). */
562 #define IsInvisible(x)          HasFlag(x, FLAG_INVISIBLE)
563 /** Return non-zero if the client caused a net.burst. */
564 #define IsJunction(x)           HasFlag(x, FLAG_JUNCTION)
565 /** Return non-zero if the client has set mode +O (local operator) locally. */
566 #define IsLocOp(x)              (MyConnect(x) && HasFlag(x, FLAG_LOCOP))
567 /** Return non-zero if the client has set mode +o (global operator). */
568 #define IsOper(x)               HasFlag(x, FLAG_OPER)
569 /** Return non-zero if the client has an active UDP ping request. */
570 #define IsUPing(x)              HasFlag(x, FLAG_UPING)
571 /** Return non-zero if the client has no '\n' in its buffer. */
572 #define NoNewLine(x)            HasFlag(x, FLAG_NONL)
573 /** Return non-zero if the client has set mode +g (debugging). */
574 #define SendDebug(x)            HasFlag(x, FLAG_DEBUG)
575 /** Return non-zero if the client has set mode +s (server notices). */
576 #define SendServNotice(x)       HasFlag(x, FLAG_SERVNOTICE)
577 /** Return non-zero if the client has set mode +w (wallops). */
578 #define SendWallops(x)          HasFlag(x, FLAG_WALLOP)
579 /** Return non-zero if the client claims to be a hub. */
580 #define IsHub(x)                HasFlag(x, FLAG_HUB)
581 /** Return non-zero if the client understands IPv6 addresses in P10. */
582 #define IsIPv6(x)               HasFlag(x, FLAG_IPV6)
583 /** Return non-zero if the client claims to be a services server. */
584 #define IsService(x)            HasFlag(x, FLAG_SERVICE)
585 /** Return non-zero if the client has an account stamp. */
586 #define IsAccount(x)            HasFlag(x, FLAG_ACCOUNT)
587 /** Return non-zero if the client has set mode +x (hidden host). */
588 #define IsHiddenHost(x)         HasFlag(x, FLAG_HIDDENHOST)
589 /** Return non-zero if the client has set mode +S (SSL Connection). */
590 #define IsSSLConn(x)            HasFlag(x, FLAG_SSLCONN)
591 /** Return non-zero if the client has an active PING request. */
592 #define IsPingSent(x)           HasFlag(x, FLAG_PINGSENT)
593
594 /** Return non-zero if the client has operator or server privileges. */
595 #define IsPrivileged(x)         (IsAnOper(x) || IsServer(x))
596 /** Return non-zero if the client's host is hidden. */
597 #define HasHiddenHost(x)        (IsHiddenHost(x) && IsAccount(x))
598
599 /** Mark a client as having an in-progress net.burst. */
600 #define SetBurst(x)             SetFlag(x, FLAG_BURST)
601 /** Mark a client as being between EOB and EOB ACK. */
602 #define SetBurstAck(x)          SetFlag(x, FLAG_BURST_ACK)
603 /** Mark a client as having mode +k (channel service). */
604 #define SetChannelService(x)    SetFlag(x, FLAG_CHSERV)
605 /** Mark a client as having mode +d (deaf). */
606 #define SetDeaf(x)              SetFlag(x, FLAG_DEAF)
607 /** Mark a client as having mode +g (debugging). */
608 #define SetDebug(x)             SetFlag(x, FLAG_DEBUG)
609 /** Mark a client as having ident looked up. */
610 #define SetGotId(x)             SetFlag(x, FLAG_GOTID)
611 /** Mark a client as being IP-checked. */
612 #define SetIPChecked(x)         SetFlag(x, FLAG_IPCHECK)
613 /** Mark a client as having mode +i (invisible). */
614 #define SetInvisible(x)         SetFlag(x, FLAG_INVISIBLE)
615 /** Mark a client as causing a net.join. */
616 #define SetJunction(x)          SetFlag(x, FLAG_JUNCTION)
617 /** Mark a client as having mode +O (local operator). */
618 #define SetLocOp(x)             SetFlag(x, FLAG_LOCOP)
619 /** Mark a client as having mode +o (global operator). */
620 #define SetOper(x)              SetFlag(x, FLAG_OPER)
621 /** Mark a client as having a pending UDP ping. */
622 #define SetUPing(x)             SetFlag(x, FLAG_UPING)
623 /** Mark a client as having mode +w (wallops). */
624 #define SetWallops(x)           SetFlag(x, FLAG_WALLOP)
625 /** Mark a client as having mode +s (server notices). */
626 #define SetServNotice(x)        SetFlag(x, FLAG_SERVNOTICE)
627 /** Mark a client as being a hub server. */
628 #define SetHub(x)               SetFlag(x, FLAG_HUB)
629 /** Mark a client as being an IPv6-grokking server. */
630 #define SetIPv6(x)              SetFlag(x, FLAG_IPV6)
631 /** Mark a client as being a services server. */
632 #define SetService(x)           SetFlag(x, FLAG_SERVICE)
633 /** Mark a client as having an account stamp. */
634 #define SetAccount(x)           SetFlag(x, FLAG_ACCOUNT)
635 /** Mark a client as having mode +x (hidden host). */
636 #define SetHiddenHost(x)        SetFlag(x, FLAG_HIDDENHOST)
637 /** Mark a client as having mode +S (SSL Connection). */
638 #define SetSSLConn(x)           SetFlag(x, FLAG_SSLCONN)
639
640 /** Mark a client as having a pending PING. */
641 #define SetPingSent(x)          SetFlag(x, FLAG_PINGSENT)
642
643 /** Return non-zero if \a sptr sees \a acptr as an operator. */
644 #define SeeOper(sptr,acptr) (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) \
645                             || HasPriv(sptr, PRIV_SEE_OPERS)))
646
647 /** Clear the client's net.burst in-progress flag. */
648 #define ClearBurst(x)           ClrFlag(x, FLAG_BURST)
649 /** Clear the client's between EOB and EOB ACK flag. */
650 #define ClearBurstAck(x)        ClrFlag(x, FLAG_BURST_ACK)
651 /** Remove mode +k (channel service) from the client. */
652 #define ClearChannelService(x)  ClrFlag(x, FLAG_CHSERV)
653 /** Remove mode +d (deaf) from the client. */
654 #define ClearDeaf(x)            ClrFlag(x, FLAG_DEAF)
655 /** Remove mode +g (debugging) from the client. */
656 #define ClearDebug(x)           ClrFlag(x, FLAG_DEBUG)
657 /** Remove the client's IP-checked flag. */
658 #define ClearIPChecked(x)       ClrFlag(x, FLAG_IPCHECK)
659 /** Remove mode +i (invisible) from the client. */
660 #define ClearInvisible(x)       ClrFlag(x, FLAG_INVISIBLE)
661 /** Remove mode +O (local operator) from the client. */
662 #define ClearLocOp(x)           ClrFlag(x, FLAG_LOCOP)
663 /** Remove mode +o (global operator) from the client. */
664 #define ClearOper(x)            ClrFlag(x, FLAG_OPER)
665 /** Clear the client's pending UDP ping flag. */
666 #define ClearUPing(x)           ClrFlag(x, FLAG_UPING)
667 /** Remove mode +w (wallops) from the client. */
668 #define ClearWallops(x)         ClrFlag(x, FLAG_WALLOP)
669 /** Remove mode +s (server notices) from the client. */
670 #define ClearServNotice(x)      ClrFlag(x, FLAG_SERVNOTICE)
671 /** Remove mode +x (hidden host) from the client. */
672 #define ClearHiddenHost(x)      ClrFlag(x, FLAG_HIDDENHOST)
673 /** Remove mode +S (SSL Connection) from the client. */
674 #define ClearSSLConn(x)         ClrFlag(x, FLAG_SSLCONN)
675 /** Clear the client's pending PING flag. */
676 #define ClearPingSent(x)        ClrFlag(x, FLAG_PINGSENT)
677 /** Clear the client's HUB flag. */
678 #define ClearHub(x)             ClrFlag(x, FLAG_HUB)
679
680 /* free flags */
681 #define FREEFLAG_SOCKET 0x0001  /**< socket needs to be freed */
682 #define FREEFLAG_TIMER  0x0002  /**< timer needs to be freed */
683
684 /* server notice stuff */
685
686 #define SNO_ADD         1       /**< Perform "or" on server notice mask. */
687 #define SNO_DEL         2       /**< Perform "and ~x" on server notice mask. */
688 #define SNO_SET         3       /**< Set server notice mask. */
689                                 /* DON'T CHANGE THESE VALUES ! */
690                                 /* THE CLIENTS DEPEND ON IT  ! */
691 #define SNO_OLDSNO      0x1     /**< unsorted old messages */
692 #define SNO_SERVKILL    0x2     /**< server kills (nick collisions) */
693 #define SNO_OPERKILL    0x4     /**< oper kills */
694 #define SNO_HACK2       0x8     /**< desyncs */
695 #define SNO_HACK3       0x10    /**< temporary desyncs */
696 #define SNO_UNAUTH      0x20    /**< unauthorized connections */
697 #define SNO_TCPCOMMON   0x40    /**< common TCP or socket errors */
698 #define SNO_TOOMANY     0x80    /**< too many connections */
699 #define SNO_HACK4       0x100   /**< Uworld actions on channels */
700 #define SNO_GLINE       0x200   /**< glines */
701 #define SNO_NETWORK     0x400   /**< net join/break, etc */
702 #define SNO_IPMISMATCH  0x800   /**< IP mismatches */
703 #define SNO_THROTTLE    0x1000  /**< host throttle add/remove notices */
704 #define SNO_OLDREALOP   0x2000  /**< old oper-only messages */
705 #define SNO_CONNEXIT    0x4000  /**< client connect/exit (ugh) */
706 #define SNO_AUTO        0x8000  /**< AUTO G-Lines */
707 #define SNO_DEBUG       0x10000 /**< debugging messages (DEBUGMODE only) */
708 #define SNO_AUTH        0x20000 /**< IAuth notices */
709
710 /** Bitmask of all valid server notice bits. */
711 #ifdef DEBUGMODE
712 # define SNO_ALL        0x3ffff
713 #else
714 # define SNO_ALL        0x2ffff
715 #endif
716
717 /** Server notice bits allowed to normal users. */
718 #define SNO_USER        (SNO_ALL & ~SNO_OPER)
719
720 /** Server notice bits enabled by default for normal users. */
721 #define SNO_DEFAULT (SNO_NETWORK|SNO_OPERKILL|SNO_GLINE)
722 /** Server notice bits enabled by default for IRC operators. */
723 #define SNO_OPERDEFAULT (SNO_DEFAULT|SNO_HACK2|SNO_HACK4|SNO_THROTTLE|SNO_OLDSNO)
724 /** Server notice bits reserved to IRC operators. */
725 #define SNO_OPER (SNO_CONNEXIT|SNO_OLDREALOP|SNO_AUTH)
726 /** Noisy server notice bits that cause other bits to be cleared during connect. */
727 #define SNO_NOISY (SNO_SERVKILL|SNO_UNAUTH)
728
729 /** Test whether a privilege has been granted to a client. */
730 #define HasPriv(cli, priv)  FlagHas(cli_privs(cli), priv)
731 /** Grant a privilege to a client. */
732 #define SetPriv(cli, priv)  FlagSet(cli_privs(cli), priv)
733 /** Revoke a privilege from a client. */
734 #define ClrPriv(cli, priv)  FlagClr(cli_privs(cli), priv)
735
736 /** Test whether a client has a capability */
737 #define HasCap(cli, cap)    CapHas(cli_capab(cli), (cap))
738 /** Test whether a client has the capability active */
739 #define CapActive(cli, cap) CapHas(cli_active(cli), (cap))
740
741 #define HIDE_IP 0 /**< Do not show IP address in get_client_name() */
742 #define SHOW_IP 1 /**< Show ident and IP address in get_client_name() */
743
744 extern const char* get_client_name(const struct Client* sptr, int showip);
745 extern const char* client_get_default_umode(const struct Client* sptr);
746 extern int client_get_ping(const struct Client* local_client);
747 extern void client_drop_sendq(struct Connection* con);
748 extern void client_add_sendq(struct Connection* con,
749                              struct Connection** con_p);
750 extern void client_set_privs(struct Client *client, struct ConfItem *oper);
751 extern int client_report_privs(struct Client* to, struct Client* client);
752
753 #endif /* INCLUDED_client_h */
754