source: root_commands.c @ 4fefb77

Last change on this file since 4fefb77 was 2ea8736, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-06-24T21:48:44Z

Fixed another crash bug (this one was triggered by just typing "chat set").

  • 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( s && 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                MIN_ARGS( 3 );
560                add_on_server = 0;
561                cmd ++;
562        }
563       
564        if( !( a = account_get( irc, cmd[1] ) ) )
565        {
566                irc_usermsg( irc, "Invalid account" );
567                return;
568        }
569        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
570        {
571                irc_usermsg( irc, "That account is not on-line" );
572                return;
573        }
574       
575        if( cmd[3] )
576        {
577                if( !nick_ok( cmd[3] ) )
578                {
579                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
580                        return;
581                }
582                else if( user_find( irc, cmd[3] ) )
583                {
584                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
585                        return;
586                }
587                else
588                {
589                        nick_set( a, cmd[2], cmd[3] );
590                }
591        }
592       
593        if( add_on_server )
594                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
595        else
596                /* Yeah, officially this is a call-*back*... So if we just
597                   called add_buddy, we'll wait for the IM server to respond
598                   before we do this. */
599                imcb_add_buddy( a->ic, cmd[2], NULL );
600       
601        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
602}
603
604static void cmd_info( irc_t *irc, char **cmd )
605{
606        struct im_connection *ic;
607        account_t *a;
608       
609        if( !cmd[2] )
610        {
611                user_t *u = user_find( irc, cmd[1] );
612                if( !u || !u->ic )
613                {
614                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
615                        return;
616                }
617                ic = u->ic;
618                cmd[2] = u->handle;
619        }
620        else if( !( a = account_get( irc, cmd[1] ) ) )
621        {
622                irc_usermsg( irc, "Invalid account" );
623                return;
624        }
625        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
626        {
627                irc_usermsg( irc, "That account is not on-line" );
628                return;
629        }
630       
631        if( !ic->acc->prpl->get_info )
632        {
633                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
634        }
635        else
636        {
637                ic->acc->prpl->get_info( ic, cmd[2] );
638        }
639}
640
641static void cmd_rename( irc_t *irc, char **cmd )
642{
643        user_t *u;
644       
645        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
646        {
647                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
648        }
649        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
650        {
651                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
652        }
653        else if( !nick_ok( cmd[2] ) )
654        {
655                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
656        }
657        else if( !( u = user_find( irc, cmd[1] ) ) )
658        {
659                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
660        }
661        else
662        {
663                user_rename( irc, cmd[1], cmd[2] );
664                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
665                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
666                {
667                        g_free( irc->mynick );
668                        irc->mynick = g_strdup( cmd[2] );
669                       
670                        /* If we're called internally (user did "set root_nick"),
671                           let's not go O(INF). :-) */
672                        if( strcmp( cmd[0], "set_rename" ) != 0 )
673                                set_setstr( &irc->set, "root_nick", cmd[2] );
674                }
675                else if( u->send_handler == buddy_send_handler )
676                {
677                        nick_set( u->ic->acc, u->handle, cmd[2] );
678                }
679               
680                irc_usermsg( irc, "Nick successfully changed" );
681        }
682}
683
684char *set_eval_root_nick( set_t *set, char *new_nick )
685{
686        irc_t *irc = set->data;
687       
688        if( strcmp( irc->mynick, new_nick ) != 0 )
689        {
690                char *cmd[] = { "set_rename", irc->mynick, new_nick, NULL };
691               
692                cmd_rename( irc, cmd );
693        }
694       
695        return strcmp( irc->mynick, new_nick ) == 0 ? new_nick : SET_INVALID;
696}
697
698static void cmd_remove( irc_t *irc, char **cmd )
699{
700        user_t *u;
701        char *s;
702       
703        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
704        {
705                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
706                return;
707        }
708        s = g_strdup( u->handle );
709       
710        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
711        nick_del( u->ic->acc, u->handle );
712        user_del( irc, cmd[1] );
713       
714        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
715        g_free( s );
716       
717        return;
718}
719
720static void cmd_block( irc_t *irc, char **cmd )
721{
722        struct im_connection *ic;
723        account_t *a;
724       
725        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
726        {
727                char *format;
728                GSList *l;
729               
730                if( strchr( irc->umode, 'b' ) != NULL )
731                        format = "%s\t%s";
732                else
733                        format = "%-32.32s  %-16.16s";
734               
735                irc_usermsg( irc, format, "Handle", "Nickname" );
736                for( l = a->ic->deny; l; l = l->next )
737                {
738                        user_t *u = user_findhandle( a->ic, l->data );
739                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
740                }
741                irc_usermsg( irc, "End of list." );
742               
743                return;
744        }
745        else if( !cmd[2] )
746        {
747                user_t *u = user_find( irc, cmd[1] );
748                if( !u || !u->ic )
749                {
750                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
751                        return;
752                }
753                ic = u->ic;
754                cmd[2] = u->handle;
755        }
756        else if( !( a = account_get( irc, cmd[1] ) ) )
757        {
758                irc_usermsg( irc, "Invalid account" );
759                return;
760        }
761        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
762        {
763                irc_usermsg( irc, "That account is not on-line" );
764                return;
765        }
766       
767        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
768        {
769                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
770        }
771        else
772        {
773                imc_rem_allow( ic, cmd[2] );
774                imc_add_block( ic, cmd[2] );
775                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
776        }
777}
778
779static void cmd_allow( irc_t *irc, char **cmd )
780{
781        struct im_connection *ic;
782        account_t *a;
783       
784        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
785        {
786                char *format;
787                GSList *l;
788               
789                if( strchr( irc->umode, 'b' ) != NULL )
790                        format = "%s\t%s";
791                else
792                        format = "%-32.32s  %-16.16s";
793               
794                irc_usermsg( irc, format, "Handle", "Nickname" );
795                for( l = a->ic->permit; l; l = l->next )
796                {
797                        user_t *u = user_findhandle( a->ic, l->data );
798                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
799                }
800                irc_usermsg( irc, "End of list." );
801               
802                return;
803        }
804        else if( !cmd[2] )
805        {
806                user_t *u = user_find( irc, cmd[1] );
807                if( !u || !u->ic )
808                {
809                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
810                        return;
811                }
812                ic = u->ic;
813                cmd[2] = u->handle;
814        }
815        else if( !( a = account_get( irc, cmd[1] ) ) )
816        {
817                irc_usermsg( irc, "Invalid account" );
818                return;
819        }
820        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
821        {
822                irc_usermsg( irc, "That account is not on-line" );
823                return;
824        }
825       
826        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
827        {
828                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
829        }
830        else
831        {
832                imc_rem_block( ic, cmd[2] );
833                imc_add_allow( ic, cmd[2] );
834               
835                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
836        }
837}
838
839static void cmd_yesno( irc_t *irc, char **cmd )
840{
841        query_t *q = NULL;
842        int numq = 0;
843       
844        if( irc->queries == NULL )
845        {
846                irc_usermsg( irc, "Did I ask you something?" );
847                return;
848        }
849       
850        /* If there's an argument, the user seems to want to answer another question than the
851           first/last (depending on the query_order setting) one. */
852        if( cmd[1] )
853        {
854                if( sscanf( cmd[1], "%d", &numq ) != 1 )
855                {
856                        irc_usermsg( irc, "Invalid query number" );
857                        return;
858                }
859               
860                for( q = irc->queries; q; q = q->next, numq -- )
861                        if( numq == 0 )
862                                break;
863               
864                if( !q )
865                {
866                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
867                        return;
868                }
869        }
870       
871        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
872                query_answer( irc, q, 1 );
873        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
874                query_answer( irc, q, 0 );
875}
876
877static void cmd_set( irc_t *irc, char **cmd )
878{
879        cmd_set_real( irc, cmd, NULL, NULL );
880}
881
882static void cmd_save( irc_t *irc, char **cmd )
883{
884        if( ( irc->status & USTATUS_IDENTIFIED ) == 0 )
885                irc_usermsg( irc, "Please create an account first" );
886        else if( storage_save( irc, NULL, TRUE ) == STORAGE_OK )
887                irc_usermsg( irc, "Configuration saved" );
888        else
889                irc_usermsg( irc, "Configuration could not be saved!" );
890}
891
892static void cmd_blist( irc_t *irc, char **cmd )
893{
894        int online = 0, away = 0, offline = 0;
895        user_t *u;
896        char s[256];
897        char *format;
898        int n_online = 0, n_away = 0, n_offline = 0;
899       
900        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
901                online = offline = away = 1;
902        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
903                offline = 1;
904        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
905                away = 1;
906        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
907                online = 1;
908        else
909                online =  away = 1;
910       
911        if( strchr( irc->umode, 'b' ) != NULL )
912                format = "%s\t%s\t%s";
913        else
914                format = "%-16.16s  %-40.40s  %s";
915       
916        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
917       
918        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
919        {
920                if( online == 1 )
921                {
922                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
923                        irc_usermsg( irc, format, u->nick, s, "Online" );
924                }
925               
926                n_online ++;
927        }
928
929        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
930        {
931                if( away == 1 )
932                {
933                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
934                        irc_usermsg( irc, format, u->nick, s, u->away );
935                }
936                n_away ++;
937        }
938       
939        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
940        {
941                if( offline == 1 )
942                {
943                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
944                        irc_usermsg( irc, format, u->nick, s, "Offline" );
945                }
946                n_offline ++;
947        }
948       
949        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
950}
951
952static void cmd_nick( irc_t *irc, char **cmd ) 
953{
954        account_t *a;
955
956        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
957        {
958                irc_usermsg( irc, "Invalid account");
959        }
960        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
961        {
962                irc_usermsg( irc, "That account is not on-line" );
963        }
964        else if ( !cmd[2] ) 
965        {
966                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
967        }
968        else if ( !a->prpl->set_my_name ) 
969        {
970                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
971        }
972        else
973        {
974                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
975               
976                a->prpl->set_my_name( a->ic, cmd[2] );
977        }
978}
979
980static void cmd_qlist( irc_t *irc, char **cmd )
981{
982        query_t *q = irc->queries;
983        int num;
984       
985        if( !q )
986        {
987                irc_usermsg( irc, "There are no pending questions." );
988                return;
989        }
990       
991        irc_usermsg( irc, "Pending queries:" );
992       
993        for( num = 0; q; q = q->next, num ++ )
994                if( q->ic ) /* Not necessary yet, but it might come later */
995                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
996                else
997                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
998}
999
1000static void cmd_join_chat( irc_t *irc, char **cmd )
1001{
1002        irc_usermsg( irc, "This command is now obsolete. "
1003                          "Please try the `chat' command instead." );
1004}
1005
1006static set_t **cmd_chat_set_findhead( irc_t *irc, char *id )
1007{
1008        struct chat *c;
1009       
1010        if( ( c = chat_get( irc, id ) ) )
1011                return &c->set;
1012        else
1013                return NULL;
1014}
1015
1016static void cmd_chat( irc_t *irc, char **cmd )
1017{
1018        account_t *acc;
1019        struct chat *c;
1020       
1021        if( g_strcasecmp( cmd[1], "add" ) == 0 )
1022        {
1023                char *channel, *s;
1024               
1025                MIN_ARGS( 3 );
1026               
1027                if( !( acc = account_get( irc, cmd[2] ) ) )
1028                {
1029                        irc_usermsg( irc, "Invalid account" );
1030                        return;
1031                }
1032               
1033                if( cmd[4] == NULL )
1034                {
1035                        channel = g_strdup( cmd[3] );
1036                        if( ( s = strchr( channel, '@' ) ) )
1037                                *s = 0;
1038                }
1039                else
1040                {
1041                        channel = g_strdup( cmd[4] );
1042                }
1043               
1044                if( strchr( CTYPES, channel[0] ) == NULL )
1045                {
1046                        s = g_strdup_printf( "%c%s", CTYPES[0], channel );
1047                        g_free( channel );
1048                        channel = s;
1049                }
1050               
1051                if( ( c = chat_add( irc, acc, cmd[3], channel ) ) )
1052                        irc_usermsg( irc, "Chatroom added successfully." );
1053                else
1054                        irc_usermsg( irc, "Could not add chatroom." );
1055               
1056                g_free( channel );
1057        }
1058        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
1059        {
1060                int i = 0;
1061               
1062                if( strchr( irc->umode, 'b' ) )
1063                        irc_usermsg( irc, "Chatroom list:" );
1064               
1065                for( c = irc->chatrooms; c; c = c->next )
1066                {
1067                        irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name,
1068                                          c->acc->user, c->handle, c->channel );
1069                       
1070                        i ++;
1071                }
1072                irc_usermsg( irc, "End of chatroom list" );
1073        }
1074        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
1075        {
1076                MIN_ARGS( 2 );
1077               
1078                cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL );
1079        }
1080        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
1081        {
1082                MIN_ARGS( 2 );
1083               
1084                if( ( c = chat_get( irc, cmd[2] ) ) )
1085                {
1086                        chat_del( irc, c );
1087                }
1088                else
1089                {
1090                        irc_usermsg( irc, "Could not remove chat." );
1091                }
1092        }
1093        else if( g_strcasecmp( cmd[1], "with" ) == 0 )
1094        {
1095                user_t *u;
1096               
1097                MIN_ARGS( 2 );
1098               
1099                if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with )
1100                {
1101                        if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) )
1102                        {
1103                                irc_usermsg( irc, "(Possible) failure while trying to open "
1104                                                  "a groupchat with %s.", u->nick );
1105                        }
1106                }
1107                else
1108                {
1109                        irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] );
1110                }
1111        }
1112        else
1113        {
1114                irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] );
1115        }
1116
1117
1118
1119#if 0
1120        account_t *a;
1121        struct im_connection *ic;
1122        char *chat, *channel, *nick = NULL, *password = NULL;
1123        struct groupchat *c;
1124       
1125        if( !( a = account_get( irc, cmd[1] ) ) )
1126        {
1127                irc_usermsg( irc, "Invalid account" );
1128                return;
1129        }
1130        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
1131        {
1132                irc_usermsg( irc, "That account is not on-line" );
1133                return;
1134        }
1135        else if( a->prpl->chat_join == NULL )
1136        {
1137                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
1138                return;
1139        }
1140        ic = a->ic;
1141       
1142        chat = cmd[2];
1143        if( cmd[3] )
1144        {
1145                if( strchr( CTYPES, cmd[3][0] ) == NULL )
1146                        channel = g_strdup_printf( "&%s", cmd[3] );
1147                else
1148                        channel = g_strdup( cmd[3] );
1149        }
1150        else
1151        {
1152                char *s;
1153               
1154                channel = g_strdup_printf( "&%s", chat );
1155                if( ( s = strchr( channel, '@' ) ) )
1156                        *s = 0;
1157        }
1158        if( cmd[3] && cmd[4] )
1159                nick = cmd[4];
1160        else
1161                nick = irc->nick;
1162        if( cmd[3] && cmd[4] && cmd[5] )
1163                password = cmd[5];
1164       
1165        if( !nick_ok( channel + 1 ) )
1166        {
1167                irc_usermsg( irc, "Invalid channel name: %s", channel );
1168                g_free( channel );
1169                return;
1170        }
1171        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
1172        {
1173                irc_usermsg( irc, "Channel already exists: %s", channel );
1174                g_free( channel );
1175                return;
1176        }
1177       
1178        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
1179        {
1180                g_free( c->channel );
1181                c->channel = channel;
1182        }
1183        else
1184        {
1185                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
1186                g_free( channel );
1187        }
1188#endif
1189}
1190
1191const command_t commands[] = {
1192        { "help",           0, cmd_help,           0 }, 
1193        { "identify",       1, cmd_identify,       0 },
1194        { "register",       1, cmd_register,       0 },
1195        { "drop",           1, cmd_drop,           0 },
1196        { "account",        1, cmd_account,        0 },
1197        { "add",            2, cmd_add,            0 },
1198        { "info",           1, cmd_info,           0 },
1199        { "rename",         2, cmd_rename,         0 },
1200        { "remove",         1, cmd_remove,         0 },
1201        { "block",          1, cmd_block,          0 },
1202        { "allow",          1, cmd_allow,          0 },
1203        { "save",           0, cmd_save,           0 },
1204        { "set",            0, cmd_set,            0 },
1205        { "yes",            0, cmd_yesno,          0 },
1206        { "no",             0, cmd_yesno,          0 },
1207        { "blist",          0, cmd_blist,          0 },
1208        { "nick",           1, cmd_nick,           0 },
1209        { "qlist",          0, cmd_qlist,          0 },
1210        { "join_chat",      2, cmd_join_chat,      0 },
1211        { "chat",           1, cmd_chat,           0 },
1212        { NULL }
1213};
Note: See TracBrowser for help on using the repository browser.