[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 | { |
---|
[46dca11] | 130 | buf = g_new0( char, sizeof( MSN_MESSAGE_HEADERS ) + strlen( text ) * 2 + 1 ); |
---|
[b7d3cc34] | 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 | |
---|
[46dca11] | 209 | msn_msgq_purge( ic, &sb->msgq ); |
---|
[b7d3cc34] | 210 | |
---|
[8ba511d] | 211 | if( sb->key ) g_free( sb->key ); |
---|
| 212 | if( sb->who ) g_free( sb->who ); |
---|
| 213 | |
---|
[b7d3cc34] | 214 | if( sb->chat ) |
---|
| 215 | { |
---|
[e35d1a1] | 216 | imcb_chat_free( sb->chat ); |
---|
[b7d3cc34] | 217 | } |
---|
| 218 | |
---|
| 219 | if( sb->handler ) |
---|
| 220 | { |
---|
| 221 | if( sb->handler->rxq ) g_free( sb->handler->rxq ); |
---|
| 222 | if( sb->handler->cmd_text ) g_free( sb->handler->cmd_text ); |
---|
| 223 | g_free( sb->handler ); |
---|
| 224 | } |
---|
| 225 | |
---|
[ba9edaa] | 226 | if( sb->inp ) b_event_remove( sb->inp ); |
---|
[b7d3cc34] | 227 | closesocket( sb->fd ); |
---|
| 228 | |
---|
| 229 | msn_switchboards = g_slist_remove( msn_switchboards, sb ); |
---|
| 230 | md->switchboards = g_slist_remove( md->switchboards, sb ); |
---|
| 231 | g_free( sb ); |
---|
| 232 | } |
---|
| 233 | |
---|
[ba9edaa] | 234 | gboolean msn_sb_connected( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 235 | { |
---|
| 236 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 237 | struct im_connection *ic; |
---|
[b7d3cc34] | 238 | struct msn_data *md; |
---|
| 239 | char buf[1024]; |
---|
| 240 | |
---|
| 241 | /* Are we still alive? */ |
---|
| 242 | if( !g_slist_find( msn_switchboards, sb ) ) |
---|
[ba9edaa] | 243 | return FALSE; |
---|
[b7d3cc34] | 244 | |
---|
[0da65d5] | 245 | ic = sb->ic; |
---|
| 246 | md = ic->proto_data; |
---|
[b7d3cc34] | 247 | |
---|
| 248 | if( source != sb->fd ) |
---|
| 249 | { |
---|
[46dca11] | 250 | debug( "Error %d while connecting to switchboard server", 1 ); |
---|
[b7d3cc34] | 251 | msn_sb_destroy( sb ); |
---|
[ba9edaa] | 252 | return FALSE; |
---|
[b7d3cc34] | 253 | } |
---|
| 254 | |
---|
| 255 | /* Prepare the callback */ |
---|
| 256 | sb->handler = g_new0( struct msn_handler_data, 1 ); |
---|
| 257 | sb->handler->fd = sb->fd; |
---|
| 258 | sb->handler->rxq = g_new0( char, 1 ); |
---|
| 259 | sb->handler->data = sb; |
---|
| 260 | sb->handler->exec_command = msn_sb_command; |
---|
| 261 | sb->handler->exec_message = msn_sb_message; |
---|
| 262 | |
---|
| 263 | if( sb->session == MSN_SB_NEW ) |
---|
[c2fb3809] | 264 | g_snprintf( buf, sizeof( buf ), "USR %d %s %s\r\n", ++sb->trId, ic->acc->user, sb->key ); |
---|
[b7d3cc34] | 265 | else |
---|
[c2fb3809] | 266 | g_snprintf( buf, sizeof( buf ), "ANS %d %s %s %d\r\n", ++sb->trId, ic->acc->user, sb->key, sb->session ); |
---|
[b7d3cc34] | 267 | |
---|
| 268 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
[ba9edaa] | 269 | sb->inp = b_input_add( sb->fd, GAIM_INPUT_READ, msn_sb_callback, sb ); |
---|
[b7d3cc34] | 270 | else |
---|
[46dca11] | 271 | debug( "Error %d while connecting to switchboard server", 2 ); |
---|
[ba9edaa] | 272 | |
---|
| 273 | return FALSE; |
---|
[b7d3cc34] | 274 | } |
---|
| 275 | |
---|
[ba9edaa] | 276 | static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 277 | { |
---|
| 278 | struct msn_switchboard *sb = data; |
---|
| 279 | |
---|
| 280 | if( msn_handler( sb->handler ) == -1 ) |
---|
| 281 | { |
---|
[46dca11] | 282 | debug( "Error: Switchboard died" ); |
---|
[b7d3cc34] | 283 | msn_sb_destroy( sb ); |
---|
[ba9edaa] | 284 | |
---|
| 285 | return FALSE; |
---|
[b7d3cc34] | 286 | } |
---|
[ba9edaa] | 287 | else |
---|
| 288 | return TRUE; |
---|
[b7d3cc34] | 289 | } |
---|
| 290 | |
---|
| 291 | static int msn_sb_command( gpointer data, char **cmd, int num_parts ) |
---|
| 292 | { |
---|
| 293 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 294 | struct im_connection *ic = sb->ic; |
---|
[b7d3cc34] | 295 | char buf[1024]; |
---|
| 296 | |
---|
| 297 | if( !num_parts ) |
---|
| 298 | { |
---|
| 299 | /* Hrrm... Empty command...? Ignore? */ |
---|
| 300 | return( 1 ); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
| 304 | { |
---|
[84b045d] | 305 | imcb_error( ic, "Received an XFR from a switchboard server, unable to comply! This is likely to be a bug, please report it!" ); |
---|
[c2fb3809] | 306 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 307 | return( 0 ); |
---|
| 308 | } |
---|
| 309 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
| 310 | { |
---|
| 311 | if( num_parts != 5 ) |
---|
| 312 | { |
---|
| 313 | msn_sb_destroy( sb ); |
---|
| 314 | return( 0 ); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | if( strcmp( cmd[2], "OK" ) != 0 ) |
---|
| 318 | { |
---|
| 319 | msn_sb_destroy( sb ); |
---|
| 320 | return( 0 ); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | if( sb->who ) |
---|
| 324 | { |
---|
| 325 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, sb->who ); |
---|
| 326 | return( msn_sb_write( sb, buf, strlen( buf ) ) ); |
---|
| 327 | } |
---|
| 328 | else |
---|
| 329 | { |
---|
| 330 | debug( "Just created a switchboard, but I don't know what to do with it." ); |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | else if( strcmp( cmd[0], "IRO" ) == 0 ) |
---|
| 334 | { |
---|
| 335 | int num, tot; |
---|
| 336 | |
---|
| 337 | if( num_parts != 6 ) |
---|
| 338 | { |
---|
| 339 | msn_sb_destroy( sb ); |
---|
| 340 | return( 0 ); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | num = atoi( cmd[2] ); |
---|
| 344 | tot = atoi( cmd[3] ); |
---|
| 345 | |
---|
| 346 | if( tot <= 0 ) |
---|
| 347 | { |
---|
| 348 | msn_sb_destroy( sb ); |
---|
| 349 | return( 0 ); |
---|
| 350 | } |
---|
| 351 | else if( tot > 1 ) |
---|
| 352 | { |
---|
| 353 | char buf[1024]; |
---|
| 354 | |
---|
| 355 | if( num == 1 ) |
---|
| 356 | { |
---|
| 357 | g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session ); |
---|
[61ae52c] | 358 | sb->chat = imcb_chat_new( ic, buf ); |
---|
[b7d3cc34] | 359 | |
---|
| 360 | g_free( sb->who ); |
---|
| 361 | sb->who = NULL; |
---|
| 362 | } |
---|
| 363 | |
---|
[61ae52c] | 364 | imcb_chat_add_buddy( sb->chat, cmd[4] ); |
---|
[b7d3cc34] | 365 | |
---|
| 366 | if( num == tot ) |
---|
| 367 | { |
---|
[61ae52c] | 368 | imcb_chat_add_buddy( sb->chat, ic->acc->user ); |
---|
[b7d3cc34] | 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | else if( strcmp( cmd[0], "ANS" ) == 0 ) |
---|
| 373 | { |
---|
| 374 | if( num_parts != 3 ) |
---|
| 375 | { |
---|
| 376 | msn_sb_destroy( sb ); |
---|
| 377 | return( 0 ); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | if( strcmp( cmd[2], "OK" ) != 0 ) |
---|
| 381 | { |
---|
| 382 | debug( "Switchboard server sent a negative ANS reply" ); |
---|
| 383 | msn_sb_destroy( sb ); |
---|
| 384 | return( 0 ); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | sb->ready = 1; |
---|
| 388 | } |
---|
| 389 | else if( strcmp( cmd[0], "CAL" ) == 0 ) |
---|
| 390 | { |
---|
| 391 | if( num_parts != 4 || !isdigit( cmd[3][0] ) ) |
---|
| 392 | { |
---|
| 393 | msn_sb_destroy( sb ); |
---|
| 394 | return( 0 ); |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | sb->session = atoi( cmd[3] ); |
---|
| 398 | } |
---|
| 399 | else if( strcmp( cmd[0], "JOI" ) == 0 ) |
---|
| 400 | { |
---|
| 401 | if( num_parts != 3 ) |
---|
| 402 | { |
---|
| 403 | msn_sb_destroy( sb ); |
---|
| 404 | return( 0 ); |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | if( sb->who && g_strcasecmp( cmd[1], sb->who ) == 0 ) |
---|
| 408 | { |
---|
| 409 | /* The user we wanted to talk to is finally there, let's send the queued messages then. */ |
---|
| 410 | struct msn_message *m; |
---|
| 411 | GSList *l; |
---|
| 412 | int st = 1; |
---|
| 413 | |
---|
| 414 | debug( "%s arrived in the switchboard session, now sending queued message(s)", cmd[1] ); |
---|
| 415 | |
---|
| 416 | /* Without this, sendmessage() will put everything back on the queue... */ |
---|
| 417 | sb->ready = 1; |
---|
| 418 | |
---|
| 419 | while( ( l = sb->msgq ) ) |
---|
| 420 | { |
---|
| 421 | m = l->data; |
---|
| 422 | if( st ) |
---|
| 423 | { |
---|
| 424 | /* This hack is meant to convert a regular new chat into a groupchat */ |
---|
| 425 | if( strcmp( m->text, GROUPCHAT_SWITCHBOARD_MESSAGE ) == 0 ) |
---|
| 426 | msn_sb_to_chat( sb ); |
---|
| 427 | else |
---|
| 428 | st = msn_sb_sendmessage( sb, m->text ); |
---|
| 429 | } |
---|
| 430 | g_free( m->text ); |
---|
| 431 | g_free( m->who ); |
---|
| 432 | g_free( m ); |
---|
| 433 | |
---|
| 434 | sb->msgq = g_slist_remove( sb->msgq, m ); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | return( st ); |
---|
| 438 | } |
---|
| 439 | else if( sb->who ) |
---|
| 440 | { |
---|
| 441 | debug( "Converting chat with %s to a groupchat because %s joined the session.", sb->who, cmd[1] ); |
---|
| 442 | |
---|
| 443 | /* This SB is a one-to-one chat right now, but someone else is joining. */ |
---|
| 444 | msn_sb_to_chat( sb ); |
---|
| 445 | |
---|
[61ae52c] | 446 | imcb_chat_add_buddy( sb->chat, cmd[1] ); |
---|
[b7d3cc34] | 447 | } |
---|
| 448 | else if( sb->chat ) |
---|
| 449 | { |
---|
[61ae52c] | 450 | imcb_chat_add_buddy( sb->chat, cmd[1] ); |
---|
[b7d3cc34] | 451 | sb->ready = 1; |
---|
| 452 | } |
---|
| 453 | else |
---|
| 454 | { |
---|
| 455 | /* PANIC! */ |
---|
| 456 | } |
---|
| 457 | } |
---|
| 458 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 459 | { |
---|
| 460 | if( num_parts != 4 ) |
---|
| 461 | { |
---|
| 462 | msn_sb_destroy( sb ); |
---|
| 463 | return( 0 ); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | sb->handler->msglen = atoi( cmd[3] ); |
---|
| 467 | |
---|
| 468 | if( sb->handler->msglen <= 0 ) |
---|
| 469 | { |
---|
| 470 | debug( "Received a corrupted message on the switchboard, the switchboard will be closed" ); |
---|
| 471 | msn_sb_destroy( sb ); |
---|
| 472 | return( 0 ); |
---|
| 473 | } |
---|
| 474 | } |
---|
[3585c5a] | 475 | else if( strcmp( cmd[0], "NAK" ) == 0 ) |
---|
| 476 | { |
---|
| 477 | if( sb->who ) |
---|
| 478 | { |
---|
| 479 | imcb_log( ic, "The MSN servers could not deliver one of your messages to %s.", sb->who ); |
---|
| 480 | } |
---|
| 481 | else |
---|
| 482 | { |
---|
| 483 | imcb_log( ic, "The MSN servers could not deliver one of your groupchat messages to all participants." ); |
---|
| 484 | } |
---|
| 485 | } |
---|
[b7d3cc34] | 486 | else if( strcmp( cmd[0], "BYE" ) == 0 ) |
---|
| 487 | { |
---|
| 488 | if( num_parts < 2 ) |
---|
| 489 | { |
---|
| 490 | msn_sb_destroy( sb ); |
---|
| 491 | return( 0 ); |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | /* if( cmd[2] && *cmd[2] == '1' ) -=> Chat is being cleaned up because of idleness */ |
---|
| 495 | |
---|
| 496 | if( sb->who ) |
---|
| 497 | { |
---|
| 498 | /* This is a single-person chat, and the other person is leaving. */ |
---|
| 499 | g_free( sb->who ); |
---|
| 500 | sb->who = NULL; |
---|
| 501 | sb->ready = 0; |
---|
| 502 | |
---|
| 503 | debug( "Person %s left the one-to-one switchboard connection. Keeping it around as a spare...", cmd[1] ); |
---|
| 504 | |
---|
| 505 | /* We could clean up the switchboard now, but keeping it around |
---|
| 506 | as a spare for a next conversation sounds more sane to me. |
---|
| 507 | The server will clean it up when it's idle for too long. */ |
---|
| 508 | } |
---|
| 509 | else if( sb->chat ) |
---|
| 510 | { |
---|
[61ae52c] | 511 | imcb_chat_remove_buddy( sb->chat, cmd[1], "" ); |
---|
[b7d3cc34] | 512 | } |
---|
| 513 | else |
---|
| 514 | { |
---|
| 515 | /* PANIC! */ |
---|
| 516 | } |
---|
| 517 | } |
---|
| 518 | else if( isdigit( cmd[0][0] ) ) |
---|
| 519 | { |
---|
| 520 | int num = atoi( cmd[0] ); |
---|
[08995b0] | 521 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
[b7d3cc34] | 522 | |
---|
[84b045d] | 523 | imcb_error( ic, "Error reported by switchboard server: %s", err->text ); |
---|
[b7d3cc34] | 524 | |
---|
| 525 | if( err->flags & STATUS_SB_FATAL ) |
---|
| 526 | { |
---|
| 527 | msn_sb_destroy( sb ); |
---|
[6048744] | 528 | return 0; |
---|
[b7d3cc34] | 529 | } |
---|
[6048744] | 530 | else if( err->flags & STATUS_FATAL ) |
---|
[b7d3cc34] | 531 | { |
---|
[c2fb3809] | 532 | imc_logout( ic, TRUE ); |
---|
[6048744] | 533 | return 0; |
---|
[b7d3cc34] | 534 | } |
---|
[6048744] | 535 | else if( err->flags & STATUS_SB_IM_SPARE ) |
---|
[3b9390b] | 536 | { |
---|
| 537 | if( sb->who ) |
---|
| 538 | { |
---|
| 539 | /* Apparently some invitation failed. We might want to use this |
---|
| 540 | board later, so keep it as a spare. */ |
---|
| 541 | g_free( sb->who ); |
---|
| 542 | sb->who = NULL; |
---|
| 543 | |
---|
| 544 | /* Also clear the msgq, otherwise someone else might get them. */ |
---|
[46dca11] | 545 | msn_msgq_purge( ic, &sb->msgq ); |
---|
[3b9390b] | 546 | } |
---|
[6048744] | 547 | |
---|
| 548 | /* Do NOT return 0 here, we want to keep this sb. */ |
---|
[3b9390b] | 549 | } |
---|
[b7d3cc34] | 550 | } |
---|
| 551 | else |
---|
| 552 | { |
---|
| 553 | debug( "Received unknown command from switchboard server: %s", cmd[0] ); |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | return( 1 ); |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
| 560 | { |
---|
| 561 | struct msn_switchboard *sb = data; |
---|
[0da65d5] | 562 | struct im_connection *ic = sb->ic; |
---|
[b7d3cc34] | 563 | char *body; |
---|
| 564 | int blen = 0; |
---|
| 565 | |
---|
| 566 | if( !num_parts ) |
---|
| 567 | return( 1 ); |
---|
| 568 | |
---|
| 569 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
| 570 | { |
---|
| 571 | body += 4; |
---|
| 572 | blen = msglen - ( body - msg ); |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 576 | { |
---|
| 577 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
| 578 | |
---|
| 579 | if( !ct ) |
---|
| 580 | return( 1 ); |
---|
| 581 | |
---|
| 582 | if( g_strncasecmp( ct, "text/plain", 10 ) == 0 ) |
---|
| 583 | { |
---|
| 584 | g_free( ct ); |
---|
| 585 | |
---|
| 586 | if( !body ) |
---|
| 587 | return( 1 ); |
---|
| 588 | |
---|
| 589 | if( sb->who ) |
---|
| 590 | { |
---|
[9624fdf] | 591 | imcb_buddy_msg( ic, cmd[1], body, 0, 0 ); |
---|
[b7d3cc34] | 592 | } |
---|
| 593 | else if( sb->chat ) |
---|
| 594 | { |
---|
[61ae52c] | 595 | imcb_chat_msg( sb->chat, cmd[1], body, 0, 0 ); |
---|
[b7d3cc34] | 596 | } |
---|
| 597 | else |
---|
| 598 | { |
---|
| 599 | /* PANIC! */ |
---|
| 600 | } |
---|
| 601 | } |
---|
| 602 | else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 ) |
---|
| 603 | { |
---|
| 604 | char *itype = msn_findheader( body, "Application-GUID:", blen ); |
---|
| 605 | char buf[1024]; |
---|
| 606 | |
---|
| 607 | g_free( ct ); |
---|
| 608 | |
---|
| 609 | *buf = 0; |
---|
| 610 | |
---|
| 611 | if( !itype ) |
---|
| 612 | return( 1 ); |
---|
| 613 | |
---|
| 614 | /* File transfer. */ |
---|
| 615 | if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 ) |
---|
| 616 | { |
---|
| 617 | char *name = msn_findheader( body, "Application-File:", blen ); |
---|
| 618 | char *size = msn_findheader( body, "Application-FileSize:", blen ); |
---|
| 619 | |
---|
| 620 | if( name && size ) |
---|
| 621 | { |
---|
| 622 | g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n" |
---|
| 623 | "Filetransfers are not supported by BitlBee for now...", name, size ); |
---|
| 624 | } |
---|
| 625 | else |
---|
| 626 | { |
---|
| 627 | strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" ); |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | if( name ) g_free( name ); |
---|
| 631 | if( size ) g_free( size ); |
---|
| 632 | } |
---|
| 633 | else |
---|
| 634 | { |
---|
| 635 | char *iname = msn_findheader( body, "Application-Name:", blen ); |
---|
| 636 | |
---|
| 637 | g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>", |
---|
| 638 | itype, iname ? iname : "no name" ); |
---|
| 639 | |
---|
| 640 | if( iname ) g_free( iname ); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | g_free( itype ); |
---|
| 644 | |
---|
| 645 | if( !*buf ) |
---|
| 646 | return( 1 ); |
---|
| 647 | |
---|
| 648 | if( sb->who ) |
---|
| 649 | { |
---|
[9624fdf] | 650 | imcb_buddy_msg( ic, cmd[1], buf, 0, 0 ); |
---|
[b7d3cc34] | 651 | } |
---|
| 652 | else if( sb->chat ) |
---|
| 653 | { |
---|
[61ae52c] | 654 | imcb_chat_msg( sb->chat, cmd[1], buf, 0, 0 ); |
---|
[b7d3cc34] | 655 | } |
---|
| 656 | else |
---|
| 657 | { |
---|
| 658 | /* PANIC! */ |
---|
| 659 | } |
---|
| 660 | } |
---|
| 661 | else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 ) |
---|
| 662 | { |
---|
| 663 | char *who = msn_findheader( msg, "TypingUser:", msglen ); |
---|
| 664 | |
---|
| 665 | if( who ) |
---|
| 666 | { |
---|
[9624fdf] | 667 | imcb_buddy_typing( ic, who, OPT_TYPING ); |
---|
[b7d3cc34] | 668 | g_free( who ); |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | g_free( ct ); |
---|
| 672 | } |
---|
| 673 | else |
---|
| 674 | { |
---|
| 675 | g_free( ct ); |
---|
| 676 | } |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | return( 1 ); |
---|
| 680 | } |
---|