chat.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/strlib.h"
  7. #include "atcommand.h" // msg_txt()
  8. #include "battle.h" // struct battle_config
  9. #include "clif.h"
  10. #include "map.h"
  11. #include "npc.h" // npc_event_do()
  12. #include "pc.h"
  13. #include "chat.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. int chat_triggerevent(struct chat_data *cd); // forward declaration
  17. /// Initializes a chatroom object (common functionality for both pc and npc chatrooms).
  18. /// Returns a chatroom object on success, or NULL on failure.
  19. 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)
  20. {
  21. struct chat_data* cd;
  22. nullpo_retr(NULL, bl);
  23. cd = (struct chat_data *) aMalloc(sizeof(struct chat_data));
  24. safestrncpy(cd->title, title, sizeof(cd->title));
  25. safestrncpy(cd->pass, pass, sizeof(cd->pass));
  26. cd->pub = pub;
  27. cd->users = 0;
  28. cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
  29. cd->trigger = trigger;
  30. memset(cd->usersd, 0, sizeof(cd->usersd));
  31. cd->owner = bl;
  32. safestrncpy(cd->npc_event, ev, sizeof(cd->npc_event));
  33. cd->bl.m = bl->m;
  34. cd->bl.x = bl->x;
  35. cd->bl.y = bl->y;
  36. cd->bl.type = BL_CHAT;
  37. cd->bl.next = cd->bl.prev = NULL;
  38. cd->bl.id = map_addobject(&cd->bl);
  39. if( cd->bl.id == 0 )
  40. {
  41. aFree(cd);
  42. cd = NULL;
  43. }
  44. return cd;
  45. }
  46. /*==========================================
  47. * player chatroom creation
  48. *------------------------------------------*/
  49. int chat_createpcchat(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
  50. {
  51. struct chat_data* cd;
  52. nullpo_retr(0, sd);
  53. if( sd->chatID )
  54. return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex]
  55. if( map[sd->bl.m].flag.nochat )
  56. {
  57. clif_displaymessage(sd->fd, msg_txt(281));
  58. return 0; //Can't create chatrooms on this map.
  59. }
  60. if( map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKNOCHAT) )
  61. {
  62. clif_displaymessage (sd->fd, "Can't create chat rooms in this Area.");
  63. return 0;
  64. }
  65. pc_stop_walking(sd,1);
  66. cd = chat_createchat(&sd->bl, title, pass, limit, pub, 0, "");
  67. if( cd )
  68. {
  69. cd->users = 1;
  70. cd->usersd[0] = sd;
  71. pc_setchatid(sd,cd->bl.id);
  72. clif_createchat(sd,0);
  73. clif_dispchat(cd,0);
  74. }
  75. else
  76. clif_createchat(sd,1);
  77. return 0;
  78. }
  79. /*==========================================
  80. * join an existing chatroom
  81. *------------------------------------------*/
  82. int chat_joinchat(struct map_session_data* sd, int chatid, const char* pass)
  83. {
  84. struct chat_data* cd;
  85. nullpo_retr(0, sd);
  86. cd = (struct chat_data*)map_id2bl(chatid);
  87. if( cd == NULL || cd->bl.type != BL_CHAT || cd->bl.m != sd->bl.m || sd->vender_id || sd->chatID || cd->users >= cd->limit )
  88. {
  89. clif_joinchatfail(sd,0);
  90. return 0;
  91. }
  92. if( !cd->pub && strncmp(pass, cd->pass, sizeof(cd->pass)) != 0 && !(battle_config.gm_join_chat && pc_isGM(sd) >= battle_config.gm_join_chat) )
  93. {
  94. clif_joinchatfail(sd,1);
  95. return 0;
  96. }
  97. pc_stop_walking(sd,1);
  98. cd->usersd[cd->users] = sd;
  99. cd->users++;
  100. pc_setchatid(sd,cd->bl.id);
  101. clif_joinchatok(sd,cd); // 新たに参加した人には全員のリスト
  102. clif_addchat(cd,sd); // 既に中に居た人には追加した人の報告
  103. clif_dispchat(cd,0); // 周囲の人には人数変化報告
  104. chat_triggerevent(cd); // イベント
  105. return 0;
  106. }
  107. /*==========================================
  108. * leave a chatroom
  109. *------------------------------------------*/
  110. int chat_leavechat(struct map_session_data* sd, bool kicked)
  111. {
  112. struct chat_data* cd;
  113. int i;
  114. int leavechar;
  115. nullpo_retr(1, sd);
  116. cd = (struct chat_data*)map_id2bl(sd->chatID);
  117. if( cd == NULL )
  118. {
  119. pc_setchatid(sd, 0);
  120. return 1;
  121. }
  122. ARR_FIND( 0, cd->users, i, cd->usersd[i] == sd );
  123. if ( i == cd->users )
  124. { // Not found in the chatroom?
  125. pc_setchatid(sd, 0);
  126. return -1;
  127. }
  128. clif_leavechat(cd, sd, kicked);
  129. pc_setchatid(sd, 0);
  130. cd->users--;
  131. leavechar = i;
  132. for( i = leavechar; i < cd->users; i++ )
  133. cd->usersd[i] = cd->usersd[i+1];
  134. if( cd->users == 0 && cd->owner->type == BL_PC )
  135. { // Delete empty chatroom
  136. clif_clearchat(cd, 0);
  137. map_delobject(cd->bl.id);
  138. return 1;
  139. }
  140. if( leavechar == 0 && cd->owner->type == BL_PC )
  141. { // Set and announce new owner
  142. cd->owner = (struct block_list*) cd->usersd[0];
  143. clif_changechatowner(cd, cd->usersd[0]);
  144. clif_clearchat(cd, 0);
  145. //Adjust Chat location after owner has been changed.
  146. map_delblock( &cd->bl );
  147. cd->bl.x=cd->usersd[0]->bl.x;
  148. cd->bl.y=cd->usersd[0]->bl.y;
  149. map_addblock( &cd->bl );
  150. clif_dispchat(cd,0);
  151. }
  152. else
  153. clif_dispchat(cd,0); // refresh chatroom
  154. return 0;
  155. }
  156. /*==========================================
  157. * change a chatroom's owner
  158. *------------------------------------------*/
  159. int chat_changechatowner(struct map_session_data* sd, const char* nextownername)
  160. {
  161. struct chat_data* cd;
  162. struct map_session_data* tmpsd;
  163. int i;
  164. nullpo_retr(1, sd);
  165. cd = (struct chat_data*)map_id2bl(sd->chatID);
  166. if( cd == NULL || (struct block_list*) sd != cd->owner )
  167. return 1;
  168. ARR_FIND( 1, cd->users, i, strncmp(cd->usersd[i]->status.name, nextownername, NAME_LENGTH) == 0 );
  169. if( i == cd->users )
  170. return -1; // name not found
  171. // erase temporarily
  172. clif_clearchat(cd,0);
  173. // set new owner
  174. cd->owner = (struct block_list*) cd->usersd[i];
  175. clif_changechatowner(cd,cd->usersd[i]);
  176. // swap the old and new owners' positions
  177. tmpsd = cd->usersd[i];
  178. cd->usersd[i] = cd->usersd[0];
  179. cd->usersd[0] = tmpsd;
  180. // set the new chatroom position
  181. map_delblock( &cd->bl );
  182. cd->bl.x = cd->owner->x;
  183. cd->bl.y = cd->owner->y;
  184. map_addblock( &cd->bl );
  185. // and display again
  186. clif_dispchat(cd,0);
  187. return 0;
  188. }
  189. /*==========================================
  190. * change a chatroom's status (title, etc)
  191. *------------------------------------------*/
  192. int chat_changechatstatus(struct map_session_data* sd, const char* title, const char* pass, int limit, bool pub)
  193. {
  194. struct chat_data* cd;
  195. nullpo_retr(1, sd);
  196. cd = (struct chat_data*)map_id2bl(sd->chatID);
  197. if( cd==NULL || (struct block_list *)sd != cd->owner )
  198. return 1;
  199. safestrncpy(cd->title, title, CHATROOM_TITLE_SIZE);
  200. safestrncpy(cd->pass, pass, CHATROOM_PASS_SIZE);
  201. cd->limit = min(limit, ARRAYLENGTH(cd->usersd));
  202. cd->pub = pub;
  203. clif_changechatstatus(cd);
  204. clif_dispchat(cd,0);
  205. return 0;
  206. }
  207. /*==========================================
  208. * kick an user from a chatroom
  209. *------------------------------------------*/
  210. int chat_kickchat(struct map_session_data* sd, const char* kickusername)
  211. {
  212. struct chat_data* cd;
  213. int i;
  214. nullpo_retr(1, sd);
  215. cd = (struct chat_data *)map_id2bl(sd->chatID);
  216. if( cd==NULL || (struct block_list *)sd != cd->owner )
  217. return -1;
  218. ARR_FIND( 0, cd->users, i, strncmp(cd->usersd[i]->status.name, kickusername, NAME_LENGTH) == 0 );
  219. if( i == cd->users )
  220. return -1;
  221. if( battle_config.gm_kick_chat && pc_isGM(cd->usersd[i]) >= battle_config.gm_kick_chat )
  222. return 0; //gm kick protection [Valaris]
  223. chat_leavechat(cd->usersd[i],1);
  224. return 0;
  225. }
  226. /// Creates a chat room for the npc.
  227. int chat_createnpcchat(struct npc_data* nd, const char* title, int limit, bool pub, int trigger, const char* ev)
  228. {
  229. struct chat_data* cd;
  230. nullpo_retr(0, nd);
  231. cd = chat_createchat(&nd->bl, title, "", limit, pub, trigger, ev);
  232. if( cd )
  233. {
  234. nd->chat_id = cd->bl.id;
  235. clif_dispchat(cd,0);
  236. }
  237. return 0;
  238. }
  239. /// Removes the chatroom from the npc.
  240. int chat_deletenpcchat(struct npc_data* nd)
  241. {
  242. struct chat_data *cd;
  243. nullpo_retr(0, nd);
  244. cd = (struct chat_data*)map_id2bl(nd->chat_id);
  245. if( cd == NULL )
  246. return 0;
  247. chat_npckickall(cd);
  248. clif_clearchat(cd, 0);
  249. map_delobject(cd->bl.id); // freeまでしてくれる
  250. nd->chat_id = 0;
  251. return 0;
  252. }
  253. /*==========================================
  254. * 規定人数以上でイベントが定義されてるなら実行
  255. *------------------------------------------*/
  256. int chat_triggerevent(struct chat_data *cd)
  257. {
  258. nullpo_retr(0, cd);
  259. if( cd->users >= cd->trigger && cd->npc_event[0] )
  260. npc_event_do(cd->npc_event);
  261. return 0;
  262. }
  263. /// Enables the event of the chat room.
  264. /// At most, 127 users are needed to trigger the event.
  265. int chat_enableevent(struct chat_data* cd)
  266. {
  267. nullpo_retr(0, cd);
  268. cd->trigger &= 0x7f;
  269. chat_triggerevent(cd);
  270. return 0;
  271. }
  272. /// Disables the event of the chat room
  273. int chat_disableevent(struct chat_data* cd)
  274. {
  275. nullpo_retr(0, cd);
  276. cd->trigger |= 0x80;
  277. return 0;
  278. }
  279. /// Kicks all the users from the chat room.
  280. int chat_npckickall(struct chat_data* cd)
  281. {
  282. nullpo_retr(0, cd);
  283. while( cd->users > 0 )
  284. chat_leavechat(cd->usersd[cd->users-1],0);
  285. return 0;
  286. }