source: root_commands.c @ d995c9b

Last change on this file since d995c9b was d995c9b, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-31T14:54:39Z

Added cleanup code.

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