userent.c 23 KB

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