source: root_commands.c @ c05eb5b

Last change on this file since c05eb5b was c05eb5b, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-29T21:53:05Z

Checking account setting's flags again before changing them.

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