[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[4e4af1b] | 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* MSN module - Notification server callbacks */ |
---|
| 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 "md5.h" |
---|
[7db65b7] | 30 | #include "soap.h" |
---|
[ca7de3a] | 31 | #include "xmltree.h" |
---|
[b7d3cc34] | 32 | |
---|
[ba9edaa] | 33 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ); |
---|
[b7d3cc34] | 34 | static int msn_ns_command( gpointer data, char **cmd, int num_parts ); |
---|
| 35 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); |
---|
| 36 | |
---|
[ca7de3a] | 37 | static void msn_ns_send_adl( struct im_connection *ic ); |
---|
[b7d3cc34] | 38 | |
---|
[ba9edaa] | 39 | gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 40 | { |
---|
[0da65d5] | 41 | struct im_connection *ic = data; |
---|
[b7d3cc34] | 42 | struct msn_data *md; |
---|
| 43 | char s[1024]; |
---|
| 44 | |
---|
[0da65d5] | 45 | if( !g_slist_find( msn_connections, ic ) ) |
---|
[ba9edaa] | 46 | return FALSE; |
---|
[b7d3cc34] | 47 | |
---|
| 48 | if( source == -1 ) |
---|
| 49 | { |
---|
[84b045d] | 50 | imcb_error( ic, "Could not connect to server" ); |
---|
[c2fb3809] | 51 | imc_logout( ic, TRUE ); |
---|
[ba9edaa] | 52 | return FALSE; |
---|
[b7d3cc34] | 53 | } |
---|
| 54 | |
---|
[0da65d5] | 55 | md = ic->proto_data; |
---|
[b7d3cc34] | 56 | |
---|
| 57 | if( !md->handler ) |
---|
| 58 | { |
---|
| 59 | md->handler = g_new0( struct msn_handler_data, 1 ); |
---|
[0da65d5] | 60 | md->handler->data = ic; |
---|
[b7d3cc34] | 61 | md->handler->exec_command = msn_ns_command; |
---|
| 62 | md->handler->exec_message = msn_ns_message; |
---|
| 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
| 66 | if( md->handler->rxq ) |
---|
| 67 | g_free( md->handler->rxq ); |
---|
| 68 | |
---|
| 69 | md->handler->rxlen = 0; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | md->handler->fd = md->fd; |
---|
| 73 | md->handler->rxq = g_new0( char, 1 ); |
---|
| 74 | |
---|
[91d6e91] | 75 | g_snprintf( s, sizeof( s ), "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER ); |
---|
[0da65d5] | 76 | if( msn_write( ic, s, strlen( s ) ) ) |
---|
[b7d3cc34] | 77 | { |
---|
[e046390] | 78 | ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic ); |
---|
[84b045d] | 79 | imcb_log( ic, "Connected to server, waiting for reply" ); |
---|
[b7d3cc34] | 80 | } |
---|
[ba9edaa] | 81 | |
---|
| 82 | return FALSE; |
---|
[b7d3cc34] | 83 | } |
---|
| 84 | |
---|
[ba9edaa] | 85 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 86 | { |
---|
[0da65d5] | 87 | struct im_connection *ic = data; |
---|
| 88 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 89 | |
---|
| 90 | if( msn_handler( md->handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */ |
---|
| 91 | { |
---|
[84b045d] | 92 | imcb_error( ic, "Error while reading from server" ); |
---|
[c2fb3809] | 93 | imc_logout( ic, TRUE ); |
---|
[ba9edaa] | 94 | |
---|
| 95 | return FALSE; |
---|
[b7d3cc34] | 96 | } |
---|
[ba9edaa] | 97 | else |
---|
| 98 | return TRUE; |
---|
[b7d3cc34] | 99 | } |
---|
| 100 | |
---|
| 101 | static int msn_ns_command( gpointer data, char **cmd, int num_parts ) |
---|
| 102 | { |
---|
[0da65d5] | 103 | struct im_connection *ic = data; |
---|
| 104 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 105 | char buf[1024]; |
---|
| 106 | |
---|
| 107 | if( num_parts == 0 ) |
---|
| 108 | { |
---|
| 109 | /* Hrrm... Empty command...? Ignore? */ |
---|
| 110 | return( 1 ); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | if( strcmp( cmd[0], "VER" ) == 0 ) |
---|
| 114 | { |
---|
[91d6e91] | 115 | if( cmd[2] && strncmp( cmd[2], MSNP_VER, 5 ) != 0 ) |
---|
[b7d3cc34] | 116 | { |
---|
[84b045d] | 117 | imcb_error( ic, "Unsupported protocol" ); |
---|
[c2fb3809] | 118 | imc_logout( ic, FALSE ); |
---|
[b7d3cc34] | 119 | return( 0 ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | g_snprintf( buf, sizeof( buf ), "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", |
---|
[c2fb3809] | 123 | ++md->trId, ic->acc->user ); |
---|
[0da65d5] | 124 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 125 | } |
---|
| 126 | else if( strcmp( cmd[0], "CVR" ) == 0 ) |
---|
| 127 | { |
---|
| 128 | /* We don't give a damn about the information we just received */ |
---|
[523fb23] | 129 | g_snprintf( buf, sizeof( buf ), "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user ); |
---|
[0da65d5] | 130 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 131 | } |
---|
| 132 | else if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
| 133 | { |
---|
| 134 | char *server; |
---|
| 135 | int port; |
---|
| 136 | |
---|
[ca7de3a] | 137 | if( num_parts >= 6 && strcmp( cmd[2], "NS" ) == 0 ) |
---|
[b7d3cc34] | 138 | { |
---|
[0da65d5] | 139 | b_event_remove( ic->inpa ); |
---|
| 140 | ic->inpa = 0; |
---|
[b7d3cc34] | 141 | closesocket( md->fd ); |
---|
| 142 | |
---|
| 143 | server = strchr( cmd[3], ':' ); |
---|
| 144 | if( !server ) |
---|
| 145 | { |
---|
[84b045d] | 146 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 147 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 148 | return( 0 ); |
---|
| 149 | } |
---|
| 150 | *server = 0; |
---|
| 151 | port = atoi( server + 1 ); |
---|
| 152 | server = cmd[3]; |
---|
| 153 | |
---|
[84b045d] | 154 | imcb_log( ic, "Transferring to other server" ); |
---|
[b7d3cc34] | 155 | |
---|
[0da65d5] | 156 | md->fd = proxy_connect( server, port, msn_ns_connected, ic ); |
---|
[b7d3cc34] | 157 | } |
---|
[ca7de3a] | 158 | else if( num_parts >= 6 && strcmp( cmd[2], "SB" ) == 0 ) |
---|
[b7d3cc34] | 159 | { |
---|
| 160 | struct msn_switchboard *sb; |
---|
| 161 | |
---|
| 162 | server = strchr( cmd[3], ':' ); |
---|
| 163 | if( !server ) |
---|
| 164 | { |
---|
[84b045d] | 165 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 166 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 167 | return( 0 ); |
---|
| 168 | } |
---|
| 169 | *server = 0; |
---|
| 170 | port = atoi( server + 1 ); |
---|
| 171 | server = cmd[3]; |
---|
| 172 | |
---|
| 173 | if( strcmp( cmd[4], "CKI" ) != 0 ) |
---|
| 174 | { |
---|
[84b045d] | 175 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
[c2fb3809] | 176 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 177 | return( 0 ); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | debug( "Connecting to a new switchboard with key %s", cmd[5] ); |
---|
[99f929c] | 181 | |
---|
| 182 | if( ( sb = msn_sb_create( ic, server, port, cmd[5], MSN_SB_NEW ) ) == NULL ) |
---|
| 183 | { |
---|
| 184 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
| 185 | definitely something serious (we ran out of file descriptors?). */ |
---|
| 186 | imcb_error( ic, "Could not create new switchboard" ); |
---|
| 187 | imc_logout( ic, TRUE ); |
---|
| 188 | return( 0 ); |
---|
| 189 | } |
---|
[b7d3cc34] | 190 | |
---|
| 191 | if( md->msgq ) |
---|
| 192 | { |
---|
| 193 | struct msn_message *m = md->msgq->data; |
---|
| 194 | GSList *l; |
---|
| 195 | |
---|
| 196 | sb->who = g_strdup( m->who ); |
---|
| 197 | |
---|
| 198 | /* Move all the messages to the first user in the message |
---|
| 199 | queue to the switchboard message queue. */ |
---|
| 200 | l = md->msgq; |
---|
| 201 | while( l ) |
---|
| 202 | { |
---|
| 203 | m = l->data; |
---|
| 204 | l = l->next; |
---|
| 205 | if( strcmp( m->who, sb->who ) == 0 ) |
---|
| 206 | { |
---|
| 207 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
| 208 | md->msgq = g_slist_remove( md->msgq, m ); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | { |
---|
[84b045d] | 215 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 216 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 217 | return( 0 ); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
| 221 | { |
---|
[523fb23] | 222 | if( num_parts >= 6 && strcmp( cmd[2], "SSO" ) == 0 && |
---|
| 223 | strcmp( cmd[3], "S" ) == 0 ) |
---|
[b7d3cc34] | 224 | { |
---|
[523fb23] | 225 | msn_soap_passport_sso_request( ic, cmd[4], cmd[5] ); |
---|
[b7d3cc34] | 226 | } |
---|
[5fecede] | 227 | else if( strcmp( cmd[2], "OK" ) == 0 ) |
---|
[b7d3cc34] | 228 | { |
---|
[84b045d] | 229 | imcb_log( ic, "Authenticated, getting buddy list" ); |
---|
[7db65b7] | 230 | msn_soap_memlist_request( ic ); |
---|
[b7d3cc34] | 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
[84b045d] | 234 | imcb_error( ic, "Unknown authentication type" ); |
---|
[c2fb3809] | 235 | imc_logout( ic, FALSE ); |
---|
[b7d3cc34] | 236 | return( 0 ); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 240 | { |
---|
[b46769d] | 241 | if( num_parts < 4 ) |
---|
[b7d3cc34] | 242 | { |
---|
[84b045d] | 243 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 244 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 245 | return( 0 ); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | md->handler->msglen = atoi( cmd[3] ); |
---|
| 249 | |
---|
| 250 | if( md->handler->msglen <= 0 ) |
---|
| 251 | { |
---|
[84b045d] | 252 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 253 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 254 | return( 0 ); |
---|
| 255 | } |
---|
| 256 | } |
---|
[ca7de3a] | 257 | else if( strcmp( cmd[0], "BLP" ) == 0 ) |
---|
[b7d3cc34] | 258 | { |
---|
[ca7de3a] | 259 | msn_ns_send_adl( ic ); |
---|
[b7d3cc34] | 260 | } |
---|
[ca7de3a] | 261 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
[b7d3cc34] | 262 | { |
---|
[ca7de3a] | 263 | if( num_parts >= 3 && strcmp( cmd[2], "OK" ) == 0 ) |
---|
[b7d3cc34] | 264 | { |
---|
[ca7de3a] | 265 | char buf[1024]; |
---|
[193dc74] | 266 | char *fn_raw; |
---|
[ca7de3a] | 267 | char *fn; |
---|
[1ad104a] | 268 | |
---|
[193dc74] | 269 | if( ic->flags & OPT_LOGGED_IN ) |
---|
| 270 | return 1; |
---|
| 271 | |
---|
| 272 | if( ( fn_raw = set_getstr( &ic->acc->set, "display_name" ) ) == NULL ) |
---|
[ca7de3a] | 273 | fn_raw = ic->acc->user; |
---|
| 274 | fn = g_malloc( strlen( fn_raw ) * 3 + 1 ); |
---|
| 275 | strcpy( fn, fn_raw ); |
---|
| 276 | http_encode( fn ); |
---|
[1ad104a] | 277 | |
---|
[ca7de3a] | 278 | g_snprintf( buf, sizeof( buf ), "PRP %d MFN %s\r\n", |
---|
| 279 | ++md->trId, fn ); |
---|
| 280 | g_free( fn ); |
---|
| 281 | |
---|
| 282 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 283 | } |
---|
[e5854a8] | 284 | else if( num_parts >= 3 ) |
---|
| 285 | { |
---|
| 286 | md->handler->msglen = atoi( cmd[2] ); |
---|
| 287 | } |
---|
[b7d3cc34] | 288 | } |
---|
[ca7de3a] | 289 | else if( strcmp( cmd[0], "PRP" ) == 0 ) |
---|
[b7d3cc34] | 290 | { |
---|
[ca7de3a] | 291 | imcb_connected( ic ); |
---|
[b7d3cc34] | 292 | } |
---|
| 293 | else if( strcmp( cmd[0], "CHL" ) == 0 ) |
---|
| 294 | { |
---|
[be7a180] | 295 | char *resp; |
---|
[b7d3cc34] | 296 | |
---|
[be7a180] | 297 | if( num_parts < 3 ) |
---|
[b7d3cc34] | 298 | { |
---|
[84b045d] | 299 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 300 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 301 | return( 0 ); |
---|
| 302 | } |
---|
| 303 | |
---|
[be7a180] | 304 | resp = msn_p11_challenge( cmd[2] ); |
---|
| 305 | g_snprintf( buf, sizeof( buf ), "QRY %d %s %zd\r\n%s", |
---|
| 306 | ++md->trId, MSNP11_PROD_ID, |
---|
| 307 | strlen( resp ), resp ); |
---|
| 308 | g_free( resp ); |
---|
[b7d3cc34] | 309 | |
---|
[0da65d5] | 310 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 311 | } |
---|
| 312 | else if( strcmp( cmd[0], "ILN" ) == 0 ) |
---|
| 313 | { |
---|
[08995b0] | 314 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 315 | |
---|
[ca7de3a] | 316 | if( num_parts < 6 ) |
---|
[b7d3cc34] | 317 | { |
---|
[84b045d] | 318 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 319 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 320 | return( 0 ); |
---|
| 321 | } |
---|
| 322 | |
---|
[ca7de3a] | 323 | http_decode( cmd[5] ); |
---|
| 324 | imcb_rename_buddy( ic, cmd[3], cmd[5] ); |
---|
[b7d3cc34] | 325 | |
---|
| 326 | st = msn_away_state_by_code( cmd[2] ); |
---|
| 327 | if( !st ) |
---|
| 328 | { |
---|
| 329 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
[b051d39] | 330 | st = msn_away_state_list + 1; |
---|
[b7d3cc34] | 331 | } |
---|
| 332 | |
---|
[b051d39] | 333 | imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | |
---|
| 334 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
| 335 | st->name, NULL ); |
---|
[b7d3cc34] | 336 | } |
---|
| 337 | else if( strcmp( cmd[0], "FLN" ) == 0 ) |
---|
| 338 | { |
---|
[9bf2481] | 339 | if( cmd[1] == NULL ) |
---|
| 340 | return 1; |
---|
| 341 | |
---|
| 342 | imcb_buddy_status( ic, cmd[1], 0, NULL, NULL ); |
---|
| 343 | |
---|
[bb839e8] | 344 | msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE ); |
---|
[b7d3cc34] | 345 | } |
---|
| 346 | else if( strcmp( cmd[0], "NLN" ) == 0 ) |
---|
| 347 | { |
---|
[08995b0] | 348 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 349 | |
---|
[b46769d] | 350 | if( num_parts < 5 ) |
---|
[b7d3cc34] | 351 | { |
---|
[84b045d] | 352 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 353 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 354 | return( 0 ); |
---|
| 355 | } |
---|
| 356 | |
---|
[5848675] | 357 | http_decode( cmd[4] ); |
---|
| 358 | imcb_rename_buddy( ic, cmd[2], cmd[4] ); |
---|
[b7d3cc34] | 359 | |
---|
| 360 | st = msn_away_state_by_code( cmd[1] ); |
---|
| 361 | if( !st ) |
---|
| 362 | { |
---|
| 363 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
[b051d39] | 364 | st = msn_away_state_list + 1; |
---|
[b7d3cc34] | 365 | } |
---|
| 366 | |
---|
[b051d39] | 367 | imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | |
---|
| 368 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
| 369 | st->name, NULL ); |
---|
[9bf2481] | 370 | |
---|
[bb839e8] | 371 | msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) ); |
---|
[b7d3cc34] | 372 | } |
---|
| 373 | else if( strcmp( cmd[0], "RNG" ) == 0 ) |
---|
| 374 | { |
---|
| 375 | struct msn_switchboard *sb; |
---|
| 376 | char *server; |
---|
| 377 | int session, port; |
---|
| 378 | |
---|
[b46769d] | 379 | if( num_parts < 7 ) |
---|
[b7d3cc34] | 380 | { |
---|
[84b045d] | 381 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 382 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 383 | return( 0 ); |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | session = atoi( cmd[1] ); |
---|
| 387 | |
---|
| 388 | server = strchr( cmd[2], ':' ); |
---|
| 389 | if( !server ) |
---|
| 390 | { |
---|
[84b045d] | 391 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 392 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 393 | return( 0 ); |
---|
| 394 | } |
---|
| 395 | *server = 0; |
---|
| 396 | port = atoi( server + 1 ); |
---|
| 397 | server = cmd[2]; |
---|
| 398 | |
---|
| 399 | if( strcmp( cmd[3], "CKI" ) != 0 ) |
---|
| 400 | { |
---|
[84b045d] | 401 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
[c2fb3809] | 402 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 403 | return( 0 ); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] ); |
---|
| 407 | |
---|
[99f929c] | 408 | if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL ) |
---|
| 409 | { |
---|
| 410 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
| 411 | definitely something serious (we ran out of file descriptors?). */ |
---|
| 412 | imcb_error( ic, "Could not create new switchboard" ); |
---|
| 413 | imc_logout( ic, TRUE ); |
---|
| 414 | return( 0 ); |
---|
| 415 | } |
---|
| 416 | else |
---|
| 417 | { |
---|
| 418 | sb->who = g_strdup( cmd[5] ); |
---|
| 419 | } |
---|
[b7d3cc34] | 420 | } |
---|
| 421 | else if( strcmp( cmd[0], "OUT" ) == 0 ) |
---|
| 422 | { |
---|
[c2fb3809] | 423 | int allow_reconnect = TRUE; |
---|
| 424 | |
---|
[b7d3cc34] | 425 | if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 ) |
---|
| 426 | { |
---|
[84b045d] | 427 | imcb_error( ic, "Someone else logged in with your account" ); |
---|
[c2fb3809] | 428 | allow_reconnect = FALSE; |
---|
[b7d3cc34] | 429 | } |
---|
| 430 | else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 ) |
---|
| 431 | { |
---|
[84b045d] | 432 | imcb_error( ic, "Terminating session because of server shutdown" ); |
---|
[b7d3cc34] | 433 | } |
---|
| 434 | else |
---|
| 435 | { |
---|
[84b045d] | 436 | imcb_error( ic, "Session terminated by remote server (reason unknown)" ); |
---|
[b7d3cc34] | 437 | } |
---|
| 438 | |
---|
[c2fb3809] | 439 | imc_logout( ic, allow_reconnect ); |
---|
[b7d3cc34] | 440 | return( 0 ); |
---|
| 441 | } |
---|
| 442 | else if( strcmp( cmd[0], "IPG" ) == 0 ) |
---|
| 443 | { |
---|
[84b045d] | 444 | imcb_error( ic, "Received IPG command, we don't handle them yet." ); |
---|
[b7d3cc34] | 445 | |
---|
| 446 | md->handler->msglen = atoi( cmd[1] ); |
---|
| 447 | |
---|
| 448 | if( md->handler->msglen <= 0 ) |
---|
| 449 | { |
---|
[84b045d] | 450 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 451 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 452 | return( 0 ); |
---|
| 453 | } |
---|
| 454 | } |
---|
[e5854a8] | 455 | #if 0 |
---|
[70ac477] | 456 | else if( strcmp( cmd[0], "ADG" ) == 0 ) |
---|
| 457 | { |
---|
| 458 | char *group = g_strdup( cmd[3] ); |
---|
| 459 | int groupnum, i; |
---|
| 460 | GSList *l, *next; |
---|
| 461 | |
---|
| 462 | http_decode( group ); |
---|
| 463 | if( sscanf( cmd[4], "%d", &groupnum ) == 1 ) |
---|
| 464 | { |
---|
| 465 | if( groupnum >= md->groupcount ) |
---|
| 466 | { |
---|
| 467 | md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 ); |
---|
| 468 | for( i = md->groupcount; i <= groupnum; i ++ ) |
---|
| 469 | md->grouplist[i] = NULL; |
---|
| 470 | md->groupcount = groupnum + 1; |
---|
| 471 | } |
---|
| 472 | g_free( md->grouplist[groupnum] ); |
---|
| 473 | md->grouplist[groupnum] = group; |
---|
| 474 | } |
---|
| 475 | else |
---|
| 476 | { |
---|
| 477 | /* Shouldn't happen, but if it does, give up on the group. */ |
---|
| 478 | g_free( group ); |
---|
| 479 | imcb_error( ic, "Syntax error" ); |
---|
| 480 | imc_logout( ic, TRUE ); |
---|
| 481 | return 0; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | for( l = md->grpq; l; l = next ) |
---|
| 485 | { |
---|
| 486 | struct msn_groupadd *ga = l->data; |
---|
| 487 | next = l->next; |
---|
| 488 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
| 489 | { |
---|
| 490 | if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) ) |
---|
| 491 | return 0; |
---|
| 492 | |
---|
| 493 | g_free( ga->group ); |
---|
| 494 | g_free( ga->who ); |
---|
| 495 | g_free( ga ); |
---|
| 496 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | } |
---|
[e5854a8] | 500 | #endif |
---|
[5fecede] | 501 | else if( strcmp( cmd[0], "GCF" ) == 0 ) |
---|
| 502 | { |
---|
| 503 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
| 504 | censore. Meh. */ |
---|
| 505 | md->handler->msglen = atoi( cmd[2] ); |
---|
| 506 | } |
---|
[be7a180] | 507 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
| 508 | { |
---|
[e5854a8] | 509 | /* Status message. */ |
---|
[be7a180] | 510 | if( num_parts >= 4 ) |
---|
| 511 | md->handler->msglen = atoi( cmd[3] ); |
---|
| 512 | } |
---|
[d93c0eb9] | 513 | else if( strcmp( cmd[0], "NOT" ) == 0 ) |
---|
| 514 | { |
---|
[e5854a8] | 515 | /* Some kind of notification, poorly documented but |
---|
| 516 | apparently used to announce address book changes. */ |
---|
| 517 | if( num_parts >= 2 ) |
---|
| 518 | md->handler->msglen = atoi( cmd[1] ); |
---|
[d93c0eb9] | 519 | } |
---|
[b7d3cc34] | 520 | else if( isdigit( cmd[0][0] ) ) |
---|
| 521 | { |
---|
| 522 | int num = atoi( cmd[0] ); |
---|
[08995b0] | 523 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
[b7d3cc34] | 524 | |
---|
[84b045d] | 525 | imcb_error( ic, "Error reported by MSN server: %s", err->text ); |
---|
[b7d3cc34] | 526 | |
---|
| 527 | if( err->flags & STATUS_FATAL ) |
---|
| 528 | { |
---|
[c2fb3809] | 529 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 530 | return( 0 ); |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | else |
---|
| 534 | { |
---|
[8ff0a61] | 535 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
[b7d3cc34] | 536 | } |
---|
| 537 | |
---|
| 538 | return( 1 ); |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
| 542 | { |
---|
[0da65d5] | 543 | struct im_connection *ic = data; |
---|
[b7d3cc34] | 544 | char *body; |
---|
| 545 | int blen = 0; |
---|
| 546 | |
---|
| 547 | if( !num_parts ) |
---|
| 548 | return( 1 ); |
---|
| 549 | |
---|
| 550 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
| 551 | { |
---|
| 552 | body += 4; |
---|
| 553 | blen = msglen - ( body - msg ); |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 557 | { |
---|
| 558 | if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 ) |
---|
| 559 | { |
---|
| 560 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
| 561 | |
---|
| 562 | if( !ct ) |
---|
| 563 | return( 1 ); |
---|
| 564 | |
---|
| 565 | if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 ) |
---|
| 566 | { |
---|
| 567 | char *mtype; |
---|
| 568 | char *arg1; |
---|
| 569 | |
---|
| 570 | if( !body ) |
---|
| 571 | return( 1 ); |
---|
| 572 | |
---|
| 573 | mtype = msn_findheader( body, "Type:", blen ); |
---|
| 574 | arg1 = msn_findheader( body, "Arg1:", blen ); |
---|
| 575 | |
---|
| 576 | if( mtype && strcmp( mtype, "1" ) == 0 ) |
---|
| 577 | { |
---|
| 578 | if( arg1 ) |
---|
[84b045d] | 579 | imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 ); |
---|
[b7d3cc34] | 580 | } |
---|
| 581 | |
---|
[ec29351] | 582 | g_free( arg1 ); |
---|
| 583 | g_free( mtype ); |
---|
[b7d3cc34] | 584 | } |
---|
| 585 | else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 ) |
---|
| 586 | { |
---|
| 587 | /* We don't care about this profile for now... */ |
---|
| 588 | } |
---|
| 589 | else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 ) |
---|
| 590 | { |
---|
[ec29351] | 591 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
[b7d3cc34] | 592 | { |
---|
[ec29351] | 593 | char *inbox = msn_findheader( body, "Inbox-Unread:", blen ); |
---|
| 594 | char *folders = msn_findheader( body, "Folders-Unread:", blen ); |
---|
| 595 | |
---|
| 596 | if( inbox && folders ) |
---|
| 597 | imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders ); |
---|
| 598 | |
---|
| 599 | g_free( inbox ); |
---|
| 600 | g_free( folders ); |
---|
[b7d3cc34] | 601 | } |
---|
| 602 | } |
---|
| 603 | else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 ) |
---|
| 604 | { |
---|
[ec29351] | 605 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
[b7d3cc34] | 606 | { |
---|
[ec29351] | 607 | char *from = msn_findheader( body, "From-Addr:", blen ); |
---|
| 608 | char *fromname = msn_findheader( body, "From:", blen ); |
---|
| 609 | |
---|
| 610 | if( from && fromname ) |
---|
| 611 | imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from ); |
---|
| 612 | |
---|
| 613 | g_free( from ); |
---|
| 614 | g_free( fromname ); |
---|
[b7d3cc34] | 615 | } |
---|
| 616 | } |
---|
| 617 | else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 ) |
---|
| 618 | { |
---|
| 619 | /* Sorry, but this one really is *USELESS* */ |
---|
| 620 | } |
---|
| 621 | else |
---|
| 622 | { |
---|
| 623 | debug( "Can't handle %s packet from notification server", ct ); |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | g_free( ct ); |
---|
| 627 | } |
---|
| 628 | } |
---|
[d93c0eb9] | 629 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
| 630 | { |
---|
| 631 | struct xt_node *psm; |
---|
| 632 | char *psm_text = NULL; |
---|
| 633 | |
---|
| 634 | psm = xt_from_string( msg ); |
---|
| 635 | if( psm && strcmp( psm->name, "Data" ) == 0 && |
---|
| 636 | ( psm = xt_find_node( psm->children, "PSM" ) ) ) |
---|
| 637 | psm_text = psm->text; |
---|
| 638 | |
---|
| 639 | imcb_buddy_status_msg( ic, cmd[1], psm_text ); |
---|
| 640 | xt_free_node( psm ); |
---|
| 641 | } |
---|
[e5854a8] | 642 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
| 643 | { |
---|
| 644 | struct xt_node *adl, *d, *c; |
---|
| 645 | |
---|
| 646 | if( !( adl = xt_from_string( msg ) ) ) |
---|
| 647 | return 1; |
---|
| 648 | |
---|
| 649 | for( d = adl->children; d; d = d->next ) |
---|
| 650 | { |
---|
| 651 | char *dn; |
---|
| 652 | if( strcmp( d->name, "d" ) != 0 || |
---|
| 653 | ( dn = xt_find_attr( d, "n" ) ) == NULL ) |
---|
| 654 | continue; |
---|
| 655 | for( c = d->children; c; c = c->next ) |
---|
| 656 | { |
---|
| 657 | bee_user_t *bu; |
---|
| 658 | struct msn_buddy_data *bd; |
---|
| 659 | char *cn, *handle, *f, *l; |
---|
| 660 | int flags; |
---|
| 661 | |
---|
| 662 | if( strcmp( c->name, "c" ) != 0 || |
---|
| 663 | ( l = xt_find_attr( c, "l" ) ) == NULL || |
---|
| 664 | ( cn = xt_find_attr( c, "n" ) ) == NULL ) |
---|
| 665 | continue; |
---|
| 666 | |
---|
| 667 | handle = g_strdup_printf( "%s@%s", cn, dn ); |
---|
| 668 | if( !( ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) || |
---|
| 669 | ( bu = bee_user_new( ic->bee, ic, handle, 0 ) ) ) ) |
---|
| 670 | { |
---|
| 671 | g_free( handle ); |
---|
| 672 | continue; |
---|
| 673 | } |
---|
| 674 | g_free( handle ); |
---|
| 675 | bd = bu->data; |
---|
| 676 | |
---|
| 677 | if( ( f = xt_find_attr( c, "f" ) ) ) |
---|
| 678 | { |
---|
| 679 | http_decode( f ); |
---|
| 680 | imcb_rename_buddy( ic, bu->handle, f ); |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | flags = atoi( l ) & 15; |
---|
| 684 | if( bd->flags != flags ) |
---|
| 685 | { |
---|
| 686 | bd->flags = flags; |
---|
| 687 | msn_buddy_ask( bu ); |
---|
| 688 | } |
---|
| 689 | } |
---|
| 690 | } |
---|
| 691 | } |
---|
[b7d3cc34] | 692 | |
---|
| 693 | return( 1 ); |
---|
| 694 | } |
---|
| 695 | |
---|
[523fb23] | 696 | void msn_auth_got_passport_token( struct im_connection *ic, char *token ) |
---|
[b7d3cc34] | 697 | { |
---|
[d84e2a9] | 698 | struct msn_data *md; |
---|
[e6648bf] | 699 | |
---|
[d84e2a9] | 700 | /* Dead connection? */ |
---|
| 701 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 702 | return; |
---|
| 703 | |
---|
| 704 | md = ic->proto_data; |
---|
[523fb23] | 705 | |
---|
[b7d3cc34] | 706 | { |
---|
[d84e2a9] | 707 | char buf[1024]; |
---|
| 708 | |
---|
[523fb23] | 709 | g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); |
---|
[d84e2a9] | 710 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 711 | } |
---|
| 712 | } |
---|
[e3413cc] | 713 | |
---|
[ca7de3a] | 714 | void msn_auth_got_contact_list( struct im_connection *ic ) |
---|
| 715 | { |
---|
| 716 | char buf[64]; |
---|
| 717 | struct msn_data *md; |
---|
| 718 | |
---|
| 719 | /* Dead connection? */ |
---|
| 720 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 721 | return; |
---|
| 722 | |
---|
| 723 | md = ic->proto_data; |
---|
| 724 | |
---|
| 725 | |
---|
| 726 | g_snprintf( buf, sizeof( buf ), "BLP %d %s\r\n", ++md->trId, "BL" ); |
---|
| 727 | msn_write( ic, buf, strlen( buf ) ); |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data ) |
---|
| 731 | { |
---|
| 732 | struct xt_node *adl = data, *d, *c; |
---|
| 733 | struct bee_user *bu = value; |
---|
| 734 | struct msn_buddy_data *bd = bu->data; |
---|
| 735 | char handle[strlen(bu->handle)]; |
---|
| 736 | char *domain; |
---|
| 737 | char l[4]; |
---|
| 738 | |
---|
[e5854a8] | 739 | if( ( bd->flags & 7 ) == 0 ) |
---|
| 740 | return FALSE; |
---|
| 741 | |
---|
[ca7de3a] | 742 | strcpy( handle, bu->handle ); |
---|
| 743 | if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */ |
---|
| 744 | return FALSE; |
---|
| 745 | *domain = '\0'; |
---|
| 746 | domain ++; |
---|
| 747 | |
---|
| 748 | if( ( d = adl->children ) == NULL || |
---|
| 749 | g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 ) |
---|
| 750 | { |
---|
| 751 | d = xt_new_node( "d", NULL, NULL ); |
---|
| 752 | xt_add_attr( d, "n", domain ); |
---|
| 753 | xt_insert_child( adl, d ); |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 ); |
---|
| 757 | c = xt_new_node( "c", NULL, NULL ); |
---|
| 758 | xt_add_attr( c, "n", handle ); |
---|
| 759 | xt_add_attr( c, "l", l ); |
---|
| 760 | xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */ |
---|
| 761 | xt_insert_child( d, c ); |
---|
| 762 | |
---|
| 763 | return FALSE; |
---|
| 764 | } |
---|
| 765 | |
---|
| 766 | static void msn_ns_send_adl( struct im_connection *ic ) |
---|
| 767 | { |
---|
| 768 | struct xt_node *adl; |
---|
| 769 | struct msn_data *md; |
---|
| 770 | char *adls, buf[64]; |
---|
| 771 | |
---|
| 772 | /* Dead connection? */ |
---|
| 773 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 774 | return; |
---|
| 775 | |
---|
| 776 | md = ic->proto_data; |
---|
| 777 | |
---|
| 778 | adl = xt_new_node( "ml", NULL, NULL ); |
---|
| 779 | xt_add_attr( adl, "l", "1" ); |
---|
| 780 | g_tree_foreach( md->domaintree, msn_ns_send_adl_1, adl ); |
---|
| 781 | adls = xt_to_string( adl ); |
---|
| 782 | |
---|
| 783 | g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) ); |
---|
| 784 | if( msn_write( ic, buf, strlen( buf ) ) ) |
---|
| 785 | msn_write( ic, adls, strlen( adls ) ); |
---|
| 786 | |
---|
| 787 | g_free( adls ); |
---|
| 788 | xt_free_node( adl ); |
---|
| 789 | } |
---|