9
3

ChatChannel.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", to_client->GetVersion())) == NULL)
  87. continue;
  88. packet_struct->setDataByName("action", CHAT_CHANNEL_OTHER_LEAVE);
  89. packet_struct->setDataByName("channel_name", name);
  90. packet_struct->setDataByName("player_name", client->GetPlayer()->GetName());
  91. to_client->QueuePacket(packet_struct->serialize());
  92. safe_delete(packet_struct);
  93. }
  94. }
  95. return ret;
  96. }
  97. bool ChatChannel::TellChannel(Client *client, const char *message, const char* name2) {
  98. vector<int32>::iterator itr;
  99. PacketStruct *packet_struct;
  100. Client *to_client;
  101. for (itr = clients.begin(); itr != clients.end(); itr++) {
  102. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  103. continue;
  104. if ((packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion())) == NULL)
  105. continue;
  106. packet_struct->setDataByName("unknown", 0);
  107. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  108. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  109. if (client)
  110. packet_struct->setDataByName("from", client->GetPlayer()->GetName());
  111. else
  112. packet_struct->setDataByName("from", name2);
  113. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  114. packet_struct->setDataByName("channel", 34);
  115. packet_struct->setDataByName("language", 0);
  116. packet_struct->setDataByName("message", message);
  117. packet_struct->setDataByName("channel_name", name);
  118. packet_struct->setDataByName("show_bubble", 1);
  119. packet_struct->setDataByName("understood", 1);
  120. packet_struct->setDataByName("unknown4", 0);
  121. to_client->QueuePacket(packet_struct->serialize());
  122. safe_delete(packet_struct);
  123. }
  124. return true;
  125. }
  126. bool ChatChannel::TellChannelClient(Client* to_client, const char* message, const char* name2) {
  127. PacketStruct *packet_struct;
  128. if (string(name2).find('[') != string::npos)
  129. return true;
  130. packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion());
  131. if (packet_struct) {
  132. packet_struct->setDataByName("unknown", 0);
  133. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  134. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  135. packet_struct->setDataByName("from", name2);
  136. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  137. packet_struct->setDataByName("channel", 34);
  138. packet_struct->setDataByName("language", 0);
  139. packet_struct->setDataByName("message", message);
  140. packet_struct->setDataByName("channel_name", name);
  141. packet_struct->setDataByName("show_bubble", 1);
  142. packet_struct->setDataByName("understood", 1);
  143. packet_struct->setDataByName("unknown4", 0);
  144. to_client->QueuePacket(packet_struct->serialize());
  145. }
  146. safe_delete(packet_struct);
  147. return true;
  148. }
  149. bool ChatChannel::SendChannelUserList(Client *client) {
  150. vector<int32>::iterator itr;
  151. PacketStruct *packet_struct;
  152. Client *to_client;
  153. int8 i = 0;
  154. if ((packet_struct = configReader.getStruct("WS_WhoChannelQueryReply", client->GetVersion())) == NULL)
  155. return false;
  156. packet_struct->setDataByName("channel_name", name);
  157. packet_struct->setDataByName("unknown", 0);
  158. packet_struct->setArrayLengthByName("num_players", clients.size());
  159. for (itr = clients.begin(); itr != clients.end(); itr++) {
  160. if ((to_client = zone_list.GetClientByCharID(*itr)) != NULL)
  161. packet_struct->setArrayDataByName("player_name", client->GetPlayer()->GetName(), i++);
  162. else
  163. packet_struct->setArrayDataByName("player_name", "<Unknown>", i++);
  164. }
  165. client->QueuePacket(packet_struct->serialize());
  166. safe_delete(packet_struct);
  167. return true;
  168. }