source: protocols/msn/msn_util.c @ 64b6635

Last change on this file since 64b6635 was bae0617, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-03T21:24:58Z

Rearrange things a bit to support multiple NS connections. This is apparently
needed for refreshing auth. tokens.

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