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