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