source: protocols/msn/msn_util.c @ e0e1546

Last change on this file since e0e1546 was e0e1546, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-15T18:46:10Z

Making display name code a bit saner. Apparently PoS MSN is still suffering
from display_name amnesia a little bit though at least with Hotmail accounts.

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