source: root_commands.c @ 673a54c

Last change on this file since 673a54c was 673a54c, checked in by Sven Moritz Hallberg <pesco@…>, at 2009-03-12T19:33:28Z

pretty blind try at merging in the latest trunk

  • Property mode set to 100644
File size: 27.3 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
81#define MIN_ARGS( x, y... )                                                    \
82        do                                                                     \
83        {                                                                      \
84                int blaat;                                                     \
85                for( blaat = 0; blaat <= x; blaat ++ )                         \
86                        if( cmd[blaat] == NULL )                               \
87                        {                                                      \
88                                irc_usermsg( irc, "Not enough parameters given (need %d).", x ); \
89                                return y;                                      \
90                        }                                                      \
91        } while( 0 )
92
93void root_command( irc_t *irc, char *cmd[] )
94{       
95        int i;
96       
97        if( !cmd[0] )
98                return;
99       
100        for( i = 0; commands[i].command; i++ )
101                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
102                {
103                        MIN_ARGS( commands[i].required_parameters );
104                       
105                        commands[i].execute( irc, cmd );
106                        return;
107                }
108       
109        irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] );
110}
111
112static void cmd_help( irc_t *irc, char **cmd )
113{
114        char param[80];
115        int i;
116        char *s;
117       
118        memset( param, 0, sizeof(param) );
119        for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) {
120                if ( i != 1 )   // prepend space except for the first parameter
121                        strcat(param, " ");
122                strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 );
123        }
124
125        s = help_get( &(global.help), param );
126        if( !s ) s = help_get( &(global.help), "" );
127       
128        if( s )
129        {
130                irc_usermsg( irc, "%s", s );
131                g_free( s );
132        }
133        else
134        {
135                irc_usermsg( irc, "Error opening helpfile." );
136        }
137}
138
139static void cmd_account( irc_t *irc, char **cmd );
140
141static void cmd_identify( irc_t *irc, char **cmd )
142{
143        storage_status_t status = storage_load( irc, cmd[1] );
144        char *account_on[] = { "account", "on", NULL };
145       
146        switch (status) {
147        case STORAGE_INVALID_PASSWORD:
148                irc_usermsg( irc, "Incorrect password" );
149                break;
150        case STORAGE_NO_SUCH_USER:
151                irc_usermsg( irc, "The nick is (probably) not registered" );
152                break;
153        case STORAGE_OK:
154                irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
155                irc_setpass( irc, cmd[1] );
156                irc->status |= USTATUS_IDENTIFIED;
157                irc_umode_set( irc, "+R", 1 );
158                if( set_getbool( &irc->set, "auto_connect" ) )
159                        cmd_account( irc, account_on );
160                break;
161        case STORAGE_OTHER_ERROR:
162        default:
163                irc_usermsg( irc, "Unknown error while loading configuration" );
164                break;
165        }
166}
167
168static void cmd_register( irc_t *irc, char **cmd )
169{
170        if( global.conf->authmode == AUTHMODE_REGISTERED )
171        {
172                irc_usermsg( irc, "This server does not allow registering new accounts" );
173                return;
174        }
175
176        switch( storage_save( irc, cmd[1], FALSE ) ) {
177                case STORAGE_ALREADY_EXISTS:
178                        irc_usermsg( irc, "Nick is already registered" );
179                        break;
180                       
181                case STORAGE_OK:
182                        irc_usermsg( irc, "Account successfully created" );
183                        irc_setpass( irc, cmd[1] );
184                        irc->status |= USTATUS_IDENTIFIED;
185                        irc_umode_set( irc, "+R", 1 );
186                        break;
187
188                default:
189                        irc_usermsg( irc, "Error registering" );
190                        break;
191        }
192}
193
194static void cmd_drop( irc_t *irc, char **cmd )
195{
196        storage_status_t status;
197       
198        status = storage_remove (irc->nick, cmd[1]);
199        switch (status) {
200        case STORAGE_NO_SUCH_USER:
201                irc_usermsg( irc, "That account does not exist" );
202                break;
203        case STORAGE_INVALID_PASSWORD:
204                irc_usermsg( irc, "Password invalid" );
205                break;
206        case STORAGE_OK:
207                irc_setpass( irc, NULL );
208                irc->status &= ~USTATUS_IDENTIFIED;
209                irc_umode_set( irc, "-R", 1 );
210                irc_usermsg( irc, "Account `%s' removed", irc->nick );
211                break;
212        default:
213                irc_usermsg( irc, "Error: `%d'", status );
214                break;
215        }
216}
217
218struct cmd_account_del_data
219{
220        account_t *a;
221        irc_t *irc;
222};
223
224void cmd_account_del_yes( void *data )
225{
226        struct cmd_account_del_data *cad = data;
227        account_t *a;
228       
229        for( a = cad->irc->accounts; a && a != cad->a; a = a->next );
230       
231        if( a == NULL )
232        {
233                irc_usermsg( cad->irc, "Account already deleted" );
234        }
235        else if( a->ic )
236        {
237                irc_usermsg( cad->irc, "Account is still logged in, can't delete" );
238        }
239        else
240        {
241                account_del( cad->irc, a );
242                irc_usermsg( cad->irc, "Account deleted" );
243        }
244        g_free( data );
245}
246
247void cmd_account_del_no( void *data )
248{
249        g_free( data );
250}
251
252static void cmd_showset( irc_t *irc, set_t **head, char *key )
253{
254        char *val;
255       
256        if( ( val = set_getstr( head, key ) ) )
257                irc_usermsg( irc, "%s = `%s'", key, val );
258        else
259                irc_usermsg( irc, "%s is empty", key );
260}
261
262typedef set_t** (*cmd_set_findhead)( irc_t*, char* );
263typedef int (*cmd_set_checkflags)( irc_t*, set_t *set );
264
265static int cmd_set_real( irc_t *irc, char **cmd, cmd_set_findhead findhead, cmd_set_checkflags checkflags )
266{
267        char *set_full = NULL, *set_name = NULL, *tmp;
268        set_t **head;
269       
270        if( cmd[1] && g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
271        {
272                MIN_ARGS( 2, 0 );
273                set_full = cmd[2];
274        }
275        else
276                set_full = cmd[1];
277       
278        if( findhead == NULL )
279        {
280                set_name = set_full;
281               
282                head = &irc->set;
283        }
284        else 
285        {
286                char *id;
287               
288                if( ( tmp = strchr( set_full, '/' ) ) )
289                {
290                        id = g_strndup( set_full, ( tmp - set_full ) );
291                        set_name = tmp + 1;
292                }
293                else
294                {
295                        id = g_strdup( set_full );
296                }
297               
298                if( ( head = findhead( irc, id ) ) == NULL )
299                {
300                        g_free( id );
301                        irc_usermsg( irc, "Could not find setting." );
302                        return 0;
303                }
304                g_free( id );
305        }
306       
307        if( cmd[1] && cmd[2] && set_name )
308        {
309                set_t *s = set_find( head, set_name );
310                int st;
311               
312                if( checkflags && checkflags( irc, s ) == 0 )
313                        return 0;
314               
315                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
316                        st = set_reset( head, set_name );
317                else
318                        st = set_setstr( head, set_name, cmd[2] );
319               
320                if( set_getstr( head, set_name ) == NULL )
321                {
322                        if( st )
323                                irc_usermsg( irc, "Setting changed successfully" );
324                        else
325                                irc_usermsg( irc, "Failed to change setting" );
326                }
327                else
328                {
329                        cmd_showset( irc, head, set_name );
330                }
331        }
332        else if( set_name )
333        {
334                cmd_showset( irc, head, set_name );
335        }
336        else
337        {
338                set_t *s = *head;
339                while( s )
340                {
341                        cmd_showset( irc, &s, s->key );
342                        s = s->next;
343                }
344        }
345       
346        return 1;
347}
348
349static set_t **cmd_account_set_findhead( irc_t *irc, char *id )
350{
351        account_t *a;
352       
353        if( ( a = account_get( irc, id ) ) )
354                return &a->set;
355        else
356                return NULL;
357}
358
359static int cmd_account_set_checkflags( irc_t *irc, set_t *s )
360{
361        account_t *a = s->data;
362       
363        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
364        {
365                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
366                return 0;
367        }
368        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
369        {
370                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
371                return 0;
372        }
373       
374        return 1;
375}
376
377static void cmd_account( irc_t *irc, char **cmd )
378{
379        account_t *a;
380       
381        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
382        {
383                irc_usermsg( irc, "This server only accepts registered users" );
384                return;
385        }
386       
387        if( g_strcasecmp( cmd[1], "add" ) == 0 )
388        {
389                struct prpl *prpl;
390               
391                MIN_ARGS( 4 );
392               
393                prpl = find_protocol( cmd[2] );
394               
395                if( prpl == NULL )
396                {
397                        irc_usermsg( irc, "Unknown protocol" );
398                        return;
399                }
400
401                a = account_add( irc, prpl, cmd[3], cmd[4] );
402                if( cmd[5] )
403                {
404                        irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' "
405                                          "is now deprecated. Use `account set' instead." );
406                        set_setstr( &a->set, "server", cmd[5] );
407                }
408               
409                irc_usermsg( irc, "Account successfully added" );
410               
411                if(otr_check_for_key(a)) {
412                        irc_usermsg(irc, "otr: you will be notified when it completes");
413                }
414        }
415        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
416        {
417                MIN_ARGS( 2 );
418
419                if( !( a = account_get( irc, cmd[2] ) ) )
420                {
421                        irc_usermsg( irc, "Invalid account" );
422                }
423                else if( a->ic )
424                {
425                        irc_usermsg( irc, "Account is still logged in, can't delete" );
426                }
427                else
428                {
429                        struct cmd_account_del_data *cad;
430                        char *msg;
431                       
432                        cad = g_malloc( sizeof( struct cmd_account_del_data ) );
433                        cad->a = a;
434                        cad->irc = irc;
435                       
436                        msg = g_strdup_printf( "If you remove this account (%s(%s)), BitlBee will "
437                                               "also forget all your saved nicknames. If you want "
438                                               "to change your username/password, use the `account "
439                                               "set' command. Are you sure you want to delete this "
440                                               "account?", a->prpl->name, a->user );
441                        query_add( irc, NULL, msg, cmd_account_del_yes, cmd_account_del_no, cad );
442                        g_free( msg );
443                }
444        }
445        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
446        {
447                int i = 0;
448               
449                if( strchr( irc->umode, 'b' ) )
450                        irc_usermsg( irc, "Account list:" );
451               
452                for( a = irc->accounts; a; a = a->next )
453                {
454                        char *con;
455                       
456                        if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) )
457                                con = " (connected)";
458                        else if( a->ic )
459                                con = " (connecting)";
460                        else if( a->reconnect )
461                                con = " (awaiting reconnect)";
462                        else
463                                con = "";
464                       
465                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
466                       
467                        i ++;
468                }
469                irc_usermsg( irc, "End of account list" );
470        }
471        else if( g_strcasecmp( cmd[1], "on" ) == 0 )
472        {
473                if( cmd[2] )
474                {
475                        if( ( a = account_get( irc, cmd[2] ) ) )
476                        {
477                                if( a->ic )
478                                {
479                                        irc_usermsg( irc, "Account already online" );
480                                        return;
481                                }
482                                else
483                                {
484                                        account_on( irc, a );
485                                }
486                        }
487                        else
488                        {
489                                irc_usermsg( irc, "Invalid account" );
490                                return;
491                        }
492                }
493                else
494                {
495                        if ( irc->accounts ) {
496                                irc_usermsg( irc, "Trying to get all accounts connected..." );
497                       
498                                for( a = irc->accounts; a; a = a->next )
499                                        if( !a->ic && a->auto_connect )
500                                                account_on( irc, a );
501                        } 
502                        else
503                        {
504                                irc_usermsg( irc, "No accounts known. Use `account add' to add one." );
505                        }
506                }
507        }
508        else if( g_strcasecmp( cmd[1], "off" ) == 0 )
509        {
510                if( !cmd[2] )
511                {
512                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
513                       
514                        for( a = irc->accounts; a; a = a->next )
515                        {
516                                if( a->ic )
517                                        account_off( irc, a );
518                                else if( a->reconnect )
519                                        cancel_auto_reconnect( a );
520                        }
521                }
522                else if( ( a = account_get( irc, cmd[2] ) ) )
523                {
524                        if( a->ic )
525                        {
526                                account_off( irc, a );
527                        }
528                        else if( a->reconnect )
529                        {
530                                cancel_auto_reconnect( a );
531                                irc_usermsg( irc, "Reconnect cancelled" );
532                        }
533                        else
534                        {
535                                irc_usermsg( irc, "Account already offline" );
536                                return;
537                        }
538                }
539                else
540                {
541                        irc_usermsg( irc, "Invalid account" );
542                        return;
543                }
544        }
545        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
546        {
547                MIN_ARGS( 2 );
548               
549                cmd_set_real( irc, cmd + 1, cmd_account_set_findhead, cmd_account_set_checkflags );
550        }
551        else
552        {
553                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "account", cmd[1] );
554        }
555}
556
557static void cmd_add( irc_t *irc, char **cmd )
558{
559        account_t *a;
560        int add_on_server = 1;
561       
562        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
563        {
564                MIN_ARGS( 3 );
565                add_on_server = 0;
566                cmd ++;
567        }
568       
569        if( !( a = account_get( irc, cmd[1] ) ) )
570        {
571                irc_usermsg( irc, "Invalid account" );
572                return;
573        }
574        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
575        {
576                irc_usermsg( irc, "That account is not on-line" );
577                return;
578        }
579       
580        if( cmd[3] )
581        {
582                if( !nick_ok( cmd[3] ) )
583                {
584                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
585                        return;
586                }
587                else if( user_find( irc, cmd[3] ) )
588                {
589                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
590                        return;
591                }
592                else
593                {
594                        nick_set( a, cmd[2], cmd[3] );
595                }
596        }
597       
598        if( add_on_server )
599                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
600        else
601                /* Yeah, officially this is a call-*back*... So if we just
602                   called add_buddy, we'll wait for the IM server to respond
603                   before we do this. */
604                imcb_add_buddy( a->ic, cmd[2], NULL );
605       
606        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
607}
608
609static void cmd_info( irc_t *irc, char **cmd )
610{
611        struct im_connection *ic;
612        account_t *a;
613       
614        if( !cmd[2] )
615        {
616                user_t *u = user_find( irc, cmd[1] );
617                if( !u || !u->ic )
618                {
619                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
620                        return;
621                }
622                ic = u->ic;
623                cmd[2] = u->handle;
624        }
625        else if( !( a = account_get( irc, cmd[1] ) ) )
626        {
627                irc_usermsg( irc, "Invalid account" );
628                return;
629        }
630        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
631        {
632                irc_usermsg( irc, "That account is not on-line" );
633                return;
634        }
635       
636        if( !ic->acc->prpl->get_info )
637        {
638                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
639        }
640        else
641        {
642                ic->acc->prpl->get_info( ic, cmd[2] );
643        }
644}
645
646static void cmd_rename( irc_t *irc, char **cmd )
647{
648        user_t *u;
649       
650        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
651        {
652                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
653        }
654        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
655        {
656                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
657        }
658        else if( !nick_ok( cmd[2] ) )
659        {
660                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
661        }
662        else if( !( u = user_find( irc, cmd[1] ) ) )
663        {
664                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
665        }
666        else
667        {
668                user_rename( irc, cmd[1], cmd[2] );
669                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
670                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
671                {
672                        g_free( irc->mynick );
673                        irc->mynick = g_strdup( cmd[2] );
674                       
675                        /* If we're called internally (user did "set root_nick"),
676                           let's not go O(INF). :-) */
677                        if( strcmp( cmd[0], "set_rename" ) != 0 )
678                                set_setstr( &irc->set, "root_nick", cmd[2] );
679                }
680                else if( u->send_handler == buddy_send_handler )
681                {
682                        nick_set( u->ic->acc, u->handle, cmd[2] );
683                }
684               
685                irc_usermsg( irc, "Nick successfully changed" );
686        }
687}
688
689char *set_eval_root_nick( set_t *set, char *new_nick )
690{
691        irc_t *irc = set->data;
692       
693        if( strcmp( irc->mynick, new_nick ) != 0 )
694        {
695                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
696               
697                cmd_rename( irc, cmd );
698        }
699       
700        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
701}
702
703static void cmd_remove( irc_t *irc, char **cmd )
704{
705        user_t *u;
706        char *s;
707       
708        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
709        {
710                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
711                return;
712        }
713        s = g_strdup( u->handle );
714       
715        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
716        nick_del( u->ic->acc, u->handle );
717        user_del( irc, cmd[1] );
718       
719        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
720        g_free( s );
721       
722        return;
723}
724
725static void cmd_block( irc_t *irc, char **cmd )
726{
727        struct im_connection *ic;
728        account_t *a;
729       
730        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
731        {
732                char *format;
733                GSList *l;
734               
735                if( strchr( irc->umode, 'b' ) != NULL )
736                        format = "%s\t%s";
737                else
738                        format = "%-32.32s  %-16.16s";
739               
740                irc_usermsg( irc, format, "Handle", "Nickname" );
741                for( l = a->ic->deny; l; l = l->next )
742                {
743                        user_t *u = user_findhandle( a->ic, l->data );
744                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
745                }
746                irc_usermsg( irc, "End of list." );
747               
748                return;
749        }
750        else if( !cmd[2] )
751        {
752                user_t *u = user_find( irc, cmd[1] );
753                if( !u || !u->ic )
754                {
755                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
756                        return;
757                }
758                ic = u->ic;
759                cmd[2] = u->handle;
760        }
761        else if( !( a = account_get( irc, cmd[1] ) ) )
762        {
763                irc_usermsg( irc, "Invalid account" );
764                return;
765        }
766        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
767        {
768                irc_usermsg( irc, "That account is not on-line" );
769                return;
770        }
771       
772        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
773        {
774                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
775        }
776        else
777        {
778                imc_rem_allow( ic, cmd[2] );
779                imc_add_block( ic, cmd[2] );
780                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
781        }
782}
783
784static void cmd_allow( irc_t *irc, char **cmd )
785{
786        struct im_connection *ic;
787        account_t *a;
788       
789        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
790        {
791                char *format;
792                GSList *l;
793               
794                if( strchr( irc->umode, 'b' ) != NULL )
795                        format = "%s\t%s";
796                else
797                        format = "%-32.32s  %-16.16s";
798               
799                irc_usermsg( irc, format, "Handle", "Nickname" );
800                for( l = a->ic->permit; l; l = l->next )
801                {
802                        user_t *u = user_findhandle( a->ic, l->data );
803                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
804                }
805                irc_usermsg( irc, "End of list." );
806               
807                return;
808        }
809        else if( !cmd[2] )
810        {
811                user_t *u = user_find( irc, cmd[1] );
812                if( !u || !u->ic )
813                {
814                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
815                        return;
816                }
817                ic = u->ic;
818                cmd[2] = u->handle;
819        }
820        else if( !( a = account_get( irc, cmd[1] ) ) )
821        {
822                irc_usermsg( irc, "Invalid account" );
823                return;
824        }
825        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
826        {
827                irc_usermsg( irc, "That account is not on-line" );
828                return;
829        }
830       
831        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
832        {
833                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
834        }
835        else
836        {
837                imc_rem_block( ic, cmd[2] );
838                imc_add_allow( ic, cmd[2] );
839               
840                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
841        }
842}
843
844static void cmd_yesno( irc_t *irc, char **cmd )
845{
846        query_t *q = NULL;
847        int numq = 0;
848       
849        if( irc->queries == NULL )
850        {
851                irc_usermsg( irc, "Did I ask you something?" );
852                return;
853        }
854       
855        /* If there's an argument, the user seems to want to answer another question than the
856           first/last (depending on the query_order setting) one. */
857        if( cmd[1] )
858        {
859                if( sscanf( cmd[1], "%d", &numq ) != 1 )
860                {
861                        irc_usermsg( irc, "Invalid query number" );
862                        return;
863                }
864               
865                for( q = irc->queries; q; q = q->next, numq -- )
866                        if( numq == 0 )
867                                break;
868               
869                if( !q )
870                {
871                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
872                        return;
873                }
874        }
875       
876        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
877                query_answer( irc, q, 1 );
878        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
879                query_answer( irc, q, 0 );
880}
881
882static void cmd_set( irc_t *irc, char **cmd )
883{
884        cmd_set_real( irc, cmd, NULL, NULL );
885}
886
887static void cmd_save( irc_t *irc, char **cmd )
888{
889        if( ( irc->status & USTATUS_IDENTIFIED ) == 0 )
890                irc_usermsg( irc, "Please create an account first" );
891        else if( storage_save( irc, NULL, TRUE ) == STORAGE_OK )
892                irc_usermsg( irc, "Configuration saved" );
893        else
894                irc_usermsg( irc, "Configuration could not be saved!" );
895}
896
897static void cmd_blist( irc_t *irc, char **cmd )
898{
899        int online = 0, away = 0, offline = 0;
900        user_t *u;
901        char s[256];
902        char *format;
903        int n_online = 0, n_away = 0, n_offline = 0;
904       
905        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
906                online = offline = away = 1;
907        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
908                offline = 1;
909        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
910                away = 1;
911        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
912                online = 1;
913        else
914                online =  away = 1;
915       
916        if( strchr( irc->umode, 'b' ) != NULL )
917                format = "%s\t%s\t%s";
918        else
919                format = "%-16.16s  %-40.40s  %s";
920       
921        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
922       
923        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
924        {
925                if( online == 1 )
926                {
927                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
928                        irc_usermsg( irc, format, u->nick, s, "Online" );
929                }
930               
931                n_online ++;
932        }
933
934        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
935        {
936                if( away == 1 )
937                {
938                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
939                        irc_usermsg( irc, format, u->nick, s, u->away );
940                }
941                n_away ++;
942        }
943       
944        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
945        {
946                if( offline == 1 )
947                {
948                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
949                        irc_usermsg( irc, format, u->nick, s, "Offline" );
950                }
951                n_offline ++;
952        }
953       
954        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
955}
956
957static void cmd_nick( irc_t *irc, char **cmd ) 
958{
959        account_t *a;
960
961        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
962        {
963                irc_usermsg( irc, "Invalid account");
964        }
965        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
966        {
967                irc_usermsg( irc, "That account is not on-line" );
968        }
969        else if ( !cmd[2] ) 
970        {
971                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
972        }
973        else if ( !a->prpl->set_my_name ) 
974        {
975                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
976        }
977        else
978        {
979                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
980               
981                a->prpl->set_my_name( a->ic, cmd[2] );
982        }
983}
984
985static void cmd_qlist( irc_t *irc, char **cmd )
986{
987        query_t *q = irc->queries;
988        int num;
989       
990        if( !q )
991        {
992                irc_usermsg( irc, "There are no pending questions." );
993                return;
994        }
995       
996        irc_usermsg( irc, "Pending queries:" );
997       
998        for( num = 0; q; q = q->next, num ++ )
999                if( q->ic ) /* Not necessary yet, but it might come later */
1000                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
1001                else
1002                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
1003}
1004
1005static void cmd_join_chat( irc_t *irc, char **cmd )
1006{
1007        irc_usermsg( irc, "This command is now obsolete. "
1008                          "Please try the `chat' command instead." );
1009}
1010
1011static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
1012{
1013        struct chat *c;
1014       
1015        if( ( c = chat_get( irc, id ) ) )
1016                return &c->set;
1017        else
1018                return NULL;
1019}
1020
1021static void cmd_chat( irc_t *irc, char **cmd )
1022{
1023        account_t *acc;
1024        struct chat *c;
1025       
1026        if( g_strcasecmp( cmd[1], "add" ) == 0 )
1027        {
1028                char *channel, *s;
1029               
1030                MIN_ARGS( 3 );
1031               
1032                if( !( acc = account_get( irc, cmd[2] ) ) )
1033                {
1034                        irc_usermsg( irc, "Invalid account" );
1035                        return;
1036                }
1037               
1038                if( cmd[4] == NULL )
1039                {
1040                        channel = g_strdup( cmd[3] );
1041                        if( ( s = strchr( channel, '@' ) ) )
1042                                *s = 0;
1043                }
1044                else
1045                {
1046                        channel = g_strdup( cmd[4] );
1047                }
1048               
1049                if( strchr( CTYPES, channel[0] ) == NULL )
1050                {
1051                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
1052                        g_free( channel );
1053                        channel = s;
1054                }
1055               
1056                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
1057                        irc_usermsg( irc, "Chatroom added successfully." );
1058                else
1059                        irc_usermsg( irc, "Could not add chatroom." );
1060               
1061                g_free( channel );
1062        }
1063        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
1064        {
1065                int i = 0;
1066               
1067                if( strchr( irc->umode, 'b' ) )
1068                        irc_usermsg( irc, "Chatroom list:" );
1069               
1070                for( c = irc->chatrooms; c; c = c->next )
1071                {
1072                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
1073                                          c->acc->user, c->handle, c->channel );
1074                       
1075                        i ++;
1076                }
1077                irc_usermsg( irc, "End of chatroom list" );
1078        }
1079        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
1080        {
1081                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
1082        }
1083        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
1084        {
1085                MIN_ARGS( 2 );
1086               
1087                if( ( c = chat_get( irc, cmd[2] ) ) )
1088                {
1089                        chat_del( irc, c );
1090                }
1091                else
1092                {
1093                        irc_usermsg( irc, "Could not remove chat." );
1094                }
1095        }
1096        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1097        {
1098                user_t *u;
1099               
1100                MIN_ARGS( 2 );
1101               
1102                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1103                {
1104                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1105                        {
1106                                irc_usermsg( irc, "(Possible) failure while trying to open "
1107                                                  "a groupchat with %s.", u->nick );
1108                        }
1109                }
1110                else
1111                {
1112                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1113                }
1114        }
1115        else
1116        {
1117                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1118        }
1119
1120
1121
1122#if 0
1123        account_t *a;
1124        struct im_connection *ic;
1125        char *chat, *channel, *nick = NULL, *password = NULL;
1126        struct groupchat *c;
1127       
1128        if( !( a = account_get( irc, cmd[1] ) ) )
1129        {
1130                irc_usermsg( irc, "Invalid account" );
1131                return;
1132        }
1133        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
1134        {
1135                irc_usermsg( irc, "That account is not on-line" );
1136                return;
1137        }
1138        else if( a->prpl->chat_join == NULL )
1139        {
1140                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
1141                return;
1142        }
1143        ic = a->ic;
1144       
1145        chat = cmd[2];
1146        if( cmd[3] )
1147        {
1148                if( strchr( CTYPES, cmd[3][0] ) == NULL )
1149                        channel = g_strdup_printf( "&%s", cmd[3] );
1150                else
1151                        channel = g_strdup( cmd[3] );
1152        }
1153        else
1154        {
1155                char *s;
1156               
1157                channel = g_strdup_printf( "&%s", chat );
1158                if( ( s = strchr( channel, '@' ) ) )
1159                        *s = 0;
1160        }
1161        if( cmd[3] && cmd[4] )
1162                nick = cmd[4];
1163        else
1164                nick = irc->nick;
1165        if( cmd[3] && cmd[4] && cmd[5] )
1166                password = cmd[5];
1167       
1168        if( !nick_ok( channel + 1 ) )
1169        {
1170                irc_usermsg( irc, "Invalid channel name: %s", channel );
1171                g_free( channel );
1172                return;
1173        }
1174        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
1175        {
1176                irc_usermsg( irc, "Channel already exists: %s", channel );
1177                g_free( channel );
1178                return;
1179        }
1180       
1181        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
1182        {
1183                g_free( c->channel );
1184                c->channel = channel;
1185        }
1186        else
1187        {
1188                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
1189                g_free( channel );
1190        }
1191#endif
1192}
1193
1194const command_t commands[] = {
1195        { "help",           0, cmd_help,           0 }, 
1196        { "identify",       1, cmd_identify,       0 },
1197        { "register",       1, cmd_register,       0 },
1198        { "drop",           1, cmd_drop,           0 },
1199        { "account",        1, cmd_account,        0 },
1200        { "add",            2, cmd_add,            0 },
1201        { "info",           1, cmd_info,           0 },
1202        { "rename",         2, cmd_rename,         0 },
1203        { "remove",         1, cmd_remove,         0 },
1204        { "block",          1, cmd_block,          0 },
1205        { "allow",          1, cmd_allow,          0 },
1206        { "save",           0, cmd_save,           0 },
1207        { "set",            0, cmd_set,            0 },
1208        { "yes",            0, cmd_yesno,          0 },
1209        { "no",             0, cmd_yesno,          0 },
1210        { "blist",          0, cmd_blist,          0 },
1211        { "nick",           1, cmd_nick,           0 },
1212        { "qlist",          0, cmd_qlist,          0 },
1213        { "join_chat",      2, cmd_join_chat,      0 },
1214        { "otr",            1, cmd_otr,            0 }, 
1215        { "chat",           1, cmd_chat,           0 },
1216        { NULL }
1217};
Note: See TracBrowser for help on using the repository browser.