[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]; |
---|
| 266 | char *fn_raw = set_getstr( &ic->acc->set, "display_name" ); |
---|
| 267 | char *fn; |
---|
[1ad104a] | 268 | |
---|
[ca7de3a] | 269 | if( fn_raw == NULL ) |
---|
| 270 | fn_raw = ic->acc->user; |
---|
| 271 | fn = g_malloc( strlen( fn_raw ) * 3 + 1 ); |
---|
| 272 | strcpy( fn, fn_raw ); |
---|
| 273 | http_encode( fn ); |
---|
[1ad104a] | 274 | |
---|
[ca7de3a] | 275 | g_snprintf( buf, sizeof( buf ), "PRP %d MFN %s\r\n", |
---|
| 276 | ++md->trId, fn ); |
---|
| 277 | g_free( fn ); |
---|
| 278 | |
---|
| 279 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 280 | } |
---|
| 281 | } |
---|
[ca7de3a] | 282 | else if( strcmp( cmd[0], "PRP" ) == 0 ) |
---|
[b7d3cc34] | 283 | { |
---|
[ca7de3a] | 284 | imcb_connected( ic ); |
---|
[b7d3cc34] | 285 | } |
---|
| 286 | else if( strcmp( cmd[0], "CHL" ) == 0 ) |
---|
| 287 | { |
---|
[be7a180] | 288 | char *resp; |
---|
[b7d3cc34] | 289 | |
---|
[be7a180] | 290 | if( num_parts < 3 ) |
---|
[b7d3cc34] | 291 | { |
---|
[84b045d] | 292 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 293 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 294 | return( 0 ); |
---|
| 295 | } |
---|
| 296 | |
---|
[be7a180] | 297 | resp = msn_p11_challenge( cmd[2] ); |
---|
| 298 | g_snprintf( buf, sizeof( buf ), "QRY %d %s %zd\r\n%s", |
---|
| 299 | ++md->trId, MSNP11_PROD_ID, |
---|
| 300 | strlen( resp ), resp ); |
---|
| 301 | g_free( resp ); |
---|
[b7d3cc34] | 302 | |
---|
[0da65d5] | 303 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 304 | } |
---|
| 305 | else if( strcmp( cmd[0], "ILN" ) == 0 ) |
---|
| 306 | { |
---|
[08995b0] | 307 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 308 | |
---|
[ca7de3a] | 309 | if( num_parts < 6 ) |
---|
[b7d3cc34] | 310 | { |
---|
[84b045d] | 311 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 312 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 313 | return( 0 ); |
---|
| 314 | } |
---|
| 315 | |
---|
[ca7de3a] | 316 | http_decode( cmd[5] ); |
---|
| 317 | imcb_rename_buddy( ic, cmd[3], cmd[5] ); |
---|
[b7d3cc34] | 318 | |
---|
| 319 | st = msn_away_state_by_code( cmd[2] ); |
---|
| 320 | if( !st ) |
---|
| 321 | { |
---|
| 322 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
[b051d39] | 323 | st = msn_away_state_list + 1; |
---|
[b7d3cc34] | 324 | } |
---|
| 325 | |
---|
[b051d39] | 326 | imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | |
---|
| 327 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
| 328 | st->name, NULL ); |
---|
[b7d3cc34] | 329 | } |
---|
| 330 | else if( strcmp( cmd[0], "FLN" ) == 0 ) |
---|
| 331 | { |
---|
[9bf2481] | 332 | if( cmd[1] == NULL ) |
---|
| 333 | return 1; |
---|
| 334 | |
---|
| 335 | imcb_buddy_status( ic, cmd[1], 0, NULL, NULL ); |
---|
| 336 | |
---|
[bb839e8] | 337 | msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE ); |
---|
[b7d3cc34] | 338 | } |
---|
| 339 | else if( strcmp( cmd[0], "NLN" ) == 0 ) |
---|
| 340 | { |
---|
[08995b0] | 341 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 342 | |
---|
[b46769d] | 343 | if( num_parts < 5 ) |
---|
[b7d3cc34] | 344 | { |
---|
[84b045d] | 345 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 346 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 347 | return( 0 ); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | http_decode( cmd[3] ); |
---|
[f0cb961] | 351 | imcb_rename_buddy( ic, cmd[2], cmd[3] ); |
---|
[b7d3cc34] | 352 | |
---|
| 353 | st = msn_away_state_by_code( cmd[1] ); |
---|
| 354 | if( !st ) |
---|
| 355 | { |
---|
| 356 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
[b051d39] | 357 | st = msn_away_state_list + 1; |
---|
[b7d3cc34] | 358 | } |
---|
| 359 | |
---|
[b051d39] | 360 | imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | |
---|
| 361 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
| 362 | st->name, NULL ); |
---|
[9bf2481] | 363 | |
---|
[bb839e8] | 364 | msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) ); |
---|
[b7d3cc34] | 365 | } |
---|
| 366 | else if( strcmp( cmd[0], "RNG" ) == 0 ) |
---|
| 367 | { |
---|
| 368 | struct msn_switchboard *sb; |
---|
| 369 | char *server; |
---|
| 370 | int session, port; |
---|
| 371 | |
---|
[b46769d] | 372 | if( num_parts < 7 ) |
---|
[b7d3cc34] | 373 | { |
---|
[84b045d] | 374 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 375 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 376 | return( 0 ); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | session = atoi( cmd[1] ); |
---|
| 380 | |
---|
| 381 | server = strchr( cmd[2], ':' ); |
---|
| 382 | if( !server ) |
---|
| 383 | { |
---|
[84b045d] | 384 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 385 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 386 | return( 0 ); |
---|
| 387 | } |
---|
| 388 | *server = 0; |
---|
| 389 | port = atoi( server + 1 ); |
---|
| 390 | server = cmd[2]; |
---|
| 391 | |
---|
| 392 | if( strcmp( cmd[3], "CKI" ) != 0 ) |
---|
| 393 | { |
---|
[84b045d] | 394 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
[c2fb3809] | 395 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 396 | return( 0 ); |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] ); |
---|
| 400 | |
---|
[99f929c] | 401 | if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL ) |
---|
| 402 | { |
---|
| 403 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
| 404 | definitely something serious (we ran out of file descriptors?). */ |
---|
| 405 | imcb_error( ic, "Could not create new switchboard" ); |
---|
| 406 | imc_logout( ic, TRUE ); |
---|
| 407 | return( 0 ); |
---|
| 408 | } |
---|
| 409 | else |
---|
| 410 | { |
---|
| 411 | sb->who = g_strdup( cmd[5] ); |
---|
| 412 | } |
---|
[b7d3cc34] | 413 | } |
---|
| 414 | else if( strcmp( cmd[0], "ADD" ) == 0 ) |
---|
| 415 | { |
---|
[b46769d] | 416 | if( num_parts >= 6 && strcmp( cmd[2], "RL" ) == 0 ) |
---|
[b7d3cc34] | 417 | { |
---|
| 418 | GSList *l; |
---|
| 419 | |
---|
| 420 | http_decode( cmd[5] ); |
---|
| 421 | |
---|
| 422 | if( strchr( cmd[4], '@' ) == NULL ) |
---|
| 423 | { |
---|
[84b045d] | 424 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 425 | imc_logout( ic, TRUE ); |
---|
[f0cb961] | 426 | return 0; |
---|
[b7d3cc34] | 427 | } |
---|
| 428 | |
---|
[f0cb961] | 429 | /* We got added by someone. If we don't have this |
---|
| 430 | person in permit/deny yet, inform the user. */ |
---|
[0da65d5] | 431 | for( l = ic->permit; l; l = l->next ) |
---|
[b7d3cc34] | 432 | if( g_strcasecmp( l->data, cmd[4] ) == 0 ) |
---|
[f0cb961] | 433 | return 1; |
---|
[b7d3cc34] | 434 | |
---|
[0da65d5] | 435 | for( l = ic->deny; l; l = l->next ) |
---|
[b7d3cc34] | 436 | if( g_strcasecmp( l->data, cmd[4] ) == 0 ) |
---|
[f0cb961] | 437 | return 1; |
---|
[b7d3cc34] | 438 | |
---|
[0da65d5] | 439 | msn_buddy_ask( ic, cmd[4], cmd[5] ); |
---|
[b7d3cc34] | 440 | } |
---|
[f0cb961] | 441 | else if( num_parts >= 6 && strcmp( cmd[2], "FL" ) == 0 ) |
---|
| 442 | { |
---|
[6acc033] | 443 | const char *group = NULL; |
---|
| 444 | int num; |
---|
| 445 | |
---|
| 446 | if( cmd[6] != NULL && sscanf( cmd[6], "%d", &num ) == 1 && num < md->groupcount ) |
---|
| 447 | group = md->grouplist[num]; |
---|
| 448 | |
---|
[f0cb961] | 449 | http_decode( cmd[5] ); |
---|
[6acc033] | 450 | imcb_add_buddy( ic, cmd[4], group ); |
---|
[f0cb961] | 451 | imcb_rename_buddy( ic, cmd[4], cmd[5] ); |
---|
| 452 | } |
---|
[b7d3cc34] | 453 | } |
---|
| 454 | else if( strcmp( cmd[0], "OUT" ) == 0 ) |
---|
| 455 | { |
---|
[c2fb3809] | 456 | int allow_reconnect = TRUE; |
---|
| 457 | |
---|
[b7d3cc34] | 458 | if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 ) |
---|
| 459 | { |
---|
[84b045d] | 460 | imcb_error( ic, "Someone else logged in with your account" ); |
---|
[c2fb3809] | 461 | allow_reconnect = FALSE; |
---|
[b7d3cc34] | 462 | } |
---|
| 463 | else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 ) |
---|
| 464 | { |
---|
[84b045d] | 465 | imcb_error( ic, "Terminating session because of server shutdown" ); |
---|
[b7d3cc34] | 466 | } |
---|
| 467 | else |
---|
| 468 | { |
---|
[84b045d] | 469 | imcb_error( ic, "Session terminated by remote server (reason unknown)" ); |
---|
[b7d3cc34] | 470 | } |
---|
| 471 | |
---|
[c2fb3809] | 472 | imc_logout( ic, allow_reconnect ); |
---|
[b7d3cc34] | 473 | return( 0 ); |
---|
| 474 | } |
---|
| 475 | else if( strcmp( cmd[0], "IPG" ) == 0 ) |
---|
| 476 | { |
---|
[84b045d] | 477 | imcb_error( ic, "Received IPG command, we don't handle them yet." ); |
---|
[b7d3cc34] | 478 | |
---|
| 479 | md->handler->msglen = atoi( cmd[1] ); |
---|
| 480 | |
---|
| 481 | if( md->handler->msglen <= 0 ) |
---|
| 482 | { |
---|
[84b045d] | 483 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 484 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 485 | return( 0 ); |
---|
| 486 | } |
---|
| 487 | } |
---|
[70ac477] | 488 | else if( strcmp( cmd[0], "ADG" ) == 0 ) |
---|
| 489 | { |
---|
| 490 | char *group = g_strdup( cmd[3] ); |
---|
| 491 | int groupnum, i; |
---|
| 492 | GSList *l, *next; |
---|
| 493 | |
---|
| 494 | http_decode( group ); |
---|
| 495 | if( sscanf( cmd[4], "%d", &groupnum ) == 1 ) |
---|
| 496 | { |
---|
| 497 | if( groupnum >= md->groupcount ) |
---|
| 498 | { |
---|
| 499 | md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 ); |
---|
| 500 | for( i = md->groupcount; i <= groupnum; i ++ ) |
---|
| 501 | md->grouplist[i] = NULL; |
---|
| 502 | md->groupcount = groupnum + 1; |
---|
| 503 | } |
---|
| 504 | g_free( md->grouplist[groupnum] ); |
---|
| 505 | md->grouplist[groupnum] = group; |
---|
| 506 | } |
---|
| 507 | else |
---|
| 508 | { |
---|
| 509 | /* Shouldn't happen, but if it does, give up on the group. */ |
---|
| 510 | g_free( group ); |
---|
| 511 | imcb_error( ic, "Syntax error" ); |
---|
| 512 | imc_logout( ic, TRUE ); |
---|
| 513 | return 0; |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | for( l = md->grpq; l; l = next ) |
---|
| 517 | { |
---|
| 518 | struct msn_groupadd *ga = l->data; |
---|
| 519 | next = l->next; |
---|
| 520 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
| 521 | { |
---|
| 522 | if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) ) |
---|
| 523 | return 0; |
---|
| 524 | |
---|
| 525 | g_free( ga->group ); |
---|
| 526 | g_free( ga->who ); |
---|
| 527 | g_free( ga ); |
---|
| 528 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | } |
---|
[5fecede] | 532 | else if( strcmp( cmd[0], "GCF" ) == 0 ) |
---|
| 533 | { |
---|
| 534 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
| 535 | censore. Meh. */ |
---|
| 536 | md->handler->msglen = atoi( cmd[2] ); |
---|
| 537 | } |
---|
[be7a180] | 538 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
| 539 | { |
---|
| 540 | /* Status message. Parser coming soon. */ |
---|
| 541 | if( num_parts >= 4 ) |
---|
| 542 | md->handler->msglen = atoi( cmd[3] ); |
---|
| 543 | } |
---|
[d93c0eb9] | 544 | else if( strcmp( cmd[0], "NOT" ) == 0 ) |
---|
| 545 | { |
---|
| 546 | /* Some kind of notification, not sure if it still exists |
---|
| 547 | but we have to skip the payload or stuff breaks. */ |
---|
| 548 | if( num_parts >= 3 ) |
---|
| 549 | md->handler->msglen = atoi( cmd[2] ); |
---|
| 550 | } |
---|
[b7d3cc34] | 551 | else if( isdigit( cmd[0][0] ) ) |
---|
| 552 | { |
---|
| 553 | int num = atoi( cmd[0] ); |
---|
[08995b0] | 554 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
[b7d3cc34] | 555 | |
---|
[84b045d] | 556 | imcb_error( ic, "Error reported by MSN server: %s", err->text ); |
---|
[b7d3cc34] | 557 | |
---|
| 558 | if( err->flags & STATUS_FATAL ) |
---|
| 559 | { |
---|
[c2fb3809] | 560 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 561 | return( 0 ); |
---|
| 562 | } |
---|
| 563 | } |
---|
| 564 | else |
---|
| 565 | { |
---|
[8ff0a61] | 566 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
[b7d3cc34] | 567 | } |
---|
| 568 | |
---|
| 569 | return( 1 ); |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
| 573 | { |
---|
[0da65d5] | 574 | struct im_connection *ic = data; |
---|
[b7d3cc34] | 575 | char *body; |
---|
| 576 | int blen = 0; |
---|
| 577 | |
---|
| 578 | if( !num_parts ) |
---|
| 579 | return( 1 ); |
---|
| 580 | |
---|
| 581 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
| 582 | { |
---|
| 583 | body += 4; |
---|
| 584 | blen = msglen - ( body - msg ); |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 588 | { |
---|
| 589 | if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 ) |
---|
| 590 | { |
---|
| 591 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
| 592 | |
---|
| 593 | if( !ct ) |
---|
| 594 | return( 1 ); |
---|
| 595 | |
---|
| 596 | if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 ) |
---|
| 597 | { |
---|
| 598 | char *mtype; |
---|
| 599 | char *arg1; |
---|
| 600 | |
---|
| 601 | if( !body ) |
---|
| 602 | return( 1 ); |
---|
| 603 | |
---|
| 604 | mtype = msn_findheader( body, "Type:", blen ); |
---|
| 605 | arg1 = msn_findheader( body, "Arg1:", blen ); |
---|
| 606 | |
---|
| 607 | if( mtype && strcmp( mtype, "1" ) == 0 ) |
---|
| 608 | { |
---|
| 609 | if( arg1 ) |
---|
[84b045d] | 610 | imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 ); |
---|
[b7d3cc34] | 611 | } |
---|
| 612 | |
---|
[ec29351] | 613 | g_free( arg1 ); |
---|
| 614 | g_free( mtype ); |
---|
[b7d3cc34] | 615 | } |
---|
| 616 | else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 ) |
---|
| 617 | { |
---|
| 618 | /* We don't care about this profile for now... */ |
---|
| 619 | } |
---|
| 620 | else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 ) |
---|
| 621 | { |
---|
[ec29351] | 622 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
[b7d3cc34] | 623 | { |
---|
[ec29351] | 624 | char *inbox = msn_findheader( body, "Inbox-Unread:", blen ); |
---|
| 625 | char *folders = msn_findheader( body, "Folders-Unread:", blen ); |
---|
| 626 | |
---|
| 627 | if( inbox && folders ) |
---|
| 628 | imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders ); |
---|
| 629 | |
---|
| 630 | g_free( inbox ); |
---|
| 631 | g_free( folders ); |
---|
[b7d3cc34] | 632 | } |
---|
| 633 | } |
---|
| 634 | else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 ) |
---|
| 635 | { |
---|
[ec29351] | 636 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
[b7d3cc34] | 637 | { |
---|
[ec29351] | 638 | char *from = msn_findheader( body, "From-Addr:", blen ); |
---|
| 639 | char *fromname = msn_findheader( body, "From:", blen ); |
---|
| 640 | |
---|
| 641 | if( from && fromname ) |
---|
| 642 | imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from ); |
---|
| 643 | |
---|
| 644 | g_free( from ); |
---|
| 645 | g_free( fromname ); |
---|
[b7d3cc34] | 646 | } |
---|
| 647 | } |
---|
| 648 | else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 ) |
---|
| 649 | { |
---|
| 650 | /* Sorry, but this one really is *USELESS* */ |
---|
| 651 | } |
---|
| 652 | else |
---|
| 653 | { |
---|
| 654 | debug( "Can't handle %s packet from notification server", ct ); |
---|
| 655 | } |
---|
| 656 | |
---|
| 657 | g_free( ct ); |
---|
| 658 | } |
---|
| 659 | } |
---|
[d93c0eb9] | 660 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
| 661 | { |
---|
| 662 | struct xt_node *psm; |
---|
| 663 | char *psm_text = NULL; |
---|
| 664 | |
---|
| 665 | psm = xt_from_string( msg ); |
---|
| 666 | if( psm && strcmp( psm->name, "Data" ) == 0 && |
---|
| 667 | ( psm = xt_find_node( psm->children, "PSM" ) ) ) |
---|
| 668 | psm_text = psm->text; |
---|
| 669 | |
---|
| 670 | imcb_buddy_status_msg( ic, cmd[1], psm_text ); |
---|
| 671 | xt_free_node( psm ); |
---|
| 672 | } |
---|
[b7d3cc34] | 673 | |
---|
| 674 | return( 1 ); |
---|
| 675 | } |
---|
| 676 | |
---|
[523fb23] | 677 | void msn_auth_got_passport_token( struct im_connection *ic, char *token ) |
---|
[b7d3cc34] | 678 | { |
---|
[d84e2a9] | 679 | struct msn_data *md; |
---|
[e6648bf] | 680 | |
---|
[d84e2a9] | 681 | /* Dead connection? */ |
---|
| 682 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 683 | return; |
---|
| 684 | |
---|
| 685 | md = ic->proto_data; |
---|
[523fb23] | 686 | |
---|
[b7d3cc34] | 687 | { |
---|
[d84e2a9] | 688 | char buf[1024]; |
---|
| 689 | |
---|
[523fb23] | 690 | g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); |
---|
[d84e2a9] | 691 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 692 | } |
---|
| 693 | } |
---|
[e3413cc] | 694 | |
---|
[ca7de3a] | 695 | void msn_auth_got_contact_list( struct im_connection *ic ) |
---|
| 696 | { |
---|
| 697 | char buf[64]; |
---|
| 698 | struct msn_data *md; |
---|
| 699 | |
---|
| 700 | /* Dead connection? */ |
---|
| 701 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 702 | return; |
---|
| 703 | |
---|
| 704 | md = ic->proto_data; |
---|
| 705 | |
---|
| 706 | |
---|
| 707 | g_snprintf( buf, sizeof( buf ), "BLP %d %s\r\n", ++md->trId, "BL" ); |
---|
| 708 | msn_write( ic, buf, strlen( buf ) ); |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data ) |
---|
| 712 | { |
---|
| 713 | struct xt_node *adl = data, *d, *c; |
---|
| 714 | struct bee_user *bu = value; |
---|
| 715 | struct msn_buddy_data *bd = bu->data; |
---|
| 716 | char handle[strlen(bu->handle)]; |
---|
| 717 | char *domain; |
---|
| 718 | char l[4]; |
---|
| 719 | |
---|
| 720 | strcpy( handle, bu->handle ); |
---|
| 721 | if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */ |
---|
| 722 | return FALSE; |
---|
| 723 | *domain = '\0'; |
---|
| 724 | domain ++; |
---|
| 725 | |
---|
| 726 | if( ( d = adl->children ) == NULL || |
---|
| 727 | g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 ) |
---|
| 728 | { |
---|
| 729 | d = xt_new_node( "d", NULL, NULL ); |
---|
| 730 | xt_add_attr( d, "n", domain ); |
---|
| 731 | xt_insert_child( adl, d ); |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 ); |
---|
| 735 | c = xt_new_node( "c", NULL, NULL ); |
---|
| 736 | xt_add_attr( c, "n", handle ); |
---|
| 737 | xt_add_attr( c, "l", l ); |
---|
| 738 | xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */ |
---|
| 739 | xt_insert_child( d, c ); |
---|
| 740 | |
---|
| 741 | return FALSE; |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | static void msn_ns_send_adl( struct im_connection *ic ) |
---|
| 745 | { |
---|
| 746 | struct xt_node *adl; |
---|
| 747 | struct msn_data *md; |
---|
| 748 | char *adls, buf[64]; |
---|
| 749 | |
---|
| 750 | /* Dead connection? */ |
---|
| 751 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 752 | return; |
---|
| 753 | |
---|
| 754 | md = ic->proto_data; |
---|
| 755 | |
---|
| 756 | adl = xt_new_node( "ml", NULL, NULL ); |
---|
| 757 | xt_add_attr( adl, "l", "1" ); |
---|
| 758 | g_tree_foreach( md->domaintree, msn_ns_send_adl_1, adl ); |
---|
| 759 | adls = xt_to_string( adl ); |
---|
| 760 | |
---|
| 761 | g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) ); |
---|
| 762 | if( msn_write( ic, buf, strlen( buf ) ) ) |
---|
| 763 | msn_write( ic, adls, strlen( adls ) ); |
---|
| 764 | |
---|
| 765 | g_free( adls ); |
---|
| 766 | xt_free_node( adl ); |
---|
| 767 | } |
---|