/*
** Copyright (C) 2000 by Kevin L. Mitchell <klmitch@mit.edu>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**
** @(#)$Id: ircdb.h,v 1.4 2000/01/27 04:09:47 klmitch Exp $
*/
#ifndef __include_ircdb_h__
#define __include_ircdb_h__

#include <hash.h>

#include "types.h"

typedef struct client_struct client_t;
typedef struct user_struct user_t;
typedef struct host_struct host_t;
typedef struct server_struct server_t;
typedef struct channel_struct channel_t;

#define NUMNICK_LEN	5	/* length of a numeric nick */
#define NICKNAME_LEN	9	/* length of a nickname */
#define USERNAME_LEN	10	/* length of a username */
#define REALNAME_LEN	50	/* length of the realname field */
#define PASSWD_LEN	20	/* length of a password */
#define HOSTNAME_LEN	63	/* length of a hostname */
#define HOSTIP_LEN	15	/* length of a dotted quad */
#define CHKEY_LEN	23	/* length of a channel key */
#define CHTOPIC_LEN	160	/* length of a channel topic */
#define CHNAME_LEN	200	/* length of a channel name */

struct client_struct {
  magic_t	c_magic;	/* magic number */
  client_t     *c_next;		/* next client in list */
  client_t    **c_prev_p;	/* what points to me */
  hash_ent	c_hash;		/* hash table entry */
  flag_t	c_flags;	/* client flags */
  char		c_nick[NICKNAME_LEN + 1];
				/* client nickname */
  char		c_numnick[NUMNICK_LEN + 1];
				/* client numeric nickname */
  char		c_name[REALNAME_LEN + 1];
				/* client full name (or whatever) */
  time_t	c_time;		/* client nick change/signon time */
  union {
    user_t     *i_user;		/* information about the user */
    server_t   *i_server;	/* information about the server */
  }		c_info;		/* information about the client */
};

#define CLIENT_MAGIC	0x4ca08286

struct user_struct {
  magic_t	u_magic;	/* magic number */
  user_t       *u_next;		/* next user in list */
  user_t      **u_prev_p;	/* what points to me */
  host_t       *u_host;		/* hostname */
  host_t       *u_ip;		/* IP address */
  char		u_user[USERNAME_LEN + 1];
				/* username */
  char	       *u_away;		/* away message */
  link_t       *u_channels;	/* list of user's channels */
  client_t     *u_uplink;	/* user's uplink */
  client_t     *u_myself;	/* my client descriptor */
};

#define USER_MAGIC	0x9da7b0c4

struct host_struct {
  magic_t	h_magic;	/* magic number */
  host_t       *h_next;		/* next host in list */
  host_t      **h_prev_p;	/* what points to me */
  hash_ent	h_hash;		/* hash entry for this host */
  int		h_count;	/* reference count */
  union {
    inaddr_t	a_ip;		/* an IP address */
    char	a_host[HOSTNAME_LEN + 1];
				/* a hostname */
  }		h_addr;		/* address data for this host */
  link_t       *h_users;	/* users using this host */
};

#define HOST_IP_MAGIC	0x9d82ec43
#define HOST_NAME_MAGIC	0x7063453d

struct server_struct {
  magic_t	s_magic;	/* magic number */
  server_t     *s_next;		/* next server in list */
  server_t    **s_prev_p;	/* what points to me */
  client_t     *s_uplink;	/* server is a leaf of (relative to me) */
  link_t       *s_users;	/* list of users */
  link_t       *s_servers;	/* list of leaf servers */
  client_t     *s_myself;	/* my client descriptor */
  int		s_hop;		/* hop count */
  int		s_proto;	/* protocol version */
  time_t	s_start;	/* server start time */
  int		s_user_count;	/* count of users on server */
  int		s_server_count;	/* count of servers on server (not uplink) */
  int		s_max_users;	/* size of numeric nick table */
  client_t	s_num_users[1];	/* server numeric nick table */
};

#define SERVER_MAGIC	0x3e599968

struct channel_struct {
  magic_t	ch_magic;	/* magic number */
  channel_t    *ch_next;	/* next channel in list */
  channel_t   **ch_prev_p;	/* what points to me */
  hash_ent	ch_hash;	/* hash entry */
  time_t	ch_time;	/* channel creation time */
  char		ch_topic[CHTOPIC_LEN + 1];
				/* channel topic */
  char		ch_topic_nick[NICKNAME_LEN + 1];
				/* who changed topic */
  time_t	ch_topic_time;	/* time topic last changed */
  int		ch_count;	/* number of users on channel */
  link_t       *ch_users;	/* users on channel */
  struct {
    flag_t	m_flags;	/* channel flags */
    uint	m_limit;	/* channel user limit */
    char	m_key[CHKEY_LEN + 1];
				/* channel key */
    link_t     *m_bans;		/* channel bans */
  }		ch_mode;	/* channel modes */
  char		ch_name[1];	/* channel name */
};

#define CHANNEL_MAGIC	0xe27bc617

extern hash_tab	clients;	/* table of clients */
extern hash_tab	channels;	/* table of channels */
extern hash_tab	hosts;		/* table of host names */
extern hash_tab	ips;		/* table of ip addresses */
extern client_t	*servers[4096];	/* servers by numeric nick */

#endif /* __include_ircdb_h__ */
