users.c 32 KB

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