source: root_commands.c @ d4810df

Last change on this file since d4810df was d4810df, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-02T07:48:56Z

Fixed check for set -del arguments.

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