userent.c 23 KB

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