userent.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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_CMDS, "@", "%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. #ifdef S_UTCTIME
  159. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  160. #else /* !S_UTCTIME */
  161. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", localtime(&tm));
  162. #endif /* S_UTCTIME */
  163. if (hnd)
  164. dprintf(idx, " -- Added %s by %s\n", tmp2, hnd);
  165. else
  166. dprintf(idx, " -- Added %s\n", tmp2);
  167. }
  168. }
  169. struct user_entry_type USERENTRY_ADDED = {
  170. 0, /* always 0 ;) */
  171. def_gotshare,
  172. def_unpack,
  173. #ifdef HUB
  174. def_write_userfile,
  175. #endif /* HUB */
  176. def_kill,
  177. def_get,
  178. def_set,
  179. added_display,
  180. "ADDED"
  181. };
  182. int config_set(struct userrec *u, struct user_entry *e, void *buf)
  183. {
  184. struct xtra_key *curr = NULL, *old = NULL, *new = buf;
  185. for (curr = e->u.extra; curr; curr = curr->next) {
  186. if (curr->key && !egg_strcasecmp(curr->key, new->key)) {
  187. old = curr;
  188. break;
  189. }
  190. }
  191. if (!old && (!new->data || !new->data[0])) {
  192. /* delete non-existant entry */
  193. free(new->key);
  194. if (new->data)
  195. free(new->data);
  196. free(new);
  197. return 1;
  198. }
  199. /* we will possibly free new below, so let's send the information
  200. * to the botnet now */
  201. if (!noshare && !cfg_noshare)
  202. shareout(NULL, "c CONFIG %s %s %s\n", u->handle, new->key, new->data ? new->data : "");
  203. if ((old && old != new) || !new->data || !new->data[0]) {
  204. list_delete((struct list_type **) (&e->u.extra), (struct list_type *) old);
  205. free(old->key);
  206. free(old->data);
  207. free(old);
  208. }
  209. if (old != new && new->data) {
  210. if (new->data[0]) {
  211. list_insert((&e->u.extra), new);
  212. } else {
  213. if (new->data)
  214. free(new->data);
  215. free(new->key);
  216. free(new);
  217. }
  218. }
  219. return 1;
  220. }
  221. int config_unpack(struct userrec *u, struct user_entry *e)
  222. {
  223. struct list_type *curr = NULL, *head = NULL;
  224. struct xtra_key *t = NULL;
  225. char *key = NULL, *data = NULL;
  226. head = curr = e->u.list;
  227. e->u.extra = NULL;
  228. while (curr) {
  229. t = calloc(1, sizeof(struct xtra_key));
  230. data = curr->extra;
  231. key = newsplit(&data);
  232. if (data[0]) {
  233. t->key = strdup(key);
  234. t->data = strdup(data);
  235. list_insert((&e->u.extra), t);
  236. }
  237. curr = curr->next;
  238. }
  239. list_type_kill(head);
  240. return 1;
  241. }
  242. void config_display(int idx, struct user_entry *e, struct userrec *u)
  243. {
  244. #ifdef HUB
  245. struct xtra_key *xk = NULL;
  246. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  247. get_user_flagrec(dcc[idx].user, &fr, NULL);
  248. /* scan thru xtra field, searching for matches */
  249. for (xk = e->u.extra; xk; xk = xk->next) {
  250. /* ok, it's a valid xtra field entry */
  251. if (glob_owner(fr))
  252. dprintf(idx, " %s: %s\n", xk->key, xk->data);
  253. }
  254. #endif /* HUB */
  255. }
  256. int config_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  257. {
  258. char *arg = NULL;
  259. struct xtra_key *xk = NULL;
  260. int l;
  261. arg = newsplit(&buf);
  262. if (!arg[0])
  263. return 1;
  264. if (!strcmp(u->handle, conf.bot->nick)) {
  265. struct cfg_entry *cfgent = NULL;
  266. int i;
  267. cfg_noshare = 1;
  268. for (i = 0; !cfgent && (i < cfg_count); i++)
  269. if (!strcmp(arg, cfg[i]->name) && (cfg[i]->flags & CFGF_LOCAL))
  270. cfgent = cfg[i];
  271. if (cfgent) {
  272. set_cfg_str(conf.bot->nick, cfgent->name, (buf && buf[0]) ? buf : NULL);
  273. }
  274. cfg_noshare = 0;
  275. return 1;
  276. }
  277. xk = calloc(1, sizeof(struct xtra_key));
  278. l = strlen(arg);
  279. if (l > 1500)
  280. l = 1500;
  281. xk->key = calloc(1, l + 1);
  282. strncpy(xk->key, arg, l + 1);
  283. if (buf && buf[0]) {
  284. int k = strlen(buf);
  285. if (k > 1500 - l)
  286. k = 1500 - l;
  287. xk->data = calloc(1, k + 1);
  288. strncpy(xk->data, buf, k + 1);
  289. }
  290. config_set(u, e, xk);
  291. return 1;
  292. }
  293. #ifdef HUB
  294. int config_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  295. {
  296. struct xtra_key *x = NULL;
  297. for (x = e->u.extra; x; x = x->next)
  298. lfprintf(f, "--CONFIG %s %s\n", x->key, x->data);
  299. return 1;
  300. }
  301. #endif /* HUB */
  302. int config_kill(struct user_entry *e)
  303. {
  304. struct xtra_key *x = NULL, *y = NULL;
  305. for (x = e->u.extra; x; x = y) {
  306. y = x->next;
  307. free(x->key);
  308. free(x->data);
  309. free(x);
  310. }
  311. free(e);
  312. return 1;
  313. }
  314. struct user_entry_type USERENTRY_CONFIG = {
  315. 0,
  316. config_gotshare,
  317. config_unpack,
  318. #ifdef HUB
  319. config_write_userfile,
  320. #endif /* HUB */
  321. config_kill,
  322. def_get,
  323. config_set,
  324. config_display,
  325. "CONFIG"
  326. };
  327. #ifdef HUB
  328. static void botmisc_display(int idx, struct user_entry *e, struct userrec *u)
  329. {
  330. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  331. get_user_flagrec(dcc[idx].user, &fr, NULL);
  332. if (glob_admin(fr))
  333. dprintf(idx, " %s: %s\n", e->type->name, e->u.string ? e->u.string : "");
  334. }
  335. #endif /* HUB */
  336. struct user_entry_type USERENTRY_USERNAME = {
  337. 0,
  338. def_gotshare,
  339. def_unpack,
  340. #ifdef HUB
  341. def_write_userfile,
  342. #endif /* HUB */
  343. def_kill,
  344. def_get,
  345. def_set,
  346. #ifdef HUB
  347. botmisc_display,
  348. #else
  349. NULL,
  350. #endif /* HUB */
  351. "USERNAME"
  352. };
  353. struct user_entry_type USERENTRY_NODENAME = {
  354. 0,
  355. def_gotshare,
  356. def_unpack,
  357. #ifdef HUB
  358. def_write_userfile,
  359. #endif /* HUB */
  360. def_kill,
  361. def_get,
  362. def_set,
  363. #ifdef HUB
  364. botmisc_display,
  365. #else
  366. NULL,
  367. #endif /* HUB */
  368. "NODENAME"
  369. };
  370. struct user_entry_type USERENTRY_OS = {
  371. 0,
  372. def_gotshare,
  373. def_unpack,
  374. #ifdef HUB
  375. def_write_userfile,
  376. #endif /* HUB */
  377. def_kill,
  378. def_get,
  379. def_set,
  380. #ifdef HUB
  381. botmisc_display,
  382. #else
  383. NULL,
  384. #endif /* HUB */
  385. "OS"
  386. };
  387. void stats_add(struct userrec *u, int login, int op)
  388. {
  389. char *s = NULL, s2[50] = "";
  390. int sl, so;
  391. if (!u)
  392. return;
  393. s = get_user(&USERENTRY_STATS, u);
  394. if (s) {
  395. strncpyz(s2, s, sizeof(s2));
  396. } else
  397. strcpy(s2, "0 0");
  398. s = strchr(s2, ' ');
  399. if (s) {
  400. s++;
  401. so = atoi(s);
  402. } else
  403. so = 0;
  404. sl = atoi(s2);
  405. if (login)
  406. sl++;
  407. if (op)
  408. so++;
  409. sprintf(s2, "%i %i", sl, so);
  410. set_user(&USERENTRY_STATS, u, s2);
  411. }
  412. void stats_display(int idx, struct user_entry *e, struct userrec *u)
  413. {
  414. /* format: logincount opcount */
  415. if (dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  416. char *p;
  417. p = strchr(e->u.string, ' ');
  418. if (p)
  419. dprintf(idx, " -- %i logins, %i ops\n", atoi(e->u.string), atoi(p));
  420. }
  421. }
  422. struct user_entry_type USERENTRY_STATS = {
  423. 0, /* always 0 ;) */
  424. def_gotshare,
  425. def_unpack,
  426. #ifdef HUB
  427. def_write_userfile,
  428. #endif /* HUB */
  429. def_kill,
  430. def_get,
  431. def_set,
  432. stats_display,
  433. "STATS"
  434. };
  435. void update_mod(char *handle, char *nick, char *cmd, char *par)
  436. {
  437. char tmp[100] = "";
  438. egg_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", now, nick, cmd, (par && par[0]) ? par : "");
  439. set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
  440. }
  441. void modified_display(int idx, struct user_entry *e, struct userrec *u)
  442. {
  443. if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  444. char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
  445. time_t tm;
  446. strncpyz(tmp, e->u.string, sizeof(tmp));
  447. hnd = strchr(tmp, ' ');
  448. if (hnd)
  449. *hnd++ = 0;
  450. tm = atoi(tmp);
  451. #ifdef S_UTCTIME
  452. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  453. #else /* !S_UTCTIME */
  454. egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", localtime(&tm));
  455. #endif /* S_UTCTIME */
  456. if (hnd)
  457. dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
  458. else
  459. dprintf(idx, " -- Modified %s\n", tmp2);
  460. }
  461. }
  462. struct user_entry_type USERENTRY_MODIFIED =
  463. {
  464. 0,
  465. def_gotshare,
  466. def_unpack,
  467. #ifdef HUB
  468. def_write_userfile,
  469. #endif /* HUB */
  470. def_kill,
  471. def_get,
  472. def_set,
  473. modified_display,
  474. "MODIFIED"
  475. };
  476. int pass_set(struct userrec *u, struct user_entry *e, void *buf)
  477. {
  478. char new[32] = "";
  479. register char *pass = buf;
  480. if (e->u.extra)
  481. free(e->u.extra);
  482. if (!pass || !pass[0] || (pass[0] == '-'))
  483. e->u.extra = NULL;
  484. else {
  485. unsigned char *p = (unsigned char *) pass;
  486. if (strlen(pass) > 15)
  487. pass[15] = 0;
  488. while (*p) {
  489. if ((*p <= 32) || (*p == 127))
  490. *p = '?';
  491. p++;
  492. }
  493. if (u->bot || (pass[0] == '+'))
  494. strcpy(new, pass);
  495. else
  496. encrypt_pass(pass, new);
  497. e->u.extra = strdup(new);
  498. }
  499. if (!noshare)
  500. shareout(NULL, "c PASS %s %s\n", u->handle, pass ? pass : "");
  501. return 1;
  502. }
  503. struct user_entry_type USERENTRY_PASS =
  504. {
  505. 0,
  506. def_gotshare,
  507. def_unpack,
  508. #ifdef HUB
  509. def_write_userfile,
  510. #endif /* HUB */
  511. def_kill,
  512. def_get,
  513. pass_set,
  514. NULL,
  515. "PASS"
  516. };
  517. void secpass_display(int idx, struct user_entry *e, struct userrec *u)
  518. {
  519. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  520. get_user_flagrec(dcc[idx].user, &fr, NULL);
  521. if (!strcmp(u->handle, dcc[idx].nick) || glob_admin(fr)) {
  522. #ifdef HUB
  523. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  524. #else
  525. dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
  526. if (dcc[idx].u.chat->su_nick)
  527. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  528. dprintf(idx, "\n");
  529. #endif /* HUB */
  530. }
  531. }
  532. struct user_entry_type USERENTRY_SECPASS =
  533. {
  534. 0,
  535. def_gotshare,
  536. def_unpack,
  537. #ifdef HUB
  538. def_write_userfile,
  539. #endif /* HUB */
  540. def_kill,
  541. def_get,
  542. def_set,
  543. secpass_display,
  544. "SECPASS"
  545. };
  546. static int laston_unpack(struct userrec *u, struct user_entry *e)
  547. {
  548. char *par = NULL, *arg = NULL;
  549. struct laston_info *li = NULL;
  550. par = e->u.list->extra;
  551. arg = newsplit (&par);
  552. if (!par[0])
  553. par = "???";
  554. li = calloc(1, sizeof(struct laston_info));
  555. li->laston = atoi(arg);
  556. li->lastonplace = strdup(par);
  557. list_type_kill(e->u.list);
  558. e->u.extra = li;
  559. return 1;
  560. }
  561. #ifdef HUB
  562. static int laston_write_userfile(FILE * f, struct userrec *u, struct user_entry *e)
  563. {
  564. struct laston_info *li = (struct laston_info *) e->u.extra;
  565. if (lfprintf(f, "--LASTON %lu %s\n", li->laston,
  566. li->lastonplace ? li->lastonplace : "") == EOF)
  567. return 0;
  568. return 1;
  569. }
  570. #endif /* HUB */
  571. static int laston_kill(struct user_entry *e)
  572. {
  573. if (((struct laston_info *) (e->u.extra))->lastonplace)
  574. free(((struct laston_info *) (e->u.extra))->lastonplace);
  575. free(e->u.extra);
  576. free(e);
  577. return 1;
  578. }
  579. static int laston_set(struct userrec *u, struct user_entry *e, void *buf)
  580. {
  581. struct laston_info *li = (struct laston_info *) e->u.extra;
  582. if (li != buf) {
  583. if (li) {
  584. free(li->lastonplace);
  585. free(li);
  586. }
  587. li = e->u.extra = buf;
  588. }
  589. /* donut share laston info */
  590. return 1;
  591. }
  592. struct user_entry_type USERENTRY_LASTON =
  593. {
  594. 0, /* always 0 ;) */
  595. 0,
  596. laston_unpack,
  597. #ifdef HUB
  598. laston_write_userfile,
  599. #endif /* HUB */
  600. laston_kill,
  601. def_get,
  602. laston_set,
  603. 0,
  604. "LASTON"
  605. };
  606. static int botaddr_unpack(struct userrec *u, struct user_entry *e)
  607. {
  608. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  609. struct bot_addr *bi = NULL;
  610. bi = 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 = calloc(1, 1);
  645. }
  646. list_type_kill(e->u.list);
  647. e->u.extra = bi;
  648. return 1;
  649. }
  650. static int 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 int 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 int 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 = e->u.extra = buf;
  684. }
  685. Assert(u);
  686. if (bi && !noshare) {
  687. shareout(NULL, "c BOTADDR %s %s %d %d %d %s\n",u->handle,
  688. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  689. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  690. }
  691. return 1;
  692. }
  693. #ifdef HUB
  694. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  695. {
  696. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  697. get_user_flagrec(dcc[idx].user, &fr, NULL);
  698. if (glob_admin(fr)) {
  699. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  700. if (bi->address && bi->hublevel && bi->hublevel != 0)
  701. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  702. dprintf(idx, " port: %d\n", bi->telnet_port);
  703. if (bi->hublevel && bi->hublevel != 0)
  704. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  705. if (bi->uplink && bi->uplink[0])
  706. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  707. }
  708. }
  709. #endif /* HUB */
  710. static int botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  711. {
  712. struct bot_addr *bi = NULL;
  713. char *arg = NULL;
  714. bi = calloc(1, sizeof(struct bot_addr));
  715. 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 int hosts_write_userfile(FILE *f, struct userrec *u, struct user_entry *e)
  750. {
  751. struct list_type *h = NULL;
  752. for (h = 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 int hosts_null(struct userrec *u, struct user_entry *e)
  759. {
  760. return 1;
  761. }
  762. static int 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 int hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  805. {
  806. if (!buf || !egg_strcasecmp(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 = 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 = calloc(1, sizeof(struct list_type));
  834. (*t)->next = NULL;
  835. (*t)->extra = strdup(host);
  836. }
  837. return 1;
  838. }
  839. static int 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. int 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. int 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. int 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. int 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. int set_user(struct user_entry_type *et, struct userrec *u, void *d)
  928. {
  929. struct user_entry *e = NULL;
  930. int r;
  931. if (!u || !et)
  932. return 0;
  933. if (!(e = find_user_entry(et, u))) {
  934. e = 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. }