source: root_commands.c @ 5c9512f

Last change on this file since 5c9512f was 5c9512f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-30T09:17:18Z

Made set.c API more generic so it's not specific to irc_t structures anymore,
but can be used for account_t structures too, for example.

  • Property mode set to 100644
File size: 20.0 KB
RevLine 
[b7d3cc34]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
[f73b969]34void root_command_string( irc_t *irc, user_t *u, char *command, int flags )
[7e563ed]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                        }
[619a681]59                        else
60                        {
61                                break;
62                        }
[7e563ed]63                }
[79c6f9f]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                }
[7e563ed]71                else if( *s == q )
72                {
73                        q = *s = 0;
74                }
75        cmd[k] = NULL;
76       
[f73b969]77        root_command( irc, cmd );
[7e563ed]78}
79
[f73b969]80void root_command( irc_t *irc, char *cmd[] )
[7e563ed]81{       
82        int i;
83       
84        if( !cmd[0] )
[f73b969]85                return;
[7e563ed]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 );
[f73b969]93                                return;
[7e563ed]94                        }
95                        commands[i].execute( irc, cmd );
[f73b969]96                        return;
[7e563ed]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
[f73b969]102static void cmd_help( irc_t *irc, char **cmd )
[b7d3cc34]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
[90bbb0e]129static void cmd_account( irc_t *irc, char **cmd );
130
[f73b969]131static void cmd_identify( irc_t *irc, char **cmd )
[b7d3cc34]132{
[ab49fdc]133        storage_status_t status = storage_load( irc->nick, cmd[1], irc );
[90bbb0e]134        char *account_on[] = { "account", "on", NULL };
[b7d3cc34]135       
[09adf08]136        switch (status) {
137        case STORAGE_INVALID_PASSWORD:
[b7d3cc34]138                irc_usermsg( irc, "Incorrect password" );
[09adf08]139                break;
140        case STORAGE_NO_SUCH_USER:
[b7d3cc34]141                irc_usermsg( irc, "The nick is (probably) not registered" );
[09adf08]142                break;
143        case STORAGE_OK:
[6ee9d2d]144                irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
[238f828]145                irc_umode_set( irc, "+R", 1 );
[5c9512f]146                if( set_getint( &irc->set, "auto_connect" ) )
[90bbb0e]147                        cmd_account( irc, account_on );
[09adf08]148                break;
[c121f89]149        case STORAGE_OTHER_ERROR:
[09adf08]150        default:
[c121f89]151                irc_usermsg( irc, "Unknown error while loading configuration" );
[09adf08]152                break;
[b7d3cc34]153        }
154}
155
[f73b969]156static void cmd_register( irc_t *irc, char **cmd )
[b7d3cc34]157{
158        if( global.conf->authmode == AUTHMODE_REGISTERED )
159        {
160                irc_usermsg( irc, "This server does not allow registering new accounts" );
[f73b969]161                return;
[b7d3cc34]162        }
[1ee6c18]163
[7cad7b4]164        irc_setpass( irc, cmd[1] );
[ab49fdc]165        switch( storage_save( irc, FALSE )) {
[a1f17d4]166                case STORAGE_ALREADY_EXISTS:
167                        irc_usermsg( irc, "Nick is already registered" );
168                        break;
169                       
170                case STORAGE_OK:
[79e826a]171                        irc->status |= USTATUS_IDENTIFIED;
[238f828]172                        irc_umode_set( irc, "+R", 1 );
[a1f17d4]173                        break;
174
175                default:
176                        irc_usermsg( irc, "Error registering" );
177                        break;
[b7d3cc34]178        }
179}
180
[f73b969]181static void cmd_drop( irc_t *irc, char **cmd )
[b7d3cc34]182{
[a1f17d4]183        storage_status_t status;
184       
[ab49fdc]185        status = storage_remove (irc->nick, cmd[1]);
[a1f17d4]186        switch (status) {
187        case STORAGE_NO_SUCH_USER:
[b7d3cc34]188                irc_usermsg( irc, "That account does not exist" );
[f73b969]189                break;
[a1f17d4]190        case STORAGE_INVALID_PASSWORD:
[1ee6c18]191                irc_usermsg( irc, "Password invalid" );
[f73b969]192                break;
[a1f17d4]193        case STORAGE_OK:
[7cad7b4]194                irc_setpass( irc, NULL );
[79e826a]195                irc->status &= ~USTATUS_IDENTIFIED;
[238f828]196                irc_umode_set( irc, "-R", 1 );
[a1f17d4]197                irc_usermsg( irc, "Account `%s' removed", irc->nick );
[f73b969]198                break;
[a1f17d4]199        default:
200                irc_usermsg( irc, "Error: '%d'", status );
[f73b969]201                break;
[b7d3cc34]202        }
203}
204
[f73b969]205static void cmd_account( irc_t *irc, char **cmd )
[b7d3cc34]206{
207        account_t *a;
208       
[3af70b0]209        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
[b7d3cc34]210        {
211                irc_usermsg( irc, "This server only accepts registered users" );
[f73b969]212                return;
[b7d3cc34]213        }
214       
215        if( g_strcasecmp( cmd[1], "add" ) == 0 )
216        {
[7b23afd]217                struct prpl *prpl;
[b7d3cc34]218               
219                if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
220                {
221                        irc_usermsg( irc, "Not enough parameters" );
[f73b969]222                        return;
[b7d3cc34]223                }
224               
[7b23afd]225                prpl = find_protocol(cmd[2]);
[b7d3cc34]226               
[7b23afd]227                if( prpl == NULL )
[b7d3cc34]228                {
229                        irc_usermsg( irc, "Unknown protocol" );
[f73b969]230                        return;
[b7d3cc34]231                }
232
[7b23afd]233                a = account_add( irc, prpl, cmd[3], cmd[4] );
[b7d3cc34]234               
235                if( cmd[5] )
236                        a->server = g_strdup( cmd[5] );
237               
238                irc_usermsg( irc, "Account successfully added" );
239        }
240        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
241        {
242                if( !cmd[2] )
243                {
244                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
245                }
246                else if( !( a = account_get( irc, cmd[2] ) ) )
247                {
248                        irc_usermsg( irc, "Invalid account" );
249                }
250                else if( a->gc )
251                {
252                        irc_usermsg( irc, "Account is still logged in, can't delete" );
253                }
254                else
255                {
256                        account_del( irc, a );
257                        irc_usermsg( irc, "Account deleted" );
258                }
259        }
260        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
261        {
262                int i = 0;
263               
[e6e1f18]264                if( strchr( irc->umode, 'b' ) )
265                        irc_usermsg( irc, "Account list:" );
266               
[b7d3cc34]267                for( a = irc->accounts; a; a = a->next )
268                {
269                        char *con;
270                       
271                        if( a->gc && ( a->gc->flags & OPT_LOGGED_IN ) )
272                                con = " (connected)";
273                        else if( a->gc )
274                                con = " (connecting)";
275                        else if( a->reconnect )
276                                con = " (awaiting reconnect)";
277                        else
278                                con = "";
279                       
[7b23afd]280                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
[b7d3cc34]281                       
282                        i ++;
283                }
284                irc_usermsg( irc, "End of account list" );
285        }
286        else if( g_strcasecmp( cmd[1], "on" ) == 0 )
287        {
288                if( cmd[2] )
289                {
290                        if( ( a = account_get( irc, cmd[2] ) ) )
291                        {
292                                if( a->gc )
293                                {
294                                        irc_usermsg( irc, "Account already online" );
[f73b969]295                                        return;
[b7d3cc34]296                                }
297                                else
298                                {
299                                        account_on( irc, a );
300                                }
301                        }
302                        else
303                        {
304                                irc_usermsg( irc, "Invalid account" );
[f73b969]305                                return;
[b7d3cc34]306                        }
307                }
308                else
309                {
310                        if ( irc->accounts ) {
311                                irc_usermsg( irc, "Trying to get all accounts connected..." );
312                       
313                                for( a = irc->accounts; a; a = a->next )
[2b14eef]314                                        if( !a->gc && a->auto_connect )
[b7d3cc34]315                                                account_on( irc, a );
316                        } 
317                        else
318                        {
319                                irc_usermsg( irc, "No accounts known. Use 'account add' to add one." );
320                        }
321                }
322        }
323        else if( g_strcasecmp( cmd[1], "off" ) == 0 )
324        {
325                if( !cmd[2] )
326                {
327                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
328                       
329                        for( a = irc->accounts; a; a = a->next )
330                        {
331                                if( a->gc )
332                                        account_off( irc, a );
333                                else if( a->reconnect )
334                                        cancel_auto_reconnect( a );
335                        }
336                }
337                else if( ( a = account_get( irc, cmd[2] ) ) )
338                {
339                        if( a->gc )
340                        {
341                                account_off( irc, a );
342                        }
343                        else if( a->reconnect )
344                        {
345                                cancel_auto_reconnect( a );
346                                irc_usermsg( irc, "Reconnect cancelled" );
347                        }
348                        else
349                        {
350                                irc_usermsg( irc, "Account already offline" );
[f73b969]351                                return;
[b7d3cc34]352                        }
353                }
354                else
355                {
356                        irc_usermsg( irc, "Invalid account" );
[f73b969]357                        return;
[b7d3cc34]358                }
359        }
360        else
361        {
[5c09a59]362                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
[b7d3cc34]363        }
364}
365
[f73b969]366static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]367{
368        account_t *a;
[f8de26f]369        int add_for_real = 1;
370       
371        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
372        {
373                add_for_real = 0;
374                cmd ++;         /* So evil... :-D */
375        }
[b7d3cc34]376       
377        if( !( a = account_get( irc, cmd[1] ) ) )
378        {
379                irc_usermsg( irc, "Invalid account" );
[f73b969]380                return;
[b7d3cc34]381        }
382        else if( !( a->gc && ( a->gc->flags & OPT_LOGGED_IN ) ) )
383        {
384                irc_usermsg( irc, "That account is not on-line" );
[f73b969]385                return;
[b7d3cc34]386        }
387       
388        if( cmd[3] )
389        {
390                if( !nick_ok( cmd[3] ) )
391                {
392                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
[f73b969]393                        return;
[b7d3cc34]394                }
395                else if( user_find( irc, cmd[3] ) )
396                {
397                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]398                        return;
[b7d3cc34]399                }
400                else
401                {
[7b23afd]402                        nick_set( irc, cmd[2], a->gc->prpl, cmd[3] );
[b7d3cc34]403                }
404        }
[f8de26f]405       
406        /* By making this optional, you can talk to people without having to
407           add them to your *real* (server-side) contact list. */
408        if( add_for_real )
409                a->gc->prpl->add_buddy( a->gc, cmd[2] );
410               
[b7d3cc34]411        add_buddy( a->gc, NULL, cmd[2], cmd[2] );
412       
413        irc_usermsg( irc, "User `%s' added to your contact list as `%s'", cmd[2], user_findhandle( a->gc, cmd[2] )->nick );
414}
415
[f73b969]416static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]417{
418        struct gaim_connection *gc;
419        account_t *a;
420       
421        if( !cmd[2] )
422        {
423                user_t *u = user_find( irc, cmd[1] );
424                if( !u || !u->gc )
425                {
426                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]427                        return;
[b7d3cc34]428                }
429                gc = u->gc;
430                cmd[2] = u->handle;
431        }
432        else if( !( a = account_get( irc, cmd[1] ) ) )
433        {
434                irc_usermsg( irc, "Invalid account" );
[f73b969]435                return;
[b7d3cc34]436        }
437        else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) )
438        {
439                irc_usermsg( irc, "That account is not on-line" );
[f73b969]440                return;
[b7d3cc34]441        }
442       
443        if( !gc->prpl->get_info )
444        {
445                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
446        }
[f73b969]447        else
448        {
449                gc->prpl->get_info( gc, cmd[2] );
450        }
[b7d3cc34]451}
452
[f73b969]453static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]454{
455        user_t *u;
456       
457        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
458        {
459                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
460        }
[f73b969]461        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
[b7d3cc34]462        {
463                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
464        }
[f73b969]465        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]466        {
467                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
468        }
[f73b969]469        else if( !( u = user_find( irc, cmd[1] ) ) )
[b7d3cc34]470        {
471                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
472        }
[f73b969]473        else
[b7d3cc34]474        {
[f73b969]475                user_rename( irc, cmd[1], cmd[2] );
476                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
477                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
478                {
479                        g_free( irc->mynick );
480                        irc->mynick = g_strdup( cmd[2] );
481                }
482                else if( u->send_handler == buddy_send_handler )
483                {
484                        nick_set( irc, u->handle, u->gc->prpl, cmd[2] );
485                }
486               
487                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]488        }
489}
490
[f73b969]491static void cmd_remove( irc_t *irc, char **cmd )
[b7d3cc34]492{
493        user_t *u;
494        char *s;
495       
496        if( !( u = user_find( irc, cmd[1] ) ) || !u->gc )
497        {
498                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
[f73b969]499                return;
[b7d3cc34]500        }
501        s = g_strdup( u->handle );
502       
503        u->gc->prpl->remove_buddy( u->gc, u->handle, NULL );
504        user_del( irc, cmd[1] );
505        nick_del( irc, cmd[1] );
506       
507        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
508        g_free( s );
509       
[f73b969]510        return;
[b7d3cc34]511}
512
[f73b969]513static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]514{
515        struct gaim_connection *gc;
516        account_t *a;
517       
[87b6a3e]518        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->gc )
519        {
520                char *format;
521                GSList *l;
522               
523                if( strchr( irc->umode, 'b' ) != NULL )
524                        format = "%s\t%s";
525                else
[57ef864]526                        format = "%-32.32s  %-16.16s";
[87b6a3e]527               
528                irc_usermsg( irc, format, "Handle", "Nickname" );
529                for( l = a->gc->deny; l; l = l->next )
530                {
531                        user_t *u = user_findhandle( a->gc, l->data );
532                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
533                }
534                irc_usermsg( irc, "End of list." );
535               
536                return;
537        }
538        else if( !cmd[2] )
[b7d3cc34]539        {
540                user_t *u = user_find( irc, cmd[1] );
541                if( !u || !u->gc )
542                {
543                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]544                        return;
[b7d3cc34]545                }
546                gc = u->gc;
547                cmd[2] = u->handle;
548        }
549        else if( !( a = account_get( irc, cmd[1] ) ) )
550        {
551                irc_usermsg( irc, "Invalid account" );
[f73b969]552                return;
[b7d3cc34]553        }
554        else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) )
555        {
556                irc_usermsg( irc, "That account is not on-line" );
[f73b969]557                return;
[b7d3cc34]558        }
559       
560        if( !gc->prpl->add_deny || !gc->prpl->rem_permit )
561        {
562                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
563        }
564        else
565        {
[da3b536]566                bim_rem_allow( gc, cmd[2] );
567                bim_add_block( gc, cmd[2] );
568                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]569        }
570}
571
[f73b969]572static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]573{
574        struct gaim_connection *gc;
575        account_t *a;
576       
[87b6a3e]577        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->gc )
578        {
579                char *format;
580                GSList *l;
581               
582                if( strchr( irc->umode, 'b' ) != NULL )
583                        format = "%s\t%s";
584                else
[57ef864]585                        format = "%-32.32s  %-16.16s";
[87b6a3e]586               
587                irc_usermsg( irc, format, "Handle", "Nickname" );
588                for( l = a->gc->deny; l; l = l->next )
589                {
590                        user_t *u = user_findhandle( a->gc, l->data );
591                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
592                }
593                irc_usermsg( irc, "End of list." );
594               
595                return;
596        }
597        else if( !cmd[2] )
[b7d3cc34]598        {
599                user_t *u = user_find( irc, cmd[1] );
600                if( !u || !u->gc )
601                {
602                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]603                        return;
[b7d3cc34]604                }
605                gc = u->gc;
606                cmd[2] = u->handle;
607        }
608        else if( !( a = account_get( irc, cmd[1] ) ) )
609        {
610                irc_usermsg( irc, "Invalid account" );
[f73b969]611                return;
[b7d3cc34]612        }
613        else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) )
614        {
615                irc_usermsg( irc, "That account is not on-line" );
[f73b969]616                return;
[b7d3cc34]617        }
618       
619        if( !gc->prpl->rem_deny || !gc->prpl->add_permit )
620        {
621                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
622        }
623        else
624        {
[da3b536]625                bim_rem_block( gc, cmd[2] );
626                bim_add_allow( gc, cmd[2] );
[b7d3cc34]627               
[da3b536]628                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]629        }
630}
631
[f73b969]632static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]633{
634        query_t *q = NULL;
635        int numq = 0;
636       
637        if( irc->queries == NULL )
638        {
639                irc_usermsg( irc, "Did I ask you something?" );
[f73b969]640                return;
[b7d3cc34]641        }
642       
643        /* If there's an argument, the user seems to want to answer another question than the
644           first/last (depending on the query_order setting) one. */
645        if( cmd[1] )
646        {
647                if( sscanf( cmd[1], "%d", &numq ) != 1 )
648                {
649                        irc_usermsg( irc, "Invalid query number" );
[f73b969]650                        return;
[b7d3cc34]651                }
652               
653                for( q = irc->queries; q; q = q->next, numq -- )
654                        if( numq == 0 )
655                                break;
656               
657                if( !q )
658                {
659                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
[f73b969]660                        return;
[b7d3cc34]661                }
662        }
663       
664        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
665                query_answer( irc, q, 1 );
666        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
667                query_answer( irc, q, 0 );
668}
669
[f73b969]670static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]671{
672        if( cmd[1] && cmd[2] )
673        {
[5c9512f]674                set_setstr( &irc->set, cmd[1], cmd[2] );
[8365610]675               
676                if( ( strcmp( cmd[2], "=" ) ) == 0 && cmd[3] )
677                        irc_usermsg( irc, "Warning: Correct syntax: \002set <variable> <value>\002 (without =)" );
[b7d3cc34]678        }
679        if( cmd[1] ) /* else 'forgotten' on purpose.. Must show new value after changing */
680        {
[5c9512f]681                char *s = set_getstr( &irc->set, cmd[1] );
[b7d3cc34]682                if( s )
683                        irc_usermsg( irc, "%s = `%s'", cmd[1], s );
684        }
685        else
686        {
687                set_t *s = irc->set;
688                while( s )
689                {
690                        if( s->value || s->def )
691                                irc_usermsg( irc, "%s = `%s'", s->key, s->value?s->value:s->def );
692                        s = s->next;
693                }
694        }
695}
696
[f73b969]697static void cmd_save( irc_t *irc, char **cmd )
[b7d3cc34]698{
[ab49fdc]699        if( storage_save( irc, TRUE ) == STORAGE_OK )
[b7d3cc34]700                irc_usermsg( irc, "Configuration saved" );
701        else
702                irc_usermsg( irc, "Configuration could not be saved!" );
703}
704
[f73b969]705static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]706{
707        int online = 0, away = 0, offline = 0;
708        user_t *u;
[aefa533e]709        char s[256];
710        char *format;
[b7d3cc34]711        int n_online = 0, n_away = 0, n_offline = 0;
712       
713        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
714                online = offline = away = 1;
715        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
716                offline = 1;
717        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
718                away = 1;
719        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
720                online = 1;
721        else
722                online =  away = 1;
723       
[aefa533e]724        if( strchr( irc->umode, 'b' ) != NULL )
725                format = "%s\t%s\t%s";
726        else
727                format = "%-16.16s  %-40.40s  %s";
728       
729        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
[b7d3cc34]730       
[aefa533e]731        for( u = irc->users; u; u = u->next ) if( u->gc && u->online && !u->away )
[b7d3cc34]732        {
[aefa533e]733                if( online == 1 )
734                {
735                        g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->gc->user->prpl->name );
736                        irc_usermsg( irc, format, u->nick, s, "Online" );
737                }
738               
[b7d3cc34]739                n_online ++;
740        }
741
[aefa533e]742        for( u = irc->users; u; u = u->next ) if( u->gc && u->online && u->away )
[b7d3cc34]743        {
[aefa533e]744                if( away == 1 )
745                {
746                        g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->gc->user->prpl->name );
747                        irc_usermsg( irc, format, u->nick, s, u->away );
748                }
[b7d3cc34]749                n_away ++;
750        }
751       
[aefa533e]752        for( u = irc->users; u; u = u->next ) if( u->gc && !u->online )
[b7d3cc34]753        {
[aefa533e]754                if( offline == 1 )
755                {
756                        g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->gc->user->prpl->name );
757                        irc_usermsg( irc, format, u->nick, s, "Offline" );
758                }
[b7d3cc34]759                n_offline ++;
760        }
761       
[aa5ac01]762        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]763}
764
[f73b969]765static void cmd_nick( irc_t *irc, char **cmd ) 
[b7d3cc34]766{
767        account_t *a;
768
769        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
770        {
771                irc_usermsg( irc, "Invalid account");
772        }
773        else if( !( a->gc && ( a->gc->flags & OPT_LOGGED_IN ) ) )
774        {
775                irc_usermsg( irc, "That account is not on-line" );
776        }
777        else if ( !cmd[2] ) 
778        {
779                irc_usermsg( irc, "Your name is `%s'" , a->gc->displayname ? a->gc->displayname : "NULL" );
780        }
781        else if ( !a->gc->prpl->set_info ) 
782        {
783                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
784        }
785        else
786        {
787                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
788               
[7d31002]789                a->gc->prpl->set_info( a->gc, cmd[2] );
[b7d3cc34]790        }
791}
792
[f73b969]793static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]794{
795        query_t *q = irc->queries;
796        int num;
797       
798        if( !q )
799        {
800                irc_usermsg( irc, "There are no pending questions." );
[f73b969]801                return;
[b7d3cc34]802        }
803       
804        irc_usermsg( irc, "Pending queries:" );
805       
806        for( num = 0; q; q = q->next, num ++ )
[5c09a59]807                if( q->gc ) /* Not necessary yet, but it might come later */
[2cdd8ce]808                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->gc->prpl->name, q->gc->username, q->question );
[5c09a59]809                else
810                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]811}
812
[f73b969]813static void cmd_import_buddies( irc_t *irc, char **cmd )
[b7d3cc34]814{
815        struct gaim_connection *gc;
816        account_t *a;
817        nick_t *n;
818       
819        if( !( a = account_get( irc, cmd[1] ) ) )
820        {
821                irc_usermsg( irc, "Invalid account" );
[f73b969]822                return;
[b7d3cc34]823        }
824        else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) )
825        {
826                irc_usermsg( irc, "That account is not on-line" );
[f73b969]827                return;
[b7d3cc34]828        }
829       
830        if( cmd[2] )
831        {
832                if( g_strcasecmp( cmd[2], "clear" ) == 0 )
833                {
834                        user_t *u;
835                       
836                        for( u = irc->users; u; u = u->next )
837                                if( u->gc == gc )
838                                {
839                                        u->gc->prpl->remove_buddy( u->gc, u->handle, NULL );
840                                        user_del( irc, u->nick );
841                                }
842                       
843                        irc_usermsg( irc, "Old buddy list cleared." );
844                }
845                else
846                {
847                        irc_usermsg( irc, "Invalid argument: %s", cmd[2] );
[f73b969]848                        return;
[b7d3cc34]849                }
850        }
851       
852        for( n = gc->irc->nicks; n; n = n->next )
853        {
[7b23afd]854                if( n->proto == gc->prpl && !user_findhandle( gc, n->handle ) )
[b7d3cc34]855                {
856                        gc->prpl->add_buddy( gc, n->handle );
857                        add_buddy( gc, NULL, n->handle, NULL );
858                }
859        }
860       
861        irc_usermsg( irc, "Sent all add requests. Please wait for a while, the server needs some time to handle all the adds." );
862}
[0298d11]863
864const command_t commands[] = {
865        { "help",           0, cmd_help,           0 }, 
866        { "identify",       1, cmd_identify,       0 },
867        { "register",       1, cmd_register,       0 },
868        { "drop",           1, cmd_drop,           0 },
869        { "account",        1, cmd_account,        0 },
870        { "add",            2, cmd_add,            0 },
871        { "info",           1, cmd_info,           0 },
872        { "rename",         2, cmd_rename,         0 },
873        { "remove",         1, cmd_remove,         0 },
874        { "block",          1, cmd_block,          0 },
875        { "allow",          1, cmd_allow,          0 },
876        { "save",           0, cmd_save,           0 },
877        { "set",            0, cmd_set,            0 },
878        { "yes",            0, cmd_yesno,          0 },
879        { "no",             0, cmd_yesno,          0 },
880        { "blist",          0, cmd_blist,          0 },
881        { "nick",           1, cmd_nick,           0 },
882        { "import_buddies", 1, cmd_import_buddies, 0 },
883        { "qlist",          0, cmd_qlist,          0 },
884        { NULL }
885};
Note: See TracBrowser for help on using the repository browser.