userent.c 21 KB

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