userent.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 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. struct xtra_key *t = NULL;
  260. char *key = NULL, *data = NULL;
  261. while (curr) {
  262. t = (struct xtra_key *) my_calloc(1, sizeof(struct xtra_key));
  263. data = curr->extra;
  264. key = newsplit(&data);
  265. if (data[0]) {
  266. t->key = strdup(key);
  267. t->data = strdup(data);
  268. list_insert((struct xtra_key **) (&e->u.extra), t);
  269. } else
  270. free(t);
  271. curr = curr->next;
  272. }
  273. list_type_kill(head);
  274. return 1;
  275. }
  276. static void set_display(int idx, struct user_entry *e, struct userrec *u)
  277. {
  278. if (conf.bot->hub) {
  279. struct xtra_key *xk = (struct xtra_key *) e->u.extra;
  280. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  281. get_user_flagrec(dcc[idx].user, &fr, NULL);
  282. /* scan thru xtra field, searching for matches */
  283. for (; xk; xk = xk->next) {
  284. /* ok, it's a valid xtra field entry */
  285. if (glob_owner(fr))
  286. dprintf(idx, " %s: %s\n", xk->key, xk->data ? xk->data : "");
  287. }
  288. }
  289. }
  290. static bool set_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  291. {
  292. char *name = newsplit(&buf);
  293. if (!name || !name[0])
  294. return 1;
  295. if (!strcasecmp(u->handle, conf.bot->nick)) {
  296. set_noshare = 1;
  297. var_set_by_name(conf.bot->nick, name, buf[0] ? buf : NULL);
  298. set_noshare = 0;
  299. /* var_set_by_name() called set_user(), no need to do it again... */
  300. if (!conf.bot->hub && !strncmp(name, "servers", 7))
  301. check_removed_server();
  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. struct xtra_key *x = (struct xtra_key *) e->u.extra;
  317. for (; x; x = x->next) {
  318. /* only write if saving local, or if sending to hub, or if sending to same user as entry, or the localhub in the chain, or sending 'groups' */
  319. if (idx == -1 || dcc[idx].hub || dcc[idx].user == u || (localhub != -1 && idx == localhub) || !strcmp(x->key, "groups")) {
  320. stream << bd::String::printf("--%s %s %s\n", e->type->name, x->key, x->data ? x->data : "");
  321. }
  322. }
  323. }
  324. static bool set_kill(struct user_entry *e)
  325. {
  326. struct xtra_key *x = (struct xtra_key *) e->u.extra, *y = NULL;
  327. for (; x; x = y) {
  328. y = x->next;
  329. free(x->key);
  330. if (x->data)
  331. free(x->data);
  332. free(x);
  333. }
  334. free(e);
  335. return 1;
  336. }
  337. struct user_entry_type USERENTRY_SET = {
  338. 0,
  339. set_gotshare,
  340. set_unpack,
  341. set_write_userfile,
  342. set_kill,
  343. def_get,
  344. set_set,
  345. set_display,
  346. "SET"
  347. };
  348. static void botmisc_display(int idx, struct user_entry *e, struct userrec *u)
  349. {
  350. if (conf.bot->hub) {
  351. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  352. get_user_flagrec(dcc[idx].user, &fr, NULL);
  353. if (glob_admin(fr))
  354. dprintf(idx, " %s: %s\n", e->type->name, e->u.string ? e->u.string : "");
  355. }
  356. }
  357. struct user_entry_type USERENTRY_USERNAME = {
  358. 0,
  359. def_gotshare,
  360. def_unpack,
  361. write_userfile_protected,
  362. def_kill,
  363. def_get,
  364. set_protected,
  365. botmisc_display,
  366. "USERNAME"
  367. };
  368. struct user_entry_type USERENTRY_NODENAME = {
  369. 0,
  370. def_gotshare,
  371. def_unpack,
  372. write_userfile_protected,
  373. def_kill,
  374. def_get,
  375. set_protected,
  376. botmisc_display,
  377. "NODENAME"
  378. };
  379. struct user_entry_type USERENTRY_OS = {
  380. 0,
  381. def_gotshare,
  382. def_unpack,
  383. write_userfile_protected,
  384. def_kill,
  385. def_get,
  386. set_protected,
  387. botmisc_display,
  388. "OS"
  389. };
  390. struct user_entry_type USERENTRY_OSVER = {
  391. 0,
  392. def_gotshare,
  393. def_unpack,
  394. write_userfile_protected,
  395. def_kill,
  396. def_get,
  397. set_protected,
  398. botmisc_display,
  399. "OSVER"
  400. };
  401. struct user_entry_type USERENTRY_ARCH = {
  402. 0,
  403. def_gotshare,
  404. def_unpack,
  405. write_userfile_protected,
  406. def_kill,
  407. def_get,
  408. set_protected,
  409. botmisc_display,
  410. "ARCH"
  411. };
  412. void stats_add(struct userrec *u, int islogin, int op)
  413. {
  414. if (!u || u->bot)
  415. return;
  416. char *s = (char *) get_user(&USERENTRY_STATS, u), s2[50] = "";
  417. int sl, so;
  418. if (s) {
  419. strlcpy(s2, s, sizeof(s2));
  420. } else
  421. strlcpy(s2, "0 0", sizeof(s2));
  422. s = strchr(s2, ' ');
  423. if (s) {
  424. s++;
  425. so = atoi(s);
  426. } else
  427. so = 0;
  428. sl = atoi(s2);
  429. if (islogin)
  430. sl++;
  431. if (op)
  432. so++;
  433. simple_snprintf(s2, sizeof(s2), "%i %i", sl, so);
  434. set_user(&USERENTRY_STATS, u, s2);
  435. }
  436. char s2_8[3] = "",s1_14[3] = "",s2_1[3] = "";
  437. static void stats_display(int idx, struct user_entry *e, struct userrec *u)
  438. {
  439. /* format: logincount opcount */
  440. if (!u->bot && dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  441. char *p = strchr(e->u.string, ' ');
  442. if (p)
  443. dprintf(idx, " -- %i logins, %i ops\n", atoi(e->u.string), atoi(p));
  444. }
  445. }
  446. struct user_entry_type USERENTRY_STATS = {
  447. 0, /* always 0 ;) */
  448. def_gotshare,
  449. def_unpack,
  450. def_write_userfile,
  451. def_kill,
  452. def_get,
  453. def_set,
  454. stats_display,
  455. "STATS"
  456. };
  457. void update_mod(char *handle, char *nick, char *cmd, char *par)
  458. {
  459. char tmp[100] = "";
  460. simple_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", (long) now, nick, cmd, (par && par[0]) ? par : "");
  461. set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
  462. }
  463. static void modified_display(int idx, struct user_entry *e, struct userrec *u)
  464. {
  465. if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  466. char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
  467. time_t tm;
  468. strlcpy(tmp, e->u.string, sizeof(tmp));
  469. hnd = strchr(tmp, ' ');
  470. if (hnd)
  471. *hnd++ = 0;
  472. tm = atoi(tmp);
  473. strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  474. if (hnd)
  475. dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
  476. else
  477. dprintf(idx, " -- Modified %s\n", tmp2);
  478. }
  479. }
  480. struct user_entry_type USERENTRY_MODIFIED =
  481. {
  482. 0,
  483. def_gotshare,
  484. def_unpack,
  485. def_write_userfile,
  486. def_kill,
  487. def_get,
  488. def_set,
  489. modified_display,
  490. "MODIFIED"
  491. };
  492. static bool pass_set(struct userrec *u, struct user_entry *e, void *buf)
  493. {
  494. char *newpass = NULL;
  495. register char *pass = (char *) buf;
  496. if (e->u.extra)
  497. free(e->u.extra);
  498. if (!pass || !pass[0] || (pass[0] == '-'))
  499. e->u.extra = NULL;
  500. else {
  501. unsigned char *p = (unsigned char *) pass;
  502. if (u->bot || (pass[0] == '+'))
  503. newpass = strdup(pass);
  504. else {
  505. while (*p) {
  506. if ((*p <= 32) || (*p == 127))
  507. *p = '?';
  508. p++;
  509. }
  510. newpass = salted_sha1(pass);
  511. }
  512. e->u.extra = strdup(newpass);
  513. }
  514. if (!noshare)
  515. shareout("c %s %s %s\n", e->type->name, u->handle, newpass ? newpass : "");
  516. if (newpass)
  517. free(newpass);
  518. /* clear old records */
  519. noshare = 1;
  520. set_user(&USERENTRY_PASS1, u, NULL);
  521. noshare = 0;
  522. return 1;
  523. }
  524. struct user_entry_type USERENTRY_PASS =
  525. {
  526. 0,
  527. def_gotshare,
  528. def_unpack,
  529. def_write_userfile,
  530. def_kill,
  531. def_get,
  532. pass_set,
  533. NULL,
  534. "PASS2"
  535. };
  536. static bool pass1_set(struct userrec *u, struct user_entry *e, void *buf)
  537. {
  538. register char *pass = (char *) buf;
  539. if (e->u.extra)
  540. free(e->u.extra);
  541. if (!pass || !pass[0] || (pass[0] == '-'))
  542. e->u.extra = NULL;
  543. else {
  544. unsigned char *p = (unsigned char *) pass;
  545. while (*p) {
  546. if ((*p <= 32) || (*p == 127))
  547. *p = '?';
  548. p++;
  549. }
  550. if (u->bot || (pass[0] == '+'))
  551. e->u.extra = strdup(pass);
  552. else
  553. e->u.extra = encrypt_string(u->handle, pass);
  554. }
  555. if (!noshare)
  556. shareout("c %s %s %s\n", e->type->name, u->handle, pass ? pass : "");
  557. return 1;
  558. }
  559. struct user_entry_type USERENTRY_PASS1 =
  560. {
  561. 0,
  562. def_gotshare,
  563. def_unpack,
  564. def_write_userfile,
  565. def_kill,
  566. def_get,
  567. pass1_set,
  568. NULL,
  569. "PASS1"
  570. };
  571. static void secpass_display(int idx, struct user_entry *e, struct userrec *u)
  572. {
  573. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  574. get_user_flagrec(dcc[idx].user, &fr, NULL);
  575. if (!strcmp(u->handle, dcc[idx].nick) || (glob_admin(fr) && isowner(dcc[idx].nick))) {
  576. if (conf.bot->hub)
  577. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  578. else {
  579. dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
  580. if (dcc[idx].u.chat->su_nick)
  581. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  582. dprintf(idx, "\n");
  583. }
  584. }
  585. }
  586. struct user_entry_type USERENTRY_SECPASS =
  587. {
  588. 0,
  589. def_gotshare,
  590. def_unpack,
  591. def_write_userfile,
  592. def_kill,
  593. def_get,
  594. def_set,
  595. secpass_display,
  596. "SECPASS"
  597. };
  598. static bool laston_unpack(struct userrec *u, struct user_entry *e)
  599. {
  600. char *par = e->u.list->extra, *arg = newsplit(&par);
  601. struct laston_info *li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
  602. if (!par[0])
  603. par = "???";
  604. li->laston = atoi(arg);
  605. li->lastonplace = strdup(par);
  606. list_type_kill(e->u.list);
  607. e->u.extra = li;
  608. return 1;
  609. }
  610. static void laston_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  611. {
  612. struct laston_info *li = (struct laston_info *) e->u.extra;
  613. stream << bd::String::printf("--LASTON %li %s\n", (long) li->laston, li->lastonplace ? li->lastonplace : "");
  614. }
  615. static bool laston_kill(struct user_entry *e)
  616. {
  617. if (((struct laston_info *) (e->u.extra))->lastonplace)
  618. free(((struct laston_info *) (e->u.extra))->lastonplace);
  619. free(e->u.extra);
  620. free(e);
  621. return 1;
  622. }
  623. static bool laston_set(struct userrec *u, struct user_entry *e, void *buf)
  624. {
  625. struct laston_info *li = (struct laston_info *) e->u.extra;
  626. if (li != buf) {
  627. if (li) {
  628. free(li->lastonplace);
  629. free(li);
  630. }
  631. li = (struct laston_info *) buf;
  632. e->u.extra = (struct laston_info *) buf;
  633. }
  634. if (!noshare)
  635. shareout("c LASTON %s %s %li\n", u->handle, li->lastonplace ? li->lastonplace : "-", (long) li->laston);
  636. return 1;
  637. }
  638. static bool laston_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  639. {
  640. char *where = NULL;
  641. time_t timeval = 0;
  642. if (par[0])
  643. where = newsplit(&par);
  644. if (where && !strcmp(where, "-"))
  645. where = NULL;
  646. if (par[0])
  647. timeval = atol(newsplit(&par));
  648. touch_laston(u, where, timeval);
  649. return 1;
  650. }
  651. struct user_entry_type USERENTRY_LASTON =
  652. {
  653. 0, /* always 0 ;) */
  654. laston_gotshare,
  655. laston_unpack,
  656. laston_write_userfile,
  657. laston_kill,
  658. def_get,
  659. laston_set,
  660. 0,
  661. "LASTON"
  662. };
  663. static bool botaddr_unpack(struct userrec *u, struct user_entry *e)
  664. {
  665. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  666. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  667. /* address:port/port:hublevel:uplink */
  668. strlcpy(p, e->u.list->extra, sizeof(p));
  669. q1 = strchr(p, ':');
  670. if (q1)
  671. *q1++ = 0;
  672. bi->address = strdup(p);
  673. if (q1) {
  674. q2 = strchr(q1, ':');
  675. if (q2)
  676. *q2++ = 0;
  677. bi->telnet_port = atoi(q1);
  678. q1 = strchr(q1, '/');
  679. if (q1) {
  680. q1++;
  681. bi->relay_port = atoi(q1);
  682. }
  683. if (q2) {
  684. q1 = strchr(q2, ':');
  685. if (q1) {
  686. *q1++ = 0;
  687. bi->uplink = strdup(q1);
  688. }
  689. bi->hublevel = atoi(q2);
  690. }
  691. }
  692. if (!bi->telnet_port)
  693. bi->telnet_port = 3333;
  694. if (!bi->relay_port)
  695. bi->relay_port = bi->telnet_port;
  696. if (!bi->uplink) {
  697. bi->uplink = (char *) my_calloc(1, 1);
  698. }
  699. list_type_kill(e->u.list);
  700. e->u.extra = bi;
  701. return 1;
  702. }
  703. static bool botaddr_kill(struct user_entry *e)
  704. {
  705. free(((struct bot_addr *) (e->u.extra))->address);
  706. free(((struct bot_addr *) (e->u.extra))->uplink);
  707. free(e->u.extra);
  708. free(e);
  709. return 1;
  710. }
  711. static void botaddr_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  712. {
  713. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  714. 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);
  715. }
  716. static bool botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
  717. {
  718. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  719. if (!bi && !buf)
  720. return 1;
  721. if (bi != buf) {
  722. if (bi) {
  723. free(bi->address);
  724. free(bi->uplink);
  725. free(bi);
  726. }
  727. ContextNote("botaddr_set", "(sharebug) occurred in botaddr_set");
  728. bi = (struct bot_addr *) buf;
  729. e->u.extra = (struct bot_addr *) buf;
  730. }
  731. if (bi && !noshare) {
  732. shareout("c BOTADDR %s %s %d %d %d %s\n",u->handle,
  733. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  734. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  735. }
  736. return 1;
  737. }
  738. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  739. {
  740. if (conf.bot->hub) {
  741. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  742. get_user_flagrec(dcc[idx].user, &fr, NULL);
  743. if (glob_admin(fr)) {
  744. register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  745. if (bi->hublevel && bi->hublevel != 999) {
  746. if (bi->address) {
  747. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  748. dprintf(idx, " port: %d\n", bi->telnet_port);
  749. }
  750. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  751. }
  752. if (bi->uplink && bi->uplink[0])
  753. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  754. }
  755. }
  756. }
  757. static bool botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  758. {
  759. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  760. char *arg = newsplit(&buf);
  761. bi->address = strdup(arg);
  762. arg = newsplit(&buf);
  763. bi->telnet_port = atoi(arg);
  764. arg = newsplit(&buf);
  765. bi->relay_port = atoi(arg);
  766. arg = newsplit(&buf);
  767. bi->hublevel = atoi(arg);
  768. bi->uplink = strdup(buf);
  769. if (!bi->telnet_port)
  770. bi->telnet_port = 3333;
  771. if (!bi->relay_port)
  772. bi->relay_port = bi->telnet_port;
  773. return botaddr_set(u, e, bi);
  774. }
  775. struct user_entry_type USERENTRY_BOTADDR =
  776. {
  777. 0, /* always 0 ;) */
  778. botaddr_gotshare,
  779. botaddr_unpack,
  780. botaddr_write_userfile,
  781. botaddr_kill,
  782. def_get,
  783. botaddr_set,
  784. botaddr_display,
  785. "BOTADDR"
  786. };
  787. static void hosts_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  788. {
  789. struct list_type *h = NULL;
  790. for (h = (struct list_type *) e->u.extra; h; h = h->next)
  791. stream << bd::String::printf("--HOSTS %s\n", h->extra);
  792. }
  793. static bool hosts_null(struct userrec *u, struct user_entry *e)
  794. {
  795. return 1;
  796. }
  797. static bool hosts_kill(struct user_entry *e)
  798. {
  799. list_type_kill(e->u.list);
  800. free(e);
  801. return 1;
  802. }
  803. static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
  804. {
  805. /* if this is a su, dont show hosts
  806. * otherwise, let users see their own hosts */
  807. if (conf.bot->hub ||
  808. (!conf.bot->hub && (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)))) {
  809. char s[1024] = "";
  810. struct list_type *q = NULL;
  811. strlcpy(s, " HOSTS: ", sizeof(s));
  812. for (q = e->u.list; q; q = q->next) {
  813. if (s[0] && !s[9])
  814. strlcat(s, q->extra, sizeof(s));
  815. else if (!s[0])
  816. simple_snprintf(s, sizeof(s), " %s", q->extra);
  817. else {
  818. if (strlen(s) + strlen(q->extra) + 2 > 65) {
  819. dprintf(idx, "%s\n", s);
  820. simple_snprintf(s, sizeof(s), " %s", q->extra);
  821. } else {
  822. strlcat(s, ", ", sizeof(s));
  823. strlcat(s, q->extra, sizeof(s));
  824. }
  825. }
  826. }
  827. if (s[0])
  828. dprintf(idx, "%s\n", s);
  829. } else if (!conf.bot->hub) {
  830. dprintf(idx, " HOSTS: Hidden on leaf bots.");
  831. if (dcc[idx].u.chat->su_nick)
  832. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  833. dprintf(idx, "\n");
  834. }
  835. }
  836. static bool hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  837. {
  838. if (!buf || !strcasecmp((const char *) buf, "none")) {
  839. /* When the bot crashes, it's in this part, not in the 'else' part */
  840. list_type_kill(e->u.list);
  841. e->u.list = NULL;
  842. } else {
  843. char *host = (char *) buf, *p = strchr(host, ',');
  844. struct list_type **t;
  845. /* Can't have ,'s in hostmasks */
  846. while (p) {
  847. *p = '?';
  848. p = strchr(host, ',');
  849. }
  850. /* fred1: check for redundant hostmasks with
  851. * controversial "superpenis" algorithm ;) */
  852. /* I'm surprised Raistlin hasn't gotten involved in this controversy */
  853. t = &(e->u.list);
  854. while (*t) {
  855. if (wild_match(host, (*t)->extra)) {
  856. struct list_type *listu;
  857. listu = *t;
  858. *t = (*t)->next;
  859. if (listu->extra)
  860. free(listu->extra);
  861. free(listu);
  862. } else
  863. t = &((*t)->next);
  864. }
  865. *t = (struct list_type *) my_calloc(1, sizeof(struct list_type));
  866. (*t)->next = NULL;
  867. (*t)->extra = strdup(host);
  868. }
  869. return 1;
  870. }
  871. static bool hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  872. {
  873. /* doh, try to be too clever and it bites your butt */
  874. return 0;
  875. }
  876. struct user_entry_type USERENTRY_HOSTS =
  877. {
  878. 0,
  879. hosts_gotshare,
  880. hosts_null,
  881. hosts_write_userfile,
  882. hosts_kill,
  883. def_get,
  884. hosts_set,
  885. hosts_display,
  886. "HOSTS"
  887. };
  888. bool list_append(struct list_type **h, struct list_type *i)
  889. {
  890. for (; *h; h = &((*h)->next))
  891. ;
  892. *h = i;
  893. return 1;
  894. }
  895. bool list_delete(struct list_type **h, struct list_type *i)
  896. {
  897. for (; *h; h = &((*h)->next))
  898. if (*h == i) {
  899. *h = i->next;
  900. return 1;
  901. }
  902. return 0;
  903. }
  904. bool list_contains(struct list_type *h, struct list_type *i)
  905. {
  906. for (; h; h = h->next)
  907. if (h == i) {
  908. return 1;
  909. }
  910. return 0;
  911. }
  912. bool add_entry_type(struct user_entry_type *type)
  913. {
  914. struct userrec *u = NULL;
  915. list_insert((&entry_type_list), type);
  916. for (u = userlist; u; u = u->next) {
  917. struct user_entry *e = find_user_entry(type, u);
  918. if (e && e->name) {
  919. e->type = type;
  920. e->type->unpack(u, e);
  921. free(e->name);
  922. e->name = NULL;
  923. }
  924. }
  925. return 1;
  926. }
  927. struct user_entry_type *find_entry_type(char *name)
  928. {
  929. struct user_entry_type *p = NULL;
  930. for (p = entry_type_list; p; p = p->next) {
  931. if (!strcasecmp(name, p->name))
  932. return p;
  933. }
  934. return NULL;
  935. }
  936. struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
  937. {
  938. struct user_entry **e = NULL, *t = NULL;
  939. for (e = &(u->entries); *e; e = &((*e)->next)) {
  940. if (((*e)->type == et) || ((*e)->name && !strcasecmp((*e)->name, et->name))) {
  941. t = *e;
  942. *e = t->next;
  943. t->next = u->entries;
  944. u->entries = t;
  945. return t;
  946. }
  947. }
  948. return NULL;
  949. }
  950. void *get_user(struct user_entry_type *et, struct userrec *u)
  951. {
  952. struct user_entry *e = NULL;
  953. if (u && (e = find_user_entry(et, u)))
  954. return et->get(u, e);
  955. return NULL;
  956. }
  957. bool set_user(struct user_entry_type *et, struct userrec *u, void *d)
  958. {
  959. if (!u || !et)
  960. return 0;
  961. struct user_entry *e = NULL;
  962. bool r;
  963. if (!(e = find_user_entry(et, u))) {
  964. e = (struct user_entry *) my_calloc(1, sizeof(struct user_entry));
  965. e->type = et;
  966. e->name = NULL;
  967. e->u.list = NULL;
  968. list_insert((&(u->entries)), e);
  969. }
  970. r = et->set(u, e, d);
  971. if (!e->u.list) {
  972. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  973. free(e);
  974. }
  975. return r;
  976. }
  977. /* vim: set sts=2 sw=2 ts=8 et: */