users.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 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. * users.c -- handles:
  22. * testing and enforcing ignores
  23. * adding and removing ignores
  24. * listing ignores
  25. * auto-linking bots
  26. * sending and receiving a userfile from a bot
  27. * listing users ('.whois' and '.match')
  28. * reading the user file
  29. *
  30. * dprintf'ized, 9nov1995
  31. *
  32. */
  33. #include "common.h"
  34. #include "users.h"
  35. #include "rfc1459.h"
  36. #include "src/mod/share.mod/share.h"
  37. #include "dcc.h"
  38. #include "settings.h"
  39. #include "userrec.h"
  40. #include "misc.h"
  41. #include "set.h"
  42. #include "match.h"
  43. #include "main.h"
  44. #include "chanprog.h"
  45. #include "dccutil.h"
  46. #include "crypt.h"
  47. #include "botnet.h"
  48. #include "chan.h"
  49. #include "tandem.h"
  50. #include "src/mod/channels.mod/channels.h"
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include "misc_file.h"
  54. char userfile[121] = ""; /* where the user records are stored */
  55. interval_t ignore_time = 10; /* how many minutes will ignores last? */
  56. bool dont_restructure = 0; /* set when we botlink() to a hub with +U, only stops bot from restructuring */
  57. /* is this nick!user@host being ignored? */
  58. bool match_ignore(char *uhost)
  59. {
  60. for (struct igrec *ir = global_ign; ir; ir = ir->next)
  61. if (wild_match(ir->igmask, uhost))
  62. return 1;
  63. return 0;
  64. }
  65. int equals_ignore(char *uhost)
  66. {
  67. struct igrec *u = global_ign;
  68. for (; u; u = u->next)
  69. if (!rfc_casecmp(u->igmask, uhost)) {
  70. if (u->flags & IGREC_PERM)
  71. return 2;
  72. else
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. char *delignore(char *ign)
  78. {
  79. int i = 0, j;
  80. struct igrec **u = NULL, *t = NULL;
  81. static char temp[256] = "";
  82. if (!strchr(ign, '!') && (j = atoi(ign))) {
  83. for (u = &global_ign, j--; *u && j; u = &((*u)->next), j--);
  84. if (*u) {
  85. strlcpy(temp, (*u)->igmask, sizeof temp);
  86. i = 1;
  87. }
  88. } else {
  89. /* find the matching host, if there is one */
  90. for (u = &global_ign; *u && !i; u = &((*u)->next))
  91. if (!rfc_casecmp(ign, (*u)->igmask)) {
  92. strlcpy(temp, ign, sizeof temp);
  93. i = 1;
  94. break;
  95. }
  96. }
  97. if (i) {
  98. if (!noshare) {
  99. char *mask = str_escape(temp, ':', '\\');
  100. if (mask) {
  101. shareout("-i %s\n", mask);
  102. free(mask);
  103. }
  104. }
  105. free((*u)->igmask);
  106. if ((*u)->msg)
  107. free((*u)->msg);
  108. if ((*u)->user)
  109. free((*u)->user);
  110. t = *u;
  111. *u = (*u)->next;
  112. free(t);
  113. } else
  114. temp[0] = 0;
  115. return temp;
  116. }
  117. void addignore(char *ign, char *from, const char *mnote, time_t expire_time)
  118. {
  119. struct igrec *p = NULL, *l = NULL;
  120. for (l = global_ign; l; l = l->next)
  121. if (!rfc_casecmp(l->igmask, ign)) {
  122. p = l;
  123. break;
  124. }
  125. if (p == NULL) {
  126. p = (struct igrec *) my_calloc(1, sizeof(struct igrec));
  127. p->next = global_ign;
  128. global_ign = p;
  129. } else {
  130. free(p->igmask);
  131. free(p->user);
  132. free(p->msg);
  133. }
  134. p->expire = expire_time;
  135. p->added = now;
  136. p->flags = expire_time ? 0 : IGREC_PERM;
  137. p->igmask = strdup(ign);
  138. p->user = strdup(from);
  139. p->msg = strdup(mnote);
  140. if (!noshare) {
  141. char *mask = str_escape(ign, ':', '\\');
  142. if (mask) {
  143. shareout("+i %s %d %c %s %s\n", mask, (int) (expire_time - now), (p->flags & IGREC_PERM) ? 'p' : '-', from, mnote);
  144. free(mask);
  145. }
  146. }
  147. }
  148. /* take host entry from ignore list and display it ignore-style */
  149. void display_ignore(int idx, int number, struct igrec *ignore)
  150. {
  151. char dates[81] = "", s[41] = "";
  152. if (ignore->added) {
  153. daysago(now, ignore->added, s);
  154. simple_snprintf(dates, sizeof(dates), "Started %s", s);
  155. }
  156. if (ignore->flags & IGREC_PERM)
  157. strcpy(s, "(perm)");
  158. else {
  159. char s1[41] = "";
  160. days(ignore->expire, now, s1);
  161. simple_snprintf(s, sizeof(s), "(expires %s)", s1);
  162. }
  163. if (number >= 0)
  164. dprintf(idx, " [%3d] %s %s\n", number, ignore->igmask, s);
  165. else
  166. dprintf(idx, "IGNORE: %s %s\n", ignore->igmask, s);
  167. if (ignore->msg && ignore->msg[0])
  168. dprintf(idx, " %s: %s\n", ignore->user, ignore->msg);
  169. else
  170. dprintf(idx, " placed by %s\n", ignore->user);
  171. if (dates[0])
  172. dprintf(idx, " %s\n", dates);
  173. }
  174. /* list the ignores and how long they've been active */
  175. void tell_ignores(int idx, char *match)
  176. {
  177. struct igrec *u = global_ign;
  178. if (u == NULL) {
  179. dprintf(idx, "No ignores.\n");
  180. return;
  181. }
  182. dprintf(idx, "%s:\n", "Currently ignoring");
  183. int k = 1;
  184. for (; u; u = u->next) {
  185. if (match[0]) {
  186. if (wild_match(match, u->igmask) ||
  187. wild_match(match, u->msg) ||
  188. wild_match(match, u->user))
  189. display_ignore(idx, k, u);
  190. k++;
  191. } else
  192. display_ignore(idx, k++, u);
  193. }
  194. }
  195. /* check for expired timed-ignores */
  196. void check_expired_ignores()
  197. {
  198. struct igrec **u = &global_ign;
  199. if (!*u)
  200. return;
  201. while (*u) {
  202. if (!((*u)->flags & IGREC_PERM) && (now >= (*u)->expire)) {
  203. putlog(LOG_MISC, "*", "%s %s (expired)", "No longer ignoring", (*u)->igmask);
  204. delignore((*u)->igmask);
  205. } else {
  206. u = &((*u)->next);
  207. }
  208. }
  209. }
  210. /* Channel mask loaded from user file. This function is
  211. * add(ban|invite|exempt)_fully merged into one. <cybah>
  212. */
  213. static void addmask_fully(struct chanset_t *chan, maskrec **m, maskrec **global,
  214. char *mask, char *from,
  215. char *note, time_t expire_time, int flags,
  216. time_t added, time_t last)
  217. {
  218. maskrec *p = (maskrec *) my_calloc(1, sizeof(maskrec));
  219. maskrec **u = (chan) ? m : global;
  220. p->next = *u;
  221. *u = p;
  222. p->expire = expire_time;
  223. p->added = added;
  224. p->lastactive = last;
  225. p->flags = flags;
  226. p->mask = strdup(mask);
  227. p->user = strdup(from);
  228. p->desc = strdup(note);
  229. }
  230. static void restore_chanmask(const char type, struct chanset_t *chan, char *host)
  231. {
  232. char *expi = NULL, *add = NULL, *last = NULL, *user = NULL, *desc = NULL;
  233. int flags = 0;
  234. maskrec **chan_masks = NULL, **global_masks = NULL;
  235. const char *str_type = (type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite");
  236. if (type == 'b') {
  237. if (chan) chan_masks = &chan->bans;
  238. global_masks = &global_bans;
  239. } else if (type == 'e') {
  240. if (chan) chan_masks = &chan->exempts;
  241. global_masks = &global_exempts;
  242. } else if (type == 'I') {
  243. if (chan) chan_masks = &chan->invites;
  244. global_masks = &global_invites;
  245. }
  246. expi = strchr_unescape(host, ':', '\\');
  247. if (expi) {
  248. if (*expi == '+') {
  249. flags |= MASKREC_PERM;
  250. expi++;
  251. }
  252. add = strchr(expi, ':');
  253. if (add) {
  254. if (add[-1] == '*') {
  255. flags |= MASKREC_STICKY;
  256. add[-1] = 0;
  257. } else
  258. *add = 0;
  259. add++;
  260. if (*add == '+') {
  261. last = strchr(add, ':');
  262. if (last) {
  263. *last = 0;
  264. last++;
  265. user = strchr(last, ':');
  266. if (user) {
  267. *user = 0;
  268. user++;
  269. desc = strchr(user, ':');
  270. if (desc) {
  271. *desc = 0;
  272. desc++;
  273. addmask_fully(chan, chan_masks, global_masks, host, user,
  274. desc, atoi(expi), flags, atoi(add), atoi(last));
  275. return;
  276. }
  277. }
  278. }
  279. } else {
  280. desc = strchr(add, ':');
  281. if (desc) {
  282. *desc = 0;
  283. desc++;
  284. addmask_fully(chan, chan_masks, global_masks, host, add, desc,
  285. atoi(expi), flags, now, 0);
  286. return;
  287. }
  288. }
  289. }
  290. }
  291. putlog(LOG_MISC, "*", "*** Malformed %sline for %s%s%s.", str_type, chan ? chan->dname : "global_", chan ? "" : str_type, chan ? "" : "s");
  292. }
  293. static void restore_ignore(char *host)
  294. {
  295. char *expi = NULL, *user = NULL, *added = NULL, *desc = NULL;
  296. int flags = 0;
  297. struct igrec *p = NULL;
  298. expi = strchr_unescape(host, ':', '\\');
  299. if (expi) {
  300. if (*expi == '+') {
  301. flags |= IGREC_PERM;
  302. expi++;
  303. }
  304. user = strchr(expi, ':');
  305. if (user) {
  306. *user = 0;
  307. user++;
  308. added = strchr(user, ':');
  309. if (added) {
  310. *added = 0;
  311. added++;
  312. desc = strchr(added, ':');
  313. if (desc) {
  314. *desc = 0;
  315. desc++;
  316. } else
  317. desc = NULL;
  318. } else {
  319. added = "0";
  320. desc = NULL;
  321. }
  322. p = (struct igrec *) my_calloc(1, sizeof(struct igrec));
  323. p->next = global_ign;
  324. global_ign = p;
  325. p->expire = atoi(expi);
  326. p->added = atoi(added);
  327. p->flags = flags;
  328. p->igmask = strdup(host);
  329. p->user = strdup(user);
  330. if (desc) {
  331. p->msg = strdup(desc);
  332. } else
  333. p->msg = NULL;
  334. return;
  335. }
  336. }
  337. putlog(LOG_MISC, "*", "*** Malformed ignore line.");
  338. }
  339. static void
  340. tell_user(int idx, struct userrec *u)
  341. {
  342. char s[81] = "", s1[81] = "", format[81] = "";
  343. time_t now2;
  344. struct chanuserrec *ch = NULL;
  345. struct chanset_t *chan = NULL;
  346. struct user_entry *ue = NULL;
  347. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  348. struct flag_record fr = {FR_GLOBAL, u->flags, 0, 0 };
  349. build_flags(s, &fr, NULL);
  350. if (!li || !li->laston)
  351. strcpy(s1, "never");
  352. else {
  353. now2 = now - li->laston;
  354. if (now2 > 86400)
  355. egg_strftime(s1, 7, "%d %b", gmtime(&li->laston));
  356. else
  357. egg_strftime(s1, 6, "%H:%M", gmtime(&li->laston));
  358. }
  359. if (!u->bot) {
  360. egg_snprintf(format, sizeof format, "%%-%us %%-5s %%-15s %%s (%%-10.10s)\n", HANDLEN);
  361. dprintf(idx, format, u->handle, get_user(&USERENTRY_PASS, u) ? "yes" : "no", s, s1, (li && li->lastonplace) ? li->lastonplace : "nowhere");
  362. } else { /* BOT */
  363. egg_snprintf(format, sizeof format, "%%-%us %%-8s %%s (%%-10.10s)\n", HANDLEN);
  364. dprintf(idx, format, u->handle, s, s1, (li && li->lastonplace) ? li->lastonplace : "nowhere");
  365. }
  366. /* channel flags? */
  367. for (ch = u->chanrec; ch; ch = ch->next) {
  368. fr.match = FR_CHAN | FR_GLOBAL;
  369. chan = findchan_by_dname(ch->channel);
  370. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  371. if (!channel_privchan(chan) || (channel_privchan(chan) && (chan_op(fr) || glob_owner(fr)))) {
  372. if (glob_op(fr) || chan_op(fr)) {
  373. if (ch->laston == 0L)
  374. strcpy(s1, "never");
  375. else {
  376. now2 = now - (ch->laston);
  377. if (now2 > 86400)
  378. egg_strftime(s1, 7, "%d %b", gmtime(&ch->laston));
  379. else
  380. egg_strftime(s1, 6, "%H:%M", gmtime(&ch->laston));
  381. }
  382. fr.match = FR_CHAN;
  383. fr.chan = ch->flags;
  384. build_flags(s, &fr, NULL);
  385. egg_snprintf(format, sizeof format, "%%%us %%-18s %%-15s %%s\n", HANDLEN-9);
  386. dprintf(idx, format, " ", ch->channel, s, s1);
  387. if (ch->info != NULL)
  388. dprintf(idx, " INFO: %s\n", ch->info);
  389. }
  390. }
  391. }
  392. /* user-defined extra fields */
  393. for (ue = u->entries; ue; ue = ue->next)
  394. if (!ue->name && ue->type->display)
  395. ue->type->display(idx, ue, u);
  396. }
  397. /* show user by ident */
  398. void tell_user_ident(int idx, char *id)
  399. {
  400. struct userrec *u = get_user_by_handle(userlist, id);
  401. struct flag_record user = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  402. get_user_flagrec(dcc[idx].user, &user, NULL);
  403. if (u == NULL)
  404. u = get_user_by_host(id);
  405. if (u == NULL || (u && !whois_access(dcc[idx].user, u))) {
  406. dprintf(idx, "Can't find anyone matching that.\n");
  407. return;
  408. }
  409. char format[81] = "";
  410. if (u->bot) {
  411. egg_snprintf(format, sizeof format, "%%-%us FLAGS LAST\n", HANDLEN);
  412. dprintf(idx, format, "BOTNICK");
  413. } else {
  414. egg_snprintf(format, sizeof format, "%%-%us PASS FLAGS LAST\n", HANDLEN);
  415. dprintf(idx, format, "HANDLE");
  416. }
  417. tell_user(idx, u);
  418. }
  419. /* match string:
  420. * wildcard to match nickname or hostmasks
  421. * +attr to find all with attr */
  422. void tell_users_match(int idx, char *mtch, int start, int limit, char *chname, int isbot)
  423. {
  424. char format[81] = "";
  425. struct userrec *u = NULL;
  426. int fnd = 0, cnt = 0, nomns = 0, flags = 0;
  427. struct list_type *q = NULL;
  428. struct flag_record user, pls, mns;
  429. dprintf(idx, "*** Matching '%s':\n", mtch);
  430. if (isbot) {
  431. egg_snprintf(format, sizeof format, "%%-%us FLAGS LAST\n", HANDLEN);
  432. dprintf(idx, format, "BOTNICK");
  433. } else {
  434. egg_snprintf(format, sizeof format, "%%-%us PASS FLAGS LAST\n", HANDLEN);
  435. dprintf(idx, format, "HANDLE");
  436. }
  437. if (start > 1)
  438. dprintf(idx, "(skipping first %d)\n", start - 1);
  439. if (strchr("+-&|", *mtch)) {
  440. user.match = pls.match = FR_GLOBAL | FR_CHAN;
  441. if (isbot) {
  442. user.match |= FR_BOT;
  443. pls.match |= FR_BOT;
  444. }
  445. break_down_flags(mtch, &pls, &mns);
  446. mns.match = pls.match ^ (FR_AND | FR_OR);
  447. if (!mns.global && !mns.chan) {
  448. nomns = 1;
  449. if (!pls.global && !pls.chan) {
  450. /* happy now BB you weenie :P */
  451. dprintf(idx, "Unknown flag specified for matching!!\n");
  452. return;
  453. }
  454. }
  455. if (!chname || !chname[0])
  456. chname = dcc[idx].u.chat->con_chan;
  457. flags = 1;
  458. }
  459. for (u = userlist; u; u = u->next) {
  460. if (!whois_access(dcc[idx].user, u) || (isbot && !u->bot) || (!isbot && u->bot)) {
  461. continue;
  462. } else if (flags) {
  463. get_user_flagrec(u, &user, chname);
  464. if (flagrec_eq(&pls, &user)) {
  465. if (nomns || !flagrec_eq(&mns, &user)) {
  466. cnt++;
  467. if ((cnt <= limit) && (cnt >= start))
  468. tell_user(idx, u);
  469. if (cnt == limit + 1)
  470. dprintf(idx, "(more than %d matches; list truncated)\n", limit);
  471. }
  472. }
  473. } else if (wild_match(mtch, u->handle)) {
  474. cnt++;
  475. if ((cnt <= limit) && (cnt >= start))
  476. tell_user(idx, u);
  477. if (cnt == limit + 1)
  478. dprintf(idx, "(more than %d matches; list truncated)\n", limit);
  479. } else {
  480. fnd = 0;
  481. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next) {
  482. if ((wild_match(mtch, q->extra)) && (!fnd)) {
  483. cnt++;
  484. fnd = 1;
  485. if ((cnt <= limit) && (cnt >= start)) {
  486. tell_user(idx, u);
  487. }
  488. if (cnt == limit + 1)
  489. dprintf(idx, "(more than %d matches; list truncated)\n", limit);
  490. }
  491. }
  492. }
  493. }
  494. dprintf(idx, "--- Found %d match%s.\n", cnt, cnt == 1 ? "" : "es");
  495. }
  496. void backup_userfile()
  497. {
  498. char s[DIRMAX] = "", s2[DIRMAX] = "";
  499. putlog(LOG_MISC, "*", "Backing up user file...");
  500. simple_snprintf(s, sizeof s, "%s/.u.0", conf.datadir);
  501. simple_snprintf(s2, sizeof s2, "%s/.u.1", conf.datadir);
  502. movefile(s, s2);
  503. copyfile(userfile, s);
  504. }
  505. /*
  506. * tagged lines in the user file:
  507. * * OLD:
  508. * # (comment)
  509. * ; (comment)
  510. * - hostmask(s)
  511. * + email
  512. * * dcc directory
  513. * = comment
  514. * : info line
  515. * . xtra (Tcl)
  516. * ! channel-specific
  517. * !! global laston
  518. * :: channel-specific bans
  519. * NEW:
  520. * *ban global bans
  521. * *ignore global ignores
  522. * ::#chan channel bans
  523. * - entries in each
  524. * + denotes tcl command
  525. * <handle> begin user entry
  526. * --KEY INFO - info on each
  527. * NEWER:
  528. * % exemptmask(s)
  529. * @ Invitemask(s)
  530. * *exempt global exempts
  531. * *Invite global Invites
  532. * && channel-specific exempts
  533. * &&#chan channel exempts
  534. * $$ channel-specific Invites
  535. * $$#chan channel Invites
  536. */
  537. int readuserfile(const char *file, struct userrec **ret)
  538. {
  539. char *p = NULL, buf[1024] = "", lasthand[512] = "", *attr = NULL, *pass = NULL;
  540. char *code = NULL, s1[1024] = "", *s = buf, cbuf[1024] = "", *temps = NULL, ignored[512] = "";
  541. FILE *f = NULL;
  542. struct userrec *bu = NULL, *u = NULL;
  543. struct chanset_t *cst = NULL;
  544. struct flag_record fr;
  545. struct chanuserrec *cr = NULL;
  546. int i, line = 0;
  547. bu = (*ret);
  548. if (bu == userlist) {
  549. clear_chanlist();
  550. lastuser = NULL;
  551. global_bans = NULL;
  552. global_ign = NULL;
  553. global_exempts = NULL;
  554. global_invites = NULL;
  555. }
  556. f = fopen(file, "r");
  557. if (f == NULL)
  558. return 0;
  559. noshare = 1;
  560. /* read opening comment */
  561. fgets(cbuf, 180, f);
  562. remove_crlf(cbuf);
  563. temps = (char *) decrypt_string(settings.salt1, cbuf);
  564. simple_snprintf(s, 180, "%s", temps);
  565. free(temps);
  566. if (s[1] < '4') {
  567. putlog(LOG_MISC, "*", "!*! Empty or malformed userfile.");
  568. return 0;
  569. }
  570. if (s[1] > '4') {
  571. putlog(LOG_MISC, "*", "Invalid userfile format.");
  572. return 0;
  573. }
  574. while (!feof(f)) {
  575. s = buf;
  576. fgets(cbuf, 1024, f);
  577. remove_crlf(cbuf);
  578. temps = (char *) decrypt_string(settings.salt1, cbuf);
  579. simple_snprintf(s, 1024, "%s", temps);
  580. free(temps);
  581. if (!feof(f)) {
  582. line++;
  583. if (s[0] != '#' && s[0] != ';' && s[0]) {
  584. code = newsplit(&s);
  585. rmspace(s);
  586. if (!strcmp(code, "-")) { /* ignores/bans */
  587. if (!lasthand[0])
  588. continue; /* Skip this entry. */
  589. if (u) { /* only break it down if there a real users */
  590. p = strchr(s, ',');
  591. while (p != NULL) {
  592. splitc(s1, s, ',');
  593. rmspace(s1);
  594. if (s1[0])
  595. set_user(&USERENTRY_HOSTS, u, s1);
  596. p = strchr(s, ',');
  597. }
  598. }
  599. /* channel bans are never stacked with , */
  600. if (s[0]) {
  601. if (lasthand[0] && strchr(CHANMETA, lasthand[0]) != NULL)
  602. restore_chanmask('b', cst, s);
  603. else if (lasthand[0] == '*') {
  604. if (lasthand[1] == IGNORE_NAME[1])
  605. restore_ignore(s);
  606. else if (lasthand[1] == SET_NAME[1]) {
  607. set_cmd_pass(s, 0); /* no need to share here, if we have a new userfile
  608. * then leaf bots under us also get the new userfile */
  609. }
  610. else
  611. restore_chanmask('b', NULL, s);
  612. } else if (lasthand[0])
  613. set_user(&USERENTRY_HOSTS, u, s);
  614. }
  615. } else if (!strcmp(code, "%")) { /* exemptmasks */
  616. if (!lasthand[0])
  617. continue; /* Skip this entry. */
  618. if (s[0]) {
  619. if (lasthand[0] == '#' || lasthand[0] == '+')
  620. restore_chanmask('e', cst, s);
  621. else if (lasthand[0] == '*')
  622. if (lasthand[1] == EXEMPT_NAME[1])
  623. restore_chanmask('e', NULL, s);
  624. }
  625. } else if (!strcmp(code, "@")) { /* Invitemasks */
  626. if (!lasthand[0])
  627. continue; /* Skip this entry. */
  628. if (s[0]) {
  629. if (lasthand[0] == '#' || lasthand[0] == '+') {
  630. restore_chanmask('I', cst, s);
  631. } else if (lasthand[0] == '*') {
  632. if (lasthand[1] == INVITE_NAME[1]) {
  633. restore_chanmask('I', NULL, s);
  634. } else if (lasthand[1] == SET_NAME[1]) {
  635. var_userfile_share_line(s, -1, 0);
  636. }
  637. }
  638. }
  639. } else if (!strcmp(code, "!")) { /* user channel record */
  640. /* ! #chan laston flags [info] */
  641. char *chname = NULL, *st = NULL, *fl = NULL;
  642. if (u) {
  643. chname = newsplit(&s);
  644. st = newsplit(&s);
  645. fl = newsplit(&s);
  646. rmspace(s);
  647. fr.match = FR_CHAN;
  648. if (u->bot)
  649. fr.match |= FR_BOT;
  650. break_down_flags(fl, &fr, 0);
  651. if (findchan_by_dname(chname)) {
  652. for (cr = u->chanrec; cr; cr = cr->next)
  653. if (!rfc_casecmp(cr->channel, chname))
  654. break;
  655. if (!cr) {
  656. cr = (struct chanuserrec *) my_calloc(1, sizeof(struct chanuserrec));
  657. cr->next = u->chanrec;
  658. u->chanrec = cr;
  659. strlcpy(cr->channel, chname, 80);
  660. cr->laston = atoi(st);
  661. cr->flags = fr.chan;
  662. if (s[0]) {
  663. cr->info = strdup(s);
  664. } else
  665. cr->info = NULL;
  666. }
  667. }
  668. }
  669. } else if (!strcmp(code, "+")) { /* add channel record */
  670. if (s[0] && lasthand[0] == '*' && lasthand[1] == CHANS_NAME[1]) {
  671. char *options = NULL, *chan = NULL, *my_ptr = NULL;
  672. char resultbuf[RESULT_LEN] = "";
  673. options = my_ptr = strdup(s);
  674. newsplit(&options);
  675. newsplit(&options);
  676. chan = newsplit(&options);
  677. /* hack to remove { } */
  678. newsplit(&options);
  679. options[strlen(options) - 1] = 0;
  680. if (channel_add(resultbuf, chan, options) != OK) {
  681. putlog(LOG_MISC, "*", "Channel parsing error in userfile on line %d", line);
  682. free(my_ptr);
  683. fclose(f);
  684. noshare = 0;
  685. return 0;
  686. }
  687. free(my_ptr);
  688. }
  689. } else if (!strncmp(code, "::", 2)) { /* channel-specific bans */
  690. strcpy(lasthand, &code[2]);
  691. u = NULL;
  692. if (!findchan_by_dname(lasthand)) {
  693. strcpy(s1, lasthand);
  694. strcat(s1, " ");
  695. if (strstr(ignored, s1) == NULL) {
  696. strcat(ignored, lasthand);
  697. strcat(ignored, " ");
  698. }
  699. lasthand[0] = 0;
  700. } else {
  701. /* Remove all bans for this channel to avoid dupes */
  702. /* NOTE only remove bans for when getting a userfile
  703. * from another bot & that channel is shared */
  704. cst = findchan_by_dname(lasthand);
  705. clear_masks(cst->bans);
  706. cst->bans = NULL;
  707. }
  708. } else if (!strncmp(code, "&&", 2)) { /* channel-specific exempts */
  709. strcpy(lasthand, &code[2]);
  710. u = NULL;
  711. if (!findchan_by_dname(lasthand)) {
  712. strcpy(s1, lasthand);
  713. strcat(s1, " ");
  714. if (strstr(ignored, s1) == NULL) {
  715. strcat(ignored, lasthand);
  716. strcat(ignored, " ");
  717. }
  718. lasthand[0] = 0;
  719. } else {
  720. /* Remove all exempts for this channel to avoid dupes */
  721. /* NOTE only remove exempts for when getting a userfile
  722. * from another bot & that channel is shared */
  723. cst = findchan_by_dname(lasthand);
  724. clear_masks(cst->exempts);
  725. cst->exempts = NULL;
  726. }
  727. } else if (!strncmp(code, "$$", 2)) { /* channel-specific invites */
  728. strcpy(lasthand, &code[2]);
  729. u = NULL;
  730. if (!findchan_by_dname(lasthand)) {
  731. strcpy(s1, lasthand);
  732. strcat(s1, " ");
  733. if (strstr(ignored, s1) == NULL) {
  734. strcat(ignored, lasthand);
  735. strcat(ignored, " ");
  736. }
  737. lasthand[0] = 0;
  738. } else {
  739. /* Remove all invites for this channel to avoid dupes */
  740. /* NOTE only remove invites for when getting a userfile
  741. * from another bot & that channel is shared */
  742. cst = findchan_by_dname(lasthand);
  743. clear_masks(cst->invites);
  744. cst->invites = NULL;
  745. }
  746. } else if (!strncmp(code, "--", 2)) { /* user USERENTRY */
  747. if (u) {
  748. /* new format storage */
  749. struct user_entry *ue = NULL;
  750. int ok = 0;
  751. for (ue = u->entries; ue && !ok; ue = ue->next)
  752. if (ue->name && !egg_strcasecmp(code + 2, ue->name)) {
  753. struct list_type *list = NULL;
  754. list = (struct list_type *) my_calloc(1, sizeof(struct list_type));
  755. list->next = NULL;
  756. list->extra = strdup(s);
  757. list_append((&ue->u.list), list);
  758. ok = 1;
  759. }
  760. /* if we don't have the entry, make it */
  761. if (!ok) {
  762. ue = (struct user_entry *) my_calloc(1, sizeof(struct user_entry));
  763. // ue->name = (char *) my_calloc(1, strlen(code + 1));
  764. ue->name = strdup(code + 2);
  765. ue->type = NULL;
  766. // strcpy(ue->name, code + 2);
  767. ue->u.list = (struct list_type *) my_calloc(1, sizeof(struct list_type));
  768. ue->u.list->next = NULL;
  769. ue->u.list->extra = strdup(s);
  770. list_insert((&u->entries), ue);
  771. }
  772. }
  773. } else if (!rfc_casecmp(code, BAN_NAME)) {
  774. strcpy(lasthand, code);
  775. u = NULL;
  776. } else if (!rfc_casecmp(code, IGNORE_NAME)) {
  777. strcpy(lasthand, code);
  778. u = NULL;
  779. } else if (!rfc_casecmp(code, EXEMPT_NAME)) {
  780. strcpy(lasthand, code);
  781. u = NULL;
  782. } else if (!rfc_casecmp(code, INVITE_NAME)) {
  783. strcpy(lasthand, code);
  784. u = NULL;
  785. } else if (!rfc_casecmp(code, CHANS_NAME)) {
  786. strcpy(lasthand, code);
  787. u = NULL;
  788. } else if (!rfc_casecmp(code, SET_NAME)) {
  789. strcpy(lasthand, code);
  790. u = NULL;
  791. } else if (code[0] == '*') {
  792. lasthand[0] = 0;
  793. u = NULL;
  794. } else { /* its a user ! */
  795. pass = newsplit(&s); /* old style passwords */
  796. attr = newsplit(&s);
  797. rmspace(s);
  798. if (!attr[0] || !pass[0]) {
  799. putlog(LOG_MISC, "*", "* Corrupt user record line: %d!", line);
  800. lasthand[0] = 0;
  801. fclose(f);
  802. noshare = 0;
  803. return 0;
  804. } else {
  805. int isbot = 0;
  806. if (code[0] == '-') {
  807. code++;
  808. isbot++;
  809. }
  810. u = get_user_by_handle(bu, code);
  811. if (u) {
  812. putlog(LOG_ERROR, "@", "* Duplicate user record '%s'!", code);
  813. lasthand[0] = 0;
  814. u = NULL;
  815. } else {
  816. fr.match = FR_GLOBAL;
  817. if (isbot)
  818. fr.match |= FR_BOT;
  819. break_down_flags(attr, &fr, 0);
  820. strcpy(lasthand, code);
  821. cst = NULL;
  822. if (strlen(code) > HANDLEN)
  823. code[HANDLEN] = 0;
  824. if (strlen(pass) > 20) { /* old style passwords */
  825. putlog(LOG_MISC, "*", "* Corrupted password reset for '%s'", code);
  826. strcpy(pass, "-");
  827. }
  828. bu = adduser(bu, code, 0, pass, sanity_check(fr.global, isbot), isbot);
  829. u = get_user_by_handle(bu, code);
  830. for (i = 0; i < dcc_total; i++)
  831. if (dcc[i].type && !egg_strcasecmp(code, dcc[i].nick))
  832. dcc[i].user = u;
  833. if (!egg_strcasecmp(code, conf.bot->nick))
  834. conf.bot->u = u;
  835. /* if s starts with '/' it's got file info */
  836. }
  837. }
  838. }
  839. }
  840. }
  841. }
  842. fclose(f);
  843. (*ret) = bu;
  844. if (ignored[0]) {
  845. putlog(LOG_MISC, "*", "Ignored masks for channel(s): %s", ignored);
  846. }
  847. putlog(LOG_MISC, "*", "Userfile loaded, unpacking...");
  848. for (u = bu; u; u = u->next) {
  849. struct user_entry *e = NULL;
  850. if (!u->bot && !egg_strcasecmp (u->handle, conf.bot->nick)) {
  851. putlog(LOG_MISC, "*", "(!) I have a user record, but am not classified as a BOT!");
  852. u->bot = 1;
  853. }
  854. for (e = u->entries; e; e = e->next)
  855. if (e->name) {
  856. struct user_entry_type *uet = find_entry_type(e->name);
  857. if (uet) {
  858. e->type = uet;
  859. uet->unpack(u, e);
  860. free(e->name);
  861. e->name = NULL;
  862. } else
  863. sdprintf("FAILED TO UNPACK '%s'", e->name);
  864. }
  865. }
  866. /* process the user data *now* */
  867. if (!conf.bot->hub)
  868. unlink(userfile);
  869. noshare = 0;
  870. return 1;
  871. }
  872. void link_pref_val(struct userrec *u, char *val)
  873. {
  874. /* val must be HANDLEN + 4 chars minimum */
  875. val[0] = 'Z';
  876. val[1] = 0;
  877. if (!u)
  878. return;
  879. if (!u->bot)
  880. return;
  881. struct bot_addr *ba = NULL;
  882. if (!(ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u))) {
  883. return;
  884. }
  885. if (!ba->hublevel || ba->hublevel == 999) {
  886. return;
  887. }
  888. sprintf(val, "%02d%s", ba->hublevel, u->handle);
  889. }
  890. /*
  891. starting at "current" or "userlist" if NULL, find next bot with a
  892. link_pref_val higher than "lowval" and lower than "highval"
  893. If none found return bot with best overall link_pref_val
  894. If still not found return NULL
  895. */
  896. struct userrec *next_hub(struct userrec *current, char *lowval, char *highval)
  897. {
  898. char thisval[NICKLEN + 4] = "", bestmatchval[NICKLEN + 4] = "z", bestallval[NICKLEN + 4] = "z";
  899. struct userrec *cur = NULL, *bestmatch = NULL, *bestall = NULL;
  900. if (current)
  901. cur = current->next;
  902. else
  903. cur = userlist;
  904. while (cur != current) {
  905. if (!cur)
  906. cur = userlist;
  907. if (cur == current)
  908. break;
  909. if (cur->bot && (egg_strcasecmp(cur->handle, conf.bot->nick))) {
  910. link_pref_val(cur, thisval);
  911. if ((strcmp(thisval, lowval) < 0) && (strcmp(thisval, highval) > 0) &&(strcmp(thisval, bestmatchval) < 0)) {
  912. strcpy(bestmatchval, thisval);
  913. bestmatch = cur;
  914. }
  915. if ((strcmp(thisval, lowval) < 0)
  916. && (strcmp(thisval, bestallval) < 0)) {
  917. strcpy(bestallval, thisval);
  918. bestall = cur;
  919. }
  920. }
  921. cur = cur->next;
  922. }
  923. if (bestmatch)
  924. return bestmatch;
  925. if (bestall)
  926. return bestall;
  927. return NULL;
  928. }
  929. void autolink_cycle_hub(char *start)
  930. {
  931. char bestval[HANDLEN + 4] = "", curval[HANDLEN + 4] = "", myval[HANDLEN + 4] = "";
  932. tand_t *bot = NULL;
  933. link_pref_val(conf.bot->u, myval);
  934. strcpy(bestval, myval);
  935. for (int i = 0; i < dcc_total; i++) {
  936. if (dcc[i].type) {
  937. if (dcc[i].type == &DCC_BOT_NEW)
  938. return;
  939. if (dcc[i].type == &DCC_FORK_BOT)
  940. return;
  941. if (dcc[i].type == &DCC_BOT) {
  942. if (dcc[i].status & (STAT_OFFEREDU | STAT_GETTINGU | STAT_SENDINGU))
  943. continue; /* lets let the binary update have it's peace. */
  944. if ((bot = findbot(dcc[i].nick)) && bot->revision != revision)
  945. continue; /* same thing. */
  946. if (dcc[i].status & (STAT_SHARE | STAT_OFFERED | STAT_SENDING | STAT_GETTING)) {
  947. link_pref_val(dcc[i].user, curval);
  948. if (strcmp(myval, curval) < 0) {
  949. /* I should be aggressive to this one */
  950. if (dcc[i].status & STAT_AGGRESSIVE) {
  951. putlog(LOG_MISC, "*", "Passively sharing with %s but should be aggressive", dcc[i].user->handle);
  952. putlog(LOG_DEBUG, "*", "My linkval: %s - %s linkval: %s", myval, dcc[i].nick, curval);
  953. botunlink(-2, dcc[i].nick, "Marked passive, should be aggressive");
  954. return;
  955. }
  956. } else {
  957. /* I should be passive to this one */
  958. if (!(dcc[i].status & STAT_AGGRESSIVE)) {
  959. putlog(LOG_MISC, "*", "Aggressively sharing with %s but should be passive", dcc[i].user->handle);
  960. putlog(LOG_DEBUG, "*", "My linkval: %s - %s linkval: %s", myval, dcc[i].nick, curval);
  961. botunlink(-2, dcc[i].nick, "Marked aggressive, should be passive");
  962. return;
  963. }
  964. if (strcmp(curval, bestval) < 0)
  965. strcpy(bestval, curval);
  966. }
  967. }
  968. }
  969. }
  970. }
  971. struct userrec *u = NULL;
  972. if (start)
  973. u = get_user_by_handle(userlist, start);
  974. if (u) {
  975. link_pref_val(u, curval);
  976. if (strcmp(bestval, curval) < 0) {
  977. /* This happens if we're already connected to a good hub but we failed to link to another hub as well
  978. can happen if you .link.... but it's nothing FATAL :) */
  979. /* This shouldn't happen. Getting a failed link attempt (start!=NULL)
  980. while a dcc scan indicates we *are* connected to a better bot than
  981. the one we failed a link to.
  982. */
  983. // putlog(LOG_BOTS, "*", "Failed link attempt to %s but connected to %s already???", u->handle, (char *) &bestval[2]);
  984. return;
  985. }
  986. } else
  987. strcpy(curval, "0");
  988. /* link to the (highlest level)/best hub */
  989. u = next_hub(u, bestval, curval);
  990. if (u && !in_chain(u->handle))
  991. botlink("", -3, u->handle);
  992. }
  993. typedef struct hublist_entry {
  994. struct hublist_entry *next;
  995. struct userrec *u;
  996. } tag_hublist_entry;
  997. int botlinkcount = 0;
  998. void autolink_random_hub(char *avoidbot) {
  999. /* Pick a random hub, but avoid 'avoidbot' */
  1000. int hlc = 0;
  1001. struct hublist_entry *hl = NULL, *hl2 = NULL;
  1002. struct userrec *tmpu = NULL;
  1003. struct flag_record fr = { FR_GLOBAL|FR_BOT, 0, 0, 0 };
  1004. for (struct userrec *u = userlist; u; u = u->next) {
  1005. get_user_flagrec(u, &fr, NULL);
  1006. if (glob_bot(fr) && egg_strcasecmp(u->handle, conf.bot->nick) && (bot_hublevel(u) < 999)) {
  1007. if (strcmp(u->handle, avoidbot)) {
  1008. hl2 = hl;
  1009. hl = (struct hublist_entry *) my_calloc(1, sizeof(struct hublist_entry));
  1010. hl->next = hl2;
  1011. hlc++;
  1012. hl->u = u;
  1013. } else
  1014. tmpu = u;
  1015. }
  1016. }
  1017. /* We probably have 1 hub and avoided it :/ */
  1018. if (!hlc && tmpu) {
  1019. hl2 = hl;
  1020. hl = (struct hublist_entry *) my_calloc(1, sizeof(struct hublist_entry));
  1021. hl->next = hl2;
  1022. hlc++;
  1023. hl->u = tmpu;
  1024. }
  1025. hlc = randint(hlc);
  1026. while (hl) {
  1027. if (!hlc) {
  1028. botlink("", -3, hl->u->handle);
  1029. }
  1030. hlc--;
  1031. hl2 = hl->next;
  1032. free(hl);
  1033. hl = hl2;
  1034. }
  1035. }
  1036. void autolink_cycle_leaf(char *start)
  1037. {
  1038. struct bot_addr *my_ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  1039. char uplink[HANDLEN + 1] = "", avoidbot[HANDLEN + 1] = "", curhub[HANDLEN + 1] = "";
  1040. /* Reset connection attempts if we ain't called due to a failed link */
  1041. if (!start)
  1042. botlinkcount = 0;
  1043. if (my_ba && (my_ba->uplink[0])) {
  1044. strlcpy(uplink, my_ba->uplink, sizeof(uplink));
  1045. }
  1046. for (int i = 0; i < dcc_total; i++) {
  1047. if (dcc[i].type) {
  1048. if ((dcc[i].type == &DCC_BOT_NEW) || (dcc[i].type == &DCC_FORK_BOT))
  1049. return;
  1050. if (dcc[i].type == &DCC_BOT) {
  1051. strcpy(curhub, dcc[i].nick);
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. if (curhub[0]) {
  1057. /* we are linked to a bot (hub) */
  1058. if (uplink[0] && !strcmp(curhub, uplink))
  1059. /* Connected to uplink, nothing more to do */
  1060. return;
  1061. if (start)
  1062. /* Failed a link... let's just wait for next regular call */
  1063. return;
  1064. if (dont_restructure)
  1065. return;
  1066. if (uplink[0]) {
  1067. /* Trying the uplink */
  1068. botlink("", -3, uplink);
  1069. return;
  1070. }
  1071. /* we got a hub currently, and no set uplink. Stay here */
  1072. return;
  1073. } else {
  1074. /* no hubs connected... pick one */
  1075. if (!start) {
  1076. /* Regular interval call, no previous failed link */
  1077. if (uplink[0]) {
  1078. /* We have a set uplink, try it */
  1079. botlink("", -3, uplink);
  1080. return;
  1081. }
  1082. /* No preferred uplink, we need a random bot */
  1083. avoidbot[0] = 0;
  1084. } else {
  1085. /* We got a failed link... */
  1086. botlinkcount++;
  1087. if (botlinkcount >= 3)
  1088. /* tried 3+ random hubs without success, wait for next regular interval call */
  1089. return;
  1090. /* We need a random bot but *not* the last we tried */
  1091. strcpy(avoidbot, start);
  1092. }
  1093. }
  1094. autolink_random_hub(avoidbot);
  1095. }
  1096. void autolink_cycle(char *start)
  1097. {
  1098. if (conf.bot->hub)
  1099. autolink_cycle_hub(start);
  1100. else
  1101. autolink_cycle_leaf(start);
  1102. }
  1103. void check_stale_dcc_users()
  1104. {
  1105. for (int i = 0; i < dcc_total; ++i) {
  1106. if (!dcc[i].type || !dcc[i].nick[0]) continue;
  1107. if (dcc[i].user == NULL) { /* Removed user */
  1108. if (dcc[i].type == &DCC_BOT || dcc[i].type == &DCC_FORK_BOT || dcc[i].type == &DCC_BOT_NEW)
  1109. botunlink(i, dcc[i].nick, "No longer a valid bot.");
  1110. else if (dcc[i].type == &DCC_CHAT)
  1111. do_boot(i, "internal", "No longer a valid user.");
  1112. }
  1113. }
  1114. }