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