userent.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * userent.c -- handles:
  3. * user-entry handling, new stylem more versatile.
  4. *
  5. */
  6. #include "common.h"
  7. #include "users.h"
  8. #include "src/mod/share.mod/share.h"
  9. #include "misc.h"
  10. #include "main.h"
  11. #include "debug.h"
  12. #include "userrec.h"
  13. #include "match.h"
  14. #include "dccutil.h"
  15. #include "crypt.h"
  16. #include "botmsg.h"
  17. static struct user_entry_type *entry_type_list;
  18. void init_userent()
  19. {
  20. entry_type_list = 0;
  21. add_entry_type(&USERENTRY_COMMENT);
  22. add_entry_type(&USERENTRY_INFO);
  23. add_entry_type(&USERENTRY_LASTON);
  24. add_entry_type(&USERENTRY_BOTADDR);
  25. add_entry_type(&USERENTRY_OS);
  26. add_entry_type(&USERENTRY_NODENAME);
  27. add_entry_type(&USERENTRY_USERNAME);
  28. add_entry_type(&USERENTRY_PASS);
  29. add_entry_type(&USERENTRY_SECPASS);
  30. add_entry_type(&USERENTRY_HOSTS);
  31. add_entry_type(&USERENTRY_STATS);
  32. add_entry_type(&USERENTRY_ADDED);
  33. add_entry_type(&USERENTRY_MODIFIED);
  34. add_entry_type(&USERENTRY_CONFIG);
  35. }
  36. void list_type_kill(struct list_type *t)
  37. {
  38. struct list_type *u = NULL;
  39. while (t) {
  40. u = t->next;
  41. if (t->extra)
  42. free(t->extra);
  43. free(t);
  44. t = u;
  45. }
  46. }
  47. int def_unpack(struct userrec *u, struct user_entry *e)
  48. {
  49. char *tmp = NULL;
  50. tmp = e->u.list->extra;
  51. e->u.list->extra = NULL;
  52. list_type_kill(e->u.list);
  53. e->u.string = tmp;
  54. return 1;
  55. }
  56. int def_kill(struct user_entry *e)
  57. {
  58. free(e->u.string);
  59. free(e);
  60. return 1;
  61. }
  62. #ifdef HUB
  63. int def_write_userfile(FILE * f, struct userrec *u, struct user_entry *e)
  64. {
  65. if (lfprintf(f, "--%s %s\n", e->type->name, e->u.string) == EOF)
  66. return 0;
  67. return 1;
  68. }
  69. #endif /* HUB */
  70. void *def_get(struct userrec *u, struct user_entry *e)
  71. {
  72. return e->u.string;
  73. }
  74. int def_set(struct userrec *u, struct user_entry *e, void *buf)
  75. {
  76. char *string = (char *) buf;
  77. if (string && !string[0])
  78. string = NULL;
  79. if (!string && !e->u.string)
  80. return 1;
  81. if (string) {
  82. int l = strlen (string);
  83. char *i;
  84. if (l > 160)
  85. l = 160;
  86. e->u.string = realloc (e->u.string, l + 1);
  87. strncpyz (e->u.string, string, l + 1);
  88. for (i = e->u.string; *i; i++)
  89. /* Allow bold, inverse, underline, color text here...
  90. * But never add cr or lf!! --rtc
  91. */
  92. if ((unsigned int) *i < 32 && !strchr ("\002\003\026\037", *i))
  93. *i = '?';
  94. } else { /* string == NULL && e->u.string != NULL */
  95. free(e->u.string);
  96. e->u.string = NULL;
  97. }
  98. if (!noshare) {
  99. shareout(NULL, "c %s %s %s\n", e->type->name, u->handle, e->u.string ? e->u.string : "");
  100. }
  101. return 1;
  102. }
  103. int def_gotshare(struct userrec *u, struct user_entry *e, char *data, int idx)
  104. {
  105. #ifdef HUB
  106. putlog(LOG_DEBUG, "@", "%s: change %s %s", dcc[idx].nick, e->type->name, u->handle);
  107. #endif
  108. return e->type->set(u, e, data);
  109. }
  110. void def_display(int idx, struct user_entry *e, struct userrec *u)
  111. {
  112. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  113. }
  114. static void comment_display(int idx, struct user_entry *e, struct userrec *u)
  115. {
  116. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER))
  117. dprintf(idx, " COMMENT: %s\n", e->u.string);
  118. }
  119. struct user_entry_type USERENTRY_COMMENT =
  120. {
  121. 0, /* always 0 ;) */
  122. def_gotshare,
  123. def_unpack,
  124. #ifdef HUB
  125. def_write_userfile,
  126. #endif /* HUB */
  127. def_kill,
  128. def_get,
  129. def_set,
  130. comment_display,
  131. "COMMENT"
  132. };
  133. struct user_entry_type USERENTRY_INFO =
  134. {
  135. 0, /* always 0 ;) */
  136. def_gotshare,
  137. def_unpack,
  138. #ifdef HUB
  139. def_write_userfile,
  140. #endif /* HUB */
  141. def_kill,
  142. def_get,
  143. def_set,
  144. def_display,
  145. "INFO"
  146. };
  147. void added_display(int idx, struct user_entry *e, struct userrec *u)
  148. {
  149. /* format: unixtime handle */
  150. if (dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  151. char tmp[30] = "", tmp2[70] = "", *hnd = NULL;
  152. time_t tm;
  153. strncpyz(tmp, e->u.string, sizeof(tmp));
  154. hnd = strchr(tmp, ' ');
  155. if (hnd)
  156. *hnd++ = 0;
  157. tm = atoi(tmp);
  158. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  159. if (hnd)
  160. dprintf(idx, " -- Added %s by %s\n", tmp2, hnd);
  161. else
  162. dprintf(idx, " -- Added %s\n", tmp2);
  163. }
  164. }
  165. struct user_entry_type USERENTRY_ADDED = {
  166. 0, /* always 0 ;) */
  167. def_gotshare,
  168. def_unpack,
  169. #ifdef HUB
  170. def_write_userfile,
  171. #endif /* HUB */
  172. def_kill,
  173. def_get,
  174. def_set,
  175. added_display,
  176. "ADDED"
  177. };
  178. int config_set(struct userrec *u, struct user_entry *e, void *buf)
  179. {
  180. struct xtra_key *curr = NULL, *old = NULL, *new = buf;
  181. for (curr = e->u.extra; curr; curr = curr->next) {
  182. if (curr->key && !egg_strcasecmp(curr->key, new->key)) {
  183. old = curr;
  184. break;
  185. }
  186. }
  187. if (!old && (!new->data || !new->data[0])) {
  188. /* delete non-existant entry */
  189. free(new->key);
  190. if (new->data)
  191. free(new->data);
  192. free(new);
  193. return 1;
  194. }
  195. /* we will possibly free new below, so let's send the information
  196. * to the botnet now */
  197. if (!noshare && !cfg_noshare)
  198. shareout(NULL, "c CONFIG %s %s %s\n", u->handle, new->key, new->data ? new->data : "");
  199. if ((old && old != new) || !new->data || !new->data[0]) {
  200. list_delete((struct list_type **) (&e->u.extra), (struct list_type *) old);
  201. free(old->key);
  202. free(old->data);
  203. free(old);
  204. }
  205. if (old != new && new->data) {
  206. if (new->data[0]) {
  207. list_insert((&e->u.extra), new);
  208. } else {
  209. if (new->data)
  210. free(new->data);
  211. free(new->key);
  212. free(new);
  213. }
  214. }
  215. return 1;
  216. }
  217. int config_unpack(struct userrec *u, struct user_entry *e)
  218. {
  219. struct list_type *curr = NULL, *head = NULL;
  220. struct xtra_key *t = NULL;
  221. char *key = NULL, *data = NULL;
  222. head = curr = e->u.list;
  223. e->u.extra = NULL;
  224. while (curr) {
  225. t = calloc(1, sizeof(struct xtra_key));
  226. data = curr->extra;
  227. key = newsplit(&data);
  228. if (data[0]) {
  229. t->key = strdup(key);
  230. t->data = strdup(data);
  231. list_insert((&e->u.extra), t);
  232. }
  233. curr = curr->next;
  234. }
  235. list_type_kill(head);
  236. return 1;
  237. }
  238. void config_display(int idx, struct user_entry *e, struct userrec *u)
  239. {
  240. #ifdef HUB
  241. struct xtra_key *xk = NULL;
  242. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  243. get_user_flagrec(dcc[idx].user, &fr, NULL);
  244. /* scan thru xtra field, searching for matches */
  245. for (xk = e->u.extra; xk; xk = xk->next) {
  246. /* ok, it's a valid xtra field entry */
  247. if (glob_owner(fr))
  248. dprintf(idx, " %s: %s\n", xk->key, xk->data);
  249. }
  250. #endif /* HUB */
  251. }
  252. int config_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  253. {
  254. char *arg = NULL;
  255. struct xtra_key *xk = NULL;
  256. int l;
  257. arg = newsplit(&buf);
  258. if (!arg[0])
  259. return 1;
  260. if (!strcmp(u->handle, conf.bot->nick)) {
  261. struct cfg_entry *cfgent = NULL;
  262. int i;
  263. cfg_noshare = 1;
  264. for (i = 0; !cfgent && (i < cfg_count); i++)
  265. if (!strcmp(arg, cfg[i]->name) && (cfg[i]->flags & CFGF_LOCAL))
  266. cfgent = cfg[i];
  267. if (cfgent) {
  268. set_cfg_str(conf.bot->nick, cfgent->name, (buf && buf[0]) ? buf : NULL);
  269. }
  270. cfg_noshare = 0;
  271. return 1;
  272. }
  273. xk = calloc(1, sizeof(struct xtra_key));
  274. l = strlen(arg);
  275. if (l > 1500)
  276. l = 1500;
  277. xk->key = calloc(1, l + 1);
  278. strncpy(xk->key, arg, l + 1);
  279. if (buf && buf[0]) {
  280. int k = strlen(buf);
  281. if (k > 1500 - l)
  282. k = 1500 - l;
  283. xk->data = calloc(1, k + 1);
  284. strncpy(xk->data, buf, k + 1);
  285. }
  286. config_set(u, e, xk);
  287. return 1;
  288. }
  289. #ifdef HUB
  290. int config_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  291. {
  292. struct xtra_key *x = NULL;
  293. for (x = e->u.extra; x; x = x->next)
  294. lfprintf(f, "--CONFIG %s %s\n", x->key, x->data);
  295. return 1;
  296. }
  297. #endif /* HUB */
  298. int config_kill(struct user_entry *e)
  299. {
  300. struct xtra_key *x = NULL, *y = NULL;
  301. for (x = e->u.extra; x; x = y) {
  302. y = x->next;
  303. free(x->key);
  304. free(x->data);
  305. free(x);
  306. }
  307. free(e);
  308. return 1;
  309. }
  310. struct user_entry_type USERENTRY_CONFIG = {
  311. 0,
  312. config_gotshare,
  313. config_unpack,
  314. #ifdef HUB
  315. config_write_userfile,
  316. #endif /* HUB */
  317. config_kill,
  318. def_get,
  319. config_set,
  320. config_display,
  321. "CONFIG"
  322. };
  323. #ifdef HUB
  324. static void botmisc_display(int idx, struct user_entry *e, struct userrec *u)
  325. {
  326. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  327. get_user_flagrec(dcc[idx].user, &fr, NULL);
  328. if (glob_admin(fr))
  329. dprintf(idx, " %s: %s\n", e->type->name, e->u.string ? e->u.string : "");
  330. }
  331. #endif /* HUB */
  332. struct user_entry_type USERENTRY_USERNAME = {
  333. 0,
  334. def_gotshare,
  335. def_unpack,
  336. #ifdef HUB
  337. def_write_userfile,
  338. #endif /* HUB */
  339. def_kill,
  340. def_get,
  341. def_set,
  342. #ifdef HUB
  343. botmisc_display,
  344. #else
  345. NULL,
  346. #endif /* HUB */
  347. "USERNAME"
  348. };
  349. struct user_entry_type USERENTRY_NODENAME = {
  350. 0,
  351. def_gotshare,
  352. def_unpack,
  353. #ifdef HUB
  354. def_write_userfile,
  355. #endif /* HUB */
  356. def_kill,
  357. def_get,
  358. def_set,
  359. #ifdef HUB
  360. botmisc_display,
  361. #else
  362. NULL,
  363. #endif /* HUB */
  364. "NODENAME"
  365. };
  366. struct user_entry_type USERENTRY_OS = {
  367. 0,
  368. def_gotshare,
  369. def_unpack,
  370. #ifdef HUB
  371. def_write_userfile,
  372. #endif /* HUB */
  373. def_kill,
  374. def_get,
  375. def_set,
  376. #ifdef HUB
  377. botmisc_display,
  378. #else
  379. NULL,
  380. #endif /* HUB */
  381. "OS"
  382. };
  383. void stats_add(struct userrec *u, int login, int op)
  384. {
  385. char *s = NULL, s2[50] = "";
  386. int sl, so;
  387. if (!u)
  388. return;
  389. s = get_user(&USERENTRY_STATS, u);
  390. if (s) {
  391. strncpyz(s2, s, sizeof(s2));
  392. } else
  393. strcpy(s2, "0 0");
  394. s = strchr(s2, ' ');
  395. if (s) {
  396. s++;
  397. so = atoi(s);
  398. } else
  399. so = 0;
  400. sl = atoi(s2);
  401. if (login)
  402. sl++;
  403. if (op)
  404. so++;
  405. sprintf(s2, "%i %i", sl, so);
  406. set_user(&USERENTRY_STATS, u, s2);
  407. }
  408. void stats_display(int idx, struct user_entry *e, struct userrec *u)
  409. {
  410. /* format: logincount opcount */
  411. if (dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  412. char *p;
  413. p = strchr(e->u.string, ' ');
  414. if (p)
  415. dprintf(idx, " -- %i logins, %i ops\n", atoi(e->u.string), atoi(p));
  416. }
  417. }
  418. struct user_entry_type USERENTRY_STATS = {
  419. 0, /* always 0 ;) */
  420. def_gotshare,
  421. def_unpack,
  422. #ifdef HUB
  423. def_write_userfile,
  424. #endif /* HUB */
  425. def_kill,
  426. def_get,
  427. def_set,
  428. stats_display,
  429. "STATS"
  430. };
  431. void update_mod(char *handle, char *nick, char *cmd, char *par)
  432. {
  433. char tmp[100] = "";
  434. egg_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", now, nick, cmd, (par && par[0]) ? par : "");
  435. set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
  436. }
  437. void modified_display(int idx, struct user_entry *e, struct userrec *u)
  438. {
  439. if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  440. char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
  441. time_t tm;
  442. strncpyz(tmp, e->u.string, sizeof(tmp));
  443. hnd = strchr(tmp, ' ');
  444. if (hnd)
  445. *hnd++ = 0;
  446. tm = atoi(tmp);
  447. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  448. if (hnd)
  449. dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
  450. else
  451. dprintf(idx, " -- Modified %s\n", tmp2);
  452. }
  453. }
  454. struct user_entry_type USERENTRY_MODIFIED =
  455. {
  456. 0,
  457. def_gotshare,
  458. def_unpack,
  459. #ifdef HUB
  460. def_write_userfile,
  461. #endif /* HUB */
  462. def_kill,
  463. def_get,
  464. def_set,
  465. modified_display,
  466. "MODIFIED"
  467. };
  468. int pass_set(struct userrec *u, struct user_entry *e, void *buf)
  469. {
  470. char new[32] = "";
  471. register char *pass = buf;
  472. if (e->u.extra)
  473. free(e->u.extra);
  474. if (!pass || !pass[0] || (pass[0] == '-'))
  475. e->u.extra = NULL;
  476. else {
  477. unsigned char *p = (unsigned char *) pass;
  478. if (strlen(pass) > 15)
  479. pass[15] = 0;
  480. while (*p) {
  481. if ((*p <= 32) || (*p == 127))
  482. *p = '?';
  483. p++;
  484. }
  485. if (u->bot || (pass[0] == '+'))
  486. strcpy(new, pass);
  487. else
  488. encrypt_pass(pass, new);
  489. e->u.extra = strdup(new);
  490. }
  491. if (!noshare)
  492. shareout(NULL, "c PASS %s %s\n", u->handle, pass ? pass : "");
  493. return 1;
  494. }
  495. struct user_entry_type USERENTRY_PASS =
  496. {
  497. 0,
  498. def_gotshare,
  499. def_unpack,
  500. #ifdef HUB
  501. def_write_userfile,
  502. #endif /* HUB */
  503. def_kill,
  504. def_get,
  505. pass_set,
  506. NULL,
  507. "PASS"
  508. };
  509. void secpass_display(int idx, struct user_entry *e, struct userrec *u)
  510. {
  511. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  512. get_user_flagrec(dcc[idx].user, &fr, NULL);
  513. if (!strcmp(u->handle, dcc[idx].nick) || glob_admin(fr)) {
  514. #ifdef HUB
  515. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  516. #else
  517. dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
  518. if (dcc[idx].u.chat->su_nick)
  519. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  520. dprintf(idx, "\n");
  521. #endif /* HUB */
  522. }
  523. }
  524. struct user_entry_type USERENTRY_SECPASS =
  525. {
  526. 0,
  527. def_gotshare,
  528. def_unpack,
  529. #ifdef HUB
  530. def_write_userfile,
  531. #endif /* HUB */
  532. def_kill,
  533. def_get,
  534. def_set,
  535. secpass_display,
  536. "SECPASS"
  537. };
  538. static int laston_unpack(struct userrec *u, struct user_entry *e)
  539. {
  540. char *par = NULL, *arg = NULL;
  541. struct laston_info *li = NULL;
  542. par = e->u.list->extra;
  543. arg = newsplit (&par);
  544. if (!par[0])
  545. par = "???";
  546. li = calloc(1, sizeof(struct laston_info));
  547. li->laston = atoi(arg);
  548. li->lastonplace = strdup(par);
  549. list_type_kill(e->u.list);
  550. e->u.extra = li;
  551. return 1;
  552. }
  553. #ifdef HUB
  554. static int laston_write_userfile(FILE * f, struct userrec *u, struct user_entry *e)
  555. {
  556. struct laston_info *li = (struct laston_info *) e->u.extra;
  557. if (lfprintf(f, "--LASTON %lu %s\n", li->laston,
  558. li->lastonplace ? li->lastonplace : "") == EOF)
  559. return 0;
  560. return 1;
  561. }
  562. #endif /* HUB */
  563. static int laston_kill(struct user_entry *e)
  564. {
  565. if (((struct laston_info *) (e->u.extra))->lastonplace)
  566. free(((struct laston_info *) (e->u.extra))->lastonplace);
  567. free(e->u.extra);
  568. free(e);
  569. return 1;
  570. }
  571. static int laston_set(struct userrec *u, struct user_entry *e, void *buf)
  572. {
  573. struct laston_info *li = (struct laston_info *) e->u.extra;
  574. if (li != buf) {
  575. if (li) {
  576. free(li->lastonplace);
  577. free(li);
  578. }
  579. li = e->u.extra = buf;
  580. }
  581. /* donut share laston info */
  582. return 1;
  583. }
  584. struct user_entry_type USERENTRY_LASTON =
  585. {
  586. 0, /* always 0 ;) */
  587. 0,
  588. laston_unpack,
  589. #ifdef HUB
  590. laston_write_userfile,
  591. #endif /* HUB */
  592. laston_kill,
  593. def_get,
  594. laston_set,
  595. 0,
  596. "LASTON"
  597. };
  598. static int botaddr_unpack(struct userrec *u, struct user_entry *e)
  599. {
  600. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  601. struct bot_addr *bi = NULL;
  602. bi = calloc(1, sizeof(struct bot_addr));
  603. /* address:port/port:hublevel:uplink */
  604. Context;
  605. Assert(e);
  606. Assert(e->name);
  607. strcpy(p, e->u.list->extra);
  608. q1 = strchr(p, ':');
  609. if (q1)
  610. *q1++ = 0;
  611. bi->address = strdup(p);
  612. if (q1) {
  613. q2 = strchr(q1, ':');
  614. if (q2)
  615. *q2++ = 0;
  616. bi->telnet_port = atoi(q1);
  617. q1 = strchr(q1, '/');
  618. if (q1) {
  619. q1++;
  620. bi->relay_port = atoi(q1);
  621. }
  622. if (q2) {
  623. q1 = strchr(q2, ':');
  624. if (q1) {
  625. *q1++ = 0;
  626. bi->uplink = strdup(q1);
  627. }
  628. bi->hublevel = atoi(q2);
  629. }
  630. }
  631. if (!bi->telnet_port)
  632. bi->telnet_port = 3333;
  633. if (!bi->relay_port)
  634. bi->relay_port = bi->telnet_port;
  635. if (!bi->uplink) {
  636. bi->uplink = calloc(1, 1);
  637. }
  638. list_type_kill(e->u.list);
  639. e->u.extra = bi;
  640. return 1;
  641. }
  642. static int botaddr_kill(struct user_entry *e)
  643. {
  644. free(((struct bot_addr *) (e->u.extra))->address);
  645. free(((struct bot_addr *) (e->u.extra))->uplink);
  646. free(e->u.extra);
  647. free(e);
  648. return 1;
  649. }
  650. #ifdef HUB
  651. static int botaddr_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  652. {
  653. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  654. if (lfprintf(f, "--%s %s:%u/%u:%u:%s\n", e->type->name, bi->address,
  655. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink) == EOF)
  656. return 0;
  657. return 1;
  658. }
  659. #endif /* HUB */
  660. static int botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
  661. {
  662. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  663. Context;
  664. if (!bi && !buf)
  665. return 1;
  666. if (bi != buf) {
  667. if (bi) {
  668. Assert(bi->address);
  669. free(bi->address);
  670. Assert(bi->uplink);
  671. free(bi->uplink);
  672. free(bi);
  673. }
  674. ContextNote("(sharebug) occurred in botaddr_set");
  675. bi = e->u.extra = buf;
  676. }
  677. Assert(u);
  678. if (bi && !noshare) {
  679. shareout(NULL, "c BOTADDR %s %s %d %d %d %s\n",u->handle,
  680. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  681. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  682. }
  683. return 1;
  684. }
  685. #ifdef HUB
  686. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  687. {
  688. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  689. get_user_flagrec(dcc[idx].user, &fr, NULL);
  690. if (glob_admin(fr)) {
  691. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  692. if (bi->address && bi->hublevel && bi->hublevel != 0) {
  693. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  694. dprintf(idx, " port: %d\n", bi->telnet_port);
  695. }
  696. if (bi->hublevel && bi->hublevel != 0)
  697. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  698. if (bi->uplink && bi->uplink[0])
  699. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  700. }
  701. }
  702. #endif /* HUB */
  703. static int botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  704. {
  705. struct bot_addr *bi = NULL;
  706. char *arg = NULL;
  707. bi = calloc(1, sizeof(struct bot_addr));
  708. arg = newsplit(&buf);
  709. bi->address = strdup(arg);
  710. arg = newsplit(&buf);
  711. bi->telnet_port = atoi(arg);
  712. arg = newsplit(&buf);
  713. bi->relay_port = atoi(arg);
  714. arg = newsplit(&buf);
  715. bi->hublevel = atoi(arg);
  716. bi->uplink = strdup(buf);
  717. if (!bi->telnet_port)
  718. bi->telnet_port = 3333;
  719. if (!bi->relay_port)
  720. bi->relay_port = bi->telnet_port;
  721. return botaddr_set(u, e, bi);
  722. }
  723. struct user_entry_type USERENTRY_BOTADDR =
  724. {
  725. 0, /* always 0 ;) */
  726. botaddr_gotshare,
  727. botaddr_unpack,
  728. #ifdef HUB
  729. botaddr_write_userfile,
  730. #endif /* HUB */
  731. botaddr_kill,
  732. def_get,
  733. botaddr_set,
  734. #ifdef HUB
  735. botaddr_display,
  736. #else
  737. NULL,
  738. #endif /* HUB */
  739. "BOTADDR"
  740. };
  741. #ifdef HUB
  742. static int hosts_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  743. {
  744. struct list_type *h = NULL;
  745. for (h = e->u.extra; h; h = h->next)
  746. if (lfprintf(f, "--HOSTS %s\n", h->extra) == EOF)
  747. return 0;
  748. return 1;
  749. }
  750. #endif /* HUB */
  751. static int hosts_null(struct userrec *u, struct user_entry *e)
  752. {
  753. return 1;
  754. }
  755. static int hosts_kill(struct user_entry *e)
  756. {
  757. list_type_kill(e->u.list);
  758. free(e);
  759. return 1;
  760. }
  761. static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
  762. {
  763. #ifdef LEAF
  764. /* if this is a su, dont show hosts
  765. * otherwise, let users see their own hosts */
  766. if (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)) {
  767. #endif /* LEAF */
  768. char s[1024] = "";
  769. struct list_type *q = NULL;
  770. strcpy(s, " HOSTS: ");
  771. for (q = e->u.list; q; q = q->next) {
  772. if (s[0] && !s[9])
  773. strcat(s, q->extra);
  774. else if (!s[0])
  775. sprintf(s, " %s", q->extra);
  776. else {
  777. if (strlen(s) + strlen(q->extra) + 2 > 65) {
  778. dprintf(idx, "%s\n", s);
  779. sprintf(s, " %s", q->extra);
  780. } else {
  781. strcat(s, ", ");
  782. strcat(s, q->extra);
  783. }
  784. }
  785. }
  786. if (s[0])
  787. dprintf(idx, "%s\n", s);
  788. #ifdef LEAF
  789. } else {
  790. dprintf(idx, " HOSTS: Hidden on leaf bots.");
  791. if (dcc[idx].u.chat->su_nick)
  792. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  793. dprintf(idx, "\n");
  794. }
  795. #endif /* LEAF */
  796. }
  797. static int hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  798. {
  799. if (!buf || !egg_strcasecmp(buf, "none")) {
  800. /* When the bot crashes, it's in this part, not in the 'else' part */
  801. list_type_kill(e->u.list);
  802. e->u.list = NULL;
  803. } else {
  804. char *host = buf, *p = strchr(host, ',');
  805. struct list_type **t;
  806. /* Can't have ,'s in hostmasks */
  807. while (p) {
  808. *p = '?';
  809. p = strchr(host, ',');
  810. }
  811. /* fred1: check for redundant hostmasks with
  812. * controversial "superpenis" algorithm ;) */
  813. /* I'm surprised Raistlin hasn't gotten involved in this controversy */
  814. t = &(e->u.list);
  815. while (*t) {
  816. if (wild_match(host, (*t)->extra)) {
  817. struct list_type *listu;
  818. listu = *t;
  819. *t = (*t)->next;
  820. if (listu->extra)
  821. free(listu->extra);
  822. free(listu);
  823. } else
  824. t = &((*t)->next);
  825. }
  826. *t = calloc(1, sizeof(struct list_type));
  827. (*t)->next = NULL;
  828. (*t)->extra = strdup(host);
  829. }
  830. return 1;
  831. }
  832. static int hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  833. {
  834. /* doh, try to be too clever and it bites your butt */
  835. return 0;
  836. }
  837. struct user_entry_type USERENTRY_HOSTS =
  838. {
  839. 0,
  840. hosts_gotshare,
  841. hosts_null,
  842. #ifdef HUB
  843. hosts_write_userfile,
  844. #endif /* HUB */
  845. hosts_kill,
  846. def_get,
  847. hosts_set,
  848. hosts_display,
  849. "HOSTS"
  850. };
  851. int list_append(struct list_type **h, struct list_type *i)
  852. {
  853. for (; *h; h = &((*h)->next));
  854. *h = i;
  855. return 1;
  856. }
  857. int list_delete(struct list_type **h, struct list_type *i)
  858. {
  859. for (; *h; h = &((*h)->next))
  860. if (*h == i) {
  861. *h = i->next;
  862. return 1;
  863. }
  864. return 0;
  865. }
  866. int list_contains(struct list_type *h, struct list_type *i)
  867. {
  868. for (; h; h = h->next)
  869. if (h == i) {
  870. return 1;
  871. }
  872. return 0;
  873. }
  874. int add_entry_type(struct user_entry_type *type)
  875. {
  876. struct userrec *u = NULL;
  877. list_insert(&entry_type_list, type);
  878. for (u = userlist; u; u = u->next) {
  879. struct user_entry *e = find_user_entry(type, u);
  880. if (e && e->name) {
  881. e->type = type;
  882. e->type->unpack(u, e);
  883. free(e->name);
  884. e->name = NULL;
  885. }
  886. }
  887. return 1;
  888. }
  889. struct user_entry_type *find_entry_type(char *name)
  890. {
  891. struct user_entry_type *p = NULL;
  892. for (p = entry_type_list; p; p = p->next) {
  893. if (!egg_strcasecmp(name, p->name))
  894. return p;
  895. }
  896. return NULL;
  897. }
  898. struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
  899. {
  900. struct user_entry **e = NULL, *t = NULL;
  901. for (e = &(u->entries); *e; e = &((*e)->next)) {
  902. if (((*e)->type == et) ||
  903. ((*e)->name && !egg_strcasecmp((*e)->name, et->name))) {
  904. t = *e;
  905. *e = t->next;
  906. t->next = u->entries;
  907. u->entries = t;
  908. return t;
  909. }
  910. }
  911. return NULL;
  912. }
  913. void *get_user(struct user_entry_type *et, struct userrec *u)
  914. {
  915. struct user_entry *e = NULL;
  916. if (u && (e = find_user_entry(et, u)))
  917. return et->get(u, e);
  918. return NULL;
  919. }
  920. int set_user(struct user_entry_type *et, struct userrec *u, void *d)
  921. {
  922. struct user_entry *e = NULL;
  923. int r;
  924. if (!u || !et)
  925. return 0;
  926. if (!(e = find_user_entry(et, u))) {
  927. e = calloc(1, sizeof(struct user_entry));
  928. e->type = et;
  929. e->name = NULL;
  930. e->u.list = NULL;
  931. list_insert((&(u->entries)), e);
  932. }
  933. r = et->set(u, e, d);
  934. if (!e->u.list) {
  935. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  936. free(e);
  937. }
  938. return r;
  939. }