userent.c 22 KB

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