#include "ircd.h"
#include "socket.h"

int main(int argc,char **argv) {
  int s,con;
  struct slist sdata;
  Sock current;
  sdata.sockets = NULL;
  sdata.users = NULL;
  sdata.channels = NULL;
  sdata.cf = (Conf)malloc(sizeof(ConfNode));
  sdata.cf->port = 6667;
  sdata.cf->ip = "0";
  sprintf(sdata.cf->servername,"irc.got-unix.net");
  sprintf(sdata.cf->version,"UbIRCD-0.1a");
  sprintf(sdata.cf->network,"None");
  sdata.lid = 1;
  s = StartSS(sdata.cf);
  while (1) {
    con = GetConnection(s,sdata.cf);
    if (con > 0) {
      AddSock(con,&sdata);
      printf("New Socket: [%i]\n",con);
      }
    current = sdata.sockets;
    while (current!=NULL) {
      if (ReadSock(current)) {
        printf("(%s)\n",current->data);
        DoIdle(&current);
        Parse(&sdata,current);
        //SendSock(current->socket,current->data);
        }
      if (current->socket == -1) {
        DeleteUser(&sdata.users,current->id);
        DeleteSock(&sdata.sockets,current->id);
        }
      //printf("[%i][%i]\n",current->socket,current->id);
      current=current->next;
      }
    }
  return(0);
  }

int DoIdle(Sock *head) {
  Sock t = *head;
  if (t->type >= 2) {
    t->userinfo->idle = time(NULL)-t->userinfo->idlet;
    }
  *head = t;
  return(0);
  }
