#ifndef CLIENT_H
#define CLIENT_H

#define MAX_CLIENTS 20
#define MAX_NICK 17 /* 16 chars + NUL */
#define MAX_USER_CHANNELS 10
#define MAX_AWAY_REASON 512

#include "iopause.h"
#include "buffer.h"

#include "channel.h"

extern const char* servername;

struct client {
  int active;
  int fd;
  iopause_fd* io;

  char in[4096];
  unsigned int in_len;
  char out_space[4096];
  buffer out;

  char nick[MAX_NICK];
  struct channel channels[MAX_USER_CHANNELS];

  int is_away;
  char away_reason[MAX_AWAY_REASON];

  struct tai stamp;
};

struct member_list {
  int n;
  struct client* v[MAX_CLIENTS];
};

struct channel_list {
  int n;
  const char* v[MAX_USER_CHANNELS*MAX_CLIENTS];
};

extern struct client clients[MAX_CLIENTS];

void client_new(int fd);
void client_read(struct client* c);

void client_reply_start(struct client* c, const char* code);
void client_reply_notregistered(struct client* c);
void client_message_start(struct client* c, struct client *f, const char* msg);
void client_puts(struct client* c, const char* buf);
void client_put(struct client* c, const char* buf, unsigned int len);

void client_flush(struct client* c);
void client_drop(struct client* c, const char* reason, int len);

struct client* client_find_by_nick(const char* s);

int client_is_member(const struct client* c, const char* channel);
int client_add_channel(struct client* c, const char* channel);
void client_del_channel(struct client* c, const char* channel);
void client_listeners(const struct client* c, struct member_list* members);

void client_remove_from_list(const struct client* c, struct member_list* m);

#endif /* CLIENT_H */
