1 |
/* |
2 |
Copyright (C) 2002 Erik Fears |
3 |
|
4 |
This program is free software; you can redistribute it and/or |
5 |
modify it under the terms of the GNU General Public License |
6 |
as published by the Free Software Foundation; either version 2 |
7 |
of the License, or (at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
|
17 |
Foundation, Inc. |
18 |
59 Temple Place - Suite 330 |
19 |
Boston, MA 02111-1307, USA. |
20 |
|
21 |
*/ |
22 |
|
23 |
#include "setup.h" |
24 |
|
25 |
#ifdef STDC_HEADERS |
26 |
# include <string.h> |
27 |
# include <stdlib.h> |
28 |
#endif |
29 |
|
30 |
#include <sys/types.h> |
31 |
#include <netinet/in.h> |
32 |
#include <time.h> |
33 |
|
34 |
#include "options.h" |
35 |
#include "irc.h" |
36 |
#include "log.h" |
37 |
#include "misc.h" |
38 |
#include "opercmd.h" |
39 |
#include "scan.h" |
40 |
#include "config.h" |
41 |
#include "extern.h" |
42 |
#include "malloc.h" |
43 |
#include "list.h" |
44 |
#include "stats.h" |
45 |
|
46 |
/* List of active commands */ |
47 |
|
48 |
list_t *COMMANDS = NULL; |
49 |
|
50 |
|
51 |
static struct Command *command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target); |
52 |
static void command_free(struct Command *); |
53 |
|
54 |
static void cmd_check(char *, char *, struct ChannelConf *); |
55 |
static void cmd_stat(char *, char *, struct ChannelConf *); |
56 |
static void cmd_fdstat(char *, char *, struct ChannelConf *); |
57 |
#if 0 |
58 |
static void cmd_op(char *, char *, struct ChannelConf *); |
59 |
#endif |
60 |
|
61 |
static struct OperCommandHash COMMAND_TABLE[] = |
62 |
{ |
63 |
{"CHECK", cmd_check }, |
64 |
{"SCAN", cmd_check }, |
65 |
{"STAT", cmd_stat }, |
66 |
{"STATS", cmd_stat }, |
67 |
{"STATUS", cmd_stat }, |
68 |
{"FDSTAT", cmd_fdstat }, |
69 |
/* {"OP", cmd_op } */ |
70 |
}; |
71 |
|
72 |
|
73 |
|
74 |
/* command_init |
75 |
* |
76 |
* Do command initialization |
77 |
* |
78 |
* Parameters: NONE |
79 |
* Return: NONE |
80 |
* |
81 |
*/ |
82 |
|
83 |
void command_init() |
84 |
{ |
85 |
if(COMMANDS == NULL) |
86 |
COMMANDS = list_create(); |
87 |
} |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
/* command_timer |
93 |
* |
94 |
* Perform ~1 second actions. |
95 |
* |
96 |
* Parameters: NONE |
97 |
* |
98 |
* Return: NONE |
99 |
* |
100 |
*/ |
101 |
|
102 |
void command_timer() |
103 |
{ |
104 |
|
105 |
static unsigned short interval; |
106 |
|
107 |
node_t *node, *next; |
108 |
struct Command *cs; |
109 |
time_t present; |
110 |
|
111 |
/* Only perform command removal every COMMANDINTERVAL seconds */ |
112 |
if(interval++ < COMMANDINTERVAL) |
113 |
return; |
114 |
else |
115 |
interval = 0; |
116 |
|
117 |
time(&present); |
118 |
|
119 |
LIST_FOREACH_SAFE(node, next, COMMANDS->head) |
120 |
{ |
121 |
cs = (struct Command *) node->data; |
122 |
if((present - cs->added) > COMMANDTIMEOUT) |
123 |
{ |
124 |
command_free(cs); |
125 |
list_remove(COMMANDS, node); |
126 |
node_free(node); |
127 |
} |
128 |
else /* Since the queue is in order, it's also ordered by time, no nodes after this will be timed out */ |
129 |
return; |
130 |
} |
131 |
} |
132 |
|
133 |
|
134 |
/* command_parse |
135 |
* |
136 |
* Parse a command to hopm (sent to a channel hopm is on). The command is parsed |
137 |
* from the parameters, and if it is a known command it is stored in a queue. A |
138 |
* userhost is performed on the user to check if they are an IRC operator. When |
139 |
* a reply is returned (command_userhost), the command will be executed. |
140 |
* |
141 |
* Parameters: |
142 |
* command: Command sent (including parameters) |
143 |
* msg: Original PRIVMSG containing the command |
144 |
* target: Channel command was sent to (we only got this far if there was only one recipient) |
145 |
* source_p: Operator (hopefully) that sent the command. |
146 |
* |
147 |
*/ |
148 |
|
149 |
void command_parse(char *command, char *msg, struct ChannelConf *target, |
150 |
struct UserInfo *source_p) |
151 |
{ |
152 |
unsigned int i; |
153 |
char *param; /* Parsed parameters */ |
154 |
|
155 |
struct Command *cs; |
156 |
node_t *node; |
157 |
|
158 |
USE_VAR(msg); |
159 |
|
160 |
if(OPT_DEBUG) |
161 |
{ |
162 |
log_printf("COMMAND -> Parsing command (%s) from %s [%s]", command, |
163 |
source_p->irc_nick, target->name); |
164 |
} |
165 |
|
166 |
/* Only allow COMMANDMAX commands in the queue */ |
167 |
if(LIST_SIZE(COMMANDS) >= COMMANDMAX) |
168 |
return; |
169 |
|
170 |
/* Parameter is the first character in command after the first space. |
171 |
param will be NULL if: |
172 |
1. There was no space |
173 |
2. There was a space but it was the last character in command, in which case |
174 |
param = '\0' |
175 |
*/ |
176 |
|
177 |
/* Skip past the botname/!all */ |
178 |
command = strchr(command, ' '); |
179 |
|
180 |
/* There is no command OR |
181 |
there is at least nothing |
182 |
past that first space. */ |
183 |
if(command == NULL || |
184 |
command++ == NULL) |
185 |
return; |
186 |
|
187 |
|
188 |
/* Find the parameters */ |
189 |
param = strchr(command, ' '); |
190 |
|
191 |
if(param != NULL) |
192 |
{ |
193 |
*param = '\0'; |
194 |
param++; |
195 |
} |
196 |
else |
197 |
param = ""; |
198 |
|
199 |
log_printf("COMMAND -> parsed [%s] [%s]", command, param); |
200 |
|
201 |
/* Lookup the command in the table */ |
202 |
for(i = 0; i < sizeof(COMMAND_TABLE) / sizeof(struct OperCommandHash); i++) |
203 |
{ |
204 |
if(strcasecmp(command, COMMAND_TABLE[i].command) == 0) |
205 |
{ |
206 |
/* Queue this command */ |
207 |
cs = command_create(i, param, source_p->irc_nick, target); |
208 |
node = node_create(cs); |
209 |
list_add(COMMANDS, node); |
210 |
} |
211 |
} |
212 |
|
213 |
irc_send("USERHOST %s", source_p->irc_nick); |
214 |
} |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
/* command_create |
220 |
* |
221 |
* Create a Command struct. |
222 |
* |
223 |
* Parameters: |
224 |
* type: Index in COMMAND_TABLE |
225 |
* param: Parameters to the command (NULL if there are not any) |
226 |
* irc_nick: Nickname of user that initiated the command |
227 |
* target: Target channel (target is ALWAYS a channel) |
228 |
* |
229 |
* Return: |
230 |
* Pointer to new Command |
231 |
*/ |
232 |
|
233 |
static struct Command *command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target) |
234 |
{ |
235 |
struct Command *ret; |
236 |
|
237 |
ret = MyMalloc(sizeof *ret); |
238 |
|
239 |
ret->type = type; |
240 |
|
241 |
if(param != NULL) |
242 |
ret->param = DupString(param); |
243 |
else |
244 |
ret->param = NULL; |
245 |
|
246 |
ret->irc_nick = (char *) DupString(irc_nick); |
247 |
ret->target = target; /* FIXME: This needs fixed if rehash is implemented */ |
248 |
|
249 |
time(&(ret->added)); |
250 |
|
251 |
return ret; |
252 |
|
253 |
} |
254 |
|
255 |
|
256 |
|
257 |
/* command_free |
258 |
* |
259 |
* Free a command struct |
260 |
* |
261 |
* Parameters: |
262 |
* command: Command struct to free |
263 |
* |
264 |
* Return: NONE |
265 |
*/ |
266 |
|
267 |
static void command_free(struct Command *command) |
268 |
{ |
269 |
if(command->param != NULL) |
270 |
MyFree(command->param); |
271 |
MyFree(command->irc_nick); |
272 |
MyFree(command); |
273 |
} |
274 |
|
275 |
|
276 |
|
277 |
|
278 |
/* command_userhost |
279 |
* |
280 |
* A 302 reply was received. The reply is parsed to check if the |
281 |
* user was an operator. If so any commands they had queued are |
282 |
* executed. |
283 |
* |
284 |
* Parameters: |
285 |
* reply: Reply to USERHOST (ex: :grifferz*=+goats@pc-62-30-219-54-pb.blueyonder.co.uk) |
286 |
* |
287 |
* Return: NONE |
288 |
* |
289 |
*/ |
290 |
|
291 |
void command_userhost(char *reply) |
292 |
{ |
293 |
node_t *node, *next; |
294 |
struct Command *cs; |
295 |
char *tmp; |
296 |
|
297 |
int oper = 0; |
298 |
|
299 |
tmp = strchr(reply, '='); |
300 |
|
301 |
/* They quit, ignore it */ |
302 |
if (!tmp) |
303 |
return; |
304 |
|
305 |
/* Operators have a * flag in a USERHOST reply */ |
306 |
if (*(tmp - 1) == '*') |
307 |
oper = 1; |
308 |
|
309 |
/* Null terminate it so tmp = the oper's nick */ |
310 |
if(oper) |
311 |
*(--tmp) = '\0'; |
312 |
else |
313 |
*(tmp) = '\0'; |
314 |
|
315 |
/* Find any queued commands that match this user */ |
316 |
LIST_FOREACH_SAFE(node, next, COMMANDS->head) |
317 |
{ |
318 |
cs = (struct Command *) node->data; |
319 |
|
320 |
if(strcmp(cs->irc_nick, reply) == 0) |
321 |
{ |
322 |
if(oper) |
323 |
COMMAND_TABLE[cs->type].handler(cs->param, cs->irc_nick, cs->target); |
324 |
|
325 |
/* Cleanup the command */ |
326 |
command_free(cs); |
327 |
list_remove(COMMANDS, node); |
328 |
node_free(node); |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
|
334 |
|
335 |
/* cmd_check |
336 |
* |
337 |
* Start a manual scan on given IP. Parameter MUST be an IP. HOPM should not |
338 |
* have to waste any time resolving a hostname. |
339 |
* |
340 |
* Parameters: |
341 |
* param: Parameters of the command |
342 |
* source: irc_nick of user who requested the command |
343 |
* target: channel command was sent to |
344 |
* |
345 |
*/ |
346 |
|
347 |
static void cmd_check(char *param, char *source, struct ChannelConf *target) |
348 |
{ |
349 |
USE_VAR(source); |
350 |
|
351 |
scan_manual(param, target); |
352 |
} |
353 |
|
354 |
|
355 |
|
356 |
/* cmd_stat |
357 |
* |
358 |
* Send output of stats to channel. |
359 |
* |
360 |
* Parameters: |
361 |
* param: Parameters of the command |
362 |
* source: irc_nick of user who requested the command |
363 |
* target: channel command was sent to |
364 |
*/ |
365 |
|
366 |
static void cmd_stat(char *param, char *source, struct ChannelConf *target) |
367 |
{ |
368 |
USE_VAR(param); |
369 |
USE_VAR(source); |
370 |
|
371 |
stats_output(target->name); |
372 |
} |
373 |
|
374 |
|
375 |
/* cmd_fdstat |
376 |
* |
377 |
* Send output of stats to channel. |
378 |
* |
379 |
* Parameters: |
380 |
* param: Parameters of the command |
381 |
* source: irc_nick of user who requested the command |
382 |
* target: channel command was sent to |
383 |
*/ |
384 |
|
385 |
static void cmd_fdstat(char *param, char *source, struct ChannelConf *target) |
386 |
{ |
387 |
USE_VAR(param); |
388 |
USE_VAR(source); |
389 |
|
390 |
fdstats_output(target->name); |
391 |
} |
392 |
|
393 |
|
394 |
/* cmd_op |
395 |
* |
396 |
* Op a user on the channel. |
397 |
* |
398 |
* Parameters: |
399 |
* param: Parameters of the command |
400 |
* source: irc_nick of user who requested the command |
401 |
* target: channel command was sent to |
402 |
* |
403 |
* XXX - Doesn't seem to currently be in use anywhere? |
404 |
* -grifferz |
405 |
*/ |
406 |
|
407 |
#if 0 |
408 |
static void cmd_op(char *param, char *source, struct ChannelConf *target) |
409 |
{ |
410 |
irc_send("MODE %s +o %s", target->name, param); |
411 |
|
412 |
USE_VAR(source); |
413 |
} |
414 |
#endif |