users.c 33 KB

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