chiark / gitweb /
Report disorder_eclient_volume() errors to the specific callback.
[disorder] / lib / rights.c
CommitLineData
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
05b75f8d 24#include "common.h"
5df73aeb 25
5df73aeb
RK
26
27#include "mem.h"
28#include "log.h"
29#include "configuration.h"
30#include "rights.h"
31#include "vector.h"
32
33static const struct {
34 rights_type bit;
35 const char *name;
36} rights_names[] = {
37 { RIGHT_READ, "read" },
38 { RIGHT_PLAY, "play" },
39 { RIGHT_MOVE_ANY, "move any" },
40 { RIGHT_MOVE_MINE, "move mine" },
41 { RIGHT_MOVE_RANDOM, "move random" },
42 { RIGHT_REMOVE_ANY, "remove any" },
43 { RIGHT_REMOVE_MINE, "remove mine" },
44 { RIGHT_REMOVE_RANDOM, "remove random" },
45 { RIGHT_SCRATCH_ANY, "scratch any" },
46 { RIGHT_SCRATCH_MINE, "scratch mine" },
47 { RIGHT_SCRATCH_RANDOM, "scratch random" },
48 { RIGHT_VOLUME, "volume" },
49 { RIGHT_ADMIN, "admin" },
50 { RIGHT_RESCAN, "rescan" },
51 { RIGHT_REGISTER, "register" },
52 { RIGHT_USERINFO, "userinfo" },
53 { RIGHT_PREFS, "prefs" },
eb5dc014
RK
54 { RIGHT_GLOBAL_PREFS, "global prefs" },
55 { RIGHT_PAUSE, "pause" }
5df73aeb
RK
56};
57#define NRIGHTS (sizeof rights_names / sizeof *rights_names)
58
59/** @brief Convert a rights word to a string */
60char *rights_string(rights_type r) {
61 struct dynstr d[1];
62 size_t n;
63
64 dynstr_init(d);
65 for(n = 0; n < NRIGHTS; ++n) {
66 if(r & rights_names[n].bit) {
67 if(d->nvec)
68 dynstr_append(d, ',');
69 dynstr_append_string(d, rights_names[n].name);
70 }
71 }
72 dynstr_terminate(d);
73 return d->vec;
74}
75
5df73aeb
RK
76/** @brief Parse a rights list
77 * @param s Rights list in string form
78 * @param rp Where to store rights, or NULL to just validate
0f55e905 79 * @param report Nonzero to log errors
5df73aeb
RK
80 * @return 0 on success, non-0 if @p s is not valid
81 */
0f55e905 82int parse_rights(const char *s, rights_type *rp, int report) {
5df73aeb
RK
83 rights_type r = 0;
84 const char *t;
85 size_t n, l;
86
87 if(!*s) {
88 /* You can't have no rights */
0f55e905
RK
89 if(report)
90 error(0, "empty rights string");
5df73aeb
RK
91 return -1;
92 }
93 while(*s) {
94 t = strchr(s, ',');
95 if(!t)
96 t = s + strlen(s);
97 l = (size_t)(t - s);
98 if(l == 3 && !strncmp(s, "all", 3))
99 r = RIGHTS__MASK;
100 else {
101 for(n = 0; n < NRIGHTS; ++n)
102 if(strlen(rights_names[n].name) == l
103 && !strncmp(rights_names[n].name, s, l))
104 break;
105 if(n >= NRIGHTS) {
0f55e905
RK
106 if(report)
107 error(0, "unknown user right '%.*s'", (int)l, s);
5df73aeb
RK
108 return -1;
109 }
110 r |= rights_names[n].bit;
111 }
112 s = t;
113 if(*s == ',')
114 ++s;
115 }
116 if(rp)
117 *rp = r;
118 return 0;
119}
120
121/*
122Local Variables:
123c-basic-offset:2
124comment-column:40
125fill-column:79
126indent-tabs-mode:nil
127End:
128*/