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