[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 | |
---|
[1d39159] | 222 | static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu ) |
---|
| 223 | { |
---|
| 224 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 225 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 226 | char *s; |
---|
| 227 | |
---|
| 228 | if( iu->fullname != iu->nick ) |
---|
| 229 | g_free( iu->fullname ); |
---|
| 230 | iu->fullname = g_strdup( bu->fullname ); |
---|
| 231 | |
---|
| 232 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
| 233 | TODO(wilmer): Do the same with away msgs again! */ |
---|
| 234 | for( s = iu->fullname; *s; s ++ ) |
---|
| 235 | if( isspace( *s ) ) *s = ' '; |
---|
| 236 | |
---|
| 237 | if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) ) |
---|
| 238 | { |
---|
| 239 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
| 240 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | s = set_getstr( &bu->ic->acc->set, "nick_source" ); |
---|
| 244 | if( strcmp( s, "handle" ) != 0 ) |
---|
| 245 | { |
---|
| 246 | char *name = g_strdup( bu->fullname ); |
---|
| 247 | |
---|
| 248 | if( strcmp( s, "first_name" ) == 0 ) |
---|
| 249 | { |
---|
| 250 | int i; |
---|
| 251 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
| 252 | name[i] = '\0'; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | imcb_buddy_nick_hint( bu->ic, bu->handle, name ); |
---|
| 256 | |
---|
| 257 | g_free( name ); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | return TRUE; |
---|
| 261 | } |
---|
| 262 | |
---|
[7e83e8e4] | 263 | static gboolean bee_irc_user_group( bee_t *bee, bee_user_t *bu ) |
---|
| 264 | { |
---|
| 265 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 266 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 267 | |
---|
| 268 | bee_irc_channel_update( irc, NULL, iu ); |
---|
| 269 | |
---|
| 270 | return TRUE; |
---|
| 271 | } |
---|
| 272 | |
---|
[e4816ea] | 273 | /* IRC->IM calls */ |
---|
| 274 | |
---|
| 275 | static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 276 | { |
---|
| 277 | if( iu->bu ) |
---|
| 278 | return bee_user_msg( iu->irc->b, iu->bu, msg, 0 ); |
---|
| 279 | else |
---|
| 280 | return FALSE; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp ) |
---|
| 284 | { |
---|
| 285 | if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0 |
---|
| 286 | && g_strcasecmp( ctcp[1], "SEND" ) == 0 ) |
---|
| 287 | { |
---|
| 288 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request ) |
---|
| 289 | { |
---|
| 290 | file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp ); |
---|
| 291 | if ( ft ) |
---|
| 292 | iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle ); |
---|
| 293 | |
---|
| 294 | return TRUE; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 ) |
---|
| 298 | { |
---|
| 299 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] ) |
---|
| 300 | { |
---|
| 301 | int st = ctcp[1][0]; |
---|
| 302 | if( st >= '0' && st <= '2' ) |
---|
| 303 | { |
---|
| 304 | st <<= 8; |
---|
| 305 | iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st ); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | return TRUE; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | return FALSE; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
| 316 | bee_irc_user_privmsg, |
---|
| 317 | bee_irc_user_ctcp, |
---|
| 318 | }; |
---|
| 319 | |
---|
[aea8b68] | 320 | |
---|
[e4816ea] | 321 | /* IM->IRC: Groupchats */ |
---|
[5a75d15] | 322 | const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
[a87754b] | 323 | |
---|
| 324 | static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c ) |
---|
[aea8b68] | 325 | { |
---|
| 326 | irc_t *irc = bee->ui_data; |
---|
| 327 | irc_channel_t *ic; |
---|
| 328 | char *topic; |
---|
[eb37735] | 329 | GSList *l; |
---|
[aea8b68] | 330 | int i; |
---|
| 331 | |
---|
[eb37735] | 332 | /* Try to find a channel that expects to receive a groupchat. |
---|
| 333 | This flag is set by groupchat_stub_invite(). */ |
---|
| 334 | for( l = irc->channels; l; l = l->next ) |
---|
| 335 | { |
---|
| 336 | ic = l->data; |
---|
| 337 | if( ic->flags & IRC_CHANNEL_CHAT_PICKME ) |
---|
| 338 | break; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | /* If we found none, just generate some stupid name. */ |
---|
| 342 | if( l == NULL ) for( i = 0; i <= 999; i ++ ) |
---|
[aea8b68] | 343 | { |
---|
| 344 | char name[16]; |
---|
[5a75d15] | 345 | sprintf( name, "#chat_%03d", i ); |
---|
[aea8b68] | 346 | if( ( ic = irc_channel_new( irc, name ) ) ) |
---|
| 347 | break; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | if( ic == NULL ) |
---|
| 351 | return FALSE; |
---|
| 352 | |
---|
| 353 | c->ui_data = ic; |
---|
| 354 | ic->data = c; |
---|
| 355 | |
---|
| 356 | topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
| 357 | irc_channel_set_topic( ic, topic, irc->root ); |
---|
| 358 | g_free( topic ); |
---|
| 359 | |
---|
| 360 | return TRUE; |
---|
| 361 | } |
---|
| 362 | |
---|
[a87754b] | 363 | static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c ) |
---|
[aea8b68] | 364 | { |
---|
| 365 | irc_channel_t *ic = c->ui_data; |
---|
| 366 | |
---|
| 367 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 368 | irc_channel_printf( ic, "Cleaning up channel, bye!" ); |
---|
| 369 | |
---|
[5a75d15] | 370 | /* irc_channel_free( ic ); */ |
---|
| 371 | |
---|
[18da20b] | 372 | irc_channel_del_user( ic, ic->irc->user, FALSE, "Chatroom closed by server" ); |
---|
[5a75d15] | 373 | ic->data = NULL; |
---|
[aea8b68] | 374 | |
---|
| 375 | return TRUE; |
---|
| 376 | } |
---|
| 377 | |
---|
[a87754b] | 378 | static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text ) |
---|
[aea8b68] | 379 | { |
---|
[27e2c66] | 380 | irc_channel_t *ic = c->ui_data; |
---|
| 381 | |
---|
| 382 | irc_channel_printf( ic, "%s", text ); |
---|
[b17ce85] | 383 | |
---|
| 384 | return TRUE; |
---|
[aea8b68] | 385 | } |
---|
| 386 | |
---|
[a87754b] | 387 | static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
[aea8b68] | 388 | { |
---|
[27e2c66] | 389 | irc_t *irc = bee->ui_data; |
---|
| 390 | irc_user_t *iu = bu->ui_data; |
---|
| 391 | irc_channel_t *ic = c->ui_data; |
---|
| 392 | char *ts = NULL; |
---|
| 393 | |
---|
| 394 | if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) ) |
---|
| 395 | ts = irc_format_timestamp( irc, sent_at ); |
---|
| 396 | |
---|
| 397 | irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts ); |
---|
| 398 | g_free( ts ); |
---|
| 399 | |
---|
| 400 | return TRUE; |
---|
[aea8b68] | 401 | } |
---|
| 402 | |
---|
[a87754b] | 403 | static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
[aea8b68] | 404 | { |
---|
| 405 | irc_t *irc = bee->ui_data; |
---|
| 406 | |
---|
| 407 | irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
[b17ce85] | 408 | |
---|
| 409 | return TRUE; |
---|
[aea8b68] | 410 | } |
---|
| 411 | |
---|
[a87754b] | 412 | static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
[aea8b68] | 413 | { |
---|
[b17ce85] | 414 | irc_t *irc = bee->ui_data; |
---|
| 415 | |
---|
[18da20b] | 416 | irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data, FALSE, NULL ); |
---|
[b17ce85] | 417 | |
---|
| 418 | return TRUE; |
---|
[aea8b68] | 419 | } |
---|
| 420 | |
---|
[9e27f18] | 421 | static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu ) |
---|
| 422 | { |
---|
| 423 | irc_t *irc = bee->ui_data; |
---|
| 424 | irc_user_t *iu; |
---|
| 425 | |
---|
| 426 | if( bu == NULL ) |
---|
| 427 | iu = irc->root; |
---|
| 428 | else if( bu == bee->user ) |
---|
| 429 | iu = irc->user; |
---|
| 430 | else |
---|
| 431 | iu = bu->ui_data; |
---|
| 432 | |
---|
| 433 | irc_channel_set_topic( c->ui_data, new, iu ); |
---|
| 434 | |
---|
| 435 | return TRUE; |
---|
| 436 | } |
---|
| 437 | |
---|
[d343eaa] | 438 | static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name ) |
---|
| 439 | { |
---|
| 440 | irc_t *irc = bee->ui_data; |
---|
| 441 | irc_channel_t *ic = c->ui_data; |
---|
| 442 | char stripped[MAX_NICK_LENGTH+1], *full_name; |
---|
| 443 | |
---|
| 444 | /* Don't rename a channel if the user's in it already. */ |
---|
| 445 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 446 | return FALSE; |
---|
| 447 | |
---|
| 448 | strncpy( stripped, name, MAX_NICK_LENGTH ); |
---|
| 449 | stripped[MAX_NICK_LENGTH] = '\0'; |
---|
| 450 | nick_strip( stripped ); |
---|
| 451 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
| 452 | nick_lc( stripped ); |
---|
| 453 | |
---|
| 454 | full_name = g_strdup_printf( "&%s", stripped ); |
---|
| 455 | |
---|
| 456 | if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL ) |
---|
| 457 | { |
---|
| 458 | g_free( ic->name ); |
---|
| 459 | ic->name = full_name; |
---|
| 460 | } |
---|
| 461 | else |
---|
| 462 | { |
---|
| 463 | g_free( full_name ); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | return TRUE; |
---|
| 467 | } |
---|
| 468 | |
---|
[a87754b] | 469 | /* IRC->IM */ |
---|
| 470 | static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg ) |
---|
| 471 | { |
---|
| 472 | struct groupchat *c = ic->data; |
---|
| 473 | |
---|
[5a75d15] | 474 | if( c == NULL ) |
---|
| 475 | return FALSE; |
---|
| 476 | |
---|
[a87754b] | 477 | bee_chat_msg( ic->irc->b, c, msg, 0 ); |
---|
| 478 | |
---|
| 479 | return TRUE; |
---|
[5a75d15] | 480 | } |
---|
| 481 | |
---|
| 482 | static gboolean bee_irc_channel_chat_join( irc_channel_t *ic ) |
---|
| 483 | { |
---|
| 484 | char *acc_s, *room; |
---|
| 485 | account_t *acc; |
---|
[a87754b] | 486 | |
---|
[5a75d15] | 487 | if( strcmp( set_getstr( &ic->set, "chat_type" ), "room" ) != 0 ) |
---|
| 488 | return TRUE; |
---|
| 489 | |
---|
| 490 | if( ( acc_s = set_getstr( &ic->set, "account" ) ) && |
---|
| 491 | ( room = set_getstr( &ic->set, "room" ) ) && |
---|
| 492 | ( acc = account_get( ic->irc->b, acc_s ) ) && |
---|
| 493 | acc->ic && acc->prpl->chat_join ) |
---|
| 494 | { |
---|
| 495 | char *nick; |
---|
| 496 | |
---|
| 497 | if( !( nick = set_getstr( &ic->set, "nick" ) ) ) |
---|
| 498 | nick = ic->irc->user->nick; |
---|
| 499 | |
---|
| 500 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
| 501 | acc->prpl->chat_join( acc->ic, room, nick, NULL ); |
---|
| 502 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
| 503 | |
---|
| 504 | return FALSE; |
---|
| 505 | } |
---|
| 506 | else |
---|
| 507 | { |
---|
| 508 | irc_send_num( ic->irc, 403, "%s :Can't join channel, account offline?", ic->name ); |
---|
| 509 | return FALSE; |
---|
| 510 | } |
---|
[a87754b] | 511 | } |
---|
| 512 | |
---|
[bfb99ee] | 513 | static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg ) |
---|
| 514 | { |
---|
| 515 | struct groupchat *c = ic->data; |
---|
| 516 | |
---|
[5a75d15] | 517 | if( c && c->ic->acc->prpl->chat_leave ) |
---|
[bfb99ee] | 518 | c->ic->acc->prpl->chat_leave( c ); |
---|
| 519 | |
---|
| 520 | return TRUE; |
---|
| 521 | } |
---|
| 522 | |
---|
[9e27f18] | 523 | static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new ) |
---|
| 524 | { |
---|
[4469e7e] | 525 | struct groupchat *c = ic->data; |
---|
[5a75d15] | 526 | |
---|
| 527 | if( c == NULL ) |
---|
| 528 | return FALSE; |
---|
[4469e7e] | 529 | |
---|
| 530 | if( c->ic->acc->prpl->chat_topic == NULL ) |
---|
| 531 | irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name ); |
---|
| 532 | else |
---|
| 533 | { |
---|
[5a75d15] | 534 | /* TODO: Need more const goodness here, sigh */ |
---|
| 535 | char *topic = g_strdup( new ); |
---|
[4469e7e] | 536 | c->ic->acc->prpl->chat_topic( c, topic ); |
---|
[5a75d15] | 537 | g_free( topic ); |
---|
[4469e7e] | 538 | return TRUE; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | return FALSE; |
---|
[9e27f18] | 542 | } |
---|
| 543 | |
---|
[66b9e36a] | 544 | static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
| 545 | { |
---|
| 546 | struct groupchat *c = ic->data; |
---|
[5a75d15] | 547 | bee_user_t *bu = iu->bu; |
---|
| 548 | |
---|
| 549 | if( bu == NULL ) |
---|
| 550 | return FALSE; |
---|
[66b9e36a] | 551 | |
---|
[5a75d15] | 552 | if( c ) |
---|
| 553 | { |
---|
| 554 | if( iu->bu->ic != c->ic ) |
---|
| 555 | irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name ); |
---|
| 556 | else if( c->ic->acc->prpl->chat_invite ) |
---|
| 557 | c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL ); |
---|
| 558 | else |
---|
| 559 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
| 560 | } |
---|
| 561 | else if( bu->ic->acc->prpl->chat_with && |
---|
| 562 | strcmp( set_getstr( &ic->set, "chat_type" ), "groupchat" ) == 0 ) |
---|
| 563 | { |
---|
| 564 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
| 565 | iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle ); |
---|
| 566 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
| 567 | } |
---|
[66b9e36a] | 568 | else |
---|
[5a75d15] | 569 | { |
---|
[66b9e36a] | 570 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
[5a75d15] | 571 | } |
---|
[66b9e36a] | 572 | |
---|
| 573 | return TRUE; |
---|
| 574 | } |
---|
| 575 | |
---|
[5a75d15] | 576 | static char *set_eval_room_account( set_t *set, char *value ); |
---|
| 577 | |
---|
| 578 | static gboolean bee_irc_channel_init( irc_channel_t *ic ) |
---|
| 579 | { |
---|
| 580 | set_add( &ic->set, "account", NULL, set_eval_room_account, ic ); |
---|
| 581 | set_add( &ic->set, "chat_type", "groupchat", NULL, ic ); |
---|
| 582 | set_add( &ic->set, "nick", NULL, NULL, ic ); |
---|
| 583 | set_add( &ic->set, "room", NULL, NULL, ic ); |
---|
| 584 | |
---|
| 585 | return TRUE; |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | static char *set_eval_room_account( set_t *set, char *value ) |
---|
| 589 | { |
---|
| 590 | struct irc_channel *ic = set->data; |
---|
| 591 | account_t *acc; |
---|
| 592 | |
---|
| 593 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
| 594 | return SET_INVALID; |
---|
| 595 | else if( !acc->prpl->chat_join ) |
---|
| 596 | { |
---|
| 597 | irc_usermsg( ic->irc, "Named chatrooms not supported on that account." ); |
---|
| 598 | return SET_INVALID; |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | static gboolean bee_irc_channel_free( irc_channel_t *ic ) |
---|
| 605 | { |
---|
| 606 | set_del( &ic->set, "account" ); |
---|
| 607 | set_del( &ic->set, "chat_type" ); |
---|
| 608 | set_del( &ic->set, "nick" ); |
---|
| 609 | set_del( &ic->set, "room" ); |
---|
| 610 | |
---|
| 611 | return TRUE; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | const struct irc_channel_funcs irc_channel_im_chat_funcs = { |
---|
[a87754b] | 615 | bee_irc_channel_chat_privmsg, |
---|
[5a75d15] | 616 | bee_irc_channel_chat_join, |
---|
[bfb99ee] | 617 | bee_irc_channel_chat_part, |
---|
[9e27f18] | 618 | bee_irc_channel_chat_topic, |
---|
[66b9e36a] | 619 | bee_irc_channel_chat_invite, |
---|
[5a75d15] | 620 | |
---|
| 621 | bee_irc_channel_init, |
---|
| 622 | bee_irc_channel_free, |
---|
[a87754b] | 623 | }; |
---|
| 624 | |
---|
[aea8b68] | 625 | |
---|
[e4816ea] | 626 | /* IM->IRC: File transfers */ |
---|
[17a6ee9] | 627 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
| 628 | { |
---|
| 629 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
| 630 | } |
---|
| 631 | |
---|
[a87754b] | 632 | static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
[17a6ee9] | 633 | { |
---|
| 634 | return dccs_recv_start( ft ); |
---|
| 635 | } |
---|
| 636 | |
---|
[a87754b] | 637 | static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
[17a6ee9] | 638 | { |
---|
| 639 | return dcc_close( ft ); |
---|
| 640 | } |
---|
| 641 | |
---|
[a87754b] | 642 | static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
[17a6ee9] | 643 | { |
---|
| 644 | dcc_file_transfer_t *df = file->priv; |
---|
| 645 | |
---|
| 646 | if( file->bytes_transferred >= file->file_size ) |
---|
| 647 | dcc_finish( file ); |
---|
| 648 | else |
---|
| 649 | df->proto_finished = TRUE; |
---|
| 650 | } |
---|
| 651 | |
---|
[d860a8d] | 652 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
[81e04e1] | 653 | bee_irc_user_new, |
---|
[d860a8d] | 654 | bee_irc_user_free, |
---|
[1d39159] | 655 | bee_irc_user_fullname, |
---|
[7e83e8e4] | 656 | bee_irc_user_group, |
---|
[d860a8d] | 657 | bee_irc_user_status, |
---|
[f012a9f] | 658 | bee_irc_user_msg, |
---|
[573dab0] | 659 | bee_irc_user_typing, |
---|
[17a6ee9] | 660 | |
---|
[aea8b68] | 661 | bee_irc_chat_new, |
---|
| 662 | bee_irc_chat_free, |
---|
[27e2c66] | 663 | bee_irc_chat_log, |
---|
| 664 | bee_irc_chat_msg, |
---|
[aea8b68] | 665 | bee_irc_chat_add_user, |
---|
[b17ce85] | 666 | bee_irc_chat_remove_user, |
---|
[9e27f18] | 667 | bee_irc_chat_topic, |
---|
[d343eaa] | 668 | bee_irc_chat_name_hint, |
---|
[aea8b68] | 669 | |
---|
[17a6ee9] | 670 | bee_irc_ft_in_start, |
---|
| 671 | bee_irc_ft_out_start, |
---|
| 672 | bee_irc_ft_close, |
---|
| 673 | bee_irc_ft_finished, |
---|
[81e04e1] | 674 | }; |
---|