[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[c92e6801] | 4 | * Copyright 2002-2005 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* MSN module - Switchboard server callbacks and utilities */ |
---|
| 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 <ctype.h> |
---|
| 27 | #include "nogaim.h" |
---|
| 28 | #include "msn.h" |
---|
| 29 | #include "passport.h" |
---|
| 30 | #include "md5.h" |
---|
| 31 | |
---|
[ba9edaa] | 32 | static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond ); |
---|
[b7d3cc34] | 33 | static int msn_sb_command( gpointer data, char **cmd, int num_parts ); |
---|
| 34 | static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); |
---|
| 35 | |
---|
| 36 | int msn_sb_write( struct msn_switchboard *sb, char *s, int len ) |
---|
| 37 | { |
---|
| 38 | int st; |
---|
| 39 | |
---|
| 40 | st = write( sb->fd, s, len ); |
---|
| 41 | if( st != len ) |
---|
| 42 | { |
---|
| 43 | msn_sb_destroy( sb ); |
---|
| 44 | return( 0 ); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | return( 1 ); |
---|
| 48 | } |
---|
| 49 | |
---|
[0da65d5] | 50 | struct msn_switchboard *msn_sb_create( struct im_connection *ic, char *host, int port, char *key, int session ) |
---|
[b7d3cc34] | 51 | { |
---|
[0da65d5] | 52 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 53 | struct msn_switchboard *sb = g_new0( struct msn_switchboard, 1 ); |
---|
| 54 | |
---|
| 55 | sb->fd = proxy_connect( host, port, msn_sb_connected, sb ); |
---|
| 56 | if( sb->fd < 0 ) |
---|
| 57 | { |
---|
| 58 | g_free( sb ); |
---|
| 59 | return( NULL ); |
---|
| 60 | } |
---|
| 61 | |
---|
[0da65d5] | 62 | sb->ic = ic; |
---|
[b7d3cc34] | 63 | sb->key = g_strdup( key ); |
---|
| 64 | sb->session = session; |
---|
| 65 | |
---|
| 66 | msn_switchboards = g_slist_append( msn_switchboards, sb ); |
---|
| 67 | md->switchboards = g_slist_append( md->switchboards, sb ); |
---|
| 68 | |
---|
| 69 | return( sb ); |
---|
| 70 | } |
---|
| 71 | |
---|
[0da65d5] | 72 | struct msn_switchboard *msn_sb_by_handle( struct im_connection *ic, char *handle ) |
---|
[b7d3cc34] | 73 | { |
---|
[0da65d5] | 74 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 75 | struct msn_switchboard *sb; |
---|
| 76 | GSList *l; |
---|
| 77 | |
---|
| 78 | for( l = md->switchboards; l; l = l->next ) |
---|
| 79 | { |
---|
| 80 | sb = l->data; |
---|
| 81 | if( sb->who && strcmp( sb->who, handle ) == 0 ) |
---|
| 82 | return( sb ); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return( NULL ); |
---|
| 86 | } |
---|
| 87 | |
---|
[0da65d5] | 88 | struct msn_switchboard *msn_sb_by_chat( struct groupchat *c ) |
---|
[b7d3cc34] | 89 | { |
---|
[0da65d5] | 90 | struct msn_data *md = c->ic->proto_data; |
---|
[b7d3cc34] | 91 | struct msn_switchboard *sb; |
---|
| 92 | GSList *l; |
---|
| 93 | |
---|
| 94 | for( l = md->switchboards; l; l = l->next ) |
---|
| 95 | { |
---|
| 96 | sb = l->data; |
---|
[fa29d093] | 97 | if( sb->chat == c ) |
---|
[b7d3cc34] | 98 | return( sb ); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return( NULL ); |
---|
| 102 | } |
---|
| 103 | |
---|
[0da65d5] | 104 | struct msn_switchboard *msn_sb_spare( struct im_connection *ic ) |
---|
[b7d3cc34] | 105 | { |
---|
[0da65d5] | 106 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 107 | struct msn_switchboard *sb; |
---|
| 108 | GSList *l; |
---|
| 109 | |
---|
| 110 | for( l = md->switchboards; l; l = l->next ) |
---|
| 111 | { |
---|
| 112 | sb = l->data; |
---|
| 113 | if( !sb->who && !sb->chat ) |
---|
| 114 | return( sb ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | return( NULL ); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int msn_sb_sendmessage( struct msn_switchboard *sb, char *text ) |
---|
| 121 | { |
---|
| 122 | if( sb->ready ) |
---|
| 123 | { |
---|
[bd28e6a] | 124 | char *packet, *buf; |
---|
[b7d3cc34] | 125 | int i, j; |
---|
| 126 | |
---|
[bd28e6a] | 127 | /* Build the message. Convert LF to CR-LF for normal messages. */ |
---|
[b7d3cc34] | 128 | if( strcmp( text, TYPING_NOTIFICATION_MESSAGE ) != 0 ) |
---|
| 129 | { |
---|
| 130 | buf = g_new0( char, sizeof( MSN_MESSAGE_HEADERS ) + strlen( text ) * 2 ); |
---|
| 131 | i = strlen( MSN_MESSAGE_HEADERS ); |
---|
| 132 | |
---|
| 133 | strcpy( buf, MSN_MESSAGE_HEADERS ); |
---|
| 134 | for( j = 0; text[j]; j ++ ) |
---|
| 135 | { |
---|
| 136 | if( text[j] == '\n' ) |
---|
| 137 | buf[i++] = '\r'; |
---|
| 138 | |
---|
| 139 | buf[i++] = text[j]; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
[c2fb3809] | 144 | i = strlen( MSN_TYPING_HEADERS ) + strlen( sb->ic->acc->user ); |
---|
[bd28e6a] | 145 | buf = g_new0( char, i ); |
---|
[c2fb3809] | 146 | i = g_snprintf( buf, i, MSN_TYPING_HEADERS, sb->ic->acc->user ); |
---|
[b7d3cc34] | 147 | } |
---|
| 148 | |
---|
[bd28e6a] | 149 | /* Build the final packet (MSG command + the message). */ |
---|
| 150 | packet = g_strdup_printf( "MSG %d N %d\r\n%s", ++sb->trId, i, buf ); |
---|
| 151 | g_free( buf ); |
---|
| 152 | if( msn_sb_write( sb, packet, strlen( packet ) ) ) |
---|
[b7d3cc34] | 153 | { |
---|
[bd28e6a] | 154 | g_free( packet ); |
---|
[b7d3cc34] | 155 | return( 1 ); |
---|
| 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
[bd28e6a] | 159 | g_free( packet ); |
---|
[b7d3cc34] | 160 | return( 0 ); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | else if( sb->who ) |
---|
| 164 | { |
---|
| 165 | struct msn_message *m = g_new0( struct msn_message, 1 ); |
---|
| 166 | |
---|
| 167 | m->who = g_strdup( "" ); |
---|
| 168 | m->text = g_strdup( text ); |
---|
| 169 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
| 170 | |
---|
| 171 | return( 1 ); |
---|
| 172 | } |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | return( 0 ); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
[0da65d5] | 179 | struct groupchat *msn_sb_to_chat( struct msn_switchboard *sb ) |
---|
[b7d3cc34] | 180 | { |
---|
[0da65d5] | 181 | struct im_connection *ic = sb->ic; |
---|
[b7d3cc34] | 182 | char buf[1024]; |
---|
| 183 | |
---|
| 184 | /* Create the groupchat structure. */ |
---|
| 185 | g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session ); |
---|
[61ae52c] | 186 | sb->chat = imcb_chat_new( ic, buf ); |
---|
[b7d3cc34] | 187 | |
---|
| 188 | /* Populate the channel. */ |
---|
[61ae52c] | 189 | if( sb->who ) imcb_chat_add_buddy( sb->chat, sb->who ); |
---|
| 190 | imcb_chat_add_buddy( sb->chat, ic->acc->user ); |
---|
[b7d3cc34] | 191 | |
---|
| 192 | /* And make sure the switchboard doesn't look like a regular chat anymore. */ |
---|
| 193 | if( sb->who ) |
---|
| 194 | { |
---|
| 195 | g_free( sb->who ); |
---|
| 196 | sb->who = NULL; |
---|
| 197 | } |
---|
[fa29d093] | 198 | |
---|
| 199 | return sb->chat; |
---|
[b7d3cc34] | 200 | } |
---|
| 201 | |
---|
| 202 | void msn_sb_destroy( struct msn_switchboard *sb ) |
---|
| 203 | { |
---|
[0da65d5] | 204 | struct im_connection *ic = sb->ic; |
---|
| 205 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 206 | |
---|
| 207 | debug( "Destroying switchboard: %s", sb->who ? sb->who : sb->key ? sb->key : "" ); |
---|
| 208 | |
---|
| 209 | if( sb->msgq ) |
---|
| 210 | { |
---|
| 211 | struct msn_message *m; |
---|
| 212 | GSList *l; |
---|
| 213 | |
---|
| 214 | for( l = sb->msgq; l; l = l->next ) |
---|
| 215 | { |
---|
| 216 | m = l->data; |
---|
[1053836] | 217 | |
---|
[b7d3cc34] | 218 | g_free( m->who ); |
---|
| 219 | g_free( m->text ); |
---|
| 220 | g_free( m ); |
---|
| 221 | } |
---|
| 222 | g_slist_free( sb->msgq ); |
---|
| 223 | |
---|
[84b045d] | 224 | imcb_log( ic, "Warning: Closing down MSN switchboard connection with " |
---|
[1053836] | 225 | "unsent message to %s, you'll have to resend it.", |
---|
[8ba511d] | 226 | sb->who ? sb->who : "(unknown)" ); |
---|
[b7d3cc34] | 227 | } |
---|
| 228 | |
---|
[8ba511d] | 229 | if( sb->key ) g_free( sb->key ); |
---|
| 230 | if( sb->who ) g_free( sb->who ); |
---|
| 231 | |
---|
[b7d3cc34] | 232 | if( sb->chat ) |
---|
| 233 | { |
---|
[e35d1a1] | 234 | imcb_chat_free( sb->chat ); |
---|
[b7d3cc34] | 235 | } |
---|
| 236 | |
---|
| 237 | if( sb->handler ) |
---|
| 238 | { |
---|
| 239 | if( sb->handler->rxq ) g_free( sb->handler->rxq ); |
---|
| 240 | if( sb->handler->cmd_text ) g_free( sb->handler->cmd_text ); |
---|
| 241 | g_free( sb->handler ); |
---|
| 242 | } |
---|
| 243 | |
---|
[ba9edaa] | 244 | if( sb->inp ) b_event_remove( sb->inp ); |
---|
[b7d3cc34] | 245 | closesocket( sb->fd ); |
---|
| 246 | |
---|
| 247 | msn_switchboards = g_slist_remove( msn_switchboards, sb ); |
---|
| 248 | md->switchboards = g_slist_remove( md->switchboards, sb ); |
---|
| 249 | g_free( sb ); |
---|
| 250 | } |
---|
| 251 | |
---|
[ba9edaa] | 252 | gboolean msn_sb_connected( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 253 | { |
---|
| 254 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 255 | struct im_connection *ic; |
---|
[b7d3cc34] | 256 | struct msn_data *md; |
---|
| 257 | char buf[1024]; |
---|
| 258 | |
---|
| 259 | /* Are we still alive? */ |
---|
| 260 | if( !g_slist_find( msn_switchboards, sb ) ) |
---|
[ba9edaa] | 261 | return FALSE; |
---|
[b7d3cc34] | 262 | |
---|
[0da65d5] | 263 | ic = sb->ic; |
---|
| 264 | md = ic->proto_data; |
---|
[b7d3cc34] | 265 | |
---|
| 266 | if( source != sb->fd ) |
---|
| 267 | { |
---|
| 268 | debug( "ERROR %d while connecting to switchboard server", 1 ); |
---|
| 269 | msn_sb_destroy( sb ); |
---|
[ba9edaa] | 270 | return FALSE; |
---|
[b7d3cc34] | 271 | } |
---|
| 272 | |
---|
| 273 | /* Prepare the callback */ |
---|
| 274 | sb->handler = g_new0( struct msn_handler_data, 1 ); |
---|
| 275 | sb->handler->fd = sb->fd; |
---|
| 276 | sb->handler->rxq = g_new0( char, 1 ); |
---|
| 277 | sb->handler->data = sb; |
---|
| 278 | sb->handler->exec_command = msn_sb_command; |
---|
| 279 | sb->handler->exec_message = msn_sb_message; |
---|
| 280 | |
---|
| 281 | if( sb->session == MSN_SB_NEW ) |
---|
[c2fb3809] | 282 | g_snprintf( buf, sizeof( buf ), "USR %d %s %s\r\n", ++sb->trId, ic->acc->user, sb->key ); |
---|
[b7d3cc34] | 283 | else |
---|
[c2fb3809] | 284 | g_snprintf( buf, sizeof( buf ), "ANS %d %s %s %d\r\n", ++sb->trId, ic->acc->user, sb->key, sb->session ); |
---|
[b7d3cc34] | 285 | |
---|
| 286 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
[ba9edaa] | 287 | sb->inp = b_input_add( sb->fd, GAIM_INPUT_READ, msn_sb_callback, sb ); |
---|
[b7d3cc34] | 288 | else |
---|
| 289 | debug( "ERROR %d while connecting to switchboard server", 2 ); |
---|
[ba9edaa] | 290 | |
---|
| 291 | return FALSE; |
---|
[b7d3cc34] | 292 | } |
---|
| 293 | |
---|
[ba9edaa] | 294 | static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 295 | { |
---|
| 296 | struct msn_switchboard *sb = data; |
---|
| 297 | |
---|
| 298 | if( msn_handler( sb->handler ) == -1 ) |
---|
| 299 | { |
---|
| 300 | debug( "ERROR: Switchboard died" ); |
---|
| 301 | msn_sb_destroy( sb ); |
---|
[ba9edaa] | 302 | |
---|
| 303 | return FALSE; |
---|
[b7d3cc34] | 304 | } |
---|
[ba9edaa] | 305 | else |
---|
| 306 | return TRUE; |
---|
[b7d3cc34] | 307 | } |
---|
| 308 | |
---|
| 309 | static int msn_sb_command( gpointer data, char **cmd, int num_parts ) |
---|
| 310 | { |
---|
| 311 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 312 | struct im_connection *ic = sb->ic; |
---|
[b7d3cc34] | 313 | char buf[1024]; |
---|
| 314 | |
---|
| 315 | if( !num_parts ) |
---|
| 316 | { |
---|
| 317 | /* Hrrm... Empty command...? Ignore? */ |
---|
| 318 | return( 1 ); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
| 322 | { |
---|
[84b045d] | 323 | imcb_error( ic, "Received an XFR from a switchboard server, unable to comply! This is likely to be a bug, please report it!" ); |
---|
[c2fb3809] | 324 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 325 | return( 0 ); |
---|
| 326 | } |
---|
| 327 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
| 328 | { |
---|
| 329 | if( num_parts != 5 ) |
---|
| 330 | { |
---|
| 331 | msn_sb_destroy( sb ); |
---|
| 332 | return( 0 ); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | if( strcmp( cmd[2], "OK" ) != 0 ) |
---|
| 336 | { |
---|
| 337 | msn_sb_destroy( sb ); |
---|
| 338 | return( 0 ); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | if( sb->who ) |
---|
| 342 | { |
---|
| 343 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, sb->who ); |
---|
| 344 | return( msn_sb_write( sb, buf, strlen( buf ) ) ); |
---|
| 345 | } |
---|
| 346 | else |
---|
| 347 | { |
---|
| 348 | debug( "Just created a switchboard, but I don't know what to do with it." ); |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | else if( strcmp( cmd[0], "IRO" ) == 0 ) |
---|
| 352 | { |
---|
| 353 | int num, tot; |
---|
| 354 | |
---|
| 355 | if( num_parts != 6 ) |
---|
| 356 | { |
---|
| 357 | msn_sb_destroy( sb ); |
---|
| 358 | return( 0 ); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | num = atoi( cmd[2] ); |
---|
| 362 | tot = atoi( cmd[3] ); |
---|
| 363 | |
---|
| 364 | if( tot <= 0 ) |
---|
| 365 | { |
---|
| 366 | msn_sb_destroy( sb ); |
---|
| 367 | return( 0 ); |
---|
| 368 | } |
---|
| 369 | else if( tot > 1 ) |
---|
| 370 | { |
---|
| 371 | char buf[1024]; |
---|
| 372 | |
---|
| 373 | if( num == 1 ) |
---|
| 374 | { |
---|
| 375 | g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session ); |
---|
[61ae52c] | 376 | sb->chat = imcb_chat_new( ic, buf ); |
---|
[b7d3cc34] | 377 | |
---|
| 378 | g_free( sb->who ); |
---|
| 379 | sb->who = NULL; |
---|
| 380 | } |
---|
| 381 | |
---|
[61ae52c] | 382 | imcb_chat_add_buddy( sb->chat, cmd[4] ); |
---|
[b7d3cc34] | 383 | |
---|
| 384 | if( num == tot ) |
---|
| 385 | { |
---|
[61ae52c] | 386 | imcb_chat_add_buddy( sb->chat, ic->acc->user ); |
---|
[b7d3cc34] | 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | else if( strcmp( cmd[0], "ANS" ) == 0 ) |
---|
| 391 | { |
---|
| 392 | if( num_parts != 3 ) |
---|
| 393 | { |
---|
| 394 | msn_sb_destroy( sb ); |
---|
| 395 | return( 0 ); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | if( strcmp( cmd[2], "OK" ) != 0 ) |
---|
| 399 | { |
---|
| 400 | debug( "Switchboard server sent a negative ANS reply" ); |
---|
| 401 | msn_sb_destroy( sb ); |
---|
| 402 | return( 0 ); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | sb->ready = 1; |
---|
| 406 | } |
---|
| 407 | else if( strcmp( cmd[0], "CAL" ) == 0 ) |
---|
| 408 | { |
---|
| 409 | if( num_parts != 4 || !isdigit( cmd[3][0] ) ) |
---|
| 410 | { |
---|
| 411 | msn_sb_destroy( sb ); |
---|
| 412 | return( 0 ); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | sb->session = atoi( cmd[3] ); |
---|
| 416 | } |
---|
| 417 | else if( strcmp( cmd[0], "JOI" ) == 0 ) |
---|
| 418 | { |
---|
| 419 | if( num_parts != 3 ) |
---|
| 420 | { |
---|
| 421 | msn_sb_destroy( sb ); |
---|
| 422 | return( 0 ); |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | if( sb->who && g_strcasecmp( cmd[1], sb->who ) == 0 ) |
---|
| 426 | { |
---|
| 427 | /* The user we wanted to talk to is finally there, let's send the queued messages then. */ |
---|
| 428 | struct msn_message *m; |
---|
| 429 | GSList *l; |
---|
| 430 | int st = 1; |
---|
| 431 | |
---|
| 432 | debug( "%s arrived in the switchboard session, now sending queued message(s)", cmd[1] ); |
---|
| 433 | |
---|
| 434 | /* Without this, sendmessage() will put everything back on the queue... */ |
---|
| 435 | sb->ready = 1; |
---|
| 436 | |
---|
| 437 | while( ( l = sb->msgq ) ) |
---|
| 438 | { |
---|
| 439 | m = l->data; |
---|
| 440 | if( st ) |
---|
| 441 | { |
---|
| 442 | /* This hack is meant to convert a regular new chat into a groupchat */ |
---|
| 443 | if( strcmp( m->text, GROUPCHAT_SWITCHBOARD_MESSAGE ) == 0 ) |
---|
| 444 | msn_sb_to_chat( sb ); |
---|
| 445 | else |
---|
| 446 | st = msn_sb_sendmessage( sb, m->text ); |
---|
| 447 | } |
---|
| 448 | g_free( m->text ); |
---|
| 449 | g_free( m->who ); |
---|
| 450 | g_free( m ); |
---|
| 451 | |
---|
| 452 | sb->msgq = g_slist_remove( sb->msgq, m ); |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | return( st ); |
---|
| 456 | } |
---|
| 457 | else if( sb->who ) |
---|
| 458 | { |
---|
| 459 | debug( "Converting chat with %s to a groupchat because %s joined the session.", sb->who, cmd[1] ); |
---|
| 460 | |
---|
| 461 | /* This SB is a one-to-one chat right now, but someone else is joining. */ |
---|
| 462 | msn_sb_to_chat( sb ); |
---|
| 463 | |
---|
[61ae52c] | 464 | imcb_chat_add_buddy( sb->chat, cmd[1] ); |
---|
[b7d3cc34] | 465 | } |
---|
| 466 | else if( sb->chat ) |
---|
| 467 | { |
---|
[61ae52c] | 468 | imcb_chat_add_buddy( sb->chat, cmd[1] ); |
---|
[b7d3cc34] | 469 | sb->ready = 1; |
---|
| 470 | } |
---|
| 471 | else |
---|
| 472 | { |
---|
| 473 | /* PANIC! */ |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 477 | { |
---|
| 478 | if( num_parts != 4 ) |
---|
| 479 | { |
---|
| 480 | msn_sb_destroy( sb ); |
---|
| 481 | return( 0 ); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | sb->handler->msglen = atoi( cmd[3] ); |
---|
| 485 | |
---|
| 486 | if( sb->handler->msglen <= 0 ) |
---|
| 487 | { |
---|
| 488 | debug( "Received a corrupted message on the switchboard, the switchboard will be closed" ); |
---|
| 489 | msn_sb_destroy( sb ); |
---|
| 490 | return( 0 ); |
---|
| 491 | } |
---|
| 492 | } |
---|
| 493 | else if( strcmp( cmd[0], "BYE" ) == 0 ) |
---|
| 494 | { |
---|
| 495 | if( num_parts < 2 ) |
---|
| 496 | { |
---|
| 497 | msn_sb_destroy( sb ); |
---|
| 498 | return( 0 ); |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | /* if( cmd[2] && *cmd[2] == '1' ) -=> Chat is being cleaned up because of idleness */ |
---|
| 502 | |
---|
| 503 | if( sb->who ) |
---|
| 504 | { |
---|
| 505 | /* This is a single-person chat, and the other person is leaving. */ |
---|
| 506 | g_free( sb->who ); |
---|
| 507 | sb->who = NULL; |
---|
| 508 | sb->ready = 0; |
---|
| 509 | |
---|
| 510 | debug( "Person %s left the one-to-one switchboard connection. Keeping it around as a spare...", cmd[1] ); |
---|
| 511 | |
---|
| 512 | /* We could clean up the switchboard now, but keeping it around |
---|
| 513 | as a spare for a next conversation sounds more sane to me. |
---|
| 514 | The server will clean it up when it's idle for too long. */ |
---|
| 515 | } |
---|
| 516 | else if( sb->chat ) |
---|
| 517 | { |
---|
[61ae52c] | 518 | imcb_chat_remove_buddy( sb->chat, cmd[1], "" ); |
---|
[b7d3cc34] | 519 | } |
---|
| 520 | else |
---|
| 521 | { |
---|
| 522 | /* PANIC! */ |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | else if( isdigit( cmd[0][0] ) ) |
---|
| 526 | { |
---|
| 527 | int num = atoi( cmd[0] ); |
---|
[08995b0] | 528 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
[b7d3cc34] | 529 | |
---|
[84b045d] | 530 | imcb_error( ic, "Error reported by switchboard server: %s", err->text ); |
---|
[b7d3cc34] | 531 | |
---|
| 532 | if( err->flags & STATUS_SB_FATAL ) |
---|
| 533 | { |
---|
| 534 | msn_sb_destroy( sb ); |
---|
[6048744] | 535 | return 0; |
---|
[b7d3cc34] | 536 | } |
---|
[6048744] | 537 | else if( err->flags & STATUS_FATAL ) |
---|
[b7d3cc34] | 538 | { |
---|
[c2fb3809] | 539 | imc_logout( ic, TRUE ); |
---|
[6048744] | 540 | return 0; |
---|
[b7d3cc34] | 541 | } |
---|
[6048744] | 542 | else if( err->flags & STATUS_SB_IM_SPARE ) |
---|
[3b9390b] | 543 | { |
---|
| 544 | if( sb->who ) |
---|
| 545 | { |
---|
| 546 | struct msn_message *m; |
---|
| 547 | GSList *l; |
---|
| 548 | |
---|
| 549 | /* Apparently some invitation failed. We might want to use this |
---|
| 550 | board later, so keep it as a spare. */ |
---|
| 551 | g_free( sb->who ); |
---|
| 552 | sb->who = NULL; |
---|
| 553 | |
---|
| 554 | /* Also clear the msgq, otherwise someone else might get them. */ |
---|
| 555 | for( l = sb->msgq; l; l = l->next ) |
---|
| 556 | { |
---|
| 557 | m = l->data; |
---|
| 558 | g_free( m->who ); |
---|
| 559 | g_free( m->text ); |
---|
| 560 | g_free( m ); |
---|
| 561 | } |
---|
| 562 | g_slist_free( sb->msgq ); |
---|
| 563 | sb->msgq = NULL; |
---|
| 564 | } |
---|
[6048744] | 565 | |
---|
| 566 | /* Do NOT return 0 here, we want to keep this sb. */ |
---|
[3b9390b] | 567 | } |
---|
[b7d3cc34] | 568 | } |
---|
| 569 | else |
---|
| 570 | { |
---|
| 571 | debug( "Received unknown command from switchboard server: %s", cmd[0] ); |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | return( 1 ); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
| 578 | { |
---|
| 579 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 580 | struct im_connection *ic = sb->ic; |
---|
[b7d3cc34] | 581 | char *body; |
---|
| 582 | int blen = 0; |
---|
| 583 | |
---|
| 584 | if( !num_parts ) |
---|
| 585 | return( 1 ); |
---|
| 586 | |
---|
| 587 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
| 588 | { |
---|
| 589 | body += 4; |
---|
| 590 | blen = msglen - ( body - msg ); |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 594 | { |
---|
| 595 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
| 596 | |
---|
| 597 | if( !ct ) |
---|
| 598 | return( 1 ); |
---|
| 599 | |
---|
| 600 | if( g_strncasecmp( ct, "text/plain", 10 ) == 0 ) |
---|
| 601 | { |
---|
| 602 | g_free( ct ); |
---|
| 603 | |
---|
| 604 | if( !body ) |
---|
| 605 | return( 1 ); |
---|
| 606 | |
---|
| 607 | if( sb->who ) |
---|
| 608 | { |
---|
[9624fdf] | 609 | imcb_buddy_msg( ic, cmd[1], body, 0, 0 ); |
---|
[b7d3cc34] | 610 | } |
---|
| 611 | else if( sb->chat ) |
---|
| 612 | { |
---|
[61ae52c] | 613 | imcb_chat_msg( sb->chat, cmd[1], body, 0, 0 ); |
---|
[b7d3cc34] | 614 | } |
---|
| 615 | else |
---|
| 616 | { |
---|
| 617 | /* PANIC! */ |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 ) |
---|
| 621 | { |
---|
| 622 | char *itype = msn_findheader( body, "Application-GUID:", blen ); |
---|
| 623 | char buf[1024]; |
---|
| 624 | |
---|
| 625 | g_free( ct ); |
---|
| 626 | |
---|
| 627 | *buf = 0; |
---|
| 628 | |
---|
| 629 | if( !itype ) |
---|
| 630 | return( 1 ); |
---|
| 631 | |
---|
| 632 | /* File transfer. */ |
---|
| 633 | if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 ) |
---|
| 634 | { |
---|
| 635 | char *name = msn_findheader( body, "Application-File:", blen ); |
---|
| 636 | char *size = msn_findheader( body, "Application-FileSize:", blen ); |
---|
| 637 | |
---|
| 638 | if( name && size ) |
---|
| 639 | { |
---|
| 640 | g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n" |
---|
| 641 | "Filetransfers are not supported by BitlBee for now...", name, size ); |
---|
| 642 | } |
---|
| 643 | else |
---|
| 644 | { |
---|
| 645 | strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" ); |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | if( name ) g_free( name ); |
---|
| 649 | if( size ) g_free( size ); |
---|
| 650 | } |
---|
| 651 | else |
---|
| 652 | { |
---|
| 653 | char *iname = msn_findheader( body, "Application-Name:", blen ); |
---|
| 654 | |
---|
| 655 | g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>", |
---|
| 656 | itype, iname ? iname : "no name" ); |
---|
| 657 | |
---|
| 658 | if( iname ) g_free( iname ); |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | g_free( itype ); |
---|
| 662 | |
---|
| 663 | if( !*buf ) |
---|
| 664 | return( 1 ); |
---|
| 665 | |
---|
| 666 | if( sb->who ) |
---|
| 667 | { |
---|
[9624fdf] | 668 | imcb_buddy_msg( ic, cmd[1], buf, 0, 0 ); |
---|
[b7d3cc34] | 669 | } |
---|
| 670 | else if( sb->chat ) |
---|
| 671 | { |
---|
[61ae52c] | 672 | imcb_chat_msg( sb->chat, cmd[1], buf, 0, 0 ); |
---|
[b7d3cc34] | 673 | } |
---|
| 674 | else |
---|
| 675 | { |
---|
| 676 | /* PANIC! */ |
---|
| 677 | } |
---|
| 678 | } |
---|
| 679 | else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 ) |
---|
| 680 | { |
---|
| 681 | char *who = msn_findheader( msg, "TypingUser:", msglen ); |
---|
| 682 | |
---|
| 683 | if( who ) |
---|
| 684 | { |
---|
[9624fdf] | 685 | imcb_buddy_typing( ic, who, OPT_TYPING ); |
---|
[b7d3cc34] | 686 | g_free( who ); |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | g_free( ct ); |
---|
| 690 | } |
---|
| 691 | else |
---|
| 692 | { |
---|
| 693 | g_free( ct ); |
---|
| 694 | } |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | return( 1 ); |
---|
| 698 | } |
---|