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