msdp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Igor van den Hoven 2011 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #include "telnet.h"
  27. // Set table size and check for errors. Call once at startup.
  28. void init_msdp_table(void)
  29. {
  30. int index;
  31. for (index = 0 ; *msdp_table[index].name ; index++)
  32. {
  33. if (strcmp(msdp_table[index].name, msdp_table[index+1].name) > 0)
  34. {
  35. if (*msdp_table[index+1].name)
  36. {
  37. print_stdout("\e[31minit_msdp_table: Improperly sorted variable: %s.\e0m", msdp_table[index+1].name);
  38. }
  39. }
  40. }
  41. gtd->msdp_table_size = index;
  42. }
  43. // Binary search on the msdp_table.
  44. int msdp_find(char *var)
  45. {
  46. int val, bot, top, srt;
  47. bot = 0;
  48. top = gtd->msdp_table_size - 1;
  49. val = top / 2;
  50. while (bot <= top)
  51. {
  52. srt = strcmp(var, msdp_table[val].name);
  53. if (srt < 0)
  54. {
  55. top = val - 1;
  56. }
  57. else if (srt > 0)
  58. {
  59. bot = val + 1;
  60. }
  61. else
  62. {
  63. return val;
  64. }
  65. val = bot + (top - bot) / 2;
  66. }
  67. tintin_printf2(NULL, "msdp_find: %s (-1)", var, val);
  68. return -1;
  69. }
  70. void arachnos_devel(struct session *ses, char *fmt, ...)
  71. {
  72. char buf[STRING_SIZE];
  73. va_list args;
  74. va_start(args, fmt);
  75. vsprintf(buf, fmt, args);
  76. va_end(args);
  77. port_printf(ses, "ARACHNOS: %s", buf);
  78. }
  79. void arachnos_mudlist(struct session *ses, char *fmt, ...)
  80. {
  81. char buf[STRING_SIZE];
  82. va_list args;
  83. va_start(args, fmt);
  84. vsprintf(buf, fmt, args);
  85. va_end(args);
  86. port_printf(ses, "ARACHNOS: %s", buf);
  87. }
  88. void msdp_update_all(char *var, char *fmt, ...)
  89. {
  90. struct session *ses;
  91. struct port_data *buddy;
  92. char buf[STRING_SIZE];
  93. va_list args;
  94. va_start(args, fmt);
  95. vsprintf(buf, fmt, args);
  96. va_end(args);
  97. for (ses = gts->next ; ses ; ses = ses->next)
  98. {
  99. if (ses->port)
  100. {
  101. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  102. {
  103. msdp_update_var(ses, buddy, var, buf);
  104. }
  105. }
  106. }
  107. }
  108. // Update a variable and queue it if it's being reported.
  109. void msdp_update_index(struct session *ses, struct port_data *buddy, int index, char *str)
  110. {
  111. if (strcmp(buddy->msdp_data[index]->value, str))
  112. {
  113. if (HAS_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_REPORTED))
  114. {
  115. SET_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_UPDATED);
  116. SET_BIT(buddy->comm_flags, COMM_FLAG_MSDPUPDATE);
  117. }
  118. RESTRING(buddy->msdp_data[index]->value, str);
  119. }
  120. }
  121. void msdp_update_var(struct session *ses, struct port_data *buddy, char *var, char *str)
  122. {
  123. int index;
  124. index = msdp_find(var);
  125. if (index == -1)
  126. {
  127. port_printf(ses, "msdp_update_var: Unknown variable: %s.", var);
  128. return;
  129. }
  130. msdp_update_index(ses, buddy, index, str);
  131. }
  132. void msdp_update_varf(struct session *ses, struct port_data *buddy, char *var, char *fmt, ...)
  133. {
  134. char buf[STRING_SIZE];
  135. va_list args;
  136. va_start(args, fmt);
  137. vsprintf(buf, fmt, args);
  138. va_end(args);
  139. msdp_update_var(ses, buddy, var, buf);
  140. }
  141. // Update a variable and send it instantly.
  142. void msdp_update_var_instant(struct session *ses, struct port_data *buddy, char *var, char *fmt, ...)
  143. {
  144. char buf[STRING_SIZE];
  145. int index, length;
  146. va_list args;
  147. index = msdp_find(var);
  148. if (index == -1)
  149. {
  150. port_printf(ses, "msdp_update_var_instant: Unknown variable: %s.", var);
  151. return;
  152. }
  153. va_start(args, fmt);
  154. vsprintf(buf, fmt, args);
  155. va_end(args);
  156. if (strcmp(buddy->msdp_data[index]->value, buf))
  157. {
  158. RESTRING(buddy->msdp_data[index]->value, buf);
  159. }
  160. if (HAS_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_REPORTED))
  161. {
  162. length = sprintf(buf, "%c%c%c%c%s%c%s%c%c", IAC, SB, TELOPT_MSDP, MSDP_VAR, msdp_table[index].name, MSDP_VAL, buf, IAC, SE);
  163. write_msdp_to_descriptor(ses, buddy, buf, length);
  164. }
  165. }
  166. // Send all reported variables that have been updated.
  167. void msdp_send_update(struct session *ses, struct port_data *buddy)
  168. {
  169. char *ptr, buf[STRING_SIZE];
  170. int index;
  171. if (buddy->msdp_data == NULL)
  172. {
  173. return;
  174. }
  175. ptr = buf;
  176. ptr += sprintf(ptr, "%c%c%c", IAC, SB, TELOPT_MSDP);
  177. for (index = 0 ; index < gtd->msdp_table_size ; index++)
  178. {
  179. if (HAS_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_UPDATED))
  180. {
  181. ptr += sprintf(ptr, "%c%s%c%s", MSDP_VAR, msdp_table[index].name, MSDP_VAL, buddy->msdp_data[index]->value);
  182. DEL_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_UPDATED);
  183. }
  184. if (ptr - buf > STRING_SIZE - BUFFER_SIZE)
  185. {
  186. port_printf(ses, "msdp_send_update: MSDP BUFFER OVERFLOW");
  187. break;
  188. }
  189. }
  190. ptr += sprintf(ptr, "%c%c", IAC, SE);
  191. write_msdp_to_descriptor(ses, buddy, buf, ptr - buf);
  192. DEL_BIT(buddy->comm_flags, COMM_FLAG_MSDPUPDATE);
  193. }
  194. char *msdp_get_var(struct session *ses, struct port_data *buddy, char *var)
  195. {
  196. int index;
  197. index = msdp_find(var);
  198. if (index == -1)
  199. {
  200. port_printf(ses, "msdp_get_var: Unknown variable: %s.", var);
  201. return NULL;
  202. }
  203. return buddy->msdp_data[index]->value;
  204. }
  205. // 1d array support for commands
  206. void process_msdp_index_val(struct session *ses, struct port_data *buddy, int var_index, char *val )
  207. {
  208. int val_index;
  209. val_index = msdp_find(val);
  210. if (val_index >= 0)
  211. {
  212. if (msdp_table[var_index].fun)
  213. {
  214. msdp_table[var_index].fun(ses, buddy, val_index);
  215. }
  216. }
  217. }
  218. // 1d array support for commands
  219. void process_msdp_array(struct session *ses, struct port_data *buddy, int var_index, char *val)
  220. {
  221. char buf[BUFFER_SIZE], *pto, *pti;
  222. pti = val;
  223. pto = buf;
  224. while (*pti)
  225. {
  226. switch (*pti)
  227. {
  228. case MSDP_ARRAY_OPEN:
  229. break;
  230. case MSDP_VAL:
  231. *pto = 0;
  232. if (*buf)
  233. {
  234. process_msdp_index_val(ses, buddy, var_index, buf);
  235. }
  236. pto = buf;
  237. break;
  238. case MSDP_ARRAY_CLOSE:
  239. *pto = 0;
  240. if (*buf)
  241. {
  242. process_msdp_index_val(ses, buddy, var_index, buf);
  243. }
  244. return;
  245. default:
  246. *pto++ = *pti;
  247. break;
  248. }
  249. pti++;
  250. }
  251. *pto = 0;
  252. if (*buf)
  253. {
  254. process_msdp_index_val(ses, buddy, var_index, buf);
  255. }
  256. return;
  257. }
  258. void process_msdp_varval(struct session *ses, struct port_data *buddy, char *var, char *val )
  259. {
  260. int var_index, val_index;
  261. var_index = msdp_find(var);
  262. if (var_index == -1)
  263. {
  264. return;
  265. }
  266. if (HAS_BIT(msdp_table[var_index].flags, MSDP_FLAG_CONFIGURABLE))
  267. {
  268. RESTRING(buddy->msdp_data[var_index]->value, val);
  269. if (msdp_table[var_index].fun)
  270. {
  271. msdp_table[var_index].fun(ses, buddy, var_index);
  272. }
  273. return;
  274. }
  275. // Commands only take variables as arguments.
  276. if (HAS_BIT(msdp_table[var_index].flags, MSDP_FLAG_COMMAND))
  277. {
  278. if (*val == MSDP_ARRAY_OPEN)
  279. {
  280. port_printf(ses, "process_msdp_varval: array");
  281. process_msdp_array(ses, buddy, var_index, val);
  282. }
  283. else
  284. {
  285. val_index = msdp_find(val);
  286. if (val_index == -1)
  287. {
  288. return;
  289. }
  290. if (msdp_table[var_index].fun)
  291. {
  292. msdp_table[var_index].fun(ses, buddy, val_index);
  293. }
  294. }
  295. return;
  296. }
  297. }
  298. void msdp_command_list(struct session *ses, struct port_data *buddy, int index)
  299. {
  300. char *ptr, buf[STRING_SIZE];
  301. int flag;
  302. if (!HAS_BIT(msdp_table[index].flags, MSDP_FLAG_LIST))
  303. {
  304. return;
  305. }
  306. ptr = buf;
  307. flag = msdp_table[index].flags;
  308. ptr += sprintf(ptr, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_MSDP, MSDP_VAR, msdp_table[index].name, MSDP_VAL, MSDP_ARRAY_OPEN);
  309. for (index = 0 ; index < gtd->msdp_table_size ; index++)
  310. {
  311. if (flag != MSDP_FLAG_LIST)
  312. {
  313. if (HAS_BIT(buddy->msdp_data[index]->flags, flag) && !HAS_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_LIST))
  314. {
  315. ptr += sprintf(ptr, "%c%s", MSDP_VAL, msdp_table[index].name);
  316. }
  317. }
  318. else
  319. {
  320. if (HAS_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_LIST))
  321. {
  322. ptr += sprintf(ptr, "%c%s", MSDP_VAL, msdp_table[index].name);
  323. }
  324. }
  325. }
  326. ptr += sprintf(ptr, "%c%c%c", MSDP_ARRAY_CLOSE, IAC, SE);
  327. write_msdp_to_descriptor(ses, buddy, buf, ptr - buf);
  328. }
  329. void msdp_command_report(struct session *ses, struct port_data *buddy, int index)
  330. {
  331. port_printf(ses, "msdp_command_report(%s,%s,%d)", ses->name, buddy->name, index);
  332. if (!HAS_BIT(msdp_table[index].flags, MSDP_FLAG_REPORTABLE))
  333. {
  334. return;
  335. }
  336. SET_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_REPORTED);
  337. if (!HAS_BIT(msdp_table[index].flags, MSDP_FLAG_SENDABLE))
  338. {
  339. return;
  340. }
  341. SET_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_UPDATED);
  342. SET_BIT(buddy->comm_flags, COMM_FLAG_MSDPUPDATE);
  343. }
  344. void msdp_command_reset(struct session *ses, struct port_data *buddy, int index)
  345. {
  346. int flag;
  347. if (!HAS_BIT(msdp_table[index].flags, MSDP_FLAG_LIST))
  348. {
  349. return;
  350. }
  351. flag = msdp_table[index].flags &= ~MSDP_FLAG_LIST;
  352. for (index = 0 ; index < gtd->msdp_table_size ; index++)
  353. {
  354. if (HAS_BIT(buddy->msdp_data[index]->flags, flag))
  355. {
  356. buddy->msdp_data[index]->flags = msdp_table[index].flags;
  357. }
  358. }
  359. }
  360. void msdp_command_send(struct session *ses, struct port_data *buddy, int index)
  361. {
  362. if (HAS_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_SENDABLE))
  363. {
  364. SET_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_UPDATED);
  365. SET_BIT(buddy->comm_flags, COMM_FLAG_MSDPUPDATE);
  366. }
  367. }
  368. void msdp_command_unreport(struct session *ses, struct port_data *buddy, int index)
  369. {
  370. if (!HAS_BIT(msdp_table[index].flags, MSDP_FLAG_REPORTABLE))
  371. {
  372. return;
  373. }
  374. DEL_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_REPORTED);
  375. }
  376. // Arachnos Map support
  377. void msdp_configure_map(struct session *ses, struct port_data *buddy, int index)
  378. {
  379. char val[BUFFER_SIZE];
  380. struct room_data *room;
  381. struct exit_data *exit;
  382. int vnum;
  383. if (ses->map == NULL || *buddy->msdp_data[index]->value == 0)
  384. {
  385. return;
  386. }
  387. if (!strcmp(msdp_table[index].name, "MAP_ROOM_INFO"))
  388. {
  389. vnum = atoi(buddy->msdp_data[index]->value);
  390. port_printf(ses, "MAP_ROOM_INFO: %d", vnum);
  391. if (vnum <= 0 || vnum >= ses->map->size)
  392. {
  393. return;
  394. }
  395. room = ses->map->room_list[vnum];
  396. if (room == NULL)
  397. {
  398. return;
  399. }
  400. sprintf(val, "ROOM\002%c", MSDP_TABLE_OPEN);
  401. cat_sprintf(val, "\001AREA\002%s", room->area);
  402. cat_sprintf(val, "\001COLOR\002%s", room->color);
  403. cat_sprintf(val, "\001DESC\002%s", room->desc);
  404. cat_sprintf(val, "\001EXITS\002%c", MSDP_TABLE_OPEN);
  405. for (exit = room->f_exit ; exit ; exit = exit->next)
  406. {
  407. cat_sprintf(val, "\001%s\002", exit->name);
  408. cat_sprintf(val, "%c", MSDP_TABLE_OPEN);
  409. cat_sprintf(val, "\001COMMAND\002%s", exit->cmd);
  410. cat_sprintf(val, "\001FLAGS\002%d", exit->flags);
  411. cat_sprintf(val, "\001NAME\002%s", exit->name);
  412. cat_sprintf(val, "\001VNUM\002%d", exit->vnum);
  413. cat_sprintf(val, "\001WEIGHT\002%.3f", exit->weight);
  414. cat_sprintf(val, "%c", MSDP_TABLE_CLOSE);
  415. }
  416. cat_sprintf(val, "%c", MSDP_TABLE_CLOSE);
  417. cat_sprintf(val, "\001FLAGS\002%d", room->flags);
  418. cat_sprintf(val, "\001NAME\002%s", room->name);
  419. cat_sprintf(val, "\001NOTE\002%s", room->note);
  420. cat_sprintf(val, "\001SYMBOL\002%s", room->symbol);
  421. cat_sprintf(val, "\001TERRAIN\002%s", room->terrain);
  422. cat_sprintf(val, "\001VNUM\002%d", room->vnum);
  423. cat_sprintf(val, "\001WEIGHT\002%.3f", room->weight);
  424. cat_sprintf(val, "%c", MSDP_TABLE_CLOSE);
  425. msdp_update_index(ses, buddy, index, val);
  426. SET_BIT(buddy->msdp_data[index]->flags, MSDP_FLAG_UPDATED);
  427. SET_BIT(buddy->comm_flags, COMM_FLAG_MSDPUPDATE);
  428. }
  429. else if (!strcmp(msdp_table[index].name, "MAP_ROOM_GOTO"))
  430. {
  431. // to be imped
  432. }
  433. }
  434. // Arachnos Intermud support
  435. void msdp_configure_arachnos(struct session *ses, struct port_data *buddy, int index)
  436. {
  437. char var[BUFFER_SIZE], val[BUFFER_SIZE];
  438. char mud_name[BUFFER_SIZE], mud_host[BUFFER_SIZE], mud_port[BUFFER_SIZE];
  439. char msg_user[BUFFER_SIZE], msg_time[BUFFER_SIZE], msg_body[BUFFER_SIZE];
  440. char mud_players[BUFFER_SIZE], mud_uptime[BUFFER_SIZE], mud_update[BUFFER_SIZE];
  441. char *pti, *pto;
  442. struct tm timeval_tm;
  443. time_t timeval_t;
  444. var[0] = val[0] = mud_name[0] = mud_host[0] = mud_port[0] = msg_user[0] = msg_time[0] = msg_body[0] = mud_players[0] = mud_uptime[0] = mud_update[0] = 0;
  445. pti = buddy->msdp_data[index]->value;
  446. while (*pti)
  447. {
  448. switch (*pti)
  449. {
  450. case MSDP_VAR:
  451. pti++;
  452. pto = var;
  453. while (*pti > MSDP_ARRAY_CLOSE)
  454. {
  455. *pto++ = *pti++;
  456. }
  457. *pto = 0;
  458. break;
  459. case MSDP_VAL:
  460. pti++;
  461. pto = val;
  462. while (*pti > MSDP_ARRAY_CLOSE)
  463. {
  464. *pto++ = *pti++;
  465. }
  466. *pto = 0;
  467. if (!strcmp(var, "MUD_NAME"))
  468. {
  469. strcpy(mud_name, val);
  470. }
  471. else if (!strcmp(var, "MUD_HOST"))
  472. {
  473. strcpy(mud_host, val);
  474. }
  475. else if (!strcmp(var, "MUD_PORT"))
  476. {
  477. strcpy(mud_port, val);
  478. }
  479. else if (!strcmp(var, "MSG_USER"))
  480. {
  481. strcpy(msg_user, val);
  482. }
  483. else if (!strcmp(var, "MSG_TIME"))
  484. {
  485. timeval_t = (time_t) atoll(val);
  486. timeval_tm = *localtime(&timeval_t);
  487. strftime(msg_time, 20, "%T %D", &timeval_tm);
  488. }
  489. else if (!strcmp(var, "MSG_BODY"))
  490. {
  491. strcpy(msg_body, val);
  492. }
  493. else if (!strcmp(var, "MUD_UPTIME"))
  494. {
  495. timeval_t = (time_t) atoll(val);
  496. timeval_tm = *localtime(&timeval_t);
  497. strftime(mud_uptime, 20, "%T %D", &timeval_tm);
  498. }
  499. else if (!strcmp(var, "MUD_UPDATE"))
  500. {
  501. timeval_t = (time_t) atoll(val);
  502. timeval_tm = *localtime(&timeval_t);
  503. strftime(mud_update, 20, "%T %D", &timeval_tm);
  504. }
  505. else if (!strcmp(var, "MUD_PLAYERS"))
  506. {
  507. strcpy(mud_players, val);
  508. }
  509. break;
  510. default:
  511. pti++;
  512. break;
  513. }
  514. }
  515. if (*mud_name && *mud_host && *mud_port)
  516. {
  517. if (!strcmp(msdp_table[index].name, "ARACHNOS_DEVEL"))
  518. {
  519. if (*msg_user && *msg_time && *msg_body)
  520. {
  521. arachnos_devel(ses, "%s %s@%s:%s devtalks: %s", msg_time, msg_user, mud_host, mud_port, msg_body);
  522. }
  523. }
  524. else if (!strcmp(msdp_table[index].name, "ARACHNOS_MUDLIST"))
  525. {
  526. if (*mud_uptime && *mud_update && *mud_players)
  527. {
  528. arachnos_mudlist(ses, "%18.18s %14.14s %5.5s %17.17s %17.17s %4.4s", mud_name, mud_host, mud_port, mud_update, mud_uptime, mud_players);
  529. }
  530. }
  531. }
  532. }
  533. struct msdp_type msdp_table[] =
  534. {
  535. { "ARACHNOS_DEVEL", MSDP_FLAG_CONFIGURABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SCOUT, msdp_configure_arachnos },
  536. { "ARACHNOS_MUDLIST", MSDP_FLAG_CONFIGURABLE, PORT_RANK_SCOUT, msdp_configure_arachnos },
  537. { "COMMANDS", MSDP_FLAG_COMMAND|MSDP_FLAG_LIST, PORT_RANK_SPY, NULL },
  538. { "CONFIGURABLE_VARIABLES", MSDP_FLAG_CONFIGURABLE|MSDP_FLAG_LIST, PORT_RANK_SPY, NULL },
  539. { "LIST", MSDP_FLAG_COMMAND, PORT_RANK_SPY, msdp_command_list },
  540. { "LISTS", MSDP_FLAG_LIST, PORT_RANK_SPY, NULL },
  541. { "MAP_ROOM_INFO", MSDP_FLAG_CONFIGURABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SCOUT, msdp_configure_map },
  542. { "REPORT", MSDP_FLAG_COMMAND, PORT_RANK_SPY, msdp_command_report },
  543. { "REPORTABLE_VARIABLES", MSDP_FLAG_REPORTABLE|MSDP_FLAG_LIST, PORT_RANK_SPY, NULL },
  544. { "REPORTED_VARIABLES", MSDP_FLAG_REPORTED|MSDP_FLAG_LIST, PORT_RANK_SPY, NULL },
  545. { "RESET", MSDP_FLAG_COMMAND, PORT_RANK_SPY, msdp_command_reset },
  546. { "SCREEN_CHARACTER_HEIGHT", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  547. { "SCREEN_CHARACTER_WIDTH", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  548. { "SCREEN_COLS", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  549. { "SCREEN_FOCUS", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  550. { "SCREEN_HEIGHT", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  551. { "SCREEN_MINIMIZED", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  552. { "SCREEN_POSITION_HEIGHT", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  553. { "SCREEN_POSITION_WIDTH", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  554. { "SCREEN_ROWS", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  555. { "SCREEN_WIDTH", MSDP_FLAG_SENDABLE|MSDP_FLAG_REPORTABLE, PORT_RANK_SPY, NULL },
  556. { "SEND", MSDP_FLAG_COMMAND, PORT_RANK_SPY, msdp_command_send },
  557. { "SENDABLE_VARIABLES", MSDP_FLAG_SENDABLE|MSDP_FLAG_LIST, PORT_RANK_SPY, NULL },
  558. { "SPECIFICATION", MSDP_FLAG_SENDABLE, PORT_RANK_SPY, NULL },
  559. { "UNREPORT", MSDP_FLAG_COMMAND, PORT_RANK_SPY, msdp_command_unreport },
  560. { "", 0, 0, NULL }
  561. };
  562. void write_msdp_to_descriptor(struct session *ses, struct port_data *buddy, char *src, int length)
  563. {
  564. char out[STRING_SIZE];
  565. if (!HAS_BIT(buddy->comm_flags, COMM_FLAG_GMCP))
  566. {
  567. port_telnet_printf(ses, buddy, length, "%s", src);
  568. }
  569. else
  570. {
  571. length = msdp2json((unsigned char *) src, length, out);
  572. port_telnet_printf(ses, buddy, length, "%s", out);
  573. }
  574. }
  575. int msdp2json(unsigned char *src, int srclen, char *out)
  576. {
  577. char *pto;
  578. int i, nest, last;
  579. nest = last = 0;
  580. pto = out;
  581. if (src[2] == TELOPT_MSDP)
  582. {
  583. pto += sprintf(pto, "%c%c%cMSDP {", IAC, SB, TELOPT_GMCP);
  584. }
  585. i = 3;
  586. while (i < srclen)
  587. {
  588. if (src[i] == IAC && src[i+1] == SE)
  589. {
  590. break;
  591. }
  592. switch (src[i])
  593. {
  594. case MSDP_TABLE_OPEN:
  595. *pto++ = '{';
  596. nest++;
  597. last = MSDP_TABLE_OPEN;
  598. break;
  599. case MSDP_TABLE_CLOSE:
  600. if (last == MSDP_VAL || last == MSDP_VAR)
  601. {
  602. *pto++ = '"';
  603. }
  604. if (nest)
  605. {
  606. nest--;
  607. }
  608. *pto++ = '}';
  609. last = MSDP_TABLE_CLOSE;
  610. break;
  611. case MSDP_ARRAY_OPEN:
  612. *pto++ = '[';
  613. nest++;
  614. last = MSDP_ARRAY_OPEN;
  615. break;
  616. case MSDP_ARRAY_CLOSE:
  617. if (last == MSDP_VAL || last == MSDP_VAR)
  618. {
  619. *pto++ = '"';
  620. }
  621. if (nest)
  622. {
  623. nest--;
  624. }
  625. *pto++ = ']';
  626. last = MSDP_ARRAY_CLOSE;
  627. break;
  628. case MSDP_VAR:
  629. if (last == MSDP_VAL || last == MSDP_VAR)
  630. {
  631. *pto++ = '"';
  632. }
  633. if (last == MSDP_VAL || last == MSDP_VAR || last == MSDP_TABLE_CLOSE || last == MSDP_ARRAY_CLOSE)
  634. {
  635. *pto++ = ',';
  636. }
  637. *pto++ = '"';
  638. last = MSDP_VAR;
  639. break;
  640. case MSDP_VAL:
  641. if (last == MSDP_VAR)
  642. {
  643. *pto++ = '"';
  644. *pto++ = ':';
  645. }
  646. if (last == MSDP_VAL)
  647. {
  648. *pto++ = '"';
  649. *pto++ = ',';
  650. }
  651. if (src[i+1] != MSDP_TABLE_OPEN && src[i+1] != MSDP_ARRAY_OPEN)
  652. {
  653. *pto++ = '"';
  654. }
  655. last = MSDP_VAL;
  656. break;
  657. case '\\':
  658. *pto++ = '\\';
  659. *pto++ = '\\';
  660. break;
  661. case '"':
  662. *pto++ = '\\';
  663. *pto++ = '"';
  664. break;
  665. default:
  666. *pto++ = src[i];
  667. break;
  668. }
  669. i++;
  670. }
  671. pto += sprintf(pto, "}%c%c", IAC, SE);
  672. return pto - out;
  673. }
  674. int json2msdp(unsigned char *src, int srclen, char *out)
  675. {
  676. char *pto;
  677. int i, nest, last, type, state[100];
  678. nest = last = 0;
  679. pto = out;
  680. if (src[2] == TELOPT_GMCP)
  681. {
  682. pto += sprintf(pto, "%c%c%c", IAC, SB, TELOPT_MSDP);
  683. }
  684. i = 3;
  685. if (!strncmp((char *) &src[3], "MSDP {", 6))
  686. {
  687. i += 6;
  688. }
  689. state[0] = nest = type = 0;
  690. while (i < srclen && src[i] != IAC && nest < 99)
  691. {
  692. switch (src[i])
  693. {
  694. case ' ':
  695. i++;
  696. break;
  697. case '{':
  698. *pto++ = MSDP_TABLE_OPEN;
  699. i++;
  700. state[++nest] = 0;
  701. break;
  702. case '}':
  703. nest--;
  704. i++;
  705. if (nest < 0)
  706. {
  707. pto += sprintf(pto, "%c%c", IAC, SE);
  708. return pto - out;
  709. }
  710. *pto++ = MSDP_TABLE_CLOSE;
  711. break;
  712. case '[':
  713. i++;
  714. state[++nest] = 1;
  715. *pto++ = MSDP_ARRAY_OPEN;
  716. break;
  717. case ']':
  718. nest--;
  719. i++;
  720. *pto++ = MSDP_ARRAY_CLOSE;
  721. break;
  722. case ':':
  723. *pto++ = MSDP_VAL;
  724. i++;
  725. break;
  726. case ',':
  727. i++;
  728. if (state[nest])
  729. {
  730. *pto++ = MSDP_VAL;
  731. }
  732. else
  733. {
  734. *pto++ = MSDP_VAR;
  735. }
  736. break;
  737. case '"':
  738. i++;
  739. if (last == 0)
  740. {
  741. last = MSDP_VAR;
  742. *pto++ = MSDP_VAR;
  743. }
  744. type = 1;
  745. while (i < srclen && src[i] != IAC && type)
  746. {
  747. switch (src[i])
  748. {
  749. case '\\':
  750. i++;
  751. if (i < srclen && src[i] == '"')
  752. {
  753. *pto++ = src[i++];
  754. }
  755. else
  756. {
  757. *pto++ = '\\';
  758. }
  759. break;
  760. case '"':
  761. i++;
  762. type = 0;
  763. break;
  764. default:
  765. *pto++ = src[i++];
  766. break;
  767. }
  768. }
  769. break;
  770. default:
  771. type = 1;
  772. while (i < srclen && src[i] != IAC && type)
  773. {
  774. switch (src[i])
  775. {
  776. case '}':
  777. case ']':
  778. case ',':
  779. case ':':
  780. type = 0;
  781. break;
  782. case ' ':
  783. i++;
  784. break;
  785. default:
  786. *pto++ = src[i++];
  787. break;
  788. }
  789. }
  790. break;
  791. }
  792. }
  793. pto += sprintf(pto, "%c%c", IAC, SE);
  794. return pto - out;
  795. }