users.c 32 KB

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