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