users.c 32 KB

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