flags.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * flags.c -- handles:
  3. * all the flag matching/conversion functions in one neat package :)
  4. *
  5. */
  6. #include "common.h"
  7. #include "crypt.h"
  8. #include "src/mod/share.mod/share.h"
  9. #include "rfc1459.h"
  10. #include "userrec.h"
  11. #include "misc.h"
  12. #include "dccutil.h"
  13. #include "userent.h"
  14. #include "users.h"
  15. #include "chanprog.h"
  16. flag_t FLAG[128];
  17. void
  18. init_flags()
  19. {
  20. unsigned char i;
  21. for (i = 0; i < 'A'; i++)
  22. FLAG[(int) i] = 0;
  23. for (; i <= 'Z'; i++)
  24. FLAG[(int) i] = (flag_t) 1 << (26 + (i - 'A'));
  25. for (; i < 'a'; i++)
  26. FLAG[(int) i] = 0;
  27. for (; i <= 'z'; i++)
  28. FLAG[(int) i] = (flag_t) 1 << (i - 'a');
  29. for (; i < 128; i++)
  30. FLAG[(int) i] = 0;
  31. }
  32. /* Some flags are mutually exclusive -- this roots them out
  33. */
  34. flag_t
  35. sanity_check(flag_t atr, int bot)
  36. {
  37. if (bot && (atr & (USER_PARTY | USER_MASTER | USER_OWNER | USER_ADMIN | USER_HUBA | USER_CHUBA)))
  38. atr &= ~(USER_PARTY | USER_MASTER | USER_OWNER | USER_ADMIN | USER_HUBA | USER_CHUBA);
  39. /* only bots should be there: */
  40. if (!bot && (atr & (USER_DOLIMIT | USER_DOVOICE | USER_UPDATEHUB | USER_CHANHUB)))
  41. atr &= ~(USER_DOLIMIT | USER_DOVOICE | USER_UPDATEHUB | USER_CHANHUB);
  42. if (atr & USER_AUTOOP)
  43. atr |= USER_OP;
  44. if ((atr & USER_OP) && (atr & USER_DEOP))
  45. atr &= ~(USER_OP | USER_DEOP);
  46. if ((atr & USER_AUTOOP) && (atr & USER_DEOP))
  47. atr &= ~(USER_AUTOOP | USER_DEOP);
  48. if ((atr & USER_VOICE) && (atr & USER_QUIET))
  49. atr &= ~(USER_VOICE | USER_QUIET);
  50. /* Can't be admin without also being owner and having hub access */
  51. if (atr & USER_ADMIN)
  52. atr |= USER_OWNER | USER_HUBA | USER_PARTY;
  53. /* Hub access gets chanhub access */
  54. if (atr & USER_HUBA)
  55. atr |= USER_CHUBA;
  56. if (atr & USER_OWNER) {
  57. atr |= USER_MASTER;
  58. }
  59. /* Master implies botmaster, op */
  60. if (atr & USER_MASTER)
  61. atr |= USER_OP;
  62. /* Can't be botnet master without party-line access */
  63. /* if (atr & USER_BOTMAST)
  64. atr |= USER_PARTY;
  65. */
  66. return atr;
  67. }
  68. /* Sanity check on channel attributes
  69. */
  70. flag_t
  71. chan_sanity_check(flag_t chatr, int bot)
  72. {
  73. /* these should only be global */
  74. if (chatr & (USER_PARTY | USER_ADMIN | USER_HUBA | USER_CHUBA | USER_UPDATEHUB))
  75. chatr &= ~(USER_PARTY | USER_ADMIN | USER_HUBA | USER_CHUBA | USER_UPDATEHUB);
  76. /* these should only be set on bots */
  77. if (!bot && (chatr & (USER_DOLIMIT | USER_DOVOICE | USER_CHANHUB)))
  78. chatr &= ~(USER_DOLIMIT | USER_DOVOICE | USER_CHANHUB);
  79. if ((chatr & USER_OP) && (chatr & USER_DEOP))
  80. chatr &= ~(USER_OP | USER_DEOP);
  81. if ((chatr & USER_AUTOOP) && (chatr & USER_DEOP))
  82. chatr &= ~(USER_AUTOOP | USER_DEOP);
  83. if ((chatr & USER_VOICE) && (chatr & USER_QUIET))
  84. chatr &= ~(USER_VOICE | USER_QUIET);
  85. /* Can't be channel owner without also being channel master */
  86. if (chatr & USER_OWNER)
  87. chatr |= USER_MASTER;
  88. /* Master implies op */
  89. if (chatr & USER_MASTER)
  90. chatr |= USER_OP;
  91. return chatr;
  92. }
  93. /* Get icon symbol for a user (depending on access level)
  94. *
  95. * (*) owner on any channel
  96. * (+) master on any channel
  97. * (%) botnet master
  98. * (@) op on any channel
  99. * (-) other
  100. */
  101. char
  102. geticon(int idx)
  103. {
  104. if (!dcc[idx].user)
  105. return '-';
  106. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  107. get_user_flagrec(dcc[idx].user, &fr, NULL);
  108. if (glob_admin(fr))
  109. return '^';
  110. if (chan_owner(fr))
  111. return '*';
  112. if (chan_master(fr))
  113. return '+';
  114. if (chan_op(fr))
  115. return '@';
  116. return '-';
  117. }
  118. void
  119. break_down_flags(const char *string, struct flag_record *plus, struct flag_record *minus)
  120. {
  121. struct flag_record *which = plus;
  122. int chan = 0; /* 0 = glob, 1 = chan */
  123. int flags = plus->match;
  124. if (!(flags & FR_GLOBAL)) {
  125. if (flags & FR_CHAN)
  126. chan = 1;
  127. else
  128. return; /* We dont actually want any..huh? */
  129. }
  130. egg_bzero(plus, sizeof(struct flag_record));
  131. if (minus)
  132. egg_bzero(minus, sizeof(struct flag_record));
  133. plus->match = FR_OR; /* Default binding type OR */
  134. while (*string) {
  135. switch (*string) {
  136. case '+':
  137. which = plus;
  138. break;
  139. case '-':
  140. which = minus ? minus : plus;
  141. break;
  142. case '|':
  143. case '&':
  144. if (!chan) {
  145. if (*string == '|')
  146. plus->match = FR_OR;
  147. else
  148. plus->match = FR_AND;
  149. }
  150. which = plus;
  151. chan++;
  152. if (chan == 2)
  153. string = "";
  154. else if (chan == 3)
  155. chan = 1;
  156. break; /* switch() */
  157. default:
  158. {
  159. flag_t flagbit = FLAG[(int) *string];
  160. if (flagbit) {
  161. switch (chan) {
  162. case 0:
  163. /* which->global |= (flag_t) 1 << (*string - 'a'); */
  164. which->global |=flagbit;
  165. break;
  166. case 1:
  167. /* which->chan |= (flag_t) 1 << (*string - 'a'); */
  168. which->chan |= flagbit;
  169. break;
  170. }
  171. }
  172. }
  173. }
  174. string++;
  175. }
  176. /*
  177. * for (which = plus; which; which = (which == plus ? minus : 0)) {
  178. * which->global &= USER_VALID;
  179. * which->chan &= CHAN_VALID;
  180. * }
  181. */
  182. plus->match |= flags;
  183. if (minus) {
  184. minus->match |= flags;
  185. if (!(plus->match & (FR_AND | FR_OR)))
  186. plus->match |= FR_OR;
  187. }
  188. }
  189. static int
  190. flag2str(char *string, flag_t flag)
  191. {
  192. unsigned char c;
  193. char *old = string;
  194. for (c = 0; c < 128; c++)
  195. if (flag & FLAG[(int) c])
  196. *string++ = c;
  197. if (string == old)
  198. *string++ = '-';
  199. return string - old;
  200. }
  201. int
  202. build_flags(char *string, struct flag_record *plus, struct flag_record *minus)
  203. {
  204. char *old = string;
  205. if (plus->match & FR_GLOBAL) {
  206. if (minus && plus->global)
  207. *string++ = '+';
  208. string += flag2str(string, plus->global);
  209. if (minus && minus->global) {
  210. *string++ = '-';
  211. string += flag2str(string, minus->global);
  212. }
  213. }
  214. if (plus->match & FR_CHAN) {
  215. if (plus->match & FR_GLOBAL)
  216. *string++ = (plus->match & FR_AND) ? '&' : '|';
  217. if (minus && plus->chan)
  218. *string++ = '+';
  219. string += flag2str(string, plus->chan);
  220. if (minus && minus->chan) {
  221. *string++ = '-';
  222. string += flag2str(string, minus->global);
  223. }
  224. }
  225. if (string == old) {
  226. *string++ = '-';
  227. *string = 0;
  228. return 0;
  229. }
  230. *string = 0;
  231. return string - old;
  232. }
  233. /* Returns 1 if flags match, 0 if they don't. */
  234. int
  235. flagrec_ok(struct flag_record *req, struct flag_record *have)
  236. {
  237. if (req->match & FR_AND) {
  238. return flagrec_eq(req, have);
  239. } else if (req->match & FR_OR) {
  240. int hav = have->global;
  241. /* no flags, is a match */
  242. if (!req->chan && !req->global)
  243. return 1;
  244. if (hav & req->global)
  245. return 1;
  246. if (have->chan & req->chan)
  247. return 1;
  248. return 0;
  249. }
  250. return 0;
  251. }
  252. /* Returns 1 if flags match, 0 if they don't. */
  253. int
  254. flagrec_eq(struct flag_record *req, struct flag_record *have)
  255. {
  256. if (req->match & FR_AND) {
  257. if (req->match & FR_GLOBAL) {
  258. if ((req->global &have->global) !=req->global)
  259. return 0;
  260. }
  261. if (req->match & FR_CHAN) {
  262. if ((req->chan & have->chan) != req->chan)
  263. return 0;
  264. }
  265. return 1;
  266. } else if (req->match & FR_OR) {
  267. if (!req->chan && !req->global)
  268. return 1;
  269. if (req->match & FR_GLOBAL) {
  270. if (have->global &req->global)
  271. return 1;
  272. }
  273. if (req->match & FR_CHAN) {
  274. if (have->chan & req->chan)
  275. return 1;
  276. }
  277. return 0;
  278. }
  279. return 0; /* fr0k3 binding, dont pass it */
  280. }
  281. void
  282. set_user_flagrec(struct userrec *u, struct flag_record *fr, const char *chname)
  283. {
  284. if (!u)
  285. return;
  286. struct chanuserrec *cr = NULL;
  287. flag_t oldflags = fr->match;
  288. char buffer[100] = "";
  289. struct chanset_t *ch;
  290. if (oldflags & FR_GLOBAL) {
  291. u->flags = fr->global;
  292. if (!noshare) {
  293. fr->match = FR_GLOBAL;
  294. build_flags(buffer, fr, NULL);
  295. shareout("a %s %s\n", u->handle, buffer);
  296. }
  297. }
  298. if ((oldflags & FR_CHAN) && chname) {
  299. for (cr = u->chanrec; cr; cr = cr->next)
  300. if (!rfc_casecmp(chname, cr->channel))
  301. break;
  302. ch = findchan_by_dname(chname);
  303. if (!cr && ch) {
  304. cr = (struct chanuserrec *) my_calloc(1, sizeof(struct chanuserrec));
  305. cr->next = u->chanrec;
  306. u->chanrec = cr;
  307. strlcpy(cr->channel, chname, sizeof cr->channel);
  308. }
  309. if (cr && ch) {
  310. cr->flags = fr->chan;
  311. if (!noshare) {
  312. fr->match = FR_CHAN;
  313. build_flags(buffer, fr, NULL);
  314. shareout("a %s %s %s\n", u->handle, buffer, chname);
  315. }
  316. }
  317. }
  318. fr->match = oldflags;
  319. }
  320. /* Always pass the dname (display name) to this function for chname <cybah>
  321. */
  322. void
  323. get_user_flagrec(struct userrec *u, struct flag_record *fr, const char *chname)
  324. {
  325. fr->bot = 0;
  326. if (!u) {
  327. fr->global = fr->chan = 0;
  328. return;
  329. }
  330. if (u->bot)
  331. fr->bot = 1;
  332. if (fr->match & FR_GLOBAL) {
  333. fr->global = u->flags;
  334. } else {
  335. fr->global = 0;
  336. }
  337. if (fr->match & FR_CHAN) {
  338. struct chanuserrec *cr = NULL;
  339. if ((fr->match & FR_ANYWH) || (fr->match & FR_ANYCH)) {
  340. if (fr->match & FR_ANYWH)
  341. fr->chan = u->flags;
  342. for (cr = u->chanrec; cr; cr = cr->next) {
  343. if (findchan_by_dname(cr->channel)) {
  344. fr->chan |= cr->flags;
  345. }
  346. }
  347. } else {
  348. if (chname) {
  349. for (cr = u->chanrec; cr; cr = cr->next) {
  350. if (!rfc_casecmp(chname, cr->channel))
  351. break;
  352. }
  353. }
  354. if (cr) {
  355. fr->chan = cr->flags;
  356. } else {
  357. fr->chan = 0;
  358. }
  359. }
  360. }
  361. }
  362. /* private returns 0 if user has access, and 1 if they dont because of +private
  363. * This function does not check if the user has "op" access, it only checks if the user is
  364. * restricted by +private for the channel
  365. */
  366. int
  367. privchan(struct flag_record fr, struct chanset_t *chan, int type)
  368. {
  369. if (!chan || !channel_privchan(chan) || glob_bot(fr) || glob_owner(fr))
  370. return 0; /* user is implicitly not restricted by +private, they may however be lacking other flags */
  371. if (type == PRIV_OP) {
  372. /* |o implies all flags above. n| has access to all +private. Bots are exempt. */
  373. if (chan_op(fr))
  374. return 0;
  375. } else if (type == PRIV_VOICE) {
  376. if (chan_voice(fr) || chan_op(fr))
  377. return 0;
  378. }
  379. return 1; /* user is restricted by +private */
  380. }
  381. int
  382. chk_op(struct flag_record fr, struct chanset_t *chan)
  383. {
  384. return real_chk_op(fr, chan, 1);
  385. }
  386. int
  387. real_chk_op(struct flag_record fr, struct chanset_t *chan, bool botbitch)
  388. {
  389. if (!chan)
  390. return 0;
  391. if (!privchan(fr, chan, PRIV_OP) && !real_chk_deop(fr, chan, botbitch)) {
  392. if (chan_op(fr) || (glob_op(fr) && !chan_deop(fr)))
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. int
  398. chk_autoop(struct flag_record fr, struct chanset_t *chan)
  399. {
  400. if (glob_bot(fr))
  401. return 0;
  402. if (!chan || (!channel_take(chan) && !privchan(fr, chan, PRIV_OP) && chk_op(fr, chan) && !chk_deop(fr, chan))) {
  403. if (channel_autoop(chan) || chan_autoop(fr) || glob_autoop(fr))
  404. return 1;
  405. }
  406. return 0;
  407. }
  408. int chk_deop(struct flag_record fr, struct chanset_t *chan)
  409. {
  410. return real_chk_deop(fr, chan, 1);
  411. }
  412. int
  413. real_chk_deop(struct flag_record fr, struct chanset_t *chan, bool botbitch)
  414. {
  415. if (chan && botbitch && channel_botbitch(chan) && !glob_bot(fr))
  416. return 1;
  417. if (chan_deop(fr) || (glob_deop(fr) && !chan_op(fr)))
  418. return 1;
  419. else
  420. return 0;
  421. }
  422. int
  423. chk_voice(struct flag_record fr, struct chanset_t *chan)
  424. {
  425. if (!chan || (!privchan(fr, chan, PRIV_VOICE) && !chk_devoice(fr))) {
  426. if (chan_voice(fr) || (glob_voice(fr) && !chan_quiet(fr)))
  427. return 1;
  428. }
  429. return 0;
  430. }
  431. int
  432. chk_devoice(struct flag_record fr)
  433. {
  434. if (chan_quiet(fr) || (glob_quiet(fr) && !chan_voice(fr)))
  435. return 1;
  436. else
  437. return 0;
  438. }
  439. int
  440. chk_noflood(struct flag_record fr)
  441. {
  442. return (chan_noflood(fr) || glob_noflood(fr));
  443. }
  444. int
  445. isupdatehub()
  446. {
  447. if (conf.bot->hub && conf.bot->u && (conf.bot->u->flags & USER_UPDATEHUB))
  448. return 1;
  449. else
  450. return 0;
  451. }
  452. int
  453. ischanhub()
  454. {
  455. if (!conf.bot->hub && conf.bot->u && (conf.bot->u->flags & USER_CHANHUB))
  456. return 1;
  457. else
  458. return 0;
  459. }
  460. int
  461. dovoice(struct chanset_t *chan)
  462. {
  463. if (!chan)
  464. return 0;
  465. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  466. get_user_flagrec(conf.bot->u, &fr, chan->dname);
  467. if (glob_dovoice(fr) || chan_dovoice(fr))
  468. return 1;
  469. return 0;
  470. }
  471. int
  472. dolimit(struct chanset_t *chan)
  473. {
  474. if (!chan)
  475. return 0;
  476. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  477. get_user_flagrec(conf.bot->u, &fr, chan->dname);
  478. if (glob_dolimit(fr) || chan_dolimit(fr))
  479. return 1;
  480. return 0;
  481. }
  482. int
  483. whois_access(struct userrec *user, struct userrec *whois_user)
  484. {
  485. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 }, whois = {
  486. FR_GLOBAL | FR_CHAN, 0, 0, 0};
  487. get_user_flagrec(user, &fr, NULL);
  488. get_user_flagrec(whois_user, &whois, NULL);
  489. if (
  490. (isowner(user->handle) && !isowner(user->handle)) ||
  491. (glob_admin(whois) && !glob_admin(fr)) ||
  492. (glob_owner(whois) && !glob_owner(fr)) ||
  493. (glob_master(whois) && !glob_master(fr)) ||
  494. (glob_bot(whois) && !glob_master(fr))
  495. )
  496. return 0;
  497. return 1;
  498. }