Chat.cpp 9.0 KB

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