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