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