source: root_commands.c @ 823de9d

Last change on this file since 823de9d was 823de9d, checked in by Sven Moritz Hallberg <pesco@…>, at 2009-03-12T19:10:06Z

commit updates by ashish shukla <wahjava@…>

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