users.c 33 KB

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