userent.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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;
  19. void init_userent()
  20. {
  21. entry_type_list = NULL;
  22. add_entry_type(&USERENTRY_COMMENT);
  23. add_entry_type(&USERENTRY_INFO);
  24. add_entry_type(&USERENTRY_LASTON);
  25. add_entry_type(&USERENTRY_BOTADDR);
  26. add_entry_type(&USERENTRY_OS);
  27. add_entry_type(&USERENTRY_NODENAME);
  28. add_entry_type(&USERENTRY_USERNAME);
  29. add_entry_type(&USERENTRY_PASS);
  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. #ifdef HUB
  63. bool 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. bool 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. size_t l = strlen (string);
  83. char *i = NULL;
  84. if (l > 160)
  85. l = 160;
  86. e->u.string = (char *) my_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. bool 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. static 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. 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. #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 = (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. #endif /* HUB */
  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. #ifdef HUB
  287. static bool config_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  288. {
  289. struct xtra_key *x = NULL;
  290. for (x = (struct xtra_key *) e->u.extra; x; x = x->next)
  291. lfprintf(f, "--CONFIG %s %s\n", x->key, x->data);
  292. return 1;
  293. }
  294. #endif /* HUB */
  295. static bool config_kill(struct user_entry *e)
  296. {
  297. struct xtra_key *x = NULL, *y = NULL;
  298. for (x = (struct xtra_key *) e->u.extra; x; x = y) {
  299. y = x->next;
  300. free(x->key);
  301. free(x->data);
  302. free(x);
  303. }
  304. free(e);
  305. return 1;
  306. }
  307. struct user_entry_type USERENTRY_CONFIG = {
  308. 0,
  309. config_gotshare,
  310. config_unpack,
  311. #ifdef HUB
  312. config_write_userfile,
  313. #endif /* HUB */
  314. config_kill,
  315. def_get,
  316. config_set,
  317. config_display,
  318. "CONFIG"
  319. };
  320. #ifdef HUB
  321. static void botmisc_display(int idx, struct user_entry *e, struct userrec *u)
  322. {
  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. #endif /* HUB */
  329. struct user_entry_type USERENTRY_USERNAME = {
  330. 0,
  331. def_gotshare,
  332. def_unpack,
  333. #ifdef HUB
  334. def_write_userfile,
  335. #endif /* HUB */
  336. def_kill,
  337. def_get,
  338. def_set,
  339. #ifdef HUB
  340. botmisc_display,
  341. #else
  342. NULL,
  343. #endif /* HUB */
  344. "USERNAME"
  345. };
  346. struct user_entry_type USERENTRY_NODENAME = {
  347. 0,
  348. def_gotshare,
  349. def_unpack,
  350. #ifdef HUB
  351. def_write_userfile,
  352. #endif /* HUB */
  353. def_kill,
  354. def_get,
  355. def_set,
  356. #ifdef HUB
  357. botmisc_display,
  358. #else
  359. NULL,
  360. #endif /* HUB */
  361. "NODENAME"
  362. };
  363. struct user_entry_type USERENTRY_OS = {
  364. 0,
  365. def_gotshare,
  366. def_unpack,
  367. #ifdef HUB
  368. def_write_userfile,
  369. #endif /* HUB */
  370. def_kill,
  371. def_get,
  372. def_set,
  373. #ifdef HUB
  374. botmisc_display,
  375. #else
  376. NULL,
  377. #endif /* HUB */
  378. "OS"
  379. };
  380. void stats_add(struct userrec *u, int login, int op)
  381. {
  382. if (!u)
  383. return;
  384. char *s = (char *) get_user(&USERENTRY_STATS, u), s2[50] = "";
  385. int sl, so;
  386. if (s) {
  387. strncpyz(s2, s, sizeof(s2));
  388. } else
  389. strcpy(s2, "0 0");
  390. s = strchr(s2, ' ');
  391. if (s) {
  392. s++;
  393. so = atoi(s);
  394. } else
  395. so = 0;
  396. sl = atoi(s2);
  397. if (login)
  398. sl++;
  399. if (op)
  400. so++;
  401. sprintf(s2, "%i %i", sl, so);
  402. set_user(&USERENTRY_STATS, u, s2);
  403. }
  404. static void stats_display(int idx, struct user_entry *e, struct userrec *u)
  405. {
  406. /* format: logincount opcount */
  407. if (dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  408. char *p = strchr(e->u.string, ' ');
  409. if (p)
  410. dprintf(idx, " -- %i logins, %i ops\n", atoi(e->u.string), atoi(p));
  411. }
  412. }
  413. struct user_entry_type USERENTRY_STATS = {
  414. 0, /* always 0 ;) */
  415. def_gotshare,
  416. def_unpack,
  417. #ifdef HUB
  418. def_write_userfile,
  419. #endif /* HUB */
  420. def_kill,
  421. def_get,
  422. def_set,
  423. stats_display,
  424. "STATS"
  425. };
  426. void update_mod(char *handle, char *nick, char *cmd, char *par)
  427. {
  428. char tmp[100] = "";
  429. egg_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", now, nick, cmd, (par && par[0]) ? par : "");
  430. set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
  431. }
  432. static void modified_display(int idx, struct user_entry *e, struct userrec *u)
  433. {
  434. if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  435. char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
  436. time_t tm;
  437. strncpyz(tmp, e->u.string, sizeof(tmp));
  438. hnd = strchr(tmp, ' ');
  439. if (hnd)
  440. *hnd++ = 0;
  441. tm = atoi(tmp);
  442. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  443. if (hnd)
  444. dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
  445. else
  446. dprintf(idx, " -- Modified %s\n", tmp2);
  447. }
  448. }
  449. struct user_entry_type USERENTRY_MODIFIED =
  450. {
  451. 0,
  452. def_gotshare,
  453. def_unpack,
  454. #ifdef HUB
  455. def_write_userfile,
  456. #endif /* HUB */
  457. def_kill,
  458. def_get,
  459. def_set,
  460. modified_display,
  461. "MODIFIED"
  462. };
  463. static bool pass_set(struct userrec *u, struct user_entry *e, void *buf)
  464. {
  465. char newpass[32] = "";
  466. register char *pass = (char *) buf;
  467. if (e->u.extra)
  468. free(e->u.extra);
  469. if (!pass || !pass[0] || (pass[0] == '-'))
  470. e->u.extra = NULL;
  471. else {
  472. unsigned char *p = (unsigned char *) pass;
  473. if (strlen(pass) > MAXPASSLEN)
  474. pass[MAXPASSLEN] = 0;
  475. while (*p) {
  476. if ((*p <= 32) || (*p == 127))
  477. *p = '?';
  478. p++;
  479. }
  480. if (u->bot || (pass[0] == '+'))
  481. strcpy(newpass, pass);
  482. else
  483. encrypt_pass(pass, newpass);
  484. e->u.extra = strdup(newpass);
  485. }
  486. if (!noshare)
  487. shareout("c PASS %s %s\n", u->handle, pass ? pass : "");
  488. return 1;
  489. }
  490. struct user_entry_type USERENTRY_PASS =
  491. {
  492. 0,
  493. def_gotshare,
  494. def_unpack,
  495. #ifdef HUB
  496. def_write_userfile,
  497. #endif /* HUB */
  498. def_kill,
  499. def_get,
  500. pass_set,
  501. NULL,
  502. "PASS"
  503. };
  504. static void secpass_display(int idx, struct user_entry *e, struct userrec *u)
  505. {
  506. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  507. get_user_flagrec(dcc[idx].user, &fr, NULL);
  508. if (!strcmp(u->handle, dcc[idx].nick) || (glob_admin(fr) && isowner(dcc[idx].nick))) {
  509. #ifdef HUB
  510. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  511. #else
  512. dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
  513. if (dcc[idx].u.chat->su_nick)
  514. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  515. dprintf(idx, "\n");
  516. #endif /* HUB */
  517. }
  518. }
  519. struct user_entry_type USERENTRY_SECPASS =
  520. {
  521. 0,
  522. def_gotshare,
  523. def_unpack,
  524. #ifdef HUB
  525. def_write_userfile,
  526. #endif /* HUB */
  527. def_kill,
  528. def_get,
  529. def_set,
  530. secpass_display,
  531. "SECPASS"
  532. };
  533. static bool laston_unpack(struct userrec *u, struct user_entry *e)
  534. {
  535. char *par = e->u.list->extra, *arg = newsplit(&par);
  536. struct laston_info *li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
  537. if (!par[0])
  538. par = "???";
  539. li->laston = atoi(arg);
  540. li->lastonplace = strdup(par);
  541. list_type_kill(e->u.list);
  542. e->u.extra = li;
  543. return 1;
  544. }
  545. #ifdef HUB
  546. static bool laston_write_userfile(FILE * f, struct userrec *u, struct user_entry *e)
  547. {
  548. struct laston_info *li = (struct laston_info *) e->u.extra;
  549. if (lfprintf(f, "--LASTON %lu %s\n", li->laston,
  550. li->lastonplace ? li->lastonplace : "") == EOF)
  551. return 0;
  552. return 1;
  553. }
  554. #endif /* HUB */
  555. static bool laston_kill(struct user_entry *e)
  556. {
  557. if (((struct laston_info *) (e->u.extra))->lastonplace)
  558. free(((struct laston_info *) (e->u.extra))->lastonplace);
  559. free(e->u.extra);
  560. free(e);
  561. return 1;
  562. }
  563. static bool laston_set(struct userrec *u, struct user_entry *e, void *buf)
  564. {
  565. struct laston_info *li = (struct laston_info *) e->u.extra;
  566. if (li != buf) {
  567. if (li) {
  568. free(li->lastonplace);
  569. free(li);
  570. }
  571. li = (struct laston_info *) buf;
  572. e->u.extra = (struct laston_info *) buf;
  573. }
  574. /* FIXME: laston sharing is disabled until a better solution is found
  575. if (!noshare)
  576. shareout("c LASTON %s %s %li\n", u->handle, li->lastonplace ? li->lastonplace : "-", li->laston);
  577. */
  578. return 1;
  579. }
  580. static bool laston_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  581. {
  582. char *where = NULL;
  583. time_t timeval = 0;
  584. if (par[0])
  585. where = newsplit(&par);
  586. if (!strcmp(where, "-"))
  587. where = NULL;
  588. if (par[0])
  589. timeval = atol(newsplit(&par));
  590. touch_laston(u, where, timeval);
  591. return 1;
  592. }
  593. struct user_entry_type USERENTRY_LASTON =
  594. {
  595. 0, /* always 0 ;) */
  596. laston_gotshare,
  597. laston_unpack,
  598. #ifdef HUB
  599. laston_write_userfile,
  600. #endif /* HUB */
  601. laston_kill,
  602. def_get,
  603. laston_set,
  604. 0,
  605. "LASTON"
  606. };
  607. static bool botaddr_unpack(struct userrec *u, struct user_entry *e)
  608. {
  609. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  610. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  611. /* address:port/port:hublevel:uplink */
  612. Context;
  613. Assert(e);
  614. Assert(e->name);
  615. strcpy(p, e->u.list->extra);
  616. q1 = strchr(p, ':');
  617. if (q1)
  618. *q1++ = 0;
  619. bi->address = strdup(p);
  620. if (q1) {
  621. q2 = strchr(q1, ':');
  622. if (q2)
  623. *q2++ = 0;
  624. bi->telnet_port = atoi(q1);
  625. q1 = strchr(q1, '/');
  626. if (q1) {
  627. q1++;
  628. bi->relay_port = atoi(q1);
  629. }
  630. if (q2) {
  631. q1 = strchr(q2, ':');
  632. if (q1) {
  633. *q1++ = 0;
  634. bi->uplink = strdup(q1);
  635. }
  636. bi->hublevel = atoi(q2);
  637. }
  638. }
  639. if (!bi->telnet_port)
  640. bi->telnet_port = 3333;
  641. if (!bi->relay_port)
  642. bi->relay_port = bi->telnet_port;
  643. if (!bi->uplink) {
  644. bi->uplink = (char *) my_calloc(1, 1);
  645. }
  646. list_type_kill(e->u.list);
  647. e->u.extra = bi;
  648. return 1;
  649. }
  650. static bool botaddr_kill(struct user_entry *e)
  651. {
  652. free(((struct bot_addr *) (e->u.extra))->address);
  653. free(((struct bot_addr *) (e->u.extra))->uplink);
  654. free(e->u.extra);
  655. free(e);
  656. return 1;
  657. }
  658. #ifdef HUB
  659. static bool botaddr_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  660. {
  661. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  662. if (lfprintf(f, "--%s %s:%u/%u:%u:%s\n", e->type->name, bi->address,
  663. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink) == EOF)
  664. return 0;
  665. return 1;
  666. }
  667. #endif /* HUB */
  668. static bool botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
  669. {
  670. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  671. Context;
  672. if (!bi && !buf)
  673. return 1;
  674. if (bi != buf) {
  675. if (bi) {
  676. Assert(bi->address);
  677. free(bi->address);
  678. Assert(bi->uplink);
  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. Assert(u);
  687. if (bi && !noshare) {
  688. shareout("c BOTADDR %s %s %d %d %d %s\n",u->handle,
  689. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  690. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  691. }
  692. return 1;
  693. }
  694. #ifdef HUB
  695. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  696. {
  697. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  698. get_user_flagrec(dcc[idx].user, &fr, NULL);
  699. if (glob_admin(fr)) {
  700. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  701. if (bi->address && bi->hublevel && bi->hublevel != 0) {
  702. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  703. dprintf(idx, " port: %d\n", bi->telnet_port);
  704. }
  705. if (bi->hublevel && bi->hublevel != 0)
  706. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  707. if (bi->uplink && bi->uplink[0])
  708. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  709. }
  710. }
  711. #endif /* HUB */
  712. static bool botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  713. {
  714. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  715. char *arg = newsplit(&buf);
  716. bi->address = strdup(arg);
  717. arg = newsplit(&buf);
  718. bi->telnet_port = atoi(arg);
  719. arg = newsplit(&buf);
  720. bi->relay_port = atoi(arg);
  721. arg = newsplit(&buf);
  722. bi->hublevel = atoi(arg);
  723. bi->uplink = strdup(buf);
  724. if (!bi->telnet_port)
  725. bi->telnet_port = 3333;
  726. if (!bi->relay_port)
  727. bi->relay_port = bi->telnet_port;
  728. return botaddr_set(u, e, bi);
  729. }
  730. struct user_entry_type USERENTRY_BOTADDR =
  731. {
  732. 0, /* always 0 ;) */
  733. botaddr_gotshare,
  734. botaddr_unpack,
  735. #ifdef HUB
  736. botaddr_write_userfile,
  737. #endif /* HUB */
  738. botaddr_kill,
  739. def_get,
  740. botaddr_set,
  741. #ifdef HUB
  742. botaddr_display,
  743. #else
  744. NULL,
  745. #endif /* HUB */
  746. "BOTADDR"
  747. };
  748. #ifdef HUB
  749. static bool hosts_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  750. {
  751. struct list_type *h = NULL;
  752. for (h = (struct list_type *) e->u.extra; h; h = h->next)
  753. if (lfprintf(f, "--HOSTS %s\n", h->extra) == EOF)
  754. return 0;
  755. return 1;
  756. }
  757. #endif /* HUB */
  758. static bool hosts_null(struct userrec *u, struct user_entry *e)
  759. {
  760. return 1;
  761. }
  762. static bool hosts_kill(struct user_entry *e)
  763. {
  764. list_type_kill(e->u.list);
  765. free(e);
  766. return 1;
  767. }
  768. static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
  769. {
  770. #ifdef LEAF
  771. /* if this is a su, dont show hosts
  772. * otherwise, let users see their own hosts */
  773. if (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)) {
  774. #endif /* LEAF */
  775. char s[1024] = "";
  776. struct list_type *q = NULL;
  777. strcpy(s, " HOSTS: ");
  778. for (q = e->u.list; q; q = q->next) {
  779. if (s[0] && !s[9])
  780. strcat(s, q->extra);
  781. else if (!s[0])
  782. sprintf(s, " %s", q->extra);
  783. else {
  784. if (strlen(s) + strlen(q->extra) + 2 > 65) {
  785. dprintf(idx, "%s\n", s);
  786. sprintf(s, " %s", q->extra);
  787. } else {
  788. strcat(s, ", ");
  789. strcat(s, q->extra);
  790. }
  791. }
  792. }
  793. if (s[0])
  794. dprintf(idx, "%s\n", s);
  795. #ifdef LEAF
  796. } else {
  797. dprintf(idx, " HOSTS: Hidden on leaf bots.");
  798. if (dcc[idx].u.chat->su_nick)
  799. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  800. dprintf(idx, "\n");
  801. }
  802. #endif /* LEAF */
  803. }
  804. static bool hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  805. {
  806. if (!buf || !egg_strcasecmp((const char *) buf, "none")) {
  807. /* When the bot crashes, it's in this part, not in the 'else' part */
  808. list_type_kill(e->u.list);
  809. e->u.list = NULL;
  810. } else {
  811. char *host = (char *) buf, *p = strchr(host, ',');
  812. struct list_type **t;
  813. /* Can't have ,'s in hostmasks */
  814. while (p) {
  815. *p = '?';
  816. p = strchr(host, ',');
  817. }
  818. /* fred1: check for redundant hostmasks with
  819. * controversial "superpenis" algorithm ;) */
  820. /* I'm surprised Raistlin hasn't gotten involved in this controversy */
  821. t = &(e->u.list);
  822. while (*t) {
  823. if (wild_match(host, (*t)->extra)) {
  824. struct list_type *listu;
  825. listu = *t;
  826. *t = (*t)->next;
  827. if (listu->extra)
  828. free(listu->extra);
  829. free(listu);
  830. } else
  831. t = &((*t)->next);
  832. }
  833. *t = (struct list_type *) my_calloc(1, sizeof(struct list_type));
  834. (*t)->next = NULL;
  835. (*t)->extra = strdup(host);
  836. }
  837. return 1;
  838. }
  839. static bool hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  840. {
  841. /* doh, try to be too clever and it bites your butt */
  842. return 0;
  843. }
  844. struct user_entry_type USERENTRY_HOSTS =
  845. {
  846. 0,
  847. hosts_gotshare,
  848. hosts_null,
  849. #ifdef HUB
  850. hosts_write_userfile,
  851. #endif /* HUB */
  852. hosts_kill,
  853. def_get,
  854. hosts_set,
  855. hosts_display,
  856. "HOSTS"
  857. };
  858. bool list_append(struct list_type **h, struct list_type *i)
  859. {
  860. for (; *h; h = &((*h)->next));
  861. *h = i;
  862. return 1;
  863. }
  864. bool list_delete(struct list_type **h, struct list_type *i)
  865. {
  866. for (; *h; h = &((*h)->next))
  867. if (*h == i) {
  868. *h = i->next;
  869. return 1;
  870. }
  871. return 0;
  872. }
  873. bool list_contains(struct list_type *h, struct list_type *i)
  874. {
  875. for (; h; h = h->next)
  876. if (h == i) {
  877. return 1;
  878. }
  879. return 0;
  880. }
  881. bool add_entry_type(struct user_entry_type *type)
  882. {
  883. struct userrec *u = NULL;
  884. list_insert((&entry_type_list), type);
  885. for (u = userlist; u; u = u->next) {
  886. struct user_entry *e = find_user_entry(type, u);
  887. if (e && e->name) {
  888. e->type = type;
  889. e->type->unpack(u, e);
  890. free(e->name);
  891. e->name = NULL;
  892. }
  893. }
  894. return 1;
  895. }
  896. struct user_entry_type *find_entry_type(char *name)
  897. {
  898. struct user_entry_type *p = NULL;
  899. for (p = entry_type_list; p; p = p->next) {
  900. if (!egg_strcasecmp(name, p->name))
  901. return p;
  902. }
  903. return NULL;
  904. }
  905. struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
  906. {
  907. struct user_entry **e = NULL, *t = NULL;
  908. for (e = &(u->entries); *e; e = &((*e)->next)) {
  909. if (((*e)->type == et) ||
  910. ((*e)->name && !egg_strcasecmp((*e)->name, et->name))) {
  911. t = *e;
  912. *e = t->next;
  913. t->next = u->entries;
  914. u->entries = t;
  915. return t;
  916. }
  917. }
  918. return NULL;
  919. }
  920. void *get_user(struct user_entry_type *et, struct userrec *u)
  921. {
  922. struct user_entry *e = NULL;
  923. if (u && (e = find_user_entry(et, u)))
  924. return et->get(u, e);
  925. return NULL;
  926. }
  927. bool set_user(struct user_entry_type *et, struct userrec *u, void *d)
  928. {
  929. if (!u || !et)
  930. return 0;
  931. struct user_entry *e = NULL;
  932. bool r;
  933. if (!(e = find_user_entry(et, u))) {
  934. e = (struct user_entry *) my_calloc(1, sizeof(struct user_entry));
  935. e->type = et;
  936. e->name = NULL;
  937. e->u.list = NULL;
  938. list_insert((&(u->entries)), e);
  939. }
  940. r = et->set(u, e, d);
  941. if (!e->u.list) {
  942. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  943. free(e);
  944. }
  945. return r;
  946. }