chat.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "chat.hpp"
  4. #include <cstring>
  5. #include <common/cbasetypes.hpp>
  6. #include <common/malloc.hpp>
  7. #include <common/mmo.hpp>
  8. #include <common/nullpo.hpp>
  9. #include <common/showmsg.hpp>
  10. #include <common/strlib.hpp>
  11. #include "achievement.hpp"
  12. #include "atcommand.hpp" // msg_txt()
  13. #include "battle.hpp" // struct battle_config
  14. #include "clif.hpp"
  15. #include "map.hpp"
  16. #include "npc.hpp" // npc_event_do()
  17. #include "pc.hpp"
  18. #include "pc_groups.hpp"
  19. int32 chat_triggerevent(struct chat_data *cd); // forward declaration
  20. /// Initializes a chatroom object (common functionality for both pc and npc chatrooms).
  21. /// Returns a chatroom object on success, or nullptr on failure.
  22. static struct chat_data* chat_createchat(struct block_list* bl, const char* title, const char* pass, int32 limit, bool pub, int32 trigger, const char* ev, int32 zeny, int32 minLvl, int32 maxLvl)
  23. {
  24. struct chat_data* cd;
  25. nullpo_retr(nullptr, bl);
  26. cd = (struct chat_data *) aMalloc(sizeof(struct chat_data));
  27. safestrncpy(cd->title, title, sizeof(cd->title));
  28. safestrncpy(cd->pass, pass, sizeof(cd->pass));
  29. cd->pub = pub;
  30. cd->users = 0;
  31. cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
  32. cd->trigger = trigger;
  33. cd->zeny = zeny;
  34. cd->minLvl = minLvl;
  35. cd->maxLvl = maxLvl;
  36. memset(cd->usersd, 0, sizeof(cd->usersd));
  37. cd->owner = bl;
  38. safestrncpy(cd->npc_event, ev, sizeof(cd->npc_event));
  39. cd->bl.id = map_get_new_object_id();
  40. cd->bl.m = bl->m;
  41. cd->bl.x = bl->x;
  42. cd->bl.y = bl->y;
  43. cd->bl.type = BL_CHAT;
  44. cd->bl.next = cd->bl.prev = nullptr;
  45. if( cd->bl.id == 0 ) {
  46. aFree(cd);
  47. cd = nullptr;
  48. }
  49. map_addiddb(&cd->bl);
  50. if( bl->type != BL_NPC )
  51. cd->kick_list = idb_alloc(DB_OPT_BASE);
  52. return cd;
  53. }
  54. /**
  55. * Player chat room creation.
  56. * @param sd : player requesting
  57. * @param title : title of chat room
  58. * @param pass : password for chat room
  59. * @param limit : amount allowed to enter
  60. * @param pub : public or private
  61. * @return 0
  62. */
  63. int32 chat_createpcchat(map_session_data* sd, const char* title, const char* pass, int32 limit, bool pub)
  64. {
  65. struct chat_data* cd;
  66. nullpo_ret(sd);
  67. if( sd->chatID )
  68. return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex]
  69. if( sd->state.vending || sd->state.buyingstore ) // not chat, when you already have a store open
  70. return 0;
  71. if( map_getmapflag(sd->bl.m, MF_NOCHAT) ) {
  72. clif_displaymessage(sd->fd, msg_txt(sd,281));
  73. return 0; //Can't create chatrooms on this map.
  74. }
  75. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOCHAT) ) {
  76. clif_displaymessage (sd->fd, msg_txt(sd,665));
  77. return 0;
  78. }
  79. pc_stop_walking(sd,1);
  80. cd = chat_createchat(&sd->bl, title, pass, limit, pub, 0, "", 0, 1, MAX_LEVEL);
  81. if( cd ) {
  82. cd->users = 1;
  83. cd->usersd[0] = sd;
  84. pc_setchatid(sd,cd->bl.id);
  85. pc_stop_attack(sd);
  86. clif_createchat( *sd, CREATEROOM_SUCCESS );
  87. clif_dispchat(*cd);
  88. if (status_isdead(sd->bl))
  89. achievement_update_objective(sd, AG_CHATTING_DYING, 1, 1);
  90. else
  91. achievement_update_objective(sd, AG_CHATTING_CREATE, 1, 1);
  92. } else
  93. clif_createchat( *sd, CREATEROOM_LIMIT_EXCEEDED );
  94. return 0;
  95. }
  96. /**
  97. * Join an existing chat room.
  98. * @param sd : player requesting
  99. * @param chatid : ID of the chat room
  100. * @param pass : password of chat room
  101. * @return 0
  102. */
  103. int32 chat_joinchat(map_session_data* sd, int32 chatid, const char* pass)
  104. {
  105. struct chat_data* cd;
  106. nullpo_ret(sd);
  107. cd = (struct chat_data*)map_id2bl(chatid);
  108. if( cd == nullptr || cd->bl.type != BL_CHAT || cd->bl.m != sd->bl.m || sd->state.vending || sd->state.buyingstore || sd->chatID || ((cd->owner->type == BL_NPC) ? cd->users+1 : cd->users) >= cd->limit ) {
  109. clif_joinchatfail( *sd, ENTERROOM_FULL );
  110. return 0;
  111. }
  112. if( !cd->pub && strncmp(pass, cd->pass, sizeof(cd->pass)) != 0 && !pc_has_permission(sd, PC_PERM_JOIN_ALL_CHAT) ) {
  113. clif_joinchatfail( *sd, ENTERROOM_WRONG_PASSWORD );
  114. return 0;
  115. }
  116. if( sd->status.base_level < cd->minLvl || sd->status.base_level > cd->maxLvl ) {
  117. if(sd->status.base_level < cd->minLvl)
  118. clif_joinchatfail( *sd, ENTERROOM_TOO_LOW_LEVEL );
  119. else
  120. clif_joinchatfail( *sd, ENTERROOM_TOO_HIGH_LEVEL );
  121. return 0;
  122. }
  123. if( sd->status.zeny < cd->zeny ) {
  124. clif_joinchatfail( *sd, ENTERROOM_NO_ZENY );
  125. return 0;
  126. }
  127. if( cd->owner->type != BL_NPC && idb_exists(cd->kick_list,sd->status.char_id) ) {
  128. clif_joinchatfail( *sd, ENTERROOM_KICKED );
  129. return 0;
  130. }
  131. pc_stop_walking(sd,1);
  132. cd->usersd[cd->users] = sd;
  133. cd->users++;
  134. pc_setchatid(sd,cd->bl.id);
  135. // To the person who newly joined the chat
  136. clif_joinchatok(*sd, *cd);
  137. // Reports to the persons, who already are in the chat
  138. clif_addchat( *cd, *sd );
  139. clif_dispchat(*cd); //Reported number of changes to the people around
  140. if (cd->owner->type == BL_PC)
  141. achievement_update_objective(map_id2sd(cd->owner->id), AG_CHATTING_COUNT, 1, cd->users);
  142. chat_triggerevent(cd); //Event
  143. return 0;
  144. }
  145. /**
  146. * Make player (sd) leave a chat room.
  147. * @param sd : player requesting
  148. * @param kicked : for clif notification, kicked=1 or regular leave
  149. * @return 0:success, 1:failed
  150. */
  151. int32 chat_leavechat(map_session_data* sd, bool kicked)
  152. {
  153. struct chat_data* cd;
  154. int32 i;
  155. int32 leavechar;
  156. nullpo_retr(1, sd);
  157. cd = (struct chat_data*)map_id2bl(sd->chatID);
  158. if( cd == nullptr ) {
  159. pc_setchatid(sd, 0);
  160. return 1;
  161. }
  162. ARR_FIND( 0, cd->users, i, cd->usersd[i] == sd );
  163. if ( i == cd->users ) { // Not found in the chatroom?
  164. pc_setchatid(sd, 0);
  165. return -1;
  166. }
  167. clif_chat_leave( *cd, *sd, kicked );
  168. pc_setchatid(sd, 0);
  169. cd->users--;
  170. leavechar = i;
  171. for( i = leavechar; i < cd->users; i++ )
  172. cd->usersd[i] = cd->usersd[i+1];
  173. if( cd->users == 0 && cd->owner->type == BL_PC ) { // Delete empty chatroom
  174. clif_clearchat(*cd);
  175. db_destroy(cd->kick_list);
  176. map_deliddb(&cd->bl);
  177. map_delblock(&cd->bl);
  178. map_freeblock(&cd->bl);
  179. skill_unit *unit = map_find_skill_unit_oncell(&sd->bl, sd->bl.x, sd->bl.y, AL_WARP, nullptr, 0);
  180. if (unit != nullptr && unit->group != nullptr)
  181. ext_skill_unit_onplace(unit, &sd->bl, unit->group->tick);
  182. return 1;
  183. }
  184. if( leavechar == 0 && cd->owner->type == BL_PC ) { // Set and announce new owner
  185. cd->owner = (struct block_list*) cd->usersd[0];
  186. clif_chat_role( *cd, *cd->usersd[0] );
  187. clif_clearchat(*cd);
  188. //Adjust Chat location after owner has been changed.
  189. map_delblock( &cd->bl );
  190. cd->bl.x = cd->usersd[0]->bl.x;
  191. cd->bl.y = cd->usersd[0]->bl.y;
  192. if(map_addblock( &cd->bl ))
  193. return 1;
  194. clif_dispchat(*cd);
  195. } else
  196. clif_dispchat(*cd); // refresh chatroom
  197. return 0;
  198. }
  199. /**
  200. * Change a chat room's owner.
  201. * @param sd : player requesting
  202. * @param nextownername : string of new owner (name should be in chatroom)
  203. * @return 0:success, 1:failure
  204. */
  205. int32 chat_changechatowner(map_session_data* sd, const char* nextownername)
  206. {
  207. struct chat_data* cd;
  208. map_session_data* tmpsd;
  209. int32 i;
  210. nullpo_retr(1, sd);
  211. cd = (struct chat_data*)map_id2bl(sd->chatID);
  212. if( cd == nullptr || (struct block_list*) sd != cd->owner )
  213. return 1;
  214. ARR_FIND( 1, cd->users, i, strncmp(cd->usersd[i]->status.name, nextownername, NAME_LENGTH) == 0 );
  215. if( i == cd->users )
  216. return -1; // name not found
  217. // erase temporarily
  218. clif_clearchat(*cd);
  219. // set new owner
  220. cd->owner = (struct block_list*) cd->usersd[i];
  221. // swap the old and new owners' positions
  222. tmpsd = cd->usersd[i];
  223. cd->usersd[i] = cd->usersd[0];
  224. cd->usersd[0] = tmpsd;
  225. clif_chat_role( *cd, *cd->usersd[0] );
  226. clif_chat_role( *cd, *cd->usersd[i] );
  227. // set the new chatroom position
  228. map_delblock( &cd->bl );
  229. cd->bl.x = cd->owner->x;
  230. cd->bl.y = cd->owner->y;
  231. if(map_addblock( &cd->bl ))
  232. return 1;
  233. // and display again
  234. clif_dispchat(*cd);
  235. return 0;
  236. }
  237. /**
  238. * Change a chat room's status (title, etc).
  239. * @param sd : player requesting
  240. * @param title : new title
  241. * @param pass : new password
  242. * @param limit : new limit
  243. * @param pub : public or private
  244. * @return 1:success, 0:failure
  245. */
  246. int32 chat_changechatstatus(map_session_data* sd, const char* title, const char* pass, int32 limit, bool pub)
  247. {
  248. struct chat_data* cd;
  249. nullpo_retr(1, sd);
  250. cd = (struct chat_data*)map_id2bl(sd->chatID);
  251. if( cd == nullptr || (struct block_list *)sd != cd->owner )
  252. return 1;
  253. safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE);
  254. safestrncpy(cd->pass, pass, CHATROOM_PASS_SIZE);
  255. cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
  256. cd->pub = pub;
  257. clif_changechatstatus(*cd);
  258. clif_dispchat(*cd);
  259. return 0;
  260. }
  261. /**
  262. * Kicks a user from the chat room.
  263. * @param cd : chat to be kicked from
  264. * @param kickusername : player name to be kicked
  265. * @retur 1:success, 0:failure
  266. */
  267. int32 chat_npckickchat(struct chat_data* cd, const char* kickusername)
  268. {
  269. int32 i;
  270. nullpo_ret(cd);
  271. ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
  272. if( i == cd->users )
  273. return -1;
  274. chat_leavechat(cd->usersd[i],1);
  275. return 0;
  276. }
  277. /**
  278. * Kick a member from a chat room.
  279. * @param sd : player requesting
  280. * @param kickusername : player name to be kicked
  281. * @retur 1:success, 0:failure
  282. */
  283. int32 chat_kickchat(map_session_data* sd, const char* kickusername)
  284. {
  285. struct chat_data* cd;
  286. int32 i;
  287. nullpo_retr(1, sd);
  288. cd = (struct chat_data *)map_id2bl(sd->chatID);
  289. if( cd == nullptr || (struct block_list *)sd != cd->owner )
  290. return -1;
  291. ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
  292. if( i == cd->users )
  293. return -1;
  294. if (pc_has_permission(cd->usersd[i], PC_PERM_NO_CHAT_KICK))
  295. return 0; //gm kick protection [Valaris]
  296. idb_put(cd->kick_list,cd->usersd[i]->status.char_id,(void*)1);
  297. chat_leavechat(cd->usersd[i],1);
  298. return 0;
  299. }
  300. /**
  301. * Creates a chat room for a NPC.
  302. * @param nd : NPC requesting
  303. * @param title : title of chat room
  304. * @param limit : limit of users in chat room
  305. * @param pub : public or private
  306. * @param trigger : event trigger
  307. * @param ev : event name
  308. * @param zeny : zeny cost
  309. * @param minLvl : minimum level to enter
  310. * @param maxLvl : maximum level to enter
  311. * @return 0
  312. */
  313. int32 chat_createnpcchat(struct npc_data* nd, const char* title, int32 limit, bool pub, int32 trigger, const char* ev, int32 zeny, int32 minLvl, int32 maxLvl)
  314. {
  315. struct chat_data* cd;
  316. nullpo_ret(nd);
  317. if( nd->chat_id ) {
  318. ShowError("chat_createnpcchat: npc '%s' already has a chatroom, cannot create new one!\n", nd->exname);
  319. return 0;
  320. }
  321. if( zeny > MAX_ZENY || maxLvl > MAX_LEVEL ) {
  322. ShowError("chat_createnpcchat: npc '%s' has a required lvl or amount of zeny over the max limit!\n", nd->exname);
  323. return 0;
  324. }
  325. cd = chat_createchat(&nd->bl, title, "", limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  326. if( cd != nullptr ){
  327. nd->chat_id = cd->bl.id;
  328. clif_dispchat(*cd);
  329. }
  330. return 0;
  331. }
  332. /**
  333. * Removes a chat room for a NPC.
  334. * @param nd : NPC requesting
  335. */
  336. int32 chat_deletenpcchat(struct npc_data* nd)
  337. {
  338. struct chat_data *cd;
  339. nullpo_ret(nd);
  340. cd = (struct chat_data*)map_id2bl(nd->chat_id);
  341. if( cd == nullptr )
  342. return 0;
  343. chat_npckickall(cd);
  344. clif_clearchat(*cd);
  345. map_deliddb(&cd->bl);
  346. map_delblock(&cd->bl);
  347. map_freeblock(&cd->bl);
  348. nd->chat_id = 0;
  349. return 0;
  350. }
  351. /**
  352. * Trigger NPC event when entering the chat room.
  353. * @param cd : chat room to trigger event
  354. * @return 0
  355. */
  356. int32 chat_triggerevent(struct chat_data *cd)
  357. {
  358. nullpo_ret(cd);
  359. if( cd->users >= cd->trigger && cd->npc_event[0] )
  360. npc_event_do(cd->npc_event);
  361. return 0;
  362. }
  363. /**
  364. * Enables the event of the chat room.
  365. * At most, 127 users are needed to trigger the event.
  366. * @param cd : chat room to trigger event
  367. */
  368. int32 chat_enableevent(struct chat_data* cd)
  369. {
  370. nullpo_ret(cd);
  371. cd->trigger &= 0x7f;
  372. chat_triggerevent(cd);
  373. return 0;
  374. }
  375. /**
  376. * Disables the event of the chat room.
  377. * @param cd : chat room to trigger event
  378. */
  379. int32 chat_disableevent(struct chat_data* cd)
  380. {
  381. nullpo_ret(cd);
  382. cd->trigger |= 0x80;
  383. return 0;
  384. }
  385. /**
  386. * Kicks all the users from the chat room.
  387. * @param cd : chat room to trigger event
  388. */
  389. int32 chat_npckickall(struct chat_data* cd)
  390. {
  391. nullpo_ret(cd);
  392. while( cd->users > 0 )
  393. chat_leavechat(cd->usersd[cd->users-1],0);
  394. return 0;
  395. }