-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_feature.c
290 lines (247 loc) · 6.05 KB
/
cmd_feature.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "common.h"
#include "cmd.h"
#include "pgsql.h"
#include "stringlist.h"
#include "serverinfo.h"
#include "input.h"
#include "table.h"
static char *feature_generator(const char *text, int state);
static char *feature_all_srvtypes_generator(const char *text, int state);
static char *feature_srvtype_generator(const char *text, int state);
CMD_FUNC(feature_list);
CMD_FUNC(feature_set);
CMD_TAB_FUNC(feature_set);
CMD_FUNC(feature_del);
CMD_TAB_FUNC(feature_del);
static const char *feat_name_tc = NULL;
static struct command commands[] = {
CMD_STUB("feature", "Feature Management"),
CMD_LIST_END
};
static struct command subcommands[] = {
// "feature" subcommands
CMD("list", feature_list, "Show features"),
CMD_TC("set", feature_set, "Set a feature"),
CMD_TC("del", feature_del, "Remove a feature"),
CMD_LIST_END
};
void cmd_feature_init()
{
cmd_register_list(commands, NULL);
cmd_register_list(subcommands, "feature");
cmd_alias("features", "feature", "list");
cmd_alias("setfeature", "feature", "set");
cmd_alias("delfeature", "feature", "del");
}
CMD_FUNC(feature_list)
{
PGresult *res;
int rows;
struct table *table;
res = pgsql_query("SELECT * FROM features ORDER BY name ASC, server_type ASC", 1, NULL);
rows = pgsql_num_rows(res);
table = table_create(3, rows);
table_set_header(table, "Name", "Servers", "Value");
for(int i = 0; i < rows; i++)
{
table_col_str(table, i, 0, (char*)pgsql_nvalue(res, i, "name"));
table_col_str(table, i, 1, (char*)pgsql_nvalue(res, i, "server_type"));
table_col_str(table, i, 2, (char*)pgsql_nvalue(res, i, "value"));
}
table_send(table);
table_free(table);
pgsql_free(res);
}
CMD_FUNC(feature_set)
{
const char *line;
char prefix[2] = "";
const char *value;
char *str;
int exists;
if(argc < 3)
{
out("Usage: setfeature <name> <*|server-type>");
return;
}
// Validate feature name
for(str = argv[1]; *str; str++)
{
if(*str != '_' && !isupper(*str))
{
error("Invalid feature name (unexpected char: '%c')", *str);
return;
}
}
// Validate server type
if(strcmp(argv[2], "*") && strcmp(argv[2], "LEAF") &&
strcmp(argv[2], "HUB") && strcmp(argv[2], "STAFF") &&
strcmp(argv[2], "BOTS"))
{
error("Invalid server type");
return;
}
pgsql_begin();
exists = pgsql_query_int("SELECT COUNT(*) FROM features WHERE name = $1 AND server_type = $2", stringlist_build(argv[1], argv[2], NULL));
if(!exists && !readline_yesno("Feature does not exist yet. Add it?", "Yes"))
{
pgsql_rollback();
return;
}
// Fetch old value
str = pgsql_query_str("SELECT value FROM features WHERE name = $1 AND server_type = $2", stringlist_build(argv[1], argv[2], NULL));
value = readline_noac("Value", exists ? str : NULL);
if(!value)
{
pgsql_rollback();
return;
}
if(exists && !strcmp(value, str))
{
out("No change made");
pgsql_rollback();
return;
}
if(*str)
{
out("Overwriting old value `%s'", str);
pgsql_query("UPDATE features\
SET value = $1\
WHERE name = $2 AND\
server_type = $3",
0, stringlist_build(value, argv[1], argv[2], NULL));
}
else
{
pgsql_query("INSERT INTO features\
(name, value, server_type)\
VALUES\
($1, $2, $3)",
0, stringlist_build(argv[1], value, argv[2], NULL));
}
pgsql_commit();
out("Feature `%s' set successfully for `%s'", argv[1], argv[2]);
}
CMD_FUNC(feature_del)
{
int cnt;
if(argc < 3)
{
out("Usage: delfeature <name> <*|server-type>");
return;
}
// Validate server type
if(strcmp(argv[2], "*") && strcmp(argv[2], "LEAF") &&
strcmp(argv[2], "HUB") && strcmp(argv[2], "STAFF") &&
strcmp(argv[2], "BOTS"))
{
error("Invalid server type");
return;
}
cnt = pgsql_query_int("SELECT COUNT(*) FROM features WHERE name = $1 AND server_type = $2", stringlist_build(argv[1], argv[2], NULL));
if(!cnt)
{
error("This feature is not set for `%s'", argv[2]);
return;
}
pgsql_query("DELETE FROM features WHERE name = $1 AND server_type = $2", 0, stringlist_build(argv[1], argv[2], NULL));
out("Feature `%s' deleted successfully from `%s'", argv[1], argv[2]);
}
// Tab completion stuff
CMD_TAB_FUNC(feature_set)
{
if(CAN_COMPLETE_ARG(1))
return feature_generator(text, state);
else if(CAN_COMPLETE_ARG(2))
return feature_all_srvtypes_generator(text, state);
return NULL;
}
CMD_TAB_FUNC(feature_del)
{
if(CAN_COMPLETE_ARG(1))
return feature_generator(text, state);
if(CAN_COMPLETE_ARG(2))
{
feat_name_tc = tc_argv[1];
return feature_srvtype_generator(text, state);
}
return NULL;
}
static char *feature_generator(const char *text, int state)
{
static int row, rows;
static size_t len;
static PGresult *res;
const char *name;
if(!state) // New word
{
row = 0;
len = strlen(text);
res = pgsql_query("SELECT DISTINCT name FROM features", 1, NULL);
rows = pgsql_num_rows(res);
}
else if(state == -1) // Cleanup
{
pgsql_free(res);
return NULL;
}
while(row < rows)
{
name = pgsql_value(res, row, 0);
row++;
if(!strncasecmp(name, text, len))
return strdup(name);
}
return NULL;
}
static char *feature_srvtype_generator(const char *text, int state)
{
static int row, rows;
static size_t len;
static PGresult *res;
const char *name;
if(!state) // New word
{
row = 0;
len = strlen(text);
res = pgsql_query("SELECT server_type FROM features WHERE name = $1", 1, stringlist_build(feat_name_tc, NULL));
rows = pgsql_num_rows(res);
}
else if(state == -1) // Cleanup
{
pgsql_free(res);
return NULL;
}
while(row < rows)
{
name = pgsql_value(res, row, 0);
row++;
if(!strncasecmp(name, text, len))
return strdup(name);
}
return NULL;
}
static char *feature_all_srvtypes_generator(const char *text, int state)
{
static const char *values[] = { "*", "LEAF", "HUB", "STAFF", "BOTS", NULL };
static int idx;
static size_t len;
const char *val;
if(!state) // New word
{
len = strlen(text);
idx = 0;
}
else if(state == -1) // Cleanup
{
return NULL;
}
// Return the next name which partially matches from the command list.
while((val = values[idx]))
{
idx++;
if(!strncasecmp(val, text, len))
return strdup(val);
}
return NULL;
}