-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_pseudo.c
287 lines (245 loc) · 6 KB
/
cmd_pseudo.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
#include "common.h"
#include "cmd.h"
#include "pgsql.h"
#include "stringlist.h"
#include "serverinfo.h"
#include "input.h"
#include "table.h"
static char *pseudo_generator(const char *text, int state);
static char *pseudo_server_generator(const char *text, int state);
CMD_FUNC(pseudo_list);
CMD_FUNC(pseudo_add);
CMD_FUNC(pseudo_del);
CMD_TAB_FUNC(pseudo_del);
static struct command commands[] = {
CMD_STUB("pseudo", "Service Management"),
CMD_LIST_END
};
static struct command subcommands[] = {
// "pseudo" subcommands
CMD("list", pseudo_list, "Show pseudo-commands"),
CMD("add", pseudo_add, "Add a pseudo-command"),
CMD_TC("del", pseudo_del, "Remove a pseudo-command"),
CMD_LIST_END
};
static const char *tc_pseudo = NULL;
void cmd_pseudo_init()
{
cmd_register_list(commands, NULL);
cmd_register_list(subcommands, "pseudo");
cmd_alias("pseudos", "pseudo", "list");
cmd_alias("addpseudo", "pseudo", "add");
cmd_alias("delpseudo", "pseudo", "del");
}
CMD_FUNC(pseudo_list)
{
PGresult *res;
int rows;
struct table *table;
res = pgsql_query("SELECT command,\
name,\
target,\
prepend,\
COALESCE(server, '*') AS server\
FROM pseudos\
ORDER BY command ASC,\
server ISNULL DESC,\
server ASC",
1, NULL);
rows = pgsql_num_rows(res);
table = table_create(5, rows);
table_set_header(table, "Command", "Name", "Target", "Server", "Prepend");
for(int i = 0; i < rows; i++)
{
table_col_str(table, i, 0, (char*)pgsql_nvalue(res, i, "command"));
table_col_str(table, i, 1, (char*)pgsql_nvalue(res, i, "name"));
table_col_str(table, i, 2, (char*)pgsql_nvalue(res, i, "target"));
table_col_str(table, i, 3, (char*)pgsql_nvalue(res, i, "server"));
table_col_str(table, i, 4, (char*)pgsql_nvalue(res, i, "prepend"));
}
table_send(table);
table_free(table);
pgsql_free(res);
}
CMD_FUNC(pseudo_add)
{
const char *line, *tmp;
char *command = NULL, *server = NULL, *name = NULL, *target = NULL, *prepend = NULL;
int cnt;
// Prompt command
while(1)
{
line = readline_noac("Command", NULL);
if(!line || !*line)
return;
if(strchr(line, ' '))
{
error("Command may not contain spaces");
continue;
}
command = strdup(line);
break;
}
// Prompt server name
out("Use `*' if you want the pseudocommand to be used on all servers which don't override it.");
while(1)
{
line = readline_custom("Server", NULL, server_nohub_generator);
if(!line)
return;
else if(!*line)
goto out;
if(!strcmp(line, "*"))
server = NULL;
else
{
tmp = pgsql_query_str("SELECT name FROM servers WHERE lower(name) = lower($1) AND type != 'HUB'", stringlist_build(line, NULL));
if(!*tmp)
{
error("A server named `%s' does not exist", line);
continue;
}
server = strdup(tmp);
}
cnt = pgsql_query_int("SELECT COUNT(*) FROM pseudos WHERE lower(command) = lower($1) AND (server = $2 OR (server ISNULL AND $2 ISNULL))", stringlist_build_n(2, command, server));
if(cnt)
{
error("There is already a pseudocommand named `%s' on `%s'", command, line);
xfree(server);
continue;
}
break;
}
// Prompt name
while(1)
{
line = readline_noac("Service name", NULL);
if(!line)
return;
else if(!*line)
continue;
name = strdup(line);
break;
}
// Prompt target
while(1)
{
line = readline_noac("Target (nick@server)", NULL);
if(!line)
return;
else if(!*line)
continue;
if(match("*?@?*", line))
{
error("Target must be in nick@server format");
continue;
}
target = strdup(line);
break;
}
// Prompt prepend string
while(1)
{
line = readline_noac("Prepend", "");
if(!line)
return;
if(*line)
prepend = strdup(line);
break;
}
pgsql_query("INSERT INTO pseudos\
(command, name, target, prepend, server)\
VALUES\
(upper($1), $2, $3, $4, $5)",
0, stringlist_build_n(5, command, name, target, prepend, server));
out("Pseudo-command `%s' added successfully", command);
out:
xfree(command);
xfree(server);
xfree(name);
xfree(target);
xfree(prepend);
}
CMD_FUNC(pseudo_del)
{
if(argc < 2)
{
out("Usage: delpseudo <command> <server>");
return;
}
int cnt = pgsql_query_int("SELECT COUNT(*) FROM pseudos WHERE lower(command) = lower($1) AND (lower(server) = lower($2) OR (server ISNULL AND $2 = '*'))", stringlist_build(argv[1], argv[2], NULL));
if(!cnt)
{
error("There is no pseudo named `%s' on `%s'", argv[1], argv[2]);
return;
}
pgsql_query("DELETE FROM pseudos WHERE lower(command) = lower($1) AND (lower(server) = lower($2) OR (server ISNULL AND $2 = '*'))", 0, stringlist_build(argv[1], argv[2], NULL));
out("Pseudo-command `%s' deleted successfully", argv[1]);
}
// Tab completion stuff
CMD_TAB_FUNC(pseudo_del)
{
if(CAN_COMPLETE_ARG(1))
return pseudo_generator(text, state);
else if(CAN_COMPLETE_ARG(2))
{
tc_pseudo = tc_argv[1];
return pseudo_server_generator(text, state);
}
return NULL;
}
static char *pseudo_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 command FROM pseudos WHERE command ILIKE $1||'%'", 1, stringlist_build(text, 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 *pseudo_server_generator(const char *text, int state)
{
static int row, rows;
static size_t len;
static PGresult *res;
const char *name;
if(!state) // New word
{
assert(tc_pseudo);
row = 0;
len = strlen(text);
res = pgsql_query("SELECT COALESCE(server, '*') FROM pseudos WHERE lower(command) = lower($1)", 1, stringlist_build(tc_pseudo, 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;
}