source: root_commands.c @ dc9797f

Last change on this file since dc9797f was 522a00f, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-15T09:27:26Z

remove thread-based keygen
replace it with a process-based stub

  • Property mode set to 100644
File size: 22.6 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
207static void cmd_account( irc_t *irc, char **cmd )
208{
209        account_t *a;
210       
211        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
212        {
213                irc_usermsg( irc, "This server only accepts registered users" );
214                return;
215        }
216       
217        if( g_strcasecmp( cmd[1], "add" ) == 0 )
218        {
219                struct prpl *prpl;
220               
221                if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
222                {
223                        irc_usermsg( irc, "Not enough parameters" );
224                        return;
225                }
226               
227                prpl = find_protocol(cmd[2]);
228               
229                if( prpl == NULL )
230                {
231                        irc_usermsg( irc, "Unknown protocol" );
232                        return;
233                }
234
235                a = account_add( irc, prpl, cmd[3], cmd[4] );
236                if( cmd[5] )
237                {
238                        irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' "
239                                          "is now deprecated. Use `account set' instead." );
240                        set_setstr( &a->set, "server", cmd[5] );
241                }
242               
243                irc_usermsg( irc, "Account successfully added" );
244               
245                otr_check_for_key(a);
246        }
247        else if( g_strcasecmp( cmd[1], "del" ) == 0 )
248        {
249                if( !cmd[2] )
250                {
251                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
252                }
253                else if( !( a = account_get( irc, cmd[2] ) ) )
254                {
255                        irc_usermsg( irc, "Invalid account" );
256                }
257                else if( a->ic )
258                {
259                        irc_usermsg( irc, "Account is still logged in, can't delete" );
260                }
261                else
262                {
263                        account_del( irc, a );
264                        irc_usermsg( irc, "Account deleted" );
265                }
266        }
267        else if( g_strcasecmp( cmd[1], "list" ) == 0 )
268        {
269                int i = 0;
270               
271                if( strchr( irc->umode, 'b' ) )
272                        irc_usermsg( irc, "Account list:" );
273               
274                for( a = irc->accounts; a; a = a->next )
275                {
276                        char *con;
277                       
278                        if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) )
279                                con = " (connected)";
280                        else if( a->ic )
281                                con = " (connecting)";
282                        else if( a->reconnect )
283                                con = " (awaiting reconnect)";
284                        else
285                                con = "";
286                       
287                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
288                       
289                        i ++;
290                }
291                irc_usermsg( irc, "End of account list" );
292        }
293        else if( g_strcasecmp( cmd[1], "on" ) == 0 )
294        {
295                if( cmd[2] )
296                {
297                        if( ( a = account_get( irc, cmd[2] ) ) )
298                        {
299                                if( a->ic )
300                                {
301                                        irc_usermsg( irc, "Account already online" );
302                                        return;
303                                }
304                                else
305                                {
306                                        account_on( irc, a );
307                                }
308                        }
309                        else
310                        {
311                                irc_usermsg( irc, "Invalid account" );
312                                return;
313                        }
314                }
315                else
316                {
317                        if ( irc->accounts ) {
318                                irc_usermsg( irc, "Trying to get all accounts connected..." );
319                       
320                                for( a = irc->accounts; a; a = a->next )
321                                        if( !a->ic && a->auto_connect )
322                                                account_on( irc, a );
323                        } 
324                        else
325                        {
326                                irc_usermsg( irc, "No accounts known. Use `account add' to add one." );
327                        }
328                }
329        }
330        else if( g_strcasecmp( cmd[1], "off" ) == 0 )
331        {
332                if( !cmd[2] )
333                {
334                        irc_usermsg( irc, "Deactivating all active (re)connections..." );
335                       
336                        for( a = irc->accounts; a; a = a->next )
337                        {
338                                if( a->ic )
339                                        account_off( irc, a );
340                                else if( a->reconnect )
341                                        cancel_auto_reconnect( a );
342                        }
343                }
344                else if( ( a = account_get( irc, cmd[2] ) ) )
345                {
346                        if( a->ic )
347                        {
348                                account_off( irc, a );
349                        }
350                        else if( a->reconnect )
351                        {
352                                cancel_auto_reconnect( a );
353                                irc_usermsg( irc, "Reconnect cancelled" );
354                        }
355                        else
356                        {
357                                irc_usermsg( irc, "Account already offline" );
358                                return;
359                        }
360                }
361                else
362                {
363                        irc_usermsg( irc, "Invalid account" );
364                        return;
365                }
366        }
367        else if( g_strcasecmp( cmd[1], "set" ) == 0 )
368        {
369                char *acc_handle, *set_name = NULL, *tmp;
370               
371                if( !cmd[2] )
372                {
373                        irc_usermsg( irc, "Not enough parameters given (need %d)", 2 );
374                        return;
375                }
376               
377                if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
378                        acc_handle = g_strdup( cmd[3] );
379                else
380                        acc_handle = g_strdup( cmd[2] );
381               
382                if( ( tmp = strchr( acc_handle, '/' ) ) )
383                {
384                        *tmp = 0;
385                        set_name = tmp + 1;
386                }
387               
388                if( ( a = account_get( irc, acc_handle ) ) == NULL )
389                {
390                        g_free( acc_handle );
391                        irc_usermsg( irc, "Invalid account" );
392                        return;
393                }
394               
395                if( cmd[3] && set_name )
396                {
397                        set_t *s = set_find( &a->set, set_name );
398                       
399                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
400                        {
401                                g_free( acc_handle );
402                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
403                                return;
404                        }
405                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
406                        {
407                                g_free( acc_handle );
408                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
409                                return;
410                        }
411                       
412                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
413                                set_reset( &a->set, set_name );
414                        else
415                                set_setstr( &a->set, set_name, cmd[3] );
416                }
417                if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
418                {
419                        char *s = set_getstr( &a->set, set_name );
420                        if( s )
421                                irc_usermsg( irc, "%s = `%s'", set_name, s );
422                        else
423                                irc_usermsg( irc, "%s is empty", set_name );
424                }
425                else
426                {
427                        set_t *s = a->set;
428                        while( s )
429                        {
430                                if( s->value || s->def )
431                                        irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
432                                else
433                                        irc_usermsg( irc, "%s is empty", s->key );
434                                s = s->next;
435                        }
436                }
437               
438                g_free( acc_handle );
439        }
440        else
441        {
442                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
443        }
444}
445
446static void cmd_add( irc_t *irc, char **cmd )
447{
448        account_t *a;
449        int add_on_server = 1;
450       
451        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
452        {
453                add_on_server = 0;
454                cmd ++;         /* So evil... :-D */
455        }
456       
457        if( !( a = account_get( irc, cmd[1] ) ) )
458        {
459                irc_usermsg( irc, "Invalid account" );
460                return;
461        }
462        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
463        {
464                irc_usermsg( irc, "That account is not on-line" );
465                return;
466        }
467       
468        if( cmd[3] )
469        {
470                if( !nick_ok( cmd[3] ) )
471                {
472                        irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] );
473                        return;
474                }
475                else if( user_find( irc, cmd[3] ) )
476                {
477                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
478                        return;
479                }
480                else
481                {
482                        nick_set( a, cmd[2], cmd[3] );
483                }
484        }
485       
486        /* By making this optional, you can talk to people without having to
487           add them to your *real* (server-side) contact list. */
488        if( add_on_server )
489                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
490       
491        /* add_buddy( a->ic, NULL, cmd[2], cmd[2] ); */
492       
493        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
494}
495
496static void cmd_info( irc_t *irc, char **cmd )
497{
498        struct im_connection *ic;
499        account_t *a;
500       
501        if( !cmd[2] )
502        {
503                user_t *u = user_find( irc, cmd[1] );
504                if( !u || !u->ic )
505                {
506                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
507                        return;
508                }
509                ic = u->ic;
510                cmd[2] = u->handle;
511        }
512        else if( !( a = account_get( irc, cmd[1] ) ) )
513        {
514                irc_usermsg( irc, "Invalid account" );
515                return;
516        }
517        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
518        {
519                irc_usermsg( irc, "That account is not on-line" );
520                return;
521        }
522       
523        if( !ic->acc->prpl->get_info )
524        {
525                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
526        }
527        else
528        {
529                ic->acc->prpl->get_info( ic, cmd[2] );
530        }
531}
532
533static void cmd_rename( irc_t *irc, char **cmd )
534{
535        user_t *u;
536       
537        if( g_strcasecmp( cmd[1], irc->nick ) == 0 )
538        {
539                irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] );
540        }
541        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
542        {
543                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
544        }
545        else if( !nick_ok( cmd[2] ) )
546        {
547                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
548        }
549        else if( !( u = user_find( irc, cmd[1] ) ) )
550        {
551                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
552        }
553        else
554        {
555                user_rename( irc, cmd[1], cmd[2] );
556                irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] );
557                if( g_strcasecmp( cmd[1], irc->mynick ) == 0 )
558                {
559                        g_free( irc->mynick );
560                        irc->mynick = g_strdup( cmd[2] );
561                }
562                else if( u->send_handler == buddy_send_handler )
563                {
564                        nick_set( u->ic->acc, u->handle, cmd[2] );
565                }
566               
567                irc_usermsg( irc, "Nick successfully changed" );
568        }
569}
570
571static void cmd_remove( irc_t *irc, char **cmd )
572{
573        user_t *u;
574        char *s;
575       
576        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
577        {
578                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
579                return;
580        }
581        s = g_strdup( u->handle );
582       
583        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
584        nick_del( u->ic->acc, u->handle );
585        user_del( irc, cmd[1] );
586       
587        irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] );
588        g_free( s );
589       
590        return;
591}
592
593static void cmd_block( irc_t *irc, char **cmd )
594{
595        struct im_connection *ic;
596        account_t *a;
597       
598        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
599        {
600                char *format;
601                GSList *l;
602               
603                if( strchr( irc->umode, 'b' ) != NULL )
604                        format = "%s\t%s";
605                else
606                        format = "%-32.32s  %-16.16s";
607               
608                irc_usermsg( irc, format, "Handle", "Nickname" );
609                for( l = a->ic->deny; l; l = l->next )
610                {
611                        user_t *u = user_findhandle( a->ic, l->data );
612                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
613                }
614                irc_usermsg( irc, "End of list." );
615               
616                return;
617        }
618        else if( !cmd[2] )
619        {
620                user_t *u = user_find( irc, cmd[1] );
621                if( !u || !u->ic )
622                {
623                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
624                        return;
625                }
626                ic = u->ic;
627                cmd[2] = u->handle;
628        }
629        else if( !( a = account_get( irc, cmd[1] ) ) )
630        {
631                irc_usermsg( irc, "Invalid account" );
632                return;
633        }
634        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
635        {
636                irc_usermsg( irc, "That account is not on-line" );
637                return;
638        }
639       
640        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
641        {
642                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
643        }
644        else
645        {
646                imc_rem_allow( ic, cmd[2] );
647                imc_add_block( ic, cmd[2] );
648                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
649        }
650}
651
652static void cmd_allow( irc_t *irc, char **cmd )
653{
654        struct im_connection *ic;
655        account_t *a;
656       
657        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
658        {
659                char *format;
660                GSList *l;
661               
662                if( strchr( irc->umode, 'b' ) != NULL )
663                        format = "%s\t%s";
664                else
665                        format = "%-32.32s  %-16.16s";
666               
667                irc_usermsg( irc, format, "Handle", "Nickname" );
668                for( l = a->ic->permit; l; l = l->next )
669                {
670                        user_t *u = user_findhandle( a->ic, l->data );
671                        irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" );
672                }
673                irc_usermsg( irc, "End of list." );
674               
675                return;
676        }
677        else if( !cmd[2] )
678        {
679                user_t *u = user_find( irc, cmd[1] );
680                if( !u || !u->ic )
681                {
682                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
683                        return;
684                }
685                ic = u->ic;
686                cmd[2] = u->handle;
687        }
688        else if( !( a = account_get( irc, cmd[1] ) ) )
689        {
690                irc_usermsg( irc, "Invalid account" );
691                return;
692        }
693        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
694        {
695                irc_usermsg( irc, "That account is not on-line" );
696                return;
697        }
698       
699        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
700        {
701                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
702        }
703        else
704        {
705                imc_rem_block( ic, cmd[2] );
706                imc_add_allow( ic, cmd[2] );
707               
708                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
709        }
710}
711
712static void cmd_yesno( irc_t *irc, char **cmd )
713{
714        query_t *q = NULL;
715        int numq = 0;
716       
717        if( irc->queries == NULL )
718        {
719                irc_usermsg( irc, "Did I ask you something?" );
720                return;
721        }
722       
723        /* If there's an argument, the user seems to want to answer another question than the
724           first/last (depending on the query_order setting) one. */
725        if( cmd[1] )
726        {
727                if( sscanf( cmd[1], "%d", &numq ) != 1 )
728                {
729                        irc_usermsg( irc, "Invalid query number" );
730                        return;
731                }
732               
733                for( q = irc->queries; q; q = q->next, numq -- )
734                        if( numq == 0 )
735                                break;
736               
737                if( !q )
738                {
739                        irc_usermsg( irc, "Uhm, I never asked you something like that..." );
740                        return;
741                }
742        }
743       
744        if( g_strcasecmp( cmd[0], "yes" ) == 0 )
745                query_answer( irc, q, 1 );
746        else if( g_strcasecmp( cmd[0], "no" ) == 0 )
747                query_answer( irc, q, 0 );
748}
749
750static void cmd_set( irc_t *irc, char **cmd )
751{
752        char *set_name = cmd[1];
753       
754        if( cmd[1] && cmd[2] )
755        {
756                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
757                {
758                        set_reset( &irc->set, cmd[2] );
759                        set_name = cmd[2];
760                }
761                else
762                {
763                        set_setstr( &irc->set, cmd[1], cmd[2] );
764                }
765        }
766        if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
767        {
768                char *s = set_getstr( &irc->set, set_name );
769                if( s )
770                        irc_usermsg( irc, "%s = `%s'", set_name, s );
771                else
772                        irc_usermsg( irc, "%s is empty", set_name );
773        }
774        else
775        {
776                set_t *s = irc->set;
777                while( s )
778                {
779                        if( s->value || s->def )
780                                irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
781                        else
782                                irc_usermsg( irc, "%s is empty", s->key );
783                        s = s->next;
784                }
785        }
786}
787
788static void cmd_save( irc_t *irc, char **cmd )
789{
790        if( storage_save( irc, TRUE ) == STORAGE_OK )
791                irc_usermsg( irc, "Configuration saved" );
792        else
793                irc_usermsg( irc, "Configuration could not be saved!" );
794}
795
796static void cmd_blist( irc_t *irc, char **cmd )
797{
798        int online = 0, away = 0, offline = 0;
799        user_t *u;
800        char s[256];
801        char *format;
802        int n_online = 0, n_away = 0, n_offline = 0;
803       
804        if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 )
805                online = offline = away = 1;
806        else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 )
807                offline = 1;
808        else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 )
809                away = 1;
810        else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 )
811                online = 1;
812        else
813                online =  away = 1;
814       
815        if( strchr( irc->umode, 'b' ) != NULL )
816                format = "%s\t%s\t%s";
817        else
818                format = "%-16.16s  %-40.40s  %s";
819       
820        irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" );
821       
822        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
823        {
824                if( online == 1 )
825                {
826                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
827                        irc_usermsg( irc, format, u->nick, s, "Online" );
828                }
829               
830                n_online ++;
831        }
832
833        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
834        {
835                if( away == 1 )
836                {
837                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
838                        irc_usermsg( irc, format, u->nick, s, u->away );
839                }
840                n_away ++;
841        }
842       
843        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
844        {
845                if( offline == 1 )
846                {
847                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
848                        irc_usermsg( irc, format, u->nick, s, "Offline" );
849                }
850                n_offline ++;
851        }
852       
853        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
854}
855
856static void cmd_nick( irc_t *irc, char **cmd ) 
857{
858        account_t *a;
859
860        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
861        {
862                irc_usermsg( irc, "Invalid account");
863        }
864        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
865        {
866                irc_usermsg( irc, "That account is not on-line" );
867        }
868        else if ( !cmd[2] ) 
869        {
870                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
871        }
872        else if ( !a->prpl->set_my_name ) 
873        {
874                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
875        }
876        else
877        {
878                irc_usermsg( irc, "Setting your name to `%s'", cmd[2] );
879               
880                a->prpl->set_my_name( a->ic, cmd[2] );
881        }
882}
883
884static void cmd_qlist( irc_t *irc, char **cmd )
885{
886        query_t *q = irc->queries;
887        int num;
888       
889        if( !q )
890        {
891                irc_usermsg( irc, "There are no pending questions." );
892                return;
893        }
894       
895        irc_usermsg( irc, "Pending queries:" );
896       
897        for( num = 0; q; q = q->next, num ++ )
898                if( q->ic ) /* Not necessary yet, but it might come later */
899                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
900                else
901                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
902}
903
904static void cmd_join_chat( irc_t *irc, char **cmd )
905{
906        account_t *a;
907        struct im_connection *ic;
908        char *chat, *channel, *nick = NULL, *password = NULL;
909        struct groupchat *c;
910       
911        if( !( a = account_get( irc, cmd[1] ) ) )
912        {
913                irc_usermsg( irc, "Invalid account" );
914                return;
915        }
916        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
917        {
918                irc_usermsg( irc, "That account is not on-line" );
919                return;
920        }
921        else if( a->prpl->chat_join == NULL )
922        {
923                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
924                return;
925        }
926        ic = a->ic;
927       
928        chat = cmd[2];
929        if( cmd[3] )
930        {
931                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
932                        channel = g_strdup_printf( "&%s", cmd[3] );
933                else
934                        channel = g_strdup( cmd[3] );
935        }
936        else
937        {
938                char *s;
939               
940                channel = g_strdup_printf( "&%s", chat );
941                if( ( s = strchr( channel, '@' ) ) )
942                        *s = 0;
943        }
944        if( cmd[3] && cmd[4] )
945                nick = cmd[4];
946        else
947                nick = irc->nick;
948        if( cmd[3] && cmd[4] && cmd[5] )
949                password = cmd[5];
950       
951        if( !nick_ok( channel + 1 ) )
952        {
953                irc_usermsg( irc, "Invalid channel name: %s", channel );
954                g_free( channel );
955                return;
956        }
957        else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) )
958        {
959                irc_usermsg( irc, "Channel already exists: %s", channel );
960                g_free( channel );
961                return;
962        }
963       
964        if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) )
965        {
966                g_free( c->channel );
967                c->channel = channel;
968        }
969        else
970        {
971                irc_usermsg( irc, "Tried to join chat, not sure if this was successful" );
972                g_free( channel );
973        }
974}
975
976const command_t commands[] = {
977        { "help",           0, cmd_help,           0 }, 
978        { "identify",       1, cmd_identify,       0 },
979        { "register",       1, cmd_register,       0 },
980        { "drop",           1, cmd_drop,           0 },
981        { "account",        1, cmd_account,        0 },
982        { "add",            2, cmd_add,            0 },
983        { "info",           1, cmd_info,           0 },
984        { "rename",         2, cmd_rename,         0 },
985        { "remove",         1, cmd_remove,         0 },
986        { "block",          1, cmd_block,          0 },
987        { "allow",          1, cmd_allow,          0 },
988        { "save",           0, cmd_save,           0 },
989        { "set",            0, cmd_set,            0 },
990        { "yes",            0, cmd_yesno,          0 },
991        { "no",             0, cmd_yesno,          0 },
992        { "blist",          0, cmd_blist,          0 },
993        { "nick",           1, cmd_nick,           0 },
994        { "qlist",          0, cmd_qlist,          0 },
995        { "join_chat",      2, cmd_join_chat,      0 },
996        { "otr",            1, cmd_otr,            0 }, 
997        { NULL }
998};
Note: See TracBrowser for help on using the repository browser.