ChatChannel.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <string.h>
  2. #include "../../common/Log.h"
  3. #include "../../common/ConfigReader.h"
  4. #include "../../common/PacketStruct.h"
  5. #include "../World.h"
  6. #include "ChatChannel.h"
  7. extern ConfigReader configReader;
  8. extern ZoneList zone_list;
  9. #define CHAT_CHANNEL_JOIN 0
  10. #define CHAT_CHANNEL_LEAVE 1
  11. #define CHAT_CHANNEL_OTHER_JOIN 2
  12. #define CHAT_CHANNEL_OTHER_LEAVE 3
  13. ChatChannel::ChatChannel() {
  14. memset(name, 0, sizeof(name));
  15. memset(password, 0, sizeof(password));
  16. type = CHAT_CHANNEL_TYPE_NONE;
  17. level_restriction = 0;
  18. races = 0;
  19. classes = 0;
  20. m_globalIRCChannel = false;
  21. }
  22. ChatChannel::~ChatChannel() {
  23. }
  24. bool ChatChannel::IsInChannel(int32 character_id) {
  25. vector<int32>::iterator itr;
  26. for (itr = clients.begin(); itr != clients.end(); itr++) {
  27. if (character_id == *itr)
  28. return true;
  29. }
  30. return false;
  31. }
  32. bool ChatChannel::JoinChannel(Client *client) {
  33. PacketStruct *packet_struct;
  34. vector<int32>::iterator itr;
  35. Client *to_client;
  36. //send the player join packet to the joining client
  37. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", client->GetVersion())) == NULL) {
  38. LogWrite(CHAT__ERROR, 0, "Chat", "Could not find packet 'WS_ChatChannelUpdate' when client %s was trying to join channel %s", client->GetPlayer()->GetName(), name);
  39. return false;
  40. }
  41. packet_struct->setDataByName("action", CHAT_CHANNEL_JOIN);
  42. packet_struct->setDataByName("channel_name", name);
  43. client->QueuePacket(packet_struct->serialize());
  44. safe_delete(packet_struct);
  45. clients.push_back(client->GetCharacterID());
  46. //loop through everyone else in the channel and send the "other" player join packet
  47. for (itr = clients.begin(); itr != clients.end(); itr++) {
  48. if (client->GetCharacterID() == *itr)
  49. continue;
  50. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  51. continue;
  52. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", to_client->GetVersion())) == NULL)
  53. continue;
  54. packet_struct->setDataByName("action", CHAT_CHANNEL_OTHER_JOIN);
  55. packet_struct->setDataByName("channel_name", name);
  56. packet_struct->setDataByName("player_name", client->GetPlayer()->GetName());
  57. to_client->QueuePacket(packet_struct->serialize());
  58. safe_delete(packet_struct);
  59. }
  60. return true;
  61. }
  62. bool ChatChannel::LeaveChannel(Client *client) {
  63. vector<int32>::iterator itr;
  64. PacketStruct *packet_struct;
  65. Client *to_client;
  66. bool ret = false;
  67. for (itr = clients.begin(); itr != clients.end(); itr++) {
  68. if (client->GetCharacterID() == *itr) {
  69. clients.erase(itr);
  70. ret = true;
  71. break;
  72. }
  73. }
  74. if (ret) {
  75. //send the packet to the leaving client
  76. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", client->GetVersion())) == NULL)
  77. return false;
  78. packet_struct->setDataByName("action", CHAT_CHANNEL_LEAVE);
  79. packet_struct->setDataByName("channel_name", name);
  80. client->QueuePacket(packet_struct->serialize());
  81. safe_delete(packet_struct);
  82. //send the leave packet to all other clients in the channel
  83. for (itr = clients.begin(); itr != clients.end(); itr++) {
  84. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  85. continue;
  86. if (to_client == client) // don't need to send to self.
  87. continue;
  88. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", to_client->GetVersion())) == NULL)
  89. continue;
  90. packet_struct->setDataByName("action", CHAT_CHANNEL_OTHER_LEAVE);
  91. packet_struct->setDataByName("channel_name", name);
  92. packet_struct->setDataByName("player_name", client->GetPlayer()->GetName());
  93. to_client->QueuePacket(packet_struct->serialize());
  94. safe_delete(packet_struct);
  95. }
  96. }
  97. return ret;
  98. }
  99. bool ChatChannel::TellChannel(Client *client, const char *message, const char* name2) {
  100. vector<int32>::iterator itr;
  101. PacketStruct *packet_struct;
  102. Client *to_client;
  103. for (itr = clients.begin(); itr != clients.end(); itr++) {
  104. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  105. continue;
  106. if ((packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion())) == NULL)
  107. continue;
  108. packet_struct->setDataByName("unknown", 0);
  109. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  110. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  111. if (client)
  112. packet_struct->setDataByName("from", client->GetPlayer()->GetName());
  113. else
  114. packet_struct->setDataByName("from", name2);
  115. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  116. packet_struct->setDataByName("channel", to_client->GetMessageChannelColor(CHANNEL_CUSTOM_CHANNEL));
  117. packet_struct->setDataByName("language", 0);
  118. packet_struct->setDataByName("message", message);
  119. packet_struct->setDataByName("channel_name", name);
  120. packet_struct->setDataByName("show_bubble", 1);
  121. packet_struct->setDataByName("understood", 1);
  122. packet_struct->setDataByName("unknown4", 0);
  123. to_client->QueuePacket(packet_struct->serialize());
  124. safe_delete(packet_struct);
  125. }
  126. return true;
  127. }
  128. bool ChatChannel::TellChannelClient(Client* to_client, const char* message, const char* name2) {
  129. PacketStruct *packet_struct;
  130. if (string(name2).find('[') != string::npos)
  131. return true;
  132. packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion());
  133. if (packet_struct) {
  134. packet_struct->setDataByName("unknown", 0);
  135. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  136. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  137. packet_struct->setDataByName("from", name2);
  138. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  139. packet_struct->setDataByName("channel", to_client->GetMessageChannelColor(CHANNEL_CUSTOM_CHANNEL));
  140. packet_struct->setDataByName("language", 0);
  141. packet_struct->setDataByName("message", message);
  142. packet_struct->setDataByName("channel_name", name);
  143. packet_struct->setDataByName("show_bubble", 1);
  144. packet_struct->setDataByName("understood", 1);
  145. packet_struct->setDataByName("unknown4", 0);
  146. to_client->QueuePacket(packet_struct->serialize());
  147. }
  148. safe_delete(packet_struct);
  149. return true;
  150. }
  151. bool ChatChannel::SendChannelUserList(Client *client) {
  152. vector<int32>::iterator itr;
  153. PacketStruct *packet_struct;
  154. Client *to_client;
  155. int8 i = 0;
  156. if ((packet_struct = configReader.getStruct("WS_WhoChannelQueryReply", client->GetVersion())) == NULL)
  157. return false;
  158. packet_struct->setDataByName("channel_name", name);
  159. packet_struct->setDataByName("unknown", 0);
  160. packet_struct->setArrayLengthByName("num_players", clients.size());
  161. for (itr = clients.begin(); itr != clients.end(); itr++) {
  162. if ((to_client = zone_list.GetClientByCharID(*itr)) != NULL)
  163. packet_struct->setArrayDataByName("player_name", client->GetPlayer()->GetName(), i++);
  164. else
  165. packet_struct->setArrayDataByName("player_name", "<Unknown>", i++);
  166. }
  167. client->QueuePacket(packet_struct->serialize());
  168. safe_delete(packet_struct);
  169. return true;
  170. }