source: root_commands.c @ 6738a67

Last change on this file since 6738a67 was 6738a67, checked in by Sven Moritz Hallberg <pesco@…>, at 2008-07-16T23:22:52Z

merge in latest trunk

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