source: root_commands.c @ 3c80a9d

Last change on this file since 3c80a9d was 764c7d1, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-03T21:30:03Z

OTR support, first checkin

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