userent.c 24 KB

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