Commit | Line | Data |
---|---|---|
5df73aeb RK |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2007 Richard Kettlewell | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | /** @file lib/rights.c | |
21 | * @brief User rights | |
22 | */ | |
23 | ||
24 | #include <config.h> | |
25 | #include "types.h" | |
26 | ||
27 | #include <string.h> | |
28 | ||
29 | #include "mem.h" | |
30 | #include "log.h" | |
31 | #include "configuration.h" | |
32 | #include "rights.h" | |
33 | #include "vector.h" | |
34 | ||
35 | static const struct { | |
36 | rights_type bit; | |
37 | const char *name; | |
38 | } rights_names[] = { | |
39 | { RIGHT_READ, "read" }, | |
40 | { RIGHT_PLAY, "play" }, | |
41 | { RIGHT_MOVE_ANY, "move any" }, | |
42 | { RIGHT_MOVE_MINE, "move mine" }, | |
43 | { RIGHT_MOVE_RANDOM, "move random" }, | |
44 | { RIGHT_REMOVE_ANY, "remove any" }, | |
45 | { RIGHT_REMOVE_MINE, "remove mine" }, | |
46 | { RIGHT_REMOVE_RANDOM, "remove random" }, | |
47 | { RIGHT_SCRATCH_ANY, "scratch any" }, | |
48 | { RIGHT_SCRATCH_MINE, "scratch mine" }, | |
49 | { RIGHT_SCRATCH_RANDOM, "scratch random" }, | |
50 | { RIGHT_VOLUME, "volume" }, | |
51 | { RIGHT_ADMIN, "admin" }, | |
52 | { RIGHT_RESCAN, "rescan" }, | |
53 | { RIGHT_REGISTER, "register" }, | |
54 | { RIGHT_USERINFO, "userinfo" }, | |
55 | { RIGHT_PREFS, "prefs" }, | |
56 | { RIGHT_GLOBAL_PREFS, "global prefs" } | |
57 | }; | |
58 | #define NRIGHTS (sizeof rights_names / sizeof *rights_names) | |
59 | ||
60 | /** @brief Convert a rights word to a string */ | |
61 | char *rights_string(rights_type r) { | |
62 | struct dynstr d[1]; | |
63 | size_t n; | |
64 | ||
65 | dynstr_init(d); | |
66 | for(n = 0; n < NRIGHTS; ++n) { | |
67 | if(r & rights_names[n].bit) { | |
68 | if(d->nvec) | |
69 | dynstr_append(d, ','); | |
70 | dynstr_append_string(d, rights_names[n].name); | |
71 | } | |
72 | } | |
73 | dynstr_terminate(d); | |
74 | return d->vec; | |
75 | } | |
76 | ||
77 | /** @brief Compute default rights for a new user | |
78 | * @return Default rights value | |
79 | */ | |
80 | rights_type default_rights(void) { | |
81 | /* TODO get rights from config */ | |
82 | rights_type r = RIGHTS__MASK & ~(RIGHT_ADMIN|RIGHT_REGISTER | |
83 | |RIGHT_MOVE__MASK | |
84 | |RIGHT_SCRATCH__MASK | |
85 | |RIGHT_REMOVE__MASK); | |
86 | if(config->restrictions & RESTRICT_SCRATCH) | |
87 | r |= RIGHT_SCRATCH_MINE|RIGHT_SCRATCH_RANDOM; | |
88 | else | |
89 | r |= RIGHT_SCRATCH_ANY; | |
90 | if(!(config->restrictions & RESTRICT_MOVE)) | |
91 | r |= RIGHT_MOVE_ANY; | |
92 | if(config->restrictions & RESTRICT_REMOVE) | |
93 | r |= RIGHT_REMOVE_MINE; | |
94 | else | |
95 | r |= RIGHT_REMOVE_ANY; | |
96 | return r; | |
97 | } | |
98 | ||
99 | /** @brief Parse a rights list | |
100 | * @param s Rights list in string form | |
101 | * @param rp Where to store rights, or NULL to just validate | |
102 | * @return 0 on success, non-0 if @p s is not valid | |
103 | */ | |
104 | int parse_rights(const char *s, rights_type *rp) { | |
105 | rights_type r = 0; | |
106 | const char *t; | |
107 | size_t n, l; | |
108 | ||
109 | if(!*s) { | |
110 | /* You can't have no rights */ | |
111 | error(0, "empty rights string"); | |
112 | return -1; | |
113 | } | |
114 | while(*s) { | |
115 | t = strchr(s, ','); | |
116 | if(!t) | |
117 | t = s + strlen(s); | |
118 | l = (size_t)(t - s); | |
119 | if(l == 3 && !strncmp(s, "all", 3)) | |
120 | r = RIGHTS__MASK; | |
121 | else { | |
122 | for(n = 0; n < NRIGHTS; ++n) | |
123 | if(strlen(rights_names[n].name) == l | |
124 | && !strncmp(rights_names[n].name, s, l)) | |
125 | break; | |
126 | if(n >= NRIGHTS) { | |
127 | error(0, "unknown user right '%.*s'", (int)l, s); | |
128 | return -1; | |
129 | } | |
130 | r |= rights_names[n].bit; | |
131 | } | |
132 | s = t; | |
133 | if(*s == ',') | |
134 | ++s; | |
135 | } | |
136 | if(rp) | |
137 | *rp = r; | |
138 | return 0; | |
139 | } | |
140 | ||
141 | /* | |
142 | Local Variables: | |
143 | c-basic-offset:2 | |
144 | comment-column:40 | |
145 | fill-column:79 | |
146 | indent-tabs-mode:nil | |
147 | End: | |
148 | */ |