[4be8239] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* The IRC-based UI - Representing (virtual) channels. */ |
---|
| 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 | #include "bitlbee.h" |
---|
| 27 | |
---|
[2b8473c] | 28 | static char *set_eval_channel_type( set_t *set, char *value ); |
---|
[e54112f] | 29 | static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ ); |
---|
[280c56a] | 30 | static const struct irc_channel_funcs control_channel_funcs; |
---|
[5a75d15] | 31 | |
---|
| 32 | extern const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
[280c56a] | 33 | |
---|
[4be8239] | 34 | irc_channel_t *irc_channel_new( irc_t *irc, const char *name ) |
---|
| 35 | { |
---|
| 36 | irc_channel_t *ic; |
---|
| 37 | |
---|
[9438323] | 38 | if( !irc_channel_name_ok( name ) || irc_channel_by_name( irc, name ) ) |
---|
[4be8239] | 39 | return NULL; |
---|
| 40 | |
---|
| 41 | ic = g_new0( irc_channel_t, 1 ); |
---|
| 42 | ic->irc = irc; |
---|
| 43 | ic->name = g_strdup( name ); |
---|
| 44 | strcpy( ic->mode, CMODE ); |
---|
| 45 | |
---|
| 46 | irc_channel_add_user( ic, irc->root ); |
---|
[6a9d068] | 47 | if( strcmp( set_getstr( &irc->b->set, "ops" ), "both" ) == 0 || |
---|
| 48 | strcmp( set_getstr( &irc->b->set, "ops" ), "root" ) == 0 ) |
---|
| 49 | irc_channel_user_set_mode( ic, irc->root, IRC_CHANNEL_USER_OP ); |
---|
[4be8239] | 50 | |
---|
[36562b0] | 51 | irc->channels = g_slist_append( irc->channels, ic ); |
---|
[4be8239] | 52 | |
---|
[2b8473c] | 53 | set_add( &ic->set, "type", "control", set_eval_channel_type, ic ); |
---|
| 54 | |
---|
[eb37735] | 55 | if( name[0] == '&' ) |
---|
[2b8473c] | 56 | set_setstr( &ic->set, "type", "control" ); |
---|
[eb37735] | 57 | else /* if( name[0] == '#' ) */ |
---|
[2b8473c] | 58 | set_setstr( &ic->set, "type", "chat" ); |
---|
[9ac3ed1] | 59 | |
---|
[4be8239] | 60 | return ic; |
---|
| 61 | } |
---|
| 62 | |
---|
[b9e020a] | 63 | irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name ) |
---|
| 64 | { |
---|
| 65 | GSList *l; |
---|
| 66 | |
---|
| 67 | for( l = irc->channels; l; l = l->next ) |
---|
| 68 | { |
---|
| 69 | irc_channel_t *ic = l->data; |
---|
| 70 | |
---|
[36562b0] | 71 | if( g_strcasecmp( name, ic->name ) == 0 ) |
---|
[b9e020a] | 72 | return ic; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | return NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
[36562b0] | 78 | irc_channel_t *irc_channel_get( irc_t *irc, char *id ) |
---|
| 79 | { |
---|
| 80 | irc_channel_t *ic, *ret = NULL; |
---|
| 81 | GSList *l; |
---|
| 82 | int nr; |
---|
| 83 | |
---|
| 84 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
| 85 | { |
---|
| 86 | for( l = irc->channels; l; l = l->next ) |
---|
| 87 | { |
---|
| 88 | ic = l->data; |
---|
| 89 | if( ( nr-- ) == 0 ) |
---|
| 90 | return ic; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | return NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /* Exact match first: Partial match only sucks if there's a channel |
---|
| 97 | #aa and #aabb */ |
---|
| 98 | if( ( ret = irc_channel_by_name( irc, id ) ) ) |
---|
| 99 | return ret; |
---|
| 100 | |
---|
| 101 | for( l = irc->channels; l; l = l->next ) |
---|
| 102 | { |
---|
| 103 | ic = l->data; |
---|
| 104 | |
---|
| 105 | if( strstr( ic->name, id ) ) |
---|
| 106 | { |
---|
| 107 | /* Make sure it's a unique match. */ |
---|
| 108 | if( !ret ) |
---|
| 109 | ret = ic; |
---|
| 110 | else |
---|
| 111 | return NULL; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | return ret; |
---|
| 116 | } |
---|
| 117 | |
---|
[63a520b] | 118 | int irc_channel_free( irc_channel_t *ic ) |
---|
| 119 | { |
---|
| 120 | irc_t *irc = ic->irc; |
---|
| 121 | |
---|
| 122 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
[18da20b] | 123 | irc_channel_del_user( ic, irc->user, FALSE, "Cleaning up channel" ); |
---|
[63a520b] | 124 | |
---|
| 125 | irc->channels = g_slist_remove( irc->channels, ic ); |
---|
[e54112f] | 126 | while( ic->users ) |
---|
| 127 | { |
---|
| 128 | g_free( ic->users->data ); |
---|
| 129 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
| 130 | } |
---|
[63a520b] | 131 | |
---|
| 132 | g_free( ic->name ); |
---|
| 133 | g_free( ic->topic ); |
---|
| 134 | g_free( ic ); |
---|
| 135 | |
---|
| 136 | return 1; |
---|
| 137 | } |
---|
| 138 | |
---|
[2b8473c] | 139 | static char *set_eval_channel_type( set_t *set, char *value ) |
---|
| 140 | { |
---|
| 141 | struct irc_channel *ic = set->data; |
---|
| 142 | const struct irc_channel_funcs *new; |
---|
| 143 | |
---|
| 144 | if( strcmp( value, "control" ) == 0 ) |
---|
| 145 | new = &control_channel_funcs; |
---|
| 146 | else if( strcmp( value, "chat" ) == 0 ) |
---|
[5a75d15] | 147 | new = &irc_channel_im_chat_funcs; |
---|
[2b8473c] | 148 | else |
---|
| 149 | return SET_INVALID; |
---|
| 150 | |
---|
| 151 | /* TODO: Return values. */ |
---|
| 152 | if( ic->f && ic->f->_free ) |
---|
| 153 | ic->f->_free( ic ); |
---|
| 154 | |
---|
| 155 | ic->f = new; |
---|
| 156 | |
---|
| 157 | if( ic->f && ic->f->_init ) |
---|
| 158 | ic->f->_init( ic ); |
---|
| 159 | |
---|
| 160 | return value; |
---|
| 161 | } |
---|
| 162 | |
---|
[4be8239] | 163 | int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 164 | { |
---|
[e54112f] | 165 | irc_channel_user_t *icu; |
---|
| 166 | |
---|
[57c96f7] | 167 | if( irc_channel_has_user( ic, iu ) ) |
---|
[4be8239] | 168 | return 0; |
---|
| 169 | |
---|
[e54112f] | 170 | icu = g_new0( irc_channel_user_t, 1 ); |
---|
| 171 | icu->iu = iu; |
---|
| 172 | |
---|
| 173 | ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp ); |
---|
[4be8239] | 174 | |
---|
| 175 | if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 176 | { |
---|
| 177 | ic->flags |= IRC_CHANNEL_JOINED; |
---|
| 178 | irc_send_join( ic, iu ); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | return 1; |
---|
| 182 | } |
---|
| 183 | |
---|
[18da20b] | 184 | int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, gboolean silent, const char *msg ) |
---|
[4be8239] | 185 | { |
---|
[e54112f] | 186 | irc_channel_user_t *icu; |
---|
| 187 | |
---|
| 188 | if( !( icu = irc_channel_has_user( ic, iu ) ) ) |
---|
[4be8239] | 189 | return 0; |
---|
| 190 | |
---|
[e54112f] | 191 | ic->users = g_slist_remove( ic->users, icu ); |
---|
| 192 | g_free( icu ); |
---|
[4be8239] | 193 | |
---|
[18da20b] | 194 | if( ic->flags & IRC_CHANNEL_JOINED && !silent ) |
---|
| 195 | irc_send_part( ic, iu, msg ); |
---|
[4be8239] | 196 | |
---|
| 197 | if( iu == ic->irc->user ) |
---|
| 198 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
| 199 | |
---|
| 200 | return 1; |
---|
| 201 | } |
---|
| 202 | |
---|
[e54112f] | 203 | irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
[0b5cc72] | 204 | { |
---|
[e54112f] | 205 | GSList *l; |
---|
| 206 | |
---|
| 207 | for( l = ic->users; l; l = l->next ) |
---|
| 208 | { |
---|
| 209 | irc_channel_user_t *icu = l->data; |
---|
| 210 | |
---|
| 211 | if( icu->iu == iu ) |
---|
| 212 | return icu; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | return NULL; |
---|
[0b5cc72] | 216 | } |
---|
| 217 | |
---|
[83e92bf] | 218 | int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu ) |
---|
[4be8239] | 219 | { |
---|
| 220 | g_free( ic->topic ); |
---|
| 221 | ic->topic = g_strdup( topic ); |
---|
| 222 | |
---|
[83e92bf] | 223 | g_free( ic->topic_who ); |
---|
| 224 | if( iu ) |
---|
| 225 | ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host ); |
---|
| 226 | else |
---|
| 227 | ic->topic_who = NULL; |
---|
| 228 | |
---|
| 229 | ic->topic_time = time( NULL ); |
---|
| 230 | |
---|
[4be8239] | 231 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
[83e92bf] | 232 | irc_send_topic( ic, TRUE ); |
---|
[4be8239] | 233 | |
---|
| 234 | return 1; |
---|
| 235 | } |
---|
[b919363] | 236 | |
---|
[6a9d068] | 237 | void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags ) |
---|
| 238 | { |
---|
| 239 | irc_channel_user_t *icu = irc_channel_has_user( ic, iu ); |
---|
| 240 | |
---|
| 241 | if( icu->flags == flags ) |
---|
| 242 | return; |
---|
| 243 | |
---|
| 244 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 245 | irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags ); |
---|
| 246 | |
---|
| 247 | icu->flags = flags; |
---|
| 248 | } |
---|
| 249 | |
---|
[9893da3] | 250 | void irc_channel_printf( irc_channel_t *ic, char *format, ... ) |
---|
| 251 | { |
---|
| 252 | va_list params; |
---|
| 253 | char *text; |
---|
| 254 | |
---|
| 255 | va_start( params, format ); |
---|
| 256 | text = g_strdup_vprintf( format, params ); |
---|
| 257 | va_end( params ); |
---|
| 258 | |
---|
| 259 | irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL ); |
---|
| 260 | g_free( text ); |
---|
| 261 | } |
---|
| 262 | |
---|
[b919363] | 263 | gboolean irc_channel_name_ok( const char *name ) |
---|
| 264 | { |
---|
[4c17d19] | 265 | char name_[strlen(name)+1]; |
---|
| 266 | |
---|
| 267 | /* Check if the first character is in CTYPES (#&) */ |
---|
| 268 | if( strchr( CTYPES, name[0] ) == NULL ) |
---|
| 269 | return FALSE; |
---|
| 270 | |
---|
| 271 | /* Check the rest of the name. Just checking name + 1 doesn't work |
---|
| 272 | since it will fail if the first character is a number, or if |
---|
| 273 | it's a one-char channel name - both of which are legal. */ |
---|
| 274 | name_[0] = '_'; |
---|
| 275 | strcpy( name_ + 1, name + 1 ); |
---|
| 276 | return nick_ok( name_ ); |
---|
[b919363] | 277 | } |
---|
[280c56a] | 278 | |
---|
[e54112f] | 279 | static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
| 280 | { |
---|
| 281 | const irc_channel_user_t *a = a_, *b = b_; |
---|
| 282 | |
---|
| 283 | return irc_user_cmp( a->iu, b->iu ); |
---|
| 284 | } |
---|
| 285 | |
---|
[280c56a] | 286 | /* Channel-type dependent functions, for control channels: */ |
---|
| 287 | static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg ) |
---|
| 288 | { |
---|
[bce78c8] | 289 | irc_t *irc = ic->irc; |
---|
| 290 | const char *s; |
---|
[fb117aee] | 291 | |
---|
[bce78c8] | 292 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
[b0364dc] | 293 | for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {} |
---|
[74f1cde] | 294 | |
---|
[b0364dc] | 295 | if( *s == ':' || *s == ',' ) |
---|
[bce78c8] | 296 | { |
---|
| 297 | char to[s-msg+1]; |
---|
| 298 | irc_user_t *iu; |
---|
| 299 | |
---|
[1a3ba05] | 300 | memset( to, 0, sizeof( to ) ); |
---|
[bce78c8] | 301 | strncpy( to, msg, s - msg ); |
---|
| 302 | while( *(++s) && isspace( *s ) ) {} |
---|
| 303 | |
---|
| 304 | iu = irc_user_by_name( irc, to ); |
---|
| 305 | if( iu && iu->f->privmsg ) |
---|
| 306 | { |
---|
| 307 | iu->flags &= ~IRC_USER_PRIVATE; |
---|
| 308 | iu->f->privmsg( iu, s ); |
---|
| 309 | } |
---|
[1a3ba05] | 310 | else |
---|
| 311 | { |
---|
[9893da3] | 312 | irc_channel_printf( ic, "User does not exist: %s", to ); |
---|
[1a3ba05] | 313 | } |
---|
[bce78c8] | 314 | } |
---|
| 315 | else |
---|
| 316 | { |
---|
| 317 | /* TODO: Maybe just use root->privmsg here now? */ |
---|
| 318 | char cmd[strlen(msg)+1]; |
---|
| 319 | |
---|
| 320 | g_free( ic->irc->last_root_cmd ); |
---|
| 321 | ic->irc->last_root_cmd = g_strdup( ic->name ); |
---|
| 322 | |
---|
| 323 | strcpy( cmd, msg ); |
---|
| 324 | root_command_string( ic->irc, cmd ); |
---|
| 325 | } |
---|
[280c56a] | 326 | |
---|
| 327 | return TRUE; |
---|
| 328 | } |
---|
| 329 | |
---|
[2b8473c] | 330 | static char *set_eval_by_account( set_t *set, char *value ); |
---|
| 331 | static char *set_eval_fill_by( set_t *set, char *value ); |
---|
| 332 | static char *set_eval_by_group( set_t *set, char *value ); |
---|
| 333 | |
---|
[9ac3ed1] | 334 | static gboolean control_channel_init( irc_channel_t *ic ) |
---|
| 335 | { |
---|
| 336 | struct irc_control_channel *icc; |
---|
| 337 | |
---|
[2b8473c] | 338 | set_add( &ic->set, "account", NULL, set_eval_by_account, ic ); |
---|
| 339 | set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic ); |
---|
| 340 | set_add( &ic->set, "group", NULL, set_eval_by_group, ic ); |
---|
| 341 | |
---|
[9ac3ed1] | 342 | ic->data = icc = g_new0( struct irc_control_channel, 1 ); |
---|
| 343 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
| 344 | |
---|
[2b8473c] | 345 | if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) ) |
---|
| 346 | { |
---|
| 347 | set_setstr( &ic->set, "group", ic->name + 1 ); |
---|
| 348 | set_setstr( &ic->set, "fill_by", "group" ); |
---|
| 349 | } |
---|
| 350 | else if( set_setstr( &ic->set, "account", ic->name + 1 ) ) |
---|
| 351 | { |
---|
| 352 | set_setstr( &ic->set, "fill_by", "account" ); |
---|
| 353 | } |
---|
[cf1a979] | 354 | else |
---|
| 355 | { |
---|
| 356 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
| 357 | } |
---|
[2b8473c] | 358 | |
---|
| 359 | return TRUE; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | static char *set_eval_by_account( set_t *set, char *value ) |
---|
| 363 | { |
---|
| 364 | struct irc_channel *ic = set->data; |
---|
| 365 | struct irc_control_channel *icc = ic->data; |
---|
| 366 | account_t *acc; |
---|
| 367 | |
---|
| 368 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
| 369 | return SET_INVALID; |
---|
| 370 | |
---|
| 371 | icc->account = acc; |
---|
[cf1a979] | 372 | if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
| 373 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
[2b8473c] | 374 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | static char *set_eval_fill_by( set_t *set, char *value ) |
---|
| 378 | { |
---|
| 379 | struct irc_channel *ic = set->data; |
---|
| 380 | struct irc_control_channel *icc = ic->data; |
---|
| 381 | |
---|
| 382 | if( strcmp( value, "all" ) == 0 ) |
---|
| 383 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
| 384 | else if( strcmp( value, "rest" ) == 0 ) |
---|
| 385 | icc->type = IRC_CC_TYPE_REST; |
---|
| 386 | else if( strcmp( value, "group" ) == 0 ) |
---|
[13c1a9f] | 387 | icc->type = IRC_CC_TYPE_GROUP; |
---|
[2b8473c] | 388 | else if( strcmp( value, "account" ) == 0 ) |
---|
[a067771] | 389 | icc->type = IRC_CC_TYPE_ACCOUNT; |
---|
[2b8473c] | 390 | else |
---|
| 391 | return SET_INVALID; |
---|
| 392 | |
---|
[cf1a979] | 393 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
[2b8473c] | 394 | return value; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | static char *set_eval_by_group( set_t *set, char *value ) |
---|
| 398 | { |
---|
| 399 | struct irc_channel *ic = set->data; |
---|
| 400 | struct irc_control_channel *icc = ic->data; |
---|
[13c1a9f] | 401 | |
---|
[2b8473c] | 402 | icc->group = bee_group_by_name( ic->irc->b, value, TRUE ); |
---|
[cf1a979] | 403 | if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
| 404 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
[2b8473c] | 405 | return g_strdup( icc->group->name ); |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | static gboolean control_channel_free( irc_channel_t *ic ) |
---|
| 409 | { |
---|
| 410 | struct irc_control_channel *icc = ic->data; |
---|
| 411 | |
---|
| 412 | set_del( &ic->set, "account" ); |
---|
| 413 | set_del( &ic->set, "fill_by" ); |
---|
| 414 | set_del( &ic->set, "group" ); |
---|
| 415 | |
---|
| 416 | g_free( icc ); |
---|
| 417 | ic->data = NULL; |
---|
[13c1a9f] | 418 | |
---|
[9ac3ed1] | 419 | return TRUE; |
---|
| 420 | } |
---|
| 421 | |
---|
[280c56a] | 422 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
| 423 | control_channel_privmsg, |
---|
[9ac3ed1] | 424 | NULL, |
---|
| 425 | NULL, |
---|
| 426 | NULL, |
---|
| 427 | NULL, |
---|
| 428 | |
---|
| 429 | control_channel_init, |
---|
[2b8473c] | 430 | control_channel_free, |
---|
[280c56a] | 431 | }; |
---|