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