users.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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, 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. egg_strftime(s1, 7, "%d %b", gmtime(&li->laston));
  434. else
  435. egg_strftime(s1, 6, "%H:%M", gmtime(&li->laston));
  436. }
  437. if (!u->bot) {
  438. egg_snprintf(format, sizeof format, "%%-%us %%-5s%%5d %%-15s %%s (%%-10.10s)\n", HANDLEN);
  439. dprintf(idx, format, u->handle, get_user(&USERENTRY_PASS, u) ? "yes" : "no", n, s, s1, (li && li->lastonplace) ? li->lastonplace : "nowhere");
  440. } else { /* BOT */
  441. egg_snprintf(format, sizeof format, "%%-%us %%-8s %%s (%%-10.10s)\n", HANDLEN);
  442. dprintf(idx, format, u->handle, s, s1, (li && li->lastonplace) ? li->lastonplace : "nowhere");
  443. }
  444. /* channel flags? */
  445. for (ch = u->chanrec; ch; ch = ch->next) {
  446. fr.match = FR_CHAN | FR_GLOBAL;
  447. chan = findchan_by_dname(ch->channel);
  448. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  449. if (!channel_private(chan) || (channel_private(chan) && (chan_op(fr) || glob_owner(fr)))) {
  450. if (glob_op(fr) || chan_op(fr)) {
  451. if (ch->laston == 0L)
  452. strcpy(s1, "never");
  453. else {
  454. now2 = now - (ch->laston);
  455. if (now2 > 86400)
  456. egg_strftime(s1, 7, "%d %b", gmtime(&ch->laston));
  457. else
  458. egg_strftime(s1, 6, "%H:%M", gmtime(&ch->laston));
  459. }
  460. fr.match = FR_CHAN;
  461. fr.chan = ch->flags;
  462. build_flags(s, &fr, NULL);
  463. egg_snprintf(format, sizeof format, "%%%us %%-18s %%-15s %%s\n", HANDLEN-9);
  464. dprintf(idx, format, " ", ch->channel, s, s1);
  465. if (ch->info != NULL)
  466. dprintf(idx, " INFO: %s\n", ch->info);
  467. }
  468. }
  469. }
  470. /* user-defined extra fields */
  471. for (ue = u->entries; ue; ue = ue->next)
  472. if (!ue->name && ue->type->display)
  473. ue->type->display(idx, ue, u);
  474. }
  475. /* show user by ident */
  476. void tell_user_ident(int idx, char *id, int master)
  477. {
  478. char format[81] = "";
  479. struct userrec *u = NULL;
  480. struct flag_record user = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  481. get_user_flagrec(dcc[idx].user, &user, NULL);
  482. u = get_user_by_handle(userlist, id);
  483. if (u == NULL)
  484. u = get_user_by_host(id);
  485. if (u == NULL || (u && !whois_access(dcc[idx].user, u))) {
  486. dprintf(idx, "%s.\n", USERF_NOMATCH);
  487. return;
  488. }
  489. if (u->bot) {
  490. egg_snprintf(format, sizeof format, "%%-%us FLAGS LAST\n", HANDLEN);
  491. dprintf(idx, format, "BOTNICK");
  492. } else {
  493. egg_snprintf(format, sizeof format, "%%-%us PASS NOTES FLAGS LAST\n", HANDLEN);
  494. dprintf(idx, format, "HANDLE");
  495. }
  496. tell_user(idx, u, master);
  497. }
  498. /* match string:
  499. * wildcard to match nickname or hostmasks
  500. * +attr to find all with attr */
  501. void tell_users_match(int idx, char *mtch, int start, int limit, int master, char *chname, int isbot)
  502. {
  503. char format[81] = "";
  504. struct userrec *u = NULL;
  505. int fnd = 0, cnt, nomns = 0, flags = 0;
  506. struct list_type *q = NULL;
  507. struct flag_record user, pls, mns;
  508. dprintf(idx, "*** %s '%s':\n", MISC_MATCHING, mtch);
  509. cnt = 0;
  510. if (isbot) {
  511. egg_snprintf(format, sizeof format, "%%-%us FLAGS LAST\n", HANDLEN);
  512. dprintf(idx, format, "BOTNICK");
  513. } else {
  514. egg_snprintf(format, sizeof format, "%%-%us PASS NOTES FLAGS LAST\n", HANDLEN);
  515. dprintf(idx, format, "HANDLE");
  516. }
  517. if (start > 1)
  518. dprintf(idx, "(%s %d)\n", MISC_SKIPPING, start - 1);
  519. if (strchr("+-&|", *mtch)) {
  520. user.match = pls.match = FR_GLOBAL | FR_CHAN;
  521. break_down_flags(mtch, &pls, &mns);
  522. mns.match = pls.match ^ (FR_AND | FR_OR);
  523. if (!mns.global && !mns.chan) {
  524. nomns = 1;
  525. if (!pls.global && !pls.chan) {
  526. /* happy now BB you weenie :P */
  527. dprintf(idx, "Unknown flag specified for matching!!\n");
  528. return;
  529. }
  530. }
  531. if (!chname || !chname[0])
  532. chname = dcc[idx].u.chat->con_chan;
  533. flags = 1;
  534. }
  535. for (u = userlist; u; u = u->next) {
  536. if (!whois_access(dcc[idx].user, u) || (isbot && !u->bot) || (!isbot && u->bot)) {
  537. continue;
  538. } else if (flags) {
  539. get_user_flagrec(u, &user, chname);
  540. if (flagrec_eq(&pls, &user)) {
  541. if (nomns || !flagrec_eq(&mns, &user)) {
  542. cnt++;
  543. if ((cnt <= limit) && (cnt >= start))
  544. tell_user(idx, u, master);
  545. if (cnt == limit + 1)
  546. dprintf(idx, MISC_TRUNCATED, limit);
  547. }
  548. }
  549. } else if (wild_match(mtch, u->handle)) {
  550. cnt++;
  551. if ((cnt <= limit) && (cnt >= start))
  552. tell_user(idx, u, master);
  553. if (cnt == limit + 1)
  554. dprintf(idx, MISC_TRUNCATED, limit);
  555. } else {
  556. fnd = 0;
  557. for (q = get_user(&USERENTRY_HOSTS, u); q; q = q->next) {
  558. if ((wild_match(mtch, q->extra)) && (!fnd)) {
  559. cnt++;
  560. fnd = 1;
  561. if ((cnt <= limit) && (cnt >= start)) {
  562. tell_user(idx, u, master);
  563. }
  564. if (cnt == limit + 1)
  565. dprintf(idx, MISC_TRUNCATED, limit);
  566. }
  567. }
  568. }
  569. }
  570. dprintf(idx, MISC_FOUNDMATCH, cnt, cnt == 1 ? "" : MISC_MATCH_PLURAL);
  571. }
  572. #ifdef HUB
  573. void backup_userfile()
  574. {
  575. char s[DIRMAX] = "", s2[DIRMAX] = "";
  576. putlog(LOG_MISC, "*", USERF_BACKUP);
  577. egg_snprintf(s, sizeof s, "%s.u.0", tempdir);
  578. egg_snprintf(s2, sizeof s2, "%s.u.1", tempdir);
  579. movefile(s, s2);
  580. copyfile(userfile, s);
  581. }
  582. #endif /* HUB */
  583. /*
  584. * tagged lines in the user file:
  585. * * OLD:
  586. * # (comment)
  587. * ; (comment)
  588. * - hostmask(s)
  589. * + email
  590. * * dcc directory
  591. * = comment
  592. * : info line
  593. * . xtra (Tcl)
  594. * ! channel-specific
  595. * !! global laston
  596. * :: channel-specific bans
  597. * NEW:
  598. * *ban global bans
  599. * *ignore global ignores
  600. * ::#chan channel bans
  601. * - entries in each
  602. * + denotes tcl command
  603. * <handle> begin user entry
  604. * --KEY INFO - info on each
  605. * NEWER:
  606. * % exemptmask(s)
  607. * @ Invitemask(s)
  608. * *exempt global exempts
  609. * *Invite global Invites
  610. * && channel-specific exempts
  611. * &&#chan channel exempts
  612. * $$ channel-specific Invites
  613. * $$#chan channel Invites
  614. */
  615. int readuserfile(const char *file, struct userrec **ret)
  616. {
  617. char *p = NULL, buf[1024] = "", lasthand[512] = "", *attr = NULL, *pass = NULL;
  618. char *code = NULL, s1[1024] = "", *s = NULL, cbuf[1024] = "", *temps = NULL, ignored[512] = "";
  619. FILE *f = NULL;
  620. struct userrec *bu = NULL, *u = NULL;
  621. struct chanset_t *cst = NULL;
  622. struct flag_record fr;
  623. struct chanuserrec *cr = NULL;
  624. int i, line = 0;
  625. bu = (*ret);
  626. ignored[0] = 0;
  627. if (bu == userlist) {
  628. clear_chanlist();
  629. lastuser = NULL;
  630. global_bans = NULL;
  631. global_ign = NULL;
  632. global_exempts = NULL;
  633. global_invites = NULL;
  634. }
  635. lasthand[0] = 0;
  636. f = fopen(file, "r");
  637. if (f == NULL)
  638. return 0;
  639. noshare = 1;
  640. /* read opening comment */
  641. s = buf;
  642. fgets(cbuf, 180, f);
  643. remove_crlf(cbuf);
  644. temps = (char *) decrypt_string(SALT1, cbuf);
  645. egg_snprintf(s, 180, "%s", temps);
  646. free(temps);
  647. if (s[1] < '4') {
  648. fatal(USERF_OLDFMT, 0);
  649. }
  650. if (s[1] > '4')
  651. fatal(USERF_INVALID, 0);
  652. while (!feof(f)) {
  653. s = buf;
  654. fgets(cbuf, 1024, f);
  655. remove_crlf(cbuf);
  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. else if (lasthand[1] == CONFIG_NAME[1]) {
  685. set_cmd_pass(s, 1);
  686. }
  687. else
  688. restore_chanban(NULL, s);
  689. } else if (lasthand[0])
  690. set_user(&USERENTRY_HOSTS, u, s);
  691. }
  692. } else if (!strcmp(code, "%")) { /* exemptmasks */
  693. if (!lasthand[0])
  694. continue; /* Skip this entry. */
  695. if (s[0]) {
  696. if (lasthand[0] == '#' || lasthand[0] == '+')
  697. restore_chanexempt(cst,s);
  698. else if (lasthand[0] == '*')
  699. if (lasthand[1] == EXEMPT_NAME[1])
  700. restore_chanexempt(NULL, s);
  701. }
  702. } else if (!strcmp(code, "@")) { /* Invitemasks */
  703. if (!lasthand[0])
  704. continue; /* Skip this entry. */
  705. if (s[0]) {
  706. if (lasthand[0] == '#' || lasthand[0] == '+') {
  707. restore_chaninvite(cst,s);
  708. } else if (lasthand[0] == '*') {
  709. if (lasthand[1] == INVITE_NAME[1]) {
  710. restore_chaninvite(NULL, s);
  711. } else if (lasthand[1] == CONFIG_NAME[1]) {
  712. userfile_cfg_line(s);
  713. }
  714. }
  715. }
  716. } else if (!strcmp(code, "!")) {
  717. /* ! #chan laston flags [info] */
  718. char *chname = NULL, *st = NULL, *fl = NULL;
  719. if (u) {
  720. chname = newsplit(&s);
  721. st = newsplit(&s);
  722. fl = newsplit(&s);
  723. rmspace(s);
  724. fr.match = FR_CHAN;
  725. break_down_flags(fl, &fr, 0);
  726. if (findchan_by_dname(chname)) {
  727. for (cr = u->chanrec; cr; cr = cr->next)
  728. if (!rfc_casecmp(cr->channel, chname))
  729. break;
  730. if (!cr) {
  731. cr = (struct chanuserrec *) calloc(1, sizeof(struct chanuserrec));
  732. cr->next = u->chanrec;
  733. u->chanrec = cr;
  734. strncpyz(cr->channel, chname, 80);
  735. cr->laston = atoi(st);
  736. cr->flags = fr.chan;
  737. if (s[0]) {
  738. cr->info = strdup(s);
  739. } else
  740. cr->info = NULL;
  741. }
  742. }
  743. }
  744. } else if (!strcmp(code, "+")) {
  745. if (s[0] && lasthand[0] == '*' && lasthand[1] == CHANS_NAME[1]) {
  746. char *options = NULL, *chan = NULL, *my_ptr = NULL;
  747. char resultbuf[2048] = "";
  748. options = my_ptr = strdup(s);
  749. newsplit(&options);
  750. newsplit(&options);
  751. chan = newsplit(&options);
  752. /* hack to remove { } */
  753. newsplit(&options);
  754. options[strlen(options) - 1] = 0;
  755. if (channel_add(resultbuf, chan, options) != OK) {
  756. putlog(LOG_MISC, "*", "Channel parsing error in userfile on line %d", line);
  757. free(my_ptr);
  758. fclose(f);
  759. return 0;
  760. }
  761. free(my_ptr);
  762. }
  763. } else if (!strncmp(code, "::", 2)) {
  764. /* channel-specific bans */
  765. strcpy(lasthand, &code[2]);
  766. u = NULL;
  767. if (!findchan_by_dname(lasthand)) {
  768. strcpy(s1, lasthand);
  769. strcat(s1, " ");
  770. if (strstr(ignored, s1) == NULL) {
  771. strcat(ignored, lasthand);
  772. strcat(ignored, " ");
  773. }
  774. lasthand[0] = 0;
  775. } else {
  776. /* Remove all bans for this channel to avoid dupes */
  777. /* NOTE only remove bans for when getting a userfile
  778. * from another bot & that channel is shared */
  779. cst = findchan_by_dname(lasthand);
  780. clear_masks(cst->bans);
  781. cst->bans = NULL;
  782. }
  783. } else if (!strncmp(code, "&&", 2)) {
  784. /* channel-specific exempts */
  785. strcpy(lasthand, &code[2]);
  786. u = NULL;
  787. if (!findchan_by_dname(lasthand)) {
  788. strcpy(s1, lasthand);
  789. strcat(s1, " ");
  790. if (strstr(ignored, s1) == NULL) {
  791. strcat(ignored, lasthand);
  792. strcat(ignored, " ");
  793. }
  794. lasthand[0] = 0;
  795. } else {
  796. /* Remove all exempts for this channel to avoid dupes */
  797. /* NOTE only remove exempts for when getting a userfile
  798. * from another bot & that channel is shared */
  799. cst = findchan_by_dname(lasthand);
  800. clear_masks(cst->exempts);
  801. cst->exempts = NULL;
  802. }
  803. } else if (!strncmp(code, "$$", 2)) {
  804. /* channel-specific invites */
  805. strcpy(lasthand, &code[2]);
  806. u = NULL;
  807. if (!findchan_by_dname(lasthand)) {
  808. strcpy(s1, lasthand);
  809. strcat(s1, " ");
  810. if (strstr(ignored, s1) == NULL) {
  811. strcat(ignored, lasthand);
  812. strcat(ignored, " ");
  813. }
  814. lasthand[0] = 0;
  815. } else {
  816. /* Remove all invites for this channel to avoid dupes */
  817. /* NOTE only remove invites for when getting a userfile
  818. * from another bot & that channel is shared */
  819. cst = findchan_by_dname(lasthand);
  820. clear_masks(cst->invites);
  821. cst->invites = NULL;
  822. }
  823. } else if (!strncmp(code, "--", 2)) {
  824. if (u) {
  825. /* new format storage */
  826. struct user_entry *ue = NULL;
  827. int ok = 0;
  828. for (ue = u->entries; ue && !ok; ue = ue->next)
  829. if (ue->name && !egg_strcasecmp(code + 2, ue->name)) {
  830. struct list_type *list = NULL;
  831. list = calloc(1, sizeof(struct list_type));
  832. list->next = NULL;
  833. list->extra = strdup(s);
  834. list_append((&ue->u.list), list);
  835. ok = 1;
  836. }
  837. if (!ok) {
  838. ue = calloc(1, sizeof(struct user_entry));
  839. ue->name = calloc(1, strlen(code + 1));
  840. ue->type = NULL;
  841. strcpy(ue->name, code + 2);
  842. ue->u.list = calloc(1, sizeof(struct list_type));
  843. ue->u.list->next = NULL;
  844. ue->u.list->extra = strdup(s);
  845. list_insert((&u->entries), ue);
  846. }
  847. }
  848. } else if (!rfc_casecmp(code, BAN_NAME)) {
  849. strcpy(lasthand, code);
  850. u = NULL;
  851. } else if (!rfc_casecmp(code, IGNORE_NAME)) {
  852. strcpy(lasthand, code);
  853. u = NULL;
  854. } else if (!rfc_casecmp(code, EXEMPT_NAME)) {
  855. strcpy(lasthand, code);
  856. u = NULL;
  857. } else if (!rfc_casecmp(code, INVITE_NAME)) {
  858. strcpy(lasthand, code);
  859. u = NULL;
  860. } else if (!rfc_casecmp(code, CHANS_NAME)) {
  861. strcpy(lasthand, code);
  862. u = NULL;
  863. } else if (!rfc_casecmp(code, CONFIG_NAME)) {
  864. strcpy(lasthand, code);
  865. u = NULL;
  866. } else if (code[0] == '*') {
  867. lasthand[0] = 0;
  868. u = NULL;
  869. } else { /* its a user ! */
  870. pass = newsplit(&s);
  871. attr = newsplit(&s);
  872. rmspace(s);
  873. if (!attr[0] || !pass[0]) {
  874. putlog(LOG_MISC, "*", "* %s line: %d!", USERF_CORRUPT, line);
  875. lasthand[0] = 0;
  876. fclose(f);
  877. return 0;
  878. } else {
  879. int isbot = 0;
  880. if (code[0] == '-') {
  881. code++;
  882. isbot++;
  883. }
  884. u = get_user_by_handle(bu, code);
  885. if (u) {
  886. putlog(LOG_ERROR, "@", "* %s '%s'!", USERF_DUPE, code);
  887. lasthand[0] = 0;
  888. u = NULL;
  889. } else {
  890. fr.match = FR_GLOBAL;
  891. break_down_flags(attr, &fr, 0);
  892. strcpy(lasthand, code);
  893. cst = NULL;
  894. /* FIXME: remove after 1.2 */
  895. if (fr.global & USER_BOT) { /* this should pick up the old +b flag for now */
  896. isbot++;
  897. fr.global &= ~USER_BOT;
  898. }
  899. if (strlen(code) > HANDLEN)
  900. code[HANDLEN] = 0;
  901. if (strlen(pass) > 20) {
  902. putlog(LOG_MISC, "*", "* %s '%s'", USERF_BROKEPASS, code);
  903. strcpy(pass, "-");
  904. }
  905. bu = adduser(bu, code, 0, pass, sanity_check(fr.global, isbot), isbot);
  906. u = get_user_by_handle(bu, code);
  907. for (i = 0; i < dcc_total; i++)
  908. if (!egg_strcasecmp(code, dcc[i].nick))
  909. dcc[i].user = u;
  910. if (!egg_strcasecmp(code, conf.bot->nick))
  911. conf.bot->u = u;
  912. /* if s starts with '/' it's got file info */
  913. }
  914. }
  915. }
  916. }
  917. }
  918. }
  919. fclose(f);
  920. (*ret) = bu;
  921. if (ignored[0]) {
  922. putlog(LOG_MISC, "*", "%s %s", USERF_IGNBANS, ignored);
  923. }
  924. putlog(LOG_MISC, "*", "Userfile loaded, unpacking...");
  925. for (u = bu; u; u = u->next) {
  926. struct user_entry *e = NULL;
  927. if (!u->bot && !egg_strcasecmp (u->handle, conf.bot->nick)) {
  928. putlog(LOG_MISC, "*", "(!) I have a user record, but am not classified as a BOT!");
  929. u->bot = 1;
  930. /* u->flags |= USER_BOT; */
  931. }
  932. for (e = u->entries; e; e = e->next)
  933. if (e->name) {
  934. struct user_entry_type *uet = find_entry_type(e->name);
  935. if (uet) {
  936. e->type = uet;
  937. uet->unpack(u, e);
  938. free(e->name);
  939. e->name = NULL;
  940. }
  941. }
  942. }
  943. /* process the user data *now* */
  944. #ifdef LEAF
  945. unlink(userfile);
  946. #endif /* LEAF */
  947. noshare = 0;
  948. return 1;
  949. }
  950. void link_pref_val(struct userrec *u, char *val)
  951. {
  952. /* val must be HANDLEN + 4 chars minimum */
  953. struct bot_addr *ba = NULL;
  954. val[0] = 'Z';
  955. val[1] = 0;
  956. if (!u)
  957. return;
  958. if (!u->bot)
  959. return;
  960. if (!(ba = get_user(&USERENTRY_BOTADDR, u))) {
  961. return;
  962. }
  963. if (!ba->hublevel) {
  964. return;
  965. }
  966. sprintf(val, "%02d%s", ba->hublevel, u->handle);
  967. }
  968. /*
  969. starting at "current" or "userlist" if NULL, find next bot with a
  970. link_pref_val higher than "lowval" and lower than "highval"
  971. If none found return bot with best overall link_pref_val
  972. If still not found return NULL
  973. */
  974. struct userrec *next_hub(struct userrec *current, char *lowval, char *highval)
  975. {
  976. char thisval[NICKLEN + 4] = "", bestmatchval[NICKLEN + 4] = "z", bestallval[NICKLEN + 4] = "z";
  977. struct userrec *cur = NULL, *bestmatch = NULL, *bestall = NULL;
  978. if (current)
  979. cur = current->next;
  980. else
  981. cur = userlist;
  982. while (cur != current) {
  983. if (!cur)
  984. cur = userlist;
  985. if (cur == current)
  986. break;
  987. if (cur->bot && (strcmp(cur->handle, conf.bot->nick))) {
  988. link_pref_val(cur, thisval);
  989. if ((strcmp(thisval, lowval) < 0) && (strcmp(thisval, highval) > 0) &&(strcmp(thisval, bestmatchval) < 0)) {
  990. strcpy(bestmatchval, thisval);
  991. bestmatch = cur;
  992. }
  993. if ((strcmp(thisval, lowval) < 0)
  994. && (strcmp(thisval, bestallval) < 0)) {
  995. strcpy(bestallval, thisval);
  996. bestall = cur;
  997. }
  998. }
  999. cur = cur->next;
  1000. }
  1001. if (bestmatch)
  1002. return bestmatch;
  1003. if (bestall)
  1004. return bestall;
  1005. return NULL;
  1006. }
  1007. #ifdef HUB
  1008. void autolink_cycle(char *start)
  1009. {
  1010. char bestval[HANDLEN + 4] = "", curval[HANDLEN + 4] = "", myval[HANDLEN + 4] = "";
  1011. struct userrec *u = NULL;
  1012. tand_t *bot = NULL;
  1013. int i;
  1014. link_pref_val(conf.bot->u, myval);
  1015. strcpy(bestval, myval);
  1016. for (i = 0; i < dcc_total; i++) {
  1017. if (dcc[i].type == &DCC_BOT_NEW)
  1018. return;
  1019. if (dcc[i].type == &DCC_FORK_BOT)
  1020. return;
  1021. if (dcc[i].type == &DCC_BOT) {
  1022. if (dcc[i].status & (STAT_OFFEREDU | STAT_GETTINGU | STAT_SENDINGU))
  1023. continue; /* lets let the binary have it's peace. */
  1024. if ((bot = findbot(dcc[i].nick)) && bot->buildts != buildts)
  1025. continue; /* same thing. */
  1026. if (dcc[i].status & (STAT_SHARE | STAT_OFFERED | STAT_SENDING | STAT_GETTING)) {
  1027. link_pref_val(dcc[i].user, curval);
  1028. if (strcmp(myval, curval) < 0) {
  1029. /* I should be aggressive to this one */
  1030. if (dcc[i].status & STAT_AGGRESSIVE) {
  1031. putlog(LOG_MISC, "*", "Passively sharing with %s but should be aggressive", dcc[i].user->handle);
  1032. putlog(LOG_DEBUG, "*", "My linkval: %s - %s linkval: %s", myval, dcc[i].nick, curval);
  1033. botunlink(-2, dcc[i].nick, "Marked passive, should be aggressive");
  1034. return;
  1035. }
  1036. } else {
  1037. /* I should be passive to this one */
  1038. if (!(dcc[i].status & STAT_AGGRESSIVE)) {
  1039. putlog(LOG_MISC, "*", "Aggressively sharing with %s but should be passive", dcc[i].user->handle);
  1040. putlog(LOG_DEBUG, "*", "My linkval: %s - %s linkval: %s", myval, dcc[i].nick, curval);
  1041. botunlink(-2, dcc[i].nick, "Marked aggressive, should be passive");
  1042. return;
  1043. }
  1044. if (strcmp(curval, bestval) < 0)
  1045. strcpy(bestval, curval);
  1046. }
  1047. } else {
  1048. botunlink(-2, dcc[i].nick, "Linked but not sharing?");
  1049. }
  1050. }
  1051. }
  1052. if (start)
  1053. u = get_user_by_handle(userlist, start);
  1054. else
  1055. u = NULL;
  1056. if (u) {
  1057. link_pref_val(u, curval);
  1058. if (strcmp(bestval, curval) < 0) {
  1059. /* This shouldn't happen. Getting a failed link attempt (start!=NULL)
  1060. while a dcc scan indicates we *are* connected to a better bot than
  1061. the one we failed a link to.
  1062. */
  1063. putlog(LOG_BOTS, "*", "Failed link attempt to %s but connected to %s already???", u->handle, (char *) &bestval[2]);
  1064. return;
  1065. }
  1066. } else
  1067. strcpy(curval, "0");
  1068. u = next_hub(u, bestval, curval);
  1069. if ((u) && (!in_chain(u->handle)))
  1070. botlink("", -3, u->handle);
  1071. }
  1072. #endif /* HUB */
  1073. #ifdef LEAF
  1074. typedef struct hublist_entry {
  1075. struct hublist_entry *next;
  1076. struct userrec *u;
  1077. } tag_hublist_entry;
  1078. int botlinkcount = 0;
  1079. void autolink_cycle(char *start)
  1080. {
  1081. struct userrec *u = NULL;
  1082. struct hublist_entry *hl = NULL, *hl2 = NULL;
  1083. struct bot_addr *my_ba = NULL;
  1084. char uplink[HANDLEN + 1] = "", avoidbot[HANDLEN + 1] = "", curhub[HANDLEN + 1] = "";
  1085. int i, hlc;
  1086. struct flag_record fr = { FR_GLOBAL, 0, 0, 0 };
  1087. /* Reset connection attempts if we ain't called due to a failed link */
  1088. if (!start)
  1089. botlinkcount = 0;
  1090. my_ba = get_user(&USERENTRY_BOTADDR, conf.bot->u);
  1091. if (my_ba && (my_ba->uplink[0])) {
  1092. strncpyz(uplink, my_ba->uplink, sizeof(uplink));
  1093. } else {
  1094. uplink[0] = 0;
  1095. }
  1096. curhub[0] = 0;
  1097. for (i = 0; i < dcc_total; i++) {
  1098. if ((dcc[i].type == &DCC_BOT_NEW) || (dcc[i].type == &DCC_FORK_BOT))
  1099. return;
  1100. if (dcc[i].type == &DCC_BOT) {
  1101. strcpy(curhub, dcc[i].nick);
  1102. break;
  1103. }
  1104. }
  1105. if (curhub[0]) {
  1106. /* we are linked to a bot (hub) */
  1107. if (uplink[0] && !strcmp(curhub, uplink))
  1108. /* Connected to uplink, nothing more to do */
  1109. return;
  1110. if (start)
  1111. /* Failed a link... let's just wait for next regular call */
  1112. return;
  1113. if (uplink[0]) {
  1114. /* Trying the uplink */
  1115. botlink("", -3, uplink);
  1116. return;
  1117. }
  1118. /* we got a hub currently, and no set uplink. Stay here */
  1119. return;
  1120. } else {
  1121. /* no hubs connected... pick one */
  1122. if (!start) {
  1123. /* Regular interval call, no previous failed link */
  1124. if (uplink[0]) {
  1125. /* We have a set uplink, try it */
  1126. botlink("", -3, uplink);
  1127. return;
  1128. }
  1129. /* No preferred uplink, we need a random bot */
  1130. avoidbot[0] = 0;
  1131. } else {
  1132. /* We got a failed link... */
  1133. botlinkcount++;
  1134. if (botlinkcount >= 3)
  1135. /* tried 3+ random hubs without success, wait for next regular interval call */
  1136. return;
  1137. /* We need a random bot but *not* the last we tried */
  1138. strcpy(avoidbot, start);
  1139. }
  1140. }
  1141. /* Pick a random hub, but avoid 'avoidbot' */
  1142. hlc = 0;
  1143. for (u = userlist; u; u = u->next) {
  1144. get_user_flagrec(u, &fr, NULL);
  1145. if (glob_bot(fr) && strcmp(u->handle, conf.bot->nick) && strcmp(u->handle, avoidbot) && (bot_hublevel(u) < 999)) {
  1146. putlog(LOG_DEBUG, "@", "Adding %s to hublist", u->handle);
  1147. hl2 = hl;
  1148. hl = calloc(1, sizeof(struct hublist_entry));
  1149. hl->next = hl2;
  1150. hlc++;
  1151. hl->u = u;
  1152. }
  1153. }
  1154. putlog(LOG_DEBUG, "@", "Picking random hub from %d hubs", hlc);
  1155. /* This is mainly a sanity check if the userfile gets fucked :P */
  1156. if (!hlc)
  1157. fatal("userlist died!", 0);
  1158. hlc = randint(hlc);
  1159. putlog(LOG_DEBUG, "@", "Picked #%d for hub", hlc);
  1160. while (hl) {
  1161. if (!hlc) {
  1162. putlog(LOG_DEBUG, "@", "Which is bot: %s", hl->u->handle);
  1163. botlink("", -3, hl->u->handle);
  1164. }
  1165. hlc--;
  1166. hl2 = hl->next;
  1167. free(hl);
  1168. hl = hl2;
  1169. }
  1170. }
  1171. #endif /* LEAF */