[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 | /* irc_channel_free( ic ); */ |
---|
| 458 | |
---|
[18da20b] | 459 | irc_channel_del_user( ic, ic->irc->user, FALSE, "Chatroom closed by server" ); |
---|
[5a75d15] | 460 | ic->data = NULL; |
---|
[aea8b68] | 461 | |
---|
| 462 | return TRUE; |
---|
| 463 | } |
---|
| 464 | |
---|
[a87754b] | 465 | static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text ) |
---|
[aea8b68] | 466 | { |
---|
[27e2c66] | 467 | irc_channel_t *ic = c->ui_data; |
---|
| 468 | |
---|
| 469 | irc_channel_printf( ic, "%s", text ); |
---|
[b17ce85] | 470 | |
---|
| 471 | return TRUE; |
---|
[aea8b68] | 472 | } |
---|
| 473 | |
---|
[a87754b] | 474 | static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
[aea8b68] | 475 | { |
---|
[27e2c66] | 476 | irc_t *irc = bee->ui_data; |
---|
| 477 | irc_user_t *iu = bu->ui_data; |
---|
| 478 | irc_channel_t *ic = c->ui_data; |
---|
| 479 | char *ts = NULL; |
---|
| 480 | |
---|
| 481 | if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) ) |
---|
| 482 | ts = irc_format_timestamp( irc, sent_at ); |
---|
| 483 | |
---|
| 484 | irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts ); |
---|
| 485 | g_free( ts ); |
---|
| 486 | |
---|
| 487 | return TRUE; |
---|
[aea8b68] | 488 | } |
---|
| 489 | |
---|
[a87754b] | 490 | static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
[aea8b68] | 491 | { |
---|
| 492 | irc_t *irc = bee->ui_data; |
---|
| 493 | |
---|
| 494 | irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
[b17ce85] | 495 | |
---|
| 496 | return TRUE; |
---|
[aea8b68] | 497 | } |
---|
| 498 | |
---|
[a87754b] | 499 | static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
[aea8b68] | 500 | { |
---|
[b17ce85] | 501 | irc_t *irc = bee->ui_data; |
---|
| 502 | |
---|
[18da20b] | 503 | irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data, FALSE, NULL ); |
---|
[b17ce85] | 504 | |
---|
| 505 | return TRUE; |
---|
[aea8b68] | 506 | } |
---|
| 507 | |
---|
[9e27f18] | 508 | static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu ) |
---|
| 509 | { |
---|
| 510 | irc_t *irc = bee->ui_data; |
---|
| 511 | irc_user_t *iu; |
---|
| 512 | |
---|
| 513 | if( bu == NULL ) |
---|
| 514 | iu = irc->root; |
---|
| 515 | else if( bu == bee->user ) |
---|
| 516 | iu = irc->user; |
---|
| 517 | else |
---|
| 518 | iu = bu->ui_data; |
---|
| 519 | |
---|
| 520 | irc_channel_set_topic( c->ui_data, new, iu ); |
---|
| 521 | |
---|
| 522 | return TRUE; |
---|
| 523 | } |
---|
| 524 | |
---|
[d343eaa] | 525 | static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name ) |
---|
| 526 | { |
---|
| 527 | irc_t *irc = bee->ui_data; |
---|
| 528 | irc_channel_t *ic = c->ui_data; |
---|
| 529 | char stripped[MAX_NICK_LENGTH+1], *full_name; |
---|
| 530 | |
---|
| 531 | /* Don't rename a channel if the user's in it already. */ |
---|
| 532 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 533 | return FALSE; |
---|
| 534 | |
---|
| 535 | strncpy( stripped, name, MAX_NICK_LENGTH ); |
---|
| 536 | stripped[MAX_NICK_LENGTH] = '\0'; |
---|
| 537 | nick_strip( stripped ); |
---|
| 538 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
| 539 | nick_lc( stripped ); |
---|
| 540 | |
---|
| 541 | full_name = g_strdup_printf( "&%s", stripped ); |
---|
| 542 | |
---|
| 543 | if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL ) |
---|
| 544 | { |
---|
| 545 | g_free( ic->name ); |
---|
| 546 | ic->name = full_name; |
---|
| 547 | } |
---|
| 548 | else |
---|
| 549 | { |
---|
| 550 | g_free( full_name ); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | return TRUE; |
---|
| 554 | } |
---|
| 555 | |
---|
[a87754b] | 556 | /* IRC->IM */ |
---|
[619dd18] | 557 | static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond ); |
---|
| 558 | |
---|
[a87754b] | 559 | static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg ) |
---|
| 560 | { |
---|
| 561 | struct groupchat *c = ic->data; |
---|
| 562 | |
---|
[5a75d15] | 563 | if( c == NULL ) |
---|
| 564 | return FALSE; |
---|
[619dd18] | 565 | else if( set_getbool( &ic->irc->b->set, "paste_buffer" ) ) |
---|
| 566 | { |
---|
| 567 | int delay; |
---|
| 568 | |
---|
| 569 | if( ic->pastebuf == NULL ) |
---|
| 570 | ic->pastebuf = g_string_new( msg ); |
---|
| 571 | else |
---|
| 572 | { |
---|
| 573 | b_event_remove( ic->pastebuf_timer ); |
---|
| 574 | g_string_append_printf( ic->pastebuf, "\n%s", msg ); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | if( ( delay = set_getint( &ic->irc->b->set, "paste_buffer_delay" ) ) <= 5 ) |
---|
| 578 | delay *= 1000; |
---|
| 579 | |
---|
| 580 | ic->pastebuf_timer = b_timeout_add( delay, bee_irc_channel_chat_privmsg_cb, ic ); |
---|
| 581 | |
---|
| 582 | return TRUE; |
---|
| 583 | } |
---|
| 584 | else |
---|
| 585 | bee_chat_msg( ic->irc->b, c, msg, 0 ); |
---|
[a87754b] | 586 | |
---|
| 587 | return TRUE; |
---|
[5a75d15] | 588 | } |
---|
| 589 | |
---|
[619dd18] | 590 | static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond ) |
---|
| 591 | { |
---|
| 592 | irc_channel_t *ic = data; |
---|
| 593 | |
---|
| 594 | bee_chat_msg( ic->irc->b, ic->data, ic->pastebuf->str, 0 ); |
---|
| 595 | |
---|
| 596 | g_string_free( ic->pastebuf, TRUE ); |
---|
| 597 | ic->pastebuf = 0; |
---|
| 598 | ic->pastebuf_timer = 0; |
---|
| 599 | |
---|
| 600 | return FALSE; |
---|
| 601 | } |
---|
| 602 | |
---|
[5a75d15] | 603 | static gboolean bee_irc_channel_chat_join( irc_channel_t *ic ) |
---|
| 604 | { |
---|
| 605 | char *acc_s, *room; |
---|
| 606 | account_t *acc; |
---|
[a87754b] | 607 | |
---|
[5a75d15] | 608 | if( strcmp( set_getstr( &ic->set, "chat_type" ), "room" ) != 0 ) |
---|
| 609 | return TRUE; |
---|
| 610 | |
---|
| 611 | if( ( acc_s = set_getstr( &ic->set, "account" ) ) && |
---|
| 612 | ( room = set_getstr( &ic->set, "room" ) ) && |
---|
| 613 | ( acc = account_get( ic->irc->b, acc_s ) ) && |
---|
| 614 | acc->ic && acc->prpl->chat_join ) |
---|
| 615 | { |
---|
| 616 | char *nick; |
---|
| 617 | |
---|
| 618 | if( !( nick = set_getstr( &ic->set, "nick" ) ) ) |
---|
| 619 | nick = ic->irc->user->nick; |
---|
| 620 | |
---|
| 621 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
| 622 | acc->prpl->chat_join( acc->ic, room, nick, NULL ); |
---|
| 623 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
| 624 | |
---|
| 625 | return FALSE; |
---|
| 626 | } |
---|
| 627 | else |
---|
| 628 | { |
---|
| 629 | irc_send_num( ic->irc, 403, "%s :Can't join channel, account offline?", ic->name ); |
---|
| 630 | return FALSE; |
---|
| 631 | } |
---|
[a87754b] | 632 | } |
---|
| 633 | |
---|
[bfb99ee] | 634 | static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg ) |
---|
| 635 | { |
---|
| 636 | struct groupchat *c = ic->data; |
---|
| 637 | |
---|
[5a75d15] | 638 | if( c && c->ic->acc->prpl->chat_leave ) |
---|
[bfb99ee] | 639 | c->ic->acc->prpl->chat_leave( c ); |
---|
| 640 | |
---|
| 641 | return TRUE; |
---|
| 642 | } |
---|
| 643 | |
---|
[9e27f18] | 644 | static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new ) |
---|
| 645 | { |
---|
[4469e7e] | 646 | struct groupchat *c = ic->data; |
---|
[5a75d15] | 647 | |
---|
| 648 | if( c == NULL ) |
---|
| 649 | return FALSE; |
---|
[4469e7e] | 650 | |
---|
| 651 | if( c->ic->acc->prpl->chat_topic == NULL ) |
---|
| 652 | irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name ); |
---|
| 653 | else |
---|
| 654 | { |
---|
[5a75d15] | 655 | /* TODO: Need more const goodness here, sigh */ |
---|
| 656 | char *topic = g_strdup( new ); |
---|
[4469e7e] | 657 | c->ic->acc->prpl->chat_topic( c, topic ); |
---|
[5a75d15] | 658 | g_free( topic ); |
---|
[4469e7e] | 659 | return TRUE; |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | return FALSE; |
---|
[9e27f18] | 663 | } |
---|
| 664 | |
---|
[66b9e36a] | 665 | static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 666 | { |
---|
| 667 | struct groupchat *c = ic->data; |
---|
[5a75d15] | 668 | bee_user_t *bu = iu->bu; |
---|
| 669 | |
---|
| 670 | if( bu == NULL ) |
---|
| 671 | return FALSE; |
---|
[66b9e36a] | 672 | |
---|
[5a75d15] | 673 | if( c ) |
---|
| 674 | { |
---|
| 675 | if( iu->bu->ic != c->ic ) |
---|
| 676 | irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name ); |
---|
| 677 | else if( c->ic->acc->prpl->chat_invite ) |
---|
| 678 | c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL ); |
---|
| 679 | else |
---|
| 680 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
| 681 | } |
---|
| 682 | else if( bu->ic->acc->prpl->chat_with && |
---|
| 683 | strcmp( set_getstr( &ic->set, "chat_type" ), "groupchat" ) == 0 ) |
---|
| 684 | { |
---|
| 685 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
| 686 | iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle ); |
---|
| 687 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
| 688 | } |
---|
[66b9e36a] | 689 | else |
---|
[5a75d15] | 690 | { |
---|
[66b9e36a] | 691 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
[5a75d15] | 692 | } |
---|
[66b9e36a] | 693 | |
---|
| 694 | return TRUE; |
---|
| 695 | } |
---|
| 696 | |
---|
[5a75d15] | 697 | static char *set_eval_room_account( set_t *set, char *value ); |
---|
| 698 | |
---|
| 699 | static gboolean bee_irc_channel_init( irc_channel_t *ic ) |
---|
| 700 | { |
---|
| 701 | set_add( &ic->set, "account", NULL, set_eval_room_account, ic ); |
---|
| 702 | set_add( &ic->set, "chat_type", "groupchat", NULL, ic ); |
---|
| 703 | set_add( &ic->set, "nick", NULL, NULL, ic ); |
---|
| 704 | set_add( &ic->set, "room", NULL, NULL, ic ); |
---|
| 705 | |
---|
| 706 | return TRUE; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | static char *set_eval_room_account( set_t *set, char *value ) |
---|
| 710 | { |
---|
| 711 | struct irc_channel *ic = set->data; |
---|
| 712 | account_t *acc; |
---|
| 713 | |
---|
| 714 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
| 715 | return SET_INVALID; |
---|
| 716 | else if( !acc->prpl->chat_join ) |
---|
| 717 | { |
---|
| 718 | irc_usermsg( ic->irc, "Named chatrooms not supported on that account." ); |
---|
| 719 | return SET_INVALID; |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | static gboolean bee_irc_channel_free( irc_channel_t *ic ) |
---|
| 726 | { |
---|
| 727 | set_del( &ic->set, "account" ); |
---|
| 728 | set_del( &ic->set, "chat_type" ); |
---|
| 729 | set_del( &ic->set, "nick" ); |
---|
| 730 | set_del( &ic->set, "room" ); |
---|
| 731 | |
---|
| 732 | return TRUE; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | const struct irc_channel_funcs irc_channel_im_chat_funcs = { |
---|
[a87754b] | 736 | bee_irc_channel_chat_privmsg, |
---|
[5a75d15] | 737 | bee_irc_channel_chat_join, |
---|
[bfb99ee] | 738 | bee_irc_channel_chat_part, |
---|
[9e27f18] | 739 | bee_irc_channel_chat_topic, |
---|
[66b9e36a] | 740 | bee_irc_channel_chat_invite, |
---|
[5a75d15] | 741 | |
---|
| 742 | bee_irc_channel_init, |
---|
| 743 | bee_irc_channel_free, |
---|
[a87754b] | 744 | }; |
---|
| 745 | |
---|
[aea8b68] | 746 | |
---|
[e4816ea] | 747 | /* IM->IRC: File transfers */ |
---|
[17a6ee9] | 748 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
| 749 | { |
---|
| 750 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
| 751 | } |
---|
| 752 | |
---|
[a87754b] | 753 | static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
[17a6ee9] | 754 | { |
---|
| 755 | return dccs_recv_start( ft ); |
---|
| 756 | } |
---|
| 757 | |
---|
[a87754b] | 758 | static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
[17a6ee9] | 759 | { |
---|
| 760 | return dcc_close( ft ); |
---|
| 761 | } |
---|
| 762 | |
---|
[a87754b] | 763 | static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
[17a6ee9] | 764 | { |
---|
| 765 | dcc_file_transfer_t *df = file->priv; |
---|
| 766 | |
---|
| 767 | if( file->bytes_transferred >= file->file_size ) |
---|
| 768 | dcc_finish( file ); |
---|
| 769 | else |
---|
| 770 | df->proto_finished = TRUE; |
---|
| 771 | } |
---|
| 772 | |
---|
[d860a8d] | 773 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
[81e04e1] | 774 | bee_irc_user_new, |
---|
[d860a8d] | 775 | bee_irc_user_free, |
---|
[1d39159] | 776 | bee_irc_user_fullname, |
---|
[6ef9065] | 777 | bee_irc_user_nick_hint, |
---|
[7e83e8e4] | 778 | bee_irc_user_group, |
---|
[d860a8d] | 779 | bee_irc_user_status, |
---|
[f012a9f] | 780 | bee_irc_user_msg, |
---|
[573dab0] | 781 | bee_irc_user_typing, |
---|
[17a6ee9] | 782 | |
---|
[aea8b68] | 783 | bee_irc_chat_new, |
---|
| 784 | bee_irc_chat_free, |
---|
[27e2c66] | 785 | bee_irc_chat_log, |
---|
| 786 | bee_irc_chat_msg, |
---|
[aea8b68] | 787 | bee_irc_chat_add_user, |
---|
[b17ce85] | 788 | bee_irc_chat_remove_user, |
---|
[9e27f18] | 789 | bee_irc_chat_topic, |
---|
[d343eaa] | 790 | bee_irc_chat_name_hint, |
---|
[aea8b68] | 791 | |
---|
[17a6ee9] | 792 | bee_irc_ft_in_start, |
---|
| 793 | bee_irc_ft_out_start, |
---|
| 794 | bee_irc_ft_close, |
---|
| 795 | bee_irc_ft_finished, |
---|
[81e04e1] | 796 | }; |
---|