users.c 33 KB

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