[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 | } |
---|
| 231 | else if( num_parts == 7 && strcmp( cmd[2], "OK" ) == 0 ) |
---|
| 232 | { |
---|
[911f2eb] | 233 | set_t *s; |
---|
| 234 | |
---|
[b7d3cc34] | 235 | http_decode( cmd[4] ); |
---|
| 236 | |
---|
[0da65d5] | 237 | strncpy( ic->displayname, cmd[4], sizeof( ic->displayname ) ); |
---|
| 238 | ic->displayname[sizeof(ic->displayname)-1] = 0; |
---|
[b7d3cc34] | 239 | |
---|
[0da65d5] | 240 | if( ( s = set_find( &ic->acc->set, "display_name" ) ) ) |
---|
[911f2eb] | 241 | { |
---|
| 242 | g_free( s->value ); |
---|
| 243 | s->value = g_strdup( cmd[4] ); |
---|
| 244 | } |
---|
| 245 | |
---|
[84b045d] | 246 | imcb_log( ic, "Authenticated, getting buddy list" ); |
---|
[b7d3cc34] | 247 | |
---|
| 248 | g_snprintf( buf, sizeof( buf ), "SYN %d 0\r\n", ++md->trId ); |
---|
[0da65d5] | 249 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 250 | } |
---|
| 251 | else |
---|
| 252 | { |
---|
[84b045d] | 253 | imcb_error( ic, "Unknown authentication type" ); |
---|
[c2fb3809] | 254 | imc_logout( ic, FALSE ); |
---|
[b7d3cc34] | 255 | return( 0 ); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 259 | { |
---|
| 260 | if( num_parts != 4 ) |
---|
| 261 | { |
---|
[84b045d] | 262 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 263 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 264 | return( 0 ); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | md->handler->msglen = atoi( cmd[3] ); |
---|
| 268 | |
---|
| 269 | if( md->handler->msglen <= 0 ) |
---|
| 270 | { |
---|
[84b045d] | 271 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 272 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 273 | return( 0 ); |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | else if( strcmp( cmd[0], "SYN" ) == 0 ) |
---|
| 277 | { |
---|
| 278 | if( num_parts == 5 ) |
---|
| 279 | { |
---|
[1145964] | 280 | int i, groupcount; |
---|
| 281 | |
---|
| 282 | groupcount = atoi( cmd[4] ); |
---|
| 283 | if( groupcount > 0 ) |
---|
| 284 | { |
---|
| 285 | /* valgrind says this is leaking memory, I'm guessing |
---|
| 286 | that this happens during server redirects. */ |
---|
| 287 | if( md->grouplist ) |
---|
| 288 | { |
---|
| 289 | for( i = 0; i < md->groupcount; i ++ ) |
---|
| 290 | g_free( md->grouplist[i] ); |
---|
| 291 | g_free( md->grouplist ); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | md->groupcount = groupcount; |
---|
[bc736cfa] | 295 | md->grouplist = g_new0( char *, md->groupcount ); |
---|
[1145964] | 296 | } |
---|
[b7d3cc34] | 297 | |
---|
[1145964] | 298 | md->buddycount = atoi( cmd[3] ); |
---|
[b7d3cc34] | 299 | if( !*cmd[3] || md->buddycount == 0 ) |
---|
[0da65d5] | 300 | msn_logged_in( ic ); |
---|
[b7d3cc34] | 301 | } |
---|
| 302 | else |
---|
| 303 | { |
---|
| 304 | /* Hrrm... This SYN reply doesn't really look like something we expected. |
---|
| 305 | Let's assume everything is okay. */ |
---|
| 306 | |
---|
[0da65d5] | 307 | msn_logged_in( ic ); |
---|
[b7d3cc34] | 308 | } |
---|
| 309 | } |
---|
| 310 | else if( strcmp( cmd[0], "LST" ) == 0 ) |
---|
| 311 | { |
---|
| 312 | int list; |
---|
| 313 | |
---|
| 314 | if( num_parts != 4 && num_parts != 5 ) |
---|
| 315 | { |
---|
[84b045d] | 316 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 317 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 318 | return( 0 ); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | http_decode( cmd[2] ); |
---|
| 322 | list = atoi( cmd[3] ); |
---|
| 323 | |
---|
| 324 | if( list & 1 ) /* FL */ |
---|
| 325 | { |
---|
[1ad104a] | 326 | char *group = NULL; |
---|
| 327 | int num; |
---|
| 328 | |
---|
[f0cb961] | 329 | if( cmd[4] != NULL && sscanf( cmd[4], "%d", &num ) == 1 && num < md->groupcount ) |
---|
[1ad104a] | 330 | group = md->grouplist[num]; |
---|
| 331 | |
---|
[f0cb961] | 332 | imcb_add_buddy( ic, cmd[1], group ); |
---|
| 333 | imcb_rename_buddy( ic, cmd[1], cmd[2] ); |
---|
[b7d3cc34] | 334 | } |
---|
| 335 | if( list & 2 ) /* AL */ |
---|
| 336 | { |
---|
[0da65d5] | 337 | ic->permit = g_slist_append( ic->permit, g_strdup( cmd[1] ) ); |
---|
[b7d3cc34] | 338 | } |
---|
| 339 | if( list & 4 ) /* BL */ |
---|
| 340 | { |
---|
[0da65d5] | 341 | ic->deny = g_slist_append( ic->deny, g_strdup( cmd[1] ) ); |
---|
[b7d3cc34] | 342 | } |
---|
| 343 | if( list & 8 ) /* RL */ |
---|
| 344 | { |
---|
| 345 | if( ( list & 6 ) == 0 ) |
---|
[0da65d5] | 346 | msn_buddy_ask( ic, cmd[1], cmd[2] ); |
---|
[b7d3cc34] | 347 | } |
---|
| 348 | |
---|
| 349 | if( --md->buddycount == 0 ) |
---|
| 350 | { |
---|
[0da65d5] | 351 | if( ic->flags & OPT_LOGGED_IN ) |
---|
[b7d3cc34] | 352 | { |
---|
[84b045d] | 353 | imcb_log( ic, "Successfully transferred to different server" ); |
---|
[b7d3cc34] | 354 | g_snprintf( buf, sizeof( buf ), "CHG %d %s %d\r\n", ++md->trId, md->away_state->code, 0 ); |
---|
[0da65d5] | 355 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 356 | } |
---|
| 357 | else |
---|
| 358 | { |
---|
[0da65d5] | 359 | msn_logged_in( ic ); |
---|
[b7d3cc34] | 360 | } |
---|
| 361 | } |
---|
| 362 | } |
---|
[bc736cfa] | 363 | else if( strcmp( cmd[0], "LSG" ) == 0 ) |
---|
[b7d3cc34] | 364 | { |
---|
[bc736cfa] | 365 | int num; |
---|
| 366 | |
---|
| 367 | if( num_parts != 4 ) |
---|
| 368 | { |
---|
[84b045d] | 369 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 370 | imc_logout( ic, TRUE ); |
---|
[bc736cfa] | 371 | return( 0 ); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | http_decode( cmd[2] ); |
---|
| 375 | num = atoi( cmd[1] ); |
---|
| 376 | |
---|
| 377 | if( num < md->groupcount ) |
---|
| 378 | md->grouplist[num] = g_strdup( cmd[2] ); |
---|
[b7d3cc34] | 379 | } |
---|
| 380 | else if( strcmp( cmd[0], "CHL" ) == 0 ) |
---|
| 381 | { |
---|
| 382 | md5_state_t state; |
---|
| 383 | md5_byte_t digest[16]; |
---|
| 384 | int i; |
---|
| 385 | |
---|
| 386 | if( num_parts != 3 ) |
---|
| 387 | { |
---|
[84b045d] | 388 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 389 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 390 | return( 0 ); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | md5_init( &state ); |
---|
| 394 | md5_append( &state, (const md5_byte_t *) cmd[2], strlen( cmd[2] ) ); |
---|
| 395 | md5_append( &state, (const md5_byte_t *) QRY_CODE, strlen( QRY_CODE ) ); |
---|
| 396 | md5_finish( &state, digest ); |
---|
| 397 | |
---|
| 398 | g_snprintf( buf, sizeof( buf ), "QRY %d %s %d\r\n", ++md->trId, QRY_NAME, 32 ); |
---|
| 399 | for( i = 0; i < 16; i ++ ) |
---|
| 400 | g_snprintf( buf + strlen( buf ), 3, "%02x", digest[i] ); |
---|
| 401 | |
---|
[0da65d5] | 402 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
[b7d3cc34] | 403 | } |
---|
| 404 | else if( strcmp( cmd[0], "ILN" ) == 0 ) |
---|
| 405 | { |
---|
[08995b0] | 406 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 407 | |
---|
| 408 | if( num_parts != 6 ) |
---|
| 409 | { |
---|
[84b045d] | 410 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 411 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 412 | return( 0 ); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | http_decode( cmd[4] ); |
---|
[f0cb961] | 416 | imcb_rename_buddy( ic, cmd[3], cmd[4] ); |
---|
[b7d3cc34] | 417 | |
---|
| 418 | st = msn_away_state_by_code( cmd[2] ); |
---|
| 419 | if( !st ) |
---|
| 420 | { |
---|
| 421 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
[b051d39] | 422 | st = msn_away_state_list + 1; |
---|
[b7d3cc34] | 423 | } |
---|
| 424 | |
---|
[b051d39] | 425 | imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | |
---|
| 426 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
| 427 | st->name, NULL ); |
---|
[b7d3cc34] | 428 | } |
---|
| 429 | else if( strcmp( cmd[0], "FLN" ) == 0 ) |
---|
| 430 | { |
---|
| 431 | if( cmd[1] ) |
---|
[6bbb939] | 432 | imcb_buddy_status( ic, cmd[1], 0, NULL, NULL ); |
---|
[b7d3cc34] | 433 | } |
---|
| 434 | else if( strcmp( cmd[0], "NLN" ) == 0 ) |
---|
| 435 | { |
---|
[08995b0] | 436 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 437 | |
---|
| 438 | if( num_parts != 5 ) |
---|
| 439 | { |
---|
[84b045d] | 440 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 441 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 442 | return( 0 ); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | http_decode( cmd[3] ); |
---|
[f0cb961] | 446 | imcb_rename_buddy( ic, cmd[2], cmd[3] ); |
---|
[b7d3cc34] | 447 | |
---|
| 448 | st = msn_away_state_by_code( cmd[1] ); |
---|
| 449 | if( !st ) |
---|
| 450 | { |
---|
| 451 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
[b051d39] | 452 | st = msn_away_state_list + 1; |
---|
[b7d3cc34] | 453 | } |
---|
| 454 | |
---|
[b051d39] | 455 | imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | |
---|
| 456 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
| 457 | st->name, NULL ); |
---|
[b7d3cc34] | 458 | } |
---|
| 459 | else if( strcmp( cmd[0], "RNG" ) == 0 ) |
---|
| 460 | { |
---|
| 461 | struct msn_switchboard *sb; |
---|
| 462 | char *server; |
---|
| 463 | int session, port; |
---|
| 464 | |
---|
| 465 | if( num_parts != 7 ) |
---|
| 466 | { |
---|
[84b045d] | 467 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 468 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 469 | return( 0 ); |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | session = atoi( cmd[1] ); |
---|
| 473 | |
---|
| 474 | server = strchr( cmd[2], ':' ); |
---|
| 475 | if( !server ) |
---|
| 476 | { |
---|
[84b045d] | 477 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 478 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 479 | return( 0 ); |
---|
| 480 | } |
---|
| 481 | *server = 0; |
---|
| 482 | port = atoi( server + 1 ); |
---|
| 483 | server = cmd[2]; |
---|
| 484 | |
---|
| 485 | if( strcmp( cmd[3], "CKI" ) != 0 ) |
---|
| 486 | { |
---|
[84b045d] | 487 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
[c2fb3809] | 488 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 489 | return( 0 ); |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | debug( "Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4] ); |
---|
| 493 | |
---|
[99f929c] | 494 | if( ( sb = msn_sb_create( ic, server, port, cmd[4], session ) ) == NULL ) |
---|
| 495 | { |
---|
| 496 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
| 497 | definitely something serious (we ran out of file descriptors?). */ |
---|
| 498 | imcb_error( ic, "Could not create new switchboard" ); |
---|
| 499 | imc_logout( ic, TRUE ); |
---|
| 500 | return( 0 ); |
---|
| 501 | } |
---|
| 502 | else |
---|
| 503 | { |
---|
| 504 | sb->who = g_strdup( cmd[5] ); |
---|
| 505 | } |
---|
[b7d3cc34] | 506 | } |
---|
| 507 | else if( strcmp( cmd[0], "ADD" ) == 0 ) |
---|
| 508 | { |
---|
| 509 | if( num_parts == 6 && strcmp( cmd[2], "RL" ) == 0 ) |
---|
| 510 | { |
---|
| 511 | GSList *l; |
---|
| 512 | |
---|
| 513 | http_decode( cmd[5] ); |
---|
| 514 | |
---|
| 515 | if( strchr( cmd[4], '@' ) == NULL ) |
---|
| 516 | { |
---|
[84b045d] | 517 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 518 | imc_logout( ic, TRUE ); |
---|
[f0cb961] | 519 | return 0; |
---|
[b7d3cc34] | 520 | } |
---|
| 521 | |
---|
[f0cb961] | 522 | /* We got added by someone. If we don't have this |
---|
| 523 | person in permit/deny yet, inform the user. */ |
---|
[0da65d5] | 524 | for( l = ic->permit; l; l = l->next ) |
---|
[b7d3cc34] | 525 | if( g_strcasecmp( l->data, cmd[4] ) == 0 ) |
---|
[f0cb961] | 526 | return 1; |
---|
[b7d3cc34] | 527 | |
---|
[0da65d5] | 528 | for( l = ic->deny; l; l = l->next ) |
---|
[b7d3cc34] | 529 | if( g_strcasecmp( l->data, cmd[4] ) == 0 ) |
---|
[f0cb961] | 530 | return 1; |
---|
[b7d3cc34] | 531 | |
---|
[0da65d5] | 532 | msn_buddy_ask( ic, cmd[4], cmd[5] ); |
---|
[b7d3cc34] | 533 | } |
---|
[f0cb961] | 534 | else if( num_parts >= 6 && strcmp( cmd[2], "FL" ) == 0 ) |
---|
| 535 | { |
---|
| 536 | http_decode( cmd[5] ); |
---|
| 537 | imcb_add_buddy( ic, cmd[4], NULL ); |
---|
| 538 | imcb_rename_buddy( ic, cmd[4], cmd[5] ); |
---|
| 539 | } |
---|
[b7d3cc34] | 540 | } |
---|
| 541 | else if( strcmp( cmd[0], "OUT" ) == 0 ) |
---|
| 542 | { |
---|
[c2fb3809] | 543 | int allow_reconnect = TRUE; |
---|
| 544 | |
---|
[b7d3cc34] | 545 | if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 ) |
---|
| 546 | { |
---|
[84b045d] | 547 | imcb_error( ic, "Someone else logged in with your account" ); |
---|
[c2fb3809] | 548 | allow_reconnect = FALSE; |
---|
[b7d3cc34] | 549 | } |
---|
| 550 | else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 ) |
---|
| 551 | { |
---|
[84b045d] | 552 | imcb_error( ic, "Terminating session because of server shutdown" ); |
---|
[b7d3cc34] | 553 | } |
---|
| 554 | else |
---|
| 555 | { |
---|
[84b045d] | 556 | imcb_error( ic, "Session terminated by remote server (reason unknown)" ); |
---|
[b7d3cc34] | 557 | } |
---|
| 558 | |
---|
[c2fb3809] | 559 | imc_logout( ic, allow_reconnect ); |
---|
[b7d3cc34] | 560 | return( 0 ); |
---|
| 561 | } |
---|
| 562 | else if( strcmp( cmd[0], "REA" ) == 0 ) |
---|
| 563 | { |
---|
| 564 | if( num_parts != 5 ) |
---|
| 565 | { |
---|
[84b045d] | 566 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 567 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 568 | return( 0 ); |
---|
| 569 | } |
---|
| 570 | |
---|
[c2fb3809] | 571 | if( g_strcasecmp( cmd[3], ic->acc->user ) == 0 ) |
---|
[b7d3cc34] | 572 | { |
---|
[911f2eb] | 573 | set_t *s; |
---|
| 574 | |
---|
[b7d3cc34] | 575 | http_decode( cmd[4] ); |
---|
[0da65d5] | 576 | strncpy( ic->displayname, cmd[4], sizeof( ic->displayname ) ); |
---|
| 577 | ic->displayname[sizeof(ic->displayname)-1] = 0; |
---|
[911f2eb] | 578 | |
---|
[0da65d5] | 579 | if( ( s = set_find( &ic->acc->set, "display_name" ) ) ) |
---|
[911f2eb] | 580 | { |
---|
| 581 | g_free( s->value ); |
---|
| 582 | s->value = g_strdup( cmd[4] ); |
---|
| 583 | } |
---|
[b7d3cc34] | 584 | } |
---|
| 585 | else |
---|
| 586 | { |
---|
| 587 | /* This is not supposed to happen, but let's handle it anyway... */ |
---|
| 588 | http_decode( cmd[4] ); |
---|
[f0cb961] | 589 | imcb_rename_buddy( ic, cmd[3], cmd[4] ); |
---|
[b7d3cc34] | 590 | } |
---|
| 591 | } |
---|
| 592 | else if( strcmp( cmd[0], "IPG" ) == 0 ) |
---|
| 593 | { |
---|
[84b045d] | 594 | imcb_error( ic, "Received IPG command, we don't handle them yet." ); |
---|
[b7d3cc34] | 595 | |
---|
| 596 | md->handler->msglen = atoi( cmd[1] ); |
---|
| 597 | |
---|
| 598 | if( md->handler->msglen <= 0 ) |
---|
| 599 | { |
---|
[84b045d] | 600 | imcb_error( ic, "Syntax error" ); |
---|
[c2fb3809] | 601 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 602 | return( 0 ); |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | else if( isdigit( cmd[0][0] ) ) |
---|
| 606 | { |
---|
| 607 | int num = atoi( cmd[0] ); |
---|
[08995b0] | 608 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
[b7d3cc34] | 609 | |
---|
[84b045d] | 610 | imcb_error( ic, "Error reported by MSN server: %s", err->text ); |
---|
[b7d3cc34] | 611 | |
---|
| 612 | if( err->flags & STATUS_FATAL ) |
---|
| 613 | { |
---|
[c2fb3809] | 614 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 615 | return( 0 ); |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | else |
---|
| 619 | { |
---|
[8ff0a61] | 620 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
[b7d3cc34] | 621 | } |
---|
| 622 | |
---|
| 623 | return( 1 ); |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
| 627 | { |
---|
[0da65d5] | 628 | struct im_connection *ic = data; |
---|
[b7d3cc34] | 629 | char *body; |
---|
| 630 | int blen = 0; |
---|
| 631 | |
---|
| 632 | if( !num_parts ) |
---|
| 633 | return( 1 ); |
---|
| 634 | |
---|
| 635 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
| 636 | { |
---|
| 637 | body += 4; |
---|
| 638 | blen = msglen - ( body - msg ); |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
| 642 | { |
---|
| 643 | if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 ) |
---|
| 644 | { |
---|
| 645 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
| 646 | |
---|
| 647 | if( !ct ) |
---|
| 648 | return( 1 ); |
---|
| 649 | |
---|
| 650 | if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 ) |
---|
| 651 | { |
---|
| 652 | char *mtype; |
---|
| 653 | char *arg1; |
---|
| 654 | |
---|
| 655 | if( !body ) |
---|
| 656 | return( 1 ); |
---|
| 657 | |
---|
| 658 | mtype = msn_findheader( body, "Type:", blen ); |
---|
| 659 | arg1 = msn_findheader( body, "Arg1:", blen ); |
---|
| 660 | |
---|
| 661 | if( mtype && strcmp( mtype, "1" ) == 0 ) |
---|
| 662 | { |
---|
| 663 | if( arg1 ) |
---|
[84b045d] | 664 | imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 ); |
---|
[b7d3cc34] | 665 | } |
---|
| 666 | |
---|
[ec29351] | 667 | g_free( arg1 ); |
---|
| 668 | g_free( mtype ); |
---|
[b7d3cc34] | 669 | } |
---|
| 670 | else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 ) |
---|
| 671 | { |
---|
| 672 | /* We don't care about this profile for now... */ |
---|
| 673 | } |
---|
| 674 | else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 ) |
---|
| 675 | { |
---|
[ec29351] | 676 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
[b7d3cc34] | 677 | { |
---|
[ec29351] | 678 | char *inbox = msn_findheader( body, "Inbox-Unread:", blen ); |
---|
| 679 | char *folders = msn_findheader( body, "Folders-Unread:", blen ); |
---|
| 680 | |
---|
| 681 | if( inbox && folders ) |
---|
| 682 | imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders ); |
---|
| 683 | |
---|
| 684 | g_free( inbox ); |
---|
| 685 | g_free( folders ); |
---|
[b7d3cc34] | 686 | } |
---|
| 687 | } |
---|
| 688 | else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 ) |
---|
| 689 | { |
---|
[ec29351] | 690 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
[b7d3cc34] | 691 | { |
---|
[ec29351] | 692 | char *from = msn_findheader( body, "From-Addr:", blen ); |
---|
| 693 | char *fromname = msn_findheader( body, "From:", blen ); |
---|
| 694 | |
---|
| 695 | if( from && fromname ) |
---|
| 696 | imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from ); |
---|
| 697 | |
---|
| 698 | g_free( from ); |
---|
| 699 | g_free( fromname ); |
---|
[b7d3cc34] | 700 | } |
---|
| 701 | } |
---|
| 702 | else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 ) |
---|
| 703 | { |
---|
| 704 | /* Sorry, but this one really is *USELESS* */ |
---|
| 705 | } |
---|
| 706 | else |
---|
| 707 | { |
---|
| 708 | debug( "Can't handle %s packet from notification server", ct ); |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | g_free( ct ); |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | return( 1 ); |
---|
| 716 | } |
---|
| 717 | |
---|
[d84e2a9] | 718 | static void msn_auth_got_passport_token( struct msn_auth_data *mad ) |
---|
[b7d3cc34] | 719 | { |
---|
[d84e2a9] | 720 | struct im_connection *ic = mad->data; |
---|
| 721 | struct msn_data *md; |
---|
[e6648bf] | 722 | |
---|
[d84e2a9] | 723 | /* Dead connection? */ |
---|
| 724 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
| 725 | return; |
---|
| 726 | |
---|
| 727 | md = ic->proto_data; |
---|
| 728 | if( mad->token ) |
---|
[b7d3cc34] | 729 | { |
---|
[d84e2a9] | 730 | char buf[1024]; |
---|
| 731 | |
---|
| 732 | g_snprintf( buf, sizeof( buf ), "USR %d TWN S %s\r\n", ++md->trId, mad->token ); |
---|
| 733 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 734 | } |
---|
| 735 | else |
---|
| 736 | { |
---|
[d84e2a9] | 737 | imcb_error( ic, "Error during Passport authentication: %s", mad->error ); |
---|
| 738 | imc_logout( ic, TRUE ); |
---|
[b7d3cc34] | 739 | } |
---|
| 740 | } |
---|