chat.c 12 KB

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