#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#include <stdarg.h>

#define MAX_CONFIG_STRING_SIZE 1024
#define BUFFER_SIZE 1024
#define VERSION "0.1"

int numUsers;

struct conf;
struct ChanList;
struct UsersList;
struct Socket;
struct ChannelUsers;
struct Chans;
struct ModInfo;

typedef struct ChannelUsers {
  struct ChannelUsers *next;
  int joined;
  char nick[32];
  char mode[10];
} ChannelUsersNode;
typedef ChannelUsersNode *ChanUsers;

typedef struct Chans {
  struct Chans *next;
  char name[64];
  char topic[256];
  ChanUsers chanusers;
} ChansNode;
typedef ChansNode *Channels;

typedef struct conf {
  int port;
  char servername[256];
  char version[32];
  char network[32];
  char *ip;
  } ConfNode;
typedef ConfNode *Conf;

typedef struct ChanList {
  struct ChanList *next;
  Channels chan;
  } ChanListNode;
typedef ChanListNode *UserChanList;

typedef struct UsersList {
  struct UserList *next;
  UserChanList chans;
  char nick[32];
  char ident[32];
  char host[64];
  char name[128];
  char server[128];
  int sid;
  int socket;
  int idle;
  int idlet;
  int signon;
  };

typedef struct Socket {
  struct Socket *next;
  struct UsersList *userinfo;
  int socket;
  int b;
  int type;
  int idle;
  int id;
  time_t tm;
  int stat;
  char data[1024];
  } SocketNode;
typedef SocketNode *Sock;

typedef struct ModInfo {
  struct Mods *next;
  void *handle;
  void (*void_ptr)();
  char *(*char_ptr)();
  int (*int_ptr)();
  char ModuleName[128];
  char ModuleDescription[512];
  } ModNode;

typedef ModNode *Mods;

struct slist {
  int lid;
  Conf cf;
  Sock sockets;
  Mods modules;
  Users users;
  Channels channels;
  };


Users AddUser(int sid,int socket,char *nick,Users *head);
Users FindNick(Users head,char *nick);
int NickList(Users head);
int Parse(struct slist *lhead,Sock shead);
int DoIdle(Sock *head);
void DeleteUser(Users *head,int val);
int SendLogon(struct slist head,char *nick,int s);
int SendMotd(struct slist head,Sock shead);
int GetHost(char *host, char *ip);
Channels FindChannel(Channels head,char *channel);
Channels AddChannel(struct slist *tmp,char *chan);
int AddChannelUser(ChanUsers *head,char *nick,char *mode);
int PrintUsers(ChanUsers head);
char *CNList(ChanUsers head);
int ShowChanPrivmsg(Channels head,Sock shead,struct slist sdata,char *prvmsg);
int ShowChanJoin(Channels head,Sock shead,struct slist sdata);
int ShowChanPart(Channels head,Sock shead,struct slist sdata,char *msg);
