source: root_commands.c @ 4eb4c0f

Last change on this file since 4eb4c0f was 4eb4c0f, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-16T17:15:31Z

merge in upstream changes

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