Chat.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "Chat.h"
  17. #include "../../common/Log.h"
  18. #include "../../common/ConfigReader.h"
  19. #include "../../common/PacketStruct.h"
  20. #include "../IRC/IRC.h"
  21. extern ConfigReader configReader;
  22. extern IRC irc;
  23. Chat::Chat() {
  24. m_channels.SetName("Chat::Channels");
  25. }
  26. Chat::~Chat() {
  27. vector<ChatChannel *>::iterator itr;
  28. m_channels.writelock(__FUNCTION__, __LINE__);
  29. for (itr = channels.begin(); itr != channels.end(); itr++)
  30. safe_delete(*itr);
  31. m_channels.releasewritelock(__FUNCTION__, __LINE__);
  32. }
  33. void Chat::AddChannel(ChatChannel *channel) {
  34. m_channels.writelock(__FUNCTION__, __LINE__);
  35. channels.push_back(channel);
  36. m_channels.releasewritelock(__FUNCTION__, __LINE__);
  37. }
  38. unsigned int Chat::GetNumChannels() {
  39. unsigned int ret;
  40. m_channels.readlock(__FUNCTION__, __LINE__);
  41. ret = (unsigned int)channels.size();
  42. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  43. return ret;
  44. }
  45. EQ2Packet * Chat::GetWorldChannelList(Client *client) {
  46. PacketStruct *packet_struct = configReader.getStruct("WS_AvailWorldChannels", client->GetVersion());
  47. Player *player = client->GetPlayer();
  48. vector<ChatChannel *> channels_to_send;
  49. vector<ChatChannel *>::iterator itr;
  50. ChatChannel *channel;
  51. EQ2Packet *packet;
  52. int32 i = 0;
  53. bool add;
  54. if (packet_struct == NULL) {
  55. LogWrite(CHAT__ERROR, 0, "Chat", "Could not find packet 'WS_AvailWorldChannels' for client %s on version %i\n", player->GetName(), client->GetVersion());
  56. return NULL;
  57. }
  58. m_channels.readlock(__FUNCTION__, __LINE__);
  59. for (itr = channels.begin(); itr != channels.end(); itr++) {
  60. channel = *itr;
  61. if (channel->GetType() == CHAT_CHANNEL_TYPE_WORLD) {
  62. add = true;
  63. if (add && !channel->CanJoinChannelByLevel(player->GetLevel()))
  64. add = false;
  65. if (add && !channel->CanJoinChannelByRace(player->GetRace()))
  66. add = false;
  67. if (add && !channel->CanJoinChannelByClass(player->GetAdventureClass()))
  68. add = false;
  69. if (add)
  70. channels_to_send.push_back(channel);
  71. }
  72. }
  73. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  74. packet_struct->setArrayLengthByName("num_channels", channels_to_send.size());
  75. for (itr = channels_to_send.begin(); itr != channels_to_send.end(); itr++, i++) {
  76. packet_struct->setArrayDataByName("channel_name", (*itr)->GetName(), i);
  77. packet_struct->setArrayDataByName("unknown", 0, i);
  78. }
  79. packet = packet_struct->serialize();
  80. safe_delete(packet_struct);
  81. return packet;
  82. }
  83. bool Chat::ChannelExists(const char *channel_name) {
  84. vector<ChatChannel *>::iterator itr;
  85. bool ret = false;
  86. m_channels.readlock(__FUNCTION__, __LINE__);
  87. for (itr = channels.begin(); itr != channels.end(); itr++) {
  88. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  89. ret = true;
  90. break;
  91. }
  92. }
  93. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  94. return ret;
  95. }
  96. bool Chat::HasPassword(const char *channel_name) {
  97. vector<ChatChannel *>::iterator itr;
  98. bool ret = false;
  99. m_channels.readlock(__FUNCTION__, __LINE__);
  100. for (itr = channels.begin(); itr != channels.end(); itr++) {
  101. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  102. ret = (*itr)->HasPassword();
  103. break;
  104. }
  105. }
  106. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  107. return ret;
  108. }
  109. bool Chat::PasswordMatches(const char *channel_name, const char *password) {
  110. vector<ChatChannel *>::iterator itr;
  111. bool ret = false;
  112. m_channels.readlock(__FUNCTION__, __LINE__);
  113. for (itr = channels.begin(); itr != channels.end(); itr++) {
  114. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  115. ret = (*itr)->PasswordMatches(password);
  116. break;
  117. }
  118. }
  119. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  120. return ret;
  121. }
  122. bool Chat::CreateChannel(const char *channel_name) {
  123. return CreateChannel(channel_name, NULL);
  124. }
  125. bool Chat::CreateChannel(const char *channel_name, const char *password) {
  126. LogWrite(CHAT__DEBUG, 0, "Chat", "Channel %s being created", channel_name);
  127. ChatChannel *channel = new ChatChannel();
  128. channel->SetName(channel_name);
  129. channel->SetType(CHAT_CHANNEL_TYPE_CUSTOM);
  130. if (password != NULL)
  131. channel->SetPassword(password);
  132. m_channels.writelock(__FUNCTION__, __LINE__);
  133. channels.push_back(channel);
  134. m_channels.releasewritelock(__FUNCTION__, __LINE__);
  135. return true;
  136. }
  137. bool Chat::IsInChannel(Client *client, const char *channel_name) {
  138. vector<ChatChannel *>::iterator itr;
  139. bool ret = false;
  140. m_channels.readlock(__FUNCTION__, __LINE__);
  141. for (itr = channels.begin(); itr != channels.end(); itr++) {
  142. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  143. ret = (*itr)->IsInChannel(client->GetCharacterID());
  144. break;
  145. }
  146. }
  147. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  148. return ret;
  149. }
  150. bool Chat::JoinChannel(Client *client, const char *channel_name) {
  151. vector<ChatChannel *>::iterator itr;
  152. bool ret = false;
  153. LogWrite(CHAT__DEBUG, 1, "Chat", "Client %s is joining channel %s", client->GetPlayer()->GetName(), channel_name);
  154. m_channels.writelock(__FUNCTION__, __LINE__);
  155. for (itr = channels.begin(); itr != channels.end(); itr++) {
  156. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  157. ret = (*itr)->JoinChannel(client);
  158. break;
  159. }
  160. }
  161. m_channels.releasewritelock(__FUNCTION__, __LINE__);
  162. return ret;
  163. }
  164. bool Chat::LeaveChannel(Client *client, const char *channel_name) {
  165. vector<ChatChannel *>::iterator itr;
  166. bool ret = false;
  167. LogWrite(CHAT__DEBUG, 1, "Chat", "Client %s is leaving channel %s", client->GetPlayer()->GetName(), channel_name);
  168. m_channels.writelock(__FUNCTION__, __LINE__);
  169. for (itr = channels.begin(); itr != channels.end(); itr++) {
  170. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  171. ret = (*itr)->LeaveChannel(client);
  172. if ((*itr)->GetType() == CHAT_CHANNEL_TYPE_CUSTOM && (*itr)->GetNumClients() == 0) {
  173. LogWrite(CHAT__DEBUG, 0, "Chat", "Custom channel %s has 0 clients left, deleting channel", channel_name);
  174. safe_delete(*itr);
  175. channels.erase(itr);
  176. }
  177. break;
  178. }
  179. }
  180. m_channels.releasewritelock(__FUNCTION__, __LINE__);
  181. return ret;
  182. }
  183. bool Chat::LeaveAllChannels(Client *client) {
  184. vector<ChatChannel *>::iterator itr;
  185. ChatChannel *channel;
  186. bool erased;
  187. m_channels.writelock(__FUNCTION__, __LINE__);
  188. itr = channels.begin();
  189. while (itr != channels.end()) {
  190. channel = *itr;
  191. erased = false;
  192. if (channel->IsInChannel(client->GetCharacterID())) {
  193. LogWrite(CHAT__DEBUG, 1, "Chat", "Client %s is leaving channel %s", client->GetPlayer()->GetName(), channel->GetName());
  194. channel->LeaveChannel(client);
  195. if (channel->GetType() == CHAT_CHANNEL_TYPE_CUSTOM && channel->GetNumClients() == 0) {
  196. LogWrite(CHAT__DEBUG, 0, "Chat", "Custom channel %s has 0 clients left, deleting channel", channel->GetName());
  197. safe_delete(*itr);
  198. itr = channels.erase(itr);
  199. erased = true;
  200. }
  201. }
  202. if (!erased)
  203. itr++;
  204. }
  205. m_channels.releasewritelock(__FUNCTION__, __LINE__);
  206. return true;
  207. }
  208. bool Chat::TellChannel(Client *client, const char *channel_name, const char *message, const char* name) {
  209. vector<ChatChannel *>::iterator itr;
  210. bool ret = false;
  211. m_channels.readlock(__FUNCTION__, __LINE__);
  212. for (itr = channels.begin(); itr != channels.end(); itr++) {
  213. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  214. if (client && name)
  215. ret = (*itr)->TellChannelClient(client, message, name);
  216. else
  217. ret = (*itr)->TellChannel(client, message, name);
  218. // if client = null then it came from irc, don't send it back to irc
  219. if (client) {
  220. IRCServer* server = 0;
  221. bool global = false;
  222. string msg;
  223. const char* safe_name = irc.GetSafeChannelName(channel_name);
  224. // If global irc channel add "PlayerName says: " to the begining of the message
  225. if ((*itr)->IsGlobalIRCChannel()) {
  226. server = irc.GetGlobalServer();
  227. msg = string(client->GetPlayer()->GetName()) + string(" says: ") + string(message);
  228. global = true;
  229. }
  230. else
  231. server = irc.GetServer(client);
  232. // if global channel don't send a client and send the modified message
  233. if (server && server->GetChannel(safe_name))
  234. irc.Say(global ? 0 : client, channel_name, global ? msg.c_str() : message);
  235. }
  236. break;
  237. }
  238. }
  239. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  240. return ret;
  241. }
  242. bool Chat::SendChannelUserList(Client *client, const char *channel_name) {
  243. vector<ChatChannel *>::iterator itr;
  244. bool ret = false;
  245. m_channels.readlock(__FUNCTION__, __LINE__);
  246. for (itr = channels.begin(); itr != channels.end(); itr++) {
  247. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  248. ret = (*itr)->SendChannelUserList(client);
  249. break;
  250. }
  251. }
  252. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  253. return ret;
  254. }
  255. ChatChannel* Chat::GetChannel(const char *channel_name) {
  256. vector<ChatChannel *>::iterator itr;
  257. ChatChannel* ret = 0;
  258. m_channels.readlock(__FUNCTION__, __LINE__);
  259. for (itr = channels.begin(); itr != channels.end(); itr++) {
  260. if (strncasecmp(channel_name, (*itr)->GetName(), CHAT_CHANNEL_MAX_NAME) == 0) {
  261. ret = (*itr);
  262. break;
  263. }
  264. }
  265. m_channels.releasereadlock(__FUNCTION__, __LINE__);
  266. return ret;
  267. }