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