users.c 32 KB

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