#include "ircd.h"

struct UsersList AddUser(int sid,int socket,char *nick,struct UsersList *head) {
  struct UsersList node;
  if ((node=(Users)calloc(1,sizeof(struct UsersList)))!=NULL) {
    node->sid=sid;
    node->socket=socket;
    node->signon=time(NULL);
    node->idlet=time(NULL);
    sprintf(node->nick,"%s",nick);
    sprintf(node->ident,"ident");
    sprintf(node->host,"host");
    (Users)node->next=head;
    head = node;
    numUsers++;
    }
  else {
    printf("Val: %i not inserted - not enough mem!\n",sid);
    }
  return(node);
  }

void DeleteUser(struct UsersList *head,int val) {
  struct UsersList temp,previous,current;
  if (val==head->sid) {
    temp=*head;
    (Users *)*head=(*head)->next; 
    free(temp);
    return;
    }
  else {
    previous=*head;
    (Users)current=(*head)->next;
    while((current!=NULL)&(current->sid!=val)) {
      previous=current;
      (Users)current=current->next;
      }
    if (current!=NULL) {   
      temp=current;  
      previous->next=current->next;
      free(temp);
      return;
      }
    }
    numUsers--;
  return;
  }


Users FindNick(Users head,char *nick) {
  Users t;
  t = head;
  while (t) {
    if (!strcasecmp(t->nick,nick)) {
      return(t);
      }
    (Users)t = t->next;
    }
  return(NULL);
  }

int NickList(Users head) {
  Users t;
  t = head;
  printf("NickList:\n");
  while (t) {
    printf("Nick: =[%s][%i]\n",t->nick,t->socket);
    (Users)t = t->next;
    }
  return(0);
  }
