users.c 32 KB

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