[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* MSN module - Main file; functions to be called from BitlBee */ |
---|
| 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 "nogaim.h" |
---|
| 27 | #include "msn.h" |
---|
| 28 | |
---|
[911f2eb] | 29 | static char *msn_set_display_name( set_t *set, char *value ); |
---|
| 30 | |
---|
| 31 | static void msn_acc_init( account_t *acc ) |
---|
| 32 | { |
---|
| 33 | set_t *s; |
---|
| 34 | |
---|
| 35 | s = set_add( &acc->set, "display_name", NULL, msn_set_display_name, acc ); |
---|
| 36 | s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
| 37 | } |
---|
| 38 | |
---|
[0a3c243] | 39 | static void msn_login( account_t *acc ) |
---|
[b7d3cc34] | 40 | { |
---|
[0a3c243] | 41 | struct gaim_connection *gc = new_gaim_conn( acc ); |
---|
[b7d3cc34] | 42 | struct msn_data *md = g_new0( struct msn_data, 1 ); |
---|
| 43 | |
---|
| 44 | set_login_progress( gc, 1, "Connecting" ); |
---|
| 45 | |
---|
| 46 | gc->proto_data = md; |
---|
| 47 | md->fd = -1; |
---|
| 48 | |
---|
[0a3c243] | 49 | if( strchr( acc->user, '@' ) == NULL ) |
---|
[b7d3cc34] | 50 | { |
---|
| 51 | hide_login_progress( gc, "Invalid account name" ); |
---|
| 52 | signoff( gc ); |
---|
| 53 | return; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, gc ); |
---|
| 57 | if( md->fd < 0 ) |
---|
| 58 | { |
---|
| 59 | hide_login_progress( gc, "Could not connect to server" ); |
---|
| 60 | signoff( gc ); |
---|
| 61 | } |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | md->gc = gc; |
---|
| 65 | md->away_state = msn_away_state_list; |
---|
| 66 | |
---|
| 67 | msn_connections = g_slist_append( msn_connections, gc ); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static void msn_close( struct gaim_connection *gc ) |
---|
| 72 | { |
---|
| 73 | struct msn_data *md = gc->proto_data; |
---|
| 74 | GSList *l; |
---|
| 75 | |
---|
[5a5c926] | 76 | if( md ) |
---|
[b7d3cc34] | 77 | { |
---|
[5a5c926] | 78 | if( md->fd >= 0 ) |
---|
| 79 | closesocket( md->fd ); |
---|
[b7d3cc34] | 80 | |
---|
[5a5c926] | 81 | if( md->handler ) |
---|
[b7d3cc34] | 82 | { |
---|
[5a5c926] | 83 | if( md->handler->rxq ) g_free( md->handler->rxq ); |
---|
| 84 | if( md->handler->cmd_text ) g_free( md->handler->cmd_text ); |
---|
| 85 | g_free( md->handler ); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | while( md->switchboards ) |
---|
| 89 | msn_sb_destroy( md->switchboards->data ); |
---|
[1053836] | 90 | |
---|
[5a5c926] | 91 | if( md->msgq ) |
---|
| 92 | { |
---|
| 93 | struct msn_message *m; |
---|
| 94 | |
---|
| 95 | for( l = md->msgq; l; l = l->next ) |
---|
| 96 | { |
---|
| 97 | m = l->data; |
---|
| 98 | |
---|
| 99 | serv_got_crap( gc, "Warning: Closing down MSN connection with unsent message to %s, you'll have to resend it.", m->who ); |
---|
| 100 | g_free( m->who ); |
---|
| 101 | g_free( m->text ); |
---|
| 102 | g_free( m ); |
---|
| 103 | } |
---|
| 104 | g_slist_free( md->msgq ); |
---|
[b7d3cc34] | 105 | } |
---|
[5a5c926] | 106 | |
---|
[bf25fa3] | 107 | while( md->groupcount > 0 ) |
---|
| 108 | g_free( md->grouplist[--md->groupcount] ); |
---|
[59f5c42a] | 109 | g_free( md->grouplist ); |
---|
| 110 | |
---|
[5a5c926] | 111 | g_free( md ); |
---|
[b7d3cc34] | 112 | } |
---|
| 113 | |
---|
| 114 | for( l = gc->permit; l; l = l->next ) |
---|
| 115 | g_free( l->data ); |
---|
| 116 | g_slist_free( gc->permit ); |
---|
| 117 | |
---|
| 118 | for( l = gc->deny; l; l = l->next ) |
---|
| 119 | g_free( l->data ); |
---|
| 120 | g_slist_free( gc->deny ); |
---|
| 121 | |
---|
| 122 | msn_connections = g_slist_remove( msn_connections, gc ); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | static int msn_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away ) |
---|
| 126 | { |
---|
| 127 | struct msn_switchboard *sb; |
---|
| 128 | struct msn_data *md = gc->proto_data; |
---|
| 129 | |
---|
| 130 | if( ( sb = msn_sb_by_handle( gc, who ) ) ) |
---|
| 131 | { |
---|
| 132 | return( msn_sb_sendmessage( sb, message ) ); |
---|
| 133 | } |
---|
| 134 | else |
---|
| 135 | { |
---|
| 136 | struct msn_message *m; |
---|
| 137 | char buf[1024]; |
---|
| 138 | |
---|
| 139 | /* Create a message. We have to arrange a usable switchboard, and send the message later. */ |
---|
| 140 | m = g_new0( struct msn_message, 1 ); |
---|
| 141 | m->who = g_strdup( who ); |
---|
| 142 | m->text = g_strdup( message ); |
---|
| 143 | |
---|
| 144 | /* FIXME: *CHECK* the reliability of using spare sb's! */ |
---|
| 145 | if( ( sb = msn_sb_spare( gc ) ) ) |
---|
| 146 | { |
---|
| 147 | debug( "Trying to use a spare switchboard to message %s", who ); |
---|
| 148 | |
---|
| 149 | sb->who = g_strdup( who ); |
---|
| 150 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 151 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
| 152 | { |
---|
| 153 | /* He/She should join the switchboard soon, let's queue the message. */ |
---|
| 154 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
| 155 | return( 1 ); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | debug( "Creating a new switchboard to message %s", who ); |
---|
| 160 | |
---|
| 161 | /* If we reach this line, there was no spare switchboard, so let's make one. */ |
---|
| 162 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
| 163 | if( !msn_write( gc, buf, strlen( buf ) ) ) |
---|
| 164 | { |
---|
| 165 | g_free( m->who ); |
---|
| 166 | g_free( m->text ); |
---|
| 167 | g_free( m ); |
---|
| 168 | |
---|
| 169 | return( 0 ); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* And queue the message to md. We'll pick it up when the switchboard comes up. */ |
---|
| 173 | md->msgq = g_slist_append( md->msgq, m ); |
---|
| 174 | |
---|
| 175 | /* FIXME: If the switchboard creation fails, the message will not be sent. */ |
---|
| 176 | |
---|
| 177 | return( 1 ); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | return( 0 ); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | static GList *msn_away_states( struct gaim_connection *gc ) |
---|
| 184 | { |
---|
[5e202b0] | 185 | static GList *l = NULL; |
---|
[b7d3cc34] | 186 | int i; |
---|
| 187 | |
---|
[5e202b0] | 188 | if( l == NULL ) |
---|
| 189 | for( i = 0; msn_away_state_list[i].number > -1; i ++ ) |
---|
| 190 | l = g_list_append( l, (void*) msn_away_state_list[i].name ); |
---|
[b7d3cc34] | 191 | |
---|
[5e202b0] | 192 | return l; |
---|
[b7d3cc34] | 193 | } |
---|
| 194 | |
---|
| 195 | static char *msn_get_status_string( struct gaim_connection *gc, int number ) |
---|
| 196 | { |
---|
[08995b0] | 197 | const struct msn_away_state *st = msn_away_state_by_number( number ); |
---|
[b7d3cc34] | 198 | |
---|
| 199 | if( st ) |
---|
[08995b0] | 200 | return( (char*) st->name ); |
---|
[b7d3cc34] | 201 | else |
---|
| 202 | return( "" ); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | static void msn_set_away( struct gaim_connection *gc, char *state, char *message ) |
---|
| 206 | { |
---|
| 207 | char buf[1024]; |
---|
| 208 | struct msn_data *md = gc->proto_data; |
---|
[08995b0] | 209 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 210 | |
---|
| 211 | if( strcmp( state, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
| 212 | st = msn_away_state_by_name( "Away" ); |
---|
| 213 | else |
---|
| 214 | st = msn_away_state_by_name( state ); |
---|
| 215 | |
---|
| 216 | if( !st ) st = msn_away_state_list; |
---|
| 217 | md->away_state = st; |
---|
| 218 | |
---|
| 219 | g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, st->code ); |
---|
| 220 | msn_write( gc, buf, strlen( buf ) ); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | static void msn_set_info( struct gaim_connection *gc, char *info ) |
---|
| 224 | { |
---|
[911f2eb] | 225 | msn_set_display_name( set_find( &gc->acc->set, "display_name" ), info ); |
---|
[b7d3cc34] | 226 | } |
---|
| 227 | |
---|
| 228 | static void msn_get_info(struct gaim_connection *gc, char *who) |
---|
| 229 | { |
---|
| 230 | /* Just make an URL and let the user fetch the info */ |
---|
| 231 | serv_got_crap( gc, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | static void msn_add_buddy( struct gaim_connection *gc, char *who ) |
---|
| 235 | { |
---|
| 236 | msn_buddy_list_add( gc, "FL", who, who ); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | static void msn_remove_buddy( struct gaim_connection *gc, char *who, char *group ) |
---|
| 240 | { |
---|
| 241 | msn_buddy_list_remove( gc, "FL", who ); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | static int msn_chat_send( struct gaim_connection *gc, int id, char *message ) |
---|
| 245 | { |
---|
| 246 | struct msn_switchboard *sb = msn_sb_by_id( gc, id ); |
---|
| 247 | |
---|
| 248 | if( sb ) |
---|
| 249 | return( msn_sb_sendmessage( sb, message ) ); |
---|
| 250 | else |
---|
| 251 | return( 0 ); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | static void msn_chat_invite( struct gaim_connection *gc, int id, char *msg, char *who ) |
---|
| 255 | { |
---|
| 256 | struct msn_switchboard *sb = msn_sb_by_id( gc, id ); |
---|
| 257 | char buf[1024]; |
---|
| 258 | |
---|
| 259 | if( sb ) |
---|
| 260 | { |
---|
| 261 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 262 | msn_sb_write( sb, buf, strlen( buf ) ); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | static void msn_chat_leave( struct gaim_connection *gc, int id ) |
---|
| 267 | { |
---|
| 268 | struct msn_switchboard *sb = msn_sb_by_id( gc, id ); |
---|
| 269 | |
---|
| 270 | if( sb ) |
---|
| 271 | msn_sb_write( sb, "OUT\r\n", 5 ); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | static int msn_chat_open( struct gaim_connection *gc, char *who ) |
---|
| 275 | { |
---|
| 276 | struct msn_switchboard *sb; |
---|
| 277 | struct msn_data *md = gc->proto_data; |
---|
| 278 | char buf[1024]; |
---|
| 279 | |
---|
| 280 | if( ( sb = msn_sb_by_handle( gc, who ) ) ) |
---|
| 281 | { |
---|
| 282 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
| 283 | msn_sb_to_chat( sb ); |
---|
| 284 | return( 1 ); |
---|
| 285 | } |
---|
| 286 | else |
---|
| 287 | { |
---|
| 288 | struct msn_message *m; |
---|
| 289 | |
---|
| 290 | if( ( sb = msn_sb_spare( gc ) ) ) |
---|
| 291 | { |
---|
| 292 | debug( "Trying to reuse an existing switchboard as a groupchat with %s", who ); |
---|
| 293 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 294 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
| 295 | { |
---|
| 296 | msn_sb_to_chat( sb ); |
---|
| 297 | return( 1 ); |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /* If the stuff above failed for some reason: */ |
---|
| 302 | debug( "Creating a new switchboard to groupchat with %s", who ); |
---|
| 303 | |
---|
| 304 | /* Request a new switchboard. */ |
---|
| 305 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
| 306 | if( !msn_write( gc, buf, strlen( buf ) ) ) |
---|
| 307 | return( 0 ); |
---|
| 308 | |
---|
| 309 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
| 310 | m = g_new0( struct msn_message, 1 ); |
---|
| 311 | m->who = g_strdup( who ); |
---|
| 312 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
| 313 | |
---|
| 314 | /* Queue the magic message and cross your fingers. */ |
---|
| 315 | md->msgq = g_slist_append( md->msgq, m ); |
---|
| 316 | |
---|
| 317 | return( 1 ); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | return( 0 ); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | static void msn_keepalive( struct gaim_connection *gc ) |
---|
| 324 | { |
---|
| 325 | msn_write( gc, "PNG\r\n", strlen( "PNG\r\n" ) ); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | static void msn_add_permit( struct gaim_connection *gc, char *who ) |
---|
| 329 | { |
---|
| 330 | msn_buddy_list_add( gc, "AL", who, who ); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | static void msn_rem_permit( struct gaim_connection *gc, char *who ) |
---|
| 334 | { |
---|
| 335 | msn_buddy_list_remove( gc, "AL", who ); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | static void msn_add_deny( struct gaim_connection *gc, char *who ) |
---|
| 339 | { |
---|
| 340 | struct msn_switchboard *sb; |
---|
| 341 | |
---|
| 342 | msn_buddy_list_add( gc, "BL", who, who ); |
---|
| 343 | |
---|
| 344 | /* If there's still a conversation with this person, close it. */ |
---|
| 345 | if( ( sb = msn_sb_by_handle( gc, who ) ) ) |
---|
| 346 | { |
---|
| 347 | msn_sb_destroy( sb ); |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | static void msn_rem_deny( struct gaim_connection *gc, char *who ) |
---|
| 352 | { |
---|
| 353 | msn_buddy_list_remove( gc, "BL", who ); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | static int msn_send_typing( struct gaim_connection *gc, char *who, int typing ) |
---|
| 357 | { |
---|
| 358 | if( typing ) |
---|
| 359 | return( msn_send_im( gc, who, TYPING_NOTIFICATION_MESSAGE, strlen( TYPING_NOTIFICATION_MESSAGE ), 0 ) ); |
---|
| 360 | else |
---|
| 361 | return( 1 ); |
---|
| 362 | } |
---|
| 363 | |
---|
[911f2eb] | 364 | static char *msn_set_display_name( set_t *set, char *value ) |
---|
| 365 | { |
---|
| 366 | account_t *acc = set->data; |
---|
| 367 | struct gaim_connection *gc = acc->gc; |
---|
| 368 | struct msn_data *md; |
---|
| 369 | char buf[1024], *fn, *s; |
---|
| 370 | int i; |
---|
| 371 | |
---|
| 372 | /* Double-check. */ |
---|
| 373 | if( gc == NULL ) |
---|
| 374 | return NULL; |
---|
| 375 | |
---|
| 376 | md = gc->proto_data; |
---|
| 377 | |
---|
| 378 | if( strlen( value ) > 129 ) |
---|
| 379 | { |
---|
| 380 | serv_got_crap( gc, "Maximum name length exceeded" ); |
---|
| 381 | return NULL; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /* Of course we could use http_encode() here, but when we encode |
---|
| 385 | every character, the server is less likely to complain about the |
---|
| 386 | chosen name. However, the MSN server doesn't seem to like escaped |
---|
| 387 | non-ASCII chars, so we keep those unescaped. */ |
---|
| 388 | s = fn = g_new0( char, strlen( value ) * 3 + 1 ); |
---|
| 389 | for( i = 0; value[i]; i ++ ) |
---|
| 390 | if( value[i] & 128 ) |
---|
| 391 | { |
---|
| 392 | *s = value[i]; |
---|
| 393 | s ++; |
---|
| 394 | } |
---|
| 395 | else |
---|
| 396 | { |
---|
| 397 | g_snprintf( s, 4, "%%%02X", value[i] ); |
---|
| 398 | s += 3; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, gc->username, fn ); |
---|
| 402 | msn_write( gc, buf, strlen( buf ) ); |
---|
| 403 | g_free( fn ); |
---|
| 404 | |
---|
| 405 | /* Returning NULL would be better, because the server still has to |
---|
| 406 | confirm the name change. However, it looks a bit confusing to the |
---|
| 407 | user. */ |
---|
| 408 | return value; |
---|
| 409 | } |
---|
| 410 | |
---|
[7b23afd] | 411 | void msn_init() |
---|
[b7d3cc34] | 412 | { |
---|
[7b23afd] | 413 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[911f2eb] | 414 | |
---|
[7b23afd] | 415 | ret->name = "msn"; |
---|
[b7d3cc34] | 416 | ret->login = msn_login; |
---|
[911f2eb] | 417 | ret->acc_init = msn_acc_init; |
---|
[b7d3cc34] | 418 | ret->close = msn_close; |
---|
| 419 | ret->send_im = msn_send_im; |
---|
| 420 | ret->away_states = msn_away_states; |
---|
| 421 | ret->get_status_string = msn_get_status_string; |
---|
| 422 | ret->set_away = msn_set_away; |
---|
| 423 | ret->set_info = msn_set_info; |
---|
| 424 | ret->get_info = msn_get_info; |
---|
| 425 | ret->add_buddy = msn_add_buddy; |
---|
| 426 | ret->remove_buddy = msn_remove_buddy; |
---|
| 427 | ret->chat_send = msn_chat_send; |
---|
| 428 | ret->chat_invite = msn_chat_invite; |
---|
| 429 | ret->chat_leave = msn_chat_leave; |
---|
| 430 | ret->chat_open = msn_chat_open; |
---|
| 431 | ret->keepalive = msn_keepalive; |
---|
| 432 | ret->add_permit = msn_add_permit; |
---|
| 433 | ret->rem_permit = msn_rem_permit; |
---|
| 434 | ret->add_deny = msn_add_deny; |
---|
| 435 | ret->rem_deny = msn_rem_deny; |
---|
| 436 | ret->send_typing = msn_send_typing; |
---|
[5b52a48] | 437 | ret->handle_cmp = g_strcasecmp; |
---|
[b7d3cc34] | 438 | |
---|
[7b23afd] | 439 | register_protocol(ret); |
---|
[b7d3cc34] | 440 | } |
---|