source: root_commands.c @ dded27d

Last change on this file since dded27d was e731120, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-01-11T00:45:18Z

Adding own handle to protocol name in blist output for people with multiple
accounts on the same IM network.

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