source: root_commands.c @ b119d5d

Last change on this file since b119d5d was f536a99, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-25T20:48:48Z

Fixed NULL pointer dereference on trying to read non-existent settings.

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