1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 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 "crypting.h" |
---|
29 | #include "bitlbee.h" |
---|
30 | #include "help.h" |
---|
31 | |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | void root_command_string( irc_t *irc, user_t *u, char *command, int flags ) |
---|
35 | { |
---|
36 | char *cmd[IRC_MAX_ARGS]; |
---|
37 | char *s; |
---|
38 | int k; |
---|
39 | char q = 0; |
---|
40 | |
---|
41 | memset( cmd, 0, sizeof( cmd ) ); |
---|
42 | cmd[0] = command; |
---|
43 | k = 1; |
---|
44 | for( s = command; *s && k < ( IRC_MAX_ARGS - 1 ); s ++ ) |
---|
45 | if( *s == ' ' && !q ) |
---|
46 | { |
---|
47 | *s = 0; |
---|
48 | while( *++s == ' ' ); |
---|
49 | if( *s == '"' || *s == '\'' ) |
---|
50 | { |
---|
51 | q = *s; |
---|
52 | s ++; |
---|
53 | } |
---|
54 | if( *s ) |
---|
55 | { |
---|
56 | cmd[k++] = s; |
---|
57 | s --; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | break; |
---|
62 | } |
---|
63 | } |
---|
64 | else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) ) |
---|
65 | { |
---|
66 | char *cpy; |
---|
67 | |
---|
68 | for( cpy = s; *cpy; cpy ++ ) |
---|
69 | cpy[0] = cpy[1]; |
---|
70 | } |
---|
71 | else if( *s == q ) |
---|
72 | { |
---|
73 | q = *s = 0; |
---|
74 | } |
---|
75 | cmd[k] = NULL; |
---|
76 | |
---|
77 | root_command( irc, cmd ); |
---|
78 | } |
---|
79 | |
---|
80 | void root_command( irc_t *irc, char *cmd[] ) |
---|
81 | { |
---|
82 | int i; |
---|
83 | |
---|
84 | if( !cmd[0] ) |
---|
85 | return; |
---|
86 | |
---|
87 | for( i = 0; commands[i].command; i++ ) |
---|
88 | if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) |
---|
89 | { |
---|
90 | if( !cmd[commands[i].required_parameters] ) |
---|
91 | { |
---|
92 | irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters ); |
---|
93 | return; |
---|
94 | } |
---|
95 | commands[i].execute( irc, cmd ); |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] ); |
---|
100 | } |
---|
101 | |
---|
102 | static void cmd_help( irc_t *irc, char **cmd ) |
---|
103 | { |
---|
104 | char param[80]; |
---|
105 | int i; |
---|
106 | char *s; |
---|
107 | |
---|
108 | memset( param, 0, sizeof(param) ); |
---|
109 | for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) { |
---|
110 | if ( i != 1 ) // prepend space except for the first parameter |
---|
111 | strcat(param, " "); |
---|
112 | strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 ); |
---|
113 | } |
---|
114 | |
---|
115 | s = help_get( &(global.help), param ); |
---|
116 | if( !s ) s = help_get( &(global.help), "" ); |
---|
117 | |
---|
118 | if( s ) |
---|
119 | { |
---|
120 | irc_usermsg( irc, "%s", s ); |
---|
121 | g_free( s ); |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | irc_usermsg( irc, "Error opening helpfile." ); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | static void cmd_account( irc_t *irc, char **cmd ); |
---|
130 | |
---|
131 | static void cmd_identify( irc_t *irc, char **cmd ) |
---|
132 | { |
---|
133 | storage_status_t status = storage_load( irc->nick, cmd[1], irc ); |
---|
134 | char *account_on[] = { "account", "on", NULL }; |
---|
135 | |
---|
136 | switch (status) { |
---|
137 | case STORAGE_INVALID_PASSWORD: |
---|
138 | irc_usermsg( irc, "Incorrect password" ); |
---|
139 | break; |
---|
140 | case STORAGE_NO_SUCH_USER: |
---|
141 | irc_usermsg( irc, "The nick is (probably) not registered" ); |
---|
142 | break; |
---|
143 | case STORAGE_OK: |
---|
144 | irc_usermsg( irc, "Password accepted, settings and accounts loaded" ); |
---|
145 | irc_umode_set( irc, "+R", 1 ); |
---|
146 | if( set_getbool( &irc->set, "auto_connect" ) ) |
---|
147 | cmd_account( irc, account_on ); |
---|
148 | break; |
---|
149 | case STORAGE_OTHER_ERROR: |
---|
150 | default: |
---|
151 | irc_usermsg( irc, "Unknown error while loading configuration" ); |
---|
152 | break; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | static void cmd_register( irc_t *irc, char **cmd ) |
---|
157 | { |
---|
158 | if( global.conf->authmode == AUTHMODE_REGISTERED ) |
---|
159 | { |
---|
160 | irc_usermsg( irc, "This server does not allow registering new accounts" ); |
---|
161 | return; |
---|
162 | } |
---|
163 | |
---|
164 | irc_setpass( irc, cmd[1] ); |
---|
165 | switch( storage_save( irc, FALSE )) { |
---|
166 | case STORAGE_ALREADY_EXISTS: |
---|
167 | irc_usermsg( irc, "Nick is already registered" ); |
---|
168 | break; |
---|
169 | |
---|
170 | case STORAGE_OK: |
---|
171 | irc_usermsg( irc, "Account successfully created" ); |
---|
172 | irc->status |= USTATUS_IDENTIFIED; |
---|
173 | irc_umode_set( irc, "+R", 1 ); |
---|
174 | break; |
---|
175 | |
---|
176 | default: |
---|
177 | irc_usermsg( irc, "Error registering" ); |
---|
178 | break; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | static void cmd_drop( irc_t *irc, char **cmd ) |
---|
183 | { |
---|
184 | storage_status_t status; |
---|
185 | |
---|
186 | status = storage_remove (irc->nick, cmd[1]); |
---|
187 | switch (status) { |
---|
188 | case STORAGE_NO_SUCH_USER: |
---|
189 | irc_usermsg( irc, "That account does not exist" ); |
---|
190 | break; |
---|
191 | case STORAGE_INVALID_PASSWORD: |
---|
192 | irc_usermsg( irc, "Password invalid" ); |
---|
193 | break; |
---|
194 | case STORAGE_OK: |
---|
195 | irc_setpass( irc, NULL ); |
---|
196 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
197 | irc_umode_set( irc, "-R", 1 ); |
---|
198 | irc_usermsg( irc, "Account `%s' removed", irc->nick ); |
---|
199 | break; |
---|
200 | default: |
---|
201 | irc_usermsg( irc, "Error: `%d'", status ); |
---|
202 | break; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | static void cmd_account( irc_t *irc, char **cmd ) |
---|
207 | { |
---|
208 | account_t *a; |
---|
209 | |
---|
210 | if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) ) |
---|
211 | { |
---|
212 | irc_usermsg( irc, "This server only accepts registered users" ); |
---|
213 | return; |
---|
214 | } |
---|
215 | |
---|
216 | if( g_strcasecmp( cmd[1], "add" ) == 0 ) |
---|
217 | { |
---|
218 | struct prpl *prpl; |
---|
219 | |
---|
220 | if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL ) |
---|
221 | { |
---|
222 | irc_usermsg( irc, "Not enough parameters" ); |
---|
223 | return; |
---|
224 | } |
---|
225 | |
---|
226 | prpl = find_protocol(cmd[2]); |
---|
227 | |
---|
228 | if( prpl == NULL ) |
---|
229 | { |
---|
230 | irc_usermsg( irc, "Unknown protocol" ); |
---|
231 | return; |
---|
232 | } |
---|
233 | |
---|
234 | a = account_add( irc, prpl, cmd[3], cmd[4] ); |
---|
235 | if( cmd[5] ) |
---|
236 | { |
---|
237 | irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' " |
---|
238 | "is now deprecated. Use `account set' instead." ); |
---|
239 | set_setstr( &a->set, "server", cmd[5] ); |
---|
240 | } |
---|
241 | |
---|
242 | irc_usermsg( irc, "Account successfully added" ); |
---|
243 | } |
---|
244 | else if( g_strcasecmp( cmd[1], "del" ) == 0 ) |
---|
245 | { |
---|
246 | if( !cmd[2] ) |
---|
247 | { |
---|
248 | irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); |
---|
249 | } |
---|
250 | else if( !( a = account_get( irc, cmd[2] ) ) ) |
---|
251 | { |
---|
252 | irc_usermsg( irc, "Invalid account" ); |
---|
253 | } |
---|
254 | else if( a->ic ) |
---|
255 | { |
---|
256 | irc_usermsg( irc, "Account is still logged in, can't delete" ); |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | account_del( irc, a ); |
---|
261 | irc_usermsg( irc, "Account deleted" ); |
---|
262 | } |
---|
263 | } |
---|
264 | else if( g_strcasecmp( cmd[1], "list" ) == 0 ) |
---|
265 | { |
---|
266 | int i = 0; |
---|
267 | |
---|
268 | if( strchr( irc->umode, 'b' ) ) |
---|
269 | irc_usermsg( irc, "Account list:" ); |
---|
270 | |
---|
271 | for( a = irc->accounts; a; a = a->next ) |
---|
272 | { |
---|
273 | char *con; |
---|
274 | |
---|
275 | if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) |
---|
276 | con = " (connected)"; |
---|
277 | else if( a->ic ) |
---|
278 | con = " (connecting)"; |
---|
279 | else if( a->reconnect ) |
---|
280 | con = " (awaiting reconnect)"; |
---|
281 | else |
---|
282 | con = ""; |
---|
283 | |
---|
284 | irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con ); |
---|
285 | |
---|
286 | i ++; |
---|
287 | } |
---|
288 | irc_usermsg( irc, "End of account list" ); |
---|
289 | } |
---|
290 | else if( g_strcasecmp( cmd[1], "on" ) == 0 ) |
---|
291 | { |
---|
292 | if( cmd[2] ) |
---|
293 | { |
---|
294 | if( ( a = account_get( irc, cmd[2] ) ) ) |
---|
295 | { |
---|
296 | if( a->ic ) |
---|
297 | { |
---|
298 | irc_usermsg( irc, "Account already online" ); |
---|
299 | return; |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | account_on( irc, a ); |
---|
304 | } |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | irc_usermsg( irc, "Invalid account" ); |
---|
309 | return; |
---|
310 | } |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | if ( irc->accounts ) { |
---|
315 | irc_usermsg( irc, "Trying to get all accounts connected..." ); |
---|
316 | |
---|
317 | for( a = irc->accounts; a; a = a->next ) |
---|
318 | if( !a->ic && a->auto_connect ) |
---|
319 | account_on( irc, a ); |
---|
320 | } |
---|
321 | else |
---|
322 | { |
---|
323 | irc_usermsg( irc, "No accounts known. Use `account add' to add one." ); |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | else if( g_strcasecmp( cmd[1], "off" ) == 0 ) |
---|
328 | { |
---|
329 | if( !cmd[2] ) |
---|
330 | { |
---|
331 | irc_usermsg( irc, "Deactivating all active (re)connections..." ); |
---|
332 | |
---|
333 | for( a = irc->accounts; a; a = a->next ) |
---|
334 | { |
---|
335 | if( a->ic ) |
---|
336 | account_off( irc, a ); |
---|
337 | else if( a->reconnect ) |
---|
338 | cancel_auto_reconnect( a ); |
---|
339 | } |
---|
340 | } |
---|
341 | else if( ( a = account_get( irc, cmd[2] ) ) ) |
---|
342 | { |
---|
343 | if( a->ic ) |
---|
344 | { |
---|
345 | account_off( irc, a ); |
---|
346 | } |
---|
347 | else if( a->reconnect ) |
---|
348 | { |
---|
349 | cancel_auto_reconnect( a ); |
---|
350 | irc_usermsg( irc, "Reconnect cancelled" ); |
---|
351 | } |
---|
352 | else |
---|
353 | { |
---|
354 | irc_usermsg( irc, "Account already offline" ); |
---|
355 | return; |
---|
356 | } |
---|
357 | } |
---|
358 | else |
---|
359 | { |
---|
360 | irc_usermsg( irc, "Invalid account" ); |
---|
361 | return; |
---|
362 | } |
---|
363 | } |
---|
364 | else if( g_strcasecmp( cmd[1], "set" ) == 0 ) |
---|
365 | { |
---|
366 | char *acc_handle, *set_name = NULL, *tmp; |
---|
367 | |
---|
368 | if( !cmd[2] ) |
---|
369 | { |
---|
370 | irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); |
---|
371 | return; |
---|
372 | } |
---|
373 | |
---|
374 | if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 ) |
---|
375 | acc_handle = g_strdup( cmd[3] ); |
---|
376 | else |
---|
377 | acc_handle = g_strdup( cmd[2] ); |
---|
378 | |
---|
379 | if( ( tmp = strchr( acc_handle, '/' ) ) ) |
---|
380 | { |
---|
381 | *tmp = 0; |
---|
382 | set_name = tmp + 1; |
---|
383 | } |
---|
384 | |
---|
385 | if( ( a = account_get( irc, acc_handle ) ) == NULL ) |
---|
386 | { |
---|
387 | g_free( acc_handle ); |
---|
388 | irc_usermsg( irc, "Invalid account" ); |
---|
389 | return; |
---|
390 | } |
---|
391 | |
---|
392 | if( cmd[3] && set_name ) |
---|
393 | { |
---|
394 | set_t *s = set_find( &a->set, set_name ); |
---|
395 | |
---|
396 | if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY ) |
---|
397 | { |
---|
398 | g_free( acc_handle ); |
---|
399 | irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" ); |
---|
400 | return; |
---|
401 | } |
---|
402 | else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY ) |
---|
403 | { |
---|
404 | g_free( acc_handle ); |
---|
405 | irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" ); |
---|
406 | return; |
---|
407 | } |
---|
408 | |
---|
409 | if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 ) |
---|
410 | set_reset( &a->set, set_name ); |
---|
411 | else |
---|
412 | set_setstr( &a->set, set_name, cmd[3] ); |
---|
413 | } |
---|
414 | if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */ |
---|
415 | { |
---|
416 | char *s = set_getstr( &a->set, set_name ); |
---|
417 | if( s ) |
---|
418 | irc_usermsg( irc, "%s = `%s'", set_name, s ); |
---|
419 | else |
---|
420 | irc_usermsg( irc, "%s is empty", set_name ); |
---|
421 | } |
---|
422 | else |
---|
423 | { |
---|
424 | set_t *s = a->set; |
---|
425 | while( s ) |
---|
426 | { |
---|
427 | if( s->value || s->def ) |
---|
428 | irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def ); |
---|
429 | else |
---|
430 | irc_usermsg( irc, "%s is empty", s->key ); |
---|
431 | s = s->next; |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | g_free( acc_handle ); |
---|
436 | } |
---|
437 | else |
---|
438 | { |
---|
439 | irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] ); |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | static void cmd_add( irc_t *irc, char **cmd ) |
---|
444 | { |
---|
445 | account_t *a; |
---|
446 | int add_on_server = 1; |
---|
447 | |
---|
448 | if( g_strcasecmp( cmd[1], "-tmp" ) == 0 ) |
---|
449 | { |
---|
450 | add_on_server = 0; |
---|
451 | cmd ++; |
---|
452 | } |
---|
453 | |
---|
454 | if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
455 | { |
---|
456 | irc_usermsg( irc, "Invalid account" ); |
---|
457 | return; |
---|
458 | } |
---|
459 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
460 | { |
---|
461 | irc_usermsg( irc, "That account is not on-line" ); |
---|
462 | return; |
---|
463 | } |
---|
464 | |
---|
465 | if( cmd[3] ) |
---|
466 | { |
---|
467 | if( !nick_ok( cmd[3] ) ) |
---|
468 | { |
---|
469 | irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] ); |
---|
470 | return; |
---|
471 | } |
---|
472 | else if( user_find( irc, cmd[3] ) ) |
---|
473 | { |
---|
474 | irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] ); |
---|
475 | return; |
---|
476 | } |
---|
477 | else |
---|
478 | { |
---|
479 | nick_set( a, cmd[2], cmd[3] ); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | if( add_on_server ) |
---|
484 | a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL ); |
---|
485 | else |
---|
486 | /* Yeah, officially this is a call-*back*... So if we just |
---|
487 | called add_buddy, we'll wait for the IM server to respond |
---|
488 | before we do this. */ |
---|
489 | imcb_add_buddy( a->ic, cmd[2], NULL ); |
---|
490 | |
---|
491 | irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2] ); |
---|
492 | } |
---|
493 | |
---|
494 | static void cmd_info( irc_t *irc, char **cmd ) |
---|
495 | { |
---|
496 | struct im_connection *ic; |
---|
497 | account_t *a; |
---|
498 | |
---|
499 | if( !cmd[2] ) |
---|
500 | { |
---|
501 | user_t *u = user_find( irc, cmd[1] ); |
---|
502 | if( !u || !u->ic ) |
---|
503 | { |
---|
504 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
505 | return; |
---|
506 | } |
---|
507 | ic = u->ic; |
---|
508 | cmd[2] = u->handle; |
---|
509 | } |
---|
510 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
511 | { |
---|
512 | irc_usermsg( irc, "Invalid account" ); |
---|
513 | return; |
---|
514 | } |
---|
515 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
516 | { |
---|
517 | irc_usermsg( irc, "That account is not on-line" ); |
---|
518 | return; |
---|
519 | } |
---|
520 | |
---|
521 | if( !ic->acc->prpl->get_info ) |
---|
522 | { |
---|
523 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
524 | } |
---|
525 | else |
---|
526 | { |
---|
527 | ic->acc->prpl->get_info( ic, cmd[2] ); |
---|
528 | } |
---|
529 | } |
---|
530 | |
---|
531 | static void cmd_rename( irc_t *irc, char **cmd ) |
---|
532 | { |
---|
533 | user_t *u; |
---|
534 | |
---|
535 | if( g_strcasecmp( cmd[1], irc->nick ) == 0 ) |
---|
536 | { |
---|
537 | irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] ); |
---|
538 | } |
---|
539 | else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) ) |
---|
540 | { |
---|
541 | irc_usermsg( irc, "Nick `%s' already exists", cmd[2] ); |
---|
542 | } |
---|
543 | else if( !nick_ok( cmd[2] ) ) |
---|
544 | { |
---|
545 | irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] ); |
---|
546 | } |
---|
547 | else if( !( u = user_find( irc, cmd[1] ) ) ) |
---|
548 | { |
---|
549 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
550 | } |
---|
551 | else |
---|
552 | { |
---|
553 | user_rename( irc, cmd[1], cmd[2] ); |
---|
554 | irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] ); |
---|
555 | if( g_strcasecmp( cmd[1], irc->mynick ) == 0 ) |
---|
556 | { |
---|
557 | g_free( irc->mynick ); |
---|
558 | irc->mynick = g_strdup( cmd[2] ); |
---|
559 | } |
---|
560 | else if( u->send_handler == buddy_send_handler ) |
---|
561 | { |
---|
562 | nick_set( u->ic->acc, u->handle, cmd[2] ); |
---|
563 | } |
---|
564 | |
---|
565 | irc_usermsg( irc, "Nick successfully changed" ); |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | static void cmd_remove( irc_t *irc, char **cmd ) |
---|
570 | { |
---|
571 | user_t *u; |
---|
572 | char *s; |
---|
573 | |
---|
574 | if( !( u = user_find( irc, cmd[1] ) ) || !u->ic ) |
---|
575 | { |
---|
576 | irc_usermsg( irc, "Buddy `%s' not found", cmd[1] ); |
---|
577 | return; |
---|
578 | } |
---|
579 | s = g_strdup( u->handle ); |
---|
580 | |
---|
581 | u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL ); |
---|
582 | nick_del( u->ic->acc, u->handle ); |
---|
583 | user_del( irc, cmd[1] ); |
---|
584 | |
---|
585 | irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] ); |
---|
586 | g_free( s ); |
---|
587 | |
---|
588 | return; |
---|
589 | } |
---|
590 | |
---|
591 | static void cmd_block( irc_t *irc, char **cmd ) |
---|
592 | { |
---|
593 | struct im_connection *ic; |
---|
594 | account_t *a; |
---|
595 | |
---|
596 | if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic ) |
---|
597 | { |
---|
598 | char *format; |
---|
599 | GSList *l; |
---|
600 | |
---|
601 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
602 | format = "%s\t%s"; |
---|
603 | else |
---|
604 | format = "%-32.32s %-16.16s"; |
---|
605 | |
---|
606 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
607 | for( l = a->ic->deny; l; l = l->next ) |
---|
608 | { |
---|
609 | user_t *u = user_findhandle( a->ic, l->data ); |
---|
610 | irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" ); |
---|
611 | } |
---|
612 | irc_usermsg( irc, "End of list." ); |
---|
613 | |
---|
614 | return; |
---|
615 | } |
---|
616 | else if( !cmd[2] ) |
---|
617 | { |
---|
618 | user_t *u = user_find( irc, cmd[1] ); |
---|
619 | if( !u || !u->ic ) |
---|
620 | { |
---|
621 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
622 | return; |
---|
623 | } |
---|
624 | ic = u->ic; |
---|
625 | cmd[2] = u->handle; |
---|
626 | } |
---|
627 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
628 | { |
---|
629 | irc_usermsg( irc, "Invalid account" ); |
---|
630 | return; |
---|
631 | } |
---|
632 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
633 | { |
---|
634 | irc_usermsg( irc, "That account is not on-line" ); |
---|
635 | return; |
---|
636 | } |
---|
637 | |
---|
638 | if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit ) |
---|
639 | { |
---|
640 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
641 | } |
---|
642 | else |
---|
643 | { |
---|
644 | imc_rem_allow( ic, cmd[2] ); |
---|
645 | imc_add_block( ic, cmd[2] ); |
---|
646 | irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] ); |
---|
647 | } |
---|
648 | } |
---|
649 | |
---|
650 | static void cmd_allow( irc_t *irc, char **cmd ) |
---|
651 | { |
---|
652 | struct im_connection *ic; |
---|
653 | account_t *a; |
---|
654 | |
---|
655 | if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic ) |
---|
656 | { |
---|
657 | char *format; |
---|
658 | GSList *l; |
---|
659 | |
---|
660 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
661 | format = "%s\t%s"; |
---|
662 | else |
---|
663 | format = "%-32.32s %-16.16s"; |
---|
664 | |
---|
665 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
666 | for( l = a->ic->permit; l; l = l->next ) |
---|
667 | { |
---|
668 | user_t *u = user_findhandle( a->ic, l->data ); |
---|
669 | irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" ); |
---|
670 | } |
---|
671 | irc_usermsg( irc, "End of list." ); |
---|
672 | |
---|
673 | return; |
---|
674 | } |
---|
675 | else if( !cmd[2] ) |
---|
676 | { |
---|
677 | user_t *u = user_find( irc, cmd[1] ); |
---|
678 | if( !u || !u->ic ) |
---|
679 | { |
---|
680 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
681 | return; |
---|
682 | } |
---|
683 | ic = u->ic; |
---|
684 | cmd[2] = u->handle; |
---|
685 | } |
---|
686 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
687 | { |
---|
688 | irc_usermsg( irc, "Invalid account" ); |
---|
689 | return; |
---|
690 | } |
---|
691 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
692 | { |
---|
693 | irc_usermsg( irc, "That account is not on-line" ); |
---|
694 | return; |
---|
695 | } |
---|
696 | |
---|
697 | if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit ) |
---|
698 | { |
---|
699 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
700 | } |
---|
701 | else |
---|
702 | { |
---|
703 | imc_rem_block( ic, cmd[2] ); |
---|
704 | imc_add_allow( ic, cmd[2] ); |
---|
705 | |
---|
706 | irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] ); |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | static void cmd_yesno( irc_t *irc, char **cmd ) |
---|
711 | { |
---|
712 | query_t *q = NULL; |
---|
713 | int numq = 0; |
---|
714 | |
---|
715 | if( irc->queries == NULL ) |
---|
716 | { |
---|
717 | irc_usermsg( irc, "Did I ask you something?" ); |
---|
718 | return; |
---|
719 | } |
---|
720 | |
---|
721 | /* If there's an argument, the user seems to want to answer another question than the |
---|
722 | first/last (depending on the query_order setting) one. */ |
---|
723 | if( cmd[1] ) |
---|
724 | { |
---|
725 | if( sscanf( cmd[1], "%d", &numq ) != 1 ) |
---|
726 | { |
---|
727 | irc_usermsg( irc, "Invalid query number" ); |
---|
728 | return; |
---|
729 | } |
---|
730 | |
---|
731 | for( q = irc->queries; q; q = q->next, numq -- ) |
---|
732 | if( numq == 0 ) |
---|
733 | break; |
---|
734 | |
---|
735 | if( !q ) |
---|
736 | { |
---|
737 | irc_usermsg( irc, "Uhm, I never asked you something like that..." ); |
---|
738 | return; |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | if( g_strcasecmp( cmd[0], "yes" ) == 0 ) |
---|
743 | query_answer( irc, q, 1 ); |
---|
744 | else if( g_strcasecmp( cmd[0], "no" ) == 0 ) |
---|
745 | query_answer( irc, q, 0 ); |
---|
746 | } |
---|
747 | |
---|
748 | static void cmd_set( irc_t *irc, char **cmd ) |
---|
749 | { |
---|
750 | char *set_name = cmd[1]; |
---|
751 | |
---|
752 | if( cmd[1] && cmd[2] ) |
---|
753 | { |
---|
754 | if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 ) |
---|
755 | { |
---|
756 | set_reset( &irc->set, cmd[2] ); |
---|
757 | set_name = cmd[2]; |
---|
758 | } |
---|
759 | else |
---|
760 | { |
---|
761 | set_setstr( &irc->set, cmd[1], cmd[2] ); |
---|
762 | } |
---|
763 | } |
---|
764 | if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */ |
---|
765 | { |
---|
766 | char *s = set_getstr( &irc->set, set_name ); |
---|
767 | if( s ) |
---|
768 | irc_usermsg( irc, "%s = `%s'", set_name, s ); |
---|
769 | else |
---|
770 | irc_usermsg( irc, "%s is empty", set_name ); |
---|
771 | } |
---|
772 | else |
---|
773 | { |
---|
774 | set_t *s = irc->set; |
---|
775 | while( s ) |
---|
776 | { |
---|
777 | if( s->value || s->def ) |
---|
778 | irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def ); |
---|
779 | else |
---|
780 | irc_usermsg( irc, "%s is empty", s->key ); |
---|
781 | s = s->next; |
---|
782 | } |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | static void cmd_save( irc_t *irc, char **cmd ) |
---|
787 | { |
---|
788 | if( storage_save( irc, TRUE ) == STORAGE_OK ) |
---|
789 | irc_usermsg( irc, "Configuration saved" ); |
---|
790 | else |
---|
791 | irc_usermsg( irc, "Configuration could not be saved!" ); |
---|
792 | } |
---|
793 | |
---|
794 | static void cmd_blist( irc_t *irc, char **cmd ) |
---|
795 | { |
---|
796 | int online = 0, away = 0, offline = 0; |
---|
797 | user_t *u; |
---|
798 | char s[256]; |
---|
799 | char *format; |
---|
800 | int n_online = 0, n_away = 0, n_offline = 0; |
---|
801 | |
---|
802 | if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 ) |
---|
803 | online = offline = away = 1; |
---|
804 | else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 ) |
---|
805 | offline = 1; |
---|
806 | else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 ) |
---|
807 | away = 1; |
---|
808 | else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 ) |
---|
809 | online = 1; |
---|
810 | else |
---|
811 | online = away = 1; |
---|
812 | |
---|
813 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
814 | format = "%s\t%s\t%s"; |
---|
815 | else |
---|
816 | format = "%-16.16s %-40.40s %s"; |
---|
817 | |
---|
818 | irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" ); |
---|
819 | |
---|
820 | for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away ) |
---|
821 | { |
---|
822 | if( online == 1 ) |
---|
823 | { |
---|
824 | g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user ); |
---|
825 | irc_usermsg( irc, format, u->nick, s, "Online" ); |
---|
826 | } |
---|
827 | |
---|
828 | n_online ++; |
---|
829 | } |
---|
830 | |
---|
831 | for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away ) |
---|
832 | { |
---|
833 | if( away == 1 ) |
---|
834 | { |
---|
835 | g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user ); |
---|
836 | irc_usermsg( irc, format, u->nick, s, u->away ); |
---|
837 | } |
---|
838 | n_away ++; |
---|
839 | } |
---|
840 | |
---|
841 | for( u = irc->users; u; u = u->next ) if( u->ic && !u->online ) |
---|
842 | { |
---|
843 | if( offline == 1 ) |
---|
844 | { |
---|
845 | g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user ); |
---|
846 | irc_usermsg( irc, format, u->nick, s, "Offline" ); |
---|
847 | } |
---|
848 | n_offline ++; |
---|
849 | } |
---|
850 | |
---|
851 | irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline ); |
---|
852 | } |
---|
853 | |
---|
854 | static void cmd_nick( irc_t *irc, char **cmd ) |
---|
855 | { |
---|
856 | account_t *a; |
---|
857 | |
---|
858 | if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) ) |
---|
859 | { |
---|
860 | irc_usermsg( irc, "Invalid account"); |
---|
861 | } |
---|
862 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
863 | { |
---|
864 | irc_usermsg( irc, "That account is not on-line" ); |
---|
865 | } |
---|
866 | else if ( !cmd[2] ) |
---|
867 | { |
---|
868 | irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" ); |
---|
869 | } |
---|
870 | else if ( !a->prpl->set_my_name ) |
---|
871 | { |
---|
872 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
873 | } |
---|
874 | else |
---|
875 | { |
---|
876 | irc_usermsg( irc, "Setting your name to `%s'", cmd[2] ); |
---|
877 | |
---|
878 | a->prpl->set_my_name( a->ic, cmd[2] ); |
---|
879 | } |
---|
880 | } |
---|
881 | |
---|
882 | static void cmd_qlist( irc_t *irc, char **cmd ) |
---|
883 | { |
---|
884 | query_t *q = irc->queries; |
---|
885 | int num; |
---|
886 | |
---|
887 | if( !q ) |
---|
888 | { |
---|
889 | irc_usermsg( irc, "There are no pending questions." ); |
---|
890 | return; |
---|
891 | } |
---|
892 | |
---|
893 | irc_usermsg( irc, "Pending queries:" ); |
---|
894 | |
---|
895 | for( num = 0; q; q = q->next, num ++ ) |
---|
896 | if( q->ic ) /* Not necessary yet, but it might come later */ |
---|
897 | irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question ); |
---|
898 | else |
---|
899 | irc_usermsg( irc, "%d, BitlBee: %s", num, q->question ); |
---|
900 | } |
---|
901 | |
---|
902 | static void cmd_join_chat( irc_t *irc, char **cmd ) |
---|
903 | { |
---|
904 | account_t *a; |
---|
905 | struct im_connection *ic; |
---|
906 | char *chat, *channel, *nick = NULL, *password = NULL; |
---|
907 | struct groupchat *c; |
---|
908 | |
---|
909 | if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
910 | { |
---|
911 | irc_usermsg( irc, "Invalid account" ); |
---|
912 | return; |
---|
913 | } |
---|
914 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
915 | { |
---|
916 | irc_usermsg( irc, "That account is not on-line" ); |
---|
917 | return; |
---|
918 | } |
---|
919 | else if( a->prpl->chat_join == NULL ) |
---|
920 | { |
---|
921 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
922 | return; |
---|
923 | } |
---|
924 | ic = a->ic; |
---|
925 | |
---|
926 | chat = cmd[2]; |
---|
927 | if( cmd[3] ) |
---|
928 | { |
---|
929 | if( cmd[3][0] != '#' && cmd[3][0] != '&' ) |
---|
930 | channel = g_strdup_printf( "&%s", cmd[3] ); |
---|
931 | else |
---|
932 | channel = g_strdup( cmd[3] ); |
---|
933 | } |
---|
934 | else |
---|
935 | { |
---|
936 | char *s; |
---|
937 | |
---|
938 | channel = g_strdup_printf( "&%s", chat ); |
---|
939 | if( ( s = strchr( channel, '@' ) ) ) |
---|
940 | *s = 0; |
---|
941 | } |
---|
942 | if( cmd[3] && cmd[4] ) |
---|
943 | nick = cmd[4]; |
---|
944 | else |
---|
945 | nick = irc->nick; |
---|
946 | if( cmd[3] && cmd[4] && cmd[5] ) |
---|
947 | password = cmd[5]; |
---|
948 | |
---|
949 | if( !nick_ok( channel + 1 ) ) |
---|
950 | { |
---|
951 | irc_usermsg( irc, "Invalid channel name: %s", channel ); |
---|
952 | g_free( channel ); |
---|
953 | return; |
---|
954 | } |
---|
955 | else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) ) |
---|
956 | { |
---|
957 | irc_usermsg( irc, "Channel already exists: %s", channel ); |
---|
958 | g_free( channel ); |
---|
959 | return; |
---|
960 | } |
---|
961 | |
---|
962 | if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) ) |
---|
963 | { |
---|
964 | g_free( c->channel ); |
---|
965 | c->channel = channel; |
---|
966 | } |
---|
967 | else |
---|
968 | { |
---|
969 | irc_usermsg( irc, "Tried to join chat, not sure if this was successful" ); |
---|
970 | g_free( channel ); |
---|
971 | } |
---|
972 | } |
---|
973 | |
---|
974 | static void cmd_transfers( irc_t *irc, char **cmd ) |
---|
975 | { |
---|
976 | GSList *files = irc->file_transfers; |
---|
977 | enum { LIST, REJECT, CANCEL }; |
---|
978 | int subcmd = LIST; |
---|
979 | int fid; |
---|
980 | |
---|
981 | if( !files ) |
---|
982 | { |
---|
983 | irc_usermsg( irc, "No pending transfers" ); |
---|
984 | return; |
---|
985 | } |
---|
986 | |
---|
987 | if( cmd[1] && |
---|
988 | ( strcmp( cmd[1], "reject" ) == 0 ) ) |
---|
989 | { |
---|
990 | subcmd = REJECT; |
---|
991 | } |
---|
992 | else if( cmd[1] && |
---|
993 | ( strcmp( cmd[1], "cancel" ) == 0 ) && |
---|
994 | cmd[2] && |
---|
995 | ( fid = atoi( cmd[2] ) ) ) |
---|
996 | { |
---|
997 | subcmd = CANCEL; |
---|
998 | } |
---|
999 | |
---|
1000 | for( ; files; files = g_slist_next( files ) ) |
---|
1001 | { |
---|
1002 | file_transfer_t *file = files->data; |
---|
1003 | |
---|
1004 | switch( subcmd ) { |
---|
1005 | case LIST: |
---|
1006 | if ( file->status == FT_STATUS_LISTENING ) |
---|
1007 | irc_usermsg( irc, |
---|
1008 | "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name); |
---|
1009 | else |
---|
1010 | { |
---|
1011 | int kb_per_s = 0; |
---|
1012 | time_t diff = time( NULL ) - file->started; |
---|
1013 | if ( ( file->started > 0 ) && ( file->bytes_transferred > 0 ) ) |
---|
1014 | kb_per_s = file->bytes_transferred / 1024 / diff; |
---|
1015 | |
---|
1016 | irc_usermsg( irc, |
---|
1017 | "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, file->file_name, |
---|
1018 | file->bytes_transferred/1024, file->file_size/1024, kb_per_s); |
---|
1019 | } |
---|
1020 | break; |
---|
1021 | case REJECT: |
---|
1022 | if( file->status == FT_STATUS_LISTENING ) |
---|
1023 | { |
---|
1024 | irc_usermsg( irc, "Rejecting file transfer for %s", file->file_name ); |
---|
1025 | imcb_file_canceled( file, "Denied by user" ); |
---|
1026 | } |
---|
1027 | break; |
---|
1028 | case CANCEL: |
---|
1029 | if( file->local_id == fid ) |
---|
1030 | { |
---|
1031 | irc_usermsg( irc, "Canceling file transfer for %s", file->file_name ); |
---|
1032 | imcb_file_canceled( file, "Canceled by user" ); |
---|
1033 | } |
---|
1034 | break; |
---|
1035 | } |
---|
1036 | } |
---|
1037 | } |
---|
1038 | |
---|
1039 | const command_t commands[] = { |
---|
1040 | { "help", 0, cmd_help, 0 }, |
---|
1041 | { "identify", 1, cmd_identify, 0 }, |
---|
1042 | { "register", 1, cmd_register, 0 }, |
---|
1043 | { "drop", 1, cmd_drop, 0 }, |
---|
1044 | { "account", 1, cmd_account, 0 }, |
---|
1045 | { "add", 2, cmd_add, 0 }, |
---|
1046 | { "info", 1, cmd_info, 0 }, |
---|
1047 | { "rename", 2, cmd_rename, 0 }, |
---|
1048 | { "remove", 1, cmd_remove, 0 }, |
---|
1049 | { "block", 1, cmd_block, 0 }, |
---|
1050 | { "allow", 1, cmd_allow, 0 }, |
---|
1051 | { "save", 0, cmd_save, 0 }, |
---|
1052 | { "set", 0, cmd_set, 0 }, |
---|
1053 | { "yes", 0, cmd_yesno, 0 }, |
---|
1054 | { "no", 0, cmd_yesno, 0 }, |
---|
1055 | { "blist", 0, cmd_blist, 0 }, |
---|
1056 | { "nick", 1, cmd_nick, 0 }, |
---|
1057 | { "qlist", 0, cmd_qlist, 0 }, |
---|
1058 | { "join_chat", 2, cmd_join_chat, 0 }, |
---|
1059 | { "transfers", 0, cmd_transfers, 0 }, |
---|
1060 | { NULL } |
---|
1061 | }; |
---|