source: protocols/msn/msn_util.c @ 4fc95c5

Last change on this file since 4fc95c5 was 4fc95c5, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-14T21:33:33Z

Add/Remove support.

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