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

Channels FindChannel(Channels head,char *channel) {
  Channels t;
  t = head;
  while (t) {
    if (!strcasecmp(t->name,channel)) {
      return(t);
      }
    (Channels)t = t->next;
    }
  return(NULL);
  }

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

Channels AddChannel(struct slist *tmp,char *chan) {
  struct slist head;
  Channels node;
  head = *tmp;
  if ((node=(Channels)calloc(1,sizeof(ChansNode)))!=NULL) {
    sprintf(node->name,"%s",chan);
    sprintf(node->topic,"");
    (Channels)node->next=head.channels;
    head.channels = node;   
    }
  else {
    printf("Val: %s not inserted - not enough mem!\n",chan);
    }
  *tmp = head;
  return(node);
  }

int AddUserChannel(UserChanList *head,Channels *channel) {
  UserChanList node;
  if ((node=(UserChanList)calloc(1,sizeof(UserChanList)))!=NULL) {
    node->chan = channel;
    (UserChanList)node->next=*head;
    *head = node;
    }
  return(0);
  }

void DeleteUserChannel(UserChanList *head,char *chan) {
  UserChanList temp,previous,current;
  printf("Test TesT Test: [%s]\n",chan);
  if (!strcasecmp((*head)->chan->name,chan)) {
    temp=*head;
    (UserChanList *)*head=(*head)->next;
    free(temp);
    return;
    }
  else {
    previous=*head;
    (UserChanList)current=(*head)->next;
    while((current!=NULL)&(strcasecmp(current->chan->name,chan))) {
      previous=current;
      (UserChanList)current=current->next;
      }
    if (current!=NULL) {   
      temp=current;  
      previous->next=current->next;
      free(temp);
      return;
      }
    }
  return;
  }

void DeleteChannelUser(ChanUsers *head,char *nick) {
  ChanUsers temp,previous,current;
  if (!strcasecmp((*head)->nick,nick)) {
    temp=*head;
    (ChanUsers *)*head=(*head)->next;
    free(temp);
    return;
    }
  else {
    previous=*head;
    (ChanUsers)current=(*head)->next;
    while((current!=NULL)&(strcasecmp(current->nick,nick))) {
      previous=current;
      (ChanUsers)current=current->next;
      }
    if (current!=NULL) {   
      temp=current;  
      previous->next=current->next;
      free(temp);
      return;
      }
    }
  return;
  }

int AddChannelUser(ChanUsers *head,char *nick,char *mode) {
  ChanUsers node;
  if ((node=(ChanUsers)calloc(1,sizeof(ChannelUsersNode)))!=NULL) {
    node->joined=time(NULL);
    sprintf(node->nick,"%s",nick);
    sprintf(node->mode,mode);
    (ChanUsers)node->next=*head;
    *head = node;
    }
  else {
    printf("Val: %s not inserted - not enough mem!\n",nick);
    return(1);
    }
  return(0);
  }

int ListChannels(Channels head) {
  Channels t;
  t = head;
  printf("All Channel List:\n");
  while (t) {
    printf("Channel Name: [%s]\n",t->name);
    (Channels)t = t->next;
    }
  return(0);
  }

int DoListChannels(Channels head,int s) {
  Channels t;
  t = head;
  SendSock(s,":home.domainatlantic.com 321 Mofo Channel :Users  Name\n");
  while (t) {
    SendSock(s,":home.domainatlantic.com 322 Mofo %s 1 :\n",t->name);
    (Channels)t = t->next;
    }
  SendSock(s,":home.domainatlantic.com 323 Mofo :End of /LIST\n");
  return(0);
  }

int PrintUsers(ChanUsers head) {
  ChanUsers t;
  t = head;
  printf("Channel Users List:\n");
  while (t) {
    printf("Channel Nick: [%s][%s]\n",t->nick,t->mode);
    (ChanUsers)t = t->next;
    }
  return(0);
  }

int PrintChannels(UserChanList head) {
  UserChanList t;
  t = head;
  printf("User Channel List:\n");
  while (t) {
    printf("Channel: [%s]\n",t->chan->name);
    (UserChanList)t = t->next;
    }
  return(0);
  }
int ShowChanPrivmsg(Channels head,Sock shead,struct slist sdata,char *prvmsg) {
  ChanUsers t;
  Users tuser;
  char data[1024];
  t = head->chanusers;
  while (t) {
    tuser = FindNick(sdata.users,t->nick);
    if (strcasecmp(tuser->nick,shead->userinfo->nick)) {
      sprintf(data,":%s!%s@%s PRIVMSG %s %s\n",shead->userinfo->nick,shead->userinfo->ident,shead->userinfo->host,head->name,prvmsg);
      SendSock(tuser->socket,data);
      }
    (ChanUsers)t = t->next;
    }
  return(1);
  }

int ShowChanPart(Channels head,Sock shead,struct slist sdata,char *msg) {
  ChanUsers t;
  Users tuser;
  char data[1024];
  t = head->chanusers;
  while (t) {
    tuser = FindNick(sdata.users,t->nick);
    SendSock(tuser->socket,":%s!%s@%s PART %s %s\n",shead->userinfo->nick,shead->userinfo->ident,shead->userinfo->host,head->name,msg);
    (ChanUsers)t = t->next;
    }
  return(1);
  }

int ShowChanJoin(Channels head,Sock shead,struct slist sdata) {
  ChanUsers t;
  Users tuser;
  char data[1024];
  t = head->chanusers;
  while (t) {
    tuser = FindNick(sdata.users,t->nick);
    if (strcasecmp(tuser->nick,shead->userinfo->nick)) {
      sprintf(data,":%s!%s@%s JOIN :%s\n",shead->userinfo->nick,shead->userinfo->ident,shead->userinfo->host,head->name);
      SendSock(tuser->socket,data);
      }
    (ChanUsers)t = t->next;
    }
  return(1);
  }
  
char *CNList(ChanUsers head) {
  ChanUsers t;
  char data[1024] = "";
  t = head;
  while (t) {
    if (strstr(t->mode,"@")) {
      if (data[0]) {
        sprintf(data,"%s @%s",data,t->nick);
        }
      else {
        sprintf(data,"@%s",t->nick);
        }
      }
    else {
      if (data[0]) {
        sprintf(data,"%s %s",data,t->nick);
        }
      else {
        sprintf(data,"%s",t->nick);
        }
      }
    (ChanUsers)t = t->next;
    }
  return((char *)data);
  }