users.c 34 KB

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