users.c 34 KB

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