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