source: root_commands.c @ 35529ae

Last change on this file since 35529ae was 07054a5, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-31T22:49:32Z

"chat add" can generate a channel name by itself if necessary. Also fixed
MIN_ARGS, using a variable named i showed why precompiler macros really
are evil. :-)

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