[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 | |
---|
[c8eeadd] | 50 | set_add( &ic->set, "auto_join", "false", set_eval_bool, ic ); |
---|
[2b8473c] | 51 | set_add( &ic->set, "type", "control", set_eval_channel_type, ic ); |
---|
| 52 | |
---|
[eb37735] | 53 | if( name[0] == '&' ) |
---|
[2b8473c] | 54 | set_setstr( &ic->set, "type", "control" ); |
---|
[eb37735] | 55 | else /* if( name[0] == '#' ) */ |
---|
[2b8473c] | 56 | set_setstr( &ic->set, "type", "chat" ); |
---|
[9ac3ed1] | 57 | |
---|
[4be8239] | 58 | return ic; |
---|
| 59 | } |
---|
| 60 | |
---|
[b9e020a] | 61 | irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name ) |
---|
| 62 | { |
---|
| 63 | GSList *l; |
---|
| 64 | |
---|
| 65 | for( l = irc->channels; l; l = l->next ) |
---|
| 66 | { |
---|
| 67 | irc_channel_t *ic = l->data; |
---|
| 68 | |
---|
[6b90431] | 69 | if( irc_channel_name_cmp( name, ic->name ) == 0 ) |
---|
[b9e020a] | 70 | return ic; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | return NULL; |
---|
| 74 | } |
---|
| 75 | |
---|
[36562b0] | 76 | irc_channel_t *irc_channel_get( irc_t *irc, char *id ) |
---|
| 77 | { |
---|
| 78 | irc_channel_t *ic, *ret = NULL; |
---|
| 79 | GSList *l; |
---|
| 80 | int nr; |
---|
| 81 | |
---|
| 82 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
| 83 | { |
---|
| 84 | for( l = irc->channels; l; l = l->next ) |
---|
| 85 | { |
---|
| 86 | ic = l->data; |
---|
| 87 | if( ( nr-- ) == 0 ) |
---|
| 88 | return ic; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return NULL; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /* Exact match first: Partial match only sucks if there's a channel |
---|
| 95 | #aa and #aabb */ |
---|
| 96 | if( ( ret = irc_channel_by_name( irc, id ) ) ) |
---|
| 97 | return ret; |
---|
| 98 | |
---|
| 99 | for( l = irc->channels; l; l = l->next ) |
---|
| 100 | { |
---|
| 101 | ic = l->data; |
---|
| 102 | |
---|
| 103 | if( strstr( ic->name, id ) ) |
---|
| 104 | { |
---|
| 105 | /* Make sure it's a unique match. */ |
---|
| 106 | if( !ret ) |
---|
| 107 | ret = ic; |
---|
| 108 | else |
---|
| 109 | return NULL; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | return ret; |
---|
| 114 | } |
---|
| 115 | |
---|
[63a520b] | 116 | int irc_channel_free( irc_channel_t *ic ) |
---|
| 117 | { |
---|
| 118 | irc_t *irc = ic->irc; |
---|
[2fe5eb9] | 119 | GSList *l; |
---|
[63a520b] | 120 | |
---|
| 121 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
[006a84f] | 122 | irc_channel_del_user( ic, irc->user, IRC_CDU_KICK, "Cleaning up channel" ); |
---|
[63a520b] | 123 | |
---|
[d7db346] | 124 | if( ic->f->_free ) |
---|
| 125 | ic->f->_free( ic ); |
---|
| 126 | |
---|
| 127 | while( ic->set ) |
---|
| 128 | set_del( &ic->set, ic->set->key ); |
---|
| 129 | |
---|
[63a520b] | 130 | irc->channels = g_slist_remove( irc->channels, ic ); |
---|
[e54112f] | 131 | while( ic->users ) |
---|
| 132 | { |
---|
| 133 | g_free( ic->users->data ); |
---|
| 134 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
| 135 | } |
---|
[63a520b] | 136 | |
---|
[2fe5eb9] | 137 | for( l = irc->users; l; l = l->next ) |
---|
| 138 | { |
---|
| 139 | irc_user_t *iu = l->data; |
---|
| 140 | |
---|
| 141 | if( iu->last_channel == ic ) |
---|
| 142 | iu->last_channel = irc->default_channel; |
---|
| 143 | } |
---|
| 144 | |
---|
[63a520b] | 145 | g_free( ic->name ); |
---|
| 146 | g_free( ic->topic ); |
---|
[d7db346] | 147 | g_free( ic->topic_who ); |
---|
[63a520b] | 148 | g_free( ic ); |
---|
| 149 | |
---|
| 150 | return 1; |
---|
| 151 | } |
---|
| 152 | |
---|
[ab6006c] | 153 | struct irc_channel_free_data |
---|
| 154 | { |
---|
| 155 | irc_t *irc; |
---|
| 156 | irc_channel_t *ic; |
---|
| 157 | char *name; |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | static gboolean irc_channel_free_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 161 | { |
---|
| 162 | struct irc_channel_free_data *d = data; |
---|
| 163 | |
---|
| 164 | if( g_slist_find( irc_connection_list, d->irc ) && |
---|
| 165 | irc_channel_by_name( d->irc, d->name ) == d->ic && |
---|
| 166 | !( d->ic->flags & IRC_CHANNEL_JOINED ) ) |
---|
| 167 | irc_channel_free( d->ic ); |
---|
| 168 | |
---|
| 169 | g_free( d->name ); |
---|
| 170 | g_free( d ); |
---|
| 171 | return FALSE; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /* Free the channel, but via the event loop, so after finishing whatever event |
---|
| 175 | we're currently handling. */ |
---|
| 176 | void irc_channel_free_soon( irc_channel_t *ic ) |
---|
| 177 | { |
---|
| 178 | struct irc_channel_free_data *d = g_new0( struct irc_channel_free_data, 1 ); |
---|
| 179 | |
---|
| 180 | d->irc = ic->irc; |
---|
| 181 | d->ic = ic; |
---|
| 182 | d->name = g_strdup( ic->name ); |
---|
| 183 | |
---|
| 184 | b_timeout_add( 0, irc_channel_free_callback, d ); |
---|
| 185 | } |
---|
| 186 | |
---|
[2b8473c] | 187 | static char *set_eval_channel_type( set_t *set, char *value ) |
---|
| 188 | { |
---|
| 189 | struct irc_channel *ic = set->data; |
---|
| 190 | const struct irc_channel_funcs *new; |
---|
| 191 | |
---|
| 192 | if( strcmp( value, "control" ) == 0 ) |
---|
| 193 | new = &control_channel_funcs; |
---|
| 194 | else if( strcmp( value, "chat" ) == 0 ) |
---|
[5a75d15] | 195 | new = &irc_channel_im_chat_funcs; |
---|
[2b8473c] | 196 | else |
---|
| 197 | return SET_INVALID; |
---|
| 198 | |
---|
| 199 | /* TODO: Return values. */ |
---|
| 200 | if( ic->f && ic->f->_free ) |
---|
| 201 | ic->f->_free( ic ); |
---|
| 202 | |
---|
| 203 | ic->f = new; |
---|
| 204 | |
---|
| 205 | if( ic->f && ic->f->_init ) |
---|
| 206 | ic->f->_init( ic ); |
---|
| 207 | |
---|
| 208 | return value; |
---|
| 209 | } |
---|
| 210 | |
---|
[4be8239] | 211 | int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 212 | { |
---|
[e54112f] | 213 | irc_channel_user_t *icu; |
---|
| 214 | |
---|
[57c96f7] | 215 | if( irc_channel_has_user( ic, iu ) ) |
---|
[4be8239] | 216 | return 0; |
---|
| 217 | |
---|
[e54112f] | 218 | icu = g_new0( irc_channel_user_t, 1 ); |
---|
| 219 | icu->iu = iu; |
---|
| 220 | |
---|
| 221 | ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp ); |
---|
[4be8239] | 222 | |
---|
[c5aefa4] | 223 | irc_channel_update_ops( ic, set_getstr( &ic->irc->b->set, "ops" ) ); |
---|
| 224 | |
---|
[4be8239] | 225 | if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 226 | { |
---|
| 227 | ic->flags |= IRC_CHANNEL_JOINED; |
---|
| 228 | irc_send_join( ic, iu ); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | return 1; |
---|
| 232 | } |
---|
| 233 | |
---|
[006a84f] | 234 | int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg ) |
---|
[4be8239] | 235 | { |
---|
[e54112f] | 236 | irc_channel_user_t *icu; |
---|
| 237 | |
---|
| 238 | if( !( icu = irc_channel_has_user( ic, iu ) ) ) |
---|
[4be8239] | 239 | return 0; |
---|
| 240 | |
---|
[e54112f] | 241 | ic->users = g_slist_remove( ic->users, icu ); |
---|
| 242 | g_free( icu ); |
---|
[4be8239] | 243 | |
---|
[006a84f] | 244 | if( !( ic->flags & IRC_CHANNEL_JOINED ) || type == IRC_CDU_SILENT ) {} |
---|
| 245 | /* Do nothing. The caller should promise it won't screw |
---|
| 246 | up state of the IRC client. :-) */ |
---|
| 247 | else if( type == IRC_CDU_PART ) |
---|
[18da20b] | 248 | irc_send_part( ic, iu, msg ); |
---|
[006a84f] | 249 | else if( type == IRC_CDU_KICK ) |
---|
| 250 | irc_send_kick( ic, iu, ic->irc->root, msg ); |
---|
[4be8239] | 251 | |
---|
| 252 | if( iu == ic->irc->user ) |
---|
[1c40aa7] | 253 | { |
---|
[4be8239] | 254 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
[1c40aa7] | 255 | |
---|
[06f9548] | 256 | if( ic->irc->status & USTATUS_SHUTDOWN ) |
---|
| 257 | { |
---|
| 258 | /* Don't do anything fancy when we're shutting down anyway. */ |
---|
| 259 | } |
---|
| 260 | else if( ic->flags & IRC_CHANNEL_TEMP ) |
---|
| 261 | { |
---|
[ab6006c] | 262 | irc_channel_free_soon( ic ); |
---|
[06f9548] | 263 | } |
---|
[9052bc1] | 264 | else |
---|
| 265 | { |
---|
| 266 | /* Flush userlist now. The user won't see it anyway. */ |
---|
| 267 | while( ic->users ) |
---|
| 268 | { |
---|
| 269 | g_free( ic->users->data ); |
---|
| 270 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
| 271 | } |
---|
| 272 | irc_channel_add_user( ic, ic->irc->root ); |
---|
| 273 | } |
---|
[1c40aa7] | 274 | } |
---|
[4be8239] | 275 | |
---|
| 276 | return 1; |
---|
| 277 | } |
---|
| 278 | |
---|
[e54112f] | 279 | irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
[0b5cc72] | 280 | { |
---|
[e54112f] | 281 | GSList *l; |
---|
| 282 | |
---|
| 283 | for( l = ic->users; l; l = l->next ) |
---|
| 284 | { |
---|
| 285 | irc_channel_user_t *icu = l->data; |
---|
| 286 | |
---|
| 287 | if( icu->iu == iu ) |
---|
| 288 | return icu; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | return NULL; |
---|
[0b5cc72] | 292 | } |
---|
| 293 | |
---|
[83e92bf] | 294 | int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu ) |
---|
[4be8239] | 295 | { |
---|
| 296 | g_free( ic->topic ); |
---|
| 297 | ic->topic = g_strdup( topic ); |
---|
| 298 | |
---|
[83e92bf] | 299 | g_free( ic->topic_who ); |
---|
| 300 | if( iu ) |
---|
| 301 | ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host ); |
---|
| 302 | else |
---|
| 303 | ic->topic_who = NULL; |
---|
| 304 | |
---|
| 305 | ic->topic_time = time( NULL ); |
---|
| 306 | |
---|
[4be8239] | 307 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
[83e92bf] | 308 | irc_send_topic( ic, TRUE ); |
---|
[4be8239] | 309 | |
---|
| 310 | return 1; |
---|
| 311 | } |
---|
[b919363] | 312 | |
---|
[6a9d068] | 313 | void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags ) |
---|
| 314 | { |
---|
| 315 | irc_channel_user_t *icu = irc_channel_has_user( ic, iu ); |
---|
| 316 | |
---|
[c5aefa4] | 317 | if( !icu || icu->flags == flags ) |
---|
[6a9d068] | 318 | return; |
---|
| 319 | |
---|
| 320 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 321 | irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags ); |
---|
| 322 | |
---|
| 323 | icu->flags = flags; |
---|
| 324 | } |
---|
| 325 | |
---|
[c8eeadd] | 326 | void irc_channel_auto_joins( irc_t *irc, account_t *acc ) |
---|
| 327 | { |
---|
| 328 | GSList *l; |
---|
| 329 | |
---|
| 330 | for( l = irc->channels; l; l = l->next ) |
---|
| 331 | { |
---|
| 332 | irc_channel_t *ic = l->data; |
---|
| 333 | gboolean aj = set_getbool( &ic->set, "auto_join" ); |
---|
| 334 | char *type; |
---|
| 335 | |
---|
| 336 | if( acc && |
---|
| 337 | ( type = set_getstr( &ic->set, "chat_type" ) ) && |
---|
| 338 | strcmp( type, "room" ) == 0 ) |
---|
| 339 | { |
---|
| 340 | /* Bit of an ugly special case: Handle chatrooms here, we |
---|
| 341 | can only auto-join them if their account is online. */ |
---|
| 342 | char *acc_s; |
---|
| 343 | |
---|
| 344 | if( !aj && !( ic->flags & IRC_CHANNEL_JOINED ) ) |
---|
| 345 | /* Only continue if this one's marked as auto_join |
---|
| 346 | or if we're in it already. (Possible if the |
---|
| 347 | client auto-rejoined it before identyfing.) */ |
---|
| 348 | continue; |
---|
| 349 | else if( !( acc_s = set_getstr( &ic->set, "account" ) ) ) |
---|
| 350 | continue; |
---|
| 351 | else if( account_get( irc->b, acc_s ) != acc ) |
---|
| 352 | continue; |
---|
| 353 | else if( acc->ic == NULL || !( acc->ic->flags & OPT_LOGGED_IN ) ) |
---|
| 354 | continue; |
---|
| 355 | else |
---|
| 356 | ic->f->join( ic ); |
---|
| 357 | } |
---|
| 358 | else if( aj ) |
---|
| 359 | { |
---|
| 360 | irc_channel_add_user( ic, irc->user ); |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | |
---|
[9893da3] | 365 | void irc_channel_printf( irc_channel_t *ic, char *format, ... ) |
---|
| 366 | { |
---|
| 367 | va_list params; |
---|
| 368 | char *text; |
---|
| 369 | |
---|
| 370 | va_start( params, format ); |
---|
| 371 | text = g_strdup_vprintf( format, params ); |
---|
| 372 | va_end( params ); |
---|
| 373 | |
---|
| 374 | irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL ); |
---|
| 375 | g_free( text ); |
---|
| 376 | } |
---|
| 377 | |
---|
[6b90431] | 378 | gboolean irc_channel_name_ok( const char *name_ ) |
---|
[b919363] | 379 | { |
---|
[6b90431] | 380 | const unsigned char *name = (unsigned char*) name_; |
---|
| 381 | int i; |
---|
[4c17d19] | 382 | |
---|
[a670aeb] | 383 | if( name_[0] == '\0' ) |
---|
| 384 | return FALSE; |
---|
| 385 | |
---|
[4c17d19] | 386 | /* Check if the first character is in CTYPES (#&) */ |
---|
[6b90431] | 387 | if( strchr( CTYPES, name_[0] ) == NULL ) |
---|
[4c17d19] | 388 | return FALSE; |
---|
| 389 | |
---|
[6b90431] | 390 | /* RFC 1459 keeps amazing me: While only a "few" chars are allowed |
---|
| 391 | in nicknames, channel names can be pretty much anything as long |
---|
| 392 | as they start with # or &. I'll be a little bit more strict and |
---|
| 393 | disallow all non-printable characters. */ |
---|
| 394 | for( i = 1; name[i]; i ++ ) |
---|
| 395 | if( name[i] <= ' ' || name[i] == ',' ) |
---|
| 396 | return FALSE; |
---|
| 397 | |
---|
| 398 | return TRUE; |
---|
| 399 | } |
---|
| 400 | |
---|
[134a02c] | 401 | void irc_channel_name_strip( char *name ) |
---|
| 402 | { |
---|
| 403 | int i, j; |
---|
| 404 | |
---|
| 405 | for( i = j = 0; name[i]; i ++ ) |
---|
| 406 | if( name[i] > ' ' && name[i] != ',' ) |
---|
| 407 | name[j++] = name[i]; |
---|
| 408 | |
---|
| 409 | name[j] = '\0'; |
---|
| 410 | } |
---|
| 411 | |
---|
[6b90431] | 412 | int irc_channel_name_cmp( const char *a_, const char *b_ ) |
---|
| 413 | { |
---|
| 414 | static unsigned char case_map[256]; |
---|
| 415 | const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_; |
---|
| 416 | int i; |
---|
| 417 | |
---|
| 418 | if( case_map['A'] == '\0' ) |
---|
| 419 | { |
---|
| 420 | for( i = 33; i < 256; i ++ ) |
---|
| 421 | if( i != ',' ) |
---|
| 422 | case_map[i] = i; |
---|
| 423 | |
---|
| 424 | for( i = 0; i < 26; i ++ ) |
---|
| 425 | case_map['A'+i] = 'a' + i; |
---|
| 426 | |
---|
| 427 | case_map['['] = '{'; |
---|
| 428 | case_map[']'] = '}'; |
---|
| 429 | case_map['~'] = '`'; |
---|
| 430 | case_map['\\'] = '|'; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if( !irc_channel_name_ok( a_ ) || !irc_channel_name_ok( b_ ) ) |
---|
| 434 | return -1; |
---|
| 435 | |
---|
| 436 | for( i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i ++ ) |
---|
| 437 | { |
---|
| 438 | if( case_map[a[i]] == case_map[b[i]] ) |
---|
| 439 | continue; |
---|
| 440 | else |
---|
| 441 | return case_map[a[i]] - case_map[b[i]]; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | return case_map[a[i]] - case_map[b[i]]; |
---|
[b919363] | 445 | } |
---|
[280c56a] | 446 | |
---|
[e54112f] | 447 | static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
| 448 | { |
---|
| 449 | const irc_channel_user_t *a = a_, *b = b_; |
---|
| 450 | |
---|
| 451 | return irc_user_cmp( a->iu, b->iu ); |
---|
| 452 | } |
---|
| 453 | |
---|
[c5aefa4] | 454 | void irc_channel_update_ops( irc_channel_t *ic, char *value ) |
---|
| 455 | { |
---|
| 456 | irc_channel_user_set_mode( ic, ic->irc->root, |
---|
| 457 | ( strcmp( value, "both" ) == 0 || |
---|
| 458 | strcmp( value, "root" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
| 459 | irc_channel_user_set_mode( ic, ic->irc->user, |
---|
| 460 | ( strcmp( value, "both" ) == 0 || |
---|
| 461 | strcmp( value, "user" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | char *set_eval_irc_channel_ops( set_t *set, char *value ) |
---|
| 465 | { |
---|
| 466 | irc_t *irc = set->data; |
---|
| 467 | GSList *l; |
---|
| 468 | |
---|
| 469 | if( strcmp( value, "both" ) != 0 && strcmp( value, "none" ) != 0 && |
---|
| 470 | strcmp( value, "user" ) != 0 && strcmp( value, "root" ) != 0 ) |
---|
| 471 | return SET_INVALID; |
---|
| 472 | |
---|
| 473 | for( l = irc->channels; l; l = l->next ) |
---|
| 474 | irc_channel_update_ops( l->data, value ); |
---|
| 475 | |
---|
| 476 | return value; |
---|
| 477 | } |
---|
| 478 | |
---|
[280c56a] | 479 | /* Channel-type dependent functions, for control channels: */ |
---|
| 480 | static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg ) |
---|
| 481 | { |
---|
[bce78c8] | 482 | irc_t *irc = ic->irc; |
---|
[f7ca587] | 483 | irc_user_t *iu; |
---|
[bce78c8] | 484 | const char *s; |
---|
[fb117aee] | 485 | |
---|
[bce78c8] | 486 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
[b0364dc] | 487 | for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {} |
---|
[74f1cde] | 488 | |
---|
[b0364dc] | 489 | if( *s == ':' || *s == ',' ) |
---|
[bce78c8] | 490 | { |
---|
| 491 | char to[s-msg+1]; |
---|
| 492 | |
---|
[1a3ba05] | 493 | memset( to, 0, sizeof( to ) ); |
---|
[bce78c8] | 494 | strncpy( to, msg, s - msg ); |
---|
| 495 | while( *(++s) && isspace( *s ) ) {} |
---|
[f7ca587] | 496 | msg = s; |
---|
[bce78c8] | 497 | |
---|
[f7ca587] | 498 | if( !( iu = irc_user_by_name( irc, to ) ) ) |
---|
[9893da3] | 499 | irc_channel_printf( ic, "User does not exist: %s", to ); |
---|
[f7ca587] | 500 | else |
---|
| 501 | ic->last_target = iu; |
---|
[bce78c8] | 502 | } |
---|
[f7ca587] | 503 | else if( g_strcasecmp( set_getstr( &irc->b->set, "default_target" ), "last" ) == 0 && |
---|
| 504 | ic->last_target && g_slist_find( irc->users, ic->last_target ) ) |
---|
| 505 | iu = ic->last_target; |
---|
[bce78c8] | 506 | else |
---|
[f7ca587] | 507 | iu = irc->root; |
---|
| 508 | |
---|
| 509 | if( iu && iu->f->privmsg ) |
---|
[bce78c8] | 510 | { |
---|
[f7ca587] | 511 | iu->last_channel = ic; |
---|
| 512 | iu->f->privmsg( iu, msg ); |
---|
[bce78c8] | 513 | } |
---|
[280c56a] | 514 | |
---|
| 515 | return TRUE; |
---|
| 516 | } |
---|
| 517 | |
---|
[46d215d] | 518 | static gboolean control_channel_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 519 | { |
---|
| 520 | struct irc_control_channel *icc = ic->data; |
---|
| 521 | bee_user_t *bu = iu->bu; |
---|
| 522 | |
---|
| 523 | if( bu == NULL ) |
---|
| 524 | return FALSE; |
---|
| 525 | |
---|
| 526 | if( icc->type != IRC_CC_TYPE_GROUP ) |
---|
| 527 | { |
---|
| 528 | irc_send_num( ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name ); |
---|
| 529 | return FALSE; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | bu->ic->acc->prpl->add_buddy( bu->ic, bu->handle, |
---|
| 533 | icc->group ? icc->group->name : NULL ); |
---|
| 534 | |
---|
| 535 | return TRUE; |
---|
| 536 | } |
---|
| 537 | |
---|
[2b8473c] | 538 | static char *set_eval_by_account( set_t *set, char *value ); |
---|
| 539 | static char *set_eval_fill_by( set_t *set, char *value ); |
---|
| 540 | static char *set_eval_by_group( set_t *set, char *value ); |
---|
[7a6ba50] | 541 | static char *set_eval_by_protocol( set_t *set, char *value ); |
---|
[94d5da9c] | 542 | static char *set_eval_show_users( set_t *set, char *value ); |
---|
[2b8473c] | 543 | |
---|
[9ac3ed1] | 544 | static gboolean control_channel_init( irc_channel_t *ic ) |
---|
| 545 | { |
---|
| 546 | struct irc_control_channel *icc; |
---|
| 547 | |
---|
[2b8473c] | 548 | set_add( &ic->set, "account", NULL, set_eval_by_account, ic ); |
---|
| 549 | set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic ); |
---|
| 550 | set_add( &ic->set, "group", NULL, set_eval_by_group, ic ); |
---|
[7a6ba50] | 551 | set_add( &ic->set, "protocol", NULL, set_eval_by_protocol, ic ); |
---|
[5a61bf59] | 552 | |
---|
| 553 | /* When changing the default, also change it below. */ |
---|
[94d5da9c] | 554 | set_add( &ic->set, "show_users", "online+,away", set_eval_show_users, ic ); |
---|
[2b8473c] | 555 | |
---|
[9ac3ed1] | 556 | ic->data = icc = g_new0( struct irc_control_channel, 1 ); |
---|
| 557 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
| 558 | |
---|
[94d5da9c] | 559 | /* Have to run the evaluator to initialize icc->modes. */ |
---|
[5a61bf59] | 560 | set_setstr( &ic->set, "show_users", "online+,away" ); |
---|
[94d5da9c] | 561 | |
---|
[2b8473c] | 562 | return TRUE; |
---|
| 563 | } |
---|
| 564 | |
---|
[c8eeadd] | 565 | static gboolean control_channel_join( irc_channel_t *ic ) |
---|
| 566 | { |
---|
| 567 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
| 568 | |
---|
| 569 | return TRUE; |
---|
| 570 | } |
---|
| 571 | |
---|
[2b8473c] | 572 | static char *set_eval_by_account( set_t *set, char *value ) |
---|
| 573 | { |
---|
| 574 | struct irc_channel *ic = set->data; |
---|
| 575 | struct irc_control_channel *icc = ic->data; |
---|
| 576 | account_t *acc; |
---|
| 577 | |
---|
| 578 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
| 579 | return SET_INVALID; |
---|
| 580 | |
---|
| 581 | icc->account = acc; |
---|
[cf1a979] | 582 | if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
| 583 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
[7a6ba50] | 584 | |
---|
[e135cd09] | 585 | return g_strdup( acc->tag ); |
---|
[2b8473c] | 586 | } |
---|
| 587 | |
---|
| 588 | static char *set_eval_fill_by( set_t *set, char *value ) |
---|
| 589 | { |
---|
| 590 | struct irc_channel *ic = set->data; |
---|
| 591 | struct irc_control_channel *icc = ic->data; |
---|
| 592 | |
---|
| 593 | if( strcmp( value, "all" ) == 0 ) |
---|
| 594 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
| 595 | else if( strcmp( value, "rest" ) == 0 ) |
---|
| 596 | icc->type = IRC_CC_TYPE_REST; |
---|
| 597 | else if( strcmp( value, "group" ) == 0 ) |
---|
[13c1a9f] | 598 | icc->type = IRC_CC_TYPE_GROUP; |
---|
[2b8473c] | 599 | else if( strcmp( value, "account" ) == 0 ) |
---|
[a067771] | 600 | icc->type = IRC_CC_TYPE_ACCOUNT; |
---|
[7a6ba50] | 601 | else if( strcmp( value, "protocol" ) == 0 ) |
---|
| 602 | icc->type = IRC_CC_TYPE_PROTOCOL; |
---|
[2b8473c] | 603 | else |
---|
| 604 | return SET_INVALID; |
---|
| 605 | |
---|
[cf1a979] | 606 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
[2b8473c] | 607 | return value; |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | static char *set_eval_by_group( set_t *set, char *value ) |
---|
| 611 | { |
---|
| 612 | struct irc_channel *ic = set->data; |
---|
| 613 | struct irc_control_channel *icc = ic->data; |
---|
[13c1a9f] | 614 | |
---|
[2b8473c] | 615 | icc->group = bee_group_by_name( ic->irc->b, value, TRUE ); |
---|
[cf1a979] | 616 | if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
| 617 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
[7a6ba50] | 618 | |
---|
[2b8473c] | 619 | return g_strdup( icc->group->name ); |
---|
| 620 | } |
---|
| 621 | |
---|
[7a6ba50] | 622 | static char *set_eval_by_protocol( set_t *set, char *value ) |
---|
| 623 | { |
---|
| 624 | struct irc_channel *ic = set->data; |
---|
| 625 | struct irc_control_channel *icc = ic->data; |
---|
| 626 | struct prpl *prpl; |
---|
| 627 | |
---|
| 628 | if( !( prpl = find_protocol( value ) ) ) |
---|
| 629 | return SET_INVALID; |
---|
| 630 | |
---|
| 631 | icc->protocol = prpl; |
---|
| 632 | if( icc->type == IRC_CC_TYPE_PROTOCOL ) |
---|
| 633 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
| 634 | |
---|
| 635 | return value; |
---|
| 636 | } |
---|
| 637 | |
---|
[94d5da9c] | 638 | static char *set_eval_show_users( set_t *set, char *value ) |
---|
| 639 | { |
---|
| 640 | struct irc_channel *ic = set->data; |
---|
| 641 | struct irc_control_channel *icc = ic->data; |
---|
| 642 | char **parts = g_strsplit( value, ",", 0 ), **part; |
---|
| 643 | char modes[4]; |
---|
| 644 | |
---|
| 645 | memset( modes, 0, 4 ); |
---|
| 646 | for( part = parts; *part; part ++ ) |
---|
| 647 | { |
---|
| 648 | char last, modechar = IRC_CHANNEL_USER_NONE; |
---|
| 649 | |
---|
| 650 | if( **part == '\0' ) |
---|
| 651 | goto fail; |
---|
| 652 | |
---|
| 653 | last = (*part)[strlen(*part+1)]; |
---|
| 654 | if( last == '+' ) |
---|
| 655 | modechar = IRC_CHANNEL_USER_VOICE; |
---|
| 656 | else if( last == '%' ) |
---|
| 657 | modechar = IRC_CHANNEL_USER_HALFOP; |
---|
| 658 | else if( last == '@' ) |
---|
| 659 | modechar = IRC_CHANNEL_USER_OP; |
---|
| 660 | |
---|
| 661 | if( strncmp( *part, "offline", 7 ) == 0 ) |
---|
| 662 | modes[0] = modechar; |
---|
| 663 | else if( strncmp( *part, "away", 4 ) == 0 ) |
---|
| 664 | modes[1] = modechar; |
---|
| 665 | else if( strncmp( *part, "online", 6 ) == 0 ) |
---|
| 666 | modes[2] = modechar; |
---|
| 667 | else |
---|
| 668 | goto fail; |
---|
| 669 | } |
---|
| 670 | memcpy( icc->modes, modes, 4 ); |
---|
| 671 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
| 672 | |
---|
| 673 | g_strfreev( parts ); |
---|
| 674 | return value; |
---|
| 675 | |
---|
| 676 | fail: |
---|
| 677 | g_strfreev( parts ); |
---|
| 678 | return SET_INVALID; |
---|
| 679 | } |
---|
| 680 | |
---|
[2b8473c] | 681 | static gboolean control_channel_free( irc_channel_t *ic ) |
---|
| 682 | { |
---|
| 683 | struct irc_control_channel *icc = ic->data; |
---|
| 684 | |
---|
| 685 | set_del( &ic->set, "account" ); |
---|
| 686 | set_del( &ic->set, "fill_by" ); |
---|
| 687 | set_del( &ic->set, "group" ); |
---|
[9052bc1] | 688 | set_del( &ic->set, "protocol" ); |
---|
[2b8473c] | 689 | |
---|
| 690 | g_free( icc ); |
---|
| 691 | ic->data = NULL; |
---|
[13c1a9f] | 692 | |
---|
[9ac3ed1] | 693 | return TRUE; |
---|
| 694 | } |
---|
| 695 | |
---|
[280c56a] | 696 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
| 697 | control_channel_privmsg, |
---|
[c8eeadd] | 698 | control_channel_join, |
---|
[9ac3ed1] | 699 | NULL, |
---|
| 700 | NULL, |
---|
[46d215d] | 701 | control_channel_invite, |
---|
[9ac3ed1] | 702 | |
---|
| 703 | control_channel_init, |
---|
[2b8473c] | 704 | control_channel_free, |
---|
[280c56a] | 705 | }; |
---|