users.c 32 KB

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