userent.c 26 KB

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