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