1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* User manager (root) commands */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "commands.h" |
---|
28 | #include "bitlbee.h" |
---|
29 | #include "help.h" |
---|
30 | |
---|
31 | #include <string.h> |
---|
32 | |
---|
33 | void root_command_string( irc_t *irc, char *command ) |
---|
34 | { |
---|
35 | root_command( irc, split_command_parts( command ) ); |
---|
36 | } |
---|
37 | |
---|
38 | #define MIN_ARGS( x, y... ) \ |
---|
39 | do \ |
---|
40 | { \ |
---|
41 | int blaat; \ |
---|
42 | for( blaat = 0; blaat <= x; blaat ++ ) \ |
---|
43 | if( cmd[blaat] == NULL ) \ |
---|
44 | { \ |
---|
45 | irc_usermsg( irc, "Not enough parameters given (need %d).", x ); \ |
---|
46 | return y; \ |
---|
47 | } \ |
---|
48 | } while( 0 ) |
---|
49 | |
---|
50 | void root_command( irc_t *irc, char *cmd[] ) |
---|
51 | { |
---|
52 | int i, len; |
---|
53 | |
---|
54 | if( !cmd[0] ) |
---|
55 | return; |
---|
56 | |
---|
57 | len = strlen( cmd[0] ); |
---|
58 | for( i = 0; commands[i].command; i++ ) |
---|
59 | if( g_strncasecmp( commands[i].command, cmd[0], len ) == 0 ) |
---|
60 | { |
---|
61 | if( commands[i+1].command && |
---|
62 | g_strncasecmp( commands[i+1].command, cmd[0], len ) == 0 ) |
---|
63 | /* Only match on the first letters if the match is unique. */ |
---|
64 | break; |
---|
65 | |
---|
66 | MIN_ARGS( commands[i].required_parameters ); |
---|
67 | |
---|
68 | commands[i].execute( irc, cmd ); |
---|
69 | return; |
---|
70 | } |
---|
71 | |
---|
72 | irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] ); |
---|
73 | } |
---|
74 | |
---|
75 | static void cmd_help( irc_t *irc, char **cmd ) |
---|
76 | { |
---|
77 | char param[80]; |
---|
78 | int i; |
---|
79 | char *s; |
---|
80 | |
---|
81 | memset( param, 0, sizeof(param) ); |
---|
82 | for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) { |
---|
83 | if ( i != 1 ) // prepend space except for the first parameter |
---|
84 | strcat(param, " "); |
---|
85 | strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 ); |
---|
86 | } |
---|
87 | |
---|
88 | s = help_get( &(global.help), param ); |
---|
89 | if( !s ) s = help_get( &(global.help), "" ); |
---|
90 | |
---|
91 | if( s ) |
---|
92 | { |
---|
93 | irc_usermsg( irc, "%s", s ); |
---|
94 | g_free( s ); |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | irc_usermsg( irc, "Error opening helpfile." ); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | static void cmd_account( irc_t *irc, char **cmd ); |
---|
103 | |
---|
104 | static void cmd_identify( irc_t *irc, char **cmd ) |
---|
105 | { |
---|
106 | storage_status_t status; |
---|
107 | char *account_on[] = { "account", "on", NULL }; |
---|
108 | gboolean load = TRUE; |
---|
109 | char *password = cmd[1]; |
---|
110 | |
---|
111 | if( irc->status & USTATUS_IDENTIFIED ) |
---|
112 | { |
---|
113 | irc_usermsg( irc, "You're already logged in." ); |
---|
114 | return; |
---|
115 | } |
---|
116 | |
---|
117 | if( strncmp( cmd[1], "-no", 3 ) == 0 ) |
---|
118 | { |
---|
119 | load = FALSE; |
---|
120 | password = cmd[2]; |
---|
121 | } |
---|
122 | else if( strncmp( cmd[1], "-force", 6 ) == 0 ) |
---|
123 | { |
---|
124 | password = cmd[2]; |
---|
125 | } |
---|
126 | else if( irc->b->accounts != NULL ) |
---|
127 | { |
---|
128 | irc_usermsg( irc, |
---|
129 | "You're trying to identify yourself, but already have " |
---|
130 | "at least one IM account set up. " |
---|
131 | "Use \x02identify -noload\x02 or \x02identify -force\x02 " |
---|
132 | "instead (see \x02help identify\x02)." ); |
---|
133 | return; |
---|
134 | } |
---|
135 | |
---|
136 | if( password == NULL ) |
---|
137 | { |
---|
138 | MIN_ARGS( 2 ); |
---|
139 | } |
---|
140 | |
---|
141 | if( load ) |
---|
142 | status = storage_load( irc, password ); |
---|
143 | else |
---|
144 | status = storage_check_pass( irc->user->nick, password ); |
---|
145 | |
---|
146 | switch (status) { |
---|
147 | case STORAGE_INVALID_PASSWORD: |
---|
148 | irc_usermsg( irc, "Incorrect password" ); |
---|
149 | break; |
---|
150 | case STORAGE_NO_SUCH_USER: |
---|
151 | irc_usermsg( irc, "The nick is (probably) not registered" ); |
---|
152 | break; |
---|
153 | case STORAGE_OK: |
---|
154 | irc_usermsg( irc, "Password accepted%s", |
---|
155 | load ? ", settings and accounts loaded" : "" ); |
---|
156 | irc_setpass( irc, password ); |
---|
157 | irc->status |= USTATUS_IDENTIFIED; |
---|
158 | irc_umode_set( irc, "+R", 1 ); |
---|
159 | irc_channel_auto_joins( irc, NULL ); |
---|
160 | if( load && set_getbool( &irc->b->set, "auto_connect" ) ) |
---|
161 | cmd_account( irc, account_on ); |
---|
162 | break; |
---|
163 | case STORAGE_OTHER_ERROR: |
---|
164 | default: |
---|
165 | irc_usermsg( irc, "Unknown error while loading configuration" ); |
---|
166 | break; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | static void cmd_register( irc_t *irc, char **cmd ) |
---|
171 | { |
---|
172 | if( global.conf->authmode == AUTHMODE_REGISTERED ) |
---|
173 | { |
---|
174 | irc_usermsg( irc, "This server does not allow registering new accounts" ); |
---|
175 | return; |
---|
176 | } |
---|
177 | |
---|
178 | switch( storage_save( irc, cmd[1], FALSE ) ) { |
---|
179 | case STORAGE_ALREADY_EXISTS: |
---|
180 | irc_usermsg( irc, "Nick is already registered" ); |
---|
181 | break; |
---|
182 | |
---|
183 | case STORAGE_OK: |
---|
184 | irc_usermsg( irc, "Account successfully created" ); |
---|
185 | irc_setpass( irc, cmd[1] ); |
---|
186 | irc->status |= USTATUS_IDENTIFIED; |
---|
187 | irc_umode_set( irc, "+R", 1 ); |
---|
188 | break; |
---|
189 | |
---|
190 | default: |
---|
191 | irc_usermsg( irc, "Error registering" ); |
---|
192 | break; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | static void cmd_drop( irc_t *irc, char **cmd ) |
---|
197 | { |
---|
198 | storage_status_t status; |
---|
199 | |
---|
200 | status = storage_remove (irc->user->nick, cmd[1]); |
---|
201 | switch (status) { |
---|
202 | case STORAGE_NO_SUCH_USER: |
---|
203 | irc_usermsg( irc, "That account does not exist" ); |
---|
204 | break; |
---|
205 | case STORAGE_INVALID_PASSWORD: |
---|
206 | irc_usermsg( irc, "Password invalid" ); |
---|
207 | break; |
---|
208 | case STORAGE_OK: |
---|
209 | irc_setpass( irc, NULL ); |
---|
210 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
211 | irc_umode_set( irc, "-R", 1 ); |
---|
212 | irc_usermsg( irc, "Account `%s' removed", irc->user->nick ); |
---|
213 | break; |
---|
214 | default: |
---|
215 | irc_usermsg( irc, "Error: `%d'", status ); |
---|
216 | break; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | static void cmd_save( irc_t *irc, char **cmd ) |
---|
221 | { |
---|
222 | if( ( irc->status & USTATUS_IDENTIFIED ) == 0 ) |
---|
223 | irc_usermsg( irc, "Please create an account first" ); |
---|
224 | else if( storage_save( irc, NULL, TRUE ) == STORAGE_OK ) |
---|
225 | irc_usermsg( irc, "Configuration saved" ); |
---|
226 | else |
---|
227 | irc_usermsg( irc, "Configuration could not be saved!" ); |
---|
228 | } |
---|
229 | |
---|
230 | static void cmd_showset( irc_t *irc, set_t **head, char *key ) |
---|
231 | { |
---|
232 | char *val; |
---|
233 | |
---|
234 | if( ( val = set_getstr( head, key ) ) ) |
---|
235 | irc_usermsg( irc, "%s = `%s'", key, val ); |
---|
236 | else |
---|
237 | irc_usermsg( irc, "%s is empty", key ); |
---|
238 | } |
---|
239 | |
---|
240 | typedef set_t** (*cmd_set_findhead)( irc_t*, char* ); |
---|
241 | typedef int (*cmd_set_checkflags)( irc_t*, set_t *set ); |
---|
242 | |
---|
243 | static int cmd_set_real( irc_t *irc, char **cmd, set_t **head, cmd_set_checkflags checkflags ) |
---|
244 | { |
---|
245 | char *set_name = NULL, *value = NULL; |
---|
246 | gboolean del = FALSE; |
---|
247 | |
---|
248 | if( cmd[1] && g_strncasecmp( cmd[1], "-del", 4 ) == 0 ) |
---|
249 | { |
---|
250 | MIN_ARGS( 2, 0 ); |
---|
251 | set_name = cmd[2]; |
---|
252 | del = TRUE; |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | set_name = cmd[1]; |
---|
257 | value = cmd[2]; |
---|
258 | } |
---|
259 | |
---|
260 | if( set_name && ( value || del ) ) |
---|
261 | { |
---|
262 | set_t *s = set_find( head, set_name ); |
---|
263 | int st; |
---|
264 | |
---|
265 | if( s && checkflags && checkflags( irc, s ) == 0 ) |
---|
266 | return 0; |
---|
267 | |
---|
268 | if( del ) |
---|
269 | st = set_reset( head, set_name ); |
---|
270 | else |
---|
271 | st = set_setstr( head, set_name, value ); |
---|
272 | |
---|
273 | if( set_getstr( head, set_name ) == NULL ) |
---|
274 | { |
---|
275 | /* This happens when changing the passwd, for example. |
---|
276 | Showing these msgs instead gives slightly clearer |
---|
277 | feedback. */ |
---|
278 | if( st ) |
---|
279 | irc_usermsg( irc, "Setting changed successfully" ); |
---|
280 | else |
---|
281 | irc_usermsg( irc, "Failed to change setting" ); |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | cmd_showset( irc, head, set_name ); |
---|
286 | } |
---|
287 | } |
---|
288 | else if( set_name ) |
---|
289 | { |
---|
290 | cmd_showset( irc, head, set_name ); |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | set_t *s = *head; |
---|
295 | while( s ) |
---|
296 | { |
---|
297 | cmd_showset( irc, &s, s->key ); |
---|
298 | s = s->next; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | return 1; |
---|
303 | } |
---|
304 | |
---|
305 | static int cmd_account_set_checkflags( irc_t *irc, set_t *s ) |
---|
306 | { |
---|
307 | account_t *a = s->data; |
---|
308 | |
---|
309 | if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY ) |
---|
310 | { |
---|
311 | irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" ); |
---|
312 | return 0; |
---|
313 | } |
---|
314 | else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY ) |
---|
315 | { |
---|
316 | irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" ); |
---|
317 | return 0; |
---|
318 | } |
---|
319 | |
---|
320 | return 1; |
---|
321 | } |
---|
322 | |
---|
323 | static void cmd_account( irc_t *irc, char **cmd ) |
---|
324 | { |
---|
325 | account_t *a; |
---|
326 | int len; |
---|
327 | |
---|
328 | if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) ) |
---|
329 | { |
---|
330 | irc_usermsg( irc, "This server only accepts registered users" ); |
---|
331 | return; |
---|
332 | } |
---|
333 | |
---|
334 | len = strlen( cmd[1] ); |
---|
335 | |
---|
336 | if( len >= 1 && g_strncasecmp( cmd[1], "add", len ) == 0 ) |
---|
337 | { |
---|
338 | struct prpl *prpl; |
---|
339 | |
---|
340 | MIN_ARGS( 4 ); |
---|
341 | |
---|
342 | prpl = find_protocol( cmd[2] ); |
---|
343 | |
---|
344 | if( prpl == NULL ) |
---|
345 | { |
---|
346 | irc_usermsg( irc, "Unknown protocol" ); |
---|
347 | return; |
---|
348 | } |
---|
349 | |
---|
350 | a = account_add( irc->b, prpl, cmd[3], cmd[4] ); |
---|
351 | if( cmd[5] ) |
---|
352 | { |
---|
353 | irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' " |
---|
354 | "is now deprecated. Use `account set' instead." ); |
---|
355 | set_setstr( &a->set, "server", cmd[5] ); |
---|
356 | } |
---|
357 | |
---|
358 | irc_usermsg( irc, "Account successfully added" ); |
---|
359 | |
---|
360 | return; |
---|
361 | } |
---|
362 | else if( len >= 1 && g_strncasecmp( cmd[1], "list", len ) == 0 ) |
---|
363 | { |
---|
364 | int i = 0; |
---|
365 | |
---|
366 | if( strchr( irc->umode, 'b' ) ) |
---|
367 | irc_usermsg( irc, "Account list:" ); |
---|
368 | |
---|
369 | for( a = irc->b->accounts; a; a = a->next ) |
---|
370 | { |
---|
371 | char *con; |
---|
372 | |
---|
373 | if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) |
---|
374 | con = " (connected)"; |
---|
375 | else if( a->ic ) |
---|
376 | con = " (connecting)"; |
---|
377 | else if( a->reconnect ) |
---|
378 | con = " (awaiting reconnect)"; |
---|
379 | else |
---|
380 | con = ""; |
---|
381 | |
---|
382 | irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con ); |
---|
383 | |
---|
384 | i ++; |
---|
385 | } |
---|
386 | irc_usermsg( irc, "End of account list" ); |
---|
387 | |
---|
388 | return; |
---|
389 | } |
---|
390 | else if( cmd[2] ) |
---|
391 | { |
---|
392 | /* Try the following two only if cmd[2] == NULL */ |
---|
393 | } |
---|
394 | else if( len >= 2 && g_strncasecmp( cmd[1], "on", len ) == 0 ) |
---|
395 | { |
---|
396 | if ( irc->b->accounts ) |
---|
397 | { |
---|
398 | irc_usermsg( irc, "Trying to get all accounts connected..." ); |
---|
399 | |
---|
400 | for( a = irc->b->accounts; a; a = a->next ) |
---|
401 | if( !a->ic && a->auto_connect ) |
---|
402 | account_on( irc->b, a ); |
---|
403 | } |
---|
404 | else |
---|
405 | { |
---|
406 | irc_usermsg( irc, "No accounts known. Use `account add' to add one." ); |
---|
407 | } |
---|
408 | |
---|
409 | return; |
---|
410 | } |
---|
411 | else if( len >= 2 && g_strncasecmp( cmd[1], "off", len ) == 0 ) |
---|
412 | { |
---|
413 | irc_usermsg( irc, "Deactivating all active (re)connections..." ); |
---|
414 | |
---|
415 | for( a = irc->b->accounts; a; a = a->next ) |
---|
416 | { |
---|
417 | if( a->ic ) |
---|
418 | account_off( irc->b, a ); |
---|
419 | else if( a->reconnect ) |
---|
420 | cancel_auto_reconnect( a ); |
---|
421 | } |
---|
422 | |
---|
423 | return; |
---|
424 | } |
---|
425 | |
---|
426 | MIN_ARGS( 2 ); |
---|
427 | len = strlen( cmd[2] ); |
---|
428 | |
---|
429 | /* At least right now, don't accept on/off/set/del as account IDs even |
---|
430 | if they're a proper match, since people not familiar with the new |
---|
431 | syntax yet may get a confusing/nasty surprise. */ |
---|
432 | if( g_strcasecmp( cmd[1], "on" ) == 0 || |
---|
433 | g_strcasecmp( cmd[1], "off" ) == 0 || |
---|
434 | g_strcasecmp( cmd[1], "set" ) == 0 || |
---|
435 | g_strcasecmp( cmd[1], "del" ) == 0 || |
---|
436 | ( a = account_get( irc->b, cmd[1] ) ) == NULL ) |
---|
437 | { |
---|
438 | irc_usermsg( irc, "Could not find account `%s'. Note that the syntax " |
---|
439 | "of the account command changed, see \x02help account\x02.", cmd[1] ); |
---|
440 | |
---|
441 | return; |
---|
442 | } |
---|
443 | |
---|
444 | if( len >= 1 && g_strncasecmp( cmd[2], "del", len ) == 0 ) |
---|
445 | { |
---|
446 | if( a->ic ) |
---|
447 | { |
---|
448 | irc_usermsg( irc, "Account is still logged in, can't delete" ); |
---|
449 | } |
---|
450 | else |
---|
451 | { |
---|
452 | account_del( irc->b, a ); |
---|
453 | irc_usermsg( irc, "Account deleted" ); |
---|
454 | } |
---|
455 | } |
---|
456 | else if( len >= 2 && g_strncasecmp( cmd[2], "on", len ) == 0 ) |
---|
457 | { |
---|
458 | if( a->ic ) |
---|
459 | irc_usermsg( irc, "Account already online" ); |
---|
460 | else |
---|
461 | account_on( irc->b, a ); |
---|
462 | } |
---|
463 | else if( len >= 2 && g_strncasecmp( cmd[2], "off", len ) == 0 ) |
---|
464 | { |
---|
465 | if( a->ic ) |
---|
466 | { |
---|
467 | account_off( irc->b, a ); |
---|
468 | } |
---|
469 | else if( a->reconnect ) |
---|
470 | { |
---|
471 | cancel_auto_reconnect( a ); |
---|
472 | irc_usermsg( irc, "Reconnect cancelled" ); |
---|
473 | } |
---|
474 | else |
---|
475 | { |
---|
476 | irc_usermsg( irc, "Account already offline" ); |
---|
477 | } |
---|
478 | } |
---|
479 | else if( len >= 1 && g_strncasecmp( cmd[2], "set", len ) == 0 ) |
---|
480 | { |
---|
481 | cmd_set_real( irc, cmd + 2, &a->set, cmd_account_set_checkflags ); |
---|
482 | } |
---|
483 | else |
---|
484 | { |
---|
485 | irc_usermsg( irc, "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "account", cmd[2] ); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | static void cmd_channel( irc_t *irc, char **cmd ) |
---|
490 | { |
---|
491 | irc_channel_t *ic; |
---|
492 | int len; |
---|
493 | |
---|
494 | len = strlen( cmd[1] ); |
---|
495 | |
---|
496 | if( len >= 1 && g_strncasecmp( cmd[1], "list", len ) == 0 ) |
---|
497 | { |
---|
498 | GSList *l; |
---|
499 | int i = 0; |
---|
500 | |
---|
501 | if( strchr( irc->umode, 'b' ) ) |
---|
502 | irc_usermsg( irc, "Channel list:" ); |
---|
503 | |
---|
504 | for( l = irc->channels; l; l = l->next ) |
---|
505 | { |
---|
506 | irc_channel_t *ic = l->data; |
---|
507 | |
---|
508 | irc_usermsg( irc, "%2d. %s, %s channel%s", i, ic->name, |
---|
509 | set_getstr( &ic->set, "type" ), |
---|
510 | ic->flags & IRC_CHANNEL_JOINED ? " (joined)" : "" ); |
---|
511 | |
---|
512 | i ++; |
---|
513 | } |
---|
514 | irc_usermsg( irc, "End of channel list" ); |
---|
515 | |
---|
516 | return; |
---|
517 | } |
---|
518 | |
---|
519 | MIN_ARGS( 2 ); |
---|
520 | len = strlen( cmd[2] ); |
---|
521 | |
---|
522 | if( ( ic = irc_channel_get( irc, cmd[1] ) ) == NULL ) |
---|
523 | { |
---|
524 | irc_usermsg( irc, "Could not find channel `%s'", cmd[1] ); |
---|
525 | return; |
---|
526 | } |
---|
527 | |
---|
528 | if( len >= 1 && g_strncasecmp( cmd[2], "set", len ) == 0 ) |
---|
529 | { |
---|
530 | cmd_set_real( irc, cmd + 2, &ic->set, NULL ); |
---|
531 | } |
---|
532 | else if( len >= 1 && g_strncasecmp( cmd[2], "del", len ) == 0 ) |
---|
533 | { |
---|
534 | if( !( ic->flags & IRC_CHANNEL_JOINED ) && |
---|
535 | ic != ic->irc->default_channel ) |
---|
536 | { |
---|
537 | irc_usermsg( irc, "Channel %s deleted.", ic->name ); |
---|
538 | irc_channel_free( ic ); |
---|
539 | } |
---|
540 | else |
---|
541 | irc_usermsg( irc, "Couldn't remove channel (main channel %s or " |
---|
542 | "channels you're still in cannot be deleted).", |
---|
543 | irc->default_channel->name ); |
---|
544 | } |
---|
545 | else |
---|
546 | { |
---|
547 | irc_usermsg( irc, "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "channel", cmd[1] ); |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | static void cmd_add( irc_t *irc, char **cmd ) |
---|
552 | { |
---|
553 | account_t *a; |
---|
554 | int add_on_server = 1; |
---|
555 | |
---|
556 | if( g_strcasecmp( cmd[1], "-tmp" ) == 0 ) |
---|
557 | { |
---|
558 | MIN_ARGS( 3 ); |
---|
559 | add_on_server = 0; |
---|
560 | cmd ++; |
---|
561 | } |
---|
562 | |
---|
563 | if( !( a = account_get( irc->b, cmd[1] ) ) ) |
---|
564 | { |
---|
565 | irc_usermsg( irc, "Invalid account" ); |
---|
566 | return; |
---|
567 | } |
---|
568 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
569 | { |
---|
570 | irc_usermsg( irc, "That account is not on-line" ); |
---|
571 | return; |
---|
572 | } |
---|
573 | |
---|
574 | if( cmd[3] ) |
---|
575 | { |
---|
576 | if( !nick_ok( cmd[3] ) ) |
---|
577 | { |
---|
578 | irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] ); |
---|
579 | return; |
---|
580 | } |
---|
581 | else if( irc_user_by_name( irc, cmd[3] ) ) |
---|
582 | { |
---|
583 | irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] ); |
---|
584 | return; |
---|
585 | } |
---|
586 | else |
---|
587 | { |
---|
588 | nick_set( a, cmd[2], cmd[3] ); |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | if( add_on_server ) |
---|
593 | a->prpl->add_buddy( a->ic, cmd[2], NULL ); |
---|
594 | else |
---|
595 | /* Only for add -tmp. For regular adds, this callback will |
---|
596 | be called once the IM server confirms. */ |
---|
597 | bee_user_new( irc->b, a->ic, cmd[2], BEE_USER_LOCAL ); |
---|
598 | |
---|
599 | irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2] ); |
---|
600 | } |
---|
601 | |
---|
602 | static void cmd_remove( irc_t *irc, char **cmd ) |
---|
603 | { |
---|
604 | irc_user_t *iu; |
---|
605 | bee_user_t *bu; |
---|
606 | char *s; |
---|
607 | |
---|
608 | if( !( iu = irc_user_by_name( irc, cmd[1] ) ) || !( bu = iu->bu ) ) |
---|
609 | { |
---|
610 | irc_usermsg( irc, "Buddy `%s' not found", cmd[1] ); |
---|
611 | return; |
---|
612 | } |
---|
613 | s = g_strdup( bu->handle ); |
---|
614 | |
---|
615 | bu->ic->acc->prpl->remove_buddy( bu->ic, bu->handle, NULL ); |
---|
616 | nick_del( bu->ic->acc, bu->handle ); |
---|
617 | //TODO(wilmer): bee_user_free() and/or let the IM mod do it? irc_user_free( irc, cmd[1] ); |
---|
618 | |
---|
619 | irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] ); |
---|
620 | g_free( s ); |
---|
621 | |
---|
622 | return; |
---|
623 | } |
---|
624 | |
---|
625 | static void cmd_info( irc_t *irc, char **cmd ) |
---|
626 | { |
---|
627 | struct im_connection *ic; |
---|
628 | account_t *a; |
---|
629 | |
---|
630 | if( !cmd[2] ) |
---|
631 | { |
---|
632 | irc_user_t *iu = irc_user_by_name( irc, cmd[1] ); |
---|
633 | if( !iu || !iu->bu ) |
---|
634 | { |
---|
635 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
636 | return; |
---|
637 | } |
---|
638 | ic = iu->bu->ic; |
---|
639 | cmd[2] = iu->bu->handle; |
---|
640 | } |
---|
641 | else if( !( a = account_get( irc->b, cmd[1] ) ) ) |
---|
642 | { |
---|
643 | irc_usermsg( irc, "Invalid account" ); |
---|
644 | return; |
---|
645 | } |
---|
646 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
647 | { |
---|
648 | irc_usermsg( irc, "That account is not on-line" ); |
---|
649 | return; |
---|
650 | } |
---|
651 | |
---|
652 | if( !ic->acc->prpl->get_info ) |
---|
653 | { |
---|
654 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
655 | } |
---|
656 | else |
---|
657 | { |
---|
658 | ic->acc->prpl->get_info( ic, cmd[2] ); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | static void cmd_rename( irc_t *irc, char **cmd ) |
---|
663 | { |
---|
664 | irc_user_t *iu; |
---|
665 | |
---|
666 | iu = irc_user_by_name( irc, cmd[1] ); |
---|
667 | |
---|
668 | if( iu == NULL ) |
---|
669 | { |
---|
670 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
671 | } |
---|
672 | else if( iu == irc->user ) |
---|
673 | { |
---|
674 | irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] ); |
---|
675 | } |
---|
676 | else if( !nick_ok( cmd[2] ) ) |
---|
677 | { |
---|
678 | irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] ); |
---|
679 | } |
---|
680 | else if( irc_user_by_name( irc, cmd[2] ) ) |
---|
681 | { |
---|
682 | irc_usermsg( irc, "Nick `%s' already exists", cmd[2] ); |
---|
683 | } |
---|
684 | else |
---|
685 | { |
---|
686 | if( !irc_user_set_nick( iu, cmd[2] ) ) |
---|
687 | { |
---|
688 | irc_usermsg( irc, "Error while changing nick" ); |
---|
689 | return; |
---|
690 | } |
---|
691 | |
---|
692 | if( iu == irc->root ) |
---|
693 | { |
---|
694 | /* If we're called internally (user did "set root_nick"), |
---|
695 | let's not go O(INF). :-) */ |
---|
696 | if( strcmp( cmd[0], "set_rename" ) != 0 ) |
---|
697 | set_setstr( &irc->b->set, "root_nick", cmd[2] ); |
---|
698 | } |
---|
699 | else if( iu->bu ) |
---|
700 | { |
---|
701 | nick_set( iu->bu->ic->acc, iu->bu->handle, cmd[2] ); |
---|
702 | } |
---|
703 | |
---|
704 | irc_usermsg( irc, "Nick successfully changed" ); |
---|
705 | } |
---|
706 | } |
---|
707 | |
---|
708 | char *set_eval_root_nick( set_t *set, char *new_nick ) |
---|
709 | { |
---|
710 | irc_t *irc = set->data; |
---|
711 | |
---|
712 | if( strcmp( irc->root->nick, new_nick ) != 0 ) |
---|
713 | { |
---|
714 | char *cmd[] = { "set_rename", irc->root->nick, new_nick, NULL }; |
---|
715 | |
---|
716 | cmd_rename( irc, cmd ); |
---|
717 | } |
---|
718 | |
---|
719 | return strcmp( irc->root->nick, new_nick ) == 0 ? new_nick : SET_INVALID; |
---|
720 | } |
---|
721 | |
---|
722 | static void cmd_block( irc_t *irc, char **cmd ) |
---|
723 | { |
---|
724 | struct im_connection *ic; |
---|
725 | account_t *a; |
---|
726 | |
---|
727 | if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic ) |
---|
728 | { |
---|
729 | char *format; |
---|
730 | GSList *l; |
---|
731 | |
---|
732 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
733 | format = "%s\t%s"; |
---|
734 | else |
---|
735 | format = "%-32.32s %-16.16s"; |
---|
736 | |
---|
737 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
738 | for( l = a->ic->deny; l; l = l->next ) |
---|
739 | { |
---|
740 | bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data ); |
---|
741 | irc_user_t *iu = bu ? bu->ui_data : NULL; |
---|
742 | irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" ); |
---|
743 | } |
---|
744 | irc_usermsg( irc, "End of list." ); |
---|
745 | |
---|
746 | return; |
---|
747 | } |
---|
748 | else if( !cmd[2] ) |
---|
749 | { |
---|
750 | irc_user_t *iu = irc_user_by_name( irc, cmd[1] ); |
---|
751 | if( !iu || !iu->bu ) |
---|
752 | { |
---|
753 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
754 | return; |
---|
755 | } |
---|
756 | ic = iu->bu->ic; |
---|
757 | cmd[2] = iu->bu->handle; |
---|
758 | } |
---|
759 | else if( !( a = account_get( irc->b, cmd[1] ) ) ) |
---|
760 | { |
---|
761 | irc_usermsg( irc, "Invalid account" ); |
---|
762 | return; |
---|
763 | } |
---|
764 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
765 | { |
---|
766 | irc_usermsg( irc, "That account is not on-line" ); |
---|
767 | return; |
---|
768 | } |
---|
769 | |
---|
770 | if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit ) |
---|
771 | { |
---|
772 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
773 | } |
---|
774 | else |
---|
775 | { |
---|
776 | imc_rem_allow( ic, cmd[2] ); |
---|
777 | imc_add_block( ic, cmd[2] ); |
---|
778 | irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] ); |
---|
779 | } |
---|
780 | } |
---|
781 | |
---|
782 | static void cmd_allow( irc_t *irc, char **cmd ) |
---|
783 | { |
---|
784 | struct im_connection *ic; |
---|
785 | account_t *a; |
---|
786 | |
---|
787 | if( !cmd[2] && ( a = account_get( irc->b, cmd[1] ) ) && a->ic ) |
---|
788 | { |
---|
789 | char *format; |
---|
790 | GSList *l; |
---|
791 | |
---|
792 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
793 | format = "%s\t%s"; |
---|
794 | else |
---|
795 | format = "%-32.32s %-16.16s"; |
---|
796 | |
---|
797 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
798 | for( l = a->ic->permit; l; l = l->next ) |
---|
799 | { |
---|
800 | bee_user_t *bu = bee_user_by_handle( irc->b, a->ic, l->data ); |
---|
801 | irc_user_t *iu = bu ? bu->ui_data : NULL; |
---|
802 | irc_usermsg( irc, format, l->data, iu ? iu->nick : "(none)" ); |
---|
803 | } |
---|
804 | irc_usermsg( irc, "End of list." ); |
---|
805 | |
---|
806 | return; |
---|
807 | } |
---|
808 | else if( !cmd[2] ) |
---|
809 | { |
---|
810 | irc_user_t *iu = irc_user_by_name( irc, cmd[1] ); |
---|
811 | if( !iu || !iu->bu ) |
---|
812 | { |
---|
813 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
814 | return; |
---|
815 | } |
---|
816 | ic = iu->bu->ic; |
---|
817 | cmd[2] = iu->bu->handle; |
---|
818 | } |
---|
819 | else if( !( a = account_get( irc->b, cmd[1] ) ) ) |
---|
820 | { |
---|
821 | irc_usermsg( irc, "Invalid account" ); |
---|
822 | return; |
---|
823 | } |
---|
824 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
825 | { |
---|
826 | irc_usermsg( irc, "That account is not on-line" ); |
---|
827 | return; |
---|
828 | } |
---|
829 | |
---|
830 | if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit ) |
---|
831 | { |
---|
832 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
833 | } |
---|
834 | else |
---|
835 | { |
---|
836 | imc_rem_block( ic, cmd[2] ); |
---|
837 | imc_add_allow( ic, cmd[2] ); |
---|
838 | |
---|
839 | irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] ); |
---|
840 | } |
---|
841 | } |
---|
842 | |
---|
843 | static void cmd_yesno( irc_t *irc, char **cmd ) |
---|
844 | { |
---|
845 | query_t *q = NULL; |
---|
846 | int numq = 0; |
---|
847 | |
---|
848 | if( irc->queries == NULL ) |
---|
849 | { |
---|
850 | irc_usermsg( irc, "Did I ask you something?" ); |
---|
851 | return; |
---|
852 | } |
---|
853 | |
---|
854 | /* If there's an argument, the user seems to want to answer another question than the |
---|
855 | first/last (depending on the query_order setting) one. */ |
---|
856 | if( cmd[1] ) |
---|
857 | { |
---|
858 | if( sscanf( cmd[1], "%d", &numq ) != 1 ) |
---|
859 | { |
---|
860 | irc_usermsg( irc, "Invalid query number" ); |
---|
861 | return; |
---|
862 | } |
---|
863 | |
---|
864 | for( q = irc->queries; q; q = q->next, numq -- ) |
---|
865 | if( numq == 0 ) |
---|
866 | break; |
---|
867 | |
---|
868 | if( !q ) |
---|
869 | { |
---|
870 | irc_usermsg( irc, "Uhm, I never asked you something like that..." ); |
---|
871 | return; |
---|
872 | } |
---|
873 | } |
---|
874 | |
---|
875 | if( g_strcasecmp( cmd[0], "yes" ) == 0 ) |
---|
876 | query_answer( irc, q, 1 ); |
---|
877 | else if( g_strcasecmp( cmd[0], "no" ) == 0 ) |
---|
878 | query_answer( irc, q, 0 ); |
---|
879 | } |
---|
880 | |
---|
881 | static void cmd_set( irc_t *irc, char **cmd ) |
---|
882 | { |
---|
883 | cmd_set_real( irc, cmd, &irc->b->set, NULL ); |
---|
884 | } |
---|
885 | |
---|
886 | static void cmd_blist( irc_t *irc, char **cmd ) |
---|
887 | { |
---|
888 | int online = 0, away = 0, offline = 0; |
---|
889 | GSList *l; |
---|
890 | char s[256]; |
---|
891 | char *format; |
---|
892 | int n_online = 0, n_away = 0, n_offline = 0; |
---|
893 | |
---|
894 | if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 ) |
---|
895 | online = offline = away = 1; |
---|
896 | else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 ) |
---|
897 | offline = 1; |
---|
898 | else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 ) |
---|
899 | away = 1; |
---|
900 | else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 ) |
---|
901 | online = 1; |
---|
902 | else |
---|
903 | online = away = 1; |
---|
904 | |
---|
905 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
906 | format = "%s\t%s\t%s"; |
---|
907 | else |
---|
908 | format = "%-16.16s %-40.40s %s"; |
---|
909 | |
---|
910 | irc_usermsg( irc, format, "Nick", "Handle/Account", "Status" ); |
---|
911 | |
---|
912 | for( l = irc->users; l; l = l->next ) |
---|
913 | { |
---|
914 | irc_user_t *iu = l->data; |
---|
915 | bee_user_t *bu = iu->bu; |
---|
916 | |
---|
917 | if( !bu || ( bu->flags & ( BEE_USER_ONLINE | BEE_USER_AWAY ) ) != BEE_USER_ONLINE ) |
---|
918 | continue; |
---|
919 | |
---|
920 | if( online == 1 ) |
---|
921 | { |
---|
922 | char st[256] = "Online"; |
---|
923 | |
---|
924 | if( bu->status_msg ) |
---|
925 | g_snprintf( st, sizeof( st ) - 1, "Online (%s)", bu->status_msg ); |
---|
926 | |
---|
927 | g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user ); |
---|
928 | irc_usermsg( irc, format, iu->nick, s, st ); |
---|
929 | } |
---|
930 | |
---|
931 | n_online ++; |
---|
932 | } |
---|
933 | |
---|
934 | for( l = irc->users; l; l = l->next ) |
---|
935 | { |
---|
936 | irc_user_t *iu = l->data; |
---|
937 | bee_user_t *bu = iu->bu; |
---|
938 | |
---|
939 | if( !bu || !( bu->flags & BEE_USER_ONLINE ) || !( bu->flags & BEE_USER_AWAY ) ) |
---|
940 | continue; |
---|
941 | |
---|
942 | if( away == 1 ) |
---|
943 | { |
---|
944 | g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user ); |
---|
945 | irc_usermsg( irc, format, iu->nick, s, irc_user_get_away( iu ) ); |
---|
946 | } |
---|
947 | n_away ++; |
---|
948 | } |
---|
949 | |
---|
950 | for( l = irc->users; l; l = l->next ) |
---|
951 | { |
---|
952 | irc_user_t *iu = l->data; |
---|
953 | bee_user_t *bu = iu->bu; |
---|
954 | |
---|
955 | if( !bu || bu->flags & BEE_USER_ONLINE ) |
---|
956 | continue; |
---|
957 | |
---|
958 | if( offline == 1 ) |
---|
959 | { |
---|
960 | g_snprintf( s, sizeof( s ) - 1, "%s %s(%s)", bu->handle, bu->ic->acc->prpl->name, bu->ic->acc->user ); |
---|
961 | irc_usermsg( irc, format, iu->nick, s, "Offline" ); |
---|
962 | } |
---|
963 | n_offline ++; |
---|
964 | } |
---|
965 | |
---|
966 | irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline ); |
---|
967 | } |
---|
968 | |
---|
969 | static void cmd_qlist( irc_t *irc, char **cmd ) |
---|
970 | { |
---|
971 | query_t *q = irc->queries; |
---|
972 | int num; |
---|
973 | |
---|
974 | if( !q ) |
---|
975 | { |
---|
976 | irc_usermsg( irc, "There are no pending questions." ); |
---|
977 | return; |
---|
978 | } |
---|
979 | |
---|
980 | irc_usermsg( irc, "Pending queries:" ); |
---|
981 | |
---|
982 | for( num = 0; q; q = q->next, num ++ ) |
---|
983 | if( q->ic ) /* Not necessary yet, but it might come later */ |
---|
984 | irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question ); |
---|
985 | else |
---|
986 | irc_usermsg( irc, "%d, BitlBee: %s", num, q->question ); |
---|
987 | } |
---|
988 | |
---|
989 | static void cmd_chat( irc_t *irc, char **cmd ) |
---|
990 | { |
---|
991 | account_t *acc; |
---|
992 | |
---|
993 | if( g_strcasecmp( cmd[1], "add" ) == 0 ) |
---|
994 | { |
---|
995 | char *channel, *s; |
---|
996 | struct irc_channel *ic; |
---|
997 | |
---|
998 | MIN_ARGS( 3 ); |
---|
999 | |
---|
1000 | if( !( acc = account_get( irc->b, cmd[2] ) ) ) |
---|
1001 | { |
---|
1002 | irc_usermsg( irc, "Invalid account" ); |
---|
1003 | return; |
---|
1004 | } |
---|
1005 | else if( !acc->prpl->chat_join ) |
---|
1006 | { |
---|
1007 | irc_usermsg( irc, "Named chatrooms not supported on that account." ); |
---|
1008 | return; |
---|
1009 | } |
---|
1010 | |
---|
1011 | if( cmd[4] == NULL ) |
---|
1012 | { |
---|
1013 | channel = g_strdup( cmd[3] ); |
---|
1014 | if( ( s = strchr( channel, '@' ) ) ) |
---|
1015 | *s = 0; |
---|
1016 | } |
---|
1017 | else |
---|
1018 | { |
---|
1019 | channel = g_strdup( cmd[4] ); |
---|
1020 | } |
---|
1021 | |
---|
1022 | if( strchr( CTYPES, channel[0] ) == NULL ) |
---|
1023 | { |
---|
1024 | s = g_strdup_printf( "#%s", channel ); |
---|
1025 | g_free( channel ); |
---|
1026 | channel = s; |
---|
1027 | |
---|
1028 | irc_channel_name_strip( channel ); |
---|
1029 | } |
---|
1030 | |
---|
1031 | if( ( ic = irc_channel_new( irc, channel ) ) && |
---|
1032 | set_setstr( &ic->set, "type", "chat" ) && |
---|
1033 | set_setstr( &ic->set, "chat_type", "room" ) && |
---|
1034 | set_setstr( &ic->set, "account", cmd[2] ) && |
---|
1035 | set_setstr( &ic->set, "room", cmd[3] ) ) |
---|
1036 | { |
---|
1037 | irc_usermsg( irc, "Chatroom successfully added." ); |
---|
1038 | } |
---|
1039 | else |
---|
1040 | { |
---|
1041 | if( ic ) |
---|
1042 | irc_channel_free( ic ); |
---|
1043 | |
---|
1044 | irc_usermsg( irc, "Could not add chatroom." ); |
---|
1045 | } |
---|
1046 | g_free( channel ); |
---|
1047 | } |
---|
1048 | else if( g_strcasecmp( cmd[1], "with" ) == 0 ) |
---|
1049 | { |
---|
1050 | irc_user_t *iu; |
---|
1051 | |
---|
1052 | MIN_ARGS( 2 ); |
---|
1053 | |
---|
1054 | if( ( iu = irc_user_by_name( irc, cmd[2] ) ) && |
---|
1055 | iu->bu && iu->bu->ic->acc->prpl->chat_with ) |
---|
1056 | { |
---|
1057 | if( !iu->bu->ic->acc->prpl->chat_with( iu->bu->ic, iu->bu->handle ) ) |
---|
1058 | { |
---|
1059 | irc_usermsg( irc, "(Possible) failure while trying to open " |
---|
1060 | "a groupchat with %s.", iu->nick ); |
---|
1061 | } |
---|
1062 | } |
---|
1063 | else |
---|
1064 | { |
---|
1065 | irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] ); |
---|
1066 | } |
---|
1067 | } |
---|
1068 | else if( g_strcasecmp( cmd[1], "list" ) == 0 || |
---|
1069 | g_strcasecmp( cmd[1], "set" ) == 0 || |
---|
1070 | g_strcasecmp( cmd[1], "del" ) == 0 ) |
---|
1071 | { |
---|
1072 | irc_usermsg( irc, "Warning: The \002chat\002 command was mostly replaced with the \002channel\002 command." ); |
---|
1073 | cmd_channel( irc, cmd ); |
---|
1074 | } |
---|
1075 | else |
---|
1076 | { |
---|
1077 | irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] ); |
---|
1078 | } |
---|
1079 | } |
---|
1080 | |
---|
1081 | static void cmd_transfer( irc_t *irc, char **cmd ) |
---|
1082 | { |
---|
1083 | GSList *files = irc->file_transfers; |
---|
1084 | enum { LIST, REJECT, CANCEL }; |
---|
1085 | int subcmd = LIST; |
---|
1086 | int fid; |
---|
1087 | |
---|
1088 | if( !files ) |
---|
1089 | { |
---|
1090 | irc_usermsg( irc, "No pending transfers" ); |
---|
1091 | return; |
---|
1092 | } |
---|
1093 | |
---|
1094 | if( cmd[1] && ( strcmp( cmd[1], "reject" ) == 0 ) ) |
---|
1095 | { |
---|
1096 | subcmd = REJECT; |
---|
1097 | } |
---|
1098 | else if( cmd[1] && ( strcmp( cmd[1], "cancel" ) == 0 ) && |
---|
1099 | cmd[2] && ( sscanf( cmd[2], "%d", &fid ) == 1 ) ) |
---|
1100 | { |
---|
1101 | subcmd = CANCEL; |
---|
1102 | } |
---|
1103 | |
---|
1104 | for( ; files; files = g_slist_next( files ) ) |
---|
1105 | { |
---|
1106 | file_transfer_t *file = files->data; |
---|
1107 | |
---|
1108 | switch( subcmd ) { |
---|
1109 | case LIST: |
---|
1110 | if ( file->status == FT_STATUS_LISTENING ) |
---|
1111 | irc_usermsg( irc, |
---|
1112 | "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name); |
---|
1113 | else |
---|
1114 | { |
---|
1115 | int kb_per_s = 0; |
---|
1116 | time_t diff = time( NULL ) - file->started ? : 1; |
---|
1117 | if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) ) |
---|
1118 | kb_per_s = file->bytes_transferred / 1024 / diff; |
---|
1119 | |
---|
1120 | irc_usermsg( irc, |
---|
1121 | "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name, |
---|
1122 | file->bytes_transferred/1024, file->file_size/1024, kb_per_s); |
---|
1123 | } |
---|
1124 | break; |
---|
1125 | case REJECT: |
---|
1126 | if( file->status == FT_STATUS_LISTENING ) |
---|
1127 | { |
---|
1128 | irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name ); |
---|
1129 | imcb_file_canceled( file->ic, file, "Denied by user" ); |
---|
1130 | } |
---|
1131 | break; |
---|
1132 | case CANCEL: |
---|
1133 | if( file->local_id == fid ) |
---|
1134 | { |
---|
1135 | irc_usermsg( irc, "Canceling file transfer for %s", file->file_name ); |
---|
1136 | imcb_file_canceled( file->ic, file, "Canceled by user" ); |
---|
1137 | } |
---|
1138 | break; |
---|
1139 | } |
---|
1140 | } |
---|
1141 | } |
---|
1142 | |
---|
1143 | /* IMPORTANT: Keep this list sorted! The short command logic needs that. */ |
---|
1144 | const command_t commands[] = { |
---|
1145 | { "account", 1, cmd_account, 0 }, |
---|
1146 | { "add", 2, cmd_add, 0 }, |
---|
1147 | { "allow", 1, cmd_allow, 0 }, |
---|
1148 | { "blist", 0, cmd_blist, 0 }, |
---|
1149 | { "block", 1, cmd_block, 0 }, |
---|
1150 | { "channel", 1, cmd_channel, 0 }, |
---|
1151 | { "chat", 1, cmd_chat, 0 }, |
---|
1152 | { "drop", 1, cmd_drop, 0 }, |
---|
1153 | { "ft", 0, cmd_transfer, 0 }, |
---|
1154 | { "help", 0, cmd_help, 0 }, |
---|
1155 | { "identify", 1, cmd_identify, 0 }, |
---|
1156 | { "info", 1, cmd_info, 0 }, |
---|
1157 | { "no", 0, cmd_yesno, 0 }, |
---|
1158 | { "qlist", 0, cmd_qlist, 0 }, |
---|
1159 | { "register", 1, cmd_register, 0 }, |
---|
1160 | { "remove", 1, cmd_remove, 0 }, |
---|
1161 | { "rename", 2, cmd_rename, 0 }, |
---|
1162 | { "save", 0, cmd_save, 0 }, |
---|
1163 | { "set", 0, cmd_set, 0 }, |
---|
1164 | { "transfer", 0, cmd_transfer, 0 }, |
---|
1165 | { "yes", 0, cmd_yesno, 0 }, |
---|
1166 | { NULL } |
---|
1167 | }; |
---|