source: root_commands.c @ 98de2cc

Last change on this file since 98de2cc was 23c4e64, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-05-11T19:37:34Z

Fixed NULL point dereference in "account set -del" code.

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