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