source: root_commands.c @ 2bebe15

Last change on this file since 2bebe15 was ced1e17, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-07T14:34:50Z

Merging BitlBee 1.2.3 release.

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