msdp.c 22 KB

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