[81e04e1] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Some glue to put the IRC and the IM stuff together. */ |
---|
| 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" |
---|
[17a6ee9] | 27 | #include "dcc.h" |
---|
[d860a8d] | 28 | |
---|
[e4816ea] | 29 | /* IM->IRC callbacks: Simple IM/buddy-related stuff. */ |
---|
[d860a8d] | 30 | |
---|
[81e04e1] | 31 | static const struct irc_user_funcs irc_user_im_funcs; |
---|
| 32 | |
---|
| 33 | static gboolean bee_irc_user_new( bee_t *bee, bee_user_t *bu ) |
---|
| 34 | { |
---|
| 35 | irc_user_t *iu; |
---|
[92c8d41] | 36 | irc_t *irc = (irc_t*) bee->ui_data; |
---|
[81e04e1] | 37 | char nick[MAX_NICK_LENGTH+1], *s; |
---|
| 38 | |
---|
| 39 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
| 40 | strcpy( nick, nick_get( bu->ic->acc, bu->handle ) ); |
---|
| 41 | |
---|
[92c8d41] | 42 | bu->ui_data = iu = irc_user_new( irc, nick ); |
---|
[d860a8d] | 43 | iu->bu = bu; |
---|
[81e04e1] | 44 | |
---|
| 45 | if( ( s = strchr( bu->handle, '@' ) ) ) |
---|
| 46 | { |
---|
| 47 | iu->host = g_strdup( s + 1 ); |
---|
| 48 | iu->user = g_strndup( bu->handle, s - bu->handle ); |
---|
| 49 | } |
---|
| 50 | else if( bu->ic->acc->server ) |
---|
| 51 | { |
---|
| 52 | iu->host = g_strdup( bu->ic->acc->server ); |
---|
| 53 | iu->user = g_strdup( bu->handle ); |
---|
| 54 | |
---|
| 55 | /* s/ /_/ ... important for AOL screennames */ |
---|
| 56 | for( s = iu->user; *s; s ++ ) |
---|
| 57 | if( *s == ' ' ) |
---|
| 58 | *s = '_'; |
---|
| 59 | } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | iu->host = g_strdup( bu->ic->acc->prpl->name ); |
---|
| 63 | iu->user = g_strdup( bu->handle ); |
---|
| 64 | } |
---|
| 65 | |
---|
[ad404ab] | 66 | if( bu->flags & BEE_USER_LOCAL ) |
---|
| 67 | { |
---|
| 68 | char *s = set_getstr( &bee->set, "handle_unknown" ); |
---|
| 69 | |
---|
| 70 | if( strcmp( s, "add_private" ) == 0 ) |
---|
[92c8d41] | 71 | iu->last_channel = NULL; |
---|
[ad404ab] | 72 | else if( strcmp( s, "add_channel" ) == 0 ) |
---|
[92c8d41] | 73 | iu->last_channel = irc->default_channel; |
---|
[ad404ab] | 74 | } |
---|
| 75 | |
---|
[81e04e1] | 76 | iu->f = &irc_user_im_funcs; |
---|
| 77 | |
---|
| 78 | return TRUE; |
---|
| 79 | } |
---|
| 80 | |
---|
[d860a8d] | 81 | static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu ) |
---|
| 82 | { |
---|
[eabc9d2] | 83 | return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data ); |
---|
[d860a8d] | 84 | } |
---|
[81e04e1] | 85 | |
---|
[d860a8d] | 86 | static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old ) |
---|
| 87 | { |
---|
[231b08b] | 88 | irc_t *irc = bee->ui_data; |
---|
[003a12b] | 89 | irc_user_t *iu = bu->ui_data; |
---|
[231b08b] | 90 | |
---|
[eb50495] | 91 | /* Do this outside the if below since away state can change without |
---|
| 92 | the online state changing. */ |
---|
| 93 | iu->flags &= ~IRC_USER_AWAY; |
---|
| 94 | if( bu->flags & BEE_USER_AWAY || !( bu->flags & BEE_USER_ONLINE ) ) |
---|
| 95 | iu->flags |= IRC_USER_AWAY; |
---|
| 96 | |
---|
[231b08b] | 97 | if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) ) |
---|
| 98 | { |
---|
| 99 | if( bu->flags & BEE_USER_ONLINE ) |
---|
[003a12b] | 100 | { |
---|
| 101 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
| 102 | irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 103 | iu->host, (int) time( NULL ), "logged online" ); |
---|
| 104 | } |
---|
[231b08b] | 105 | else |
---|
[003a12b] | 106 | { |
---|
| 107 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
| 108 | irc_send_num( irc, 601, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 109 | iu->host, (int) time( NULL ), "logged offline" ); |
---|
| 110 | } |
---|
[231b08b] | 111 | } |
---|
| 112 | |
---|
[1c8e5f7] | 113 | /* Reset this one since the info may have changed. */ |
---|
| 114 | iu->away_reply_timeout = 0; |
---|
| 115 | |
---|
[13c1a9f] | 116 | bee_irc_channel_update( irc, NULL, iu ); |
---|
| 117 | |
---|
[d860a8d] | 118 | return TRUE; |
---|
| 119 | } |
---|
[81e04e1] | 120 | |
---|
[13c1a9f] | 121 | void bee_irc_channel_update( irc_t *irc, irc_channel_t *ic, irc_user_t *iu ) |
---|
| 122 | { |
---|
| 123 | struct irc_control_channel *icc; |
---|
| 124 | GSList *l; |
---|
[bc4b469] | 125 | gboolean show = FALSE; |
---|
[13c1a9f] | 126 | |
---|
| 127 | if( ic == NULL ) |
---|
| 128 | { |
---|
| 129 | for( l = irc->channels; l; l = l->next ) |
---|
| 130 | { |
---|
| 131 | ic = l->data; |
---|
| 132 | /* TODO: Just add a type flag or so.. */ |
---|
[9052bc1] | 133 | if( ic->f == irc->default_channel->f && |
---|
| 134 | ( ic->flags & IRC_CHANNEL_JOINED ) ) |
---|
[13c1a9f] | 135 | bee_irc_channel_update( irc, ic, iu ); |
---|
| 136 | } |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | if( iu == NULL ) |
---|
| 140 | { |
---|
| 141 | for( l = irc->users; l; l = l->next ) |
---|
| 142 | { |
---|
| 143 | iu = l->data; |
---|
| 144 | if( iu->bu ) |
---|
| 145 | bee_irc_channel_update( irc, ic, l->data ); |
---|
| 146 | } |
---|
| 147 | return; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | icc = ic->data; |
---|
| 151 | |
---|
| 152 | if( !( iu->bu->flags & BEE_USER_ONLINE ) ) |
---|
| 153 | show = FALSE; |
---|
| 154 | else if( icc->type == IRC_CC_TYPE_DEFAULT ) |
---|
| 155 | show = TRUE; |
---|
| 156 | else if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
| 157 | show = iu->bu->group == icc->group; |
---|
[a067771] | 158 | else if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
| 159 | show = iu->bu->ic->acc == icc->account; |
---|
[7a6ba50] | 160 | else if( icc->type == IRC_CC_TYPE_PROTOCOL ) |
---|
| 161 | show = iu->bu->ic->acc->prpl == icc->protocol; |
---|
[13c1a9f] | 162 | |
---|
| 163 | if( !show ) |
---|
| 164 | { |
---|
[18da20b] | 165 | irc_channel_del_user( ic, iu, FALSE, NULL ); |
---|
[13c1a9f] | 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | irc_channel_add_user( ic, iu ); |
---|
| 170 | |
---|
| 171 | if( set_getbool( &irc->b->set, "away_devoice" ) ) |
---|
| 172 | irc_channel_user_set_mode( ic, iu, ( iu->bu->flags & BEE_USER_AWAY ) ? |
---|
| 173 | 0 : IRC_CHANNEL_USER_VOICE ); |
---|
[0e8b3e8] | 174 | else |
---|
| 175 | irc_channel_user_set_mode( ic, iu, 0 ); |
---|
[13c1a9f] | 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
[f012a9f] | 179 | static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
| 180 | { |
---|
| 181 | irc_t *irc = bee->ui_data; |
---|
| 182 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 183 | char *dst, *prefix = NULL; |
---|
[21c87a7] | 184 | char *wrapped, *ts = NULL; |
---|
| 185 | |
---|
| 186 | if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) ) |
---|
| 187 | ts = irc_format_timestamp( irc, sent_at ); |
---|
[f012a9f] | 188 | |
---|
[92c8d41] | 189 | if( iu->last_channel ) |
---|
[f012a9f] | 190 | { |
---|
[92c8d41] | 191 | dst = iu->last_channel->name; |
---|
| 192 | prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" ); |
---|
[f012a9f] | 193 | } |
---|
| 194 | else |
---|
| 195 | { |
---|
[92c8d41] | 196 | dst = irc->user->nick; |
---|
| 197 | prefix = ts; |
---|
| 198 | ts = NULL; |
---|
[f012a9f] | 199 | } |
---|
| 200 | |
---|
| 201 | wrapped = word_wrap( msg, 425 ); |
---|
| 202 | irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix ); |
---|
| 203 | |
---|
| 204 | g_free( wrapped ); |
---|
| 205 | g_free( prefix ); |
---|
[21c87a7] | 206 | g_free( ts ); |
---|
[f012a9f] | 207 | |
---|
| 208 | return TRUE; |
---|
| 209 | } |
---|
| 210 | |
---|
[573dab0] | 211 | static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags ) |
---|
| 212 | { |
---|
| 213 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 214 | |
---|
| 215 | if( set_getbool( &bee->set, "typing_notice" ) ) |
---|
| 216 | irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick, |
---|
| 217 | "\001TYPING %d\001", ( flags >> 8 ) & 3 ); |
---|
| 218 | else |
---|
| 219 | return FALSE; |
---|
| 220 | |
---|
| 221 | return TRUE; |
---|
| 222 | } |
---|
| 223 | |
---|
[6ef9065] | 224 | static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint ); |
---|
| 225 | |
---|
[1d39159] | 226 | static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu ) |
---|
| 227 | { |
---|
| 228 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 229 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 230 | char *s; |
---|
| 231 | |
---|
| 232 | if( iu->fullname != iu->nick ) |
---|
| 233 | g_free( iu->fullname ); |
---|
| 234 | iu->fullname = g_strdup( bu->fullname ); |
---|
| 235 | |
---|
| 236 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
| 237 | TODO(wilmer): Do the same with away msgs again! */ |
---|
| 238 | for( s = iu->fullname; *s; s ++ ) |
---|
| 239 | if( isspace( *s ) ) *s = ' '; |
---|
| 240 | |
---|
| 241 | if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) ) |
---|
| 242 | { |
---|
| 243 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
| 244 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | s = set_getstr( &bu->ic->acc->set, "nick_source" ); |
---|
| 248 | if( strcmp( s, "handle" ) != 0 ) |
---|
| 249 | { |
---|
| 250 | char *name = g_strdup( bu->fullname ); |
---|
| 251 | |
---|
| 252 | if( strcmp( s, "first_name" ) == 0 ) |
---|
| 253 | { |
---|
| 254 | int i; |
---|
| 255 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
| 256 | name[i] = '\0'; |
---|
| 257 | } |
---|
| 258 | |
---|
[6ef9065] | 259 | bee_irc_user_nick_hint( bee, bu, name ); |
---|
[1d39159] | 260 | |
---|
| 261 | g_free( name ); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | return TRUE; |
---|
| 265 | } |
---|
| 266 | |
---|
[6ef9065] | 267 | static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint ) |
---|
| 268 | { |
---|
| 269 | irc_user_t *iu = bu->ui_data; |
---|
| 270 | char newnick[MAX_NICK_LENGTH+1], *translit; |
---|
| 271 | |
---|
| 272 | if( bu->flags & BEE_USER_ONLINE ) |
---|
| 273 | /* Ignore if the user is visible already. */ |
---|
| 274 | return TRUE; |
---|
| 275 | |
---|
| 276 | if( nick_saved( bu->ic->acc, bu->handle ) ) |
---|
| 277 | /* The user already assigned a nickname to this person. */ |
---|
| 278 | return TRUE; |
---|
| 279 | |
---|
| 280 | /* Credits to Josay_ in #bitlbee for this idea. //TRANSLIT should |
---|
| 281 | do lossy/approximate conversions, so letters with accents don't |
---|
| 282 | just get stripped. Note that it depends on LC_CTYPE being set to |
---|
| 283 | something other than C/POSIX. */ |
---|
| 284 | translit = g_convert( hint, -1, "ASCII//TRANSLIT//IGNORE", "UTF-8", |
---|
| 285 | NULL, NULL, NULL ); |
---|
| 286 | |
---|
| 287 | strncpy( newnick, translit ? : hint, MAX_NICK_LENGTH ); |
---|
| 288 | newnick[MAX_NICK_LENGTH] = 0; |
---|
| 289 | g_free( translit ); |
---|
| 290 | |
---|
| 291 | /* Some processing to make sure this string is a valid IRC nickname. */ |
---|
| 292 | nick_strip( newnick ); |
---|
| 293 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
| 294 | nick_lc( newnick ); |
---|
| 295 | |
---|
| 296 | if( strcmp( iu->nick, newnick ) != 0 ) |
---|
| 297 | { |
---|
| 298 | /* Only do this if newnick is different from the current one. |
---|
| 299 | If rejoining a channel, maybe we got this nick already |
---|
| 300 | (and dedupe would only add an underscore. */ |
---|
| 301 | nick_dedupe( bu->ic->acc, bu->handle, newnick ); |
---|
| 302 | irc_user_set_nick( iu, newnick ); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | return TRUE; |
---|
| 306 | } |
---|
| 307 | |
---|
[7e83e8e4] | 308 | static gboolean bee_irc_user_group( bee_t *bee, bee_user_t *bu ) |
---|
| 309 | { |
---|
| 310 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 311 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 312 | |
---|
| 313 | bee_irc_channel_update( irc, NULL, iu ); |
---|
| 314 | |
---|
| 315 | return TRUE; |
---|
| 316 | } |
---|
| 317 | |
---|
[e4816ea] | 318 | /* IRC->IM calls */ |
---|
| 319 | |
---|
[619dd18] | 320 | static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
| 321 | |
---|
[e4816ea] | 322 | static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 323 | { |
---|
[1c8e5f7] | 324 | const char *away; |
---|
| 325 | |
---|
[619dd18] | 326 | if( iu->bu == NULL ) |
---|
[e4816ea] | 327 | return FALSE; |
---|
[1c8e5f7] | 328 | |
---|
| 329 | if( ( away = irc_user_get_away( iu ) ) && |
---|
| 330 | time( NULL ) >= iu->away_reply_timeout ) |
---|
| 331 | { |
---|
| 332 | irc_send_num( iu->irc, 301, "%s :%s", iu->nick, away ); |
---|
| 333 | iu->away_reply_timeout = time( NULL ) + |
---|
| 334 | set_getint( &iu->irc->b->set, "away_reply_timeout" ); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | if( set_getbool( &iu->irc->b->set, "paste_buffer" ) ) |
---|
[619dd18] | 338 | { |
---|
| 339 | int delay; |
---|
| 340 | |
---|
| 341 | if( iu->pastebuf == NULL ) |
---|
| 342 | iu->pastebuf = g_string_new( msg ); |
---|
| 343 | else |
---|
| 344 | { |
---|
| 345 | b_event_remove( iu->pastebuf_timer ); |
---|
| 346 | g_string_append_printf( iu->pastebuf, "\n%s", msg ); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | if( ( delay = set_getint( &iu->irc->b->set, "paste_buffer_delay" ) ) <= 5 ) |
---|
| 350 | delay *= 1000; |
---|
| 351 | |
---|
| 352 | iu->pastebuf_timer = b_timeout_add( delay, bee_irc_user_privmsg_cb, iu ); |
---|
| 353 | |
---|
| 354 | return TRUE; |
---|
| 355 | } |
---|
| 356 | else |
---|
| 357 | return bee_user_msg( iu->irc->b, iu->bu, msg, 0 ); |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond ) |
---|
| 361 | { |
---|
| 362 | irc_user_t *iu = data; |
---|
| 363 | |
---|
| 364 | bee_user_msg( iu->irc->b, iu->bu, iu->pastebuf->str, 0 ); |
---|
| 365 | |
---|
| 366 | g_string_free( iu->pastebuf, TRUE ); |
---|
| 367 | iu->pastebuf = 0; |
---|
| 368 | iu->pastebuf_timer = 0; |
---|
| 369 | |
---|
| 370 | return FALSE; |
---|
[e4816ea] | 371 | } |
---|
| 372 | |
---|
| 373 | static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp ) |
---|
| 374 | { |
---|
| 375 | if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0 |
---|
| 376 | && g_strcasecmp( ctcp[1], "SEND" ) == 0 ) |
---|
| 377 | { |
---|
| 378 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request ) |
---|
| 379 | { |
---|
| 380 | file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp ); |
---|
| 381 | if ( ft ) |
---|
| 382 | iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle ); |
---|
| 383 | |
---|
| 384 | return TRUE; |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 ) |
---|
| 388 | { |
---|
| 389 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] ) |
---|
| 390 | { |
---|
| 391 | int st = ctcp[1][0]; |
---|
| 392 | if( st >= '0' && st <= '2' ) |
---|
| 393 | { |
---|
| 394 | st <<= 8; |
---|
| 395 | iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st ); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | return TRUE; |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | return FALSE; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
| 406 | bee_irc_user_privmsg, |
---|
| 407 | bee_irc_user_ctcp, |
---|
| 408 | }; |
---|
| 409 | |
---|
[aea8b68] | 410 | |
---|
[e4816ea] | 411 | /* IM->IRC: Groupchats */ |
---|
[5a75d15] | 412 | const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
[a87754b] | 413 | |
---|
| 414 | static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c ) |
---|
[aea8b68] | 415 | { |
---|
| 416 | irc_t *irc = bee->ui_data; |
---|
| 417 | irc_channel_t *ic; |
---|
| 418 | char *topic; |
---|
[eb37735] | 419 | GSList *l; |
---|
[aea8b68] | 420 | int i; |
---|
| 421 | |
---|
[eb37735] | 422 | /* Try to find a channel that expects to receive a groupchat. |
---|
[52a2521] | 423 | This flag is set earlier in our current call trace. */ |
---|
[eb37735] | 424 | for( l = irc->channels; l; l = l->next ) |
---|
| 425 | { |
---|
| 426 | ic = l->data; |
---|
| 427 | if( ic->flags & IRC_CHANNEL_CHAT_PICKME ) |
---|
| 428 | break; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | /* If we found none, just generate some stupid name. */ |
---|
| 432 | if( l == NULL ) for( i = 0; i <= 999; i ++ ) |
---|
[aea8b68] | 433 | { |
---|
| 434 | char name[16]; |
---|
[5a75d15] | 435 | sprintf( name, "#chat_%03d", i ); |
---|
[aea8b68] | 436 | if( ( ic = irc_channel_new( irc, name ) ) ) |
---|
| 437 | break; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | if( ic == NULL ) |
---|
| 441 | return FALSE; |
---|
| 442 | |
---|
| 443 | c->ui_data = ic; |
---|
| 444 | ic->data = c; |
---|
| 445 | |
---|
| 446 | topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
| 447 | irc_channel_set_topic( ic, topic, irc->root ); |
---|
| 448 | g_free( topic ); |
---|
| 449 | |
---|
| 450 | return TRUE; |
---|
| 451 | } |
---|
| 452 | |
---|
[a87754b] | 453 | static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c ) |
---|
[aea8b68] | 454 | { |
---|
| 455 | irc_channel_t *ic = c->ui_data; |
---|
| 456 | |
---|
| 457 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 458 | irc_channel_printf( ic, "Cleaning up channel, bye!" ); |
---|
| 459 | |
---|
[5a75d15] | 460 | ic->data = NULL; |
---|
[1c40aa7] | 461 | irc_channel_del_user( ic, ic->irc->user, FALSE, "Chatroom closed by server" ); |
---|
[aea8b68] | 462 | |
---|
| 463 | return TRUE; |
---|
| 464 | } |
---|
| 465 | |
---|
[a87754b] | 466 | static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text ) |
---|
[aea8b68] | 467 | { |
---|
[27e2c66] | 468 | irc_channel_t *ic = c->ui_data; |
---|
| 469 | |
---|
| 470 | irc_channel_printf( ic, "%s", text ); |
---|
[b17ce85] | 471 | |
---|
| 472 | return TRUE; |
---|
[aea8b68] | 473 | } |
---|
| 474 | |
---|
[a87754b] | 475 | static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
[aea8b68] | 476 | { |
---|
[27e2c66] | 477 | irc_t *irc = bee->ui_data; |
---|
| 478 | irc_user_t *iu = bu->ui_data; |
---|
| 479 | irc_channel_t *ic = c->ui_data; |
---|
| 480 | char *ts = NULL; |
---|
| 481 | |
---|
| 482 | if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) ) |
---|
| 483 | ts = irc_format_timestamp( irc, sent_at ); |
---|
| 484 | |
---|
| 485 | irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts ); |
---|
| 486 | g_free( ts ); |
---|
| 487 | |
---|
| 488 | return TRUE; |
---|
[aea8b68] | 489 | } |
---|
| 490 | |
---|
[a87754b] | 491 | static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
[aea8b68] | 492 | { |
---|
| 493 | irc_t *irc = bee->ui_data; |
---|
| 494 | |
---|
| 495 | irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
[b17ce85] | 496 | |
---|
| 497 | return TRUE; |
---|
[aea8b68] | 498 | } |
---|
| 499 | |
---|
[a87754b] | 500 | static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
[aea8b68] | 501 | { |
---|
[b17ce85] | 502 | irc_t *irc = bee->ui_data; |
---|
| 503 | |
---|
[1c40aa7] | 504 | /* TODO: Possible bug here: If a module removes $user here instead of just |
---|
| 505 | using imcb_chat_free() and the channel was IRC_CHANNEL_TEMP, we get into |
---|
| 506 | a broken state around here. */ |
---|
[18da20b] | 507 | irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data, FALSE, NULL ); |
---|
[b17ce85] | 508 | |
---|
| 509 | return TRUE; |
---|
[aea8b68] | 510 | } |
---|
| 511 | |
---|
[9e27f18] | 512 | static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu ) |
---|
| 513 | { |
---|
| 514 | irc_t *irc = bee->ui_data; |
---|
| 515 | irc_user_t *iu; |
---|
| 516 | |
---|
| 517 | if( bu == NULL ) |
---|
| 518 | iu = irc->root; |
---|
| 519 | else if( bu == bee->user ) |
---|
| 520 | iu = irc->user; |
---|
| 521 | else |
---|
| 522 | iu = bu->ui_data; |
---|
| 523 | |
---|
| 524 | irc_channel_set_topic( c->ui_data, new, iu ); |
---|
| 525 | |
---|
| 526 | return TRUE; |
---|
| 527 | } |
---|
| 528 | |
---|
[d343eaa] | 529 | static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name ) |
---|
| 530 | { |
---|
| 531 | irc_t *irc = bee->ui_data; |
---|
[52a2521] | 532 | irc_channel_t *ic = c->ui_data, *oic; |
---|
[d343eaa] | 533 | char stripped[MAX_NICK_LENGTH+1], *full_name; |
---|
| 534 | |
---|
| 535 | /* Don't rename a channel if the user's in it already. */ |
---|
| 536 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 537 | return FALSE; |
---|
| 538 | |
---|
| 539 | strncpy( stripped, name, MAX_NICK_LENGTH ); |
---|
| 540 | stripped[MAX_NICK_LENGTH] = '\0'; |
---|
[134a02c] | 541 | irc_channel_name_strip( stripped ); |
---|
[d343eaa] | 542 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
| 543 | nick_lc( stripped ); |
---|
| 544 | |
---|
[52a2521] | 545 | if( stripped[0] == '\0' ) |
---|
| 546 | return FALSE; |
---|
[d343eaa] | 547 | |
---|
[52a2521] | 548 | full_name = g_strdup_printf( "#%s", stripped ); |
---|
| 549 | if( ( oic = irc_channel_by_name( irc, full_name ) ) ) |
---|
[d343eaa] | 550 | { |
---|
[52a2521] | 551 | char *type, *chat_type; |
---|
| 552 | |
---|
| 553 | type = set_getstr( &oic->set, "type" ); |
---|
| 554 | chat_type = set_getstr( &oic->set, "chat_type" ); |
---|
| 555 | |
---|
| 556 | if( type && chat_type && oic->data == FALSE && |
---|
| 557 | strcmp( type, "chat" ) == 0 && |
---|
| 558 | strcmp( chat_type, "groupchat" ) == 0 ) |
---|
| 559 | { |
---|
| 560 | /* There's a channel with this name already, but it looks |
---|
| 561 | like it's not in use yet. Most likely the IRC client |
---|
| 562 | rejoined the channel after a reconnect. Remove it so |
---|
| 563 | we can reuse its name. */ |
---|
| 564 | irc_channel_free( oic ); |
---|
| 565 | } |
---|
| 566 | else |
---|
| 567 | { |
---|
| 568 | g_free( full_name ); |
---|
| 569 | return FALSE; |
---|
| 570 | } |
---|
[d343eaa] | 571 | } |
---|
| 572 | |
---|
[52a2521] | 573 | g_free( ic->name ); |
---|
| 574 | ic->name = full_name; |
---|
| 575 | |
---|
[d343eaa] | 576 | return TRUE; |
---|
| 577 | } |
---|
| 578 | |
---|
[a87754b] | 579 | /* IRC->IM */ |
---|
[619dd18] | 580 | static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
| 581 | |
---|
[a87754b] | 582 | static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg ) |
---|
| 583 | { |
---|
| 584 | struct groupchat *c = ic->data; |
---|
| 585 | |
---|
[5a75d15] | 586 | if( c == NULL ) |
---|
| 587 | return FALSE; |
---|
[619dd18] | 588 | else if( set_getbool( &ic->irc->b->set, "paste_buffer" ) ) |
---|
| 589 | { |
---|
| 590 | int delay; |
---|
| 591 | |
---|
| 592 | if( ic->pastebuf == NULL ) |
---|
| 593 | ic->pastebuf = g_string_new( msg ); |
---|
| 594 | else |
---|
| 595 | { |
---|
| 596 | b_event_remove( ic->pastebuf_timer ); |
---|
| 597 | g_string_append_printf( ic->pastebuf, "\n%s", msg ); |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | if( ( delay = set_getint( &ic->irc->b->set, "paste_buffer_delay" ) ) <= 5 ) |
---|
| 601 | delay *= 1000; |
---|
| 602 | |
---|
| 603 | ic->pastebuf_timer = b_timeout_add( delay, bee_irc_channel_chat_privmsg_cb, ic ); |
---|
| 604 | |
---|
| 605 | return TRUE; |
---|
| 606 | } |
---|
| 607 | else |
---|
| 608 | bee_chat_msg( ic->irc->b, c, msg, 0 ); |
---|
[a87754b] | 609 | |
---|
| 610 | return TRUE; |
---|
[5a75d15] | 611 | } |
---|
| 612 | |
---|
[619dd18] | 613 | static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond ) |
---|
| 614 | { |
---|
| 615 | irc_channel_t *ic = data; |
---|
| 616 | |
---|
| 617 | bee_chat_msg( ic->irc->b, ic->data, ic->pastebuf->str, 0 ); |
---|
| 618 | |
---|
| 619 | g_string_free( ic->pastebuf, TRUE ); |
---|
| 620 | ic->pastebuf = 0; |
---|
| 621 | ic->pastebuf_timer = 0; |
---|
| 622 | |
---|
| 623 | return FALSE; |
---|
| 624 | } |
---|
| 625 | |
---|
[5a75d15] | 626 | static gboolean bee_irc_channel_chat_join( irc_channel_t *ic ) |
---|
| 627 | { |
---|
| 628 | char *acc_s, *room; |
---|
| 629 | account_t *acc; |
---|
[a87754b] | 630 | |
---|
[5a75d15] | 631 | if( strcmp( set_getstr( &ic->set, "chat_type" ), "room" ) != 0 ) |
---|
| 632 | return TRUE; |
---|
| 633 | |
---|
| 634 | if( ( acc_s = set_getstr( &ic->set, "account" ) ) && |
---|
| 635 | ( room = set_getstr( &ic->set, "room" ) ) && |
---|
| 636 | ( acc = account_get( ic->irc->b, acc_s ) ) && |
---|
| 637 | acc->ic && acc->prpl->chat_join ) |
---|
| 638 | { |
---|
| 639 | char *nick; |
---|
| 640 | |
---|
| 641 | if( !( nick = set_getstr( &ic->set, "nick" ) ) ) |
---|
| 642 | nick = ic->irc->user->nick; |
---|
| 643 | |
---|
| 644 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
| 645 | acc->prpl->chat_join( acc->ic, room, nick, NULL ); |
---|
| 646 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
| 647 | |
---|
| 648 | return FALSE; |
---|
| 649 | } |
---|
| 650 | else |
---|
| 651 | { |
---|
| 652 | irc_send_num( ic->irc, 403, "%s :Can't join channel, account offline?", ic->name ); |
---|
| 653 | return FALSE; |
---|
| 654 | } |
---|
[a87754b] | 655 | } |
---|
| 656 | |
---|
[bfb99ee] | 657 | static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg ) |
---|
| 658 | { |
---|
| 659 | struct groupchat *c = ic->data; |
---|
| 660 | |
---|
[5a75d15] | 661 | if( c && c->ic->acc->prpl->chat_leave ) |
---|
[bfb99ee] | 662 | c->ic->acc->prpl->chat_leave( c ); |
---|
| 663 | |
---|
| 664 | return TRUE; |
---|
| 665 | } |
---|
| 666 | |
---|
[9e27f18] | 667 | static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new ) |
---|
| 668 | { |
---|
[4469e7e] | 669 | struct groupchat *c = ic->data; |
---|
[5a75d15] | 670 | |
---|
| 671 | if( c == NULL ) |
---|
| 672 | return FALSE; |
---|
[4469e7e] | 673 | |
---|
| 674 | if( c->ic->acc->prpl->chat_topic == NULL ) |
---|
| 675 | irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name ); |
---|
| 676 | else |
---|
| 677 | { |
---|
[5a75d15] | 678 | /* TODO: Need more const goodness here, sigh */ |
---|
| 679 | char *topic = g_strdup( new ); |
---|
[4469e7e] | 680 | c->ic->acc->prpl->chat_topic( c, topic ); |
---|
[5a75d15] | 681 | g_free( topic ); |
---|
[4469e7e] | 682 | return TRUE; |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | return FALSE; |
---|
[9e27f18] | 686 | } |
---|
| 687 | |
---|
[66b9e36a] | 688 | static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 689 | { |
---|
| 690 | struct groupchat *c = ic->data; |
---|
[5a75d15] | 691 | bee_user_t *bu = iu->bu; |
---|
| 692 | |
---|
| 693 | if( bu == NULL ) |
---|
| 694 | return FALSE; |
---|
[66b9e36a] | 695 | |
---|
[5a75d15] | 696 | if( c ) |
---|
| 697 | { |
---|
| 698 | if( iu->bu->ic != c->ic ) |
---|
| 699 | irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name ); |
---|
| 700 | else if( c->ic->acc->prpl->chat_invite ) |
---|
| 701 | c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL ); |
---|
| 702 | else |
---|
| 703 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
| 704 | } |
---|
| 705 | else if( bu->ic->acc->prpl->chat_with && |
---|
| 706 | strcmp( set_getstr( &ic->set, "chat_type" ), "groupchat" ) == 0 ) |
---|
| 707 | { |
---|
| 708 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
| 709 | iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle ); |
---|
| 710 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
| 711 | } |
---|
[66b9e36a] | 712 | else |
---|
[5a75d15] | 713 | { |
---|
[66b9e36a] | 714 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
[5a75d15] | 715 | } |
---|
[66b9e36a] | 716 | |
---|
| 717 | return TRUE; |
---|
| 718 | } |
---|
| 719 | |
---|
[5a75d15] | 720 | static char *set_eval_room_account( set_t *set, char *value ); |
---|
[1c40aa7] | 721 | static char *set_eval_chat_type( set_t *set, char *value ); |
---|
[5a75d15] | 722 | |
---|
| 723 | static gboolean bee_irc_channel_init( irc_channel_t *ic ) |
---|
| 724 | { |
---|
| 725 | set_add( &ic->set, "account", NULL, set_eval_room_account, ic ); |
---|
[1c40aa7] | 726 | set_add( &ic->set, "chat_type", "groupchat", set_eval_chat_type, ic ); |
---|
[5a75d15] | 727 | set_add( &ic->set, "nick", NULL, NULL, ic ); |
---|
| 728 | set_add( &ic->set, "room", NULL, NULL, ic ); |
---|
| 729 | |
---|
[1c40aa7] | 730 | /* chat_type == groupchat */ |
---|
| 731 | ic->flags |= IRC_CHANNEL_TEMP; |
---|
| 732 | |
---|
[5a75d15] | 733 | return TRUE; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | static char *set_eval_room_account( set_t *set, char *value ) |
---|
| 737 | { |
---|
| 738 | struct irc_channel *ic = set->data; |
---|
| 739 | account_t *acc; |
---|
| 740 | |
---|
| 741 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
| 742 | return SET_INVALID; |
---|
| 743 | else if( !acc->prpl->chat_join ) |
---|
| 744 | { |
---|
| 745 | irc_usermsg( ic->irc, "Named chatrooms not supported on that account." ); |
---|
| 746 | return SET_INVALID; |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
| 750 | } |
---|
| 751 | |
---|
[1c40aa7] | 752 | static char *set_eval_chat_type( set_t *set, char *value ) |
---|
| 753 | { |
---|
| 754 | struct irc_channel *ic = set->data; |
---|
| 755 | |
---|
| 756 | if( strcmp( value, "groupchat" ) == 0 ) |
---|
| 757 | ic->flags |= IRC_CHANNEL_TEMP; |
---|
| 758 | else if( strcmp( value, "room" ) == 0 ) |
---|
| 759 | ic->flags &= ~IRC_CHANNEL_TEMP; |
---|
| 760 | else |
---|
| 761 | return NULL; |
---|
| 762 | |
---|
| 763 | return value; |
---|
| 764 | } |
---|
| 765 | |
---|
[5a75d15] | 766 | static gboolean bee_irc_channel_free( irc_channel_t *ic ) |
---|
| 767 | { |
---|
| 768 | set_del( &ic->set, "account" ); |
---|
| 769 | set_del( &ic->set, "chat_type" ); |
---|
| 770 | set_del( &ic->set, "nick" ); |
---|
| 771 | set_del( &ic->set, "room" ); |
---|
| 772 | |
---|
[1c40aa7] | 773 | ic->flags &= ~IRC_CHANNEL_TEMP; |
---|
| 774 | |
---|
[5a75d15] | 775 | return TRUE; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | const struct irc_channel_funcs irc_channel_im_chat_funcs = { |
---|
[a87754b] | 779 | bee_irc_channel_chat_privmsg, |
---|
[5a75d15] | 780 | bee_irc_channel_chat_join, |
---|
[bfb99ee] | 781 | bee_irc_channel_chat_part, |
---|
[9e27f18] | 782 | bee_irc_channel_chat_topic, |
---|
[66b9e36a] | 783 | bee_irc_channel_chat_invite, |
---|
[5a75d15] | 784 | |
---|
| 785 | bee_irc_channel_init, |
---|
| 786 | bee_irc_channel_free, |
---|
[a87754b] | 787 | }; |
---|
| 788 | |
---|
[aea8b68] | 789 | |
---|
[e4816ea] | 790 | /* IM->IRC: File transfers */ |
---|
[17a6ee9] | 791 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
| 792 | { |
---|
| 793 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
| 794 | } |
---|
| 795 | |
---|
[a87754b] | 796 | static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
[17a6ee9] | 797 | { |
---|
| 798 | return dccs_recv_start( ft ); |
---|
| 799 | } |
---|
| 800 | |
---|
[a87754b] | 801 | static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
[17a6ee9] | 802 | { |
---|
| 803 | return dcc_close( ft ); |
---|
| 804 | } |
---|
| 805 | |
---|
[a87754b] | 806 | static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
[17a6ee9] | 807 | { |
---|
| 808 | dcc_file_transfer_t *df = file->priv; |
---|
| 809 | |
---|
| 810 | if( file->bytes_transferred >= file->file_size ) |
---|
| 811 | dcc_finish( file ); |
---|
| 812 | else |
---|
| 813 | df->proto_finished = TRUE; |
---|
| 814 | } |
---|
| 815 | |
---|
[d860a8d] | 816 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
[81e04e1] | 817 | bee_irc_user_new, |
---|
[d860a8d] | 818 | bee_irc_user_free, |
---|
[1d39159] | 819 | bee_irc_user_fullname, |
---|
[6ef9065] | 820 | bee_irc_user_nick_hint, |
---|
[7e83e8e4] | 821 | bee_irc_user_group, |
---|
[d860a8d] | 822 | bee_irc_user_status, |
---|
[f012a9f] | 823 | bee_irc_user_msg, |
---|
[573dab0] | 824 | bee_irc_user_typing, |
---|
[17a6ee9] | 825 | |
---|
[aea8b68] | 826 | bee_irc_chat_new, |
---|
| 827 | bee_irc_chat_free, |
---|
[27e2c66] | 828 | bee_irc_chat_log, |
---|
| 829 | bee_irc_chat_msg, |
---|
[aea8b68] | 830 | bee_irc_chat_add_user, |
---|
[b17ce85] | 831 | bee_irc_chat_remove_user, |
---|
[9e27f18] | 832 | bee_irc_chat_topic, |
---|
[d343eaa] | 833 | bee_irc_chat_name_hint, |
---|
[aea8b68] | 834 | |
---|
[17a6ee9] | 835 | bee_irc_ft_in_start, |
---|
| 836 | bee_irc_ft_out_start, |
---|
| 837 | bee_irc_ft_close, |
---|
| 838 | bee_irc_ft_finished, |
---|
[81e04e1] | 839 | }; |
---|