userent.c 25 KB

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