source: root_commands.c @ 96863f6

Last change on this file since 96863f6 was 5100caa, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-07-01T15:52:05Z

Added "account set" command.

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