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