source: protocols/msn/msn_util.c @ 5fecede

Last change on this file since 5fecede was 2528cda, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-08T15:34:49Z

Merging msn-offline branch. A tiny bit of MSNP13, and it works for the first
minute of the session (after that the MSN server finds out the rest of
BitlBee still speaks MSNP8).

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