userent.c 23 KB

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