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