chat.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. int 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 NULL on failure.
  22. static struct chat_data* chat_createchat(struct block_list* bl, const char* title, const char* pass, int limit, bool pub, int trigger, const char* ev, int zeny, int minLvl, int maxLvl)
  23. {
  24. struct chat_data* cd;
  25. nullpo_retr(NULL, 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 = NULL;
  45. if( cd->bl.id == 0 ) {
  46. aFree(cd);
  47. cd = NULL;
  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. int chat_createpcchat(map_session_data* sd, const char* title, const char* pass, int 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,0);
  87. clif_dispchat(cd,0);
  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,1);
  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. int chat_joinchat(map_session_data* sd, int 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 == NULL || 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,0);
  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,1);
  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,5);
  119. else
  120. clif_joinchatfail(sd,6);
  121. return 0;
  122. }
  123. if( sd->status.zeny < cd->zeny ) {
  124. clif_joinchatfail(sd,4);
  125. return 0;
  126. }
  127. if( cd->owner->type != BL_NPC && idb_exists(cd->kick_list,sd->status.char_id) ) {
  128. clif_joinchatfail(sd,2);//You have been kicked out of the room.
  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. clif_joinchatok(sd, cd); //To the person who newly joined the list of all
  136. clif_addchat(cd, sd); //Reports To the person who already in the chat
  137. clif_dispchat(cd, 0); //Reported number of changes to the people around
  138. if (cd->owner->type == BL_PC)
  139. achievement_update_objective(map_id2sd(cd->owner->id), AG_CHATTING_COUNT, 1, cd->users);
  140. chat_triggerevent(cd); //Event
  141. return 0;
  142. }
  143. /**
  144. * Make player (sd) leave a chat room.
  145. * @param sd : player requesting
  146. * @param kicked : for clif notification, kicked=1 or regular leave
  147. * @return 0:success, 1:failed
  148. */
  149. int chat_leavechat(map_session_data* sd, bool kicked)
  150. {
  151. struct chat_data* cd;
  152. int i;
  153. int leavechar;
  154. nullpo_retr(1, sd);
  155. cd = (struct chat_data*)map_id2bl(sd->chatID);
  156. if( cd == NULL ) {
  157. pc_setchatid(sd, 0);
  158. return 1;
  159. }
  160. ARR_FIND( 0, cd->users, i, cd->usersd[i] == sd );
  161. if ( i == cd->users ) { // Not found in the chatroom?
  162. pc_setchatid(sd, 0);
  163. return -1;
  164. }
  165. clif_leavechat(cd, sd, kicked);
  166. pc_setchatid(sd, 0);
  167. cd->users--;
  168. leavechar = i;
  169. for( i = leavechar; i < cd->users; i++ )
  170. cd->usersd[i] = cd->usersd[i+1];
  171. if( cd->users == 0 && cd->owner->type == BL_PC ) { // Delete empty chatroom
  172. clif_clearchat(cd, 0);
  173. db_destroy(cd->kick_list);
  174. map_deliddb(&cd->bl);
  175. map_delblock(&cd->bl);
  176. map_freeblock(&cd->bl);
  177. skill_unit *unit = map_find_skill_unit_oncell(&sd->bl, sd->bl.x, sd->bl.y, AL_WARP, nullptr, 0);
  178. if (unit != nullptr && unit->group != nullptr)
  179. ext_skill_unit_onplace(unit, &sd->bl, unit->group->tick);
  180. return 1;
  181. }
  182. if( leavechar == 0 && cd->owner->type == BL_PC ) { // Set and announce new owner
  183. cd->owner = (struct block_list*) cd->usersd[0];
  184. clif_changechatowner(cd, cd->usersd[0]);
  185. clif_clearchat(cd, 0);
  186. //Adjust Chat location after owner has been changed.
  187. map_delblock( &cd->bl );
  188. cd->bl.x = cd->usersd[0]->bl.x;
  189. cd->bl.y = cd->usersd[0]->bl.y;
  190. if(map_addblock( &cd->bl ))
  191. return 1;
  192. clif_dispchat(cd,0);
  193. } else
  194. clif_dispchat(cd,0); // refresh chatroom
  195. return 0;
  196. }
  197. /**
  198. * Change a chat room's owner.
  199. * @param sd : player requesting
  200. * @param nextownername : string of new owner (name should be in chatroom)
  201. * @return 0:success, 1:failure
  202. */
  203. int chat_changechatowner(map_session_data* sd, const char* nextownername)
  204. {
  205. struct chat_data* cd;
  206. map_session_data* tmpsd;
  207. int i;
  208. nullpo_retr(1, sd);
  209. cd = (struct chat_data*)map_id2bl(sd->chatID);
  210. if( cd == NULL || (struct block_list*) sd != cd->owner )
  211. return 1;
  212. ARR_FIND( 1, cd->users, i, strncmp(cd->usersd[i]->status.name, nextownername, NAME_LENGTH) == 0 );
  213. if( i == cd->users )
  214. return -1; // name not found
  215. // erase temporarily
  216. clif_clearchat(cd,0);
  217. // set new owner
  218. cd->owner = (struct block_list*) cd->usersd[i];
  219. clif_changechatowner(cd,cd->usersd[i]);
  220. // swap the old and new owners' positions
  221. tmpsd = cd->usersd[i];
  222. cd->usersd[i] = cd->usersd[0];
  223. cd->usersd[0] = tmpsd;
  224. // set the new chatroom position
  225. map_delblock( &cd->bl );
  226. cd->bl.x = cd->owner->x;
  227. cd->bl.y = cd->owner->y;
  228. if(map_addblock( &cd->bl ))
  229. return 1;
  230. // and display again
  231. clif_dispchat(cd,0);
  232. return 0;
  233. }
  234. /**
  235. * Change a chat room's status (title, etc).
  236. * @param sd : player requesting
  237. * @param title : new title
  238. * @param pass : new password
  239. * @param limit : new limit
  240. * @param pub : public or private
  241. * @return 1:success, 0:failure
  242. */
  243. int chat_changechatstatus(map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
  244. {
  245. struct chat_data* cd;
  246. nullpo_retr(1, sd);
  247. cd = (struct chat_data*)map_id2bl(sd->chatID);
  248. if( cd == NULL || (struct block_list *)sd != cd->owner )
  249. return 1;
  250. safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE);
  251. safestrncpy(cd->pass, pass, CHATROOM_PASS_SIZE);
  252. cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
  253. cd->pub = pub;
  254. clif_changechatstatus(cd);
  255. clif_dispchat(cd,0);
  256. return 0;
  257. }
  258. /**
  259. * Kicks a user from the chat room.
  260. * @param cd : chat to be kicked from
  261. * @param kickusername : player name to be kicked
  262. * @retur 1:success, 0:failure
  263. */
  264. int chat_npckickchat(struct chat_data* cd, const char* kickusername)
  265. {
  266. int i;
  267. nullpo_ret(cd);
  268. ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
  269. if( i == cd->users )
  270. return -1;
  271. chat_leavechat(cd->usersd[i],1);
  272. return 0;
  273. }
  274. /**
  275. * Kick a member from a chat room.
  276. * @param sd : player requesting
  277. * @param kickusername : player name to be kicked
  278. * @retur 1:success, 0:failure
  279. */
  280. int chat_kickchat(map_session_data* sd, const char* kickusername)
  281. {
  282. struct chat_data* cd;
  283. int i;
  284. nullpo_retr(1, sd);
  285. cd = (struct chat_data *)map_id2bl(sd->chatID);
  286. if( cd == NULL || (struct block_list *)sd != cd->owner )
  287. return -1;
  288. ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
  289. if( i == cd->users )
  290. return -1;
  291. if (pc_has_permission(cd->usersd[i], PC_PERM_NO_CHAT_KICK))
  292. return 0; //gm kick protection [Valaris]
  293. idb_put(cd->kick_list,cd->usersd[i]->status.char_id,(void*)1);
  294. chat_leavechat(cd->usersd[i],1);
  295. return 0;
  296. }
  297. /**
  298. * Creates a chat room for a NPC.
  299. * @param nd : NPC requesting
  300. * @param title : title of chat room
  301. * @param limit : limit of users in chat room
  302. * @param pub : public or private
  303. * @param trigger : event trigger
  304. * @param ev : event name
  305. * @param zeny : zeny cost
  306. * @param minLvl : minimum level to enter
  307. * @param maxLvl : maximum level to enter
  308. * @return 0
  309. */
  310. int chat_createnpcchat(struct npc_data* nd, const char* title, int limit, bool pub, int trigger, const char* ev, int zeny, int minLvl, int maxLvl)
  311. {
  312. struct chat_data* cd;
  313. nullpo_ret(nd);
  314. if( nd->chat_id ) {
  315. ShowError("chat_createnpcchat: npc '%s' already has a chatroom, cannot create new one!\n", nd->exname);
  316. return 0;
  317. }
  318. if( zeny > MAX_ZENY || maxLvl > MAX_LEVEL ) {
  319. ShowError("chat_createnpcchat: npc '%s' has a required lvl or amount of zeny over the max limit!\n", nd->exname);
  320. return 0;
  321. }
  322. cd = chat_createchat(&nd->bl, title, "", limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  323. if( cd ) {
  324. nd->chat_id = cd->bl.id;
  325. clif_dispchat(cd,0);
  326. }
  327. return 0;
  328. }
  329. /**
  330. * Removes a chat room for a NPC.
  331. * @param nd : NPC requesting
  332. */
  333. int chat_deletenpcchat(struct npc_data* nd)
  334. {
  335. struct chat_data *cd;
  336. nullpo_ret(nd);
  337. cd = (struct chat_data*)map_id2bl(nd->chat_id);
  338. if( cd == NULL )
  339. return 0;
  340. chat_npckickall(cd);
  341. clif_clearchat(cd, 0);
  342. map_deliddb(&cd->bl);
  343. map_delblock(&cd->bl);
  344. map_freeblock(&cd->bl);
  345. nd->chat_id = 0;
  346. return 0;
  347. }
  348. /**
  349. * Trigger NPC event when entering the chat room.
  350. * @param cd : chat room to trigger event
  351. * @return 0
  352. */
  353. int chat_triggerevent(struct chat_data *cd)
  354. {
  355. nullpo_ret(cd);
  356. if( cd->users >= cd->trigger && cd->npc_event[0] )
  357. npc_event_do(cd->npc_event);
  358. return 0;
  359. }
  360. /**
  361. * Enables the event of the chat room.
  362. * At most, 127 users are needed to trigger the event.
  363. * @param cd : chat room to trigger event
  364. */
  365. int chat_enableevent(struct chat_data* cd)
  366. {
  367. nullpo_ret(cd);
  368. cd->trigger &= 0x7f;
  369. chat_triggerevent(cd);
  370. return 0;
  371. }
  372. /**
  373. * Disables the event of the chat room.
  374. * @param cd : chat room to trigger event
  375. */
  376. int chat_disableevent(struct chat_data* cd)
  377. {
  378. nullpo_ret(cd);
  379. cd->trigger |= 0x80;
  380. return 0;
  381. }
  382. /**
  383. * Kicks all the users from the chat room.
  384. * @param cd : chat room to trigger event
  385. */
  386. int chat_npckickall(struct chat_data* cd)
  387. {
  388. nullpo_ret(cd);
  389. while( cd->users > 0 )
  390. chat_leavechat(cd->usersd[cd->users-1],0);
  391. return 0;
  392. }