[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 | |
---|
| 29 | /* IM->IRC callbacks */ |
---|
| 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 | |
---|
[81e04e1] | 68 | iu->f = &irc_user_im_funcs; |
---|
| 69 | //iu->last_typing_notice = 0; |
---|
| 70 | |
---|
| 71 | return TRUE; |
---|
| 72 | } |
---|
| 73 | |
---|
[d860a8d] | 74 | static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu ) |
---|
| 75 | { |
---|
[eabc9d2] | 76 | return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data ); |
---|
[d860a8d] | 77 | } |
---|
[81e04e1] | 78 | |
---|
[d860a8d] | 79 | static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old ) |
---|
| 80 | { |
---|
[231b08b] | 81 | irc_t *irc = bee->ui_data; |
---|
[003a12b] | 82 | irc_user_t *iu = bu->ui_data; |
---|
[231b08b] | 83 | irc_channel_t *ic = irc->channels->data; /* For now, just pick the first channel. */ |
---|
| 84 | |
---|
[eb50495] | 85 | /* Do this outside the if below since away state can change without |
---|
| 86 | the online state changing. */ |
---|
| 87 | iu->flags &= ~IRC_USER_AWAY; |
---|
| 88 | if( bu->flags & BEE_USER_AWAY || !( bu->flags & BEE_USER_ONLINE ) ) |
---|
| 89 | iu->flags |= IRC_USER_AWAY; |
---|
| 90 | |
---|
[231b08b] | 91 | if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) ) |
---|
| 92 | { |
---|
| 93 | if( bu->flags & BEE_USER_ONLINE ) |
---|
[003a12b] | 94 | { |
---|
| 95 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
| 96 | irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
| 97 | iu->host, (int) time( NULL ), "logged online" ); |
---|
| 98 | |
---|
| 99 | irc_channel_add_user( ic, iu ); |
---|
[6a9d068] | 100 | |
---|
| 101 | if( set_getbool( &bee->set, "away_devoice" ) ) |
---|
| 102 | irc_channel_user_set_mode( ic, iu, ( bu->flags & BEE_USER_AWAY ) ? |
---|
| 103 | 0 : IRC_CHANNEL_USER_VOICE ); |
---|
[003a12b] | 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 | |
---|
| 111 | irc_channel_del_user( ic, iu ); |
---|
| 112 | } |
---|
[231b08b] | 113 | } |
---|
| 114 | |
---|
[d860a8d] | 115 | return TRUE; |
---|
| 116 | } |
---|
[81e04e1] | 117 | |
---|
[f012a9f] | 118 | static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
| 119 | { |
---|
| 120 | irc_t *irc = bee->ui_data; |
---|
| 121 | irc_channel_t *ic = irc->channels->data; |
---|
| 122 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 123 | char *dst, *prefix = NULL; |
---|
[21c87a7] | 124 | char *wrapped, *ts = NULL; |
---|
| 125 | |
---|
| 126 | if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) ) |
---|
| 127 | ts = irc_format_timestamp( irc, sent_at ); |
---|
[f012a9f] | 128 | |
---|
| 129 | if( iu->flags & IRC_USER_PRIVATE ) |
---|
| 130 | { |
---|
| 131 | dst = irc->user->nick; |
---|
[21c87a7] | 132 | prefix = ts; |
---|
| 133 | ts = NULL; |
---|
[f012a9f] | 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
| 137 | dst = ic->name; |
---|
[bce78c8] | 138 | prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" ); |
---|
[f012a9f] | 139 | } |
---|
| 140 | |
---|
| 141 | wrapped = word_wrap( msg, 425 ); |
---|
| 142 | irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix ); |
---|
| 143 | |
---|
| 144 | g_free( wrapped ); |
---|
| 145 | g_free( prefix ); |
---|
[21c87a7] | 146 | g_free( ts ); |
---|
[f012a9f] | 147 | |
---|
| 148 | return TRUE; |
---|
| 149 | } |
---|
| 150 | |
---|
[573dab0] | 151 | static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags ) |
---|
| 152 | { |
---|
| 153 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 154 | |
---|
| 155 | if( set_getbool( &bee->set, "typing_notice" ) ) |
---|
| 156 | irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick, |
---|
| 157 | "\001TYPING %d\001", ( flags >> 8 ) & 3 ); |
---|
| 158 | else |
---|
| 159 | return FALSE; |
---|
| 160 | |
---|
| 161 | return TRUE; |
---|
| 162 | } |
---|
| 163 | |
---|
[1d39159] | 164 | static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu ) |
---|
| 165 | { |
---|
| 166 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
| 167 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
| 168 | char *s; |
---|
| 169 | |
---|
| 170 | if( iu->fullname != iu->nick ) |
---|
| 171 | g_free( iu->fullname ); |
---|
| 172 | iu->fullname = g_strdup( bu->fullname ); |
---|
| 173 | |
---|
| 174 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
| 175 | TODO(wilmer): Do the same with away msgs again! */ |
---|
| 176 | for( s = iu->fullname; *s; s ++ ) |
---|
| 177 | if( isspace( *s ) ) *s = ' '; |
---|
| 178 | |
---|
| 179 | if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) ) |
---|
| 180 | { |
---|
| 181 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
| 182 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | s = set_getstr( &bu->ic->acc->set, "nick_source" ); |
---|
| 186 | if( strcmp( s, "handle" ) != 0 ) |
---|
| 187 | { |
---|
| 188 | char *name = g_strdup( bu->fullname ); |
---|
| 189 | |
---|
| 190 | if( strcmp( s, "first_name" ) == 0 ) |
---|
| 191 | { |
---|
| 192 | int i; |
---|
| 193 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
| 194 | name[i] = '\0'; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | imcb_buddy_nick_hint( bu->ic, bu->handle, name ); |
---|
| 198 | |
---|
| 199 | g_free( name ); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | return TRUE; |
---|
| 203 | } |
---|
| 204 | |
---|
[aea8b68] | 205 | |
---|
| 206 | /* Groupchats */ |
---|
| 207 | gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c ) |
---|
| 208 | { |
---|
| 209 | irc_t *irc = bee->ui_data; |
---|
| 210 | irc_channel_t *ic; |
---|
| 211 | char *topic; |
---|
| 212 | int i; |
---|
| 213 | |
---|
| 214 | for( i = 0; i <= 999; i ++ ) |
---|
| 215 | { |
---|
| 216 | char name[16]; |
---|
| 217 | sprintf( name, "&chat_%03d", i ); |
---|
| 218 | if( ( ic = irc_channel_new( irc, name ) ) ) |
---|
| 219 | break; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if( ic == NULL ) |
---|
| 223 | return FALSE; |
---|
| 224 | |
---|
| 225 | c->ui_data = ic; |
---|
| 226 | ic->data = c; |
---|
| 227 | |
---|
| 228 | topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
| 229 | irc_channel_set_topic( ic, topic, irc->root ); |
---|
| 230 | g_free( topic ); |
---|
| 231 | |
---|
| 232 | return TRUE; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c ) |
---|
| 236 | { |
---|
| 237 | irc_channel_t *ic = c->ui_data; |
---|
| 238 | |
---|
| 239 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
| 240 | irc_channel_printf( ic, "Cleaning up channel, bye!" ); |
---|
| 241 | |
---|
| 242 | irc_channel_free( ic ); |
---|
| 243 | |
---|
| 244 | return TRUE; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *format, ... ) |
---|
| 248 | { |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, const char *who, const char *msg, time_t sent_at ) |
---|
| 252 | { |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
| 256 | { |
---|
| 257 | irc_t *irc = bee->ui_data; |
---|
| 258 | |
---|
| 259 | irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
| 263 | { |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
[17a6ee9] | 267 | /* File transfers */ |
---|
| 268 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
| 269 | { |
---|
| 270 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
| 274 | { |
---|
| 275 | return dccs_recv_start( ft ); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
| 279 | { |
---|
| 280 | return dcc_close( ft ); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
| 284 | { |
---|
| 285 | dcc_file_transfer_t *df = file->priv; |
---|
| 286 | |
---|
| 287 | if( file->bytes_transferred >= file->file_size ) |
---|
| 288 | dcc_finish( file ); |
---|
| 289 | else |
---|
| 290 | df->proto_finished = TRUE; |
---|
| 291 | } |
---|
| 292 | |
---|
[d860a8d] | 293 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
[81e04e1] | 294 | bee_irc_user_new, |
---|
[d860a8d] | 295 | bee_irc_user_free, |
---|
[1d39159] | 296 | bee_irc_user_fullname, |
---|
[d860a8d] | 297 | bee_irc_user_status, |
---|
[f012a9f] | 298 | bee_irc_user_msg, |
---|
[573dab0] | 299 | bee_irc_user_typing, |
---|
[17a6ee9] | 300 | |
---|
[aea8b68] | 301 | bee_irc_chat_new, |
---|
| 302 | bee_irc_chat_free, |
---|
| 303 | NULL, |
---|
| 304 | NULL, |
---|
| 305 | bee_irc_chat_add_user, |
---|
| 306 | NULL, |
---|
| 307 | |
---|
[17a6ee9] | 308 | bee_irc_ft_in_start, |
---|
| 309 | bee_irc_ft_out_start, |
---|
| 310 | bee_irc_ft_close, |
---|
| 311 | bee_irc_ft_finished, |
---|
[81e04e1] | 312 | }; |
---|
| 313 | |
---|
[d860a8d] | 314 | |
---|
| 315 | /* IRC->IM calls */ |
---|
| 316 | |
---|
| 317 | static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg ) |
---|
| 318 | { |
---|
| 319 | if( iu->bu ) |
---|
| 320 | return bee_user_msg( iu->irc->b, iu->bu, msg, 0 ); |
---|
| 321 | else |
---|
| 322 | return FALSE; |
---|
| 323 | } |
---|
| 324 | |
---|
[89c11e7] | 325 | static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp ) |
---|
| 326 | { |
---|
| 327 | if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0 |
---|
| 328 | && g_strcasecmp( ctcp[1], "SEND" ) == 0 ) |
---|
| 329 | { |
---|
| 330 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request ) |
---|
| 331 | { |
---|
| 332 | file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp ); |
---|
| 333 | if ( ft ) |
---|
| 334 | iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle ); |
---|
| 335 | |
---|
| 336 | return TRUE; |
---|
| 337 | } |
---|
| 338 | } |
---|
[3a9b123] | 339 | else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 ) |
---|
| 340 | { |
---|
| 341 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] ) |
---|
| 342 | { |
---|
| 343 | int st = ctcp[1][0]; |
---|
| 344 | if( st >= '0' && st <= '2' ) |
---|
| 345 | { |
---|
| 346 | st <<= 8; |
---|
| 347 | iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st ); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | return TRUE; |
---|
| 351 | } |
---|
| 352 | } |
---|
[89c11e7] | 353 | |
---|
| 354 | return FALSE; |
---|
| 355 | } |
---|
| 356 | |
---|
[81e04e1] | 357 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
[d860a8d] | 358 | bee_irc_user_privmsg, |
---|
[89c11e7] | 359 | bee_irc_user_ctcp, |
---|
[81e04e1] | 360 | }; |
---|