PageRenderTime 150ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/0.6/src/game/ChatHandler.cpp

https://github.com/freghar/mangos-svn-backup
C++ | 381 lines | 293 code | 62 blank | 26 comment | 68 complexity | bf57c02a2fa62d99e7e341c6e8357510 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. /*
  2. * Copyright (C) 2005,2006,2007 MaNGOS <http://www.mangosproject.org/>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "Common.h"
  19. #include "Log.h"
  20. #include "WorldPacket.h"
  21. #include "WorldSession.h"
  22. #include "World.h"
  23. #include "Opcodes.h"
  24. #include "ObjectMgr.h"
  25. #include "Chat.h"
  26. #include "Database/DatabaseEnv.h"
  27. #include "ChannelMgr.h"
  28. #include "Group.h"
  29. #include "Guild.h"
  30. #include "MapManager.h"
  31. #include "ObjectAccessor.h"
  32. #include "ScriptCalls.h"
  33. void WorldSession::HandleMessagechatOpcode( WorldPacket & recv_data )
  34. {
  35. WorldPacket data;
  36. uint32 type;
  37. uint32 lang;
  38. recv_data >> type;
  39. recv_data >> lang;
  40. //sLog.outDebug("CHAT: packet received. type %u, lang %u", type, lang );
  41. // prevent talking at unknown language (cheating)
  42. LanguageDesc const* langDesc = GetLanguageDescByID(lang);
  43. if(!langDesc || langDesc->skill_id != 0 && !_player->HasSkill(langDesc->skill_id))
  44. {
  45. SendNotification("Unknown language");
  46. return;
  47. }
  48. if (sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION))
  49. lang = LANG_UNIVERSAL;
  50. switch(type)
  51. {
  52. case CHAT_MSG_SAY:
  53. case CHAT_MSG_EMOTE: // "/me text" emote type
  54. {
  55. std::string msg = "";
  56. recv_data >> msg;
  57. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  58. break;
  59. if(type==CHAT_MSG_SAY)
  60. {
  61. sChatHandler.FillMessageData( &data, this, type, lang, NULL, 0, msg.c_str() );
  62. GetPlayer()->SendMessageToSet( &data, true );
  63. }
  64. else
  65. {
  66. std::ostringstream msg2;
  67. msg2 << GetPlayer()->GetName() << " " << msg;
  68. sChatHandler.FillMessageData( &data, this, type, lang, NULL, 0, msg2.str().c_str() );
  69. GetPlayer()->SendMessageToOwnTeamSet( &data, true );
  70. }
  71. } break;
  72. case CHAT_MSG_PARTY:
  73. {
  74. std::string msg = "";
  75. recv_data >> msg;
  76. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  77. break;
  78. Group *group = GetPlayer()->groupInfo.group;
  79. if(!group)
  80. return;
  81. WorldPacket data;
  82. sChatHandler.FillMessageData(&data, this, CHAT_MSG_PARTY, lang, NULL, 0, msg.c_str());
  83. group->BroadcastPacket(&data, group->GetMemberGroup(GetPlayer()->GetGUID()));
  84. }
  85. break;
  86. case CHAT_MSG_GUILD:
  87. {
  88. std::string msg = "";
  89. recv_data >> msg;
  90. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  91. break;
  92. if (GetPlayer()->GetGuildId())
  93. {
  94. Guild *guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
  95. if (guild)
  96. guild->BroadcastToGuild(this, msg);
  97. }
  98. break;
  99. }
  100. case CHAT_MSG_OFFICER:
  101. {
  102. std::string msg = "";
  103. recv_data >> msg;
  104. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  105. break;
  106. if (GetPlayer()->GetGuildId())
  107. {
  108. Guild *guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
  109. if (guild)
  110. guild->BroadcastToOfficers(this, msg);
  111. }
  112. break;
  113. }
  114. case CHAT_MSG_YELL:
  115. {
  116. std::string msg = "";
  117. recv_data >> msg;
  118. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  119. break;
  120. sChatHandler.FillMessageData(&data, this, type, lang, NULL, 0, msg.c_str() );
  121. //please test this, its important that this is correct. I leave the previouse code becouse of this.
  122. GetPlayer()->SendMessageToSet( &data, true );
  123. //SendPacket(&data);
  124. //sWorld.SendGlobalMessage(&data, this);
  125. } break;
  126. case CHAT_MSG_WHISPER:
  127. {
  128. std::string to, msg;
  129. recv_data >> to >> msg;
  130. if(to.size() == 0)
  131. {
  132. WorldPacket data(SMSG_CHAT_PLAYER_NOT_FOUND, (to.size()+1));
  133. data<<to;
  134. SendPacket(&data);
  135. break;
  136. }
  137. normalizePlayerName(to);
  138. Player *player = objmgr.GetPlayer(to.c_str());
  139. // send whispers from player to GM only if GM accept its (not show online state GM in other case)
  140. if(!player || GetSecurity() == 0 && player->GetSession()->GetSecurity() > 0 && !player->isAcceptWhispers())
  141. {
  142. WorldPacket data(SMSG_CHAT_PLAYER_NOT_FOUND, (to.size()+1));
  143. data<<to;
  144. SendPacket(&data);
  145. break;
  146. }
  147. if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION) && GetSecurity() == 0 && player->GetSession()->GetSecurity() == 0 )
  148. {
  149. uint32 sidea = GetPlayer()->GetTeam();
  150. uint32 sideb = player->GetTeam();
  151. if( sidea != sideb )
  152. {
  153. WorldPacket data(SMSG_CHAT_PLAYER_NOT_FOUND, (to.size()+1));
  154. data<<to;
  155. SendPacket(&data);
  156. break;
  157. }
  158. }
  159. sChatHandler.FillMessageData(&data, this, type, lang, NULL, 0, msg.c_str() );
  160. player->GetSession()->SendPacket(&data);
  161. sChatHandler.FillMessageData(&data,this,CHAT_MSG_WHISPER_INFORM,lang,NULL,player->GetGUID(),msg.c_str() );
  162. SendPacket(&data);
  163. if(player->isAFK())
  164. {
  165. sChatHandler.FillMessageData(&data, this, CHAT_MSG_AFK, LANG_UNIVERSAL, NULL, player->GetGUID(), player->afkMsg.c_str());
  166. SendPacket(&data);
  167. }
  168. if(player->isDND())
  169. {
  170. sChatHandler.FillMessageData(&data, this, CHAT_MSG_DND, LANG_UNIVERSAL, NULL, player->GetGUID(), player->dndMsg.c_str());
  171. SendPacket(&data);
  172. }
  173. // Auto enable whispers accepting at sending whispers
  174. if(!GetPlayer()->isAcceptWhispers())
  175. {
  176. GetPlayer()->SetAcceptWhispers(true);
  177. sChatHandler.SendSysMessage(this ,"Whispers accepting now: ON");
  178. }
  179. } break;
  180. case CHAT_MSG_CHANNEL:
  181. {
  182. std::string channel = "", msg = "";
  183. recv_data >> channel;
  184. recv_data >> msg;
  185. if(ChannelMgr* cMgr = channelMgr(GetPlayer()->GetTeam()))
  186. if(Channel *chn = cMgr->GetChannel(channel,GetPlayer()))
  187. chn->Say(GetPlayer(),msg.c_str(),lang);
  188. } break;
  189. case CHAT_MSG_AFK:
  190. {
  191. std::string msg;
  192. recv_data >> msg;
  193. GetPlayer()->afkMsg = msg;
  194. if((msg.size() == 0 || !GetPlayer()->isAFK()) && !GetPlayer()->isInCombat() )
  195. {
  196. GetPlayer()->ToggleAFK();
  197. if(GetPlayer()->isAFK() && GetPlayer()->isDND())
  198. GetPlayer()->ToggleDND();
  199. }
  200. } break;
  201. case CHAT_MSG_DND:
  202. {
  203. std::string msg;
  204. recv_data >> msg;
  205. GetPlayer()->dndMsg = msg;
  206. if(msg.size() == 0 || !GetPlayer()->isDND())
  207. {
  208. GetPlayer()->ToggleDND();
  209. if(GetPlayer()->isDND() && GetPlayer()->isAFK())
  210. GetPlayer()->ToggleAFK();
  211. }
  212. } break;
  213. case CHAT_MSG_RAID:
  214. {
  215. std::string msg="";
  216. recv_data >> msg;
  217. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  218. break;
  219. Group *group = GetPlayer()->groupInfo.group;
  220. if(!group || !group->isRaidGroup())
  221. return;
  222. WorldPacket data;
  223. sChatHandler.FillMessageData(&data, this, CHAT_MSG_RAID, lang, "", 0, msg.c_str());
  224. group->BroadcastPacket(&data);
  225. } break;
  226. case CHAT_MSG_RAID_LEADER:
  227. {
  228. std::string msg="";
  229. recv_data >> msg;
  230. if (sChatHandler.ParseCommands(msg.c_str(), this) > 0)
  231. break;
  232. Group *group = GetPlayer()->groupInfo.group;
  233. if(!group || !group->isRaidGroup() || !group->IsLeader(GetPlayer()->GetGUID()))
  234. return;
  235. WorldPacket data;
  236. sChatHandler.FillMessageData(&data, this, CHAT_MSG_RAID_LEADER, lang, "", 0, msg.c_str());
  237. group->BroadcastPacket(&data);
  238. } break;
  239. case CHAT_MSG_RAID_WARN:
  240. {
  241. std::string msg="";
  242. recv_data >> msg;
  243. Group *group = GetPlayer()->groupInfo.group;
  244. if(!group || !group->isRaidGroup() || !group->IsLeader(GetPlayer()->GetGUID()))
  245. return;
  246. WorldPacket data;
  247. sChatHandler.FillMessageData(&data, this, CHAT_MSG_RAID_WARN, lang, "", 0, msg.c_str());
  248. group->BroadcastPacket(&data);
  249. } break;
  250. default:
  251. sLog.outError("CHAT: unknown msg type %u, lang: %u", type, lang);
  252. }
  253. }
  254. void WorldSession::HandleTextEmoteOpcode( WorldPacket & recv_data )
  255. {
  256. uint32 text_emote, emoteNum;
  257. uint64 guid;
  258. recv_data >> text_emote;
  259. recv_data >> emoteNum;
  260. recv_data >> guid;
  261. const char *nam = 0;
  262. uint32 namlen = 1;
  263. Unit* unit = ObjectAccessor::Instance().GetUnit(*_player, guid);
  264. Creature *pCreature = dynamic_cast<Creature *>(unit);
  265. if( pCreature != NULL )
  266. {
  267. nam = pCreature->GetCreatureInfo()->Name;
  268. namlen = (nam ? strlen(nam) : 0) + 1;
  269. }
  270. {
  271. Player *pChar = dynamic_cast<Player *>(unit);
  272. if( pChar != NULL )
  273. {
  274. nam = pChar->GetName();
  275. namlen = (nam ? strlen(nam) : 0) + 1;
  276. }
  277. }
  278. EmotesTextEntry const *em = sEmotesTextStore.LookupEntry(text_emote);
  279. if (em)
  280. {
  281. uint32 emote_anim = em->textid;
  282. WorldPacket data;
  283. data.Initialize(SMSG_EMOTE, 12);
  284. data << (uint32)emote_anim;
  285. data << GetPlayer()->GetGUID();
  286. WPAssert(data.size() == 12);
  287. GetPlayer()->SendMessageToSet( &data, true );
  288. data.Initialize(SMSG_TEXT_EMOTE, (20+namlen));
  289. data << GetPlayer()->GetGUID();
  290. data << (uint32)text_emote;
  291. data << emoteNum;
  292. data << (uint32)namlen;
  293. if( namlen > 1 )
  294. {
  295. data.append(nam, namlen);
  296. }
  297. else
  298. {
  299. data << (uint8)0x00;
  300. }
  301. WPAssert(data.size() == 20 + namlen);
  302. GetPlayer()->SendMessageToSet( &data, true );
  303. //Send scripted event call
  304. if (pCreature && Script)
  305. Script->ReceiveEmote(GetPlayer(),pCreature,emote_anim);
  306. }
  307. }
  308. void WorldSession::HandleChatIgnoredOpcode(WorldPacket& recv_data )
  309. {
  310. WorldPacket data;
  311. uint64 iguid;
  312. //sLog.outDebug("WORLD: Received CMSG_CHAT_IGNORED");
  313. recv_data >> iguid;
  314. Player *player = objmgr.GetPlayer(iguid);
  315. if(!player || !player->GetSession())
  316. return;
  317. sChatHandler.FillMessageData(&data, this, CHAT_MSG_IGNORED, LANG_UNIVERSAL, NULL, GetPlayer()->GetGUID(), GetPlayer()->GetName());
  318. player->GetSession()->SendPacket(&data);
  319. }