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