source: root_commands.c @ c595308

Last change on this file since c595308 was c595308, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-11T21:20:35Z
  • use a recursive otr_mutex
  • implement 'otr forget fingerprint' and 'otr forget context' commands
  • Property mode set to 100644
File size: 22.8 KB
RevLine 
[b7d3cc34]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"
[764c7d1]31#include "otr.h"
[b7d3cc34]32
33#include <string.h>
34
[f73b969]35void root_command_string( irc_t *irc, user_t *u, char *command, int flags )
[7e563ed]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                        }
[619a681]60                        else
61                        {
62                                break;
63                        }
[7e563ed]64                }
[79c6f9f]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                }
[7e563ed]72                else if( *s == q )
73                {
74                        q = *s = 0;
75                }
76        cmd[k] = NULL;
77       
[f73b969]78        root_command( irc, cmd );
[7e563ed]79}
80
[f73b969]81void root_command( irc_t *irc, char *cmd[] )
[7e563ed]82{       
83        int i;
84       
85        if( !cmd[0] )
[f73b969]86                return;
[7e563ed]87       
[c595308]88        if(!g_static_rec_mutex_trylock(&irc->otr_mutex)) {
[764c7d1]89                irc_usermsg(irc, "keygen in progress, bitlbee comatose - please wait");
90                return;
91        }
92       
[7e563ed]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 );
[c595308]99                                g_static_rec_mutex_unlock(&irc->otr_mutex);
[f73b969]100                                return;
[7e563ed]101                        }
102                        commands[i].execute( irc, cmd );
[c595308]103                        g_static_rec_mutex_unlock(&irc->otr_mutex);
[f73b969]104                        return;
[7e563ed]105                }
106       
107        irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] );
[c595308]108        g_static_rec_mutex_unlock(&irc->otr_mutex);
[7e563ed]109}
110
[f73b969]111static void cmd_help( irc_t *irc, char **cmd )
[b7d3cc34]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
[5bf5edf]138void cmd_account( irc_t *irc, char **cmd );
[90bbb0e]139
[f73b969]140static void cmd_identify( irc_t *irc, char **cmd )
[b7d3cc34]141{
[ab49fdc]142        storage_status_t status = storage_load( irc->nick, cmd[1], irc );
[90bbb0e]143        char *account_on[] = { "account", "on", NULL };
[b7d3cc34]144       
[09adf08]145        switch (status) {
146        case STORAGE_INVALID_PASSWORD:
[b7d3cc34]147                irc_usermsg( irc, "Incorrect password" );
[09adf08]148                break;
149        case STORAGE_NO_SUCH_USER:
[b7d3cc34]150                irc_usermsg( irc, "The nick is (probably) not registered" );
[09adf08]151                break;
152        case STORAGE_OK:
[6ee9d2d]153                irc_usermsg( irc, "Password accepted, settings and accounts loaded" );
[238f828]154                irc_umode_set( irc, "+R", 1 );
[d5ccd83]155                if( set_getbool( &irc->set, "auto_connect" ) )
[90bbb0e]156                        cmd_account( irc, account_on );
[09adf08]157                break;
[c121f89]158        case STORAGE_OTHER_ERROR:
[09adf08]159        default:
[c121f89]160                irc_usermsg( irc, "Unknown error while loading configuration" );
[09adf08]161                break;
[b7d3cc34]162        }
163}
164
[f73b969]165static void cmd_register( irc_t *irc, char **cmd )
[b7d3cc34]166{
167        if( global.conf->authmode == AUTHMODE_REGISTERED )
168        {
169                irc_usermsg( irc, "This server does not allow registering new accounts" );
[f73b969]170                return;
[b7d3cc34]171        }
[1ee6c18]172
[7cad7b4]173        irc_setpass( irc, cmd[1] );
[ab49fdc]174        switch( storage_save( irc, FALSE )) {
[a1f17d4]175                case STORAGE_ALREADY_EXISTS:
176                        irc_usermsg( irc, "Nick is already registered" );
177                        break;
178                       
179                case STORAGE_OK:
[0383943]180                        irc_usermsg( irc, "Account successfully created" );
[79e826a]181                        irc->status |= USTATUS_IDENTIFIED;
[238f828]182                        irc_umode_set( irc, "+R", 1 );
[a1f17d4]183                        break;
184
185                default:
186                        irc_usermsg( irc, "Error registering" );
187                        break;
[b7d3cc34]188        }
189}
190
[f73b969]191static void cmd_drop( irc_t *irc, char **cmd )
[b7d3cc34]192{
[a1f17d4]193        storage_status_t status;
194       
[ab49fdc]195        status = storage_remove (irc->nick, cmd[1]);
[a1f17d4]196        switch (status) {
197        case STORAGE_NO_SUCH_USER:
[b7d3cc34]198                irc_usermsg( irc, "That account does not exist" );
[f73b969]199                break;
[a1f17d4]200        case STORAGE_INVALID_PASSWORD:
[1ee6c18]201                irc_usermsg( irc, "Password invalid" );
[f73b969]202                break;
[a1f17d4]203        case STORAGE_OK:
[7cad7b4]204                irc_setpass( irc, NULL );
[79e826a]205                irc->status &= ~USTATUS_IDENTIFIED;
[238f828]206                irc_umode_set( irc, "-R", 1 );
[a1f17d4]207                irc_usermsg( irc, "Account `%s' removed", irc->nick );
[f73b969]208                break;
[a1f17d4]209        default:
[30ce1ce]210                irc_usermsg( irc, "Error: `%d'", status );
[f73b969]211                break;
[b7d3cc34]212        }
213}
214
[5bf5edf]215void cmd_account( irc_t *irc, char **cmd )
[b7d3cc34]216{
217        account_t *a;
218       
[3af70b0]219        if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) )
[b7d3cc34]220        {
221                irc_usermsg( irc, "This server only accepts registered users" );
[f73b969]222                return;
[b7d3cc34]223        }
224       
225        if( g_strcasecmp( cmd[1], "add" ) == 0 )
226        {
[7b23afd]227                struct prpl *prpl;
[b7d3cc34]228               
229                if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL )
230                {
231                        irc_usermsg( irc, "Not enough parameters" );
[f73b969]232                        return;
[b7d3cc34]233                }
234               
[7b23afd]235                prpl = find_protocol(cmd[2]);
[b7d3cc34]236               
[7b23afd]237                if( prpl == NULL )
[b7d3cc34]238                {
239                        irc_usermsg( irc, "Unknown protocol" );
[f73b969]240                        return;
[b7d3cc34]241                }
242
[7b23afd]243                a = account_add( irc, prpl, cmd[3], cmd[4] );
[b7d3cc34]244                if( cmd[5] )
[30ce1ce]245                {
246                        irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' "
247                                          "is now deprecated. Use `account set' instead." );
[5100caa]248                        set_setstr( &a->set, "server", cmd[5] );
[30ce1ce]249                }
[b7d3cc34]250               
251                irc_usermsg( irc, "Account successfully added" );
[764c7d1]252               
253                otr_check_for_key(a);
[b7d3cc34]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                }
[0da65d5]265                else if( a->ic )
[b7d3cc34]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               
[e6e1f18]279                if( strchr( irc->umode, 'b' ) )
280                        irc_usermsg( irc, "Account list:" );
281               
[b7d3cc34]282                for( a = irc->accounts; a; a = a->next )
283                {
284                        char *con;
285                       
[0da65d5]286                        if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) )
[b7d3cc34]287                                con = " (connected)";
[0da65d5]288                        else if( a->ic )
[b7d3cc34]289                                con = " (connecting)";
290                        else if( a->reconnect )
291                                con = " (awaiting reconnect)";
292                        else
293                                con = "";
294                       
[7b23afd]295                        irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con );
[b7d3cc34]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                        {
[0da65d5]307                                if( a->ic )
[b7d3cc34]308                                {
309                                        irc_usermsg( irc, "Account already online" );
[f73b969]310                                        return;
[b7d3cc34]311                                }
312                                else
313                                {
314                                        account_on( irc, a );
315                                }
316                        }
317                        else
318                        {
319                                irc_usermsg( irc, "Invalid account" );
[f73b969]320                                return;
[b7d3cc34]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 )
[0da65d5]329                                        if( !a->ic && a->auto_connect )
[b7d3cc34]330                                                account_on( irc, a );
331                        } 
332                        else
333                        {
[30ce1ce]334                                irc_usermsg( irc, "No accounts known. Use `account add' to add one." );
[b7d3cc34]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                        {
[0da65d5]346                                if( a->ic )
[b7d3cc34]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                {
[0da65d5]354                        if( a->ic )
[b7d3cc34]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" );
[f73b969]366                                return;
[b7d3cc34]367                        }
368                }
369                else
370                {
371                        irc_usermsg( irc, "Invalid account" );
[f73b969]372                        return;
[b7d3cc34]373                }
374        }
[5100caa]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               
[cd428e4]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               
[5100caa]390                if( ( tmp = strchr( acc_handle, '/' ) ) )
391                {
392                        *tmp = 0;
393                        set_name = tmp + 1;
394                }
395               
[cd428e4]396                if( ( a = account_get( irc, acc_handle ) ) == NULL )
[5100caa]397                {
[75a4b85]398                        g_free( acc_handle );
[5100caa]399                        irc_usermsg( irc, "Invalid account" );
400                        return;
401                }
402               
[9334cc2]403                if( cmd[3] && set_name )
[5100caa]404                {
405                        set_t *s = set_find( &a->set, set_name );
406                       
[0da65d5]407                        if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY )
[5100caa]408                        {
[75a4b85]409                                g_free( acc_handle );
[911f2eb]410                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" );
411                                return;
412                        }
[0da65d5]413                        else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY )
[911f2eb]414                        {
[75a4b85]415                                g_free( acc_handle );
[911f2eb]416                                irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" );
[5100caa]417                                return;
418                        }
419                       
[71dc854]420                        if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 )
[cd428e4]421                                set_reset( &a->set, set_name );
422                        else
423                                set_setstr( &a->set, set_name, cmd[3] );
[5100caa]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 )
[cd428e4]439                                        irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]440                                else
441                                        irc_usermsg( irc, "%s is empty", s->key );
442                                s = s->next;
443                        }
444                }
445               
446                g_free( acc_handle );
447        }
[b7d3cc34]448        else
449        {
[5c09a59]450                irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] );
[b7d3cc34]451        }
452}
453
[f73b969]454static void cmd_add( irc_t *irc, char **cmd )
[b7d3cc34]455{
456        account_t *a;
[f0cb961]457        int add_on_server = 1;
[f8de26f]458       
459        if( g_strcasecmp( cmd[1], "-tmp" ) == 0 )
460        {
[f0cb961]461                add_on_server = 0;
[f8de26f]462                cmd ++;         /* So evil... :-D */
463        }
[b7d3cc34]464       
465        if( !( a = account_get( irc, cmd[1] ) ) )
466        {
467                irc_usermsg( irc, "Invalid account" );
[f73b969]468                return;
[b7d3cc34]469        }
[0da65d5]470        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]471        {
472                irc_usermsg( irc, "That account is not on-line" );
[f73b969]473                return;
[b7d3cc34]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] );
[f73b969]481                        return;
[b7d3cc34]482                }
483                else if( user_find( irc, cmd[3] ) )
484                {
485                        irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] );
[f73b969]486                        return;
[b7d3cc34]487                }
488                else
489                {
[5b52a48]490                        nick_set( a, cmd[2], cmd[3] );
[b7d3cc34]491                }
492        }
[f8de26f]493       
494        /* By making this optional, you can talk to people without having to
495           add them to your *real* (server-side) contact list. */
[f0cb961]496        if( add_on_server )
[0da65d5]497                a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL );
[b7d3cc34]498       
[f0cb961]499        /* add_buddy( a->ic, NULL, cmd[2], cmd[2] ); */
500       
501        irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2]  );
[b7d3cc34]502}
503
[f73b969]504static void cmd_info( irc_t *irc, char **cmd )
[b7d3cc34]505{
[0da65d5]506        struct im_connection *ic;
[b7d3cc34]507        account_t *a;
508       
509        if( !cmd[2] )
510        {
511                user_t *u = user_find( irc, cmd[1] );
[0da65d5]512                if( !u || !u->ic )
[b7d3cc34]513                {
514                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]515                        return;
[b7d3cc34]516                }
[0da65d5]517                ic = u->ic;
[b7d3cc34]518                cmd[2] = u->handle;
519        }
520        else if( !( a = account_get( irc, cmd[1] ) ) )
521        {
522                irc_usermsg( irc, "Invalid account" );
[f73b969]523                return;
[b7d3cc34]524        }
[0da65d5]525        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]526        {
527                irc_usermsg( irc, "That account is not on-line" );
[f73b969]528                return;
[b7d3cc34]529        }
530       
[0da65d5]531        if( !ic->acc->prpl->get_info )
[b7d3cc34]532        {
533                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
534        }
[f73b969]535        else
536        {
[0da65d5]537                ic->acc->prpl->get_info( ic, cmd[2] );
[f73b969]538        }
[b7d3cc34]539}
540
[f73b969]541static void cmd_rename( irc_t *irc, char **cmd )
[b7d3cc34]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        }
[f73b969]549        else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) )
[b7d3cc34]550        {
551                irc_usermsg( irc, "Nick `%s' already exists", cmd[2] );
552        }
[f73b969]553        else if( !nick_ok( cmd[2] ) )
[b7d3cc34]554        {
555                irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] );
556        }
[f73b969]557        else if( !( u = user_find( irc, cmd[1] ) ) )
[b7d3cc34]558        {
559                irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
560        }
[f73b969]561        else
[b7d3cc34]562        {
[f73b969]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                {
[0da65d5]572                        nick_set( u->ic->acc, u->handle, cmd[2] );
[f73b969]573                }
574               
575                irc_usermsg( irc, "Nick successfully changed" );
[b7d3cc34]576        }
577}
578
[f73b969]579static void cmd_remove( irc_t *irc, char **cmd )
[b7d3cc34]580{
581        user_t *u;
582        char *s;
583       
[0da65d5]584        if( !( u = user_find( irc, cmd[1] ) ) || !u->ic )
[b7d3cc34]585        {
586                irc_usermsg( irc, "Buddy `%s' not found", cmd[1] );
[f73b969]587                return;
[b7d3cc34]588        }
589        s = g_strdup( u->handle );
590       
[0da65d5]591        u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL );
592        nick_del( u->ic->acc, u->handle );
[b7d3cc34]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       
[f73b969]598        return;
[b7d3cc34]599}
600
[f73b969]601static void cmd_block( irc_t *irc, char **cmd )
[b7d3cc34]602{
[0da65d5]603        struct im_connection *ic;
[b7d3cc34]604        account_t *a;
605       
[0da65d5]606        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]607        {
608                char *format;
609                GSList *l;
610               
611                if( strchr( irc->umode, 'b' ) != NULL )
612                        format = "%s\t%s";
613                else
[57ef864]614                        format = "%-32.32s  %-16.16s";
[87b6a3e]615               
616                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]617                for( l = a->ic->deny; l; l = l->next )
[87b6a3e]618                {
[0da65d5]619                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]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] )
[b7d3cc34]627        {
628                user_t *u = user_find( irc, cmd[1] );
[0da65d5]629                if( !u || !u->ic )
[b7d3cc34]630                {
631                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]632                        return;
[b7d3cc34]633                }
[0da65d5]634                ic = u->ic;
[b7d3cc34]635                cmd[2] = u->handle;
636        }
637        else if( !( a = account_get( irc, cmd[1] ) ) )
638        {
639                irc_usermsg( irc, "Invalid account" );
[f73b969]640                return;
[b7d3cc34]641        }
[0da65d5]642        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]643        {
644                irc_usermsg( irc, "That account is not on-line" );
[f73b969]645                return;
[b7d3cc34]646        }
647       
[0da65d5]648        if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit )
[b7d3cc34]649        {
650                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
651        }
652        else
653        {
[84b045d]654                imc_rem_allow( ic, cmd[2] );
655                imc_add_block( ic, cmd[2] );
[da3b536]656                irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] );
[b7d3cc34]657        }
658}
659
[f73b969]660static void cmd_allow( irc_t *irc, char **cmd )
[b7d3cc34]661{
[0da65d5]662        struct im_connection *ic;
[b7d3cc34]663        account_t *a;
664       
[0da65d5]665        if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic )
[87b6a3e]666        {
667                char *format;
668                GSList *l;
669               
670                if( strchr( irc->umode, 'b' ) != NULL )
671                        format = "%s\t%s";
672                else
[57ef864]673                        format = "%-32.32s  %-16.16s";
[87b6a3e]674               
675                irc_usermsg( irc, format, "Handle", "Nickname" );
[0da65d5]676                for( l = a->ic->permit; l; l = l->next )
[87b6a3e]677                {
[0da65d5]678                        user_t *u = user_findhandle( a->ic, l->data );
[87b6a3e]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] )
[b7d3cc34]686        {
687                user_t *u = user_find( irc, cmd[1] );
[0da65d5]688                if( !u || !u->ic )
[b7d3cc34]689                {
690                        irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] );
[f73b969]691                        return;
[b7d3cc34]692                }
[0da65d5]693                ic = u->ic;
[b7d3cc34]694                cmd[2] = u->handle;
695        }
696        else if( !( a = account_get( irc, cmd[1] ) ) )
697        {
698                irc_usermsg( irc, "Invalid account" );
[f73b969]699                return;
[b7d3cc34]700        }
[0da65d5]701        else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]702        {
703                irc_usermsg( irc, "That account is not on-line" );
[f73b969]704                return;
[b7d3cc34]705        }
706       
[0da65d5]707        if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit )
[b7d3cc34]708        {
709                irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] );
710        }
711        else
712        {
[84b045d]713                imc_rem_block( ic, cmd[2] );
714                imc_add_allow( ic, cmd[2] );
[b7d3cc34]715               
[da3b536]716                irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] );
[b7d3cc34]717        }
718}
719
[f73b969]720static void cmd_yesno( irc_t *irc, char **cmd )
[b7d3cc34]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?" );
[f73b969]728                return;
[b7d3cc34]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" );
[f73b969]738                        return;
[b7d3cc34]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..." );
[f73b969]748                        return;
[b7d3cc34]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
[f73b969]758static void cmd_set( irc_t *irc, char **cmd )
[b7d3cc34]759{
[71dc854]760        char *set_name = cmd[1];
[cd428e4]761       
[b7d3cc34]762        if( cmd[1] && cmd[2] )
763        {
[71dc854]764                if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 )
[cd428e4]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                }
[b7d3cc34]773        }
[56f260a]774        if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */
[b7d3cc34]775        {
[cd428e4]776                char *s = set_getstr( &irc->set, set_name );
777                if( s )
778                        irc_usermsg( irc, "%s = `%s'", set_name, s );
[5100caa]779                else
[cd428e4]780                        irc_usermsg( irc, "%s is empty", set_name );
[b7d3cc34]781        }
782        else
783        {
784                set_t *s = irc->set;
785                while( s )
786                {
787                        if( s->value || s->def )
[cd428e4]788                                irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def );
[5100caa]789                        else
790                                irc_usermsg( irc, "%s is empty", s->key );
[b7d3cc34]791                        s = s->next;
792                }
793        }
794}
795
[f73b969]796static void cmd_save( irc_t *irc, char **cmd )
[b7d3cc34]797{
[ab49fdc]798        if( storage_save( irc, TRUE ) == STORAGE_OK )
[b7d3cc34]799                irc_usermsg( irc, "Configuration saved" );
800        else
801                irc_usermsg( irc, "Configuration could not be saved!" );
802}
803
[f73b969]804static void cmd_blist( irc_t *irc, char **cmd )
[b7d3cc34]805{
806        int online = 0, away = 0, offline = 0;
807        user_t *u;
[aefa533e]808        char s[256];
809        char *format;
[b7d3cc34]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       
[aefa533e]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" );
[b7d3cc34]829       
[0da65d5]830        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away )
[b7d3cc34]831        {
[aefa533e]832                if( online == 1 )
833                {
[e731120]834                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]835                        irc_usermsg( irc, format, u->nick, s, "Online" );
836                }
837               
[b7d3cc34]838                n_online ++;
839        }
840
[0da65d5]841        for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away )
[b7d3cc34]842        {
[aefa533e]843                if( away == 1 )
844                {
[e731120]845                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]846                        irc_usermsg( irc, format, u->nick, s, u->away );
847                }
[b7d3cc34]848                n_away ++;
849        }
850       
[0da65d5]851        for( u = irc->users; u; u = u->next ) if( u->ic && !u->online )
[b7d3cc34]852        {
[aefa533e]853                if( offline == 1 )
854                {
[e731120]855                        g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user );
[aefa533e]856                        irc_usermsg( irc, format, u->nick, s, "Offline" );
857                }
[b7d3cc34]858                n_offline ++;
859        }
860       
[aa5ac01]861        irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline );
[b7d3cc34]862}
863
[f73b969]864static void cmd_nick( irc_t *irc, char **cmd ) 
[b7d3cc34]865{
866        account_t *a;
867
868        if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) )
869        {
870                irc_usermsg( irc, "Invalid account");
871        }
[0da65d5]872        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[b7d3cc34]873        {
874                irc_usermsg( irc, "That account is not on-line" );
875        }
876        else if ( !cmd[2] ) 
877        {
[0da65d5]878                irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" );
[b7d3cc34]879        }
[0da65d5]880        else if ( !a->prpl->set_my_name ) 
[b7d3cc34]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               
[0da65d5]888                a->prpl->set_my_name( a->ic, cmd[2] );
[b7d3cc34]889        }
890}
891
[f73b969]892static void cmd_qlist( irc_t *irc, char **cmd )
[b7d3cc34]893{
894        query_t *q = irc->queries;
895        int num;
896       
897        if( !q )
898        {
899                irc_usermsg( irc, "There are no pending questions." );
[f73b969]900                return;
[b7d3cc34]901        }
902       
903        irc_usermsg( irc, "Pending queries:" );
904       
905        for( num = 0; q; q = q->next, num ++ )
[0da65d5]906                if( q->ic ) /* Not necessary yet, but it might come later */
[c2fb3809]907                        irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question );
[5c09a59]908                else
909                        irc_usermsg( irc, "%d, BitlBee: %s", num, q->question );
[b7d3cc34]910}
911
[fa29d093]912static void cmd_join_chat( irc_t *irc, char **cmd )
913{
914        account_t *a;
[0da65d5]915        struct im_connection *ic;
[fa29d093]916        char *chat, *channel, *nick = NULL, *password = NULL;
[0da65d5]917        struct groupchat *c;
[fa29d093]918       
919        if( !( a = account_get( irc, cmd[1] ) ) )
920        {
921                irc_usermsg( irc, "Invalid account" );
922                return;
923        }
[0da65d5]924        else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) )
[fa29d093]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        }
[0da65d5]934        ic = a->ic;
[fa29d093]935       
936        chat = cmd[2];
937        if( cmd[3] )
938        {
[9da0bbf]939                if( cmd[3][0] != '#' && cmd[3][0] != '&' )
[7bf4326]940                        channel = g_strdup_printf( "&%s", cmd[3] );
941                else
942                        channel = g_strdup( cmd[3] );
[fa29d093]943        }
944        else
945        {
946                char *s;
947               
[0e7ab64]948                channel = g_strdup_printf( "&%s", chat );
[fa29d093]949                if( ( s = strchr( channel, '@' ) ) )
950                        *s = 0;
951        }
952        if( cmd[3] && cmd[4] )
953                nick = cmd[4];
[e35d1a1]954        else
955                nick = irc->nick;
[fa29d093]956        if( cmd[3] && cmd[4] && cmd[5] )
957                password = cmd[5];
958       
[7bf4326]959        if( !nick_ok( channel + 1 ) )
[0e7ab64]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        }
[fa29d093]971       
[e35d1a1]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        }
[fa29d093]982}
983
[0298d11]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 },
[fa29d093]1003        { "join_chat",      2, cmd_join_chat,      0 },
[764c7d1]1004        { "otr",            1, cmd_otr,            0 }, 
[0298d11]1005        { NULL }
1006};
Note: See TracBrowser for help on using the repository browser.