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