[39] | 1 | #include "IRCClient.h" |
---|
| 2 | #define MAXDATASIZE 512 |
---|
| 3 | #include <fcntl.h> |
---|
| 4 | |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <sys/types.h> |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #define _UNICODE |
---|
| 10 | |
---|
| 11 | #ifdef _MBCS |
---|
| 12 | #undef _MBCS |
---|
| 13 | #endif |
---|
| 14 | |
---|
| 15 | bool IRCClient::InitSock() |
---|
| 16 | { |
---|
| 17 | #ifdef _WIN32 |
---|
| 18 | WSADATA wsaData; //WSAData |
---|
| 19 | if(WSAStartup(MAKEWORD(2,0),&wsaData) != 0) |
---|
| 20 | { |
---|
| 21 | sLog.outError("IRC Error: Winsock Initialization Error"); |
---|
| 22 | return false; |
---|
| 23 | } |
---|
| 24 | #endif |
---|
| 25 | if ((sIRC.SOCKET = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) |
---|
| 26 | { |
---|
| 27 | sLog.outError("IRC Error: Socket Error"); |
---|
| 28 | return false; |
---|
| 29 | } |
---|
| 30 | int on = 1; |
---|
| 31 | if ( setsockopt ( sIRC.SOCKET, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 ) |
---|
| 32 | { |
---|
| 33 | sLog.outError("IRC Error: Invalid Socket"); |
---|
| 34 | return false; |
---|
| 35 | } |
---|
| 36 | #ifdef _WIN32 |
---|
| 37 | u_long iMode = 0; |
---|
| 38 | ioctlsocket(sIRC.SOCKET, FIONBIO, &iMode); |
---|
| 39 | #else |
---|
| 40 | fcntl(sIRC.SOCKET, F_SETFL, O_NONBLOCK); // set to non-blocking |
---|
| 41 | fcntl(sIRC.SOCKET, F_SETFL, O_ASYNC); // set to asynchronous I/O |
---|
| 42 | #endif |
---|
| 43 | return true; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | bool IRCClient::Connect(const char *cHost, int nPort) |
---|
| 47 | { |
---|
| 48 | sIRC.Connected = false; |
---|
| 49 | struct hostent *he; |
---|
| 50 | if ((he=gethostbyname(cHost)) == NULL) |
---|
| 51 | { |
---|
| 52 | sLog.outError("IRCLIENT: Could not resolve host: %s", cHost); |
---|
| 53 | return false; |
---|
| 54 | } |
---|
| 55 | struct sockaddr_in their_addr; |
---|
| 56 | their_addr.sin_family = AF_INET; |
---|
| 57 | their_addr.sin_port = htons(nPort); |
---|
| 58 | their_addr.sin_addr = *((struct in_addr *)he->h_addr); |
---|
| 59 | memset(&(their_addr.sin_zero), '\0', 8); |
---|
| 60 | if (::connect(sIRC.SOCKET, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) |
---|
| 61 | { |
---|
| 62 | sLog.outError("IRCLIENT: Cannot connect to %s", cHost); |
---|
| 63 | return false; |
---|
| 64 | } |
---|
| 65 | //FD_ZERO(&sIRC.sfdset); |
---|
| 66 | //FD_SET(sIRC.SOCKET,&sIRC.sfdset); |
---|
| 67 | sIRC.Connected = true; |
---|
| 68 | return true; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | bool IRCClient::Login(std::string sNick, std::string sUser, std::string sPass) |
---|
| 72 | { |
---|
| 73 | char hostname[128]; |
---|
| 74 | gethostname(hostname, sizeof(hostname)); |
---|
| 75 | if(SendIRC("HELLO")) |
---|
| 76 | if(SendIRC("PASS " + sPass)) |
---|
| 77 | if(SendIRC("NICK " + sNick)) |
---|
| 78 | if(SendIRC("USER " + sUser + " " + (std::string)hostname + " MangChat :MangChat "+sIRC._Mver.c_str())) |
---|
| 79 | return true; |
---|
| 80 | return false; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | bool IRCClient::SendData(const char *data) |
---|
| 84 | { |
---|
| 85 | if(sIRC.Connected) |
---|
| 86 | { |
---|
| 87 | if (send(sIRC.SOCKET, data, strlen(data), 0) == -1) |
---|
| 88 | { |
---|
| 89 | sLog.outError("IRC Error: Socket Receieve ** \n"); |
---|
| 90 | //Disconnect(); |
---|
| 91 | return false; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | return true; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | bool IRCClient::SendIRC(std::string data) |
---|
| 98 | { |
---|
| 99 | std::string RealData = data + "\n"; |
---|
| 100 | return SendData(RealData.c_str()); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void IRCClient::Disconnect() |
---|
| 104 | { |
---|
| 105 | if(sIRC.SOCKET) |
---|
| 106 | { |
---|
| 107 | #ifdef _WIN32 |
---|
| 108 | closesocket(sIRC.SOCKET); |
---|
| 109 | //WSACleanup(); |
---|
| 110 | #else |
---|
| 111 | close(sIRC.SOCKET); |
---|
| 112 | #endif |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void IRCClient::SockRecv() |
---|
| 117 | { |
---|
| 118 | // wchar_t bufferdata; |
---|
| 119 | |
---|
| 120 | char szBuffer[MAXDATASIZE]; |
---|
| 121 | |
---|
| 122 | memset(szBuffer, 0, MAXDATASIZE ); |
---|
| 123 | |
---|
| 124 | int nBytesRecv = ::recv(sIRC.SOCKET, szBuffer, MAXDATASIZE - 1, 0 ); |
---|
| 125 | if ( nBytesRecv == -1 ) |
---|
| 126 | { |
---|
| 127 | sLog.outError("Connection lost."); |
---|
| 128 | sIRC.Connected = false; |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | if (-1 == nBytesRecv) |
---|
| 133 | { |
---|
| 134 | sLog.outError("Error occurred while receiving from socket."); |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | std::string reply; |
---|
| 139 | std::istringstream iss(szBuffer); |
---|
| 140 | while(getline(iss, reply)) |
---|
| 141 | { |
---|
| 142 | Handle_IRC(reply); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|