source: root_commands.c @ 5f8ab6a9

Last change on this file since 5f8ab6a9 was 5f8ab6a9, checked in by Sven Moritz Hallberg <pesco@…>, at 2010-06-03T10:41:03Z

merge in bitlbee 1.2.5

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