source: protocols/msn/msn_util.c @ e9caacd

Last change on this file since e9caacd was 79bb7e4, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-09-16T17:40:44Z

Online status should be read properly now.

  • Property mode set to 100644
File size: 12.8 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 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
230/* *NOT* thread-safe, but that's not a problem for now... */
231char **msn_linesplit( char *line )
232{
233        static char **ret = NULL;
234        static int size = 3;
235        int i, n = 0;
236       
237        if( ret == NULL )
238                ret = g_new0( char*, size );
239       
240        for( i = 0; line[i] && line[i] == ' '; i ++ );
241        if( line[i] )
242        {
243                ret[n++] = line + i;
244                for( i ++; line[i]; i ++ )
245                {
246                        if( line[i] == ' ' )
247                                line[i] = 0;
248                        else if( line[i] != ' ' && !line[i-1] )
249                                ret[n++] = line + i;
250                       
251                        if( n >= size )
252                                ret = g_renew( char*, ret, size += 2 );
253                }
254        }
255        ret[n] = NULL;
256       
257        return( ret );
258}
259
260/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
261   commands, but sometimes they give additional data (payload). This function tries to handle
262   this all in a nice way and send all data to the right places. */
263
264/* Return values: -1: Read error, abort connection.
265                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
266                   1: OK */
267
268int msn_handler( struct msn_handler_data *h )
269{
270        int st;
271       
272        h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 );
273        st = read( h->fd, h->rxq + h->rxlen, 1024 );
274        h->rxlen += st;
275       
276        if( st <= 0 )
277                return( -1 );
278       
279        if( getenv( "BITLBEE_DEBUG" ) )
280        {
281                write( 2, "->C:", 4 );
282                write( 2, h->rxq + h->rxlen - st, st );
283        }
284       
285        while( st )
286        {
287                int i;
288               
289                if( h->msglen == 0 )
290                {
291                        for( i = 0; i < h->rxlen; i ++ )
292                        {
293                                if( h->rxq[i] == '\r' || h->rxq[i] == '\n' )
294                                {
295                                        char *cmd_text, **cmd;
296                                        int count;
297                                       
298                                        cmd_text = g_strndup( h->rxq, i );
299                                        cmd = msn_linesplit( cmd_text );
300                                        for( count = 0; cmd[count]; count ++ );
301                                        st = h->exec_command( h, cmd, count );
302                                        g_free( cmd_text );
303                                       
304                                        /* If the connection broke, don't continue. We don't even exist anymore. */
305                                        if( !st )
306                                                return( 0 );
307                                       
308                                        if( h->msglen )
309                                                h->cmd_text = g_strndup( h->rxq, i );
310                                       
311                                        /* Skip to the next non-emptyline */
312                                        while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++;
313                                       
314                                        break;
315                                }
316                        }
317                       
318                        /* If we reached the end of the buffer, there's still an incomplete command there.
319                           Return and wait for more data. */
320                        if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' )
321                                break;
322                }
323                else
324                {
325                        char *msg, **cmd;
326                        int count;
327                       
328                        /* Do we have the complete message already? */
329                        if( h->msglen > h->rxlen )
330                                break;
331                       
332                        msg = g_strndup( h->rxq, h->msglen );
333                        cmd = msn_linesplit( h->cmd_text );
334                        for( count = 0; cmd[count]; count ++ );
335                       
336                        st = h->exec_message( h, msg, h->msglen, cmd, count );
337                        g_free( msg );
338                        g_free( h->cmd_text );
339                        h->cmd_text = NULL;
340                       
341                        if( !st )
342                                return( 0 );
343                       
344                        i = h->msglen;
345                        h->msglen = 0;
346                }
347               
348                /* More data after this block? */
349                if( i < h->rxlen )
350                {
351                        char *tmp;
352                       
353                        tmp = g_memdup( h->rxq + i, h->rxlen - i );
354                        g_free( h->rxq );
355                        h->rxq = tmp;
356                        h->rxlen -= i;
357                        i = 0;
358                }
359                else
360                /* If not, reset the rx queue and get lost. */
361                {
362                        g_free( h->rxq );
363                        h->rxq = g_new0( char, 1 );
364                        h->rxlen = 0;
365                        return( 1 );
366                }
367        }
368       
369        return( 1 );
370}
371
372void msn_msgq_purge( struct im_connection *ic, GSList **list )
373{
374        struct msn_message *m;
375        GString *ret;
376        GSList *l;
377        int n = 0;
378       
379        l = *list;
380        if( l == NULL )
381                return;
382       
383        m = l->data;
384        ret = g_string_sized_new( 1024 );
385        g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent "
386                              "messages to %s:", m->who ? m->who : "unknown recipient" );
387       
388        while( l )
389        {
390                m = l->data;
391               
392                if( strncmp( m->text, "\r\r\r", 3 ) != 0 )
393                {
394                        g_string_append_printf( ret, "\n%s", m->text );
395                        n ++;
396                }
397               
398                g_free( m->who );
399                g_free( m->text );
400                g_free( m );
401               
402                l = l->next;
403        }
404        g_slist_free( *list );
405        *list = NULL;
406       
407        if( n > 0 )
408                imcb_log( ic, "%s", ret->str );
409        g_string_free( ret, TRUE );
410}
411
412/* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */
413char *msn_p11_challenge( char *challenge )
414{
415        char *output, buf[256];
416        md5_state_t md5c;
417        unsigned char md5Hash[16], *newHash;
418        unsigned int *md5Parts, *chlStringParts, newHashParts[5];
419        long long nHigh = 0, nLow = 0;
420        int i, n;
421
422        /* Create the MD5 hash */
423        md5_init(&md5c);
424        md5_append(&md5c, (unsigned char*) challenge, strlen(challenge));
425        md5_append(&md5c, (unsigned char*) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY));
426        md5_finish(&md5c, md5Hash);
427
428        /* Split it into four integers */
429        md5Parts = (unsigned int *)md5Hash;
430        for (i = 0; i < 4; i ++)
431        { 
432                md5Parts[i] = GUINT32_TO_LE(md5Parts[i]);
433               
434                /* & each integer with 0x7FFFFFFF */
435                /* and save one unmodified array for later */
436                newHashParts[i] = md5Parts[i];
437                md5Parts[i] &= 0x7FFFFFFF;
438        }
439       
440        /* make a new string and pad with '0' */
441        n = g_snprintf(buf, sizeof(buf)-5, "%s%s00000000", challenge, MSNP11_PROD_ID);
442        /* truncate at an 8-byte boundary */
443        buf[n&=~7] = '\0';
444       
445        /* split into integers */
446        chlStringParts = (unsigned int *)buf;
447       
448        /* this is magic */
449        for (i = 0; i < (n / 4) - 1; i += 2)
450        {
451                long long temp;
452
453                chlStringParts[i]   = GUINT32_TO_LE(chlStringParts[i]);
454                chlStringParts[i+1] = GUINT32_TO_LE(chlStringParts[i+1]);
455
456                temp  = (md5Parts[0] * (((0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF)+nHigh) + md5Parts[1])%0x7FFFFFFF;
457                nHigh = (md5Parts[2] * (((long long)chlStringParts[i+1]+temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
458                nLow  = nLow + nHigh + temp;
459        }
460        nHigh = (nHigh+md5Parts[1]) % 0x7FFFFFFF;
461        nLow = (nLow+md5Parts[3]) % 0x7FFFFFFF;
462       
463        newHashParts[0] ^= nHigh;
464        newHashParts[1] ^= nLow;
465        newHashParts[2] ^= nHigh;
466        newHashParts[3] ^= nLow;
467       
468        /* swap more bytes if big endian */
469        for (i = 0; i < 4; i ++)
470                newHashParts[i] = GUINT32_TO_LE(newHashParts[i]); 
471       
472        /* make a string of the parts */
473        newHash = (unsigned char *)newHashParts;
474       
475        /* convert to hexadecimal */
476        output = g_new(char, 33);
477        for (i = 0; i < 16; i ++)
478                sprintf(output + i * 2, "%02x", newHash[i]);
479       
480        return output;
481}
482
483gint msn_domaintree_cmp( gconstpointer a_, gconstpointer b_ )
484{
485        const char *a = a_, *b = b_;
486        gint ret;
487       
488        if( !( a = strchr( a, '@' ) ) || !( b = strchr( b, '@' ) ) ||
489            ( ret = strcmp( a, b ) ) == 0 )
490                ret = strcmp( a_, b_ );
491       
492        return ret;
493}
494
495struct msn_group *msn_group_by_name( struct im_connection *ic, const char *name )
496{
497        struct msn_data *md = ic->proto_data;
498        GSList *l;
499       
500        for( l = md->groups; l; l = l->next )
501        {
502                struct msn_group *mg = l->data;
503               
504                if( g_strcasecmp( mg->name, name ) == 0 )
505                        return mg;
506        }
507       
508        return NULL;
509}
510
511struct msn_group *msn_group_by_id( struct im_connection *ic, const char *id )
512{
513        struct msn_data *md = ic->proto_data;
514        GSList *l;
515       
516        for( l = md->groups; l; l = l->next )
517        {
518                struct msn_group *mg = l->data;
519               
520                if( g_strcasecmp( mg->id, id ) == 0 )
521                        return mg;
522        }
523       
524        return NULL;
525}
526
527int msn_ns_set_display_name( struct im_connection *ic, const char *value )
528{
529        struct msn_data *md = ic->proto_data;
530        char fn[strlen(value)*3+1];
531       
532        strcpy( fn, value );
533        http_encode( fn );
534       
535        /* Note: We don't actually know if the server accepted the new name,
536           and won't give proper feedback yet if it doesn't. */
537        return msn_ns_write( ic, -1, "PRP %d MFN %s\r\n", ++md->trId, fn );
538}
539
540const char *msn_normalize_handle( const char *handle )
541{
542        if( strncmp( handle, "1:", 2 ) == 0 )
543                return handle + 2;
544        else
545                return handle;
546}
Note: See TracBrowser for help on using the repository browser.