#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <conio.h>
#include <wslib.h>
#include <misclib.h>

#include "connap.h"
#include "io.h"
#include "connect.h"
#include "message.h"
#include "command.h"
#include "chat.h"
#include "whois.h"
#include "util.h"


/* #define DEBUG */


int send_msg(int type, char *msg)
{
   unsigned short slen, stype;

   slen = BSWAP16((unsigned short)strlen(msg));
   stype = BSWAP16((unsigned short)type);

   memcpy(Buf, &slen, 2);
   memcpy(Buf + 2, &stype, 2);
   strcpy(Buf + 4, msg);

   return (write_sock(srv_sock, Buf, strlen(msg) + 4));
}


void get_msg(void)
{
   unsigned short type, len;
   char tmp[1024], *p;

   if (read_chars(srv_sock, &len, 2) < 2) {
               disconnect("Server EOF");
               return;
   }
   len = BSWAP16(len);

   if (read_chars(srv_sock, &type, 2) < 2) {
               disconnect("Server EOF");
               return;
   }
   type = BSWAP16(type);

   memset(Buf, 0, sizeof(Buf));
   if (read_chars(srv_sock, Buf, len) < len) {
               disconnect("Server EOF");
               return;
   }

#ifdef DEBUG
   printf("type: %d  message: %s\n", type, Buf);
#endif

   switch(type) {
               case MSG_SERVER_ERROR:
                       err_msg(Buf);
               break;

               case MSG_SERVER_EMAIL:
/*
                   (XtTimerCallbackProc)DoLoginStuff, NULL);
*/
               break;

               case MSG_SERVER_REGISTER_OK:
               sprintf(tmp, "%s %s %04d \"%s\" %d %s",
                       user_info.user_name, user_info.password,
                       user_info.data_port, client, user_info.link_type,
                       user_info.email);

               if (send_msg(MSG_CLIENT_LOGIN_REGISTER, tmp)) {
                               disconnect(ws_strerror());
                               return;
               }
               user_info.registered = 1;
/*              write_config(); */
               break;

               case MSG_SERVER_REGISTER_FAIL:
               disconnect("Nick name already registered");
               break;

               case MSG_SERVER_BAD_NICK:
               disconnect("Invalid nick name");
               break;

               case MSG_SERVER_PASS_OK:

               break;

               case MSG_SERVER_SEARCH_RESULT:
/*              AddResult(Buf, 0); */
               break;

               case MSG_SERVER_SEARCH_END:
/*              show_results(0); */
               break;

               case MSG_SERVER_FILE_READY:
/*
               (void)strtok(Buf, " ");
               destIp = BSWAP32(strtoul(strtok(NULL, " "), NULL, 10));
               destPort = atoi(strtok(NULL, " "));
*/
               break;

               case MSG_CLIENT_PRIVMSG:
               rcv_global(type, Buf);
               break;

               case MSG_SERVER_SEND_ERROR:

               break;

               case MSG_SERVER_USER_SIGNON:
/*              UserSignOn(Buf); */
               break;

               case MSG_SERVER_USER_SIGNOFF:
/*              UserSignOff(Buf); */
               break;

               case MSG_SERVER_BROWSE_RESPONSE:
/*              AddResult(Buf, 1); */
               break;

               case MSG_SERVER_BROWSE_END:
/*              ShowResults(1); */
               break;

               case MSG_SERVER_STATS:
               p = strtok(Buf, " ");
               sprintf(tmp, "Users: %s", p);
               p = strtok(NULL, " ");
               sprintf(tmp, "%s  Files: %s", tmp, p);
               break;

               case MSG_SERVER_HOTLIST_ACK:

               break;

               case MSG_SERVER_IGNORE_ENTRY:

               break;

               case MSG_SERVER_NOT_IGNORED:

               break;

               case MSG_SERVER_ALREADY_IGNORED:

               break;

               case MSG_CLIENT_PART:
               do_part_channel(Buf);
               break;

               case MSG_SERVER_PUBLIC:
               rcv_channel(type, Buf);
               break;

               case MSG_SERVER_NOSUCH:
               put_rcvd(Buf, INFO_COLOR);
               break;

               case MSG_SERVER_JOIN_ACK:
               do_join_channel(Buf);
               break;

               case MSG_SERVER_JOIN:
               update_userlist(type, Buf);
               break;

               case MSG_SERVER_PART:
               update_userlist(type, Buf);
               break;

               case MSG_SERVER_CHANNEL_USER_LIST:
               update_userlist(type, Buf);
               break;

               case MSG_SERVER_CHANNEL_USER_LIST_END:
                       show_users(Buf);
               break;

               case MSG_SERVER_TOPIC:
               rcv_channel(type, Buf);
               break;

               case MSG_SERVER_CHANNEL_BAN_LIST:

               break;

               case MSG_SERVER_UPLOAD_FIREWALL:
/*
               fw_upload(Buf);
*/
               break;

               case MSG_SERVER_USER_SPEED:

               break;

               case MSG_SERVER_WHOIS_RESPONSE:
               show_whois(Buf);
               break;

               case MSG_SERVER_WHOWAS:
               show_whowas(Buf);
               break;

               case MSG_SERVER_UPLOAD_REQUEST:
/*
               strcpy(tmp, Buf);
               if (send_msg(MSG_CLIENT_UPLOAD_OK, tmp))
                               disconnect(ws_strerror());
*/
               break;

               case MSG_CLIENT_ALTER_PORT:
/*              set_data_port(atoi(Buf), 0); */
               break;

               case MSG_CLIENT_BANLIST:
/*              do_list_bans();  */
               break;

               case MSG_SERVER_IP_BANLIST:
/*                  AddBan(Buf); */
               break;

               case MSG_SERVER_CHANNEL_LIST_END:
/*              DoListChannels(); */
               break;

               case MSG_SERVER_CHANNEL_LIST:
/*              AddChannel(Buf); */
               break;

               case MSG_SERVER_LIMIT:

               break;

               case MSG_SERVER_MOTD:
               put_rcvd(Buf, NORMAL_COLOR);
               break;

               case MSG_SERVER_DATA_PORT_ERROR:

               break;

               case MSG_SERVER_WALLOP:
               rcv_global(type, Buf);
               break;

               case MSG_SERVER_ANNOUNCE:
               rcv_global(type, Buf);
               break;

               case MSG_SERVER_NICK_BANLIST:

               break;

               case MSG_SERVER_PING:
               rcv_global(type, Buf);
               break;

               case MSG_SERVER_PONG:
               rcv_global(type, Buf);
               break;

               case MSG_SERVER_NAMES_LIST:

               break;

               case MSG_SERVER_FULL_CHANNEL_INFO:

               break;

               case MSG_SERVER_NAMES_LIST_END:

               break;

               case MSG_SERVER_GLOBAL_USER_LIST:

               break;

               case MSG_CLIENT_EMOTE:
               rcv_channel(type, Buf);
               break;

               case MSG_SERVER_LINKS:
/*
               if (! strlen(Buf))
                               DoListLinks();
               else
                               AddLink(Buf);
*/
               break;

               case MSG_SERVER_USAGE_STATS:
/*              DoShowServerStats(Buf); */
               break;

               case MSG_SERVER_USER_MODE:
               put_rcvd(Buf, INFO_COLOR);
               break;
   }
}