users.c 32 KB

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