source: protocols/msn/msn_util.c @ 6ddb223

Last change on this file since 6ddb223 was 6ddb223, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-14T19:57:13Z

Separate boilerplate and body of abservice SOAP requests since the former's
the same all the time (and I have to add some more request types).

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