source: root_commands.c @ 99f929c

Last change on this file since 99f929c was 1195cec, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-04-05T13:03:31Z

Changed root nicknames are now saved. (Bug #378)

  • Property mode set to 100644
File size: 24.1 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( ( tmp = strchr( acc_handle, '/' ) ) )
426                {
427                        *tmp = 0;
428                        set_name = tmp + 1;
429                }
430               
431                if( ( a = account_get( irc, acc_handle ) ) == NULL )
432                {
433                        g_free( acc_handle );
434                        irc_usermsg( irc, "Invalid account" );
435                        return;
436                }
437               
438                if( cmd[3] && set_name )
439                {
440                        set_t *s = set_find( &a->set, set_name );
441                       
442                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
443                        {
444                                g_free( acc_handle );
445                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
446                                return;
447                        }
448                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
449                        {
450                                g_free( acc_handle );
451                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
452                                return;
453                        }
454                       
455                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
456                                set_reset( &a->set, set_name );
457                        else
458                                set_setstr( &a->set, set_name, cmd[3] );
459                }
460                if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
461                {
462                        char *s = set_getstr( &a->set, set_name );
463                        if( s )
464                                irc_usermsg( irc, "%s = `%s'", set_name, s );
465                        else
466                                irc_usermsg( irc, "%s is empty", set_name );
467                }
468                else
469                {
470                        set_t *s = a->set;
471                        while( s )
472                        {
473                                if( s->value || s->def )
474                                        irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
475                                else
476                                        irc_usermsg( irc, "%s is empty", s->key );
477                                s = s->next;
478                        }
479                }
480               
481                g_free( acc_handle );
482        }
483        else
484        {
485                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
486        }
487}
488
489static void cmd_add( irc_t *irc, char **cmd )
490{
491        account_t *a;
492        int add_on_server = 1;
493       
494        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
495        {
496                add_on_server = 0;
497                cmd ++;
498        }
499       
500        if( !( a = account_get( irc, cmd[1] ) ) )
501        {
502                irc_usermsg( irc, "Invalid account" );
503                return;
504        }
505        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
506        {
507                irc_usermsg( irc, "That account is not on-line" );
508                return;
509        }
510       
511        if( cmd[3] )
512        {
513                if( !nick_ok( cmd[3] ) )
514                {
515                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
516                        return;
517                }
518                else if( user_find( irc, cmd[3] ) )
519                {
520                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
521                        return;
522                }
523                else
524                {
525                        nick_set( a, cmd[2], cmd[3] );
526                }
527        }
528       
529        if( add_on_server )
530                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
531        else
532                /* Yeah, officially this is a call-*back*... So if we just
533                   called add_buddy, we'll wait for the IM server to respond
534                   before we do this. */
535                imcb_add_buddy( a->ic, cmd[2], NULL );
536       
537        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
538}
539
540static void cmd_info( irc_t *irc, char **cmd )
541{
542        struct im_connection *ic;
543        account_t *a;
544       
545        if( !cmd[2] )
546        {
547                user_t *u = user_find( irc, cmd[1] );
548                if( !u || !u->ic )
549                {
550                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
551                        return;
552                }
553                ic = u->ic;
554                cmd[2] = u->handle;
555        }
556        else if( !( a = account_get( irc, cmd[1] ) ) )
557        {
558                irc_usermsg( irc, "Invalid account" );
559                return;
560        }
561        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
562        {
563                irc_usermsg( irc, "That account is not on-line" );
564                return;
565        }
566       
567        if( !ic->acc->prpl->get_info )
568        {
569                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
570        }
571        else
572        {
573                ic->acc->prpl->get_info( ic, cmd[2] );
574        }
575}
576
577static void cmd_rename( irc_t *irc, char **cmd )
578{
579        user_t *u;
580       
581        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
582        {
583                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
584        }
585        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
586        {
587                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
588        }
589        else if( !nick_ok( cmd[2] ) )
590        {
591                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
592        }
593        else if( !( u = user_find( irc, cmd[1] ) ) )
594        {
595                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
596        }
597        else
598        {
599                user_rename( irc, cmd[1], cmd[2] );
600                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
601                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
602                {
603                        g_free( irc->mynick );
604                        irc->mynick = g_strdup( cmd[2] );
605                       
606                        if( strcmp( cmd[0], "set_rename" ) != 0 )
607                                set_setstr( &irc->set, "root_nick", cmd[2] );
608                }
609                else if( u->send_handler == buddy_send_handler )
610                {
611                        nick_set( u->ic->acc, u->handle, cmd[2] );
612                }
613               
614                irc_usermsg( irc, "Nick successfully changed" );
615        }
616}
617
618char *set_eval_root_nick( set_t *set, char *new_nick )
619{
620        irc_t *irc = set->data;
621       
622        if( strcmp( irc->mynick, new_nick ) != 0 )
623        {
624                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
625               
626                cmd_rename( irc, cmd );
627        }
628       
629        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : NULL;
630}
631
632static void cmd_remove( irc_t *irc, char **cmd )
633{
634        user_t *u;
635        char *s;
636       
637        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
638        {
639                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
640                return;
641        }
642        s = g_strdup( u->handle );
643       
644        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
645        nick_del( u->ic->acc, u->handle );
646        user_del( irc, cmd[1] );
647       
648        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
649        g_free( s );
650       
651        return;
652}
653
654static void cmd_block( irc_t *irc, char **cmd )
655{
656        struct im_connection *ic;
657        account_t *a;
658       
659        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
660        {
661                char *format;
662                GSList *l;
663               
664                if( strchr( irc->umode, 'b' ) != NULL )
665                        format = "%s\t%s";
666                else
667                        format = "%-32.32s  %-16.16s";
668               
669                irc_usermsg( irc, format, "Handle", "Nickname" );
670                for( l = a->ic->deny; l; l = l->next )
671                {
672                        user_t *u = user_findhandle( a->ic, l->data );
673                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
674                }
675                irc_usermsg( irc, "End of list." );
676               
677                return;
678        }
679        else if( !cmd[2] )
680        {
681                user_t *u = user_find( irc, cmd[1] );
682                if( !u || !u->ic )
683                {
684                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
685                        return;
686                }
687                ic = u->ic;
688                cmd[2] = u->handle;
689        }
690        else if( !( a = account_get( irc, cmd[1] ) ) )
691        {
692                irc_usermsg( irc, "Invalid account" );
693                return;
694        }
695        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
696        {
697                irc_usermsg( irc, "That account is not on-line" );
698                return;
699        }
700       
701        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
702        {
703                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
704        }
705        else
706        {
707                imc_rem_allow( ic, cmd[2] );
708                imc_add_block( ic, cmd[2] );
709                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
710        }
711}
712
713static void cmd_allow( irc_t *irc, char **cmd )
714{
715        struct im_connection *ic;
716        account_t *a;
717       
718        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
719        {
720                char *format;
721                GSList *l;
722               
723                if( strchr( irc->umode, 'b' ) != NULL )
724                        format = "%s\t%s";
725                else
726                        format = "%-32.32s  %-16.16s";
727               
728                irc_usermsg( irc, format, "Handle", "Nickname" );
729                for( l = a->ic->permit; l; l = l->next )
730                {
731                        user_t *u = user_findhandle( a->ic, l->data );
732                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
733                }
734                irc_usermsg( irc, "End of list." );
735               
736                return;
737        }
738        else if( !cmd[2] )
739        {
740                user_t *u = user_find( irc, cmd[1] );
741                if( !u || !u->ic )
742                {
743                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
744                        return;
745                }
746                ic = u->ic;
747                cmd[2] = u->handle;
748        }
749        else if( !( a = account_get( irc, cmd[1] ) ) )
750        {
751                irc_usermsg( irc, "Invalid account" );
752                return;
753        }
754        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
755        {
756                irc_usermsg( irc, "That account is not on-line" );
757                return;
758        }
759       
760        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
761        {
762                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
763        }
764        else
765        {
766                imc_rem_block( ic, cmd[2] );
767                imc_add_allow( ic, cmd[2] );
768               
769                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
770        }
771}
772
773static void cmd_yesno( irc_t *irc, char **cmd )
774{
775        query_t *q = NULL;
776        int numq = 0;
777       
778        if( irc->queries == NULL )
779        {
780                irc_usermsg( irc, "Did I ask you something?" );
781                return;
782        }
783       
784        /* If there's an argument, the user seems to want to answer another question than the
785           first/last (depending on the query_order setting) one. */
786        if( cmd[1] )
787        {
788                if( sscanf( cmd[1], "%d", &numq ) != 1 )
789                {
790                        irc_usermsg( irc, "Invalid query number" );
791                        return;
792                }
793               
794                for( q = irc->queries; q; q = q->next, numq -- )
795                        if( numq == 0 )
796                                break;
797               
798                if( !q )
799                {
800                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
801                        return;
802                }
803        }
804       
805        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
806                query_answer( irc, q, 1 );
807        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
808                query_answer( irc, q, 0 );
809}
810
811static void cmd_set( irc_t *irc, char **cmd )
812{
813        char *set_name = cmd[1];
814       
815        if( cmd[1] && cmd[2] )
816        {
817                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
818                {
819                        set_reset( &irc->set, cmd[2] );
820                        set_name = cmd[2];
821                }
822                else
823                {
824                        set_setstr( &irc->set, cmd[1], cmd[2] );
825                }
826        }
827        if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
828        {
829                char *s = set_getstr( &irc->set, set_name );
830                if( s )
831                        irc_usermsg( irc, "%s = `%s'", set_name, s );
832                else
833                        irc_usermsg( irc, "%s is empty", set_name );
834
835                if( strchr( set_name, '/' ) )
836                        irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." );
837        }
838        else
839        {
840                set_t *s = irc->set;
841                while( s )
842                {
843                        if( s->value || s->def )
844                                irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
845                        else
846                                irc_usermsg( irc, "%s is empty", s->key );
847                        s = s->next;
848                }
849        }
850}
851
852static void cmd_save( irc_t *irc, char **cmd )
853{
854        if( storage_save( irc, TRUE ) == STORAGE_OK )
855                irc_usermsg( irc, "Configuration saved" );
856        else
857                irc_usermsg( irc, "Configuration could not be saved!" );
858}
859
860static void cmd_blist( irc_t *irc, char **cmd )
861{
862        int online = 0, away = 0, offline = 0;
863        user_t *u;
864        char s[256];
865        char *format;
866        int n_online = 0, n_away = 0, n_offline = 0;
867       
868        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
869                online = offline = away = 1;
870        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
871                offline = 1;
872        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
873                away = 1;
874        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
875                online = 1;
876        else
877                online =  away = 1;
878       
879        if( strchr( irc->umode, 'b' ) != NULL )
880                format = "%s\t%s\t%s";
881        else
882                format = "%-16.16s  %-40.40s  %s";
883       
884        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
885       
886        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
887        {
888                if( online == 1 )
889                {
890                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
891                        irc_usermsg( irc, format, u->nick, s, "Online" );
892                }
893               
894                n_online ++;
895        }
896
897        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
898        {
899                if( away == 1 )
900                {
901                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
902                        irc_usermsg( irc, format, u->nick, s, u->away );
903                }
904                n_away ++;
905        }
906       
907        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
908        {
909                if( offline == 1 )
910                {
911                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
912                        irc_usermsg( irc, format, u->nick, s, "Offline" );
913                }
914                n_offline ++;
915        }
916       
917        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
918}
919
920static void cmd_nick( irc_t *irc, char **cmd ) 
921{
922        account_t *a;
923
924        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
925        {
926                irc_usermsg( irc, "Invalid account");
927        }
928        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
929        {
930                irc_usermsg( irc, "That account is not on-line" );
931        }
932        else if ( !cmd[2] ) 
933        {
934                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
935        }
936        else if ( !a->prpl->set_my_name ) 
937        {
938                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
939        }
940        else
941        {
942                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
943               
944                a->prpl->set_my_name( a->ic, cmd[2] );
945        }
946}
947
948static void cmd_qlist( irc_t *irc, char **cmd )
949{
950        query_t *q = irc->queries;
951        int num;
952       
953        if( !q )
954        {
955                irc_usermsg( irc, "There are no pending questions." );
956                return;
957        }
958       
959        irc_usermsg( irc, "Pending queries:" );
960       
961        for( num = 0; q; q = q->next, num ++ )
962                if( q->ic ) /* Not necessary yet, but it might come later */
963                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
964                else
965                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
966}
967
968static void cmd_join_chat( irc_t *irc, char **cmd )
969{
970        account_t *a;
971        struct im_connection *ic;
972        char *chat, *channel, *nick = NULL, *password = NULL;
973        struct groupchat *c;
974       
975        if( !( a = account_get( irc, cmd[1] ) ) )
976        {
977                irc_usermsg( irc, "Invalid account" );
978                return;
979        }
980        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
981        {
982                irc_usermsg( irc, "That account is not on-line" );
983                return;
984        }
985        else if( a->prpl->chat_join == NULL )
986        {
987                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
988                return;
989        }
990        ic = a->ic;
991       
992        chat = cmd[2];
993        if( cmd[3] )
994        {
995                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
996                        channel = g_strdup_printf( "&%s", cmd[3] );
997                else
998                        channel = g_strdup( cmd[3] );
999        }
1000        else
1001        {
1002                char *s;
1003               
1004                channel = g_strdup_printf( "&%s", chat );
1005                if( ( s = strchr( channel, '@' ) ) )
1006                        *s = 0;
1007        }
1008        if( cmd[3] && cmd[4] )
1009                nick = cmd[4];
1010        else
1011                nick = irc->nick;
1012        if( cmd[3] && cmd[4] && cmd[5] )
1013                password = cmd[5];
1014       
1015        if( !nick_ok( channel + 1 ) )
1016        {
1017                irc_usermsg( irc, "Invalid channel name: %s", channel );
1018                g_free( channel );
1019                return;
1020        }
1021        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
1022        {
1023                irc_usermsg( irc, "Channel already exists: %s", channel );
1024                g_free( channel );
1025                return;
1026        }
1027       
1028        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
1029        {
1030                g_free( c->channel );
1031                c->channel = channel;
1032        }
1033        else
1034        {
1035                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
1036                g_free( channel );
1037        }
1038}
1039
1040const command_t commands[] = {
1041        { "help",           0, cmd_help,           0 }, 
1042        { "identify",       1, cmd_identify,       0 },
1043        { "register",       1, cmd_register,       0 },
1044        { "drop",           1, cmd_drop,           0 },
1045        { "account",        1, cmd_account,        0 },
1046        { "add",            2, cmd_add,            0 },
1047        { "info",           1, cmd_info,           0 },
1048        { "rename",         2, cmd_rename,         0 },
1049        { "remove",         1, cmd_remove,         0 },
1050        { "block",          1, cmd_block,          0 },
1051        { "allow",          1, cmd_allow,          0 },
1052        { "save",           0, cmd_save,           0 },
1053        { "set",            0, cmd_set,            0 },
1054        { "yes",            0, cmd_yesno,          0 },
1055        { "no",             0, cmd_yesno,          0 },
1056        { "blist",          0, cmd_blist,          0 },
1057        { "nick",           1, cmd_nick,           0 },
1058        { "qlist",          0, cmd_qlist,          0 },
1059        { "join_chat",      2, cmd_join_chat,      0 },
1060        { NULL }
1061};
Note: See TracBrowser for help on using the repository browser.