users.c 33 KB

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