source: root_commands.c @ e7bc722

Last change on this file since e7bc722 was e7bc722, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-31T00:04:53Z

Integrated cmd_set() and the "account set" into one fully unreadable
cmd_set_real() function and using this to get a proper "chat set" command.

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