flags.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * flags.c -- handles:
  3. * all the flag matching/conversion functions in one neat package :)
  4. *
  5. * $Id: flags.c,v 1.9 2000/01/17 16:14:45 per Exp $
  6. */
  7. /*
  8. * Copyright (C) 1997 Robey Pointer
  9. * Copyright (C) 1999, 2000 Eggheads
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #include "main.h"
  26. extern int use_console_r,
  27. debug_output,
  28. noshare,
  29. allow_dk_cmds;
  30. extern struct dcc_t *dcc;
  31. int use_console_r = 0; /* allow users to set their console +r */
  32. int logmodes(char *s)
  33. {
  34. int i;
  35. int res = 0;
  36. for (i = 0; i < strlen(s); i++)
  37. switch (s[i]) {
  38. case 'm':
  39. case 'M':
  40. res |= LOG_MSGS;
  41. break;
  42. case 'p':
  43. case 'P':
  44. res |= LOG_PUBLIC;
  45. break;
  46. case 'j':
  47. case 'J':
  48. res |= LOG_JOIN;
  49. break;
  50. case 'k':
  51. case 'K':
  52. res |= LOG_MODES;
  53. break;
  54. case 'c':
  55. case 'C':
  56. res |= LOG_CMDS;
  57. break;
  58. case 'o':
  59. case 'O':
  60. res |= LOG_MISC;
  61. break;
  62. case 'b':
  63. case 'B':
  64. res |= LOG_BOTS;
  65. break;
  66. case 'r':
  67. case 'R':
  68. res |= use_console_r ? LOG_RAW : 0;
  69. break;
  70. case 'w':
  71. case 'W':
  72. res |= LOG_WALL;
  73. break;
  74. case 'x':
  75. case 'X':
  76. res |= LOG_FILES;
  77. break;
  78. case 's':
  79. case 'S':
  80. res |= LOG_SERV;
  81. break;
  82. case 'd':
  83. case 'D':
  84. res |= LOG_DEBUG;
  85. break;
  86. case 'v':
  87. case 'V':
  88. res |= debug_output ? LOG_SRVOUT : 0;
  89. break;
  90. case 't':
  91. case 'T':
  92. res |= debug_output ? LOG_BOTNET : 0;
  93. break;
  94. case 'h':
  95. case 'H':
  96. res |= debug_output ? LOG_BOTSHARE : 0;
  97. break;
  98. case '1':
  99. res |= LOG_LEV1;
  100. break;
  101. case '2':
  102. res |= LOG_LEV2;
  103. break;
  104. case '3':
  105. res |= LOG_LEV3;
  106. break;
  107. case '4':
  108. res |= LOG_LEV4;
  109. break;
  110. case '5':
  111. res |= LOG_LEV5;
  112. break;
  113. case '6':
  114. res |= LOG_LEV6;
  115. break;
  116. case '7':
  117. res |= LOG_LEV7;
  118. break;
  119. case '8':
  120. res |= LOG_LEV8;
  121. break;
  122. case '*':
  123. res |= LOG_ALL;
  124. break;
  125. }
  126. return res;
  127. }
  128. char *masktype(int x)
  129. {
  130. static char s[24]; /* change this if you change the levels */
  131. char *p = s;
  132. if (x & LOG_MSGS)
  133. *p++ = 'm';
  134. if (x & LOG_PUBLIC)
  135. *p++ = 'p';
  136. if (x & LOG_JOIN)
  137. *p++ = 'j';
  138. if (x & LOG_MODES)
  139. *p++ = 'k';
  140. if (x & LOG_CMDS)
  141. *p++ = 'c';
  142. if (x & LOG_MISC)
  143. *p++ = 'o';
  144. if (x & LOG_BOTS)
  145. *p++ = 'b';
  146. if ((x & LOG_RAW) && use_console_r)
  147. *p++ = 'r';
  148. if (x & LOG_FILES)
  149. *p++ = 'x';
  150. if (x & LOG_SERV)
  151. *p++ = 's';
  152. if (x & LOG_DEBUG)
  153. *p++ = 'd';
  154. if (x & LOG_WALL)
  155. *p++ = 'w';
  156. if ((x & LOG_SRVOUT) && debug_output)
  157. *p++ = 'v';
  158. if ((x & LOG_BOTNET) && debug_output)
  159. *p++ = 't';
  160. if ((x & LOG_BOTSHARE) && debug_output)
  161. *p++ = 'h';
  162. if (x & LOG_LEV1)
  163. *p++ = '1';
  164. if (x & LOG_LEV2)
  165. *p++ = '2';
  166. if (x & LOG_LEV3)
  167. *p++ = '3';
  168. if (x & LOG_LEV4)
  169. *p++ = '4';
  170. if (x & LOG_LEV5)
  171. *p++ = '5';
  172. if (x & LOG_LEV6)
  173. *p++ = '6';
  174. if (x & LOG_LEV7)
  175. *p++ = '7';
  176. if (x & LOG_LEV8)
  177. *p++ = '8';
  178. if (p == s)
  179. *p++ = '-';
  180. *p = 0;
  181. return s;
  182. }
  183. /* some flags are mutually exclusive -- this roots them out */
  184. int sanity_check(int atr)
  185. {
  186. if ((atr & USER_BOT) && (atr & (USER_PARTY | USER_MASTER | USER_COMMON | USER_OWNER)))
  187. atr &= ~(USER_PARTY | USER_MASTER | USER_COMMON | USER_OWNER);
  188. /* If +d or +k, kill everything else */
  189. if (atr & (USER_DEOP|USER_KICK))
  190. atr &= ~(USER_OP|USER_MASTER|USER_OWNER|USER_PARTY|USER_FRIEND);
  191. if ((atr & USER_VOICE) && (atr & USER_QUIET))
  192. atr &= ~(USER_VOICE);
  193. if ((atr & USER_GVOICE) && (atr & USER_QUIET))
  194. atr &= ~(USER_GVOICE);
  195. /* can't be owner without also being master */
  196. if (atr & USER_OWNER)
  197. atr |= USER_MASTER;
  198. /* master implies botmaster, op, friend and janitor */
  199. if (atr & USER_MASTER)
  200. atr |= USER_OP | USER_FRIEND;
  201. /* can't be botnet master without party-line access */
  202. if (atr & USER_MASTER)
  203. atr |= USER_PARTY;
  204. return atr;
  205. }
  206. /* sanity check on channel attributes */
  207. int chan_sanity_check(int chatr, int atr)
  208. {
  209. if ((chatr & USER_OP) && (chatr & USER_DEOP))
  210. chatr &= ~(USER_OP);
  211. if ((chatr & USER_VOICE) && (chatr & USER_QUIET))
  212. chatr &= ~(USER_VOICE);
  213. if ((chatr & USER_GVOICE) && (chatr & USER_QUIET))
  214. chatr &= ~(USER_GVOICE);
  215. /* can't be channel owner without also being channel master */
  216. if (chatr & USER_OWNER)
  217. chatr |= USER_MASTER;
  218. /* master implies friend & op */
  219. if (chatr & USER_MASTER)
  220. chatr |= USER_OP | USER_FRIEND;
  221. return chatr;
  222. }
  223. /* get icon symbol for a user (depending on access level)
  224. * (*)owner on any channel
  225. * (+)master on any channel
  226. * (%) botnet master
  227. * (@) op on any channel
  228. * (-) other */
  229. char geticon(int idx)
  230. {
  231. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0 };
  232. if (!dcc[idx].user)
  233. return '-';
  234. get_user_flagrec(dcc[idx].user, &fr, 0);
  235. if (chan_owner(fr))
  236. return '*';
  237. if (chan_master(fr))
  238. return '+';
  239. if (chan_op(fr))
  240. return '@';
  241. return '-';
  242. }
  243. void break_down_flags(char *string, struct flag_record *plus, struct flag_record *minus)
  244. {
  245. struct flag_record *which = plus;
  246. int mode = 0; /* 0 = glob, 1 = chan, 2 = bot */
  247. int flags = plus->match;
  248. if (!(flags & FR_GLOBAL)) {
  249. if (flags & FR_CHAN)
  250. mode = 1;
  251. else
  252. return; /* we dont actually want any..huh? */
  253. }
  254. bzero(plus, sizeof(struct flag_record));
  255. if (minus)
  256. bzero(minus, sizeof(struct flag_record));
  257. plus->match = FR_OR; /* befault binding type OR */
  258. while (*string) {
  259. switch (*string) {
  260. case '+':
  261. which = plus;
  262. break;
  263. case '-':
  264. which = minus ? minus : plus;
  265. break;
  266. case '|':
  267. case '&':
  268. if (mode == 0) {
  269. if (*string == '|')
  270. plus->match = FR_OR;
  271. else
  272. plus->match = FR_AND;
  273. }
  274. which = plus;
  275. mode++;
  276. if ((mode == 2) && !(flags & (FR_CHAN)))
  277. string = "";
  278. else if (mode == 3)
  279. mode = 1;
  280. break;
  281. default:
  282. if ((*string >= 'a') && (*string <= 'z')) {
  283. switch (mode) {
  284. case 0:
  285. which->global |=1 << (*string - 'a');
  286. break;
  287. case 1:
  288. which->chan |= 1 << (*string - 'a');
  289. break;
  290. }
  291. } else if ((*string >= 'A') && (*string <= 'Z')) {
  292. switch (mode) {
  293. case 0:
  294. which->udef_global |= 1 << (*string - 'A');
  295. break;
  296. case 1:
  297. which->udef_chan |= 1 << (*string - 'A');
  298. break;
  299. }
  300. } else if ((*string >= '0') && (*string <= '9')) {
  301. switch (mode) {
  302. /* map 0->9 to A->K for glob/chan so they are not lost */
  303. case 0:
  304. which->udef_global |= 1 << (*string - '0');
  305. break;
  306. case 1:
  307. which->udef_chan |= 1 << (*string - '0');
  308. break;
  309. }
  310. }
  311. }
  312. string++;
  313. }
  314. for (which = plus; which; which = (which == plus ? minus : 0)) {
  315. which->global &=USER_VALID;
  316. which->udef_global &= 0x03ffffff;
  317. which->chan &= CHAN_VALID;
  318. which->udef_chan &= 0x03ffffff;
  319. }
  320. plus->match |= flags;
  321. if (minus) {
  322. minus->match |= flags;
  323. if (!(plus->match & (FR_AND | FR_OR)))
  324. plus->match |= FR_OR;
  325. }
  326. }
  327. int flag2str(char *string, int bot, int udef)
  328. {
  329. char x = 'a',
  330. *old = string;
  331. while (bot && (x <= 'z')) {
  332. if (bot & 1)
  333. *string++ = x;
  334. x++;
  335. bot = bot >> 1;
  336. }
  337. x = 'A';
  338. while (udef && (x <= 'Z')) {
  339. if (udef & 1)
  340. *string++ = x;
  341. udef = udef >> 1;
  342. x++;
  343. }
  344. if (string == old)
  345. *string++ = '-';
  346. return string - old;
  347. }
  348. int build_flags(char *string, struct flag_record *plus, struct flag_record *minus)
  349. {
  350. char *old = string;
  351. if (plus->match & FR_GLOBAL) {
  352. if (minus && (plus->global ||plus->udef_global))
  353. *string++ = '+';
  354. string += flag2str(string, plus->global, plus->udef_global);
  355. if (minus && (minus->global ||minus->udef_global)) {
  356. *string++ = '-';
  357. string += flag2str(string, minus->global, minus->udef_global);
  358. }
  359. }
  360. if (plus->match & FR_CHAN) {
  361. if (plus->match & (FR_GLOBAL))
  362. *string++ = (plus->match & FR_AND) ? '&' : '|';
  363. if (minus && (plus->chan || plus->udef_chan))
  364. *string++ = '+';
  365. string += flag2str(string, plus->chan, plus->udef_chan);
  366. if (minus && (minus->chan || minus->udef_chan)) {
  367. *string++ = '-';
  368. string += flag2str(string, minus->global, minus->udef_chan);
  369. }
  370. }
  371. if (string == old) {
  372. *string++ = '-';
  373. *string = 0;
  374. return 0;
  375. }
  376. *string = 0;
  377. return string - old;
  378. }
  379. int flagrec_ok(struct flag_record *req, struct flag_record *have)
  380. {
  381. if (req->match & FR_AND) {
  382. return flagrec_eq(req, have);
  383. } else if (req->match & FR_OR) {
  384. int hav = have->global;
  385. /* exception 1 - global +d/+k cant use -|-, unless they are +p */
  386. if (!req->chan && !req->global &&!req->udef_global && !req->udef_chan) {
  387. if (!allow_dk_cmds) {
  388. if (glob_party(*have))
  389. return 1;
  390. if (glob_kick(*have) || chan_kick(*have))
  391. return 0; /* +k cant use -|- commands */
  392. if (glob_deop(*have) || chan_deop(*have))
  393. return 0; /* neither can +d's */
  394. }
  395. return 1;
  396. }
  397. /* the +n/+m checks arent needed anymore since +n/+m
  398. * automatically add lower flags */
  399. if (hav & req->global)
  400. return 1;
  401. if (have->chan & req->chan)
  402. return 1;
  403. if (have->udef_global & req->udef_global)
  404. return 1;
  405. if (have->udef_chan & req->udef_chan)
  406. return 1;
  407. return 0;
  408. }
  409. return 0; /* fr0k3 binding, dont pass it */
  410. }
  411. int flagrec_eq(struct flag_record *req, struct flag_record *have)
  412. {
  413. if (req->match & FR_AND) {
  414. if (req->match & FR_GLOBAL) {
  415. if ((req->global &have->global) !=req->global)
  416. return 0;
  417. if ((req->udef_global & have->udef_global) != req->udef_global)
  418. return 0;
  419. }
  420. if (req->match & FR_CHAN) {
  421. if ((req->chan & have->chan) != req->chan)
  422. return 0;
  423. if ((req->udef_chan & have->udef_chan) != req->udef_chan)
  424. return 0;
  425. }
  426. return 1;
  427. } else if (req->match & FR_OR) {
  428. if (!req->chan && !req->global &&!req->udef_chan && !req->udef_global)
  429. return 1;
  430. if (req->match & FR_GLOBAL) {
  431. if (have->global &req->global)
  432. return 1;
  433. if (have->udef_global & req->udef_global)
  434. return 1;
  435. }
  436. if (req->match & FR_CHAN) {
  437. if (have->chan & req->chan)
  438. return 1;
  439. if (have->udef_chan & req->udef_chan)
  440. return 1;
  441. }
  442. return 0;
  443. }
  444. return 0; /* fr0k3 binding, dont pass it */
  445. }
  446. void set_user_flagrec(struct userrec *u, struct flag_record *fr, char *chname)
  447. {
  448. struct chanuserrec *cr = NULL;
  449. int oldflags = fr->match;
  450. char buffer[100];
  451. struct chanset_t *ch;
  452. if (!u)
  453. return;
  454. if (oldflags & FR_GLOBAL) {
  455. if ( (u->flags & USER_BOT) && (u->flags & USER_OP) && !(fr->global & USER_OP)) {
  456. int i=nextbot(u->handle);
  457. if ((i>=0) && (!strcasecmp2(dcc[i].nick, u->handle))) {
  458. botunlink(-1, u->handle, STR("Rejected"));
  459. }
  460. }
  461. u->flags = fr->global;
  462. u->flags_udef = fr->udef_global;
  463. if (!noshare && !(u->flags & USER_UNSHARED)) {
  464. fr->match = FR_GLOBAL;
  465. build_flags(buffer, fr, NULL);
  466. shareout(NULL, STR("a %s %s\n"), u->handle, buffer);
  467. }
  468. }
  469. /* dont share bot attrs */
  470. if ((oldflags & FR_CHAN) && chname) {
  471. for (cr = u->chanrec; cr; cr = cr->next)
  472. if (!rfc_casecmp(chname, cr->channel))
  473. break;
  474. ch = findchan(chname);
  475. if (!cr && ch) {
  476. cr = user_malloc(sizeof(struct chanuserrec));
  477. bzero(cr, sizeof(struct chanuserrec));
  478. cr->next = u->chanrec;
  479. u->chanrec = cr;
  480. strncpy0(cr->channel, chname, 80);
  481. cr->channel[80] = 0;
  482. }
  483. if (cr && ch) {
  484. cr->flags = fr->chan;
  485. cr->flags_udef = fr->udef_chan;
  486. if (!noshare && !(u->flags & USER_UNSHARED)) {
  487. fr->match = FR_CHAN;
  488. build_flags(buffer, fr, NULL);
  489. shareout(ch, STR("a %s %s %s\n"), u->handle, buffer, chname);
  490. }
  491. }
  492. }
  493. fr->match = oldflags;
  494. }
  495. void get_user_flagrec(struct userrec *u, struct flag_record *fr, char *chname)
  496. {
  497. struct chanuserrec *cr = NULL;
  498. if (!u) {
  499. fr->global = fr->udef_global = fr->chan = fr->udef_chan = 0;
  500. return;
  501. }
  502. if (fr->match & FR_GLOBAL) {
  503. fr->global = u->flags;
  504. fr->udef_global = u->flags_udef;
  505. } else {
  506. fr->global = 0;
  507. fr->udef_global = 0;
  508. }
  509. if (fr->match & FR_CHAN) {
  510. if (fr->match & FR_ANYWH) {
  511. fr->chan = u->flags;
  512. fr->udef_chan = u->flags_udef;
  513. for (cr = u->chanrec; cr; cr = cr->next)
  514. if (findchan(cr->channel)) {
  515. fr->chan |= cr->flags;
  516. fr->udef_chan |= cr->flags_udef;
  517. }
  518. } else {
  519. if (chname)
  520. for (cr = u->chanrec; cr; cr = cr->next)
  521. if (!rfc_casecmp(chname, cr->channel))
  522. break;
  523. if (chname && cr) {
  524. fr->chan = cr->flags;
  525. fr->udef_chan = cr->flags_udef;
  526. } else {
  527. fr->chan = 0;
  528. fr->udef_chan = 0;
  529. }
  530. }
  531. }
  532. }