#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/un.h>
#include <netdb.h>

#include "OIrc.h"

char * _modenames[] = {"NOTICE", "PRIVMSG"};

//----------------------------------------------------------------------

OIrc::OIrc() {
  _connected = 0;
}

OIrc::~OIrc() {
}

int OIrc::ProcessMessage(char * message) {
  char char1[TCP_BUFFER_LENGTH], char2[TCP_BUFFER_LENGTH],
  char3[TCP_BUFFER_LENGTH], char4[TCP_BUFFER_LENGTH],
  char5[TCP_BUFFER_LENGTH], command[TCP_BUFFER_LENGTH];
  int int1, int2, int3;

  if (sscanf(message, "PING :%s", char1) == 1) {
    sprintf(command, "PONG :%s\n", char1);
    Send(command);
  } else if ((sscanf(message,":%s %i %s @ %s :%n",
                            char1,&int1,char2,char3,&int2)==4) ||
             (sscanf(message,":%s %i %s = %s :%n",
                             char1,&int1,char2,char3,&int2)==4)) {
    switch (int1) {
      case 353:
        ProcessNickList(char3, message+int2);
        break;
      default:
        Log(message);
        break;
    }
  } else if (sscanf(message,":%s %i %s :%n",char1,&int1,char2,&int2)==3) {
    switch (int1) {
      case 1:
      case 2:
      case 3:
      case 251:
      case 252:
      case 253:
      case 254:
      case 255:
      case 265:
      case 266:
      case 372:
      case 375:
      case 377:
        Log(message+int2, "cyan");
        break;
      default:
        Log(message, "red");
        break;
    }
  } else if (sscanf(message,":%s %i %s %n",char1,&int1,char2,&int2)==3) {
    switch (int1) {
      case 375:
      case 376:
        break;
      default:
        Log(message, "red");
        break;
    }
  } else if (sscanf(message,":%[^!]!%s %n",
                             char1,char2,&int1)==2) {
    if (sscanf(message+int1,"PRIVMSG %s :%n", char3,&int2)==1) {
      ProcessMsg(char3, char1, message+int1+int2, PRIVMSG);
    } else if (sscanf(message+int1,"NOTICE %s :%n", char3,&int2)==1) {
      ProcessMsg(char3, char1, message+int1+int2, NOTICE);
    } else if (sscanf(message+int1,"JOIN :%s", char3)==1) {
      ProcessJoin(char3, char1);
    } else if (sscanf(message+int1,"PART %s", char3)==1) {
      ProcessPart(char3, char1);
    } else if (sscanf(message+int1,"NICK :%s", char3)==1) {
      ProcessNick(char1, char3);
    } else if (sscanf(message+int1,"QUIT %s", char3)==1) {
      ProcessQuit(char1);
    } else {
      Log(message, "red");
    }
  } else {
    Log(message, "red");
  }
  return 0;
}

void OIrc::ProcessNickList(char * channel, char * nicklist) {
}

void OIrc::ProcessJoin(char * channel, char * nick) {
}

void OIrc::ProcessPart(char * channel, char * nick) {
}

void OIrc::ProcessNick(char * old_nick /* :*] */, char * new_nick) {
}

void OIrc::ProcessQuit(char * nick) {
}

void OIrc::ProcessMsg(char * channel, char * nick, char * msg, int mode) {
}

void OIrc::CTCPSend(char * nick, char * command, int mode) {
  char s[TCP_BUFFER_LENGTH];

  if ((mode >= 0) && (mode <=1)) {
    sprintf(s, "%s %s :%c%s%c\n", _modenames[mode], nick, 1, command, 1);
    Send(s);
  }
}
