channel.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "channel.hpp"
  4. #include <stdlib.h>
  5. #include <common/cbasetypes.hpp>
  6. #include <common/conf.hpp> //libconfig
  7. #include <common/malloc.hpp>
  8. #include <common/nullpo.hpp>
  9. #include <common/showmsg.hpp>
  10. #include <common/socket.hpp> //set_eof
  11. #include <common/strlib.hpp> //safestrncpy
  12. #include <common/timer.hpp> // DIFF_TICK
  13. #include "battle.hpp"
  14. #include "clif.hpp" //clif_chsys_msg
  15. #include "guild.hpp"
  16. #include "map.hpp" //msg_conf
  17. #include "pc.hpp"
  18. #include "pc_groups.hpp"
  19. static DBMap* channel_db; // channels
  20. struct Channel_Config channel_config;
  21. DBMap* channel_get_db(void){ return channel_db; }
  22. /**
  23. * Create a channel
  24. * - Will then add it in the channel_db if the type is not map or ally
  25. * @param name: Channel name, can't be null
  26. * @param pass: Channel password, can be null
  27. * @param color: Display color
  28. * @param chantype: Channel type
  29. * @return NULL on failure or Channel on success
  30. */
  31. struct Channel* channel_create(struct Channel *tmp_chan) {
  32. struct Channel* channel;
  33. if (!tmp_chan->name[0])
  34. return NULL;
  35. CREATE(channel, struct Channel, 1); //will exit on fail allocation
  36. //channel->id = tmp_chan->id;
  37. channel->users = idb_alloc(DB_OPT_BASE);
  38. channel->banned = idb_alloc(static_cast<DBOptions>(DB_OPT_BASE|DB_OPT_RELEASE_DATA) );
  39. channel->opt = tmp_chan->opt;
  40. channel->type = tmp_chan->type;
  41. channel->color = tmp_chan->color;
  42. safestrncpy(channel->name, tmp_chan->name, CHAN_NAME_LENGTH); // Store channel cname without '#'
  43. safestrncpy(channel->alias, tmp_chan->alias[0] ? tmp_chan->alias : tmp_chan->name, CHAN_NAME_LENGTH);
  44. if (!tmp_chan->pass[0])
  45. channel->pass[0] = '\0';
  46. else
  47. safestrncpy(channel->pass, tmp_chan->pass, CHAN_NAME_LENGTH);
  48. channel->msg_delay = tmp_chan->msg_delay;
  49. switch (channel->type) {
  50. case CHAN_TYPE_MAP:
  51. channel->m = tmp_chan->m;
  52. break;
  53. case CHAN_TYPE_ALLY:
  54. channel->gid = tmp_chan->gid;
  55. break;
  56. case CHAN_TYPE_PRIVATE:
  57. channel->char_id = tmp_chan->char_id;
  58. default:
  59. strdb_put(channel_db, channel->name, channel);
  60. break;
  61. }
  62. if (battle_config.etc_log)
  63. ShowInfo("Create channel %s alias %s type=%d, owner=%d/%d/%d\n",channel->name,channel->alias,channel->type,channel->char_id,channel->m,channel->gid);
  64. return channel;
  65. }
  66. /**
  67. * Allocates the proper settings depending on the channel type
  68. * @param name: Channel name, can't be null
  69. * @param pass: Channel password, can be null
  70. * @param chantype: Channel type
  71. * @param owner: Owner ID
  72. * @return NULL on failure or Channel on success
  73. */
  74. struct Channel* channel_create_simple(char *name, char *pass, enum Channel_Type chantype, unsigned int owner) {
  75. struct Channel tmp_chan;
  76. memset(&tmp_chan, 0, sizeof(struct Channel));
  77. switch (chantype) {
  78. case CHAN_TYPE_ALLY:
  79. memcpy(&tmp_chan, &channel_config.ally_tmpl, sizeof(channel_config.ally_tmpl));
  80. tmp_chan.gid = (int)owner;
  81. break;
  82. case CHAN_TYPE_MAP:
  83. memcpy(&tmp_chan, &channel_config.map_tmpl, sizeof(channel_config.map_tmpl));
  84. tmp_chan.m = (uint16)owner;
  85. break;
  86. case CHAN_TYPE_PUBLIC:
  87. safestrncpy(tmp_chan.name, name+1, sizeof(tmp_chan.name));
  88. if (pass)
  89. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  90. else
  91. tmp_chan.pass[0] = '\0';
  92. safestrncpy(tmp_chan.alias, name, sizeof(tmp_chan.name));
  93. tmp_chan.type = CHAN_TYPE_PUBLIC;
  94. tmp_chan.opt = CHAN_OPT_BASE;
  95. tmp_chan.msg_delay = 1000;
  96. tmp_chan.color = channel_getColor("Default");
  97. break;
  98. case CHAN_TYPE_PRIVATE:
  99. safestrncpy(tmp_chan.name, name+1, sizeof(tmp_chan.name));
  100. if (pass)
  101. safestrncpy(tmp_chan.pass, pass, sizeof(tmp_chan.pass));
  102. else
  103. tmp_chan.pass[0] = '\0';
  104. safestrncpy(tmp_chan.alias, name, sizeof(tmp_chan.name));
  105. tmp_chan.type = CHAN_TYPE_PRIVATE;
  106. tmp_chan.opt = channel_config.private_channel.opt;
  107. tmp_chan.msg_delay = channel_config.private_channel.delay;
  108. tmp_chan.color = channel_config.private_channel.color;
  109. tmp_chan.char_id = owner;
  110. break;
  111. default:
  112. return NULL;
  113. }
  114. return channel_create(&tmp_chan);
  115. }
  116. /**
  117. * Delete a channel
  118. * - Checks if there is any user in channel and make them quit
  119. * @param channel: Channel data
  120. * @param force: Forcefully remove channel
  121. * @return
  122. * 0: Success
  123. * -1: Invalid channel
  124. * -2: Can't delete now
  125. */
  126. int channel_delete(struct Channel *channel, bool force) {
  127. if(!channel)
  128. return -1;
  129. // only delete those serv stop
  130. if( !force && channel->type == CHAN_TYPE_PUBLIC && global_core->is_running() ){
  131. return -2;
  132. }
  133. if( db_size(channel->users)) {
  134. map_session_data *sd;
  135. DBIterator *iter = db_iterator(channel->users);
  136. for( sd = (map_session_data *)dbi_first(iter); dbi_exists(iter); sd = (map_session_data *)dbi_next(iter) ) { //for all users
  137. channel_clean(channel,sd,1); //make all quit
  138. }
  139. dbi_destroy(iter);
  140. }
  141. if (battle_config.etc_log)
  142. ShowInfo("Deleting channel %s alias %s type %d\n",channel->name,channel->alias,channel->type);
  143. db_destroy(channel->users);
  144. db_destroy(channel->banned);
  145. if (channel->groups)
  146. aFree(channel->groups);
  147. channel->groups = NULL;
  148. channel->group_count = 0;
  149. switch(channel->type){
  150. case CHAN_TYPE_MAP:
  151. map_getmapdata(channel->m)->channel = NULL;
  152. aFree(channel);
  153. break;
  154. case CHAN_TYPE_ALLY: {
  155. struct guild *g = guild_search(channel->gid);
  156. if(g) g->channel = NULL;
  157. aFree(channel);
  158. break;
  159. }
  160. default:
  161. strdb_remove(channel_db, channel->name);
  162. break;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * Make a player join a channel
  168. * - Add player to channel user list
  169. * - Add channel to user channel list
  170. * @param channel: Channel data
  171. * @param sd: Player data
  172. * @return
  173. * 0: Success
  174. * -1: Invalid channel or player
  175. * -2: Player already in channel
  176. * -3: Player banned
  177. * -4: Reached max limit
  178. */
  179. int channel_join(struct Channel *channel, map_session_data *sd) {
  180. if(!channel || !sd)
  181. return -1;
  182. if(sd->state.autotrade)
  183. return 0; // fake success
  184. if(channel_haspc(channel,sd)==1)
  185. return -2;
  186. if (!pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) && !channel_pccheckgroup(channel, sd->group_id))
  187. return -2;
  188. if(channel_haspcbanned(channel,sd)==1){
  189. char output[CHAT_SIZE_MAX];
  190. sprintf(output, msg_txt(sd,1438),channel->name); //You're currently banned from the '%s' channel.
  191. clif_displaymessage(sd->fd, output);
  192. return -3;
  193. }
  194. if (channel->type == CHAN_TYPE_PRIVATE && db_size(channel->users) >= channel_config.private_channel.max_member) {
  195. char output[CHAT_SIZE_MAX];
  196. sprintf(output, msg_txt(sd,760), channel->name, channel_config.private_channel.max_member); // You cannot join channel '%s'. Limit of %d has been met.
  197. clif_displaymessage(sd->fd, output);
  198. return -4;
  199. }
  200. RECREATE(sd->channels, struct Channel *, ++sd->channel_count);
  201. sd->channels[ sd->channel_count - 1 ] = channel;
  202. idb_put(channel->users, sd->status.char_id, sd);
  203. RECREATE(sd->channel_tick, t_tick, sd->channel_count);
  204. sd->channel_tick[sd->channel_count-1] = 0;
  205. if( sd->stealth ) {
  206. sd->stealth = false;
  207. } else if( channel->opt & CHAN_OPT_ANNOUNCE_JOIN ) {
  208. char output[CHAT_SIZE_MAX];
  209. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,761), channel->alias, sd->status.name); // %s %s has joined.
  210. clif_channel_msg(channel,output,channel->color);
  211. }
  212. /* someone is cheating, we kindly disconnect the bastard */
  213. if( sd->channel_count > 200 ) {
  214. set_eof(sd->fd);
  215. }
  216. return 0;
  217. }
  218. /**
  219. * Make a player join the map channel
  220. * - Create the map_channel if it does not exist
  221. * @param sd: Player data
  222. * @return
  223. * -1: Invalid player
  224. * -2: Player already in channel (channel_join)
  225. * -3: Player banned (channel_join)
  226. */
  227. int channel_mjoin(map_session_data *sd) {
  228. char mout[60];
  229. if(!sd) return -1;
  230. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  231. if( !mapdata->channel ) {
  232. mapdata->channel = channel_create_simple(NULL,NULL,CHAN_TYPE_MAP,sd->bl.m);
  233. }
  234. if( mapdata->channel->opt & CHAN_OPT_ANNOUNCE_SELF ) {
  235. sprintf(mout, msg_txt(sd,1435),mapdata->channel->name,mapdata->name); // You're now in the '#%s' channel for '%s'.
  236. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], mout, false, SELF);
  237. }
  238. return channel_join(mapdata->channel,sd);
  239. }
  240. /**
  241. * Make all ally members of a guild join the guild channel
  242. * - They only join if they are in their own guild channel (if not they probably left it)
  243. * @param g: Guild data
  244. * @return
  245. * 0: Success
  246. * -1: Invalid guild or no channel for guild
  247. */
  248. int channel_ajoin(struct guild *g){
  249. int i, j;
  250. map_session_data *pl_sd;
  251. if(!g || !g->channel) return -1;
  252. for (i = 0; i < MAX_GUILDALLIANCE; i++){
  253. struct guild *ag; //allied guld
  254. struct guild_alliance *ga = &g->alliance[i]; //guild alliance
  255. if(ga->guild_id && (ga->opposition==0) && (ag=guild_search(ga->guild_id))){
  256. for (j = 0; j < ag->max_member; j++){ //load all guildmember
  257. pl_sd = ag->member[j].sd;
  258. if(channel_haspc(ag->channel,pl_sd)==1) //only if they are in their own guildchan
  259. channel_join(g->channel,pl_sd);
  260. }
  261. }
  262. }
  263. return 0;
  264. }
  265. /**
  266. * Make a player join the guild channel
  267. * - Create a guild channel if it does not exist
  268. * @param sd: Player data
  269. * @param flag: Join type (1 - Guild chat, 2 - Ally chat)
  270. * @return
  271. * 0: Success
  272. * -1: Invalid player
  273. * -2: Player has no guild attached
  274. */
  275. int channel_gjoin(map_session_data *sd, int flag){
  276. struct Channel *channel;
  277. struct guild *g;
  278. if(!sd || sd->state.autotrade) return -1;
  279. g = sd->guild;
  280. if(!g) return -2;
  281. channel = g->channel;
  282. if(!channel){
  283. channel = channel_create_simple(NULL,NULL,CHAN_TYPE_ALLY,g->guild_id);
  284. g->channel = channel;
  285. channel_ajoin(g);
  286. }
  287. if(flag&1) {
  288. channel_join(channel,sd); //join our guild chat
  289. }
  290. if(flag&2){
  291. int i;
  292. for (i = 0; i < MAX_GUILDALLIANCE; i++){
  293. struct guild *ag; //allied guld
  294. struct guild_alliance *ga = &g->alliance[i]; //guild alliance
  295. if(ga->guild_id && (ga->opposition==0) && (ag=guild_search(ga->guild_id)) ) //only join allies
  296. channel_join(ag->channel,sd);
  297. }
  298. }
  299. return 0;
  300. }
  301. /**
  302. * Make player leave the channel and cleanup association
  303. * - If no one remains in the chat, delete it
  304. * @param channel: Channel data
  305. * @param sd: Player data
  306. * @param flag: Called from deletion process, do not recall delete
  307. * @return
  308. * 0: Success
  309. * -1: Invalid player or channel
  310. */
  311. int channel_clean(struct Channel *channel, map_session_data *sd, int flag) {
  312. unsigned char i;
  313. if(!channel || !sd)
  314. return -1;
  315. if( channel == sd->gcbind )
  316. sd->gcbind = NULL;
  317. ARR_FIND(0, sd->channel_count, i, sd->channels[i] == channel);
  318. if( i < sd->channel_count ) {
  319. unsigned char cursor = i;
  320. sd->channels[i] = NULL;
  321. sd->channel_tick[i] = 0;
  322. for(; i < sd->channel_count; i++ ) { //slice move list down
  323. if( sd->channels[i] == NULL )
  324. continue;
  325. if(i != cursor) {
  326. sd->channels[cursor] = sd->channels[i];
  327. sd->channel_tick[cursor] = sd->channel_tick[i];
  328. }
  329. cursor++;
  330. }
  331. if ( !(sd->channel_count = cursor) ) { //if in no more chan delete db
  332. aFree(sd->channels);
  333. aFree(sd->channel_tick);
  334. sd->channels = NULL;
  335. sd->channel_tick = NULL;
  336. }
  337. }
  338. idb_remove(channel->users,sd->status.char_id); //remove user for channel user list
  339. //auto delete when no more user in
  340. if( !db_size(channel->users) && !(flag&1) )
  341. channel_delete(channel,false);
  342. return 0;
  343. }
  344. /**
  345. * Make a player leave a type of channel
  346. * @param sd: Player data
  347. * @param type: Quit type (1 - Quit guild channel, 2 - Quit ally channel, 4 - Quit map channel, 8 - Quit all users in channel)
  348. * @return
  349. * 0: Success
  350. * -1: Invalid player
  351. */
  352. int channel_pcquit(map_session_data *sd, int type){
  353. int i;
  354. //On closing state we could have clean all chan by sd but pcquit is more used to free unit when
  355. //he quit a map_server, not call in map_quit cause we need to cleanup when we switch map-server as well
  356. if(!sd) return -1;
  357. // Leave all chat channels.
  358. if(type&(1|2) && channel_config.ally_tmpl.name[0] && sd->guild){ //quit guild and ally chan
  359. struct guild *g = sd->guild;
  360. if(type&1 && channel_haspc(g->channel,sd)==1){
  361. channel_clean(g->channel,sd,0); //leave guild chan
  362. }
  363. if(type&2){
  364. for (i = 0; i < MAX_GUILDALLIANCE; i++) { //leave all alliance chan
  365. struct guild *ag; //allied guild
  366. if( g->alliance[i].guild_id && (ag = guild_search(g->alliance[i].guild_id) ) ) {
  367. if(channel_haspc(ag->channel,sd) == 1)
  368. channel_clean(ag->channel,sd,0);
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  375. if(type&4 && channel_config.map_tmpl.name[0] && channel_haspc(mapdata->channel,sd)==1){ //quit map chan
  376. channel_clean(mapdata->channel,sd,0);
  377. }
  378. if(type&8 && sd->channel_count ) { //quit all chan
  379. uint8 count = sd->channel_count;
  380. for( i = count-1; i >= 0; i--) { //going backward to avoid shifting
  381. channel_clean(sd->channels[i],sd,0);
  382. }
  383. }
  384. return 0;
  385. }
  386. /**
  387. * Format message from player to send to the channel
  388. * - Also truncate extra characters if message is too long
  389. * @param channel: Channel data
  390. * @param sd: Player data
  391. * @param msg: Message to send
  392. * @return
  393. * 0: Success
  394. * -1: Invalid player, channel, or message
  395. * -2: Delay message from last message
  396. */
  397. int channel_send(struct Channel *channel, map_session_data *sd, const char *msg) {
  398. int idx = 0;
  399. if(!channel || !sd || !msg || (idx = channel_pc_haschan(sd, channel)) < 0)
  400. return -1;
  401. if(!pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) && channel->msg_delay != 0 && DIFF_TICK(sd->channel_tick[idx] + channel->msg_delay, gettick()) > 0) {
  402. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1455),false,SELF); //You're talking too fast!
  403. return -2;
  404. }
  405. else {
  406. char output[CHAT_SIZE_MAX];
  407. unsigned long color = channel->color;
  408. if((channel->opt&CHAN_OPT_COLOR_OVERRIDE) && sd->fontcolor && sd->fontcolor < channel_config.colors_count && channel_config.colors[sd->fontcolor])
  409. color = channel_config.colors[sd->fontcolor];
  410. safesnprintf(output, CHAT_SIZE_MAX, "%s %s : %s", channel->alias, sd->status.name, msg);
  411. clif_channel_msg(channel,output,color);
  412. sd->channel_tick[idx] = gettick();
  413. }
  414. return 0;
  415. }
  416. /**
  417. * Check parameters for channel creation
  418. * @param chname: Channel name
  419. * @param chpass: Channel password
  420. * @param type: Check types (1 - Check name # + length, 2 - Check if channel already exists, 4 - Check password length)
  421. * @return
  422. * 0: Success
  423. * -1: Bad channel name
  424. * -2: bad channel name length
  425. * -3: Password given too long
  426. * -4: Channel already exists
  427. */
  428. int channel_chk(char *chname, char *chpass, int type){
  429. if(type&1){ //check name
  430. if( chname[0] != '#' )
  431. return -1;
  432. if ( strlen(chname) < 3 || strlen(chname) > CHAN_NAME_LENGTH )
  433. return -2;
  434. if( (type&2) && (
  435. strcmpi(chname + 1,channel_config.map_tmpl.name) == 0
  436. || strcmpi(chname + 1,channel_config.ally_tmpl.name) == 0
  437. || strdb_exists(channel_db, chname + 1) )
  438. ) {
  439. return -4;
  440. }
  441. }
  442. if (type&4 && (chpass[0] != '\0' && strlen(chpass) > CHAN_NAME_LENGTH ) ) {
  443. return -3;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * Lookup a channel name
  449. * @param chname: Channel name
  450. * @param sd: Player data, can be NULL, used to solve #map and #ally cases
  451. * @param flag: Lookup types (1 - Create channel if it does not exist (map or ally only), 2 - Join the channel if not joined yet (map or ally only))
  452. * @return NULL on channel not found or channel data on success
  453. */
  454. struct Channel* channel_name2channel(char *chname, map_session_data *sd, int flag){
  455. if(channel_chk(chname, NULL, 1))
  456. return NULL;
  457. struct map_data *mapdata = sd ? map_getmapdata(sd->bl.m) : NULL;
  458. if(sd && strcmpi(chname + 1,channel_config.map_tmpl.name) == 0){
  459. if(flag&1 && !mapdata->channel)
  460. mapdata->channel = channel_create_simple(NULL,NULL,CHAN_TYPE_MAP,sd->bl.m);
  461. if(flag&2 && channel_pc_haschan(sd,mapdata->channel) < 1)
  462. channel_mjoin(sd);
  463. return mapdata->channel;
  464. }
  465. else if(sd && (strcmpi(chname + 1,channel_config.ally_tmpl.name) == 0) && sd->guild){
  466. if(flag&1 && !sd->guild->channel)
  467. sd->guild->channel = channel_create_simple(NULL,NULL,CHAN_TYPE_ALLY,sd->guild->guild_id);
  468. if(flag&2 && channel_pc_haschan(sd,mapdata->channel) < 1)
  469. channel_gjoin(sd,3);
  470. return sd->guild->channel;
  471. }
  472. else
  473. return (struct Channel*) strdb_get(channel_db, chname + 1);
  474. }
  475. /**
  476. * Check if player is in a channel
  477. * @param channel: Channel data
  478. * @param sd: Player data
  479. * @return
  480. * -1: Invalid player or channel
  481. * 0: Player not found or not banned
  482. * 1: Player is in channel
  483. */
  484. int channel_haspc(struct Channel *channel,map_session_data *sd){
  485. if(!channel || !sd) return -1;
  486. return (idb_exists(channel->users, sd->status.char_id))?1:0;
  487. }
  488. /**
  489. * Check if player is banned from channel
  490. * @return
  491. * -1: Invalid player or channel
  492. * 0: Player not found or not banned
  493. * 1: Player is banned
  494. */
  495. int channel_haspcbanned(struct Channel *channel,map_session_data *sd){
  496. if(!channel || !sd) return -1;
  497. return (idb_exists(channel->banned, sd->status.char_id))?1:0;
  498. }
  499. /**
  500. * Check if player has channel in their channel list
  501. * @param sd: Player data
  502. * @param channel: Channel data
  503. * @return
  504. * -1: Invalid channel or player
  505. * -2: Player not found or not in channel
  506. * x > 0: has_channel at index x
  507. */
  508. int channel_pc_haschan(map_session_data *sd, struct Channel *channel){
  509. int k;
  510. if(!channel || !sd) return -1; //channel or player doesn't exist
  511. ARR_FIND(0, sd->channel_count, k, strcmpi(channel->name,sd->channels[k]->name) == 0);
  512. if( k >= sd->channel_count ) return -2;
  513. return k;
  514. }
  515. /**
  516. * Display some information to users in channel
  517. * @param sd: Player data
  518. * @param options:
  519. * colors: Display available colors for channel system
  520. * mine: List of players in channel and number of users
  521. * void: List of public channel and map and guild and number of users
  522. * @return 0 on success or -1 on failure
  523. */
  524. int channel_display_list(map_session_data *sd, const char *options){
  525. if(!sd || !options)
  526. return -1;
  527. //display availaible colors
  528. if( options[0] != '\0' && strcmpi(options,"colors") == 0 ) {
  529. char msg[40];
  530. unsigned char k;
  531. clif_displaymessage(sd->fd, msg_txt(sd,1444)); // ---- Available Colors ----
  532. for( k = 0; k < channel_config.colors_count; k++ ) {
  533. if (channel_config.colors[k]) {
  534. sprintf(msg, msg_txt(sd,1445),channel_config.colors_name[k]);// - '%s'
  535. clif_messagecolor(&sd->bl,channel_config.colors[k],msg,false,SELF);
  536. }
  537. }
  538. }
  539. else if( options[0] != '\0' && strcmpi(options,"mine") == 0 ) { //display chan I'm into
  540. clif_displaymessage(sd->fd, msg_txt(sd,1475)); // ---- My Channels ----
  541. if(!sd->channel_count)
  542. clif_displaymessage(sd->fd, msg_txt(sd,1476)); // You have not joined any channels.
  543. else {
  544. unsigned char k;
  545. for(k = 0; k < sd->channel_count; k++) {
  546. char output[CHAT_SIZE_MAX];
  547. struct Channel *channel;
  548. if (!(channel = sd->channels[k]))
  549. continue;
  550. sprintf(output, msg_txt(sd,1409), channel->name, db_size(channel->users));// - #%s (%d users)
  551. clif_displaymessage(sd->fd, output);
  552. }
  553. }
  554. }
  555. else { //display public chanels
  556. DBIterator *iter;
  557. bool has_perm = pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) ? true : false;
  558. struct Channel *channel;
  559. char output[CHAT_SIZE_MAX];
  560. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  561. clif_displaymessage(sd->fd, msg_txt(sd,1410)); // ---- Public Channels ----
  562. if( channel_config.map_tmpl.name[0] && mapdata->channel ) {
  563. sprintf(output, msg_txt(sd,1409), mapdata->channel->name, db_size(mapdata->channel->users));// - #%s (%d users)
  564. clif_displaymessage(sd->fd, output);
  565. }
  566. if( channel_config.ally_tmpl.name[0] && sd->status.guild_id ) {
  567. struct guild *g = sd->guild;
  568. if (g && g->channel) {
  569. sprintf(output, msg_txt(sd,1409), g->channel->name, db_size(((struct Channel *)g->channel)->users));// - #%s (%d users)
  570. clif_displaymessage(sd->fd, output);
  571. }
  572. }
  573. iter = db_iterator(channel_db);
  574. for(channel = (struct Channel *)dbi_first(iter); dbi_exists(iter); channel = (struct Channel *)dbi_next(iter)) {
  575. if (!has_perm && !channel_pccheckgroup(channel, sd->group_id))
  576. continue;
  577. if( has_perm || channel->type == CHAN_TYPE_PUBLIC ) {
  578. sprintf(output, msg_txt(sd,1409), channel->name, db_size(channel->users));// - #%s (%d users)
  579. clif_displaymessage(sd->fd, output);
  580. }
  581. }
  582. dbi_destroy(iter);
  583. }
  584. return 0;
  585. }
  586. /**
  587. * A player is attempting to create a channel
  588. * @param sd: Player data
  589. * @param chname: Channel name
  590. * @param chpass: Channel password
  591. * @return 0 on success or -1 on failure
  592. */
  593. int channel_pccreate(map_session_data *sd, char *chname, char *chpass){
  594. char output[CHAT_SIZE_MAX];
  595. int8 res;
  596. if(!sd || !chname)
  597. return 0;
  598. res = channel_chk(chname,chpass,7);
  599. if(res==0){ //success
  600. struct Channel *channel = channel_create_simple(chname,chpass,CHAN_TYPE_PRIVATE,sd->status.char_id);
  601. channel_join(channel,sd);
  602. if( ( channel->opt & CHAN_OPT_ANNOUNCE_SELF ) ) {
  603. sprintf(output, msg_txt(sd,1403),chname); // You're now in the '%s' channel.
  604. clif_displaymessage(sd->fd, output);
  605. }
  606. } else { //failure display cause
  607. switch(res){
  608. case -1: sprintf(output, msg_txt(sd,1405), CHAN_NAME_LENGTH); break;// Channel name must start with '#'.
  609. case -2: sprintf(output, msg_txt(sd,1406), CHAN_NAME_LENGTH); break;// Channel length must be between 3 and %d.
  610. case -3: sprintf(output, msg_txt(sd,1436), CHAN_NAME_LENGTH); break;// Channel password can't be over %d characters.
  611. case -4: sprintf(output, msg_txt(sd,1407), chname);// Channel '%s' is not available.
  612. }
  613. clif_displaymessage(sd->fd, output);
  614. return -1;
  615. }
  616. return 0;
  617. }
  618. /**
  619. * A player is attempting to delete a channel
  620. * @param sd: Player data
  621. * @param chname: Channel name
  622. * @return 0 on success or -1 on failure
  623. */
  624. int channel_pcdelete(map_session_data *sd, char *chname){
  625. struct Channel *channel;
  626. char output[CHAT_SIZE_MAX];
  627. if(!sd || !chname) return 0;
  628. if( channel_chk(chname,NULL,1) ) {
  629. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  630. return -1;
  631. }
  632. channel = channel_name2channel(chname,sd,0);
  633. if(channel_pc_haschan(sd,channel)<0){
  634. sprintf(output, msg_txt(sd,1425),chname);// You're not part of the '%s' channel.
  635. clif_displaymessage(sd->fd, output);
  636. return -2; //channel doesn't exist or player don't have it
  637. }
  638. channel_delete(channel,false);
  639. sprintf(output, msg_txt(sd,1448),chname); // Channel '%s' deleted.
  640. clif_displaymessage(sd->fd, output);
  641. return 0;
  642. }
  643. /**
  644. * A player is attempting to leave a channel
  645. * @param sd: Player data
  646. * @param chname: Channel name
  647. * @return 0 on success or -1 on failure
  648. */
  649. int channel_pcleave(map_session_data *sd, char *chname){
  650. struct Channel *channel;
  651. char output[CHAT_SIZE_MAX];
  652. if(!sd || !chname)
  653. return 0;
  654. if( channel_chk(chname,NULL,1) ) {
  655. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  656. return -1;
  657. }
  658. channel = channel_name2channel(chname,sd,0);
  659. if(channel_pc_haschan(sd,channel)<0){
  660. sprintf(output, msg_txt(sd,1425),chname);// You're not part of the '%s' channel.
  661. clif_displaymessage(sd->fd, output);
  662. return -2; //channel doesn't exist or player don't have it
  663. }
  664. if (!(channel->opt&CHAN_OPT_CAN_LEAVE)) {
  665. sprintf(output, msg_txt(sd,762), chname); // You cannot leave channel '%s'.
  666. clif_displaymessage(sd->fd, output);
  667. return -1;
  668. }
  669. if( !channel_config.closing && (channel->opt & CHAN_OPT_ANNOUNCE_LEAVE) ) {
  670. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,763), channel->alias, sd->status.name); // %s %s left.
  671. clif_channel_msg(channel,output,channel->color);
  672. }
  673. switch(channel->type){
  674. case CHAN_TYPE_ALLY: channel_pcquit(sd,3); break;
  675. case CHAN_TYPE_MAP: channel_pcquit(sd,4); break;
  676. default: //private and public atm
  677. channel_clean(channel,sd,0);
  678. }
  679. sprintf(output, msg_txt(sd,1426),chname); // You've left the '%s' channel.
  680. clif_displaymessage(sd->fd, output);
  681. return 0;
  682. }
  683. /**
  684. * A player is attempting to join a channel
  685. * @param sd: Player data
  686. * @param chname: Channel name
  687. * @param pass: Channel password
  688. * @return 0 on success or -1 on failure
  689. */
  690. int channel_pcjoin(map_session_data *sd, char *chname, char *pass){
  691. struct Channel *channel;
  692. char output[CHAT_SIZE_MAX];
  693. if(!sd || !chname)
  694. return 0;
  695. if( channel_chk(chname,NULL,1) ) {
  696. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  697. return -1;
  698. }
  699. channel = channel_name2channel(chname,sd,1);
  700. if(channel){
  701. if (!pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) && !channel_pccheckgroup(channel, sd->group_id)) {
  702. sprintf(output, msg_txt(sd,1407), chname); // Channel '%s' is not available.
  703. clif_displaymessage(sd->fd, output);
  704. return -1;
  705. }
  706. if(channel_haspc(channel,sd)==1) {
  707. sprintf(output, msg_txt(sd,1434),chname); // You're already in the '%s' channel.
  708. clif_displaymessage(sd->fd, output);
  709. return -1;
  710. }
  711. else if( channel->pass[0] != '\0') { //chan has a pass
  712. if(strcmp(channel->pass,pass) != 0){ //wrong pass entry
  713. if( pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) ) {
  714. sd->stealth = true;
  715. } else {
  716. sprintf(output, msg_txt(sd,1401),chname,"@join"); // Channel '%s' is password-protected (usage: %s <#channel_name> <password>).
  717. clif_displaymessage(sd->fd, output);
  718. return -1;
  719. }
  720. }
  721. }
  722. }
  723. else {
  724. sprintf(output, msg_txt(sd,1400),chname,"@join"); // Unknown channel '%s' (usage: %s <#channel_name>).
  725. clif_displaymessage(sd->fd, output);
  726. return -1;
  727. }
  728. switch(channel->type){
  729. case CHAN_TYPE_ALLY: channel_gjoin(sd,3); break;
  730. case CHAN_TYPE_MAP: channel_mjoin(sd); break;
  731. default: //private and public atm
  732. if (channel_join(channel,sd) != 0)
  733. return -1;
  734. }
  735. if( ( channel->opt & CHAN_OPT_ANNOUNCE_SELF ) ) {
  736. sprintf(output, msg_txt(sd,1403),chname); // You're now in the '%s' channel.
  737. clif_displaymessage(sd->fd, output);
  738. }
  739. return 0;
  740. }
  741. /**
  742. * A player is attempting to change the channel color
  743. * @param sd: Player data
  744. * @param chname: Channel name
  745. * @param color: New color
  746. * @return 0 on success or -1 on failure
  747. */
  748. int channel_pccolor(map_session_data *sd, char *chname, char *color){
  749. struct Channel *channel;
  750. char output[CHAT_SIZE_MAX];
  751. int k;
  752. if(!sd)
  753. return 0;
  754. if( channel_chk(chname,NULL,1) ) {
  755. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  756. return -1;
  757. }
  758. channel = channel_name2channel(chname,sd,0);
  759. if( !channel ) {
  760. sprintf(output, msg_txt(sd,1407), chname);// Channel '%s' is not available.
  761. clif_displaymessage(sd->fd, output);
  762. return -1;
  763. }
  764. if( !pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) ) {
  765. if (channel->char_id != sd->status.char_id) {
  766. sprintf(output, msg_txt(sd,1412), chname);// You're not the owner of channel '%s'.
  767. clif_displaymessage(sd->fd, output);
  768. return -1;
  769. }
  770. else if (!(channel->opt&CHAN_OPT_COLOR_OVERRIDE)) {
  771. sprintf(output, msg_txt(sd,764), chname); // You cannot change the color for channel '%s'.
  772. clif_displaymessage(sd->fd, output);
  773. return -1;
  774. }
  775. }
  776. ARR_FIND(0,channel_config.colors_count,k,( strcmpi(color,channel_config.colors_name[k]) == 0 ) );
  777. if( k >= channel_config.colors_count ) {
  778. sprintf(output, msg_txt(sd,1411), color);// Unknown color '%s'.
  779. clif_displaymessage(sd->fd, output);
  780. return -1;
  781. }
  782. channel->color = channel_config.colors[k];
  783. sprintf(output, msg_txt(sd,1413),chname,channel_config.colors_name[k]);// '%s' channel color updated to '%s'.
  784. clif_displaymessage(sd->fd, output);
  785. return 0;
  786. }
  787. /**
  788. * A player is attempting to bind
  789. * - Makes default message output display to the channel
  790. * @param sd: Player data
  791. * @param chname: Channel name
  792. * @return 0 on success, -1 on incorrect channel name, or -2 channel doesn't exist or player didn't join
  793. */
  794. int channel_pcbind(map_session_data *sd, char *chname){
  795. struct Channel *channel;
  796. char output[CHAT_SIZE_MAX];
  797. if(!sd)
  798. return 0;
  799. if( channel_chk(chname,NULL,1) ) {
  800. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  801. return -1;
  802. }
  803. channel = channel_name2channel(chname,sd,0);
  804. if(channel_pc_haschan(sd,channel)<0){
  805. sprintf(output, msg_txt(sd,1425),chname);// You're not part of the '%s' channel.
  806. clif_displaymessage(sd->fd, output);
  807. return -2; //channel doesn't exist or player don't have it
  808. }
  809. sd->gcbind = channel;
  810. sprintf(output, msg_txt(sd,1431),chname); // Your global chat is now binded to the '%s' channel.
  811. clif_displaymessage(sd->fd, output);
  812. return 0;
  813. }
  814. /**
  815. * A player is attempting to unbind
  816. * @param sd: Player data
  817. * @return 0 on success or -1 on failure
  818. */
  819. int channel_pcunbind(map_session_data *sd){
  820. char output[CHAT_SIZE_MAX];
  821. if(!sd)
  822. return 0;
  823. if( sd->gcbind == NULL ) {
  824. clif_displaymessage(sd->fd, msg_txt(sd,1432));// Your global chat is not binded to any channel.
  825. return -1;
  826. }
  827. sprintf(output, msg_txt(sd,1433),sd->gcbind->name); // Your global chat is now unbinded from the '#%s' channel.
  828. clif_displaymessage(sd->fd, output);
  829. sd->gcbind = NULL;
  830. return 0;
  831. }
  832. /**
  833. * A player is attempting to modify the banlist
  834. * @param sd: Player data
  835. * @param chname: Channel name
  836. * @param pname: Player to ban or unban
  837. * @param flag: Ban options (0 - Ban, 1 - Unban, 2 - Unban all, 3 - Ban list)
  838. * @return 0 on success or -1 on failure
  839. */
  840. int channel_pcban(map_session_data *sd, char *chname, char *pname, int flag){
  841. struct Channel *channel;
  842. char output[CHAT_SIZE_MAX];
  843. map_session_data *tsd = map_nick2sd(pname,false);
  844. if( channel_chk(chname,NULL,1) ) {
  845. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  846. return -1;
  847. }
  848. channel = channel_name2channel(chname,sd,0);
  849. if( !channel ) {
  850. sprintf(output, msg_txt(sd,1407), chname);// Channel '%s' is not available.
  851. clif_displaymessage(sd->fd, output);
  852. return -1;
  853. }
  854. if( !pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) ) {
  855. if (channel->char_id != sd->status.char_id) {
  856. sprintf(output, msg_txt(sd,1412), chname);// You're not the owner of channel '%s'.
  857. clif_displaymessage(sd->fd, output);
  858. return -1;
  859. } else if (!channel_config.private_channel.ban) {
  860. sprintf(output, msg_txt(sd,765), chname); // You're not allowed to ban a player.
  861. clif_displaymessage(sd->fd, output);
  862. return -1;
  863. }
  864. }
  865. if(flag != 2 && flag != 3){
  866. char banned;
  867. if(!tsd || pc_has_permission(tsd, PC_PERM_CHANNEL_ADMIN) ) {
  868. sprintf(output, msg_txt(sd,1464), pname);// Ban failed for player '%s'.
  869. clif_displaymessage(sd->fd, output);
  870. return -1;
  871. }
  872. banned = channel_haspcbanned(channel,tsd);
  873. if(!flag && banned==1) {
  874. sprintf(output, msg_txt(sd,1465), tsd->status.name);// Player '%s' is already banned from this channel.
  875. clif_displaymessage(sd->fd, output);
  876. return -1;
  877. }
  878. else if(flag==1 && banned==0) {
  879. sprintf(output, msg_txt(sd,1440), tsd->status.name);// Player '%s' is not banned from this channel.
  880. clif_displaymessage(sd->fd, output);
  881. return -1;
  882. }
  883. }
  884. else {
  885. if( !db_size(channel->banned) ) {
  886. sprintf(output, msg_txt(sd,1439), chname);// Channel '%s' contains no banned players.
  887. clif_displaymessage(sd->fd, output);
  888. return 0;
  889. }
  890. }
  891. //let properly alter the list now
  892. switch(flag){
  893. case 0: {
  894. struct chan_banentry *cbe;
  895. if (!tsd)
  896. return -1;
  897. CREATE(cbe, struct chan_banentry, 1);
  898. cbe->char_id = tsd->status.char_id;
  899. strcpy(cbe->char_name,tsd->status.name);
  900. idb_put(channel->banned, tsd->status.char_id, cbe);
  901. channel_clean(channel,tsd,0);
  902. sprintf(output, msg_txt(sd,1437),tsd->status.name,chname); // Player '%s' is banned from the '%s' channel.
  903. break;
  904. }
  905. case 1:
  906. if (!tsd)
  907. return -1;
  908. idb_remove(channel->banned, tsd->status.char_id);
  909. sprintf(output, msg_txt(sd,1441),tsd->status.name,chname); // Player '%s' is unbanned from the '%s' channel.
  910. break;
  911. case 2:
  912. db_clear(channel->banned);
  913. sprintf(output, msg_txt(sd,1442),chname); // Cleared all bans from the '%s' channel.
  914. break;
  915. case 3: {
  916. DBIterator *iter = db_iterator(channel->banned);
  917. struct chan_banentry *cbe;
  918. sprintf(output, msg_txt(sd,1443), channel->name);// ---- '#%s' Ban List:
  919. clif_displaymessage(sd->fd, output);
  920. for( cbe = (struct chan_banentry *)dbi_first(iter); dbi_exists(iter); cbe = (struct chan_banentry *)dbi_next(iter) ) { //for all users
  921. if (cbe->char_name[0])
  922. sprintf(output, "%d: %s",cbe->char_id,cbe->char_name);
  923. else
  924. sprintf(output, "%d: ****",cbe->char_id);
  925. clif_displaymessage(sd->fd, output);
  926. }
  927. dbi_destroy(iter);
  928. }
  929. return 0;
  930. }
  931. clif_displaymessage(sd->fd, output);
  932. return 0;
  933. }
  934. /**
  935. * A player is attemting to kick a player
  936. * @param sd: Player data
  937. * @param chname: Channel name
  938. * @param pname: Player name to kick
  939. * @return 0 on success or -1 on failure
  940. */
  941. int channel_pckick(map_session_data *sd, char *chname, char *pname) {
  942. struct Channel *channel;
  943. char output[CHAT_SIZE_MAX];
  944. map_session_data *tsd = map_nick2sd(pname,false);
  945. if( channel_chk(chname,NULL,1) ) {
  946. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  947. return -1;
  948. }
  949. channel = channel_name2channel(chname,sd,0);
  950. if( !channel ) {
  951. sprintf(output, msg_txt(sd,1407), chname);// Channel '%s' is not available.
  952. clif_displaymessage(sd->fd, output);
  953. return -1;
  954. }
  955. if (!tsd) {
  956. clif_displaymessage(sd->fd, msg_txt(sd,3));
  957. return -1;
  958. }
  959. if( !pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) ) {
  960. if (channel->char_id != sd->status.char_id) {
  961. sprintf(output, msg_txt(sd,1412), chname);// You're not the owner of channel '%s'.
  962. clif_displaymessage(sd->fd, output);
  963. } else if (!channel_config.private_channel.kick) {
  964. sprintf(output, msg_txt(sd,766), chname); // You cannot kick a player from channel '%s'.
  965. clif_displaymessage(sd->fd, output);
  966. }
  967. return -1;
  968. }
  969. if (channel_pc_haschan(sd, channel) < 0) {
  970. sprintf(output, msg_txt(sd,1425), chname); // You're not part of the '%s' channel.
  971. clif_displaymessage(sd->fd, output);
  972. return -1;
  973. }
  974. if (channel->char_id == sd->status.char_id) {
  975. clif_displaymessage(sd->fd, msg_txt(sd, 767)); // You're not allowed to kick a player.
  976. return -1;
  977. }
  978. if( !channel_config.closing && (channel->opt & CHAN_OPT_ANNOUNCE_LEAVE) ) {
  979. safesnprintf(output, CHAT_SIZE_MAX, msg_txt(sd,768), channel->alias, tsd->status.name); // %s %s has been kicked.
  980. clif_channel_msg(channel,output,channel->color);
  981. }
  982. switch(channel->type){
  983. case CHAN_TYPE_ALLY: channel_pcquit(tsd,3); break;
  984. case CHAN_TYPE_MAP: channel_pcquit(tsd,4); break;
  985. default: //private and public atm
  986. channel_clean(channel,tsd,0);
  987. }
  988. return 1;
  989. }
  990. /**
  991. * A player is attempting to set an option on the channel
  992. * @param sd: Player data
  993. * @param chname: Channel name
  994. * @param option: Option to change
  995. * @param val: Option value
  996. * @return 0 on success or -1 on failure
  997. */
  998. int channel_pcsetopt(map_session_data *sd, char *chname, const char *option, const char *val){
  999. struct Channel *channel;
  1000. char output[CHAT_SIZE_MAX];
  1001. int k, s = 0, opt;
  1002. const char* opt_str[] = {
  1003. "None",
  1004. "SelfAnnounce",
  1005. "JoinAnnounce",
  1006. "LeaveAnnounce",
  1007. "MessageDelay",
  1008. "ColorOverride",
  1009. "CanChat",
  1010. "CanLeave",
  1011. "Autojoin",
  1012. };
  1013. if( channel_chk(chname,NULL,1) ) {
  1014. clif_displaymessage(sd->fd, msg_txt(sd,1405));// Channel name must start with '#'.
  1015. return -1;
  1016. }
  1017. if (!sd)
  1018. return - 1;
  1019. channel = channel_name2channel(chname,sd,0);
  1020. if( !channel ) {
  1021. sprintf(output, msg_txt(sd,1407), chname);// Channel '%s' is not available.
  1022. clif_displaymessage(sd->fd, output);
  1023. return -1;
  1024. }
  1025. if( sd && channel->char_id != sd->status.char_id && !pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) ) {
  1026. sprintf(output, msg_txt(sd,1412), chname);// You're not the owner of channel '%s'.
  1027. clif_displaymessage(sd->fd, output);
  1028. return -1;
  1029. }
  1030. s = ARRAYLENGTH(opt_str);
  1031. ARR_FIND(1,s,k,( strncmpi(option,opt_str[k],3) == 0 )); //we only cmp 3 letter atm
  1032. if(!option || option[0] == '\0' || k >= s ) {
  1033. sprintf(output, msg_txt(sd,1447), option);// Unknown channel option '%s'.
  1034. clif_displaymessage(sd->fd, output);
  1035. clif_displaymessage(sd->fd, msg_txt(sd,1414));// ---- Available options:
  1036. for( k = 1; k < s; k++ ) {
  1037. sprintf(output, msg_txt(sd,1445), opt_str[k]);// - '%s'
  1038. clif_displaymessage(sd->fd, output);
  1039. }
  1040. return -1;
  1041. }
  1042. opt = 1<<(k-1);
  1043. if (channel->type == CHAN_TYPE_PRIVATE && !pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN)) {
  1044. switch (opt) {
  1045. case CHAN_OPT_MSG_DELAY:
  1046. if (!channel_config.private_channel.change_delay)
  1047. return -1;
  1048. break;
  1049. case CHAN_OPT_COLOR_OVERRIDE:
  1050. if (!channel_config.private_channel.color_override)
  1051. return -1;
  1052. break;
  1053. }
  1054. }
  1055. if( val[0] == '\0' ) {
  1056. if ( opt == CHAN_OPT_MSG_DELAY ) {
  1057. sprintf(output, msg_txt(sd,1466), opt_str[k]);// Input the number of seconds (0-10) for the '%s' option.
  1058. clif_displaymessage(sd->fd, output);
  1059. return -1;
  1060. } else if( channel->opt & opt ) {
  1061. sprintf(output, msg_txt(sd,1449), opt_str[k],opt_str[k]); // Option '%s' is already enabled (use '@channel setopt %s 0' to disable).
  1062. clif_displaymessage(sd->fd, output);
  1063. return -1;
  1064. } else {
  1065. channel->opt |= opt;
  1066. sprintf(output, msg_txt(sd,1450), opt_str[k],channel->name);// Option '%s' is enabled for channel '#%s'.
  1067. clif_displaymessage(sd->fd, output);
  1068. }
  1069. } else {
  1070. int v = atoi(val);
  1071. if( opt == CHAN_OPT_MSG_DELAY ) {
  1072. if( v < 0 || v > 10 ) {
  1073. sprintf(output, msg_txt(sd,1451), v, opt_str[k]);// Value '%d' for option '%s' is out of range (limit 0-10).
  1074. clif_displaymessage(sd->fd, output);
  1075. return -1;
  1076. }
  1077. if( v == 0 ) {
  1078. channel->opt &=~ opt;
  1079. channel->msg_delay = 0;
  1080. sprintf(output, msg_txt(sd,1453), opt_str[k],channel->name,v);// Option '%s' is disabled for channel '#%s'.
  1081. clif_displaymessage(sd->fd, output);
  1082. } else {
  1083. channel->opt |= opt;
  1084. channel->msg_delay = v * 1000;
  1085. sprintf(output, msg_txt(sd,1452), opt_str[k],channel->name,v);// Option '%s' is enabled for channel '#%s' at %d seconds.
  1086. clif_displaymessage(sd->fd, output);
  1087. }
  1088. } else {
  1089. if( v ) {
  1090. if( channel->opt & opt ) {
  1091. sprintf(output, msg_txt(sd,1449), opt_str[k],opt_str[k]); // Option '%s' is already enabled (use '@channel setopt %s 0' to disable).
  1092. clif_displaymessage(sd->fd, output);
  1093. return -1;
  1094. } else {
  1095. channel->opt |= opt;
  1096. sprintf(output, msg_txt(sd,1450), opt_str[k],channel->name);// Option '%s' is enabled for channel '#%s'.
  1097. clif_displaymessage(sd->fd, output);
  1098. }
  1099. } else {
  1100. if( !(channel->opt & opt) ) {
  1101. sprintf(output, msg_txt(sd,1450), opt_str[k],channel->name); // Option '%s' is enabled for channel '#%s'.
  1102. clif_displaymessage(sd->fd, output);
  1103. return -1;
  1104. } else {
  1105. channel->opt &=~ opt;
  1106. sprintf(output, msg_txt(sd,1453), opt_str[k],channel->name);// Option '%s' is disabled for channel '#%s'.
  1107. clif_displaymessage(sd->fd, output);
  1108. }
  1109. }
  1110. }
  1111. }
  1112. return 0;
  1113. }
  1114. /**
  1115. * Check if the given group ID can join the channel
  1116. * @param channel: Channel data
  1117. * @param group_id: Group ID to check
  1118. * @return True on success or false on failure
  1119. */
  1120. bool channel_pccheckgroup(struct Channel *channel, int group_id) {
  1121. unsigned short i;
  1122. nullpo_ret(channel);
  1123. if (!channel->groups || !channel->group_count)
  1124. return true;
  1125. for (i = 0; i < channel->group_count; i++) {
  1126. if (channel->groups[i] == group_id)
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. /**
  1132. * Attempt to autojoin a player to a channel
  1133. */
  1134. int channel_pcautojoin_sub(DBKey key, DBData *data, va_list ap) {
  1135. struct Channel *channel = (struct Channel *)db_data2ptr(data);
  1136. map_session_data *sd = NULL;
  1137. char channame[CHAN_NAME_LENGTH+1];
  1138. nullpo_ret(channel);
  1139. nullpo_ret((sd = va_arg(ap, map_session_data *)));
  1140. if (channel->pass[0])
  1141. return 0;
  1142. if (!(channel->opt&CHAN_OPT_AUTOJOIN))
  1143. return 0;
  1144. if (!pc_has_permission(sd, PC_PERM_CHANNEL_ADMIN) && !channel_pccheckgroup(channel, sd->group_id))
  1145. return 0;
  1146. safesnprintf(channame, sizeof(channame), "#%s", channel->name);
  1147. channel_pcjoin(sd, channame, NULL);
  1148. return 1;
  1149. }
  1150. /**
  1151. * Attempt to autojoin a player to a channel
  1152. * @param sd: Player data
  1153. */
  1154. void channel_autojoin(map_session_data *sd) {
  1155. nullpo_retv(sd);
  1156. if (sd->state.autotrade || !sd->fd)
  1157. return;
  1158. channel_db->foreach(channel_db, channel_pcautojoin_sub, sd);
  1159. }
  1160. /**
  1161. * Get color by name or RGB hex code
  1162. * @param color_str: Color name
  1163. * @return color in RGB
  1164. */
  1165. unsigned long channel_getColor(const char *color_str) {
  1166. int i;
  1167. for (i = 0; i < channel_config.colors_count; i++) {
  1168. if (strcmpi(channel_config.colors_name[i], color_str) == 0)
  1169. return channel_config.colors[i];
  1170. }
  1171. return channel_config.colors[0];
  1172. }
  1173. /**
  1174. * Attempt to create a global channel from the channel config
  1175. * @param chan: Channel list
  1176. * @param tmp_chan: Temporary channel data
  1177. * @param i: Index
  1178. * @return True on success or false on failure
  1179. */
  1180. bool channel_read_sub(config_setting_t *chan, struct Channel *tmp_chan, uint8 i) {
  1181. config_setting_t *group_list = NULL;
  1182. int delay = 1000, autojoin = 0, leave = 1, chat = 1, color_override = 0,
  1183. self_notif = 1, join_notif = 0, leave_notif = 0;
  1184. int64 type = CHAN_TYPE_PUBLIC;
  1185. int group_count = 0;
  1186. const char *name = NULL, *password = NULL, *alias = NULL, *color_str = "Default", *type_str = NULL;
  1187. if (tmp_chan == NULL)
  1188. return false;
  1189. if (!config_setting_lookup_string(chan, "name", &name)) {
  1190. ShowError("Please input channel 'name' at '%s' line '%d'! Skipping...\n", chan->file, chan->line);
  1191. return false;
  1192. }
  1193. if (config_setting_lookup_string(chan, "type", &type_str) && !script_get_constant(type_str, &type)) {
  1194. ShowError("Invalid channel type %s at '%s' line '%d'! Skipping...\n", type_str, chan->file, chan->line);
  1195. return false;
  1196. }
  1197. config_setting_lookup_string(chan, "password", &password);
  1198. config_setting_lookup_string(chan, "alias", &alias);
  1199. config_setting_lookup_string(chan, "color", &color_str);
  1200. config_setting_lookup_int(chan, "delay", &delay);
  1201. config_setting_lookup_bool(chan, "autojoin", &autojoin);
  1202. config_setting_lookup_bool(chan, "leave", &leave);
  1203. config_setting_lookup_bool(chan, "chat", &chat);
  1204. config_setting_lookup_bool(chan, "color_override", &color_override);
  1205. config_setting_lookup_bool(chan, "self_notif", &self_notif);
  1206. config_setting_lookup_bool(chan, "join_notif", &join_notif);
  1207. config_setting_lookup_bool(chan, "leave_notif", &leave_notif);
  1208. safestrncpy(tmp_chan->name,name+1,sizeof(tmp_chan->name));
  1209. if (password)
  1210. safestrncpy(tmp_chan->pass,password,sizeof(tmp_chan->pass));
  1211. else
  1212. tmp_chan->pass[0] = '\0';
  1213. safestrncpy(tmp_chan->alias,alias?alias:name,sizeof(tmp_chan->alias));
  1214. tmp_chan->msg_delay = delay;
  1215. tmp_chan->type = (enum Channel_Type)type;
  1216. tmp_chan->color = channel_getColor(color_str);
  1217. tmp_chan->opt = (autojoin ? CHAN_OPT_AUTOJOIN : 0) |
  1218. (leave ? CHAN_OPT_CAN_LEAVE : 0) |
  1219. (chat ? CHAN_OPT_CAN_CHAT : 0) |
  1220. (color_override ? CHAN_OPT_COLOR_OVERRIDE : 0) |
  1221. (self_notif ? CHAN_OPT_ANNOUNCE_SELF : 0) |
  1222. (join_notif ? CHAN_OPT_ANNOUNCE_JOIN : 0) |
  1223. (leave_notif ? CHAN_OPT_ANNOUNCE_LEAVE : 0);
  1224. if ((group_list = config_setting_get_member(chan, "groupid")) && (group_count = config_setting_length(group_list)) > 0) {
  1225. int j;
  1226. CREATE(tmp_chan->groups, unsigned short, group_count);
  1227. tmp_chan->group_count = group_count;
  1228. for (j = 0; j < group_count; j++) {
  1229. int groupid = config_setting_get_int_elem(group_list, j);
  1230. tmp_chan->groups[j] = groupid;
  1231. }
  1232. }
  1233. return true;
  1234. }
  1235. /**
  1236. * Read and verify configuration in file
  1237. * Assign table value with value
  1238. */
  1239. void channel_read_config(void) {
  1240. config_t channels_conf;
  1241. config_setting_t *chan_setting = NULL;
  1242. if (conf_read_file(&channels_conf, channel_conf)) {
  1243. ShowError("Cannot read file '%s' for channel connfig.\n", channel_conf);
  1244. return;
  1245. }
  1246. chan_setting = config_lookup(&channels_conf, "channel_config");
  1247. if (chan_setting != NULL) {
  1248. config_setting_t *colors, *private_channel = NULL, *channels = NULL;
  1249. int count = 0, channel_count = 0;
  1250. colors = config_setting_get_member(chan_setting, "colors");
  1251. if (colors != NULL) {
  1252. int i, color_count = config_setting_length(colors);
  1253. CREATE(channel_config.colors, unsigned long, color_count);
  1254. CREATE(channel_config.colors_name, char *, color_count);
  1255. for (i = 0; i < color_count; i++) {
  1256. config_setting_t *color = config_setting_get_elem(colors, i);
  1257. CREATE(channel_config.colors_name[i], char, CHAN_NAME_LENGTH);
  1258. safestrncpy(channel_config.colors_name[i], config_setting_name(color), CHAN_NAME_LENGTH);
  1259. channel_config.colors[i] = strtoul(config_setting_get_string_elem(colors,i),NULL,0);
  1260. channel_config.colors[i] = (channel_config.colors[i] & 0x0000FF) << 16 | (channel_config.colors[i] & 0x00FF00) | (channel_config.colors[i] & 0xFF0000) >> 16;//RGB to BGR
  1261. }
  1262. channel_config.colors_count = color_count;
  1263. }
  1264. private_channel = config_setting_get_member(chan_setting, "private_channel");
  1265. if (private_channel != NULL) {
  1266. int allow = 1, ban = 1, kick = 1, color_override = 0, change_delay = 0,
  1267. self_notif = 1, join_notif = 0, leave_notif = 0,
  1268. delay = 1000, max_member = 1000;
  1269. const char *color_str = "Default";
  1270. config_setting_lookup_bool(private_channel, "allow", &allow);
  1271. config_setting_lookup_int(private_channel, "delay", &delay);
  1272. config_setting_lookup_string(private_channel, "color", &color_str);
  1273. config_setting_lookup_int(private_channel, "max_member", &max_member);
  1274. config_setting_lookup_bool(private_channel, "self_notif", &self_notif);
  1275. config_setting_lookup_bool(private_channel, "join_notif", &join_notif);
  1276. config_setting_lookup_bool(private_channel, "leave_notif", &leave_notif);
  1277. config_setting_lookup_bool(private_channel, "ban", &ban);
  1278. config_setting_lookup_bool(private_channel, "kick", &kick);
  1279. config_setting_lookup_bool(private_channel, "color_override", &color_override);
  1280. config_setting_lookup_bool(private_channel, "change_delay", &change_delay);
  1281. channel_config.private_channel.allow = allow;
  1282. channel_config.private_channel.color = channel_getColor(color_str);
  1283. channel_config.private_channel.delay = delay;
  1284. channel_config.private_channel.max_member = min(max_member, UINT16_MAX);
  1285. channel_config.private_channel.ban = ban;
  1286. channel_config.private_channel.kick = kick;
  1287. channel_config.private_channel.color_override = color_override;
  1288. channel_config.private_channel.change_delay = change_delay;
  1289. channel_config.private_channel.opt = CHAN_OPT_CAN_CHAT|CHAN_OPT_CAN_LEAVE|
  1290. (color_override ? CHAN_OPT_COLOR_OVERRIDE : 0) |
  1291. (self_notif ? CHAN_OPT_ANNOUNCE_SELF : 0) |
  1292. (join_notif ? CHAN_OPT_ANNOUNCE_JOIN : 0) |
  1293. (leave_notif ? CHAN_OPT_ANNOUNCE_LEAVE : 0);
  1294. }
  1295. channels = config_setting_get_member(chan_setting, "ally");
  1296. if (channels != NULL) {
  1297. memset(&channel_config.ally_tmpl, 0, sizeof(struct Channel));
  1298. channel_read_sub(channels, &channel_config.ally_tmpl, 0);
  1299. }
  1300. channels = config_setting_get_member(chan_setting, "map");
  1301. if (channels != NULL) {
  1302. memset(&channel_config.map_tmpl, 0, sizeof(struct Channel));
  1303. channel_read_sub(channels, &channel_config.map_tmpl, 0);
  1304. }
  1305. channels = config_setting_get_member(chan_setting, "channels");
  1306. if (channels != NULL && (count = config_setting_length(channels)) > 0) {
  1307. int i;
  1308. for (i = 0; i < count; i++) {
  1309. config_setting_t *chan = config_setting_get_elem(channels, i);
  1310. struct Channel *channel = NULL, tmp_chan;
  1311. memset(&tmp_chan, 0, sizeof(struct Channel));
  1312. if (!channel_read_sub(chan, &tmp_chan, i))
  1313. continue;
  1314. if ((channel = channel_create(&tmp_chan))) {
  1315. channel_count++;
  1316. channel->group_count = tmp_chan.group_count;
  1317. channel->groups = tmp_chan.groups;
  1318. }
  1319. }
  1320. }
  1321. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' channels in '" CL_WHITE "%s" CL_RESET "'.\n", db_size(channel_db), channel_conf);
  1322. config_destroy(&channels_conf);
  1323. }
  1324. }
  1325. /**
  1326. * Initialise db and read configuration
  1327. */
  1328. void do_init_channel(void) {
  1329. channel_db = stridb_alloc(static_cast<DBOptions>(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA), CHAN_NAME_LENGTH);
  1330. memset(&channel_config.private_channel, 0, sizeof(struct Channel));
  1331. memset(&channel_config.ally_tmpl, 0, sizeof(struct Channel));
  1332. memset(&channel_config.map_tmpl, 0, sizeof(struct Channel));
  1333. channel_read_config();
  1334. }
  1335. /**
  1336. * Close all channels and cleanup
  1337. */
  1338. void do_final_channel(void) {
  1339. DBIterator *iter;
  1340. struct Channel *channel;
  1341. //delete all in remaining chan db
  1342. iter = db_iterator(channel_db);
  1343. for( channel = (struct Channel *)dbi_first(iter); dbi_exists(iter); channel = (struct Channel *)dbi_next(iter) ) {
  1344. channel_delete(channel,false);
  1345. }
  1346. dbi_destroy(iter);
  1347. //at this point all user should have left their channel (private and public should be gone)
  1348. db_destroy(channel_db);
  1349. //delete all color thing
  1350. if( channel_config.colors_count ) {
  1351. int i=0;
  1352. for(i = 0; i < channel_config.colors_count; i++) {
  1353. aFree(channel_config.colors_name[i]);
  1354. }
  1355. aFree(channel_config.colors_name);
  1356. aFree(channel_config.colors);
  1357. }
  1358. }