[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[21029d0] | 4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* MSN module - Miscellaneous utilities */ |
---|
| 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 "nogaim.h" |
---|
| 27 | #include "msn.h" |
---|
[21029d0] | 28 | #include "md5.h" |
---|
[193dc74] | 29 | #include "soap.h" |
---|
[b7d3cc34] | 30 | #include <ctype.h> |
---|
| 31 | |
---|
[0da65d5] | 32 | int msn_write( struct im_connection *ic, char *s, int len ) |
---|
[b7d3cc34] | 33 | { |
---|
[0da65d5] | 34 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 35 | int st; |
---|
| 36 | |
---|
[523fb23] | 37 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
| 38 | { |
---|
| 39 | write( 2, "->NS:", 5 ); |
---|
| 40 | write( 2, s, len ); |
---|
| 41 | } |
---|
| 42 | |
---|
[b7d3cc34] | 43 | st = write( md->fd, s, len ); |
---|
| 44 | if( st != len ) |
---|
| 45 | { |
---|
[84b045d] | 46 | imcb_error( ic, "Short write() to main server" ); |
---|
[c2fb3809] | 47 | imc_logout( ic, TRUE ); |
---|
[e3413cc] | 48 | return 0; |
---|
[b7d3cc34] | 49 | } |
---|
| 50 | |
---|
[e3413cc] | 51 | return 1; |
---|
[b7d3cc34] | 52 | } |
---|
| 53 | |
---|
[0da65d5] | 54 | int msn_logged_in( struct im_connection *ic ) |
---|
[b7d3cc34] | 55 | { |
---|
[84b045d] | 56 | imcb_connected( ic ); |
---|
[b7d3cc34] | 57 | |
---|
| 58 | return( 0 ); |
---|
| 59 | } |
---|
| 60 | |
---|
[193dc74] | 61 | static char *adlrml_entry( const char *handle_, msn_buddy_flags_t list ) |
---|
| 62 | { |
---|
| 63 | char *domain, handle[strlen(handle_)+1]; |
---|
| 64 | |
---|
| 65 | strcpy( handle, handle_ ); |
---|
| 66 | if( ( domain = strchr( handle, '@' ) ) ) |
---|
| 67 | *(domain |
---|
| 68 | ++) = '\0'; |
---|
| 69 | else |
---|
| 70 | return NULL; |
---|
| 71 | |
---|
| 72 | return g_markup_printf_escaped( "<ml><d n=\"%s\"><c n=\"%s\" l=\"%d\" t=\"1\"/></d></ml>", |
---|
| 73 | domain, handle, list ); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | int msn_buddy_list_add( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname_, const char *group ) |
---|
[b7d3cc34] | 77 | { |
---|
[0da65d5] | 78 | struct msn_data *md = ic->proto_data; |
---|
[193dc74] | 79 | char buf[1024], groupid[8]; |
---|
| 80 | bee_user_t *bu; |
---|
| 81 | struct msn_buddy_data *bd; |
---|
| 82 | char *adl; |
---|
[b7d3cc34] | 83 | |
---|
[6acc033] | 84 | *groupid = '\0'; |
---|
[193dc74] | 85 | #if 0 |
---|
[6acc033] | 86 | if( group ) |
---|
| 87 | { |
---|
| 88 | int i; |
---|
| 89 | for( i = 0; i < md->groupcount; i ++ ) |
---|
| 90 | if( g_strcasecmp( md->grouplist[i], group ) == 0 ) |
---|
| 91 | { |
---|
| 92 | g_snprintf( groupid, sizeof( groupid ), " %d", i ); |
---|
| 93 | break; |
---|
| 94 | } |
---|
[b7d3cc34] | 95 | |
---|
[70ac477] | 96 | if( *groupid == '\0' ) |
---|
| 97 | { |
---|
| 98 | /* Have to create this group, it doesn't exist yet. */ |
---|
| 99 | struct msn_groupadd *ga; |
---|
| 100 | GSList *l; |
---|
| 101 | |
---|
| 102 | for( l = md->grpq; l; l = l->next ) |
---|
| 103 | { |
---|
| 104 | ga = l->data; |
---|
| 105 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
| 106 | break; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | ga = g_new0( struct msn_groupadd, 1 ); |
---|
| 110 | ga->who = g_strdup( who ); |
---|
| 111 | ga->group = g_strdup( group ); |
---|
| 112 | md->grpq = g_slist_prepend( md->grpq, ga ); |
---|
| 113 | |
---|
| 114 | if( l == NULL ) |
---|
| 115 | { |
---|
[4452e69] | 116 | char groupname[strlen(group)+1]; |
---|
| 117 | strcpy( groupname, group ); |
---|
| 118 | http_encode( groupname ); |
---|
[70ac477] | 119 | g_snprintf( buf, sizeof( buf ), "ADG %d %s %d\r\n", ++md->trId, groupname, 0 ); |
---|
| 120 | return msn_write( ic, buf, strlen( buf ) ); |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | /* This can happen if the user's doing lots of adds to a |
---|
| 125 | new group at once; we're still waiting for the server |
---|
| 126 | to confirm group creation. */ |
---|
| 127 | return 1; |
---|
| 128 | } |
---|
| 129 | } |
---|
[b7d3cc34] | 130 | } |
---|
[193dc74] | 131 | #endif |
---|
| 132 | |
---|
| 133 | if( !( bu = bee_user_by_handle( ic->bee, ic, who ) ) || |
---|
| 134 | !( bd = bu->data ) || bd->flags & list ) |
---|
| 135 | return 1; |
---|
[b7d3cc34] | 136 | |
---|
[193dc74] | 137 | bd->flags |= list; |
---|
[b7d3cc34] | 138 | |
---|
[193dc74] | 139 | msn_soap_memlist_edit( ic, who, TRUE, list ); |
---|
| 140 | |
---|
| 141 | if( ( adl = adlrml_entry( who, list ) ) ) |
---|
| 142 | { |
---|
| 143 | g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n%s", |
---|
| 144 | ++md->trId, strlen( adl ), adl ); |
---|
| 145 | g_free( adl ); |
---|
| 146 | |
---|
| 147 | return msn_write( ic, buf, strlen( buf ) ); |
---|
| 148 | } |
---|
[b7d3cc34] | 149 | } |
---|
| 150 | |
---|
[193dc74] | 151 | int msn_buddy_list_remove( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group ) |
---|
[b7d3cc34] | 152 | { |
---|
[0da65d5] | 153 | struct msn_data *md = ic->proto_data; |
---|
[8b01217] | 154 | char buf[1024], groupid[8]; |
---|
[193dc74] | 155 | bee_user_t *bu; |
---|
| 156 | struct msn_buddy_data *bd; |
---|
| 157 | char *adl; |
---|
[b7d3cc34] | 158 | |
---|
[8b01217] | 159 | *groupid = '\0'; |
---|
[193dc74] | 160 | #if 0 |
---|
[8b01217] | 161 | if( group ) |
---|
| 162 | { |
---|
| 163 | int i; |
---|
| 164 | for( i = 0; i < md->groupcount; i ++ ) |
---|
| 165 | if( g_strcasecmp( md->grouplist[i], group ) == 0 ) |
---|
| 166 | { |
---|
| 167 | g_snprintf( groupid, sizeof( groupid ), " %d", i ); |
---|
| 168 | break; |
---|
| 169 | } |
---|
| 170 | } |
---|
[193dc74] | 171 | #endif |
---|
| 172 | |
---|
| 173 | if( !( bu = bee_user_by_handle( ic->bee, ic, who ) ) || |
---|
| 174 | !( bd = bu->data ) || !( bd->flags & list ) ) |
---|
| 175 | return 1; |
---|
| 176 | |
---|
| 177 | bd->flags &= ~list; |
---|
[8b01217] | 178 | |
---|
[193dc74] | 179 | msn_soap_memlist_edit( ic, who, FALSE, list ); |
---|
| 180 | |
---|
| 181 | if( ( adl = adlrml_entry( who, list ) ) ) |
---|
| 182 | { |
---|
| 183 | g_snprintf( buf, sizeof( buf ), "RML %d %zd\r\n%s", |
---|
| 184 | ++md->trId, strlen( adl ), adl ); |
---|
| 185 | g_free( adl ); |
---|
| 186 | |
---|
| 187 | return msn_write( ic, buf, strlen( buf ) ); |
---|
| 188 | } |
---|
[b7d3cc34] | 189 | |
---|
| 190 | return( 0 ); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | struct msn_buddy_ask_data |
---|
| 194 | { |
---|
[0da65d5] | 195 | struct im_connection *ic; |
---|
[b7d3cc34] | 196 | char *handle; |
---|
| 197 | char *realname; |
---|
| 198 | }; |
---|
| 199 | |
---|
[9143aeb] | 200 | static void msn_buddy_ask_yes( void *data ) |
---|
[b7d3cc34] | 201 | { |
---|
[9143aeb] | 202 | struct msn_buddy_ask_data *bla = data; |
---|
| 203 | |
---|
[193dc74] | 204 | msn_buddy_list_add( bla->ic, MSN_BUDDY_AL, bla->handle, bla->realname, NULL ); |
---|
[b7d3cc34] | 205 | |
---|
[17a6ee9] | 206 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
[e6d6047] | 207 | |
---|
[b7d3cc34] | 208 | g_free( bla->handle ); |
---|
| 209 | g_free( bla->realname ); |
---|
| 210 | g_free( bla ); |
---|
| 211 | } |
---|
| 212 | |
---|
[9143aeb] | 213 | static void msn_buddy_ask_no( void *data ) |
---|
[b7d3cc34] | 214 | { |
---|
[9143aeb] | 215 | struct msn_buddy_ask_data *bla = data; |
---|
| 216 | |
---|
[193dc74] | 217 | msn_buddy_list_add( bla->ic, MSN_BUDDY_BL, bla->handle, bla->realname, NULL ); |
---|
[b7d3cc34] | 218 | |
---|
| 219 | g_free( bla->handle ); |
---|
| 220 | g_free( bla->realname ); |
---|
| 221 | g_free( bla ); |
---|
| 222 | } |
---|
| 223 | |
---|
[e5854a8] | 224 | void msn_buddy_ask( bee_user_t *bu ) |
---|
[b7d3cc34] | 225 | { |
---|
[e5854a8] | 226 | struct msn_buddy_ask_data *bla; |
---|
| 227 | struct msn_buddy_data *bd = bu->data; |
---|
[b7d3cc34] | 228 | char buf[1024]; |
---|
| 229 | |
---|
[e5854a8] | 230 | if( ( bd->flags & 30 ) != 8 && ( bd->flags & 30 ) != 16 ) |
---|
| 231 | return; |
---|
| 232 | |
---|
| 233 | bla = g_new0( struct msn_buddy_ask_data, 1 ); |
---|
| 234 | bla->ic = bu->ic; |
---|
| 235 | bla->handle = g_strdup( bu->handle ); |
---|
| 236 | bla->realname = g_strdup( bu->fullname ); |
---|
[b7d3cc34] | 237 | |
---|
| 238 | g_snprintf( buf, sizeof( buf ), |
---|
[5c09a59] | 239 | "The user %s (%s) wants to add you to his/her buddy list.", |
---|
[e5854a8] | 240 | bu->handle, bu->fullname ); |
---|
| 241 | imcb_ask( bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no ); |
---|
[b7d3cc34] | 242 | } |
---|
| 243 | |
---|
| 244 | char *msn_findheader( char *text, char *header, int len ) |
---|
| 245 | { |
---|
| 246 | int hlen = strlen( header ), i; |
---|
| 247 | char *ret; |
---|
| 248 | |
---|
| 249 | if( len == 0 ) |
---|
| 250 | len = strlen( text ); |
---|
| 251 | |
---|
| 252 | i = 0; |
---|
| 253 | while( ( i + hlen ) < len ) |
---|
| 254 | { |
---|
| 255 | /* Maybe this is a bit over-commented, but I just hate this part... */ |
---|
| 256 | if( g_strncasecmp( text + i, header, hlen ) == 0 ) |
---|
| 257 | { |
---|
| 258 | /* Skip to the (probable) end of the header */ |
---|
| 259 | i += hlen; |
---|
| 260 | |
---|
| 261 | /* Find the first non-[: \t] character */ |
---|
| 262 | while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++; |
---|
| 263 | |
---|
| 264 | /* Make sure we're still inside the string */ |
---|
| 265 | if( i >= len ) return( NULL ); |
---|
| 266 | |
---|
| 267 | /* Save the position */ |
---|
| 268 | ret = text + i; |
---|
| 269 | |
---|
| 270 | /* Search for the end of this line */ |
---|
| 271 | while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++; |
---|
| 272 | |
---|
| 273 | /* Make sure we're still inside the string */ |
---|
| 274 | if( i >= len ) return( NULL ); |
---|
| 275 | |
---|
| 276 | /* Copy the found data */ |
---|
| 277 | return( g_strndup( ret, text + i - ret ) ); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | /* This wasn't the header we were looking for, skip to the next line. */ |
---|
| 281 | while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++; |
---|
| 282 | while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++; |
---|
| 283 | |
---|
| 284 | /* End of headers? */ |
---|
[75ec2c8] | 285 | if( ( i >= 4 && strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ) || |
---|
| 286 | ( i >= 2 && ( strncmp( text + i - 2, "\n\n", 2 ) == 0 || |
---|
| 287 | strncmp( text + i - 2, "\r\r", 2 ) == 0 ) ) ) |
---|
[b7d3cc34] | 288 | { |
---|
| 289 | break; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | return( NULL ); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | /* *NOT* thread-safe, but that's not a problem for now... */ |
---|
| 297 | char **msn_linesplit( char *line ) |
---|
| 298 | { |
---|
| 299 | static char **ret = NULL; |
---|
| 300 | static int size = 3; |
---|
| 301 | int i, n = 0; |
---|
| 302 | |
---|
| 303 | if( ret == NULL ) |
---|
| 304 | ret = g_new0( char*, size ); |
---|
| 305 | |
---|
| 306 | for( i = 0; line[i] && line[i] == ' '; i ++ ); |
---|
| 307 | if( line[i] ) |
---|
| 308 | { |
---|
| 309 | ret[n++] = line + i; |
---|
| 310 | for( i ++; line[i]; i ++ ) |
---|
| 311 | { |
---|
| 312 | if( line[i] == ' ' ) |
---|
| 313 | line[i] = 0; |
---|
| 314 | else if( line[i] != ' ' && !line[i-1] ) |
---|
| 315 | ret[n++] = line + i; |
---|
| 316 | |
---|
| 317 | if( n >= size ) |
---|
| 318 | ret = g_renew( char*, ret, size += 2 ); |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | ret[n] = NULL; |
---|
| 322 | |
---|
| 323 | return( ret ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | /* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give |
---|
| 327 | commands, but sometimes they give additional data (payload). This function tries to handle |
---|
| 328 | this all in a nice way and send all data to the right places. */ |
---|
| 329 | |
---|
| 330 | /* Return values: -1: Read error, abort connection. |
---|
| 331 | 0: Command reported error; Abort *immediately*. (The connection does not exist anymore) |
---|
| 332 | 1: OK */ |
---|
| 333 | |
---|
| 334 | int msn_handler( struct msn_handler_data *h ) |
---|
| 335 | { |
---|
| 336 | int st; |
---|
| 337 | |
---|
| 338 | h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 ); |
---|
| 339 | st = read( h->fd, h->rxq + h->rxlen, 1024 ); |
---|
| 340 | h->rxlen += st; |
---|
| 341 | |
---|
| 342 | if( st <= 0 ) |
---|
| 343 | return( -1 ); |
---|
| 344 | |
---|
[523fb23] | 345 | if( getenv( "BITLBEE_DEBUG" ) ) |
---|
| 346 | { |
---|
| 347 | write( 2, "->C:", 4 ); |
---|
| 348 | write( 2, h->rxq + h->rxlen - st, st ); |
---|
| 349 | } |
---|
| 350 | |
---|
[b7d3cc34] | 351 | while( st ) |
---|
| 352 | { |
---|
| 353 | int i; |
---|
| 354 | |
---|
| 355 | if( h->msglen == 0 ) |
---|
| 356 | { |
---|
| 357 | for( i = 0; i < h->rxlen; i ++ ) |
---|
| 358 | { |
---|
| 359 | if( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) |
---|
| 360 | { |
---|
| 361 | char *cmd_text, **cmd; |
---|
| 362 | int count; |
---|
| 363 | |
---|
| 364 | cmd_text = g_strndup( h->rxq, i ); |
---|
| 365 | cmd = msn_linesplit( cmd_text ); |
---|
| 366 | for( count = 0; cmd[count]; count ++ ); |
---|
| 367 | st = h->exec_command( h->data, cmd, count ); |
---|
| 368 | g_free( cmd_text ); |
---|
| 369 | |
---|
| 370 | /* If the connection broke, don't continue. We don't even exist anymore. */ |
---|
| 371 | if( !st ) |
---|
| 372 | return( 0 ); |
---|
| 373 | |
---|
| 374 | if( h->msglen ) |
---|
| 375 | h->cmd_text = g_strndup( h->rxq, i ); |
---|
| 376 | |
---|
| 377 | /* Skip to the next non-emptyline */ |
---|
| 378 | while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++; |
---|
| 379 | |
---|
| 380 | break; |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /* If we reached the end of the buffer, there's still an incomplete command there. |
---|
| 385 | Return and wait for more data. */ |
---|
| 386 | if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' ) |
---|
| 387 | break; |
---|
| 388 | } |
---|
| 389 | else |
---|
| 390 | { |
---|
| 391 | char *msg, **cmd; |
---|
| 392 | int count; |
---|
| 393 | |
---|
| 394 | /* Do we have the complete message already? */ |
---|
| 395 | if( h->msglen > h->rxlen ) |
---|
| 396 | break; |
---|
| 397 | |
---|
| 398 | msg = g_strndup( h->rxq, h->msglen ); |
---|
| 399 | cmd = msn_linesplit( h->cmd_text ); |
---|
| 400 | for( count = 0; cmd[count]; count ++ ); |
---|
| 401 | |
---|
| 402 | st = h->exec_message( h->data, msg, h->msglen, cmd, count ); |
---|
| 403 | g_free( msg ); |
---|
| 404 | g_free( h->cmd_text ); |
---|
| 405 | h->cmd_text = NULL; |
---|
| 406 | |
---|
| 407 | if( !st ) |
---|
| 408 | return( 0 ); |
---|
| 409 | |
---|
| 410 | i = h->msglen; |
---|
| 411 | h->msglen = 0; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | /* More data after this block? */ |
---|
| 415 | if( i < h->rxlen ) |
---|
| 416 | { |
---|
| 417 | char *tmp; |
---|
| 418 | |
---|
| 419 | tmp = g_memdup( h->rxq + i, h->rxlen - i ); |
---|
| 420 | g_free( h->rxq ); |
---|
| 421 | h->rxq = tmp; |
---|
| 422 | h->rxlen -= i; |
---|
| 423 | i = 0; |
---|
| 424 | } |
---|
| 425 | else |
---|
| 426 | /* If not, reset the rx queue and get lost. */ |
---|
| 427 | { |
---|
| 428 | g_free( h->rxq ); |
---|
| 429 | h->rxq = g_new0( char, 1 ); |
---|
| 430 | h->rxlen = 0; |
---|
| 431 | return( 1 ); |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | return( 1 ); |
---|
| 436 | } |
---|
[54794b8] | 437 | |
---|
[46dca11] | 438 | void msn_msgq_purge( struct im_connection *ic, GSList **list ) |
---|
| 439 | { |
---|
| 440 | struct msn_message *m; |
---|
| 441 | GString *ret; |
---|
| 442 | GSList *l; |
---|
[4255320] | 443 | int n = 0; |
---|
[46dca11] | 444 | |
---|
| 445 | l = *list; |
---|
| 446 | if( l == NULL ) |
---|
| 447 | return; |
---|
| 448 | |
---|
| 449 | m = l->data; |
---|
| 450 | ret = g_string_sized_new( 1024 ); |
---|
[43462708] | 451 | g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent " |
---|
[46dca11] | 452 | "messages to %s:", m->who ? m->who : "unknown recipient" ); |
---|
| 453 | |
---|
| 454 | while( l ) |
---|
| 455 | { |
---|
| 456 | m = l->data; |
---|
| 457 | |
---|
[4255320] | 458 | if( strncmp( m->text, "\r\r\r", 3 ) != 0 ) |
---|
| 459 | { |
---|
| 460 | g_string_append_printf( ret, "\n%s", m->text ); |
---|
| 461 | n ++; |
---|
| 462 | } |
---|
[46dca11] | 463 | |
---|
| 464 | g_free( m->who ); |
---|
| 465 | g_free( m->text ); |
---|
| 466 | g_free( m ); |
---|
| 467 | |
---|
| 468 | l = l->next; |
---|
| 469 | } |
---|
| 470 | g_slist_free( *list ); |
---|
| 471 | *list = NULL; |
---|
| 472 | |
---|
[4255320] | 473 | if( n > 0 ) |
---|
| 474 | imcb_log( ic, "%s", ret->str ); |
---|
[46dca11] | 475 | g_string_free( ret, TRUE ); |
---|
| 476 | } |
---|
[e3413cc] | 477 | |
---|
[21029d0] | 478 | /* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */ |
---|
| 479 | char *msn_p11_challenge( char *challenge ) |
---|
| 480 | { |
---|
| 481 | char *output, buf[256]; |
---|
| 482 | md5_state_t md5c; |
---|
| 483 | unsigned char md5Hash[16], *newHash; |
---|
| 484 | unsigned int *md5Parts, *chlStringParts, newHashParts[5]; |
---|
| 485 | long long nHigh = 0, nLow = 0; |
---|
| 486 | int i, n; |
---|
| 487 | |
---|
| 488 | /* Create the MD5 hash */ |
---|
| 489 | md5_init(&md5c); |
---|
| 490 | md5_append(&md5c, (unsigned char*) challenge, strlen(challenge)); |
---|
| 491 | md5_append(&md5c, (unsigned char*) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY)); |
---|
| 492 | md5_finish(&md5c, md5Hash); |
---|
| 493 | |
---|
| 494 | /* Split it into four integers */ |
---|
| 495 | md5Parts = (unsigned int *)md5Hash; |
---|
| 496 | for (i = 0; i < 4; i ++) |
---|
| 497 | { |
---|
[523fb23] | 498 | md5Parts[i] = GUINT32_TO_LE(md5Parts[i]); |
---|
[21029d0] | 499 | |
---|
| 500 | /* & each integer with 0x7FFFFFFF */ |
---|
| 501 | /* and save one unmodified array for later */ |
---|
| 502 | newHashParts[i] = md5Parts[i]; |
---|
| 503 | md5Parts[i] &= 0x7FFFFFFF; |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | /* make a new string and pad with '0' */ |
---|
| 507 | n = g_snprintf(buf, sizeof(buf)-5, "%s%s00000000", challenge, MSNP11_PROD_ID); |
---|
| 508 | /* truncate at an 8-byte boundary */ |
---|
| 509 | buf[n&=~7] = '\0'; |
---|
| 510 | |
---|
| 511 | /* split into integers */ |
---|
| 512 | chlStringParts = (unsigned int *)buf; |
---|
| 513 | |
---|
| 514 | /* this is magic */ |
---|
| 515 | for (i = 0; i < (n / 4) - 1; i += 2) |
---|
| 516 | { |
---|
| 517 | long long temp; |
---|
| 518 | |
---|
[523fb23] | 519 | chlStringParts[i] = GUINT32_TO_LE(chlStringParts[i]); |
---|
| 520 | chlStringParts[i+1] = GUINT32_TO_LE(chlStringParts[i+1]); |
---|
[21029d0] | 521 | |
---|
| 522 | temp = (md5Parts[0] * (((0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF)+nHigh) + md5Parts[1])%0x7FFFFFFF; |
---|
| 523 | nHigh = (md5Parts[2] * (((long long)chlStringParts[i+1]+temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF; |
---|
| 524 | nLow = nLow + nHigh + temp; |
---|
| 525 | } |
---|
| 526 | nHigh = (nHigh+md5Parts[1]) % 0x7FFFFFFF; |
---|
| 527 | nLow = (nLow+md5Parts[3]) % 0x7FFFFFFF; |
---|
| 528 | |
---|
| 529 | newHashParts[0] ^= nHigh; |
---|
| 530 | newHashParts[1] ^= nLow; |
---|
| 531 | newHashParts[2] ^= nHigh; |
---|
| 532 | newHashParts[3] ^= nLow; |
---|
| 533 | |
---|
| 534 | /* swap more bytes if big endian */ |
---|
| 535 | for (i = 0; i < 4; i ++) |
---|
[523fb23] | 536 | newHashParts[i] = GUINT32_TO_LE(newHashParts[i]); |
---|
[21029d0] | 537 | |
---|
| 538 | /* make a string of the parts */ |
---|
| 539 | newHash = (unsigned char *)newHashParts; |
---|
| 540 | |
---|
| 541 | /* convert to hexadecimal */ |
---|
| 542 | output = g_new(char, 33); |
---|
| 543 | for (i = 0; i < 16; i ++) |
---|
| 544 | sprintf(output + i * 2, "%02x", newHash[i]); |
---|
| 545 | |
---|
| 546 | return output; |
---|
| 547 | } |
---|
[ca7de3a] | 548 | |
---|
| 549 | gint msn_domaintree_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
| 550 | { |
---|
| 551 | const char *a = a_, *b = b_; |
---|
| 552 | gint ret; |
---|
| 553 | |
---|
| 554 | if( !( a = strchr( a, '@' ) ) || !( b = strchr( b, '@' ) ) || |
---|
| 555 | ( ret = strcmp( a, b ) ) == 0 ) |
---|
| 556 | ret = strcmp( a_, b_ ); |
---|
| 557 | |
---|
| 558 | return ret; |
---|
| 559 | } |
---|