[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 | { |
---|
[54794b8] | 214 | char buf[1024], *fn; |
---|
[b7d3cc34] | 215 | struct msn_data *md = gc->proto_data; |
---|
| 216 | |
---|
| 217 | if( strlen( info ) > 129 ) |
---|
| 218 | { |
---|
| 219 | do_error_dialog( gc, "Maximum name length exceeded", "MSN" ); |
---|
| 220 | return; |
---|
| 221 | } |
---|
| 222 | |
---|
[54794b8] | 223 | fn = msn_http_encode( info ); |
---|
[b7d3cc34] | 224 | |
---|
| 225 | g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, gc->username, fn ); |
---|
| 226 | msn_write( gc, buf, strlen( buf ) ); |
---|
| 227 | g_free( fn ); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | static void msn_get_info(struct gaim_connection *gc, char *who) |
---|
| 231 | { |
---|
| 232 | /* Just make an URL and let the user fetch the info */ |
---|
| 233 | serv_got_crap( gc, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | static void msn_add_buddy( struct gaim_connection *gc, char *who ) |
---|
| 237 | { |
---|
| 238 | msn_buddy_list_add( gc, "FL", who, who ); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | static void msn_remove_buddy( struct gaim_connection *gc, char *who, char *group ) |
---|
| 242 | { |
---|
| 243 | msn_buddy_list_remove( gc, "FL", who ); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | static int msn_chat_send( struct gaim_connection *gc, int id, char *message ) |
---|
| 247 | { |
---|
| 248 | struct msn_switchboard *sb = msn_sb_by_id( gc, id ); |
---|
| 249 | |
---|
| 250 | if( sb ) |
---|
| 251 | return( msn_sb_sendmessage( sb, message ) ); |
---|
| 252 | else |
---|
| 253 | return( 0 ); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | static void msn_chat_invite( struct gaim_connection *gc, int id, char *msg, char *who ) |
---|
| 257 | { |
---|
| 258 | struct msn_switchboard *sb = msn_sb_by_id( gc, id ); |
---|
| 259 | char buf[1024]; |
---|
| 260 | |
---|
| 261 | if( sb ) |
---|
| 262 | { |
---|
| 263 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 264 | msn_sb_write( sb, buf, strlen( buf ) ); |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | static void msn_chat_leave( struct gaim_connection *gc, int id ) |
---|
| 269 | { |
---|
| 270 | struct msn_switchboard *sb = msn_sb_by_id( gc, id ); |
---|
| 271 | |
---|
| 272 | if( sb ) |
---|
| 273 | msn_sb_write( sb, "OUT\r\n", 5 ); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | static int msn_chat_open( struct gaim_connection *gc, char *who ) |
---|
| 277 | { |
---|
| 278 | struct msn_switchboard *sb; |
---|
| 279 | struct msn_data *md = gc->proto_data; |
---|
| 280 | char buf[1024]; |
---|
| 281 | |
---|
| 282 | if( ( sb = msn_sb_by_handle( gc, who ) ) ) |
---|
| 283 | { |
---|
| 284 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
| 285 | msn_sb_to_chat( sb ); |
---|
| 286 | return( 1 ); |
---|
| 287 | } |
---|
| 288 | else |
---|
| 289 | { |
---|
| 290 | struct msn_message *m; |
---|
| 291 | |
---|
| 292 | if( ( sb = msn_sb_spare( gc ) ) ) |
---|
| 293 | { |
---|
| 294 | debug( "Trying to reuse an existing switchboard as a groupchat with %s", who ); |
---|
| 295 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 296 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
| 297 | { |
---|
| 298 | msn_sb_to_chat( sb ); |
---|
| 299 | return( 1 ); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /* If the stuff above failed for some reason: */ |
---|
| 304 | debug( "Creating a new switchboard to groupchat with %s", who ); |
---|
| 305 | |
---|
| 306 | /* Request a new switchboard. */ |
---|
| 307 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
| 308 | if( !msn_write( gc, buf, strlen( buf ) ) ) |
---|
| 309 | return( 0 ); |
---|
| 310 | |
---|
| 311 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
| 312 | m = g_new0( struct msn_message, 1 ); |
---|
| 313 | m->who = g_strdup( who ); |
---|
| 314 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
| 315 | |
---|
| 316 | /* Queue the magic message and cross your fingers. */ |
---|
| 317 | md->msgq = g_slist_append( md->msgq, m ); |
---|
| 318 | |
---|
| 319 | return( 1 ); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | return( 0 ); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | static void msn_keepalive( struct gaim_connection *gc ) |
---|
| 326 | { |
---|
| 327 | msn_write( gc, "PNG\r\n", strlen( "PNG\r\n" ) ); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | static void msn_add_permit( struct gaim_connection *gc, char *who ) |
---|
| 331 | { |
---|
| 332 | msn_buddy_list_add( gc, "AL", who, who ); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | static void msn_rem_permit( struct gaim_connection *gc, char *who ) |
---|
| 336 | { |
---|
| 337 | msn_buddy_list_remove( gc, "AL", who ); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | static void msn_add_deny( struct gaim_connection *gc, char *who ) |
---|
| 341 | { |
---|
| 342 | struct msn_switchboard *sb; |
---|
| 343 | |
---|
| 344 | msn_buddy_list_add( gc, "BL", who, who ); |
---|
| 345 | |
---|
| 346 | /* If there's still a conversation with this person, close it. */ |
---|
| 347 | if( ( sb = msn_sb_by_handle( gc, who ) ) ) |
---|
| 348 | { |
---|
| 349 | msn_sb_destroy( sb ); |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | static void msn_rem_deny( struct gaim_connection *gc, char *who ) |
---|
| 354 | { |
---|
| 355 | msn_buddy_list_remove( gc, "BL", who ); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | static int msn_send_typing( struct gaim_connection *gc, char *who, int typing ) |
---|
| 359 | { |
---|
| 360 | if( typing ) |
---|
| 361 | return( msn_send_im( gc, who, TYPING_NOTIFICATION_MESSAGE, strlen( TYPING_NOTIFICATION_MESSAGE ), 0 ) ); |
---|
| 362 | else |
---|
| 363 | return( 1 ); |
---|
| 364 | } |
---|
| 365 | |
---|
[7b23afd] | 366 | void msn_init() |
---|
[b7d3cc34] | 367 | { |
---|
[7b23afd] | 368 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
| 369 | ret->name = "msn"; |
---|
[b7d3cc34] | 370 | ret->login = msn_login; |
---|
| 371 | ret->close = msn_close; |
---|
| 372 | ret->send_im = msn_send_im; |
---|
| 373 | ret->away_states = msn_away_states; |
---|
| 374 | ret->get_status_string = msn_get_status_string; |
---|
| 375 | ret->set_away = msn_set_away; |
---|
| 376 | ret->set_info = msn_set_info; |
---|
| 377 | ret->get_info = msn_get_info; |
---|
| 378 | ret->add_buddy = msn_add_buddy; |
---|
| 379 | ret->remove_buddy = msn_remove_buddy; |
---|
| 380 | ret->chat_send = msn_chat_send; |
---|
| 381 | ret->chat_invite = msn_chat_invite; |
---|
| 382 | ret->chat_leave = msn_chat_leave; |
---|
| 383 | ret->chat_open = msn_chat_open; |
---|
| 384 | ret->keepalive = msn_keepalive; |
---|
| 385 | ret->add_permit = msn_add_permit; |
---|
| 386 | ret->rem_permit = msn_rem_permit; |
---|
| 387 | ret->add_deny = msn_add_deny; |
---|
| 388 | ret->rem_deny = msn_rem_deny; |
---|
| 389 | ret->send_typing = msn_send_typing; |
---|
[9cb9868] | 390 | ret->cmp_buddynames = g_strcasecmp; |
---|
[b7d3cc34] | 391 | |
---|
[7b23afd] | 392 | register_protocol(ret); |
---|
[b7d3cc34] | 393 | } |
---|