IRCServer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #ifdef _WIN32
  20. #include <WinSock2.h>
  21. #include <windows.h>
  22. #else
  23. #include <errno.h>
  24. #include <netdb.h>
  25. #include <netinet/in.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/socket.h>
  28. #include <sys/types.h>
  29. #include <unistd.h>
  30. #include "../../common/unix.h"
  31. #endif
  32. #include "../World.h"
  33. #include "IRCReplyCodes.h"
  34. #include "IRCServer.h"
  35. #include "../Chat/Chat.h"
  36. #include "../../common/Log.h"
  37. extern ZoneList zone_list;
  38. extern Chat chat;
  39. IRCServer::IRCServer() {
  40. character_id = 0;
  41. memset(host, 0, sizeof(host));
  42. port = 0;
  43. memset(nick, 0, sizeof(nick));
  44. sockfd = -1;
  45. connected = false;
  46. m_globalServer = false;
  47. }
  48. IRCServer::IRCServer(int32 character_id, const char *host, short port, const char *nick) {
  49. assert(host != NULL);
  50. assert(nick != NULL);
  51. this->character_id = character_id;
  52. strncpy(this->host, host, IRC_HOST_LEN_MAX);
  53. this->port = port;
  54. strncpy(this->nick, nick, IRC_NICK_LEN_MAX);
  55. sockfd = -1;
  56. connected = false;
  57. m_globalServer = false;
  58. }
  59. IRCServer::IRCServer(const char *host, short port, const char *nick) {
  60. assert(host != NULL);
  61. assert(nick != NULL);
  62. this->character_id = 0;
  63. strncpy(this->host, host, IRC_HOST_LEN_MAX);
  64. this->port = port;
  65. strncpy(this->nick, nick, IRC_NICK_LEN_MAX);
  66. sockfd = -1;
  67. connected = false;
  68. m_globalServer = true;
  69. }
  70. IRCServer::~IRCServer() {
  71. Disconnect();
  72. }
  73. IRCChannel * IRCServer::GetChannel(const char *channel_name) {
  74. vector<IRCChannel *>::iterator itr;
  75. IRCChannel *channel = NULL;
  76. assert(channel_name != NULL);
  77. for (itr = channels.begin(); itr != channels.end(); itr++) {
  78. if (strncmp((*itr)->GetName(), channel_name, IRC_CHANNEL_LEN_MAX) == 0) {
  79. channel = *itr;
  80. break;
  81. }
  82. }
  83. return channel;
  84. }
  85. IRCChannel * IRCServer::GetChannel(int32 channel_index) {
  86. //channel indexes start at 1 according to the client
  87. channel_index--;
  88. return channel_index < channels.size() ? channels[channel_index] : NULL;
  89. }
  90. int IRCServer::Connect() {
  91. struct sockaddr_in server_addr;
  92. struct hostent *server;
  93. #ifdef _WIN32
  94. u_long nonblocking = 1;
  95. WSADATA wsa;
  96. #else
  97. int nonblocking = 1;
  98. #endif
  99. //you're already connected numbnuts!
  100. if (connected)
  101. return IRC_CONNECT_ALREADY_CONNECTED;
  102. //make sure a nick was given (should always have one)
  103. if (nick[0] == '\0')
  104. return IRC_CONNECT_NO_NICK;
  105. #ifdef _WIN32
  106. //initialize winsock for version 2.2
  107. if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
  108. Disconnect();
  109. return IRC_CONNECT_WINSOCK_INIT;
  110. }
  111. //make sure we support this version of winsock
  112. if (LOBYTE(wsa.wVersion) != 2 || HIBYTE(wsa.wHighVersion) != 2) {
  113. Disconnect();
  114. return IRC_CONNECT_WINSOCK_VERSION;
  115. }
  116. #endif
  117. //attempt to create a tcp socket
  118. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  119. Disconnect();
  120. return IRC_CONNECT_SOCKET;
  121. }
  122. //find the hostname
  123. if ((server = gethostbyname(host)) == NULL) {
  124. Disconnect();
  125. return IRC_CONNECT_NO_HOST;
  126. }
  127. //setup the server info
  128. memset(&server_addr, 0, sizeof(server_addr));
  129. server_addr.sin_family = AF_INET;
  130. memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
  131. server_addr.sin_port = htons(port);
  132. //connect to the irc server
  133. if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
  134. Disconnect();
  135. return IRC_CONNECT_FAIL;
  136. }
  137. //we want a non blocking socket
  138. #ifdef _WIN32
  139. if (ioctlsocket(sockfd, FIONBIO, &nonblocking) != 0) {
  140. #else
  141. if (ioctl(sockfd, FIONBIO, &nonblocking) != 0) {
  142. #endif
  143. Disconnect();
  144. return IRC_CONNECT_SOCKET_OPT;
  145. }
  146. //woohoo! connected - send the register messages
  147. Send(new IRCMessage("NICK %s", nick));
  148. Send(new IRCMessage("USER %s 0 * :scott", nick));
  149. connected = true;
  150. return IRC_CONNECT_SUCCESS;
  151. }
  152. void IRCServer::Disconnect() {
  153. connected = false;
  154. #ifdef _WIN32
  155. if (sockfd > 0) {
  156. closesocket(sockfd);
  157. sockfd = -1;
  158. }
  159. WSACleanup();
  160. #else
  161. if (sockfd > 0) {
  162. close(sockfd);
  163. sockfd = -1;
  164. }
  165. #endif
  166. vector<IRCChannel*>::iterator itr;
  167. for (itr = channels.begin(); itr != channels.end(); itr++) {
  168. safe_delete(*itr);
  169. }
  170. channels.clear();
  171. }
  172. int IRCServer::JoinChannel(const char *channel_name) {
  173. vector<IRCChannel *>::iterator itr;
  174. IRCChannel *channel;
  175. assert(channel_name != NULL);
  176. //let's make sure they're already not in the channel
  177. for (itr = channels.begin(); itr != channels.end(); itr++) {
  178. if (strncmp((*itr)->GetName(), channel_name, IRC_CHANNEL_LEN_MAX) == 0)
  179. return IRC_JOIN_CHANNEL_ALREADY_IN;
  180. }
  181. //add the channel to the list of channels and mark it as joining. we'll unmark it once we get a response back from the server
  182. channel = new IRCChannel(channel_name);
  183. channel->SetJoining(true);
  184. channels.push_back(channel);
  185. //send the JOIN command to the IRC server
  186. Send(new IRCMessage("JOIN %s", channel_name));
  187. return IRC_JOIN_CHANNEL_SUCCESS;
  188. }
  189. int IRCServer::LeaveChannel(const char *channel_name) {
  190. vector<IRCChannel *>::iterator itr;
  191. IRCChannel *channel;
  192. assert(channel_name != NULL);
  193. for (itr = channels.begin(); itr != channels.end(); itr++) {
  194. channel = *itr;
  195. if (strncmp(channel->GetName(), channel_name, IRC_CHANNEL_LEN_MAX) == 0) {
  196. //send the leave message to the IRC server
  197. Send(new IRCMessage("PART %s", channel->GetName()));
  198. //free resourceses
  199. safe_delete(channel);
  200. channels.erase(itr);
  201. return IRC_LEAVE_CHANNEL_SUCCESS;
  202. }
  203. }
  204. return IRC_LEAVE_CHANNEL_NOT_IN;
  205. }
  206. int IRCServer::Say(const char *channel_name, const char *message) {
  207. IRCChannel *channel;
  208. assert(channel_name != NULL);
  209. assert(message != NULL);
  210. if ((channel = GetChannel(channel_name)) == NULL)
  211. return IRC_SAY_NOT_IN;
  212. Say(channel, message);
  213. return IRC_SAY_SUCCESS;
  214. }
  215. int IRCServer::Say(int32 channel_index, const char *message) {
  216. IRCChannel *channel;
  217. assert(message != NULL);
  218. if ((channel = GetChannel(channel_index)) == NULL)
  219. return IRC_SAY_NOT_IN;
  220. Say(channel, message);
  221. return IRC_SAY_SUCCESS;
  222. }
  223. void IRCServer::Say(IRCChannel *channel, const char *message) {
  224. assert(channel != NULL);
  225. assert(message != NULL);
  226. Send(new IRCMessage("PRIVMSG %s %s", channel->GetName(), message));
  227. }
  228. bool IRCServer::Process() {
  229. char *start, *end;
  230. char buf[8192];
  231. int i, count;
  232. Client *client = 0;
  233. if (!connected)
  234. return false;
  235. //make sure we can find a client associated with this character id
  236. if (!m_globalServer && (client = zone_list.GetClientByCharID(character_id)) == NULL) {
  237. LogWrite(CHAT__ERROR, 0, "IRC", "No client found with character ID %u", character_id);
  238. Disconnect();
  239. return false;
  240. }
  241. //read off the socket - remember this socket is non blocking
  242. memset(buf, 0, sizeof(buf));
  243. count = recv(sockfd, buf, sizeof(buf), 0);
  244. //did server shut us down gracefully?
  245. if (count == 0) {
  246. if (client)
  247. client->Message(CHANNEL_COLOR_YELLOW, "You have been disconnected from IRC server %s.", host);
  248. else
  249. LogWrite(CHAT__DEBUG, 0, "IRC", "You have been disconnected from global IRC server");
  250. Disconnect();
  251. return false;
  252. }
  253. //did we get an error?
  254. //if we get WSAWEOULDBLOCK or EAGAIN, then there was no data for us on the socket this time around, don't disconnect
  255. #ifdef _WIN32
  256. if (count < 0) {
  257. if (WSAGetLastError() == WSAEWOULDBLOCK)
  258. return true;
  259. #else
  260. if (count < 0) {
  261. if (errno == EAGAIN)
  262. return true;
  263. #endif
  264. else {
  265. if (client)
  266. client->Message(CHANNEL_COLOR_YELLOW, "You have been disconnected from IRC server %s because of an error.", host);
  267. else
  268. LogWrite(CHAT__DEBUG, 0, "IRC", "You have been disconnected from global IRC server because of an error");
  269. Disconnect();
  270. return false;
  271. }
  272. }
  273. //if we made it here then we got data!
  274. //irc messages are separated by \r\n, so loop until we find a \n, replace it with a null to create a string then process it
  275. start = buf;
  276. end = buf;
  277. i = 0;
  278. while (i < count) {
  279. if (*end == '\n') {
  280. *end = '\0';
  281. ProcessLine(client, start);
  282. //move to the next potential string
  283. start = end + 1;
  284. }
  285. i++;
  286. end++;
  287. }
  288. return true;
  289. }
  290. void IRCServer::ProcessLine(Client *client, const char *line) {
  291. char *ptr, *ptr2, *ptr3, *ptr4, *findme;
  292. size_t len;
  293. char *copy;
  294. //assert(client != NULL);
  295. assert(line != NULL);
  296. if ((len = strlen(line)) == 0) {
  297. LogWrite(CHAT__DEBUG, 0, "IRC", "Blank line recieved in IRCServer::ProcessLine");
  298. //fprintf(stderr, "Blank line received in IRCServer::ProcessLine\n");
  299. return;
  300. }
  301. LogWrite(CHAT__DEBUG, 7, "IRC", "%s", line);
  302. //printf("%s\n", line);
  303. //copy the line buffer so we dont ruin it with strtok
  304. copy = new char[len + 1];
  305. strncpy(copy, line, len);
  306. if (len >= 4 && strncmp(line, "PING", 4) == 0)
  307. HandlePing();
  308. else if (line[0] == ':') {
  309. ptr = strtok(copy, " :");
  310. if (ptr == NULL) {
  311. LogWrite(CHAT__DEBUG, 0, "IRC", "Unknown IRC line in IRCServer::ProcessLine: '%s'", line);
  312. //fprintf(stderr, "Unknown IRC line in IRCServer::ProcessLine: '%s'\n", line);
  313. }
  314. else {
  315. //:scatman!scatman@hidden-BC7E1801.hsd1.pa.comcast.net PRIVMSG #EQ2Dev :test --from user, public channel msg
  316. //:scatman!scatman@hidden-BC7E1801.hsd1.pa.comcast.net PRIVMSG scatman :test --from user, private message
  317. //is this message from the server or a user?
  318. if (strncmp(ptr, host, IRC_HOST_LEN_MAX) == 0) {
  319. }
  320. else {
  321. ptr2 = strtok(NULL, " :");
  322. if (ptr2 == NULL) {
  323. LogWrite(CHAT__DEBUG, 0, "IRC", "Unknown IRC line in IRCServer::ProcessLine: '%s'", line);
  324. //fprintf(stderr, "Unknown IRC line in IRCServer::ProcessLine: '%s'\n", line);
  325. }
  326. else {
  327. if (strncmp(ptr2, "PRIVMSG", 7) == 0) {
  328. ptr3 = strtok(NULL, " :"); //channel
  329. ptr4 = strtok(NULL, "\r"); //message
  330. //strip the user's address
  331. if ((findme = strstr(ptr, "!")) != NULL)
  332. *findme = '\0';
  333. //remove the leading semi-colon from the beginning of the message
  334. if (ptr4[0] == ':')
  335. ptr4++;
  336. if (ptr[0] == ':')
  337. ptr++;
  338. //client->Message(CHANNEL_COLOR_YELLOW, "%s says to %s: \"%s\"", ptr, ptr3, ptr4);
  339. if (ptr3[0] == '#')
  340. ptr3++;
  341. // Add "[IRC]" to the front of the name
  342. string name = string("[IRC]") + string(ptr);
  343. LogWrite(CHAT__DEBUG, 7, "IRC", "IRC sending chat to EQ2 channel (Channel Name: %s, Message: %s, Name: %s)", ptr3, ptr4, name.c_str());
  344. chat.TellChannel(client, ptr3, ptr4, name.c_str());
  345. }
  346. }
  347. }
  348. }
  349. }
  350. safe_delete_array(copy);
  351. }
  352. void IRCServer::Send(IRCMessage *message) {
  353. const char *msg;
  354. size_t len;
  355. int count;
  356. assert(message != NULL);
  357. msg = message->Serialize();
  358. len = message->GetLength();
  359. if (msg == NULL) {
  360. LogWrite(CHAT__ERROR, 0, "IRC", "Error sending IRC message to server %s. Message was null.", host);
  361. //fprintf(stderr, "Error sending IRC message to server %s. Message was null.", host);
  362. }
  363. else if (len == 0) {
  364. LogWrite(CHAT__ERROR, 0, "IRC", "Error sending IRC message server %s. Message length was 0.", host);
  365. //fprintf(stderr, "Error sending IRC message server %s. Message length was 0.", host);
  366. }
  367. else {
  368. LogWrite(CHAT__DEBUG, 7, "IRC", "IRC MSG: '%s'", msg);
  369. //printf("IRC MSG: '%s'", msg);
  370. if ((count = send(sockfd, msg, len, 0)) != (int)len) {
  371. LogWrite(CHAT__ERROR, 0, "IRC", "Error sending IRC message '%s' to server %s. Tried to write %u bytes but only wrote %i", msg, host, len, count);
  372. //fprintf(stderr, "Error sending IRC message '%s' to server %s. Tried to write %u bytes but only wrote %i", msg, host, len, count);
  373. }
  374. }
  375. safe_delete(message);
  376. }
  377. void IRCServer::HandlePing() {
  378. Send(new IRCMessage("PONG %s", host));
  379. }