userent.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 = (char *) 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("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, *mynew = (struct xtra_key *) buf;
  181. for (curr = (struct xtra_key *) e->u.extra; curr; curr = curr->next) {
  182. if (curr->key && !egg_strcasecmp(curr->key, mynew->key)) {
  183. old = curr;
  184. break;
  185. }
  186. }
  187. if (!old && (!mynew->data || !mynew->data[0])) {
  188. /* delete non-existant entry */
  189. free(mynew->key);
  190. if (mynew->data)
  191. free(mynew->data);
  192. free(mynew);
  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("c CONFIG %s %s %s\n", u->handle, mynew->key, mynew->data ? mynew->data : "");
  199. if ((old && old != mynew) || !mynew->data || !mynew->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 != mynew && mynew->data) {
  206. if (mynew->data[0]) {
  207. list_insert((&e->u.extra), mynew);
  208. } else {
  209. if (mynew->data)
  210. free(mynew->data);
  211. free(mynew->key);
  212. free(mynew);
  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 = (struct xtra_key *) 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 = (struct xtra_key *) calloc(1, sizeof(struct xtra_key));
  274. l = strlen(arg);
  275. if (l > 1500)
  276. l = 1500;
  277. xk->key = (char *) 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 = (char *) 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 = (struct xtra_key *) 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 = (char *) 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 newpass[32] = "";
  471. register char *pass = (char *) 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) > MAXPASSLEN)
  479. pass[MAXPASSLEN] = 0;
  480. while (*p) {
  481. if ((*p <= 32) || (*p == 127))
  482. *p = '?';
  483. p++;
  484. }
  485. if (u->bot || (pass[0] == '+'))
  486. strcpy(newpass, pass);
  487. else
  488. encrypt_pass(pass, newpass);
  489. e->u.extra = strdup(newpass);
  490. }
  491. if (!noshare)
  492. shareout("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 = (struct laston_info *) 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 = (struct laston_info *) buf;
  580. e->u.extra = (struct laston_info *) buf;
  581. }
  582. /* FIXME: laston sharing is disabled until a better solution is found
  583. if (!noshare)
  584. shareout("c LASTON %s %s %li\n", u->handle, li->lastonplace ? li->lastonplace : "-", li->laston);
  585. */
  586. return 1;
  587. }
  588. static int laston_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  589. {
  590. char *where = NULL;
  591. time_t timeval = 0;
  592. if (par[0])
  593. where = newsplit(&par);
  594. if (!strcmp(where, "-"))
  595. where = NULL;
  596. if (par[0])
  597. timeval = atol(newsplit(&par));
  598. touch_laston(u, where, timeval);
  599. return 1;
  600. }
  601. struct user_entry_type USERENTRY_LASTON =
  602. {
  603. 0, /* always 0 ;) */
  604. laston_gotshare,
  605. laston_unpack,
  606. #ifdef HUB
  607. laston_write_userfile,
  608. #endif /* HUB */
  609. laston_kill,
  610. def_get,
  611. laston_set,
  612. 0,
  613. "LASTON"
  614. };
  615. static int botaddr_unpack(struct userrec *u, struct user_entry *e)
  616. {
  617. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  618. struct bot_addr *bi = NULL;
  619. bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  620. /* address:port/port:hublevel:uplink */
  621. Context;
  622. Assert(e);
  623. Assert(e->name);
  624. strcpy(p, e->u.list->extra);
  625. q1 = strchr(p, ':');
  626. if (q1)
  627. *q1++ = 0;
  628. bi->address = strdup(p);
  629. if (q1) {
  630. q2 = strchr(q1, ':');
  631. if (q2)
  632. *q2++ = 0;
  633. bi->telnet_port = atoi(q1);
  634. q1 = strchr(q1, '/');
  635. if (q1) {
  636. q1++;
  637. bi->relay_port = atoi(q1);
  638. }
  639. if (q2) {
  640. q1 = strchr(q2, ':');
  641. if (q1) {
  642. *q1++ = 0;
  643. bi->uplink = strdup(q1);
  644. }
  645. bi->hublevel = atoi(q2);
  646. }
  647. }
  648. if (!bi->telnet_port)
  649. bi->telnet_port = 3333;
  650. if (!bi->relay_port)
  651. bi->relay_port = bi->telnet_port;
  652. if (!bi->uplink) {
  653. bi->uplink = (char *) calloc(1, 1);
  654. }
  655. list_type_kill(e->u.list);
  656. e->u.extra = bi;
  657. return 1;
  658. }
  659. static int botaddr_kill(struct user_entry *e)
  660. {
  661. free(((struct bot_addr *) (e->u.extra))->address);
  662. free(((struct bot_addr *) (e->u.extra))->uplink);
  663. free(e->u.extra);
  664. free(e);
  665. return 1;
  666. }
  667. #ifdef HUB
  668. static int botaddr_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  669. {
  670. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  671. if (lfprintf(f, "--%s %s:%u/%u:%u:%s\n", e->type->name, bi->address,
  672. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink) == EOF)
  673. return 0;
  674. return 1;
  675. }
  676. #endif /* HUB */
  677. static int botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
  678. {
  679. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  680. Context;
  681. if (!bi && !buf)
  682. return 1;
  683. if (bi != buf) {
  684. if (bi) {
  685. Assert(bi->address);
  686. free(bi->address);
  687. Assert(bi->uplink);
  688. free(bi->uplink);
  689. free(bi);
  690. }
  691. ContextNote("(sharebug) occurred in botaddr_set");
  692. bi = (struct bot_addr *) buf;
  693. e->u.extra = (struct bot_addr *) buf;
  694. }
  695. Assert(u);
  696. if (bi && !noshare) {
  697. shareout("c BOTADDR %s %s %d %d %d %s\n",u->handle,
  698. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  699. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  700. }
  701. return 1;
  702. }
  703. #ifdef HUB
  704. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  705. {
  706. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  707. get_user_flagrec(dcc[idx].user, &fr, NULL);
  708. if (glob_admin(fr)) {
  709. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  710. if (bi->address && bi->hublevel && bi->hublevel != 0) {
  711. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  712. dprintf(idx, " port: %d\n", bi->telnet_port);
  713. }
  714. if (bi->hublevel && bi->hublevel != 0)
  715. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  716. if (bi->uplink && bi->uplink[0])
  717. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  718. }
  719. }
  720. #endif /* HUB */
  721. static int botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  722. {
  723. struct bot_addr *bi = NULL;
  724. char *arg = NULL;
  725. bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  726. arg = newsplit(&buf);
  727. bi->address = strdup(arg);
  728. arg = newsplit(&buf);
  729. bi->telnet_port = atoi(arg);
  730. arg = newsplit(&buf);
  731. bi->relay_port = atoi(arg);
  732. arg = newsplit(&buf);
  733. bi->hublevel = atoi(arg);
  734. bi->uplink = strdup(buf);
  735. if (!bi->telnet_port)
  736. bi->telnet_port = 3333;
  737. if (!bi->relay_port)
  738. bi->relay_port = bi->telnet_port;
  739. return botaddr_set(u, e, bi);
  740. }
  741. struct user_entry_type USERENTRY_BOTADDR =
  742. {
  743. 0, /* always 0 ;) */
  744. botaddr_gotshare,
  745. botaddr_unpack,
  746. #ifdef HUB
  747. botaddr_write_userfile,
  748. #endif /* HUB */
  749. botaddr_kill,
  750. def_get,
  751. botaddr_set,
  752. #ifdef HUB
  753. botaddr_display,
  754. #else
  755. NULL,
  756. #endif /* HUB */
  757. "BOTADDR"
  758. };
  759. #ifdef HUB
  760. static int hosts_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  761. {
  762. struct list_type *h = NULL;
  763. for (h = e->u.extra; h; h = h->next)
  764. if (lfprintf(f, "--HOSTS %s\n", h->extra) == EOF)
  765. return 0;
  766. return 1;
  767. }
  768. #endif /* HUB */
  769. static int hosts_null(struct userrec *u, struct user_entry *e)
  770. {
  771. return 1;
  772. }
  773. static int hosts_kill(struct user_entry *e)
  774. {
  775. list_type_kill(e->u.list);
  776. free(e);
  777. return 1;
  778. }
  779. static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
  780. {
  781. #ifdef LEAF
  782. /* if this is a su, dont show hosts
  783. * otherwise, let users see their own hosts */
  784. if (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)) {
  785. #endif /* LEAF */
  786. char s[1024] = "";
  787. struct list_type *q = NULL;
  788. strcpy(s, " HOSTS: ");
  789. for (q = e->u.list; q; q = q->next) {
  790. if (s[0] && !s[9])
  791. strcat(s, q->extra);
  792. else if (!s[0])
  793. sprintf(s, " %s", q->extra);
  794. else {
  795. if (strlen(s) + strlen(q->extra) + 2 > 65) {
  796. dprintf(idx, "%s\n", s);
  797. sprintf(s, " %s", q->extra);
  798. } else {
  799. strcat(s, ", ");
  800. strcat(s, q->extra);
  801. }
  802. }
  803. }
  804. if (s[0])
  805. dprintf(idx, "%s\n", s);
  806. #ifdef LEAF
  807. } else {
  808. dprintf(idx, " HOSTS: Hidden on leaf bots.");
  809. if (dcc[idx].u.chat->su_nick)
  810. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  811. dprintf(idx, "\n");
  812. }
  813. #endif /* LEAF */
  814. }
  815. static int hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  816. {
  817. if (!buf || !egg_strcasecmp((const char *) buf, "none")) {
  818. /* When the bot crashes, it's in this part, not in the 'else' part */
  819. list_type_kill(e->u.list);
  820. e->u.list = NULL;
  821. } else {
  822. char *host = (char *) buf, *p = strchr(host, ',');
  823. struct list_type **t;
  824. /* Can't have ,'s in hostmasks */
  825. while (p) {
  826. *p = '?';
  827. p = strchr(host, ',');
  828. }
  829. /* fred1: check for redundant hostmasks with
  830. * controversial "superpenis" algorithm ;) */
  831. /* I'm surprised Raistlin hasn't gotten involved in this controversy */
  832. t = &(e->u.list);
  833. while (*t) {
  834. if (wild_match(host, (*t)->extra)) {
  835. struct list_type *listu;
  836. listu = *t;
  837. *t = (*t)->next;
  838. if (listu->extra)
  839. free(listu->extra);
  840. free(listu);
  841. } else
  842. t = &((*t)->next);
  843. }
  844. *t = (struct list_type *) calloc(1, sizeof(struct list_type));
  845. (*t)->next = NULL;
  846. (*t)->extra = strdup(host);
  847. }
  848. return 1;
  849. }
  850. static int hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  851. {
  852. /* doh, try to be too clever and it bites your butt */
  853. return 0;
  854. }
  855. struct user_entry_type USERENTRY_HOSTS =
  856. {
  857. 0,
  858. hosts_gotshare,
  859. hosts_null,
  860. #ifdef HUB
  861. hosts_write_userfile,
  862. #endif /* HUB */
  863. hosts_kill,
  864. def_get,
  865. hosts_set,
  866. hosts_display,
  867. "HOSTS"
  868. };
  869. int list_append(struct list_type **h, struct list_type *i)
  870. {
  871. for (; *h; h = &((*h)->next));
  872. *h = i;
  873. return 1;
  874. }
  875. int list_delete(struct list_type **h, struct list_type *i)
  876. {
  877. for (; *h; h = &((*h)->next))
  878. if (*h == i) {
  879. *h = i->next;
  880. return 1;
  881. }
  882. return 0;
  883. }
  884. int list_contains(struct list_type *h, struct list_type *i)
  885. {
  886. for (; h; h = h->next)
  887. if (h == i) {
  888. return 1;
  889. }
  890. return 0;
  891. }
  892. int add_entry_type(struct user_entry_type *type)
  893. {
  894. struct userrec *u = NULL;
  895. list_insert(&entry_type_list, type);
  896. for (u = userlist; u; u = u->next) {
  897. struct user_entry *e = find_user_entry(type, u);
  898. if (e && e->name) {
  899. e->type = type;
  900. e->type->unpack(u, e);
  901. free(e->name);
  902. e->name = NULL;
  903. }
  904. }
  905. return 1;
  906. }
  907. struct user_entry_type *find_entry_type(char *name)
  908. {
  909. struct user_entry_type *p = NULL;
  910. for (p = entry_type_list; p; p = p->next) {
  911. if (!egg_strcasecmp(name, p->name))
  912. return p;
  913. }
  914. return NULL;
  915. }
  916. struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
  917. {
  918. struct user_entry **e = NULL, *t = NULL;
  919. for (e = &(u->entries); *e; e = &((*e)->next)) {
  920. if (((*e)->type == et) ||
  921. ((*e)->name && !egg_strcasecmp((*e)->name, et->name))) {
  922. t = *e;
  923. *e = t->next;
  924. t->next = u->entries;
  925. u->entries = t;
  926. return t;
  927. }
  928. }
  929. return NULL;
  930. }
  931. void *get_user(struct user_entry_type *et, struct userrec *u)
  932. {
  933. struct user_entry *e = NULL;
  934. if (u && (e = find_user_entry(et, u)))
  935. return et->get(u, e);
  936. return NULL;
  937. }
  938. int set_user(struct user_entry_type *et, struct userrec *u, void *d)
  939. {
  940. struct user_entry *e = NULL;
  941. int r;
  942. if (!u || !et)
  943. return 0;
  944. if (!(e = find_user_entry(et, u))) {
  945. e = (struct user_entry *) calloc(1, sizeof(struct user_entry));
  946. e->type = et;
  947. e->name = NULL;
  948. e->u.list = NULL;
  949. list_insert((&(u->entries)), e);
  950. }
  951. r = et->set(u, e, d);
  952. if (!e->u.list) {
  953. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  954. free(e);
  955. }
  956. return r;
  957. }