users.c 31 KB

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