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