/********************************************************************\
* BitlBee -- An IRC to other IM-networks gateway *
* *
* Copyright 2002-2013 Wilmer van der Gaast and others *
\********************************************************************/
/* Account management functions */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License with
the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
Fifth Floor, Boston, MA 02110-1301 USA
*/
#define BITLBEE_CORE
#include "bitlbee.h"
#include "account.h"
static const char* account_protocols_local[] pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ /********************************************************************\
* BitlBee -- An IRC to other IM-networks gateway *
* *
* Copyright 2002-2010 Wilmer van der Gaast and others *
\********************************************************************/
/* The IRC-based UI - Representing (virtual) channels. */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License with
the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
if not, write to the Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA
*/
#include "bitlbee.h"
static char *set_eval_channel_type( set_t *set, char *value );
static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ );
static const struct irc_channel_funcs control_channel_funcs;
extern const struct irc_channel_funcs irc_channel_im_chat_funcs;
irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
{
irc_channel_t *ic;
if( !irc_channel_name_ok( name ) || irc_channel_by_name( irc, name ) )
return NULL;
ic = g_new0( irc_channel_t, 1 );
ic->irc = irc;
ic->name = g_strdup( name );
strcpy( ic->mode, CMODE );
irc_channel_add_user( ic, irc->root );
irc->channels = g_slist_append( irc->channels, ic );
set_add( &ic->set, "auto_join", "false", set_eval_bool, ic );
set_add( &ic->set, "type", "control", set_eval_channel_type, ic );
if( name[0] == '&' )
set_setstr( &ic->set, "type", "control" );
else /* if( name[0] == '#' ) */
set_setstr( &ic->set, "type", "chat" );
return ic;
}
irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
{
GSList *l;
for( l = irc->channels; l; l = l->next )
{
irc_channel_t *ic = l->data;
if( irc_channel_name_cmp( name, ic->name ) == 0 )
return ic;
}
return NULL;
}
irc_channel_t *irc_channel_get( irc_t *irc, char *id )
{
irc_channel_t *ic, *ret = NULL;
GSList *l;
int nr;
if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
{
for( l = irc->channels; l; l = l->next )
{
ic = l->data;
if( ( nr-- ) == 0 )
return ic;
}
return NULL;
}
/* Exact match first: Partial match only sucks if there's a channel
#aa and #aabb */
if( ( ret = irc_channel_by_name( irc, id ) ) )
return ret;
for( l = irc->channels; l; l = l->next )
{
ic = l->data;
if( strstr( ic->name, id ) )
{
/* Make sure it's a unique match. */
if( !ret )
ret = ic;
else
return NULL;
}
}
return ret;
}
int irc_channel_free( irc_channel_t *ic )
{
irc_t *irc = ic->irc;
if( ic->flags & IRC_CHANNEL_JOINED )
irc_channel_del_user( ic, irc->user, IRC_CDU_KICK, "Cleaning up channel" );
if( ic->f->_free )
ic->f->_free( ic );
while( ic->set )
set_del( &ic->set, ic->set->key );
irc->channels = g_slist_remove( irc->channels, ic );
while( ic->users )
{
g_free( ic->users->data );
ic->users = g_slist_remove( ic->users, ic->users->data );
}
g_free( ic->name );
g_free( ic->topic );
g_free( ic->topic_who );
g_free( ic );
return 1;
}
struct irc_channel_free_data
{
irc_t *irc;
irc_channel_t *ic;
char *name;
};
static gboolean irc_channel_free_callback( gpointer data, gint fd, b_input_condition cond )
{
struct irc_channel_free_data *d = data;
if( g_slist_find( irc_connection_list, d->irc ) &&
irc_channel_by_name( d->irc, d->name ) == d->ic &&
!( d->ic->flags & IRC_CHANNEL_JOINED ) )
irc_channel_free( d->ic );
g_free( d->name );
g_free( d );
return FALSE;
}
/* Free the channel, but via the event loop, so after finishing whatever event
we're currently handling. */
void irc_channel_free_soon( irc_channel_t *ic )
{
struct irc_channel_free_data *d = g_new0( struct irc_channel_free_data, 1 );
d->irc = ic->irc;
d->ic = ic;
d->name = g_strdup( ic->name );
b_timeout_add( 0, irc_channel_free_callback, d );
}
static char *set_eval_channel_type( set_t *set, char *value )
{
struct irc_channel *ic = set->data;
const struct irc_channel_funcs *new;
if( strcmp( value, "control" ) == 0 )
new = &control_channel_funcs;
else if( strcmp( value, "chat" ) == 0 )
new = &irc_channel_im_chat_funcs;
else
return SET_INVALID;
/* TODO: Return values. */
if( ic->f && ic->f->_free )
ic->f->_free( ic );
ic->f = new;
if( ic->f && ic->f->_init )
ic->f->_init( ic );
return value;
}
int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
{
irc_channel_user_t *icu;
if( irc_channel_has_user( ic, iu ) )
return 0;
icu = g_new0( irc_channel_user_t, 1 );
icu->iu = iu;
ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp );
irc_channel_update_ops( ic, set_getstr( &ic->irc->b->set, "ops" ) );
if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
{
ic->flags |= IRC_CHANNEL_JOINED;
irc_send_join( ic, iu );
}
return 1;
}
int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg )
{
irc_channel_user_t *icu;
if( !( icu = irc_channel_has_user( ic, iu ) ) )
return 0;
ic->users = g_slist_remove( ic->users, icu );
g_free( icu );
if( !( ic->flags & IRC_CHANNEL_JOINED ) || type == IRC_CDU_SILENT ) {}
/* Do nothing. The caller should promise it won't screw
up state of the IRC client. :-) */
else if( type == IRC_CDU_PART )
irc_send_part( ic, iu, msg );
else if( type == IRC_CDU_KICK )
irc_send_kick( ic, iu, ic->irc->root, msg );
if( iu == ic->irc->user )
{
ic->flags &= ~IRC_CHANNEL_JOINED;
if( ic->irc->status & USTATUS_SHUTDOWN )
{
/* Don't do anything fancy when we're shutting down anyway. */
}
else if( ic->flags & IRC_CHANNEL_TEMP )
{
irc_channel_free_soon( ic );
}
else
{
/* Flush userlist now. The user won't see it anyway. */
while( ic->users )
{
g_free( ic->users->data );
ic->users = g_slist_remove( ic->users, ic->users->data );
}
irc_channel_add_user( ic, ic->irc->root );
}
}
return 1;
}
irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
{
GSList *l;
for( l = ic->users; l; l = l->next )
{
irc_channel_user_t *icu = l->data;
if( icu->iu == iu )
return icu;
}
return NULL;
}
int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
{
g_free( ic->topic );
ic->topic = g_strdup( topic );
g_free( ic->topic_who );
if( iu )
ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
else
ic->topic_who = NULL;
ic->topic_time = time( NULL );
if( ic->flags & IRC_CHANNEL_JOINED )
irc_send_topic( ic, TRUE );
return 1;
}
void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags )
{
irc_channel_user_t *icu = irc_channel_has_user( ic, iu );
if( !icu || icu->flags == flags )
return;
if( ic->flags & IRC_CHANNEL_JOINED )
irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags );
icu->flags = flags;
}
void irc_channel_auto_joins( irc_t *irc, account_t *acc )
{
GSList *l;
for( l = irc->channels; l; l = l->next )
{
irc_channel_t *ic = l->data;
gboolean aj = set_getbool( &ic->set, "auto_join" );
char *type;
if( acc &&
( type = set_getstr( &ic->set, "chat_type" ) ) &&
strcmp( type, "room" ) == 0 )
{
/* Bit of an ugly special case: Handle chatrooms here, we
can only auto-join them if their account is online. */
char *acc_s;
if( !aj && !( ic->flags & IRC_CHANNEL_JOINED ) )
/* Only continue if this one's marked as auto_join
or if we're in it already. (Possible if the
client auto-rejoined it before identyfing.) */
continue;
else if( !( acc_s = set_getstr( &ic->set, "account" ) ) )
continue;
else if( account_get( irc->b, acc_s ) != acc )
continue;
else if( acc->ic == NULL || !( acc->ic->flags & OPT_LOGGED_IN ) )
continue;
else
ic->f->join( ic );
}
else if( aj )
{
irc_channel_add_user( ic, irc->user );
}
}
}
void irc_channel_printf( irc_channel_t *ic, char *format, ... )
{
va_list params;
char *text;
va_start( params, format );
text = g_strdup_vprintf( format, params );
va_end( params );
irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL );
g_free( text );
}
gboolean irc_channel_name_ok( const char *name_ )
{
const unsigned char *name = (unsigned char*) name_;
int i;
if( name_[0] == '\0' )
return FALSE;
/* Check if the first character is in CTYPES (#&) */
if( strchr( CTYPES, name_[0] ) == NULL )
return FALSE;
/* RFC 1459 keeps amazing me: While only a "few" chars are allowed
in nicknames, channel names can be pretty much anything as long
as they start with # or &. I'll be a little bit more strict and
disallow all non-printable characters. */
for( i = 1; name[i]; i ++ )
if( name[i] <= ' ' || name[i] == ',' )
return FALSE;
return TRUE;
}
void irc_channel_name_strip( char *name )
{
int i, j;
for( i = j = 0; name[i]; i ++ )
if( name[i] > ' ' && name[i] != ',' )
name[j++] = name[i];
name[j] = '\0';
}
int irc_channel_name_cmp( const char *a_, const char *b_ )
{
static unsigned char case_map[256];
const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_;
int i;
if( case_map['A'] == '\0' )
{
for( i = 33; i < 256; i ++ )
if( i != ',' )
case_map[i] = i;
for( i = 0; i < 26; i ++ )
case_map['A'+i] = 'a' + i;
case_map['['] = '{';
case_map[']'] = '}';
case_map['~'] = '`';
case_map['\\'] = '|';
}
if( !irc_channel_name_ok( a_